fix(memory): increase memory counters by the actual size of reallocated memory

On OSX, we had a test failure on memory module. The problem was in
the realloc function (line 38):

    void * realloc(void * ptr, size_t sz) {
        size_t old_sz = malloc_size(ptr);
        g_global_memory.dec(old_sz);
        g_global_memory.inc(sz);
        g_thread_memory.dec(old_sz);
        g_thread_memory.inc(sz);
        void * r = realloc_core(ptr, sz);
        if (r || sz == 0)
            return r;
        else
            ...

The size of r could be bigger than sz. For instance,

    |ptr| = 40 but |r| = 48

In the current code, here we only increase counters by 40.
But later when we free it, we decrease them by 48, and this
caused the problem, underflow of an unsigned counter in
g_global_memory.
This commit is contained in:
Soonho Kong 2013-09-26 00:31:30 -07:00
parent 4bae715350
commit a2a5a77a44

View file

@ -135,10 +135,11 @@ void * malloc(size_t sz) {
void * realloc(void * ptr, size_t sz) { void * realloc(void * ptr, size_t sz) {
size_t old_sz = malloc_size(ptr); size_t old_sz = malloc_size(ptr);
g_global_memory.dec(old_sz); g_global_memory.dec(old_sz);
g_global_memory.inc(sz);
g_thread_memory.dec(old_sz); g_thread_memory.dec(old_sz);
g_thread_memory.inc(sz);
void * r = realloc_core(ptr, sz); void * r = realloc_core(ptr, sz);
size_t new_sz = malloc_size(r);
g_global_memory.inc(new_sz);
g_thread_memory.inc(new_sz);
if (r || sz == 0) if (r || sz == 0)
return r; return r;
else else