aboutsummaryrefslogtreecommitdiffstats
path: root/test.c
diff options
context:
space:
mode:
Diffstat (limited to 'test.c')
-rw-r--r--test.c131
1 files changed, 76 insertions, 55 deletions
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 <sys/syscall.h>
-#include <malloc.h>
#undef strndup
#undef memdup
+#undef memalign
+#undef valloc
+#undef pvalloc
char *argv0 = (char []){"<test>"};
@@ -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
@@ -59,59 +62,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)
{
struct allocinfo *info;
@@ -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)
{