开发者

BoehmGC - Understanding memory allocator GC_malloc

I am breaking my head in understanding the BoehmGC allocation scheme - GC_malloc. I am not getting how it allocates memory, not seen any malloc or mmap which GC_malloc internally calls.

Can someone kindly help me? Any links or code snippet will be of big help.

Huge thanks in advance. Boehm GC source code

 enter code here
 254 /* Allocate lb bytes of composite (pointerful) data */
 255 #ifdef THREAD_LOCAL_ALLOC
 256   void * GC_core_malloc(size_t lb)
 257 #else
 258   void * GC_malloc(size_t lb)
 259 #endif
 260 {
 261     void *op;
 262     void **opp;
 263     size_t lg;
 264     DCL_LOCK_STATE;
 265 
 266     if(SMALL_OBJ(lb)) {
 267         lg = GC_size_map[lb];
 268         opp = (void **)&(GC_objfreelist[lg]);
 269         LOCK();
 270         if( EXPECT((op = *opp) == 0, 0) ) {
 271             UNLOCK();
 272             return(GENERAL_MALLOC((word)lb, NORMAL));
 273         }
 274         /* See above comment on signals.        */
 275         GC_ASSERT(0 == obj_link(op)
 276                   || (word)obj_link(op)
 277                         <= (word)GC_greatest_plausi开发者_如何学运维ble_heap_addr
 278                      && (word)obj_link(op)
 279                         >= (word)GC_least_plausible_heap_addr);
 280         *opp = obj_link(op);
 281         obj_link(op) = 0;
 282         GC_bytes_allocd += GRANULES_TO_BYTES(lg);
 283         UNLOCK();
 284         return op;
 285    } else {
 286        return(GENERAL_MALLOC(lb, NORMAL));
 287    }
 288 }


There are two possibilities:

  • It returns a pointer given by GENERAL_MALLOC (two returns)
  • it sets op = *opp (the line with the EXPECT) and then it returns op. I'll say that the second is to reuse freed blocks.

For the second case: look at the value of opp before the if:

opp = (void **)&(GC_objfreelist[lg]);

In opp there is a pointer to the "free" list of objects.

The if probably checks if there is any block in that list. If there isn't (== 0) then it uses GENERAL_MALLOC.


Look at the os_deps.c file where (most) of the OS-dependent functions are implemented.

mmap can be used by Boehm-GC if it's configured to use that. (See the various GC_unix_get_mem(bytes) functions.)

If mmap isn't used, the other (bare) allocator used sbrk.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜