dmalloc 原文 翻译整理(3)

dmalloc库的实现不是非常模块化,过度使用宏。在MSSPACES定义下,它提供带mspace参数的内存分配函数,支持线程局部内存分配。编译时需注意size_t数值的精度问题,以及在Windows环境下可能遇到的分配问题。编译选项如WIN32会影响其在MS环境下的表现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

L164 实现不是完全模块化和过度使用宏。也许有朝一日所有的C编译器现在不能作的蛮力扩展嵌入模块码能做到,但是现在他们似乎是不足的。

 

 The implementation is not very modular and seriously overuses macros. Perhaps someday all C compilers will do as good a job inlining modular code as can now be done by brute-force expansion, but now, enough of them seem not to.

 

一些编译器发送许多的警告,关于代码死掉/不能找到仅仅在一些平台上,并且也关于一些故意不用的无符号类型。所有一只能被忽略的原因。

 

Some compilers issue a lot of warnings about code that is dead/unreachable only on some platforms, and also about intentional uses of negation on unsigned types. All known cases of each can be ignored.

 

一个冗长,但是高等级的描述,看http://gee.cs.oswego.edu/dl/html/malloc.html

 

For a longer but out of date high-level description, see http://gee.cs.oswego.edu/dl/html/malloc.html

 

L178 MSSPACES

 

如果定义了MSSPACES,然后加入malloc,free,等.中,这个文件也定义 mspace_malloc, mspace_free,等。这些是常规版本malloc采取"mspace"参数获得引用create_mspace,去控制所有的内部记录。如果 ONLY_MSPACES被定义,仅这些版本被编译。这样如果你想使用这个内存分配器为仅仅一些内存分配并且你的系统malloc作其他的,你可以仅编译ONLY_MSPACES并然后做一些事情像...

   static mspace myspace = create_mspace(0,0);//例子

   #define mymalloc(bytes)  mspace_malloc(mymspace, bytes)

 

* MSPACES
  If MSPACES is defined, then in addition to malloc, free, etc., this file also defines mspace_malloc, mspace_free, etc. These are versions of malloc routines that take an "mspace" argument obtained using create_mspace, to control all internal bookkeeping. If ONLY_MSPACES is defined, only these versions are compiled. So if you would like to use this allocator for only some allocations, and your system malloc for others, you can compile with ONLY_MSPACES and then do something like...
    static mspace mymspace = create_mspace(0,0); // for example
    #define mymalloc(bytes)  mspace_malloc(mymspace, bytes)

 

L189 注解:如果你仅需一个mspace实例,你可以替换用USE_DL_PREFIX去重注全局malloc

你同样能建立线程局部内存分配存储mspaces像局部线程。例如:

     static __thread mspace tlms = 0;

     void* tlmalloc(size_t bytes)

     {

          if(tlms == 0) tlms = create_mspace(0,0);

          return mspace_malloc(tlms, bytes); 

     }

     void tlfree(void* mem) { mspace_free(tlms,mem);}

除非 FOOTERS是定义的,每一个mspace是独立完成的,你不能从一个自由分配到另一个(尽管一致性检查是弱的,错误的用法不总是被捕获)。如果FOOTERS被定义,每一个块携带着一个标签标明它源于mspace,并且释放指向它的源spaces。

 

  (Note: If you only need one instance of an mspace, you can instead use "USE_DL_PREFIX" to relabel the global malloc.)

  You can similarly create thread-local allocators by storing mspaces as thread-locals. For example:
    static __thread mspace tlms = 0;
    void*  tlmalloc(size_t bytes) {
      if (tlms == 0) tlms = create_mspace(0, 0);
      return mspace_malloc(tlms, bytes);
    }
    void  tlfree(void* mem) { mspace_free(tlms, mem); }

  Unless FOOTERS is defined, each mspace is completely independent. You cannot allocate from one and free to another (although conformance is only weakly checked, so usage errors are not always caught). If FOOTERS is defined, then each chunk carries around a tag indicating its originating mspace, and frees are directed to their originating spaces.

 

L208--------------------------------编译选项-----------------------------------------------------

谨慎建立size_t类型数学常量值的定义。一些系统,默认的值不是自动适应size_t精度除非他们明确的定义,你也能使用符号值MAX_SIZE_T,SIZE_T_ONE,等如下。

 

WIN32                       默认:定义如果 _WIN32定义

定义WIN32建立默认为MS环境并编译器。另外默认为unix。注意的是,似乎有一些malloc的情况下,这可能不是一个纯粹的简易替换为WIN32的malloc:随机查看失败从Win32 GDI API的原由也许是在一些视频驱动执行当像素缓存被分配,并且虚拟缓存溢出的BUG。因为 dlmalloc使用一个小的默认粒度为64k的,像素缓存可能经常跨虚拟缓存范围分配当用微软的内存分配器时。你能解决这个使用VirtualAlloc()和VirtualFree()为所有的像素缓存宁可用malloc()。如果这是不可能的,重新编译这个malloc带着一个大的DEFAULT_GRANULARITY。

 


 -------------------------  Compile-time options ---------------------------

Be careful in setting #define values for numerical constants of typesize_t. On some systems, literal values are not automatically extended to size_t precision unless they are explicitly casted. You can also use the symbolic values MAX_SIZE_T, SIZE_T_ONE, etc below.

WIN32                    default: defined if _WIN32 defined
  Defining WIN32 sets up defaults for MS environment and compilers.  Otherwise defaults are for unix. Beware that there seem to be some cases where this malloc might not be a pure drop-in replacement for Win32 malloc:Random-looking failures from Win32 GDI API's (eg; SetDIBits()) may be due to bugs in some video driver implementations when pixel buffers are malloc()ed, and the region spans more than one VirtualAlloc()ed region. Because dlmalloc uses a small (64Kb)
  default granularity, pixel buffers may straddle virtual allocation regions more often than when using the Microsoft allocator.  You can avoid this by using VirtualAlloc() and VirtualFree() for all pixel buffers rather than using malloc().  If this is not possible, recompile this malloc with a larger DEFAULT_GRANULARITY.

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值