Skip to content

Commit

Permalink
Merge pull request #14492 from LabNConsulting/oper-state
Browse files Browse the repository at this point in the history
oper state
  • Loading branch information
idryzhov authored Dec 29, 2023
2 parents 080299f + f725838 commit 353ee7b
Show file tree
Hide file tree
Showing 86 changed files with 15,493 additions and 474 deletions.
2 changes: 2 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ ForEachMacros:
# libyang outliers:
- 'LY_FOR_KEYS'
- 'LY_LIST_FOR'
- 'LYD_LIST_FOR_INST'
- 'LYD_LIST_FOR_INST_SAFE'
- 'LY_TREE_FOR'
- 'LY_TREE_DFS_BEGIN'
- 'LYD_TREE_DFS_BEGIN'
Expand Down
1 change: 1 addition & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,7 @@ else
fi
AC_C_FLAG([-Wno-unused-parameter])
AC_C_FLAG([-Wno-missing-field-initializers])
AC_C_FLAG([-Wno-microsoft-anon-tag])

AC_C_FLAG([-Wc++-compat], [], [CXX_COMPAT_CFLAGS="-Wc++-compat"])
AC_SUBST([CXX_COMPAT_CFLAGS])
Expand Down
49 changes: 48 additions & 1 deletion gdb/lib.txt
Original file line number Diff line number Diff line change
Expand Up @@ -306,12 +306,59 @@ define mq_walk
end
set $mg = $mg->next
end
end

document mg_walk
document mq_walk
Walk the memory data structures to show what is holding memory.

Arguments:
1st: A (struct memgroup *) where to start the walk. If you are not
sure where to start pass it mg_first, which is a global DS for
all memory allocated in FRR
end

define __darr_meta
set $_ = ((struct darr_metadata *)$arg0) - 1
end
document __darr_meta
Store a pointer to the struct darr_metadata in $_ for the given dynamic array.

Argument: a pointer to a darr dynamic array.
Returns: pointer to the struct darr_metadata in $_.
end

define darr_meta
__darr_meta $arg0
p *$_
end
document darr_meta
Print the struct darr_metadata for the given dynamic array. Store the value
in $_ as well.

Argument: a pointer to a darr dynamic array.
Returns: pointer to the struct darr_metadata in $_.
end

define darr_len
__darr_meta $arg0
set $_ = $_->len
p $_
end
document darr_len
Print the length of the given dynamic array, and store in $_.

Argument: a pointer to a darr dynamic array.
Returns: length of the array.
end

define darr_cap
__darr_meta $arg0
set $_ = $_->cap
p $_
end
document darr_len
Print the capacity of the given dynamic array, and store in $_.

Argument: a pointer to a darr dynamic array.
Returns: capacity of the array.
end
62 changes: 53 additions & 9 deletions lib/darr.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -52,29 +53,72 @@ static size_t darr_size(uint count, size_t esize)
return count * esize + sizeof(struct darr_metadata);
}

void *__darr_resize(void *a, uint count, size_t esize)
char *__darr_in_vsprintf(char **sp, bool concat, const char *fmt, va_list ap)
{
size_t inlen = concat ? darr_strlen(*sp) : 0;
size_t capcount = strlen(fmt) + MIN(inlen + 64, 128);
ssize_t len;

darr_ensure_cap(*sp, capcount);

if (!concat)
darr_reset(*sp);

/* code below counts on having a NUL terminated string */
if (darr_len(*sp) == 0)
*darr_append(*sp) = 0;
again:
len = vsnprintf(darr_last(*sp), darr_avail(*sp), fmt, ap);
if (len < 0)
darr_in_strcat(*sp, fmt);
else if ((size_t)len < darr_avail(*sp))
_darr_len(*sp) += len;
else {
darr_ensure_cap(*sp, darr_len(*sp) + (size_t)len);
goto again;
}
return *sp;
}

char *__darr_in_sprintf(char **sp, bool concat, const char *fmt, ...)
{
va_list ap;

va_start(ap, fmt);
(void)__darr_in_vsprintf(sp, concat, fmt, ap);
va_end(ap);
return *sp;
}


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;

Expand All @@ -89,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;
}

Expand Down
Loading

0 comments on commit 353ee7b

Please sign in to comment.