diff options
| author | Igor Ryzhov <iryzhov@nfware.com> | 2020-11-30 18:50:51 +0300 |
|---|---|---|
| committer | Igor Ryzhov <iryzhov@nfware.com> | 2020-11-30 18:55:40 +0300 |
| commit | 6df43392d8703df6e92b7ae6e96373e6c3fc5083 (patch) | |
| tree | 24694e9cbb47dcc06281f7da2b55b69f98380a74 | |
| parent | 40ab41115dd415cc42e311a96d66bb83ce4f1150 (diff) | |
vtysh: fix incorrect memory statistics
As code comment states, 1 count of MTYPE_COMPLETION is leaked for each
autocompleted token. Let's manually decrement the counter before passing
the pointer to readline.
Signed-off-by: Igor Ryzhov <iryzhov@nfware.com>
| -rw-r--r-- | lib/memory.c | 6 | ||||
| -rw-r--r-- | lib/memory.h | 3 | ||||
| -rw-r--r-- | vtysh/vtysh.c | 8 |
3 files changed, 12 insertions, 5 deletions
diff --git a/lib/memory.c b/lib/memory.c index f715044ea3..a377d3b945 100644 --- a/lib/memory.c +++ b/lib/memory.c @@ -127,6 +127,12 @@ void *qstrdup(struct memtype *mt, const char *str) return str ? mt_checkalloc(mt, strdup(str), strlen(str) + 1) : NULL; } +void qcountfree(struct memtype *mt, void *ptr) +{ + if (ptr) + mt_count_free(mt, ptr); +} + void qfree(struct memtype *mt, void *ptr) { if (ptr) diff --git a/lib/memory.h b/lib/memory.h index 13f2f9b11a..e9db12fce2 100644 --- a/lib/memory.h +++ b/lib/memory.h @@ -162,12 +162,15 @@ extern void *qrealloc(struct memtype *mt, void *ptr, size_t size) __attribute__((_ALLOC_SIZE(3), nonnull(1) _RET_NONNULL)); extern void *qstrdup(struct memtype *mt, const char *str) __attribute__((malloc, nonnull(1) _RET_NONNULL)); +extern void qcountfree(struct memtype *mt, void *ptr) + __attribute__((nonnull(1))); extern void qfree(struct memtype *mt, void *ptr) __attribute__((nonnull(1))); #define XMALLOC(mtype, size) qmalloc(mtype, size) #define XCALLOC(mtype, size) qcalloc(mtype, size) #define XREALLOC(mtype, ptr, size) qrealloc(mtype, ptr, size) #define XSTRDUP(mtype, str) qstrdup(mtype, str) +#define XCOUNTFREE(mtype, ptr) qcountfree(mtype, ptr) #define XFREE(mtype, ptr) \ do { \ qfree(mtype, ptr); \ diff --git a/vtysh/vtysh.c b/vtysh/vtysh.c index ace4139551..ef0dfccba9 100644 --- a/vtysh/vtysh.c +++ b/vtysh/vtysh.c @@ -1144,12 +1144,10 @@ static char *command_generator(const char *text, int state) cmd_free_strvec(vline); } - if (matched && matched[index]) - /* - * this is free()'d by readline, but we leak 1 count of - * MTYPE_COMPLETION - */ + if (matched && matched[index]) { + XCOUNTFREE(MTYPE_COMPLETION, matched[index]); return matched[index++]; + } XFREE(MTYPE_TMP, matched); |
