Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of memory allocation Strategy in Utility

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly shows you the "example analysis of memory allocation strategy in Utility", which is easy to understand and clear. I hope it can help you solve your doubts. Let me lead you to study and study the "example analysis of memory allocation strategy in Utility".

Allocating memory from the system memory pool (that is, heap) mainly uses these functions defined by ANSI: typedef unsigned int size_t;void * malloc (size_t nBytes); void * calloc (size_t elemNum, size_t elemSize); void * realloc (void * pBlock, size_t newSize); void free (void * ptr); VxWorks also implements a cfree () typedef int _ Vx_STATUS;typedef _ Vx_STATUS STATUS;STATUS cfree (char * pBlock) that works specifically with calloc ()

And how exactly are those blocks of memory stored in the heap managed?

The strategy used by Vx5 is First Fit, which can be translated into the first allocation algorithm. Under this strategy, all free memory blocks are arranged by address from lowest to highest. When you need to apply for memory, start with a low address, and the first block of memory that meets the requirements is allocated. So when the system requests more memory, the low address will leave a large number of small blocks of memory, resulting in a slightly longer search time in the later stage. The approximate code is as follows

Void * memXxxAlloc (PART_ID partId, / * memory partition to allocate from * / unsigned nBytes, / * number of bytes to allocate * / unsigned alignment/* boundary to align to * /) {. DL_NODE * pNode = DLL_FIRST (& partId- > freeList); While (pNode! = NULL) {/ * fits if: *-blocksize > requested size + extra room for alignment or *-block is already aligned and exactly the right size * / if ((NODE_TO_HDR (pNode)-> nWords > nWordsExtra) | | (NODE_TO_HDR (pNode)-> nWords = = nWords) & & (ALIGNED (HDR_TO_BLOCK (NODE_TO_HDR (pNode)), alignment) break PNode = DLL_NEXT (pNode);}.} when it comes to Vx6&7, the strategy used is Best Fit, which can be translated into an optimal allocation algorithm. Under this strategy, all free memory blocks are arranged from small to large and maintained using AVL (balanced binary tree). When memory is requested, the search starts with the Root node, and the memory block that meets the demand and the smallest size is allocated. This algorithm retains large blocks of memory, improving the overall probability of successful allocation, but with some very small fragments, but the time complexity is only o (ln (N)). The approximate code is as follows: AVLU_NODE * avlXxxGet (AVLU_TREE root, / * root node pointer * / UINT key / * search key * /) {AVLU_NODE * pNode; AVLU_NODE * pSuccessor; pNode = root; pSuccessor = NULL; while (pNode! = NULL) {if (key > = pNode- > key) {pNode = pNode- > right } else {pSuccessor = pNode; pNode = pNode- > left;}} return (pSuccessor);} of course, no matter which strategy, when memory is freed, it is automatically merged with the front and back free blocks, if any. Otherwise, the system will be fragmented after a while.

Computer professional children's shoes should know that there is another strategy called Worst Fit, which can be translated into the worst allocation algorithm. Under this strategy, all free memory blocks are arranged from large to small in size. When you need to apply for memory, either allocate the first block, if it fails. Therefore, the allocation speed of this algorithm is the fastest, and there are few small fragments, but it is easy to cause the failure of large memory applications, so it is suitable for the situation where the memory blocks to be allocated are more unified.

The above is all the contents of the article "sample Analysis of memory allocation policies in Utility". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, 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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report