Changeset 1649 for zzuf


Ignore:
Timestamp:
Jan 11, 2007, 11:21:11 AM (13 years ago)
Author:
Sam Hocevar
Message:
  • Better realloc dummy implementation.
File:
1 edited

Legend:

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

    r1648 r1649  
    5454#include "fd.h"
    5555
    56 /* TODO: mremap, maybe brk/sbrk */
     56/* TODO: mremap, maybe brk/sbrk (haha) */
    5757
    5858/* Library functions that we divert */
     
    107107}
    108108
    109 /* 32k of ugly static memory for programs that call us *before* we’re
    110  * initialised */
    111 static uint64_t dummy_buffer[4096];
     109/* We need a static memory buffer because some functions call memory
     110 * allocation routines before our library is loaded. Hell, even dlsym()
     111 * calls calloc(), so we need to do something about it */
     112#define DUMMY_BYTES 655360 /* 640 kB ought to be enough for anybody */
     113static uint64_t dummy_buffer[DUMMY_BYTES / 8];
    112114static int dummy_offset = 0;
     115#define DUMMY_START ((uintptr_t)dummy_buffer)
     116#define DUMMY_STOP ((uintptr_t)dummy_buffer + DUMMY_BYTES)
    113117
    114118void *calloc(size_t nmemb, size_t size)
     
    117121    if(!calloc_orig)
    118122    {
    119         int i = (nmemb * size + 7) / 8;
    120123        ret = dummy_buffer + dummy_offset;
    121         dummy_offset += i;
    122         /* Calloc says we must zero the data */
    123         memset(ret, 0, size);
     124        memset(ret, 0, (nmemb * size + 7) / 8);
     125        dummy_offset += (nmemb * size + 7) / 8;
    124126        return ret;
    125127    }
     
    135137    if(!malloc_orig)
    136138    {
    137         int i = (size + 7) / 8;
    138139        ret = dummy_buffer + dummy_offset;
    139         dummy_offset += i;
     140        dummy_offset += (size + 7) / 8;
    140141        return ret;
    141142    }
     
    148149void free(void *ptr)
    149150{
    150     if((uintptr_t)ptr >= (uintptr_t)dummy_buffer
    151        && (uintptr_t)ptr <= (uintptr_t)dummy_buffer + sizeof(dummy_buffer))
     151    if((uintptr_t)ptr >= DUMMY_START && (uintptr_t)ptr < DUMMY_STOP)
    152152        return;
    153153    LOADSYM(free);
     
    158158{
    159159    void *ret;
    160     if((uintptr_t)ptr >= (uintptr_t)dummy_buffer
    161        && (uintptr_t)ptr <= (uintptr_t)dummy_buffer + sizeof(dummy_buffer))
    162         return ptr; /* FIXME: who would call realloc() so early? */
     160    if((uintptr_t)ptr >= DUMMY_START && (uintptr_t)ptr < DUMMY_STOP)
     161    {
     162        ret = dummy_buffer + dummy_offset;
     163        memcpy(ret, ptr, size);
     164        dummy_offset += (size + 7) * 8;
     165        return ret;
     166    }
    163167    LOADSYM(realloc);
    164168    ret = realloc_orig(ptr, size);
Note: See TracChangeset for help on using the changeset viewer.