How can I use Git's malloc wrapper in my code?
I want to use the git's malloc and realloc wrappers in my code for OOM(out of memory) conditions. Here is its code:
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;
}
but the release_pack_memory f开发者_开发知识库unction is in sha1_file.c header file and this function have references to the functions in other header files in Git's code and I didn't want to put so much effort for isolate this function from Git's codebase. At the moment I am looking for an alternative function for release_pack_memory function, or can you recommend me another alternative. I'll be thankful for any kind of help
Why do you want to use Git's malloc wrapper? Do you understand what it's doing? If so, why do you think you need a "replacement" for release_pack_memory?
All this wrapper does* is, if malloc
fails, it tries to free up some memory that it uses for caches (which is what release_pack_memory
does) and then tries again. If you don't have any in-memory caches then there's really no point copying this wrapper (and if you do have in-memory caches, then you should already know how to free memory from it without having to copy this function).
* It also contains a check for if size
is 0 on platforms that do not support malloc(0)
, if this is a concern to you, then the release_pack_memory stuff is still useless.
精彩评论