Skip to content

Commit

Permalink
Cleanse more.
Browse files Browse the repository at this point in the history
  • Loading branch information
tadd committed Oct 12, 2024
1 parent 2a7b17e commit 8853f33
Showing 1 changed file with 8 additions and 14 deletions.
22 changes: 8 additions & 14 deletions scary.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,21 +64,15 @@ void scary_free(void *p)
free(get(p));
}

static void maybe_resize(Scary **pary)
{
Scary *ary = *pary;
if (ary->capacity > ary->length * ary->elem_size)
return;
ary->capacity *= 2;
*pary = xrealloc(ary, sizeof(Scary) + ary->capacity);
}

static Scary *maybe_resize_and_get(void *p)
static Scary *maybe_resize(void *p)
{
const void **pp = p;
Scary *ary = get(*pp);
maybe_resize(&ary);
*pp = opaque(ary);
if (ary->capacity <= ary->length * ary->elem_size) {
ary->capacity *= 2;
ary = xrealloc(ary, sizeof(Scary) + ary->capacity);
*pp = opaque(ary);
}
return ary;
}

Expand All @@ -91,7 +85,7 @@ size_t scary_length(const void *p)
#define DEF_PUSH_VARIANT2(type, suffix) \
void scary_push_##suffix(type **p, type elem) \
{ \
Scary *ary = maybe_resize_and_get(p); \
Scary *ary = maybe_resize(p); \
type *sp = (type *) ary->space; \
sp[ary->length++] = elem; \
}
Expand All @@ -110,7 +104,7 @@ DEF_PUSH_VARIANT1(char)

void scary_push_ptr(void *p, const void *elem)
{
Scary *ary = maybe_resize_and_get(p);
Scary *ary = maybe_resize(p);
const void **sp = (const void **) ary->space;
sp[ary->length++] = elem;
}
Expand Down

0 comments on commit 8853f33

Please sign in to comment.