aboutsummaryrefslogtreecommitdiffstats
path: root/lib/resource.c
diff options
context:
space:
mode:
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;
+}