X-Git-Url: https://git.distorted.org.uk/~mdw/mLib/blobdiff_plain/3745e24b5b5526174fda7f83999a0ca301c4c146..34f655c10412ca059d43c78d1e137ab577acdd17:/darray.c diff --git a/darray.c b/darray.c index 0825bca..83150f7 100644 --- a/darray.c +++ b/darray.c @@ -1,6 +1,6 @@ /* -*-c-*- * - * $Id: darray.c,v 1.1 1999/10/22 22:37:26 mdw Exp $ + * $Id: darray.c,v 1.4 1999/11/06 12:40:45 mdw Exp $ * * Dynamically growing dense arrays * @@ -30,6 +30,15 @@ /*----- Revision history --------------------------------------------------* * * $Log: darray.c,v $ + * Revision 1.4 1999/11/06 12:40:45 mdw + * Minor changes to allocation strategy. + * + * Revision 1.3 1999/10/29 22:59:22 mdw + * New array adjustment macros for unsigned arguments. + * + * Revision 1.2 1999/10/28 22:05:28 mdw + * Modify and debug allocation routines. + * * Revision 1.1 1999/10/22 22:37:26 mdw * New dynamic array implementation replaces `dynarray.h'. * @@ -46,7 +55,7 @@ /*----- Magic numbers -----------------------------------------------------*/ -#define DA_INITSZ 64 /* Default size for new array */ +#define DA_INITSZ 16 /* Default size for new array */ #define DA_SLOTS 8 /* Number of preshifted slots */ /*----- Main code ---------------------------------------------------------*/ @@ -100,10 +109,11 @@ void *da_ensure(da_base *b, void *v, size_t sz, size_t n) /* --- Maybe just shunt data around a bit --- * * * If the vector is large enough, then theoretically we could cope by - * moving the objects about in their existing storage. + * moving the objects about in their existing storage. It's not worth + * bothering if there's not actually double the amount of space I need. */ - if (rq < b->sz + b->off) { + if (rq * 2 < b->sz + b->off) { q = p - (b->off - slots) * sz; memmove(q, p, b->len * sz); b->sz += b->off - slots; @@ -112,15 +122,37 @@ void *da_ensure(da_base *b, void *v, size_t sz, size_t n) return (q); } - /* --- Reallocate the array --- */ + /* --- Decide on a new size --- * + * + * There's a minimum possible size for the array which is used if it's + * currently completely empty. Otherwise I choose the smallest power of + * two which is big enough, starting at double the current size. + */ - nsz = v ? b->sz + b->off : DA_INITSZ; + nsz = v ? b->sz + b->off : (DA_INITSZ >> 1); do nsz <<= 1; while (nsz < rq); - q = xmalloc(nsz * sz); - q += slots * sz; - memcpy(q, p, b->len * sz); - if (p) - free(p - b->off * sz); + + /* --- Reallocate the block --- * + * + * If I'm not changing the base offset then it's worth using @realloc@; + * otherwise there'll probably be two calls to @memcpy@ to shunt the data + * around so it's not worth bothering. + */ + + if (p && slots == b->off) { + q = xrealloc(p - b->off * sz, nsz * sz); + q += slots * sz; + } else { + q = xmalloc(nsz * sz); + q += slots * sz; + if (p) { + memcpy(q, p, b->len * sz); + free(p - b->off * sz); + } + } + + /* --- Fill in the other parts of the base structure --- */ + b->off = slots; b->sz = nsz - slots; b->unshift = b->push = 0; @@ -179,10 +211,11 @@ void *da_shunt(da_base *b, void *v, size_t sz, size_t n) /* --- Maybe just shunt data around a bit --- * * * If the vector is large enough, then theoretically we could cope by - * moving the objects about in their existing storage. + * moving the objects about in their existing storage. Again, if there's + * not actually twice the space needed, reallocate the array. */ - if (rq < b->sz + b->off) { + if (rq * 2 < b->sz + b->off) { q = p + (b->sz - slots) * sz; memmove(q, p, b->len * sz); b->off += b->sz - slots; @@ -191,15 +224,37 @@ void *da_shunt(da_base *b, void *v, size_t sz, size_t n) return (q); } - /* --- Reallocate the array --- */ + /* --- Reallocate the array --- * + * + * The neat @realloc@ code doesn't need to be here: the offset changes + * almost all the time -- that's the whole point of this routine! + */ - nsz = v ? b->sz + b->off : DA_INITSZ; + /* --- Decide on a new size --- * + * + * There's a minimum possible size for the array which is used if it's + * currently completely empty. Otherwise I choose the smallest power of + * two which is big enough, starting at double the current size. + */ + + nsz = v ? b->sz + b->off : (DA_INITSZ >> 1); do nsz <<= 1; while (nsz < rq); + + /* --- Reallocate the block --- * + * + * The neat @realloc@ code doesn't need to be here: the offset changes + * almost all the time -- that's the whole point of this routine! + */ + q = xmalloc(nsz * sz); q += (nsz - slots) * sz; - memcpy(q, p, b->len * sz); - if (p) + if (p) { + memcpy(q, p, b->len * sz); free(p - b->off * sz); + } + + /* --- Fill in the other parts of the base structure --- */ + b->off = nsz - slots; b->sz = slots; b->unshift = b->push = 0;