mirror of
https://dev.iopsys.eu/bbf/bbfdm.git
synced 2026-03-11 03:39:11 +01:00
Improve memory api
This commit is contained in:
parent
d04a757b2c
commit
7f8d825a52
9 changed files with 54 additions and 178 deletions
|
|
@ -34,7 +34,6 @@ libbbfdm_la_SOURCES = \
|
|||
../dmentry.c \
|
||||
../dmdynamiclibrary.c \
|
||||
../dmdynamicjson.c \
|
||||
../dmdynamicmem.c \
|
||||
../dmdiagnostics.c \
|
||||
../dmbbfcommon.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);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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 <amin.benramdhane@pivasoftware.com>
|
||||
*
|
||||
*/
|
||||
|
||||
#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;
|
||||
}
|
||||
|
|
@ -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 <amin.benramdhane@pivasoftware.com>
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef __DM_DYNAMIC_MEM_H
|
||||
#define __DM_DYNAMIC_MEM_H
|
||||
|
||||
#include <libbbf_api/dmcommon.h>
|
||||
|
||||
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
|
||||
|
|
@ -16,7 +16,6 @@
|
|||
#include "dmdynamicjson.h"
|
||||
#include "dmdynamiclibrary.h"
|
||||
#include "dmdynamicvendor.h"
|
||||
#include "dmdynamicmem.h"
|
||||
#include "device.h"
|
||||
#include "dmbbfcommon.h"
|
||||
|
||||
|
|
|
|||
|
|
@ -8,7 +8,6 @@
|
|||
* Author: Yalu Zhang, yalu.zhang@iopsys.eu
|
||||
*/
|
||||
|
||||
#include "dmdynamicmem.h"
|
||||
#include "dmentry.h"
|
||||
#include "common.h"
|
||||
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -18,6 +18,8 @@
|
|||
#include <string.h>
|
||||
#include <libubox/list.h>
|
||||
|
||||
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)
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue