如何使用Git的malloc的包装在我的code? [英] How can I use Git's malloc wrapper in my code?

查看:164
本文介绍了如何使用Git的malloc的包装在我的code?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Git的malloc和realloc的包装在我的code的OOM(内存不足)的条件。下面是它的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;
}

但release_pack_memory功能在 sha1_file.c 头文件,这个功能在其他头文件在Git的code的函数的引用,我并没有想从Git的codeBase的投入这么多精力用于隔离此功能。目前,我正在寻找的release_pack_memory功能的替代功能,或者你可以给我推荐另一种选择。我会感谢任何形式的帮助。

but the release_pack_memory function 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

推荐答案

为什么你想使用Git的malloc的包装?你明白它在做什么?如果是这样,为什么你认为你需要一个替代的release_pack_memory?

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?

所有这些包装不*就是如果的malloc 失败,它会尝试释放一些内存,它使用的缓存(这是 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).


*它还包含如尺寸 0上不支持的malloc平台的检查(0) ,如果这是给你的关注,那么release_pack_memory东西还是没用。

* 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.

这篇关于如何使用Git的malloc的包装在我的code?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆