Wrapping malloc - C
- by Appu
I am a beginner in C. While reading git's source code, I found this wrapper function around malloc.
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;
}
Questions
I couldn't understand why are they using malloc(1)?
What does release_pack_memory does and I can't find this functions implementation in the whole source code.
What does the #ifdef XMALLOC_POISON memset(ret, 0xA5, size); does?
I am planning to reuse this function on my project. Is this a good wrapper around malloc?
Any help would be great.