fixup! Added memory backend

This commit is contained in:
2025-12-18 13:31:56 +01:00
parent 4ed78e3a7d
commit 6ea3af5727
2 changed files with 13 additions and 57 deletions

View File

@@ -18,12 +18,14 @@
/* ------------------------------ calloc ------------------------------ */ /* ------------------------------ calloc ------------------------------ */
/*
* Our libc_be_mem allocator does not wrap calloc.
* So we need to implement calloc only using the public-facing malloc call without relying on libc
* internals.
*/
void* calloc(size_t n_elements, size_t elem_size) void* calloc(size_t n_elements, size_t elem_size)
{ {
mchunkptr p; size_t size;
unsigned long clearsize;
unsigned long nclears;
size_t size, *d;
void* mem; void* mem;
@@ -35,59 +37,9 @@ void* calloc(size_t n_elements, size_t elem_size)
return NULL; return NULL;
} }
__MALLOC_LOCK;
mem = malloc(size); mem = malloc(size);
if (mem != 0) { if (mem != 0)
p = mem2chunk(mem); memset(mem, 0, size);
if (!chunk_is_mmapped(p))
{
/*
Unroll clear of <= 36 bytes (72 if 8byte sizes)
We know that contents have an odd number of
size_t-sized words; minimally 3.
*/
d = (size_t*)mem;
clearsize = chunksize(p) - (sizeof(size_t));
nclears = clearsize / sizeof(size_t);
assert(nclears >= 3);
if (nclears > 9)
memset(d, 0, clearsize);
else {
*(d+0) = 0;
*(d+1) = 0;
*(d+2) = 0;
if (nclears > 4) {
*(d+3) = 0;
*(d+4) = 0;
if (nclears > 6) {
*(d+5) = 0;
*(d+6) = 0;
if (nclears > 8) {
*(d+7) = 0;
*(d+8) = 0;
}
}
}
}
}
#if 0
else
{
/* Standard unix mmap using /dev/zero clears memory so calloc
* doesn't need to actually zero anything....
*/
d = (size_t*)mem;
/* Note the additional (sizeof(size_t)) */
clearsize = chunksize(p) - 2*(sizeof(size_t));
memset(d, 0, clearsize);
}
#endif
}
__MALLOC_UNLOCK;
return mem; return mem;
} }

View File

@@ -147,7 +147,11 @@ typedef struct
// vector registers in the TCB and we want the storage to be aligned. // vector registers in the TCB and we want the storage to be aligned.
// unfortunately there isn't yet a type for these values and hence no // unfortunately there isn't yet a type for these values and hence no
// 32-byte alignment requirement. Make this explicit, for now. // 32-byte alignment requirement. Make this explicit, for now.
# define TLS_TCB_ALIGN 32 // # define TLS_TCB_ALIGN 32
// The libc_be_mem allocator does not wrap memalign.
// This hack avoids libpthread actually using memalign but is very likely not
// correct and might lead to problems later on.
# define TLS_TCB_ALIGN 4
/* The TCB can have any size and the memory following the address the /* The TCB can have any size and the memory following the address the
thread pointer points to is unspecified. Allocate the TCB there. */ thread pointer points to is unspecified. Allocate the TCB there. */