16 #if defined(WITH_DMALLOC) 23 #if !defined(EXIT_FAILURE) 24 #define EXIT_FAILURE 1 30 fprintf(stderr,
_(
"memory alloc (%u bytes) returned NULL.\n"), (
unsigned)size);
54 const char * (*dbg) (
void *item)
84 yarnPossess(pool->
have);
86 while ((item = pool->
head) != NULL) {
89 if (item->
use != NULL)
90 item->
use = yarnFreeLock(item->
use);
94 yarnRelease(pool->
have);
95 pool->
have = yarnFreeLock(pool->
have);
98 assert(pool->
made == count);
100 if (pool->
made != count)
101 rpmlog(
RPMLOG_WARNING,
D_(
"pool %s: FIXME: made %d, count %d\nNote: This is a harmless memory leak discovered while exiting, relax ...\n"), pool->
name, pool->
made, count);
111 char * (*
dbg) (
void *item),
112 void (*
init) (
void *item),
113 void (*
fini) (
void *item))
117 #if defined(WITH_VALGRIND) 119 static int is_zeroed = 0;
121 is_zeroed = is_zeroed;
124 pool->
have = yarnNewLock(0);
131 pool->
dbg = (
const char* (*)(
void*))
dbg;
145 const char * fn,
unsigned ln)
148 if (item == NULL)
return NULL;
149 yarnPossess(item->
use);
152 const char * imsg = (pool->
dbg ? (*pool->
dbg)((
void *)item) :
"");
154 fprintf(stderr,
"--> %s %p -- %ld %s at %s:%u%s\n", pool->
name,
155 item, yarnPeekLock(item->
use), msg, fn, ln, imsg);
158 yarnTwist(item->
use, BY, -1);
167 const char * fn,
unsigned ln)
170 if (item == NULL)
return NULL;
171 yarnPossess(item->
use);
173 const char * imsg = (pool->
dbg ? (*pool->
dbg)((
void *)item) :
"");
175 fprintf(stderr,
"--> %s %p ++ %ld %s at %s:%u%s\n", pool->
name,
176 item, yarnPeekLock(item->
use)+1, msg, fn, ln, imsg);
180 yarnTwist(item->
use, BY, 1);
188 const char * msg,
const char * fn,
unsigned ln)
192 if (item == NULL)
return NULL;
195 assert(item->
pool != NULL);
197 yarnPossess(item->
use);
200 const char * imsg = (pool->
dbg ? (*pool->
dbg)((
void *)item) :
"");
202 fprintf(stderr,
"--> %s %p -- %ld %s at %s:%u%s\n", pool->
name,
203 item, yarnPeekLock(item->
use), msg, fn, ln, imsg);
206 if (yarnPeekLock(item->
use) <= 1
L) {
208 if (pool != NULL && pool->
fini != NULL)
209 (*pool->
fini) ((
void *)item);
213 yarnTwist(item->
use, BY, -1);
215 return (
void *) item;
227 yarnPossess(pool->
have);
228 if (pool->
limit == 0)
229 yarnWaitFor(pool->
have, NOT_TO_BE, 0);
232 if (pool->
head != NULL) {
235 if (pool->
head == NULL)
239 yarnTwist(pool->
have, BY, -1);
247 assert(pool->
limit != 0);
251 yarnRelease(pool->
have);
255 item->
use = yarnNewLock(0);
270 yarnPossess(pool->
have);
274 yarnTwist(pool->
have, BY, 1);
275 if (item->
use != NULL)
276 yarnTwist(item->
use, TO, 0);
280 if (item->
use != NULL) {
281 yarnTwist(item->
use, TO, 0);
282 item->
use = yarnFreeLock(item->
use);
289 #if !(HAVE_MCHECK_H && defined(__GNUC__)) && !defined(__LCLINT__) 293 register void *value;
294 if (size == 0) size++;
295 value = malloc (size);
303 register void *value;
304 if (size == 0) size++;
305 if (nmemb == 0) nmemb++;
306 value = calloc (nmemb, size);
314 register void *value;
315 if (size == 0) size++;
316 value = realloc (ptr, size);
324 size_t size = strlen(str) + 1;
325 char *newstr = (
char *) malloc (size);
327 newstr = (
char *)
vmefail(size);
328 strcpy (newstr, str);
rpmioItem rpmioLinkPoolItem(rpmioItem item, const char *msg, const char *fn, unsigned ln)
Increment a pool item refcount.
static rpmioPool _rpmioPool
static void rpmlog(int code, const char *fmt,...)
#define VALGRIND_MEMPOOL_FREE(pool, addr)
#define VALGRIND_CREATE_MEMPOOL(pool, rzB, is_zeroed)
const char *(* dbg)(void *item)
void * xrealloc(void *ptr, size_t size)
Yet Another syslog(3) API clone.
struct rpmioPool_s * rpmioPool
#define VALGRIND_DESTROY_MEMPOOL(pool)
void * rpmioFreePoolItem(rpmioItem item, const char *msg, const char *fn, unsigned ln)
Free a pool item.
void * xmalloc(size_t size)
rpmioItem rpmioUnlinkPoolItem(rpmioItem item, const char *msg, const char *fn, unsigned ln)
Decrement a pool item refcount.
rpmioPool rpmioNewPool(const char *name, size_t size, int limit, int flags, char *(*dbg)(void *item), void(*init)(void *item), void(*fini)(void *item))
Create a memory pool.
rpmioItem rpmioPutPool(rpmioItem item)
Put unused item into pool (or free).
rpmioItem rpmioGetPool(rpmioPool pool, size_t size)
Get unused item from pool, or alloc a new item.
struct rpmioItem_s * rpmioItem
#define ANNOTATE_HAPPENS_BEFORE(_obj)
static void * _free(const void *p)
Wrapper to free(3), hides const compilation noise, permit NULL, return NULL.
#define VALGRIND_HG_CLEAN_MEMORY(_qzz_start, _qzz_len)
#define VALGRIND_MEMPOOL_ALLOC(pool, addr, size)
struct yarnLock_s * yarnLock
#define ANNOTATE_HAPPENS_AFTER(_obj)
rpmioPool rpmioFreePool(rpmioPool pool)
Reclaim memory pool items.
void * vmefail(size_t size)
void * xcalloc(size_t nmemb, size_t size)
char * xstrdup(const char *str)