欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 文旅 > 八卦 > 嵌入式科普(26)为什么heap通常8字节对齐

嵌入式科普(26)为什么heap通常8字节对齐

2025/4/2 10:01:58 来源:https://blog.csdn.net/cl234583745/article/details/144971609  浏览:    关键词:嵌入式科普(26)为什么heap通常8字节对齐

目录

一、概述

二、newlibc heap

2.1 stm32cubeide .ld heap

2.2 e2studio .ld heap

三、glibc源码

3.1 Ubuntu c heap

四、总结


一、概述

  • 结论:在嵌入式c语言中,heap通常8字节对齐

  • 本文主要分析这个问题的分析过程

二、newlibc heap

newlibc:
https://github.com/blueskycoco/newlibc/tree/master/newlib/libc

图片

2.1 stm32cubeide .ld heap

stm32 .ld

  /* User_heap_stack section, used to check that there is enough "RAM" Ram type memory left */._user_heap_stack :{. = ALIGN(8);PROVIDE ( end = . );PROVIDE ( _end = . );. = . + _Min_Heap_Size;. = . + _Min_Stack_Size;. = ALIGN(8);} >RAM

2.2 e2studio .ld heap

ra6m3

    .heap (NOLOAD):{. = ALIGN(8);__HeapBase = .;/* Place the STD heap here. */KEEP(*(.heap))__HeapLimit = .;} > RAM
/* real_size is the size we actually have to allocate, allowing foroverhead and alignment.  */
#define REAL_SIZE(sz) \((sz) < sizeof (struct freelist_entry) - sizeof (size_t) \? sizeof (struct freelist_entry) \: sz + sizeof (size_t) + M_ALIGN(sz, sizeof (size_t)))

三、glibc源码

glibc:
https://github.com/bminor/glibc/tree/master

图片

/* MALLOC_ALIGNMENT is the minimum alignment for malloc'ed chunks.  Itmust be a power of two at least 2 * SIZE_SZ, even on machines forwhich smaller alignments would suffice. It may be defined as largerthan this though. Note however that code and data structures areoptimized for the case of 8-byte alignment.  */
#define MALLOC_ALIGNMENT (2 * SIZE_SZ < __alignof__ (long double) \? __alignof__ (long double) : 2 * SIZE_SZ)

3.1 Ubuntu c heap

Ubuntu:

图片

#include <stdio.h>  
#include <stdlib.h>  
#include <stdint.h> int main() {  // 不同大小的请求,包括不是8的倍数  size_t sizes[] = {15, 16, 23, 24, 31, 32, 64, 127, 128, 255, 256, 1023, 1024};   size_t num_sizes = sizeof(sizes) / sizeof(sizes[0]);  for (size_t i = 0; i < num_sizes; i++) {  void *ptr = malloc(sizes[i]);  // 检查 malloc 是否成功  if (ptr == NULL) {  perror("malloc failed");  return EXIT_FAILURE;  }  // 获取指针的地址  uintptr_t addr = (uintptr_t)ptr;  // 检查地址是否是8字节对齐  if (addr % 8 == 0) {  printf("Pointer for size %zu is 8-byte aligned: %p\n", sizes[i], ptr);  } else {  printf("Pointer for size %zu is NOT 8-byte aligned: %p\n", sizes[i], ptr);  }  // 释放分配的内存  free(ptr);  }  return EXIT_SUCCESS;  
} 
jerry@jerry-virtual-machine:~/桌面$ ./check
Pointer for size 15 is 8-byte aligned: 0x5615aab2d2a0
Pointer for size 16 is 8-byte aligned: 0x5615aab2d2a0
Pointer for size 23 is 8-byte aligned: 0x5615aab2d2a0
Pointer for size 24 is 8-byte aligned: 0x5615aab2d2a0
Pointer for size 31 is 8-byte aligned: 0x5615aab2d6d0
Pointer for size 32 is 8-byte aligned: 0x5615aab2d6d0
Pointer for size 64 is 8-byte aligned: 0x5615aab2d700
Pointer for size 127 is 8-byte aligned: 0x5615aab2d750
Pointer for size 128 is 8-byte aligned: 0x5615aab2d750
Pointer for size 255 is 8-byte aligned: 0x5615aab2d7e0
Pointer for size 256 is 8-byte aligned: 0x5615aab2d7e0
Pointer for size 1023 is 8-byte aligned: 0x5615aab2d8f0
Pointer for size 1024 is 8-byte aligned: 0x5615aab2d8f0
jerry@jerry-virtual-machine:~/桌面$ uname -m
x86_64cat /usr/lib/x86_64-linux-gnu/ldscripts/elf32_x86_64.x 

四、总结

  • 不论是在STM32的32位微控制器上还是在64位的Ubuntu系统中,heap 8字节对齐都因其带来的性能优化、硬件兼容性和代码可移植性而成为一种广泛接受的标准。这种做法帮助开发者构建既高效又可靠的应用程序,符合现代计算环境的需求

图片

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词