chiark
/
gitweb
/
~mdw
/
mLib
/ commitdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
| commitdiff |
tree
raw
|
patch
|
inline
| side by side (parent:
bcb9998
)
Minor changes to allocation strategy.
author
mdw
<mdw>
Sat, 6 Nov 1999 12:40:45 +0000
(12:40 +0000)
committer
mdw
<mdw>
Sat, 6 Nov 1999 12:40:45 +0000
(12:40 +0000)
darray.c
patch
|
blob
|
blame
|
history
diff --git
a/darray.c
b/darray.c
index d7967c6219387859341085b15398ebf546f8c44d..83150f7c12ec52088213f1c1a4245f41b7b0d9c2 100644
(file)
--- a/
darray.c
+++ b/
darray.c
@@
-1,6
+1,6
@@
/* -*-c-*-
*
/* -*-c-*-
*
- * $Id: darray.c,v 1.
3 1999/10/29 22:59:22
mdw Exp $
+ * $Id: darray.c,v 1.
4 1999/11/06 12:40:45
mdw Exp $
*
* Dynamically growing dense arrays
*
*
* Dynamically growing dense arrays
*
@@
-30,6
+30,9
@@
/*----- Revision history --------------------------------------------------*
*
* $Log: darray.c,v $
/*----- 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.3 1999/10/29 22:59:22 mdw
* New array adjustment macros for unsigned arguments.
*
@@
-106,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
/* --- 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;
q = p - (b->off - slots) * sz;
memmove(q, p, b->len * sz);
b->sz += b->off - slots;
@@
-118,15
+122,23
@@
void *da_ensure(da_base *b, void *v, size_t sz, size_t n)
return (q);
}
return (q);
}
- /* ---
Reallocate the array
--- *
+ /* ---
Decide on a new size
--- *
*
*
- *
If the offset isn't changing, it's sensible to use @realloc@ if
- *
available. Otherwise the overhead of copying all the data twice
- *
probably isn't worth it
.
+ *
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);
*/
nsz = v ? b->sz + b->off : (DA_INITSZ >> 1);
do nsz <<= 1; while (nsz < rq);
+
+ /* --- 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;
if (p && slots == b->off) {
q = xrealloc(p - b->off * sz, nsz * sz);
q += slots * sz;
@@
-138,6
+150,9
@@
void *da_ensure(da_base *b, void *v, size_t sz, size_t n)
free(p - b->off * 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;
b->off = slots;
b->sz = nsz - slots;
b->unshift = b->push = 0;
@@
-196,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
/* --- 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;
q = p + (b->sz - slots) * sz;
memmove(q, p, b->len * sz);
b->off += b->sz - slots;
@@
-214,14
+230,31
@@
void *da_shunt(da_base *b, void *v, size_t sz, size_t n)
* almost all the time -- that's the whole point of this routine!
*/
* almost all the time -- that's the whole point of this routine!
*/
+ /* --- 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);
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;
if (p) {
memcpy(q, p, b->len * sz);
free(p - b->off * sz);
}
q = xmalloc(nsz * sz);
q += (nsz - 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 = nsz - slots;
b->sz = slots;
b->unshift = b->push = 0;
b->off = nsz - slots;
b->sz = slots;
b->unshift = b->push = 0;