Changeset 1646 for zzuf


Ignore:
Timestamp:
Jan 10, 2007, 7:11:13 PM (13 years ago)
Author:
Sam Hocevar
Message:
  • Allow several calls to malloc()/calloc() in the library preloading stage.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • zzuf/trunk/src/load-mem.c

    r1645 r1646  
    109109/* 32k of ugly static memory for programs that call us *before* we’re
    110110 * initialised */
    111 uint64_t dummy_buffer[4096];
     111static uint64_t dummy_buffer[4096];
     112static int dummy_offset = 0;
    112113
    113114void *calloc(size_t nmemb, size_t size)
     
    116117    if(!_zz_ready)
    117118    {
     119        int i = (nmemb * size + 7) / 8;
     120        ret = dummy_buffer + dummy_offset;
     121        dummy_offset += i;
    118122        /* Calloc says we must zero the data */
    119         int i = (nmemb * size + 7) / 8;
    120         while(i--)
    121             dummy_buffer[i] = 0;
    122         return dummy_buffer;
     123        memset(ret, 0, size);
     124        return ret;
    123125    }
    124126    ret = calloc_orig(nmemb, size);
     
    132134    void *ret;
    133135    if(!_zz_ready)
    134         return dummy_buffer;
     136    {
     137        int i = (size + 7) / 8;
     138        ret = dummy_buffer + dummy_offset;
     139        dummy_offset += i;
     140        return ret;
     141    }
    135142    ret = malloc_orig(size);
    136143    if(ret == NULL && _zz_memory && errno == ENOMEM)
     
    141148void free(void *ptr)
    142149{
    143     if(ptr == dummy_buffer)
     150    if((uintptr_t)ptr >= (uintptr_t)dummy_buffer
     151       && (uintptr_t)ptr <= (uintptr_t)dummy_buffer + sizeof(dummy_buffer))
    144152        return;
    145153    if(!_zz_ready)
     
    151159{
    152160    void *ret;
    153     if(ptr == dummy_buffer)
    154         return ptr;
     161    if((uintptr_t)ptr >= (uintptr_t)dummy_buffer
     162       && (uintptr_t)ptr <= (uintptr_t)dummy_buffer + sizeof(dummy_buffer))
     163        return ptr; /* FIXME: who would call realloc() so early? */
    155164    if(!_zz_ready)
    156165        LOADSYM(realloc);
Note: See TracChangeset for help on using the changeset viewer.