iOS - 内存对齐

1. 基本的内存对齐

objectivec 复制代码
// 对象内存对齐
struct objc_object {
    // isa 指针 8 字节对齐
    isa_t isa __attribute__((aligned(8)));
};

// 定义对齐常量
#define WORD_MASK 7UL            // 字对齐掩码
#define WORD_SHIFT 3UL           // 字对齐位移
#define WORD_SIZE 8              // 64位系统下字的大小

2. 弱引用表的内存对齐

objectivec 复制代码
struct weak_entry_t {
    // 确保指针对齐
    DisguisedPtr<objc_object> referent;
    
    union {
        struct {
            // 确保引用数组对齐
            weak_referrer_t *referrers;
        };
        struct {
            // 内联数组对齐
            weak_referrer_t inline_referrers[WEAK_INLINE_COUNT];
        };
    };
};

// 弱引用表大小对齐
static size_t aligned_size() {
    return (size + WORD_MASK) & ~WORD_MASK;
}

3. 缓存对齐

objectivec 复制代码
struct cache_t {
    // bucket 数组对齐
    struct bucket_t *_buckets __attribute__((aligned(CACHE_LINE_SIZE)));
    
    // 掩码对齐优化
    mask_t _mask;  // 总是 2^n - 1
    
    // 计算对齐的大小
    static size_t bytesForCapacity(uint32_t cap) {
        return sizeof(bucket_t) * cap + sizeof(cache_t);
    }
};

4. 内存分配对齐

objectivec 复制代码
// 分配内存时的对齐处理
void *calloc(size_t count, size_t size) {
    // 计算对齐后的大小
    size_t alignedSize = (size + WORD_MASK) & ~WORD_MASK;
    
    // 分配对齐的内存
    void *result = malloc(count * alignedSize);
    if (result) {
        // 清零
        bzero(result, count * alignedSize);
    }
    return result;
}

5. 属性对齐

objectivec 复制代码
// 属性内存对齐
struct property_t {
    const char *name;
    const char *attributes;
} __attribute__((aligned(WORD_SIZE)));

// 确保属性列表对齐
struct property_list_t {
    uint32_t count;
    uint32_t size;
    property_t first;  // 这里开始的属性数组会自动对齐
};

6. 优化相关的对齐

objectivec 复制代码
// 1. CPU 缓存行对齐
#define CACHE_LINE_SIZE 64
struct cache_aligned_t {
    // 确保数据在缓存行边界上对齐
    char data[32] __attribute__((aligned(CACHE_LINE_SIZE)));
};

// 2. SIMD 指令对齐
struct simd_data {
    // 16字节对齐用于 SIMD 指令
    float values[4] __attribute__((aligned(16)));
};

7. 对齐检查和处理

objectivec 复制代码
// 检查对齐
static inline bool isAligned(const void *ptr, size_t alignment) {
    return (((uintptr_t)ptr) & (alignment - 1)) == 0;
}

// 计算对齐填充
static inline size_t alignmentPadding(size_t size, size_t alignment) {
    return (alignment - (size & (alignment - 1))) & (alignment - 1);
}

8. 内存对齐的优点

objectivec 复制代码
/*
1. 性能优化:
   - 减少内存访问次数
   - 提高缓存命中率
   - 支持原子操作

2. 硬件要求:
   - 满足处理器对齐要求
   - 支持 SIMD 指令
   - 优化内存访问

3. 内存效率:
   - 减少内存碎片
   - 优化内存布局
   - 提高访问效率
*/

总结要点:

  1. 对齐目的:
  • 提高访问效率
  • 满足硬件要求
  • 支持特殊指令
  1. 对齐实现:
  • 属性对齐
  • 结构体对齐
  • 内存分配对齐
  1. 优化考虑:
  • 缓存行对齐
  • SIMD 对齐
  • 填充优化
  1. 注意事项:
  • 内存开销
  • 平台兼容性
  • 性能影响
相关推荐
ZZH_AI项目交付1 天前
同一套前摄心率算法,为什么 iPhone 16 Pro 稳,iPhone XR 会失准?
ios·app·ai编程
DeMinds1 天前
内容没有丢,我为什么总在重新整理?|DeMinds 如何让工作接着继续
ios·github·markdown
apd_csdn1 天前
清华邮箱苹果邮件app设置(全)
ios·thu
MDM.Plus1 天前
从“遥控”到“自治”:苹果 MDM 技术的代际跨越与业务重构
ios·智能手机·重构·mdm
黑化旺仔1 天前
iOS - 天气预报仿写总结
ios
Coffeeee1 天前
ios零基础的Android开发能否靠AI让老板省一笔人工费呢
android·ios·ai编程
2501_916007471 天前
深入理解HTTPS对称与非对称加密机制及Charles抓包实践
网络协议·http·ios·小程序·https·uni-app·iphone
2501_916008891 天前
HTTPS 抓包遇到证书绑定怎么办,使用 TraceEagle 解除 App 证书校验
网络协议·计算机网络·http·网络安全·ios·adb·https
白玉cfc1 天前
【iOS】MRC和ARC
macos·ios·cocoa
胡琦博客1 天前
HarmonyOS 智能工具箱(三):语音交互工具
交互·xcode·harmonyos