glibc 2.15经营多个分配 场所
。每个竞技场都有自己的锁。当线程需要分配内存时,
malloc()选择一个竞技场,将其锁定,然后从中分配内存。
选择竞技场的机制有些复杂,旨在减少锁争用:
考虑到这一点,
malloc()基本上看起来像这样(为简便起见编辑):
mstate ar_ptr; void *victim; arena_lookup(ar_ptr); arena_lock(ar_ptr, bytes); if(!ar_ptr) return 0; victim = _int_malloc(ar_ptr, bytes); if(!victim) { if(ar_ptr != &main_arena) { (void)mutex_unlock(&ar_ptr->mutex); ar_ptr = &main_arena; (void)mutex_lock(&ar_ptr->mutex); victim = _int_malloc(ar_ptr, bytes); (void)mutex_unlock(&ar_ptr->mutex); } else { ar_ptr = arena_get2(ar_ptr->next ? ar_ptr : 0, bytes); (void)mutex_unlock(&main_arena.mutex); if(ar_ptr) { victim = _int_malloc(ar_ptr, bytes); (void)mutex_unlock(&ar_ptr->mutex); } } } else (void)mutex_unlock(&ar_ptr->mutex); return victim;该分配器称为
ptmalloc。它基于Doug
Lea的早期工作,并由Wolfram Gloger维护。



