当前位置:网站首页>Allocate aligned heap space
Allocate aligned heap space
2022-06-27 18:23:00 【Xiao Qiu HUST】
Recently, I used a written by others FFT Hardware acceleration module , requirement DDR Data alignment in 4k The border , It is estimated that the data bus used by this module is AXI Agreed ,AXI Burst transmission of cannot exceed 4k The border . The platform used is C6678DSP, Compiler supported C++ The version is C++98, No, aligned new, So I can only write by myself aligned_malloc function .
Extra space overhead

C++ Of new What is called essentially is malloc function ,malloc You can apply for a certain size of space in the heap area , Then return to the first address of this space ; Corresponding free Function to free the allocated heap space .
As shown in the figure above , Calling malloc Post return pointer p p p,B Represents the aligned boundary , The number of bytes aligned is L. Generally speaking, we get p p p The address pointed to by the pointer is not aligned . But we can allocate a little more space , Make the data stored in the aligned position . So if you do that , The worst-case overhead is L-1, here p p p The pointer is assigned to “ The border +1” Location .
How to calculate the first aligned address from the assigned address . Just add... To the assigned address L-1, Then clear the low order of the address . For example, to align 4k The border :
a d d r a l i g n e d = ( p + 0 x F F F ) & ∼ ( 0 x F F F ) { {addr_{aligned} = (p + \rm{0xFFF})\& \sim(\rm{0xFFF})}} addraligned=(p+0xFFF)&∼(0xFFF)
So if p p p Already aligned to the boundary , Then the result will be p p p; and p p p If not aligned to the boundary , The result will be the first aligned address above the current address . This aligned address can be used as aligned_malloc The return value of the function .
Space release
In order to properly free the space just allocated , We still need to get ,malloc The address actually assigned p p p preserved . p p p yes void* type , So we need one more sizeof(void*) The size of the space used to store p p p. This storage location can be placed just below the aligned address .
The worst case scenario at this time is that additional L-1 Byte space ,malloc The assigned address is located at “ The border -(sizeof(void*)-1)” The location of .
Sample code
void *aligned_malloc(size_t num, size_t alignment){
void *pOrig, pAligned;
size_t offset;
// check alignment is power of 2
if(alignment & (alignment - 1) != 0) pAligned = NULL;
else{
offset = alignment - 1 + sizeof(void *);
pOrig = malloc(num + offset);
pAligned = (void *)(((size_t)pOrig + (size_t)offset) & (~(alignment-1)));
*((void **)((size_t)pAligned-sizeof(void *))) = pOrig;
}
return pAligned;
}
void aligned_free(void *pAligned){
void **p = (void **)((size_t)pAligned- sizeof(void *));
free(*p);
}
边栏推荐
- Synchronization mechanism of dual namenodes
- 07. Express routing
- [fxcg] today's market analysis
- 06. First introduction to express
- 当发布/订阅模式遇上.NET
- Software testing learning - dark horse programmer, software testing learning outline
- 2/14 preliminary calculation geometry
- Kubernetes基础自学系列 | Ingress API讲解
- Delete duplicate elements in the sorting linked list
- Wanzhou gold industry: what are the common gold investment and warehouse building modes?
猜你喜欢

07. Express routing

Why should string be designed to be immutable?

Software testing learning - dark horse programmer, software testing learning outline

Shardingsphere sharding proxy actual combat scenario

The power of code refactoring: how to measure the success of refactoring

09 route guard authenticates URL

2022 Liaoning's latest eight members (Safety Officer) simulated test question bank and answers

leetcode 70. climb stairs

Computing trends in three times of machine learning

Bit. Store: long bear market, stable stacking products may become the main theme
随机推荐
全面解析零知识证明:消解扩容难题 重新定义「隐私安全」
Bit.Store:熊市漫漫,稳定Staking产品或成主旋律
SQLite net (SQLite is used by unity, WPF and WinForm)
Detailed explanation of various GPIO input and output modes (push-pull, open drain, quasi bidirectional port)
如何制作登录界面
Delete duplicate elements in the sorting linked list
Offline disk group
[UVM basics] UVM tree organizational structure
Overview of Inspur Yunxi database executor
wheel ui
Cloud security daily 220216: root privilege escalation vulnerability found on IBM SaaS integration platform needs to be upgraded as soon as possible
Wanzhou gold industry: what knowledge points do you need to master to invest in precious metals?
d3dx9_ Where is 35.dll? d3dx9_ Where can I download 35.dll
D use in
Use pyinstaller to package py files into exe. Precautions and error typeerror:_ get_ sysconfigdata_ name() missing 1...‘ check_ Solutions to exists'
Kubernetes基础自学系列 | Ingress API讲解
Hash encryption
JXL export Excel
seata性能可以通过什么方式提高?比如增加数据库的计算节点?
【多线程】线程通信调度、等待集 wait() 、notify()