Wrapping malloc - C

Posted by Appu on Stack Overflow See other posts from Stack Overflow or by Appu
Published on 2010-03-19T18:29:27Z Indexed on 2010/03/19 18:31 UTC
Read the original article Hit count: 569

Filed under:
|
|

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

  1. I couldn't understand why are they using malloc(1)?
  2. What does release_pack_memory does and I can't find this functions implementation in the whole source code.
  3. 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.

© Stack Overflow or respective owner

Related posts about c

    Related posts about malloc