In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
In this issue, the editor will bring you the analysis of how to use heap and stack in IAR. The article is rich in content and analyzed and described from a professional point of view. I hope you can get something after reading this article.
What's a pile?
Heap is a part of memory space reserved for dynamic memory allocation
When an application needs to use a certain amount of memory temporarily, it can be allocated or borrowed from the heap space, which is realized by calling the malloc () function in C and through 'new' in C++.
This memory can be freed when it is no longer needed by calling the free () function or by using the delet operator. Once the memory is freed, it can be allocated again.
The location and size of the heap are statically set at compile time.
It is important to allocate the appropriate heap space for your application, otherwise it may crash at run time.
How to determine the heap size
The first question when considering dynamic memory used by an application is how much heap space do I need?
It's more like an estimate than a calculation.
If you know how your application behaves, you should know roughly how much memory should be allocated at each moment.
In addition, you need to consider that there is some extra overhead in managing the heap space.
The heap manager needs to track the amount of space already used and remaining, the block size that has been allocated, and usually contains a pointer to the next available memory
In addition, development tools may retain certain blocks of memory in order to maintain the memory architecture. For example, EWARM compilers typically retain blocks of memory with 8-byte multiples to ensure stack alignment.
Potential problems
When memory blocks of different sizes are allocated and released frequently, dynamic memory allocation is the most common problem.
When memory is freed, there will be an empty space
If the next space to be allocated is larger than all the vacant spaces, this will be a problem.
This makes it difficult for debugging because the total free memory on the heap is larger than the one to be allocated, but the memory request can fail because the free space is not contiguous.
For example, assume the following:
The location of the heap is 0x20000-0x20FFF
An 8-byte block of memory is distributed at the beginning of the interval.
Followed by a 1024-byte block
After that, the first block is released and can be used again. However, the application needs to allocate more than 8 bytes of space, so the first 8 bytes of space cannot be used. This is memory fragmentation, which is wasted if the application no longer requires 8 bytes or less. This example also illustrates why small blocks of memory in the heap are inefficient, and this overhead is equal to the amount of data available to the application.
Layout of heap
In some cases it is useful to know exactly what is on the heap and where it is located.
The heap contains more than just dispatched data:
There is some extra overhead of maintaining the heap
Each allocated memory block contains two integers, the size of the pointer depends on the architecture of CPU, and e.g. ARM devices use 32bit integers.
The first integer indicates the size of the allocated memory block.
The last heap object is followed by a value of 32bit to indicate the remaining heap space.
The 32-bit value at the beginning of the last heap object indicates the address of this data.
The heap also tracks the location of the next allocation, using a structure that resides outside the heap.
Typedef struct {
_ data_Cell _ _ data * _ _ data * _ Plast
_ _ data_Cell _ _ data * _ Head;}
_ _ data_Altab
The pointer _ Head describes the current state of the heap. The pointer _ Plast indicates where to start searching for available blocks that are not currently in use.
The _ _ data_Cell structure is defined as follows:
Typedef struct _ _ data_Cell
{
_ _ data_size_t _ Size
Struct _ data_Cell _ data * _ Next
} _ _ data_Cell
Where _ Size indicates the remaining amount of the heap, and _ Next indicates the next available allocated memory address. If the heap space is exhausted, the _ Next pointer is NULL.
Final thinking of heap
Without some tools to help you analyze dynamic memory requirements, it is difficult to estimate heap space directly.
There is such a tool in the desktop Java (HAT,Heap Analysis Tool)
There are no similar tools in embedded CumberCraft +.
For embedded systems with limited resources, dynamic memory should be used as little as possible due to additional overhead and possible fragmentation of heap memory.
You can create multiple structures or objects in your code (in test cases)
MISRA C requires that all memory must be statically allocated at compile time, so you will never run out of heap space.
Heap statistics in EWARM
The C-Spy debugger in EWARM allows you to see the current heap usage
To see this feature, dlmalloc.c must be included in the project
Calling _ _ iar_dlmallinfo () and _ _ iar_dlmalloc_stats () prints heap statistics in the Terminal I _ iar_dlmalloc_stats O window.
4. If given too much stack space, RAM is wasted, and if the given stack space is too small, two situations occur (depending on the location of the stack in memory space):
The a variable can be overwritten, resulting in undefined behavior
The b stack will exceed the memory space, resulting in abnormal termination of the application
Because the second option is used for detection, you should consider setting the stack to grow towards memory focus.
Static stack checker
There are many static C language checkers
Express Logic's StackX
Abslnt's StackAnalyzer
John Regher's Stack Analysis (for AVR/430 only)
AdaCore's GNATStack
Embedded Workbench's Stack Plug-in
EW contains an easy-to-use stack plug-in that monitors the size of the CSTACK and outputs messages to the debug log if you exceed the specified stack threshold.
Warning: this plug-in is not valid for RTOS, which always reports stack overflows when using RTOS.
The above is the analysis of how to use heap and stack in IAR shared by Xiaobian. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.