diff options
Diffstat (limited to 'src/libstddjb')
-rw-r--r-- | src/libstddjb/alloc-internal.h | 21 | ||||
-rw-r--r-- | src/libstddjb/alloc.c | 44 | ||||
-rw-r--r-- | src/libstddjb/alloc_0.c | 13 | ||||
-rw-r--r-- | src/libstddjb/alloc_realloc.c | 12 | ||||
-rw-r--r-- | src/libstddjb/stralloc_ready_tuned.c | 2 | ||||
-rw-r--r-- | src/libstddjb/stralloc_shrink.c | 2 |
6 files changed, 16 insertions, 78 deletions
diff --git a/src/libstddjb/alloc-internal.h b/src/libstddjb/alloc-internal.h deleted file mode 100644 index 6fd312e..0000000 --- a/src/libstddjb/alloc-internal.h +++ /dev/null @@ -1,21 +0,0 @@ -/* ISC license. */ - -#ifndef ALLOC_INTERNAL_H -#define ALLOC_INTERNAL_H - -#include <skalibs/sysdeps.h> -#include <skalibs/alloc.h> - -#ifdef SKALIBS_HASMALLOC0 - -#include <stdlib.h> - -#define alloc_0 (aligned_char_ref)malloc(0) - -#else - -extern aligned_char_ref alloc_0 ; - -#endif - -#endif diff --git a/src/libstddjb/alloc.c b/src/libstddjb/alloc.c index 95a5df9..d223a56 100644 --- a/src/libstddjb/alloc.c +++ b/src/libstddjb/alloc.c @@ -1,49 +1,9 @@ /* ISC license. */ -#include <skalibs/sysdeps.h> -#include <errno.h> #include <stdlib.h> #include <skalibs/alloc.h> -#include "alloc-internal.h" -#ifdef DEBUG_ALLOC -# include "buffer.h" -# include "strerr2.h" -# include "lolstdio.h" -# define PLM(...) (bprintf(buffer_2, "%s: debug_alloc: ", PROG), bprintf(buffer_2, __VA_ARGS__), buffer_putflush(buffer_2, "\n", 1)) -#endif - -aligned_char *alloc (size_t n) -{ - aligned_char *p = n ? (aligned_char *)malloc(n) : (aligned_char *)alloc_0 ; -#ifdef DEBUG_ALLOC - static unsigned int counter = 0 ; - PLM("alloc(%u): %p. Allocated: %u", n, p, ++counter) ; -#endif - return p ; -} - -void alloc_free (void *p) -{ - int e = errno ; -#ifdef DEBUG_ALLOC - static unsigned int counter = 0 ; - PLM("alloc_free(%p). Freed: %u", p, ++counter) ; -#endif -#ifndef SKALIBS_HASMALLOC0 - if (p != alloc_0) -#endif - free(p) ; - errno = e ; -} - -int alloc_realloc (aligned_char **x, size_t n) +void *alloc (size_t n) { - aligned_char *y = n ? (aligned_char *)realloc(*x, n) : (free(*x), alloc_0) ; -#ifdef DEBUG_ALLOC - PLM("alloc_realloc(&%p) -> new address = %p", *x, y) ; -#endif - if (!y) return 0 ; - *x = y ; - return 1 ; + return malloc(n ? n : 1) ; } diff --git a/src/libstddjb/alloc_0.c b/src/libstddjb/alloc_0.c deleted file mode 100644 index bdd42a4..0000000 --- a/src/libstddjb/alloc_0.c +++ /dev/null @@ -1,13 +0,0 @@ -/* ISC license. */ - -#include <skalibs/sysdeps.h> -#include <skalibs/alloc.h> -#include "alloc-internal.h" - -#ifndef SKALIBS_HASMALLOC0 - -#define ALIGNMENT 16 -static union { unsigned char blah[ALIGNMENT] ; long double ld ; } const zeroblock ; -aligned_char *alloc_0 = (aligned_char_ref)(&zeroblock) ; - -#endif diff --git a/src/libstddjb/alloc_realloc.c b/src/libstddjb/alloc_realloc.c new file mode 100644 index 0000000..8787291 --- /dev/null +++ b/src/libstddjb/alloc_realloc.c @@ -0,0 +1,12 @@ +/* ISC license. */ + +#include <stdlib.h> +#include <skalibs/alloc.h> + +int alloc_realloc (void **x, size_t n) +{ + void *y = n ? realloc(*x, n) : (free(*x), alloc(0)) ; + if (!y) return 0 ; + *x = y ; + return 1 ; +} diff --git a/src/libstddjb/stralloc_ready_tuned.c b/src/libstddjb/stralloc_ready_tuned.c index d365d6e..f292f22 100644 --- a/src/libstddjb/stralloc_ready_tuned.c +++ b/src/libstddjb/stralloc_ready_tuned.c @@ -19,7 +19,7 @@ int stralloc_ready_tuned (stralloc *sa, size_t n, size_t base, size_t a, size_t } else if (n > sa->a) { - if (!alloc_re(&sa->s, sa->a, t)) return 0 ; + if (!alloc_re((void **)&sa->s, sa->a, t)) return 0 ; sa->a = t ; } return 1 ; diff --git a/src/libstddjb/stralloc_shrink.c b/src/libstddjb/stralloc_shrink.c index 0dceca2..a372084 100644 --- a/src/libstddjb/stralloc_shrink.c +++ b/src/libstddjb/stralloc_shrink.c @@ -7,7 +7,7 @@ int stralloc_shrink (stralloc *sa) { if (sa->a > sa->len) { - if (!alloc_re(&sa->s, sa->a, sa->len)) return 0 ; + if (!alloc_re((void **)&sa->s, sa->a, sa->len)) return 0 ; sa->a = sa->len ; } return 1 ; |