2009-04-20 15:00:56 +02:00
|
|
|
/*
|
|
|
|
* Various trivial helper wrappers around standard functions
|
|
|
|
*/
|
|
|
|
#include "cache.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* There's no pack memory to release - but stay close to the Git
|
|
|
|
* version so wrap this away:
|
|
|
|
*/
|
2009-07-01 12:37:06 +02:00
|
|
|
static inline void release_pack_memory(size_t size __used, int flag __used)
|
2009-04-20 15:00:56 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
char *xstrdup(const char *str)
|
|
|
|
{
|
|
|
|
char *ret = strdup(str);
|
|
|
|
if (!ret) {
|
|
|
|
release_pack_memory(strlen(str) + 1, -1);
|
|
|
|
ret = strdup(str);
|
|
|
|
if (!ret)
|
|
|
|
die("Out of memory, strdup failed");
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *xmalloc(size_t size)
|
|
|
|
{
|
|
|
|
void *ret = malloc(size);
|
|
|
|
if (!ret && !size)
|
|
|
|
ret = malloc(1);
|
|
|
|
if (!ret) {
|
|
|
|
release_pack_memory(size, -1);
|
|
|
|
ret = malloc(size);
|
|
|
|
if (!ret && !size)
|
|
|
|
ret = malloc(1);
|
|
|
|
if (!ret)
|
|
|
|
die("Out of memory, malloc failed");
|
|
|
|
}
|
|
|
|
#ifdef XMALLOC_POISON
|
|
|
|
memset(ret, 0xA5, size);
|
|
|
|
#endif
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* xmemdupz() allocates (len + 1) bytes of memory, duplicates "len" bytes of
|
|
|
|
* "data" to the allocated memory, zero terminates the allocated memory,
|
|
|
|
* and returns a pointer to the allocated memory. If the allocation fails,
|
|
|
|
* the program dies.
|
|
|
|
*/
|
2010-05-18 18:29:23 -03:00
|
|
|
static void *xmemdupz(const void *data, size_t len)
|
2009-04-20 15:00:56 +02:00
|
|
|
{
|
|
|
|
char *p = xmalloc(len + 1);
|
|
|
|
memcpy(p, data, len);
|
|
|
|
p[len] = '\0';
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *xstrndup(const char *str, size_t len)
|
|
|
|
{
|
|
|
|
char *p = memchr(str, '\0', len);
|
2009-07-01 12:37:06 +02:00
|
|
|
|
|
|
|
return xmemdupz(str, p ? (size_t)(p - str) : len);
|
2009-04-20 15:00:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void *xrealloc(void *ptr, size_t size)
|
|
|
|
{
|
|
|
|
void *ret = realloc(ptr, size);
|
|
|
|
if (!ret && !size)
|
|
|
|
ret = realloc(ptr, 1);
|
|
|
|
if (!ret) {
|
|
|
|
release_pack_memory(size, -1);
|
|
|
|
ret = realloc(ptr, size);
|
|
|
|
if (!ret && !size)
|
|
|
|
ret = realloc(ptr, 1);
|
|
|
|
if (!ret)
|
|
|
|
die("Out of memory, realloc failed");
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|