tweak bag

This commit is contained in:
Jethro Grassie 2020-09-03 05:01:58 -04:00
parent 2efa10a194
commit 9184050df2
No known key found for this signature in database
GPG key ID: DE8ED755616565BB

View file

@ -97,21 +97,23 @@ gbag_get(gbag_t *gb)
assert(gb && gb->max && gb->b); assert(gb && gb->max && gb->b);
char *end = gb->b + (gb->max * gb->z); char *end = gb->b + (gb->max * gb->z);
char *from = gb->n; char *from = gb->n;
size_t nm, cz; size_t nc, oc, ocz;
char *b = NULL; char *b = NULL;
char *rv = NULL;
if (gb->ref == gb->max) if (gb->ref == gb->max)
goto grow; goto grow;
scan: scan:
do while(gb->n < end)
{ {
if (!*gb->n) if (!*gb->n)
{ {
gb->ref++; gb->ref++;
return gb->n; rv = gb->n;
gb->n += gb->z;
return rv;
} }
gb->n += gb->z; gb->n += gb->z;
} }
while(gb->n < end);
if (from != gb->b) if (from != gb->b)
{ {
end = from; end = from;
@ -122,19 +124,21 @@ scan:
else else
{ {
grow: grow:
cz = gb->max * gb->z; ocz = gb->max * gb->z;
nm = gb->max << 1; oc = gb->max;
b = (char*) realloc(gb->b, nm * gb->z); nc = gb->max << 1;
if (b == NULL) b = (char*) realloc(gb->b, nc * gb->z);
if (!b)
return NULL; return NULL;
memset(b + cz, 0, cz); rv = b + ocz;
gb->max = nm; memset(rv, 0, ocz);
if (gb->mv && gb->b != b) if (gb->mv && gb->b != b)
gb->mv(b, cz); gb->mv(b, oc);
gb->b = b; gb->max = nc;
gb->n = b + cz;
gb->ref++; gb->ref++;
return gb->n; gb->b = b;
gb->n = rv + gb->z;
return rv;
} }
return NULL; return NULL;
} }
@ -188,13 +192,13 @@ gbag_first(gbag_t *gb)
char *s = gb->b; char *s = gb->b;
char *e = gb->b + (gb->max * gb->z); char *e = gb->b + (gb->max * gb->z);
gb->ni = s; gb->ni = s;
do while (s<e)
{ {
if (*s) if (*s)
return s; return s;
s += gb->z; s += gb->z;
gb->ni = s;
} }
while (s<e);
return NULL; return NULL;
} }