Inline alloc the SAlloc struct

This commit is contained in:
Nils O. Selåsdal
2025-06-27 23:05:56 +02:00
parent 22e4bbd2f9
commit 01db95842d
+34 -22
View File
@@ -21,16 +21,24 @@ struct Chunk {
struct SAlloc { struct SAlloc {
size_t chunksz; //data size of each chunk size_t chunksz; //data size of each chunk
Chunk *first; //head of the chunk list Chunk *first; //head of the chunk list
Chunk *current; Chunk *current;
}; };
static Chunk * static inline Chunk *
add_chunk(SAlloc *p) alloc_chunk(size_t sz)
{ {
Chunk *newchunk; Chunk *newchunk;
newchunk = malloc(sizeof *newchunk + p->chunksz - (sizeof *newchunk - offsetof(Chunk, data.data))); newchunk = malloc(sizeof *newchunk + sz - (sizeof *newchunk - offsetof(Chunk, data.data)));
if (newchunk == NULL) return newchunk;
}
static Chunk *
add_chunk(SAlloc *p, Chunk *c)
{
Chunk *newchunk;
if (c == NULL)
return NULL; return NULL;
if (p->current != NULL) if (p->current != NULL)
@@ -50,8 +58,10 @@ next_chunk(SAlloc *p)
if (p->current->next) { if (p->current->next) {
newchunk = p->current->next; newchunk = p->current->next;
newchunk->firstfree = 0; newchunk->firstfree = 0;
} else } else {
newchunk = add_chunk(p); Chunk *c = alloc_chunk(p->chunksz);
newchunk = add_chunk(p, c);
}
if (newchunk) if (newchunk)
p->current = newchunk; p->current = newchunk;
@@ -63,19 +73,21 @@ SAlloc *
uc_new_salloc(size_t chunksz) uc_new_salloc(size_t chunksz)
{ {
SAlloc *p; SAlloc *p;
Chunk *first; Chunk *first;
p = malloc(sizeof *p); first = alloc_chunk(sizeof *p + chunksz);
if (p == NULL) if (first == NULL) {
return NULL; return NULL;
}
p = &first->data.data[0];
first->firstfree = sizeof *p;
p->chunksz = chunksz; p->chunksz = chunksz;
p->current = NULL; p->current = NULL;
if ((first = add_chunk(p)) == NULL) { if ((first = add_chunk(p, first)) == NULL) {
free(p); free(p);
return NULL; return NULL;
} }
p->first = p->current = first; p->first = p->current = first;
return p; return p;
@@ -86,7 +98,9 @@ uc_reset_salloc(SAlloc *p)
{ {
Chunk *tmp; Chunk *tmp;
for(tmp = p->first; tmp; tmp = tmp->next) tmp = p->first;
tmp->firstfree = sizeof *p;
for(tmp = tmp->next; tmp; tmp = tmp->next)
tmp->firstfree = 0; tmp->firstfree = 0;
p->current = p->first; p->current = p->first;
@@ -97,9 +111,9 @@ uc_s_alloc(SAlloc *p,size_t sz)
{ {
Chunk *chunk; Chunk *chunk;
void *newmem; void *newmem;
chunk = p->current; chunk = p->current;
if (chunk->firstfree + sz > p->chunksz) { if (chunk->firstfree + sz > p->chunksz) {
if (sz > p->chunksz) if (sz > p->chunksz)
return NULL; return NULL;
@@ -107,7 +121,7 @@ uc_s_alloc(SAlloc *p,size_t sz)
if (chunk == NULL) if (chunk == NULL)
return NULL; return NULL;
} }
newmem = &chunk->data.data[chunk->firstfree]; newmem = &chunk->data.data[chunk->firstfree];
chunk->firstfree += sz; chunk->firstfree += sz;
@@ -119,17 +133,17 @@ uc_s_allocz(SAlloc *p,size_t sz)
{ {
void *newmem = uc_s_alloc(p, sz); void *newmem = uc_s_alloc(p, sz);
if (newmem != NULL) if (newmem != NULL)
memset(newmem, 0, sz); memset(newmem, 0, sz);
return newmem; return newmem;
} }
static void static void
free_chunks(Chunk *chunk) free_chunks(Chunk *chunk)
{ {
Chunk *next; Chunk *next;
for (next = NULL; chunk != NULL; chunk = next) { for (next = NULL; chunk != NULL; chunk = next) {
next = chunk->next; next = chunk->next;
free(chunk); free(chunk);
@@ -140,7 +154,5 @@ void
uc_free_salloc(SAlloc *p) uc_free_salloc(SAlloc *p)
{ {
free_chunks(p->first); free_chunks(p->first);
free(p);
} }