iOS中malloc和calloc源碼分析
calloc
- 我們知道在
iOS創(chuàng)建對(duì)象的alloc方法中,最終通過調(diào)用calloc方法來開辟內(nèi)存。如果這里具體流程不夠清楚的話,可以參考Runtime源碼分析-alloc - 那么
calloc具體是如何實(shí)現(xiàn)的呢?由于在objc4中沒有提供該方法,我們通過libmalloc-317.40.8代碼去研究。
1. calloc
- 首先進(jìn)入
calloc方法
void *
calloc(size_t num_items, size_t size)
{
return _malloc_zone_calloc(default_zone, num_items, size, MZ_POSIX);
}
- 內(nèi)部調(diào)用了
_malloc_zone_calloc()方法
2. _malloc_zone_calloc
MALLOC_NOINLINE
static void *
_malloc_zone_calloc(malloc_zone_t *zone, size_t num_items, size_t size,
malloc_zone_options_t mzo)
{
MALLOC_TRACE(TRACE_calloc | DBG_FUNC_START, (uintptr_t)zone, num_items, size, 0);
void *ptr;
if (malloc_check_start) {
internal_check();
}
// ptr指針指向一塊內(nèi)存
ptr = zone->calloc(zone, num_items, size);
if (os_unlikely(malloc_logger)) {
malloc_logger(MALLOC_LOG_TYPE_ALLOCATE | MALLOC_LOG_TYPE_HAS_ZONE | MALLOC_LOG_TYPE_CLEARED, (uintptr_t)zone,
(uintptr_t)(num_items * size), 0, (uintptr_t)ptr, 0);
}
MALLOC_TRACE(TRACE_calloc | DBG_FUNC_END, (uintptr_t)zone, num_items, size, (uintptr_t)ptr);
if (os_unlikely(ptr == NULL)) {
malloc_set_errno_fast(mzo, ENOMEM);
}
return ptr;
}
該方法中最終返回結(jié)果是一個(gè)指針,所以最重要的是13行。它創(chuàng)建了一塊內(nèi)存,并讓
ptr指向它。這個(gè)時(shí)候,我們點(diǎn)擊
zone->calloc跳轉(zhuǎn)對(duì)應(yīng)的實(shí)現(xiàn),發(fā)現(xiàn)點(diǎn)不進(jìn)去。這個(gè)時(shí)候才用匯編方式,看到該方法最終跳轉(zhuǎn)至default_zone_calloc方法
3. default_zone_calloc
static void *
default_zone_calloc(malloc_zone_t *zone, size_t num_items, size_t size)
{
zone = runtime_default_zone();
return zone->calloc(zone, num_items, size);
}
- 該方法內(nèi)部調(diào)用
zone->calloc方法,仍然點(diǎn)不進(jìn)去。再次使用匯編方法,發(fā)現(xiàn)該方法跳轉(zhuǎn)至nano_calloc
4. nano_calloc
static void *
nano_calloc(nanozone_t *nanozone, size_t num_items, size_t size)
{
size_t total_bytes;
if (calloc_get_size(num_items, size, 0, &total_bytes)) {
return NULL;
}
if (total_bytes <= NANO_MAX_SIZE) {
void *p = _nano_malloc_check_clear(nanozone, total_bytes, 1);
if (p) {
return p;
} else {
/* FALLTHROUGH to helper zone */
}
}
malloc_zone_t *zone = (malloc_zone_t *)(nanozone->helper_zone);
return zone->calloc(zone, 1, total_bytes);
}
- 此處我們還是先看返回值,發(fā)現(xiàn)有三處返回。第一處直接返回
NULL,這肯定不是我們需要的答案,直接忽略。剩下兩處,無法確定。這個(gè)時(shí)候通過斷點(diǎn)調(diào)試,發(fā)現(xiàn)一般情況走的是_nano_malloc_check_clear方法
5. _nano_malloc_check_clear
static void *
_nano_malloc_check_clear(nanozone_t *nanozone, size_t size, boolean_t cleared_requested)
{
MALLOC_TRACE(TRACE_nano_malloc, (uintptr_t)nanozone, size, cleared_requested, 0);
void *ptr;
size_t slot_key;
// 1. 計(jì)算合適的內(nèi)存大小
size_t slot_bytes = segregated_size_to_fit(nanozone, size, &slot_key); // Note slot_key is set here
mag_index_t mag_index = nano_mag_index(nanozone);
nano_meta_admin_t pMeta = &(nanozone->meta_data[mag_index][slot_key]);
// 2. 開辟一片內(nèi)存,并讓ptr指針指向這塊內(nèi)存
ptr = OSAtomicDequeue(&(pMeta->slot_LIFO), offsetof(struct chained_block_s, next));
if (ptr) {
unsigned debug_flags = nanozone->debug_flags;
#if NANO_FREE_DEQUEUE_DILIGENCE
size_t gotSize;
nano_blk_addr_t p; // the compiler holds this in a register
p.addr = (uint64_t)ptr; // Begin the dissection of ptr
if (NANOZONE_SIGNATURE != p.fields.nano_signature) {
malloc_zone_error(debug_flags, true,
"Invalid signature for pointer %p dequeued from free list\n",
ptr);
}
if (mag_index != p.fields.nano_mag_index) {
malloc_zone_error(debug_flags, true,
"Mismatched magazine for pointer %p dequeued from free list\n",
ptr);
}
gotSize = _nano_vet_and_size_of_free(nanozone, ptr);
if (0 == gotSize) {
malloc_zone_error(debug_flags, true,
"Invalid pointer %p dequeued from free list\n", ptr);
}
if (gotSize != slot_bytes) {
malloc_zone_error(debug_flags, true,
"Mismatched size for pointer %p dequeued from free list\n",
ptr);
}
if (!_nano_block_has_canary_value(nanozone, ptr)) {
malloc_zone_error(debug_flags, true,
"Heap corruption detected, free list canary is damaged for %p\n"
"*** Incorrect guard value: %lu\n", ptr,
((chained_block_t)ptr)->double_free_guard);
}
#if defined(DEBUG)
void *next = (void *)(((chained_block_t)ptr)->next);
if (next) {
p.addr = (uint64_t)next; // Begin the dissection of next
if (NANOZONE_SIGNATURE != p.fields.nano_signature) {
malloc_zone_error(debug_flags, true,
"Invalid next signature for pointer %p dequeued from free "
"list, next = %p\n", ptr, "next");
}
if (mag_index != p.fields.nano_mag_index) {
malloc_zone_error(debug_flags, true,
"Mismatched next magazine for pointer %p dequeued from "
"free list, next = %p\n", ptr, next);
}
gotSize = _nano_vet_and_size_of_free(nanozone, next);
if (0 == gotSize) {
malloc_zone_error(debug_flags, true,
"Invalid next for pointer %p dequeued from free list, "
"next = %p\n", ptr, next);
}
if (gotSize != slot_bytes) {
malloc_zone_error(debug_flags, true,
"Mismatched next size for pointer %p dequeued from free "
"list, next = %p\n", ptr, next);
}
}
#endif /* DEBUG */
#endif /* NANO_FREE_DEQUEUE_DILIGENCE */
((chained_block_t)ptr)->double_free_guard = 0;
((chained_block_t)ptr)->next = NULL; // clear out next pointer to protect free list
} else {
// 如果ptr指針為空,則去去找下一塊合適內(nèi)存
ptr = segregated_next_block(nanozone, pMeta, slot_bytes, mag_index);
}
// 3. 是否給內(nèi)存進(jìn)行初始化
if (cleared_requested && ptr) {
memset(ptr, 0, slot_bytes); // TODO: Needs a memory barrier after memset to ensure zeroes land first?
}
return ptr;
}
- 該方法中,它主要干了三件事:
-
segregated_size_to_fit:計(jì)算需要開辟的內(nèi)存大小 -
OSAtomicDequeue或者segregated_next_block:開辟內(nèi)存 -
memset(ptr, 0, slot_bytes);:是否進(jìn)行初始化
-
segregated_size_to_fit
static MALLOC_INLINE size_t
segregated_size_to_fit(nanozone_t *nanozone, size_t size, size_t *pKey)
{
size_t k, slot_bytes;
if (0 == size) {
size = NANO_REGIME_QUANTA_SIZE; // Historical behavior
}
// 內(nèi)存按照16字節(jié)對(duì)齊
// k = (size + 16 - 1) >> 4 右移4位
k = (size + NANO_REGIME_QUANTA_SIZE - 1) >> SHIFT_NANO_QUANTUM; // round up and shift for number of quanta
// slot_bytes = k << 4 左移4位
slot_bytes = k << SHIFT_NANO_QUANTUM; // multiply by power of two quanta size
*pKey = k - 1; // Zero-based!
return slot_bytes;
}
- 這里主要是內(nèi)存對(duì)齊算法,算法流程是:
- 當(dāng)前size + 15,左移4位
- 再把上面的值,右移4位
- 具體計(jì)算過程如下:假設(shè)
size為8字節(jié)-
(size + NANO_REGIME_QUANTA_SIZE - 1)= 8 + 15 = 23,用二進(jìn)制表示:0001 0111 -
k >> SHIFT_NANO_QUANTUM,用二進(jìn)制表示:0000 0001 -
k << SHIFT_NANO_QUANTUM,用二進(jìn)制表示:0001 0000 - 最終結(jié)果是16字節(jié),實(shí)現(xiàn)了按照16字節(jié)對(duì)齊。
-
OSAtomicDequeue或者segregated_next_block
- 首先會(huì)通過
OSAtomicDequeue方法來開辟內(nèi)存- 如果開辟成功,則返回內(nèi)存首地址給
ptr。然后對(duì)ptr進(jìn)行一系列的驗(yàn)證 - 如果開辟失敗,則通過
segregated_next_block方法進(jìn)行再次嘗試開辟
- 如果開辟成功,則返回內(nèi)存首地址給
static MALLOC_INLINE void *
segregated_next_block(nanozone_t *nanozone, nano_meta_admin_t pMeta, size_t slot_bytes, unsigned int mag_index)
{
while (1) {
// 當(dāng)前這塊pMeta可用內(nèi)存結(jié)束地址
uintptr_t theLimit = pMeta->slot_limit_addr; // Capture the slot limit that bounds slot_bump_addr right now
uintptr_t b = OSAtomicAdd64Barrier(slot_bytes, (volatile int64_t *)&(pMeta->slot_bump_addr));
// 減去添加的偏移量,獲取當(dāng)前可以獲取的地址
b -= slot_bytes;
if (b < theLimit) { // Did we stay within the bound of the present slot allocation?
// 如果地址還在范圍之內(nèi),則返回地址
return (void *)b;
} else {
// pMeta這塊內(nèi)存已經(jīng)用完了
if (pMeta->slot_exhausted) {
pMeta->slot_bump_addr = theLimit;
return 0; // We're toast
} else {
// One thread will grow the heap, others will see its been grown and retry allocation
_malloc_lock_lock(&nanozone->band_resupply_lock[mag_index]);
// 由于多線程,這里再次進(jìn)行檢查是否用完
if (pMeta->slot_exhausted) {
_malloc_lock_unlock(&nanozone->band_resupply_lock[mag_index]);
return 0; // Toast
} else if (b < pMeta->slot_limit_addr) {
// 如果小于最大限制地址,當(dāng)重新申請一個(gè)新的band后,重新嘗試while
_malloc_lock_unlock(&nanozone->band_resupply_lock[mag_index]);
continue;
} else if (segregated_band_grow(nanozone, pMeta, slot_bytes, mag_index)) {
// 申請新的band成功,重新嘗試while
_malloc_lock_unlock(&nanozone->band_resupply_lock[mag_index]);
continue;
} else {
pMeta->slot_exhausted = TRUE;
pMeta->slot_bump_addr = theLimit;
_malloc_lock_unlock(&nanozone->band_resupply_lock[mag_index]);
return 0;
}
}
}
}
}
- 該方法里面主要是去堆上獲取一塊合適的內(nèi)存。
memset(ptr, 0, slot_bytes)
- 根據(jù)傳入
cleared_requested參數(shù),來決定是否給內(nèi)存初始化為0
malloc
- 在底層中,除了常用的
alloc方法之外,還有malloc也經(jīng)常使用。我們看一下實(shí)現(xiàn),發(fā)現(xiàn)具體有什么區(qū)別。
1. malloc
void *
malloc(size_t size)
{
return _malloc_zone_malloc(default_zone, size, MZ_POSIX);
}
- 內(nèi)部調(diào)用了
_malloc_zone_malloc()方法
2. _malloc_zone_malloc
MALLOC_NOINLINE
static void *
_malloc_zone_malloc(malloc_zone_t *zone, size_t size, malloc_zone_options_t mzo)
{
MALLOC_TRACE(TRACE_malloc | DBG_FUNC_START, (uintptr_t)zone, size, 0, 0);
void *ptr = NULL;
if (malloc_check_start) {
internal_check();
}
if (size > MALLOC_ABSOLUTE_MAX_SIZE) {
goto out;
}
ptr = zone->malloc(zone, size); // if lite zone is passed in then we still call the lite methods
if (os_unlikely(malloc_logger)) {
malloc_logger(MALLOC_LOG_TYPE_ALLOCATE | MALLOC_LOG_TYPE_HAS_ZONE, (uintptr_t)zone, (uintptr_t)size, 0, (uintptr_t)ptr, 0);
}
MALLOC_TRACE(TRACE_malloc | DBG_FUNC_END, (uintptr_t)zone, size, (uintptr_t)ptr, 0);
out:
if (os_unlikely(ptr == NULL)) {
malloc_set_errno_fast(mzo, ENOMEM);
}
return ptr;
}
- 該方法中最終返回結(jié)果是一個(gè)指針,所以最重要的是16行。它創(chuàng)建了一塊內(nèi)存,并讓
ptr指向它。 - 這個(gè)時(shí)候點(diǎn)擊它的實(shí)現(xiàn),發(fā)現(xiàn)也跳轉(zhuǎn)不進(jìn)去。這個(gè)時(shí)候還是使用匯編手段,可以得知它調(diào)用
default_zone_malloc方法
3. default_zone_malloc
static void *
default_zone_malloc(malloc_zone_t *zone, size_t size)
{
zone = runtime_default_zone();
return zone->malloc(zone, size);
}
- 該方法調(diào)用
zone->malloc方法,仍然點(diǎn)不進(jìn)去,繼續(xù)查看匯編。發(fā)現(xiàn)調(diào)用了nano_malloc方法
4. nano_malloc
static void *
nano_malloc(nanozone_t *nanozone, size_t size)
{
if (size <= NANO_MAX_SIZE) {
void *p = _nano_malloc_check_clear(nanozone, size, 0);
if (p) {
return p;
} else {
/* FALLTHROUGH to helper zone */
}
}
malloc_zone_t *zone = (malloc_zone_t *)(nanozone->helper_zone);
return zone->malloc(zone, size);
}
- 這里有兩處返回值,通過斷點(diǎn)調(diào)試得知一般走
_nano_malloc_check_clear方法 - 看到這個(gè)方法有沒有一絲絲熟悉?原來它開辟內(nèi)存調(diào)用的方法和
calloc調(diào)用的是同一個(gè)方法。此處仔細(xì)對(duì)比發(fā)現(xiàn),雖然是調(diào)用同一個(gè)方法,但是參數(shù)穿的不同
// calloc中
void *p = _nano_malloc_check_clear(nanozone, total_bytes, 1);
// malloc中
void *p = _nano_malloc_check_clear(nanozone, size, 0);
static void *
_nano_malloc_check_clear(nanozone_t *nanozone, size_t size, boolean_t cleared_requested)
- 發(fā)現(xiàn)
cleared_requested這個(gè)參數(shù)傳的不同。calloc是1,malloc是0。那個(gè)這個(gè)參數(shù)有什么作用? - 我們進(jìn)入這個(gè)方法,發(fā)現(xiàn)以下實(shí)現(xiàn)
if (cleared_requested && ptr) {
memset(ptr, 0, slot_bytes);
}
- 原來是對(duì)開辟的內(nèi)存進(jìn)行初始化。
calloc會(huì)對(duì)它初始化,malloc不會(huì)對(duì)它初始化
總結(jié)
- 我們最終可以得出以下結(jié)論
-
malloc和calloc其實(shí)底層都調(diào)用的同一套開辟內(nèi)存方法 - 不同在于,
calloc在開辟完內(nèi)存會(huì)進(jìn)行初始化,malloc不會(huì)進(jìn)行初始化,則是原始臟數(shù)據(jù)。如果需要使用malloc這塊內(nèi)存,還需要我們手動(dòng)初始化