aligned memory allocation

本文介绍了一个用于内存对齐分配的 C 函数 `aligned_alloc` 和对应的释放函数 `aligned_free`。`aligned_alloc` 函数接收所需内存大小与对齐方式作为参数,并返回指向已对齐内存的指针;`aligned_free` 则用于释放由 `aligned_alloc` 分配的内存。
void *aligned_alloc(size_t size, size_t align) {
  void *p = NULL;
  void *ret = NULL;
  p = malloc(size + align + sizeof(void *));
  if (!p) return NULL;
  
  size_t offset = align - (size_t)p % align;
  if (offset < sizeof (void*)) offset += align;
  
  ret = (p + offset);
  *((size_t*)ret - 1) = (size_t)p;
  
  return ret;
}

void aligned_free(void *p) {
  free((void *)(*((size_t *)p - 1)));
}

#define MALLOC_CAP_EXEC (1<<0) ///< Memory must be able to run executable code #define MALLOC_CAP_32BIT (1<<1) ///< Memory must allow for aligned 32-bit data accesses #define MALLOC_CAP_8BIT (1<<2) ///< Memory must allow for 8/16/...-bit data accesses #define MALLOC_CAP_DMA (1<<3) ///< Memory must be able to accessed by DMA #define MALLOC_CAP_PID2 (1<<4) ///< Memory must be mapped to PID2 memory space (PIDs are not currently used) #define MALLOC_CAP_PID3 (1<<5) ///< Memory must be mapped to PID3 memory space (PIDs are not currently used) #define MALLOC_CAP_PID4 (1<<6) ///< Memory must be mapped to PID4 memory space (PIDs are not currently used) #define MALLOC_CAP_PID5 (1<<7) ///< Memory must be mapped to PID5 memory space (PIDs are not currently used) #define MALLOC_CAP_PID6 (1<<8) ///< Memory must be mapped to PID6 memory space (PIDs are not currently used) #define MALLOC_CAP_PID7 (1<<9) ///< Memory must be mapped to PID7 memory space (PIDs are not currently used) #define MALLOC_CAP_SPIRAM (1<<10) ///< Memory must be in SPI RAM #define MALLOC_CAP_INTERNAL (1<<11) ///< Memory must be internal; specifically it should not disappear when flash/spiram cache is switched off #define MALLOC_CAP_DEFAULT (1<<12) ///< Memory can be returned in a non-capability-specific memory allocation (e.g. malloc(), calloc()) call #define MALLOC_CAP_IRAM_8BIT (1<<13) ///< Memory must be in IRAM and allow unaligned access #define MALLOC_CAP_RETENTION (1<<14) ///< Memory must be able to accessed by retention DMA #define MALLOC_CAP_RTCRAM (1<<15) ///< Memory must be in RTC fast memory #define MALLOC_CAP_TCM (1<<16) ///< Memory must be in TCM memory #define MALLOC_CAP_DMA_DESC_AHB (1<<17) ///< Memory must be capable of containing AHB DMA descriptors #define MALLOC_CAP_DMA_DESC_AXI (1<<18) ///< Memory must be capable of containing AXI DMA descriptors #define MALLOC_CAP_CACHE_ALIGNED (1<<19) ///< Memory must be aligned to the cache line size of any intermediate caches #define MALLOC_CAP_INVALID (1<<31) ///< Memory can't be used / list end marker解析ESP32各内存
09-05
### 关于 `_aligned_malloc` 的使用 在 C 语言中,`_aligned_malloc` 是一种专门用于分配具有特定对齐方式的动态内存的函数。这种函数通常被用来满足某些硬件或软件需求,比如 SIMD(单指令多数据流)操作需要的数据对齐要求。 #### 函数原型 ```c void *_aligned_malloc(size_t size, size_t alignment); ``` - `size`: 表示要分配的字节数。 - `alignment`: 表示所需的内存地址对齐边界大小,必须是 2 的幂次方[^1]。 返回值是一个指向已分配内存区域的指针,如果失败则返回 `NULL`。 #### 使用注意事项 1. 分配的内存必须通过 `_aligned_free` 进行释放,而不能使用普通的 `free()` 函数来释放[^3]。 2. 对齐参数 `alignment` 需要是 2 的幂次方,并且一般情况下应大于等于目标平台上的最大基本类型的对齐要求[^1]。 #### 示例代码 下面提供了一个简单的例子展示如何使用 `_aligned_malloc` 和 `_aligned_free`: ```c #include <stdio.h> #include <stdlib.h> int main() { const size_t size = 1024; // 要申请的空间大小 const size_t alignment = 16; // 所需的对齐边界 // 动态分配一块按指定对界对齐的内存 double *aligned_memory = (double *)_aligned_malloc(size, alignment); if (!aligned_memory) { fprintf(stderr, "Memory allocation failed.\n"); return EXIT_FAILURE; } printf("Allocated memory at address %p with alignment of %zu bytes.\n", aligned_memory, alignment); // 初始化并打印部分数据作为演示 for (size_t i = 0; i < 8 && ((char*)aligned_memory + i * sizeof(double)) < ((char*)aligned_memory + size); ++i){ aligned_memory[i] = i * 1.1; printf("Value at index [%zu]: %.2f\n", i, aligned_memory[i]); } // 正确释放内存 _aligned_free(aligned_memory); printf("Aligned memory has been freed successfully.\n"); return EXIT_SUCCESS; } ``` 此程序展示了如何利用 `_aligned_malloc` 来获取一段按照给定对齐条件分配出来的存储区,并对其进行初始化以及最后的安全释放过程[^2]。 #### 总结 以上就是关于 `_aligned_malloc` 的基础介绍及其典型应用实例。需要注意的是,在实际开发过程中应当严格遵循其使用的规则以避免潜在的风险问题发生。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值