summaryrefslogtreecommitdiff
path: root/lib/resource.c
diff options
context:
space:
mode:
authorOndrej Zajicek <santiago@crfreenet.org>2013-09-10 12:09:36 +0200
committerOndrej Zajicek <santiago@crfreenet.org>2013-09-10 12:09:36 +0200
commitbf139664aa2ae9956b520ba4813bb6e03bf1a3e8 (patch)
tree33381e1e2214b32fcb169def039a891b0db58509 /lib/resource.c
parentbff9ce5130d16af2fd802d42bdb2bff00980c9ae (diff)
Initial BFD commit, work in progress.
Diffstat (limited to 'lib/resource.c')
-rw-r--r--lib/resource.c29
1 files changed, 21 insertions, 8 deletions
diff --git a/lib/resource.c b/lib/resource.c
index 42243aa2..775b0c53 100644
--- a/lib/resource.c
+++ b/lib/resource.c
@@ -366,21 +366,21 @@ mb_allocz(pool *p, unsigned size)
/**
* mb_realloc - reallocate a memory block
- * @p: pool
* @m: memory block
* @size: new size of the block
*
* mb_realloc() changes the size of the memory block @m to a given size.
* The contents will be unchanged to the minimum of the old and new sizes;
- * newly allocated memory will be uninitialized. If @m is NULL, the call
- * is equivalent to mb_alloc(@p, @size).
+ * newly allocated memory will be uninitialized. Contrary to realloc()
+ * behavior, @m must be non-NULL, because the resource pool is inherited
+ * from it.
*
* Like mb_alloc(), mb_realloc() also returns a pointer to the memory
- * chunk , not to the resource, hence you have to free it using
+ * chunk, not to the resource, hence you have to free it using
* mb_free(), not rfree().
*/
void *
-mb_realloc(pool *p, void *m, unsigned size)
+mb_realloc(void *m, unsigned size)
{
struct mblock *ob = NULL;
@@ -392,9 +392,7 @@ mb_realloc(pool *p, void *m, unsigned size)
}
struct mblock *b = xrealloc(ob, sizeof(struct mblock) + size);
-
- b->r.class = &mb_class;
- add_tail(&p->inside, &b->r.n);
+ replace_node(&b->r.n, &b->r.n);
b->size = size;
return b->data;
}
@@ -413,3 +411,18 @@ mb_free(void *m)
rfree(b);
}
+
+
+#define STEP_UP(x) ((x) + (x)/2 + 4)
+
+void
+buffer_realloc(void **buf, unsigned *size, unsigned need, unsigned item_size)
+{
+ unsigned nsize = MIN(*size, need);
+
+ while (nsize < need)
+ nsize = STEP_UP(nsize);
+
+ *buf = mb_realloc(*buf, nsize*isize);
+ *size = nsize;
+}