Inline alloc the SAlloc struct
This commit is contained in:
+25
-13
@@ -24,13 +24,21 @@ struct SAlloc {
|
||||
Chunk *current;
|
||||
};
|
||||
|
||||
static Chunk *
|
||||
add_chunk(SAlloc *p)
|
||||
static inline Chunk *
|
||||
alloc_chunk(size_t sz)
|
||||
{
|
||||
Chunk *newchunk;
|
||||
|
||||
newchunk = malloc(sizeof *newchunk + p->chunksz - (sizeof *newchunk - offsetof(Chunk, data.data)));
|
||||
if (newchunk == NULL)
|
||||
newchunk = malloc(sizeof *newchunk + sz - (sizeof *newchunk - offsetof(Chunk, data.data)));
|
||||
return newchunk;
|
||||
}
|
||||
|
||||
static Chunk *
|
||||
add_chunk(SAlloc *p, Chunk *c)
|
||||
{
|
||||
Chunk *newchunk;
|
||||
|
||||
if (c == NULL)
|
||||
return NULL;
|
||||
|
||||
if (p->current != NULL)
|
||||
@@ -50,8 +58,10 @@ next_chunk(SAlloc *p)
|
||||
if (p->current->next) {
|
||||
newchunk = p->current->next;
|
||||
newchunk->firstfree = 0;
|
||||
} else
|
||||
newchunk = add_chunk(p);
|
||||
} else {
|
||||
Chunk *c = alloc_chunk(p->chunksz);
|
||||
newchunk = add_chunk(p, c);
|
||||
}
|
||||
|
||||
if (newchunk)
|
||||
p->current = newchunk;
|
||||
@@ -64,14 +74,16 @@ uc_new_salloc(size_t chunksz)
|
||||
{
|
||||
SAlloc *p;
|
||||
Chunk *first;
|
||||
p = malloc(sizeof *p);
|
||||
if (p == NULL)
|
||||
first = alloc_chunk(sizeof *p + chunksz);
|
||||
if (first == NULL) {
|
||||
return NULL;
|
||||
|
||||
}
|
||||
p = &first->data.data[0];
|
||||
first->firstfree = sizeof *p;
|
||||
p->chunksz = chunksz;
|
||||
p->current = NULL;
|
||||
|
||||
if ((first = add_chunk(p)) == NULL) {
|
||||
if ((first = add_chunk(p, first)) == NULL) {
|
||||
free(p);
|
||||
return NULL;
|
||||
}
|
||||
@@ -86,7 +98,9 @@ uc_reset_salloc(SAlloc *p)
|
||||
{
|
||||
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;
|
||||
|
||||
p->current = p->first;
|
||||
@@ -140,7 +154,5 @@ void
|
||||
uc_free_salloc(SAlloc *p)
|
||||
{
|
||||
free_chunks(p->first);
|
||||
free(p);
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user