1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
|
/*
version 20220222
*/
#include <stdlib.h>
#include <errno.h>
#include "log.h"
#include "alloc.h"
static unsigned char space[alloc_STATICSPACE]
__attribute__((aligned(alloc_ALIGNMENT)));
static unsigned long long avail = sizeof space;
static unsigned long long allocated = 0;
static void **ptr = 0;
static unsigned long long ptrlen = 0;
static unsigned long long ptralloc = 0;
static int ptr_add(void *x) {
void **newptr;
unsigned long long i;
if (!x) return 1;
if (ptrlen + 1 > ptralloc) {
while (ptrlen + 1 > ptralloc) ptralloc = 2 * ptralloc + 1;
newptr = (void **) malloc(ptralloc * sizeof(void *));
if (!newptr) return 0;
if (ptr) {
for (i = 0; i < ptrlen; ++i) newptr[i] = ptr[i];
free(ptr);
}
ptr = newptr;
}
ptr[ptrlen++] = x;
return 1;
}
static int ptr_remove(void *x) {
unsigned long long i;
for (i = 0; i < ptrlen; ++i) {
if (ptr[i] == x) goto ok;
}
return 0;
ok:
--ptrlen;
ptr[i] = ptr[ptrlen];
return 1;
}
static void cleanup(void *xv, unsigned long long xlen) {
volatile unsigned long *x = (volatile unsigned long *) xv;
xlen /= sizeof(unsigned long);
while (xlen-- > 0) *x++ = 0;
__asm__ __volatile__("" : : "r"(xv) : "memory");
}
void *alloc(long long norig) {
unsigned char *x;
unsigned long long i, n = norig;
if (norig < 0) {
log_e3("alloc(", lognum(norig), ") ... failed, < 0");
goto inval;
}
if (n == 0) {
log_t3("alloc(0), will allocate ", lognum(alloc_ALIGNMENT), " bytes");
n = alloc_ALIGNMENT;
}
n = ((n + alloc_ALIGNMENT - 1) / alloc_ALIGNMENT) * alloc_ALIGNMENT;
if (n <= avail) {
avail -= n;
log_t3("alloc(", lognum(norig), ") ... ok, static");
return (void *) (space + avail);
}
n += alloc_ALIGNMENT;
allocated += n;
if (n != (unsigned long long) (size_t) n) {
log_e3("alloc(", lognum(norig), ") ... failed, size_t overflow");
goto nomem;
}
x = (unsigned char *) malloc(n);
if (!x) {
log_e3("alloc(", lognum(norig), ") ... failed, malloc() failed");
goto nomem;
}
cleanup(x, n);
for (i = 0; i < alloc_ALIGNMENT; ++i) {
*x++ = n;
n >>= 8;
}
if (!ptr_add(x)) {
log_e3("alloc(", lognum(norig), ") ... failed, malloc() failed");
goto nomem;
}
log_t5("alloc(", lognum(norig), ") ... ok, using malloc(), total ",
lognum(allocated), " bytes");
return (void *) x;
nomem:
errno = ENOMEM;
return (void *) 0;
inval:
errno = EINVAL;
return (void *) 0;
}
void alloc_free(void *xv) {
unsigned char *x = xv;
unsigned long long i, n = 0;
if (!x) {
log_w1("alloc_free(0)");
return;
}
if (x >= space)
if (x < space + sizeof space) return;
ptr_remove(x);
for (i = 0; i < alloc_ALIGNMENT; ++i) {
n <<= 8;
n |= *--x;
}
cleanup(x, n);
free(x);
}
void alloc_freeall(void) {
while (ptrlen > 0) { alloc_free(ptr[0]); }
if (ptr) {
free(ptr);
ptr = 0;
ptrlen = ptralloc = 0;
}
cleanup(space, sizeof space);
}
|