Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You should take a look at the source code of realloc() from the libc you are using. From there, it should be easy to see the path followed when it can increase the size in place, and the else case where a new pointer will be returned instead. Then, use that to code your own tryrealloc() function.</p> <p>For example, this is the realloc() source code from uclibc : <a href="http://cristi.indefero.net/p/uClibc-cristi/source/tree/nptl/libc/stdlib/malloc/realloc.c" rel="nofollow">http://cristi.indefero.net/p/uClibc-cristi/source/tree/nptl/libc/stdlib/malloc/realloc.c</a></p> <pre><code>24 void * 25 realloc (void *mem, size_t new_size) 26 { ... 57 if (new_size &gt; size) 58 /* Grow the block. */ 59 { 60 size_t extra = new_size - size; 61 62 __heap_lock (&amp;__malloc_heap_lock); 63 extra = __heap_alloc_at (&amp;__malloc_heap, base_mem + size, extra); 64 __heap_unlock (&amp;__malloc_heap_lock); 65 66 if (extra) 67 /* Record the changed size. */ 68 MALLOC_SET_SIZE (base_mem, size + extra); 69 else 70 /* Our attempts to extend MEM in place failed, just 71 allocate-and-copy. */ 72 { 73 void *new_mem = malloc (new_size - MALLOC_HEADER_SIZE); 74 if (new_mem) 75 { 76 memcpy (new_mem, mem, size - MALLOC_HEADER_SIZE); 77 free (mem); 78 } 79 mem = new_mem; 80 } 81 } ... </code></pre> <p>I have removed some parts for clarity. But you can see that at line 66, it check if it can simply increase the memory for the current pointer. This is the part you want to keep. The <code>else</code> case starting at line 69 is to handle the case where the old memory will be freed and a new pointer will be returned. This is the part you want to kick out and handle it differently. From what you are saying, I guess you will only want to remove line 77, where it does the free.</p> <p>If you go this way, remember that you will have to manually either free the old pointer or the new one, as both will now be valid (and you don't want a memory leak).</p> <p>Also, this is for uclibc. If you are already using a different libc, you should base your new <code>tryrealloc()</code> function on the <code>realloc()</code> function of that libc.</p> <p><strong>EDIT:</strong> You must be careful if you use this approach. You will be basing your solution on the internals of the memory manager, so things can change and be different between the various libc implementations, but also between different versions of the same libc. So do that with the appropriate care and warning in mind.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload