From 47128b7def4954c4cf544924c94f018d88033374 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Sun, 2 Sep 2018 15:32:56 +0200 Subject: misc MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- test.c | 131 ++++++++++++++++++++++++++++++++++++++--------------------------- 1 file changed, 76 insertions(+), 55 deletions(-) (limited to 'test.c') diff --git a/test.c b/test.c index 084359e..b954ccd 100644 --- a/test.c +++ b/test.c @@ -2,10 +2,12 @@ #include "libsimple.h" #include "test.h" #include -#include #undef strndup #undef memdup +#undef memalign +#undef valloc +#undef pvalloc char *argv0 = (char []){""}; @@ -21,6 +23,7 @@ volatile int stderr_real = 0; volatile int stderr_ok = 0; static volatile int custom_malloc = 0; +static volatile void *just_alloced = NULL; size_t @@ -58,59 +61,6 @@ get_allocinfo(void *ptr) } -void * -malloc(size_t size) -{ - size_t alignment = get_pagesize(); - while (alignment < sizeof(long long int)) - alignment *= 2; - while (alignment < sizeof(long double)) - alignment *= 2; - return memalign(alignment, size); -} - - -void * -calloc(size_t nelem, size_t elsize) -{ - struct allocinfo *info; - void *volatile ret; - assert(nelem && elsize); /* unspecified behaviour otherwise */ - if (nelem > SIZE_MAX / elsize) { - errno = ENOMEM; - return NULL; - } - ret = malloc(nelem * elsize); - if (!ret) - return NULL; - memset(ret, 0, nelem * elsize); - info = get_allocinfo(ret); - info->zeroed = nelem * elsize; - return ret; -} - - -void * -realloc(void *ptr, size_t size) -{ - struct allocinfo *info; - void *volatile ret; - size_t n; - assert(size); /* unspecified behaviour otherwise */ - if (!ptr) - return malloc(size); - ret = malloc(size); - if (!ret) - return malloc; - info = get_allocinfo(ret); - n = MIN(size, info->size); - info->zeroed = MIN(n, info->zeroed); - memcpy(ret, ptr, n); - free(ptr); - return ret; -} - - void * memalign(size_t alignment, size_t size) { @@ -156,14 +106,69 @@ memalign(size_t alignment, size_t size) info->zeroed = 0; info->refcount = 1; + just_alloced = ptr; return ptr; enomem: + just_alloced = NULL; errno = ENOMEM; return NULL; } +void * +malloc(size_t size) +{ + size_t alignment = get_pagesize(); + while (alignment < sizeof(long long int)) + alignment *= 2; + while (alignment < sizeof(long double)) + alignment *= 2; + return memalign(alignment, size); +} + + +void * +calloc(size_t nelem, size_t elsize) +{ + struct allocinfo *info; + void *volatile ret; + assert(nelem && elsize); /* unspecified behaviour otherwise */ + if (nelem > SIZE_MAX / elsize) { + errno = ENOMEM; + return NULL; + } + ret = malloc(nelem * elsize); + if (!ret) + return NULL; + memset(ret, 0, nelem * elsize); + info = get_allocinfo(ret); + info->zeroed = nelem * elsize; + return ret; +} + + +void * +realloc(void *ptr, size_t size) +{ + struct allocinfo *info; + void *volatile ret; + size_t n; + assert(size); /* unspecified behaviour otherwise */ + if (!ptr) + return malloc(size); + ret = malloc(size); + if (!ret) + return malloc; + info = get_allocinfo(ret); + n = MIN(size, info->size); + info->zeroed = MIN(n, info->zeroed); + memcpy(ret, ptr, n); + free(ptr); + return ret; +} + + int posix_memalign(void **memptr, size_t alignment, size_t size) { @@ -171,8 +176,9 @@ posix_memalign(void **memptr, size_t alignment, size_t size) void *volatile *volatile ptrp = memptr; assert(!(alignment % sizeof(void *))); assert(ptrp); + errno = 0; *memptr = memalign(alignment, size); - ret = *memptr ? ENOMEM : 0; + ret = errno; errno = saved_errno; return ret; } @@ -237,6 +243,21 @@ free(void *ptr) } +void * +memset(void *s, int c, size_t n) +{ + char *str = s; + struct allocinfo *info; + if (just_alloced && s == just_alloced) { + info = get_allocinfo(s); + info->zeroed = MAX(info->zeroed, n); + } + while (n--) + str[n] = (char)c; + return s; +} + + void exit(int status) { -- cgit v1.2.3-70-g09d2