aboutsummaryrefslogtreecommitdiffstats
path: root/test.c
diff options
context:
space:
mode:
authorMattias Andrée <maandree@kth.se>2018-09-02 15:32:56 +0200
committerMattias Andrée <maandree@kth.se>2018-09-02 15:32:56 +0200
commit47128b7def4954c4cf544924c94f018d88033374 (patch)
treec75e6c76c078a095ff691d42a1f0b2d0ea0f0357 /test.c
parentAdd malloc function attribute were appropriate (diff)
downloadlibsimple-47128b7def4954c4cf544924c94f018d88033374.tar.gz
libsimple-47128b7def4954c4cf544924c94f018d88033374.tar.bz2
libsimple-47128b7def4954c4cf544924c94f018d88033374.tar.xz
misc
Signed-off-by: Mattias Andrée <maandree@kth.se>
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)
{