From 7f8d825a52e04d388cca72130e54f0352d3d5f64 Mon Sep 17 00:00:00 2001 From: Amin Ben Ramdhane Date: Thu, 21 Oct 2021 14:47:56 +0100 Subject: [PATCH] Improve memory api --- bin/Makefile.am | 1 - dmdynamicjson.c | 8 ++-- dmdynamiclibrary.c | 4 +- dmdynamicmem.c | 93 ------------------------------------------- dmdynamicmem.h | 37 ----------------- dmentry.c | 1 - dmtree/tr104/common.c | 1 - libbbf_api/dmmem.c | 36 ++++++++--------- libbbf_api/dmmem.h | 51 +++++++++++++++--------- 9 files changed, 54 insertions(+), 178 deletions(-) delete mode 100644 dmdynamicmem.c delete mode 100644 dmdynamicmem.h diff --git a/bin/Makefile.am b/bin/Makefile.am index 7c45a2eb..b76cba97 100644 --- a/bin/Makefile.am +++ b/bin/Makefile.am @@ -34,7 +34,6 @@ libbbfdm_la_SOURCES = \ ../dmentry.c \ ../dmdynamiclibrary.c \ ../dmdynamicjson.c \ - ../dmdynamicmem.c \ ../dmdiagnostics.c \ ../dmbbfcommon.c diff --git a/dmdynamicjson.c b/dmdynamicjson.c index 85f39743..13d1ba25 100644 --- a/dmdynamicjson.c +++ b/dmdynamicjson.c @@ -10,7 +10,6 @@ */ #include "dmdynamicjson.h" -#include "dmdynamicmem.h" #include "dmentry.h" #define json_object_get_string(x) (char *)json_object_get_string(x) @@ -47,9 +46,8 @@ static void free_json_data(struct list_head *json_list) while (json_list->next != json_list) { dm_json_obj = list_entry(json_list->next, struct dm_json_obj, list); list_del(&dm_json_obj->list); - if (dm_json_obj->name) - dm_dynamic_free(dm_json_obj->name); - dm_dynamic_free(dm_json_obj); + dmfree(dm_json_obj->name); + dmfree(dm_json_obj); } } @@ -168,7 +166,7 @@ static char *generate_path_without_instance(char *full_obj, bool is_obj) if (pos && !is_obj) buf[pos - 1] = 0; - if (str) dm_dynamic_free(str); + dmfree(str); return dm_dynamic_strdup(&json_memhead, buf); } diff --git a/dmdynamiclibrary.c b/dmdynamiclibrary.c index a4c9300d..56b52f62 100644 --- a/dmdynamiclibrary.c +++ b/dmdynamiclibrary.c @@ -9,7 +9,6 @@ * */ -#include "dmdynamicmem.h" #include "dmdynamiclibrary.h" LIST_HEAD(loaded_library_list); @@ -134,8 +133,7 @@ static char *get_path_without_instance(char *path) pos += snprintf(&res_path[pos], sizeof(res_path) - pos, "%s%s", pch, (pchr != NULL && *pchr != '\0') ? "." : ""); } - if (str) - dm_dynamic_free(str); + dmfree(str); return dm_dynamic_strdup(&library_memhead, res_path); } diff --git a/dmdynamicmem.c b/dmdynamicmem.c deleted file mode 100644 index 6661e25a..00000000 --- a/dmdynamicmem.c +++ /dev/null @@ -1,93 +0,0 @@ -/* - * Copyright (C) 2019 iopsys Software Solutions AB - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License version 2.1 - * as published by the Free Software Foundation - * - * Author: Amin Ben Ramdhane - * - */ - -#include "dmdynamicmem.h" - -inline void *__dm_dynamic_malloc(struct list_head *mem_list, size_t size) -{ - struct dm_dynamic_mem *m = malloc(sizeof(struct dm_dynamic_mem) + size); - if (m == NULL) return NULL; - list_add(&m->list, mem_list); - return (void *)m->mem; -} - -inline void *__dm_dynamic_calloc(struct list_head *mem_list, int n, size_t size) -{ - struct dm_dynamic_mem *m = calloc(n, sizeof(struct dm_dynamic_mem) + size); - if (m == NULL) return NULL; - list_add(&m->list, mem_list); - return (void *)m->mem; -} - -inline void *__dm_dynamic_realloc(struct list_head *mem_list, void *n, size_t size) -{ - struct dm_dynamic_mem *m = NULL; - if (n != NULL) { - m = container_of(n, struct dm_dynamic_mem, mem); - list_del(&m->list); - } - struct dm_dynamic_mem *new_m = realloc(m, sizeof(struct dm_dynamic_mem) + size); - if (new_m == NULL) { - dm_dynamic_free(m); - return NULL; - } else - m = new_m; - list_add(&m->list, mem_list); - return (void *)m->mem; -} - -inline void dm_dynamic_free(void *m) -{ - if (m == NULL) return; - struct dm_dynamic_mem *rm; - rm = container_of(m, struct dm_dynamic_mem, mem); - list_del(&rm->list); - free(rm); -} - -void dm_dynamic_cleanmem(struct list_head *mem_list) -{ - struct dm_dynamic_mem *dmm; - while (mem_list->next != mem_list) { - dmm = list_entry(mem_list->next, struct dm_dynamic_mem, list); - list_del(&dmm->list); - free(dmm); - } -} - -char *__dm_dynamic_strdup(struct list_head *mem_list, const char *s) -{ - size_t len = strlen(s) + 1; - void *new = __dm_dynamic_malloc(mem_list, len); - if (new == NULL) return NULL; - return (char *) memcpy(new, s, len); -} - -int __dm_dynamic_asprintf(struct list_head *mem_list, char **s, const char *format, ...) -{ - int size; - char *str = NULL; - va_list arg; - - va_start(arg, format); - size = vasprintf(&str, format, arg); - va_end(arg); - - if (size < 0 || str == NULL) - return -1; - - *s = __dm_dynamic_strdup(mem_list, str); - - free(str); - if (*s == NULL) - return -1; - return 0; -} diff --git a/dmdynamicmem.h b/dmdynamicmem.h deleted file mode 100644 index 3da5beee..00000000 --- a/dmdynamicmem.h +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Copyright (C) 2019 iopsys Software Solutions AB - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License version 2.1 - * as published by the Free Software Foundation - * - * Author: Amin Ben Ramdhane - * - */ - -#ifndef __DM_DYNAMIC_MEM_H -#define __DM_DYNAMIC_MEM_H - -#include - -struct dm_dynamic_mem -{ - struct list_head list; - char mem[0]; -}; - -void *__dm_dynamic_malloc(struct list_head *mem_list, size_t size); -void *__dm_dynamic_calloc(struct list_head *mem_list, int n, size_t size); -void *__dm_dynamic_realloc(struct list_head *mem_list, void *n, size_t size); -char *__dm_dynamic_strdup(struct list_head *mem_list, const char *s); -int __dm_dynamic_asprintf(struct list_head *mem_list, char **s, const char *format, ...); -void dm_dynamic_free(void *m); -void dm_dynamic_cleanmem(struct list_head *mem_list); - -#define dm_dynamic_malloc(m, x) __dm_dynamic_malloc(m, x) -#define dm_dynamic_calloc(m, n, x) __dm_dynamic_calloc(m, n, x) -#define dm_dynamic_realloc(m, x, n) __dm_dynamic_realloc(m, x, n) -#define dm_dynamic_strdup(m, x) __dm_dynamic_strdup(m, x) -#define dm_dynamic_asprintf(m, s, format, ...) __dm_dynamic_asprintf(m, s, format, ## __VA_ARGS__) - -#endif //__DM_DYNAMIC_MEM_H diff --git a/dmentry.c b/dmentry.c index 001a7d0f..2bf523fd 100644 --- a/dmentry.c +++ b/dmentry.c @@ -16,7 +16,6 @@ #include "dmdynamicjson.h" #include "dmdynamiclibrary.h" #include "dmdynamicvendor.h" -#include "dmdynamicmem.h" #include "device.h" #include "dmbbfcommon.h" diff --git a/dmtree/tr104/common.c b/dmtree/tr104/common.c index bac2d613..e559055d 100644 --- a/dmtree/tr104/common.c +++ b/dmtree/tr104/common.c @@ -8,7 +8,6 @@ * Author: Yalu Zhang, yalu.zhang@iopsys.eu */ -#include "dmdynamicmem.h" #include "dmentry.h" #include "common.h" diff --git a/libbbf_api/dmmem.c b/libbbf_api/dmmem.c index e1d7ea78..2f307601 100644 --- a/libbbf_api/dmmem.c +++ b/libbbf_api/dmmem.c @@ -19,12 +19,12 @@ inline void *__dmmalloc #ifdef WITH_MEMTRACK const char *file, const char *func, int line, #endif /*WITH_MEMTRACK*/ -size_t size +struct list_head *mem_list, size_t size ) { struct dmmem *m = malloc(sizeof(struct dmmem) + size); if (m == NULL) return NULL; - list_add(&m->list, &memhead); + list_add(&m->list, mem_list); #ifdef WITH_MEMTRACK m->file = (char *)file; m->func = (char *)func; @@ -38,12 +38,12 @@ inline void *__dmcalloc #ifdef WITH_MEMTRACK const char *file, const char *func, int line, #endif /*WITH_MEMTRACK*/ -int n, size_t size +struct list_head *mem_list, int n, size_t size ) { struct dmmem *m = calloc(n, sizeof(struct dmmem) + size); if (m == NULL) return NULL; - list_add(&m->list, &memhead); + list_add(&m->list, mem_list); #ifdef WITH_MEMTRACK m->file = (char *)file; m->func = (char *)func; @@ -57,7 +57,7 @@ inline void *__dmrealloc #ifdef WITH_MEMTRACK const char *file, const char *func, int line, #endif /*WITH_MEMTRACK*/ -void *n, size_t size +struct list_head *mem_list, void *n, size_t size ) { struct dmmem *m = NULL; @@ -71,7 +71,7 @@ void *n, size_t size return NULL; } else m = new_m; - list_add(&m->list, &memhead); + list_add(&m->list, mem_list); #ifdef WITH_MEMTRACK m->file = (char *)file; m->func = (char *)func; @@ -89,11 +89,11 @@ inline void dmfree(void *m) free(rm); } -void dmcleanmem() +inline void __dmcleanmem(struct list_head *mem_list) { struct dmmem *dmm; - while (memhead.next != &memhead) { - dmm = list_entry(memhead.next, struct dmmem, list); + while (mem_list->next != mem_list) { + dmm = list_entry(mem_list->next, struct dmmem, list); #ifdef WITH_MEMTRACK fprintf(stderr, "Allocated memory in {%s, %s(), line %d} is not freed\n", dmm->file, dmm->func, dmm->line); #endif /*WITH_MEMTRACK*/ @@ -107,14 +107,14 @@ char *__dmstrdup #ifdef WITH_MEMTRACK const char *file, const char *func, int line, #endif /*WITH_MEMTRACK*/ -const char *s +struct list_head *mem_list, const char *s ) { size_t len = strlen(s) + 1; #ifdef WITH_MEMTRACK - void *new = __dmmalloc(file, func, line, len); + void *new = __dmmalloc(file, func, line, mem_list, len); #else - void *new = __dmmalloc(len); + void *new = __dmmalloc(mem_list, len); #endif /*WITH_MEMTRACK*/ if (new == NULL) return NULL; return (char *) memcpy(new, s, len); @@ -126,7 +126,7 @@ int __dmasprintf #ifdef WITH_MEMTRACK const char *file, const char *func, int line, #endif /*WITH_MEMTRACK*/ -char **s, const char *format, ... +struct list_head *mem_list, char **s, const char *format, ... ) { int size; @@ -141,9 +141,9 @@ char **s, const char *format, ... return -1; #ifdef WITH_MEMTRACK - *s = __dmstrdup(file, func, line, str); + *s = __dmstrdup(file, func, line, mem_list, str); #else - *s = __dmstrdup(str); + *s = __dmstrdup(mem_list, str); #endif /*WITH_MEMTRACK*/ free(str); @@ -157,7 +157,7 @@ int __dmastrcat #ifdef WITH_MEMTRACK const char *file, const char *func, int line, #endif /*WITH_MEMTRACK*/ -char **s, char *obj, char *lastname +struct list_head *mem_list, char **s, char *obj, char *lastname ) { char buf[2048]; @@ -166,9 +166,9 @@ char **s, char *obj, char *lastname int llen = strlen(lastname) + 1; memcpy(buf + olen, lastname, llen); #ifdef WITH_MEMTRACK - *s = __dmstrdup(file, func, line, buf); + *s = __dmstrdup(file, func, line, mem_list, buf); #else - *s = __dmstrdup(buf); + *s = __dmstrdup(mem_list, buf); #endif /*WITH_MEMTRACK*/ if (*s == NULL) return -1; return 0; diff --git a/libbbf_api/dmmem.h b/libbbf_api/dmmem.h index bf158b02..265b93b9 100644 --- a/libbbf_api/dmmem.h +++ b/libbbf_api/dmmem.h @@ -18,6 +18,8 @@ #include #include +extern struct list_head memhead; + void dmfree(void *m); static inline void dm_empty_func() { @@ -46,7 +48,7 @@ void *__dmmalloc #ifdef WITH_MEMTRACK const char *file, const char *func, int line, #endif /*WITH_MEMTRACK*/ -size_t size +struct list_head *mem_list, size_t size ); void *__dmcalloc @@ -54,7 +56,7 @@ void *__dmcalloc #ifdef WITH_MEMTRACK const char *file, const char *func, int line, #endif /*WITH_MEMTRACK*/ -int n, size_t size +struct list_head *mem_list, int n, size_t size ); void *__dmrealloc @@ -62,7 +64,7 @@ void *__dmrealloc #ifdef WITH_MEMTRACK const char *file, const char *func, int line, #endif /*WITH_MEMTRACK*/ -void *n, size_t size +struct list_head *mem_list, void *n, size_t size ); char *__dmstrdup @@ -70,17 +72,18 @@ char *__dmstrdup #ifdef WITH_MEMTRACK const char *file, const char *func, int line, #endif /*WITH_MEMTRACK*/ -const char *s +struct list_head *mem_list, const char *s ); -void dmcleanmem(); +void __dmcleanmem(struct list_head *mem_list); #endif /*WITH_MEMLEACKSEC*/ + int __dmasprintf ( #ifdef WITH_MEMTRACK const char *file, const char *func, int line, #endif /*WITH_MEMTRACK*/ -char **s, const char *format, ... +struct list_head *mem_list, char **s, const char *format, ... ); int __dmastrcat @@ -88,25 +91,35 @@ int __dmastrcat #ifdef WITH_MEMTRACK const char *file, const char *func, int line, #endif /*WITH_MEMTRACK*/ -char **s, char *obj, char *lastname +struct list_head *mem_list, char **s, char *obj, char *lastname ); #ifdef WITH_MEMLEACKSEC #ifdef WITH_MEMTRACK -#define dmmalloc(x) __dmmalloc(__FILE__, __func__, __LINE__, x) -#define dmcalloc(n, x) __dmcalloc(__FILE__, __func__, __LINE__, n, x) -#define dmrealloc(x, n) __dmrealloc(__FILE__, __func__, __LINE__, x, n) -#define dmstrdup(x) __dmstrdup(__FILE__, __func__, __LINE__, x) -#define dmasprintf(s, format, ...) __dmasprintf(__FILE__, __func__, __LINE__, s, format, ## __VA_ARGS__) -#define dmastrcat(s, b, m) __dmastrcat(__FILE__, __func__, __LINE__, s, b, m) +#define dmmalloc(x) __dmmalloc(__FILE__, __func__, __LINE__, &memhead, x) +#define dmcalloc(n, x) __dmcalloc(__FILE__, __func__, __LINE__, &memhead, n, x) +#define dmrealloc(x, n) __dmrealloc(__FILE__, __func__, __LINE__, &memhead, x, n) +#define dmstrdup(x) __dmstrdup(__FILE__, __func__, __LINE__, &memhead, x) +#define dmasprintf(s, format, ...) __dmasprintf(__FILE__, __func__, __LINE__, &memhead, s, format, ## __VA_ARGS__) +#define dmastrcat(s, b, m) __dmastrcat(__FILE__, __func__, __LINE__, &memhead, s, b, m) +#define dmcleanmem() __dmcleanmem(&memhead) #else -#define dmmalloc(x) __dmmalloc(x) -#define dmcalloc(n, x) __dmcalloc(n, x) -#define dmrealloc(x, n) __dmrealloc(x, n) -#define dmstrdup(x) __dmstrdup(x) -#define dmasprintf(s, format, ...) __dmasprintf(s, format, ## __VA_ARGS__) -#define dmastrcat(s, b, m) __dmastrcat(s, b, m) +#define dmmalloc(x) __dmmalloc(&memhead, x) +#define dmcalloc(n, x) __dmcalloc(&memhead, n, x) +#define dmrealloc(x, n) __dmrealloc(&memhead, x, n) +#define dmstrdup(x) __dmstrdup(&memhead, x) +#define dmasprintf(s, format, ...) __dmasprintf(&memhead, s, format, ## __VA_ARGS__) +#define dmastrcat(s, b, m) __dmastrcat(&memhead, s, b, m) +#define dmcleanmem() __dmcleanmem(&memhead) #endif /*WITH_MEMTRACK*/ + +#define dm_dynamic_malloc(m, x) __dmmalloc(m, x) +#define dm_dynamic_calloc(m, n, x) __dmcalloc(m, n, x) +#define dm_dynamic_realloc(m, x, n) __dmrealloc(m, x, n) +#define dm_dynamic_strdup(m, x) __dmstrdup(m, x) +#define dm_dynamic_asprintf(m, s, format, ...) __dmasprintf(m, s, format, ## __VA_ARGS__) +#define dm_dynamic_cleanmem(m) __dmcleanmem(m) + #else #define dmmalloc(x) malloc(x) #define dmcalloc(n, x) calloc(n, x)