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
+25 -13
View File
@@ -24,13 +24,21 @@ struct SAlloc {
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;
@@ -64,14 +74,16 @@ 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;
} }
@@ -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;
@@ -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);
} }