summaryrefslogtreecommitdiffstats
path: root/lib/darr.c
diff options
context:
space:
mode:
authorChristian Hopps <chopps@labn.net>2023-12-16 23:17:39 +0100
committerChristian Hopps <chopps@labn.net>2023-12-28 18:52:57 +0100
commitb7db6b26753cbdec2fa22ed4ad7a85deedf67f7b (patch)
tree804ade1079c11f8847e2c3a4675155b754b288cf /lib/darr.c
parentlib: yang: add yang_get_node_keys() function (diff)
downloadfrr-b7db6b26753cbdec2fa22ed4ad7a85deedf67f7b.tar.xz
frr-b7db6b26753cbdec2fa22ed4ad7a85deedf67f7b.zip
lib: darr: add ability to set MTYPE for dynamic arrays
Signed-off-by: Christian Hopps <chopps@labn.net>
Diffstat (limited to 'lib/darr.c')
-rw-r--r--lib/darr.c23
1 files changed, 15 insertions, 8 deletions
diff --git a/lib/darr.c b/lib/darr.c
index 9ec3cb63b..f7a64fc39 100644
--- a/lib/darr.c
+++ b/lib/darr.c
@@ -10,6 +10,7 @@
#include "memory.h"
DEFINE_MTYPE(LIB, DARR, "Dynamic Array");
+DEFINE_MTYPE(LIB, DARR_STR, "Dynamic Array String");
static uint _msb(uint count)
{
@@ -90,28 +91,34 @@ char *__darr_in_sprintf(char **sp, bool concat, const char *fmt, ...)
}
-void *__darr_resize(void *a, uint count, size_t esize)
+void *__darr_resize(void *a, uint count, size_t esize, struct memtype *mtype)
{
uint ncount = darr_next_count(count, esize);
size_t osz = (a == NULL) ? 0 : darr_size(darr_cap(a), esize);
size_t sz = darr_size(ncount, esize);
- struct darr_metadata *dm = XREALLOC(MTYPE_DARR,
- a ? _darr_meta(a) : NULL, sz);
+ struct darr_metadata *dm;
- if (sz > osz)
- memset((char *)dm + osz, 0, sz - osz);
+ if (a) {
+ dm = XREALLOC(_darr_meta(a)->mtype, _darr_meta(a), sz);
+ if (sz > osz)
+ memset((char *)dm + osz, 0, sz - osz);
+ } else {
+ dm = XCALLOC(mtype, sz);
+ dm->mtype = mtype;
+ }
dm->cap = ncount;
return (void *)(dm + 1);
}
-void *__darr_insert_n(void *a, uint at, uint count, size_t esize, bool zero)
+void *__darr_insert_n(void *a, uint at, uint count, size_t esize, bool zero,
+ struct memtype *mtype)
{
struct darr_metadata *dm;
uint olen, nlen;
if (!a)
- a = __darr_resize(NULL, at + count, esize);
+ a = __darr_resize(NULL, at + count, esize, mtype);
dm = (struct darr_metadata *)a - 1;
olen = dm->len;
@@ -126,7 +133,7 @@ void *__darr_insert_n(void *a, uint at, uint count, size_t esize, bool zero)
nlen = olen + count;
if (nlen > dm->cap) {
- a = __darr_resize(a, nlen, esize);
+ a = __darr_resize(a, nlen, esize, mtype);
dm = (struct darr_metadata *)a - 1;
}