Newer
Older
/*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* Copyright (C) 2019 iopsys Software Solutions AB
* Author: MOHAMED Kallel <mohamed.kallel@pivasoftware.com>
*
*/
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#include "dmmem.h"
#ifdef WITH_MEMLEACKSEC
LIST_HEAD(memhead);
inline void *__dmmalloc
(
#ifdef WITH_MEMTRACK
const char *file, const char *func, int line,
#endif /*WITH_MEMTRACK*/
size_t size
)
{
struct dmmem *m = malloc(sizeof(struct dmmem) + size);
if (m == NULL) return NULL;
list_add(&m->list, &memhead);
#ifdef WITH_MEMTRACK
m->file = (char *)file;
m->func = (char *)func;
m->line = line;
#endif /*WITH_MEMTRACK*/
return (void *)m->mem;
}
inline void *__dmcalloc
(
#ifdef WITH_MEMTRACK
const char *file, const char *func, int line,
#endif /*WITH_MEMTRACK*/
int n, size_t size
)
{
struct dmmem *m = calloc(n, sizeof(struct dmmem) + size);
if (m == NULL) return NULL;
list_add(&m->list, &memhead);
#ifdef WITH_MEMTRACK
m->file = (char *)file;
m->func = (char *)func;
m->line = line;
#endif /*WITH_MEMTRACK*/
return (void *)m->mem;
}
inline void *__dmrealloc
(
#ifdef WITH_MEMTRACK
const char *file, const char *func, int line,
#endif /*WITH_MEMTRACK*/
void *old, size_t size
)
{
struct dmmem *m = NULL;
if (old != NULL) {
m = container_of(old, struct dmmem, mem);
list_del(&m->list);
}
m = realloc(m, sizeof(struct dmmem) + size);
if (m == NULL) return NULL;
list_add(&m->list, &memhead);
#ifdef WITH_MEMTRACK
m->file = (char *)file;
m->func = (char *)func;
m->line = line;
#endif /*WITH_MEMTRACK*/
return (void *)m->mem;
}
inline void dmfree(void *m)
{
if (m == NULL) return;
struct dmmem *rm;
rm = container_of(m, struct dmmem, mem);
list_del(&rm->list);
free(rm);
}
void dmcleanmem()
{
struct dmmem *dmm;
while (memhead.next != &memhead) {
dmm = list_entry(memhead.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*/
list_del(&dmm->list);
free(dmm);
}
}
char *__dmstrdup
(
#ifdef WITH_MEMTRACK
const char *file, const char *func, int line,
#endif /*WITH_MEMTRACK*/
const char *s
)
{
size_t len = strlen(s) + 1;
#ifdef WITH_MEMTRACK
void *new = __dmmalloc(file, func, line, len);
#else
void *new = __dmmalloc(len);
#endif /*WITH_MEMTRACK*/
if (new == NULL) return NULL;
return (char *) memcpy(new, s, len);
}
#endif /*WITH_MEMLEACKSEC*/
int __dmasprintf
(
#ifdef WITH_MEMTRACK
const char *file, const char *func, int line,
#endif /*WITH_MEMTRACK*/
char **s, const char *format, ...
)
{
char buf[512];
va_list arg;
int ret;
va_start(arg,format);
ret = vsprintf(buf, format, arg);
va_end(arg);
#ifdef WITH_MEMTRACK
*s = __dmstrdup(file, func, line, buf);
#else
*s = __dmstrdup(buf);
#endif /*WITH_MEMTRACK*/
if (*s == NULL) return -1;
return 0;
}
int __dmastrcat
(
#ifdef WITH_MEMTRACK
const char *file, const char *func, int line,
#endif /*WITH_MEMTRACK*/
char **s, char *obj, char *lastname
)
{
char buf[512];
int olen = strlen(obj);
memcpy(buf, obj, olen);
int llen = strlen(lastname) + 1;
memcpy(buf + olen, lastname, llen);
#ifdef WITH_MEMTRACK
*s = __dmstrdup(file, func, line, buf);
#else
*s = __dmstrdup(buf);
#endif /*WITH_MEMTRACK*/
if (*s == NULL) return -1;
return 0;
}