iOS OC 对象的内存对齐原则

749 阅读3分钟

1.问题的引入

初始化一个OC类,具有如下属性:

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface LGTeacher : NSObject
@property (nonatomic, copy) NSString *name;
@property (nonatomic, assign) int age;
@property (nonatomic, assign) long height;
@property (nonatomic, strong) NSString *hobby;

@end

NS_ASSUME_NONNULL_END

初始化对象,并获取对象的内存size:

        LGTeacher  *p = [[LGTeacher alloc] init];
        p.name = @"LG_Cooci";
        p.age  = 18;
        p.height = 185;
        p.hobby  = @"女";
        
        NSLog(@"%lu - %lu",class_getInstanceSize([p class]),malloc_size((__bridge const void *)(p)));

打印结果:

由以上打印结果可以看出 class_getInstanceSizemalloc_size获取到的内存大小不一样,那么是什么导致的两者获取同一对象的内存大小不一样呢?我们下一步继续探索。

首先我们先手动计算一下这个对象所占的内存: isa -- 8字节,name -- 8字节, age -- 4字节, height -- 8字节, hobby -- 8字节;总计36字节。

我们跟踪objc源码可以发现改变size的地方有两个地方:

    1. instanceSize instanceSize 继续跟踪,
size_t instanceSize(size_t extraBytes) const {
        if (fastpath(cache.hasFastInstanceSize(extraBytes))) {
            return cache.fastInstanceSize(extraBytes);
        }

        size_t size = alignedInstanceSize() + extraBytes;// alignedInstanceSize
        // CF requires all objects be at least 16 bytes.
        if (size < 16) size = 16;
        return size;
}

uint32_t alignedInstanceSize() const {
        return word_align(unalignedInstanceSize());
}

#   define WORD_MASK 7UL
static inline uint32_t word_align(uint32_t x) {
    return (x + WORD_MASK) & ~WORD_MASK;
}

由以上源码可以得到instanceSize 使用8字节对齐原则处理Size,并且最小为16字节。 其中的原理可以参考本人其他篇文章:内存对齐小记内存对齐算法

    1. calloc

由于calloc属于malloc源码里面

跟踪libmalloc源码:

calloc源码实现:

void *
calloc(size_t num_items, size_t size)
{
	void *retval;
	retval = malloc_zone_calloc(default_zone, num_items, size);
	if (retval == NULL) {
		errno = ENOMEM;
	}
	return retval;
}

// malloc_zone_calloc
void *
	malloc_zone_calloc(malloc_zone_t *zone, size_t num_items, size_t size)
{
	MALLOC_TRACE(TRACE_calloc | DBG_FUNC_START, (uintptr_t)zone, num_items, size, 0);

	void *ptr;
	if (malloc_check_start && (malloc_check_counter++ >= malloc_check_start)) {
		internal_check();
	}

	ptr = zone->calloc(zone, num_items, size);
	
	if (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);
	return ptr;
}

断点打印 zone->calloc

  • ①:得到其真实调用为default_zone_calloc
  • ②:搜索default_zone_calloc继续跟进,打印default_zone_calloc内部的zone->calloc得到 nano_calloc
  • ③:分析nano_calloc源码可以知道在 _nano_malloc_check_clear内进行了相关操作
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);
}

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);
}

跳转到_nano_malloc_check_clear内部发现代码很多,一脸懵逼,但是仔细一看很多都是做一些容错判断,除去这些代码后,返现与size有关的只有一行代码:

size_t slot_bytes = segregated_size_to_fit(nanozone, size, &slot_key);

跳转进 segregated_size_to_fit 可以看到又是内存对齐的代码,这里的内存对齐是以16字节原则进行对齐的。

内存对齐的原理可以参考本人其他篇文章:内存对齐小记内存对齐算法

#define SHIFT_NANO_QUANTUM		4
#define NANO_REGIME_QUANTA_SIZE	(1 << SHIFT_NANO_QUANTUM)	// 16

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
	}
	k = (size + NANO_REGIME_QUANTA_SIZE - 1) >> SHIFT_NANO_QUANTUM; // round up and shift for number of quanta
	slot_bytes = k << SHIFT_NANO_QUANTUM;							// multiply by power of two quanta size
	*pKey = k - 1;													// Zero-based!

	return slot_bytes;
}

总结

经过上述的各种分析,我们可以得到的结论是instanceSize是以8字节进行对齐的, 后面calloc是以16字节进行对齐的,说明calloc进一步对对象进行了处理。也就解释了我们打印出来的40-48了。

由以上可以知道对象申请的内存大小和系统开辟的大小存在不一致的情况,8字节对齐应用于对象的属性,16字节对齐应用于对象,由于对象的内存是连续的,这样可以规避一些不必要的风险,以空间换时间来得到更高的安全性。