In-depth analysis on the principle of ucgui dynamic memory allocation

Source: Internet
Author: User

In-depth analysis on the principle of ucgui dynamic memory allocation
Memory, ucgui, principle, dynamic
 
........................................................................................................................ ......
If you know about ucgui, you must know the Window System in ucgui.ProgramDynamic Memory creation, so dynamic memory application is required. Now we will analyze this topic to understand the dynamic memory allocation in ucgui, it is very important to understand the basis of its window system.

The following are some key words used in this article:

[Memory allocation information node] -------- A tblock struct that records the information of allocated memory blocks, which can be referred to as the allocation node.

[Memory allocation information node array] --- an array of memory allocation information nodes.

[Memory handle] ------------------- refers to allocating the index value of the element position in the memory block array.

[Minimum granularity alignment] ------------- indicates that the memory allocation size should be an integer multiple of the minimum granularity.

1. Enable the predefined option for dynamic allocation.

There is such a definition in the guiconf. h configuration file.

# Define gui_alloc_size 12500/* size of dynamic memory... For WM and memory devices */

Gui_alloc_size defines the size of the memory that can be dynamically allocated in the whole ucgui. The size cannot be 0, and only after the predefined value is enabled, the dynamic memory allocation function provided by ucgui can be used in guialloc. c file.

2. Basic functions of dynamic memory allocation.

The following definition is provided in gui. h:

/*************************************** ******************************

*

* Dynamic memory management

*

**************************************** ******************************

*/

# If! Defined (gui_alloc_alloc)

Void gui_alloc_init (void );

Void * gui_alloc_h2p (gui_hmem hmem );

Void gui_alloc_free (gui_hmem hmem );

Void gui_alloc_freeptr (gui_hmem * phmem );

Gui_hmem gui_alloc_alloc (INT size );

/* Diagnostics */

Int gui_alloc_getused (void );

Int gui_alloc_getnumfreebytes (void );

Int gui_alloc_getmaxsize (void );

/* Macros */

# Define gui_alloc_alloc (size)

# Define gui_alloc_h2p (h) gui_alloc_h2p (h)

# Define gui_alloc_free (handle)

# Define gui_alloc_lock (handle) gui_alloc_h2p (handle)

# Define gui_alloc_unlock (handle)

# Endif

In general, dynamic memory allocation provides the following functions:

1. dynamic memory initialization.

[Gui_alloc_init]

2. dynamic memory allocation, release, unlock, and fragment.

[Gui_alloc_free/gui_alloc_alloc], [gui_alloc_lock/gui_alloc_unlock]

3. dynamic memory usage statistics.

[Gui_alloc_getused], [gui_alloc_getnumfreebytes], [gui_alloc_getmaxsize]

Three dynamic memory allocation implementation principle.

1. First, we will introduce several constants and structures related to dynamic memory allocation.

---- Constant

Gui_alloc_size ---------------- the size that can be used for allocation. For example, to enable dynamic memory allocation, it is specified that the value must be greater than 0. Otherwise, compilation fails.

Gui_alloc_autdefrag ------------- whether to perform fragment. fragment is required only when the request cannot meet the requirements in the memory. All allocated memory data must be moved forward. For example, the total size is 12500, when the last 200 bytes are configured, But 800 bytes are requested, if fragment is defined, unused fragments are sorted out and all allocated memory is moved forward, merge the fragments to the backend to form a large surplus space.

Gui_block_align ----------------- alignment of memory allocation, which is to ensure that each memory allocated starts from the alignment granularity and has a value of 4 bytes. 29 ~ 31 bytes are 32 bytes, namely (29 + 3) & 0 xfffffffc, which is completed in size2legalsize.

Gui_maxblocks ------------------- maximum number of memory blocks that can be divided. It is the size of the memory allocation information record array. It determines the minimum value of each block when the memory is allocated. The minimum value is 32, in the future, we call each element [memory allocation information node].

Tallocint ------------------------- the type of variable that records the memory offset start point of each memory. The value is 2 bytes or 4 bytes. When gui_alloc_size is greater than 32767, the value must be 4 bytes.

Handle -------------------------- memory block handle type, 1 byte or 2 byte. When gui_maxblocks is greater than 256, 2 bytes are used.

# If gui_alloc_size: <32767

# Define tallocint I16

# Else

# Define tallocint i32

# Endif

# If gui_maxblocks >=256

# Define handle 2010

# Else

# Define handle u8

# Endif

The definition of tallocint and handle affects the size of information nodes used to record each piece of memory, that is, the deactivating of dynamic memory allocation.

---- Structure

Node structure that records information about each memory block.

Typedef struct {

Tallocint off;/* offset of memory area */

Tallocint size;/* usable size of allocated block */

Handle next;/* Next handle in linked list */

Handle Prev;

} Tblock;

[Off] ------------------------ records the offset of the block memory relative to the starting point of the entire memory.

[Size] ---------------------- record the memory size of this block.

[Next] ---------------------- record the pointer of the next memory block. In fact, this indicates that the next block contains the memory allocation information in the record array.

Elements.

[Prev] ---------------------- record the memory pointer of this block.

[The following structure is defined in Gui. H]

Typedef Union {

Int aintheap [gui_alloc_size/4];/* required for proper alignement */

U8 abheap [gui_alloc_size];

} Gui_heap;

Gui_heap;/* Public for debugging only */

Static tblock ablock [gui_maxblocks];

From the above, we can know that the memory allocation in ucgui is actually implemented through a large global array space, which is allocated after the program is compiled. Its size is in guiconf. the pre-defined gui_alloc_size in H is based on 1-byte access with abheap and 4-byte access with gui_heap.

Ablock is an array used to record all memory allocation blocks. The size is gui_maxblocks and gui_maxblocks = (2 + gui_alloc_size/32). Each element records the information of one memory allocation block, as long as you know the location of the memory allocation block, you can retrieve the information of the allocation block from the array. In fact, all the memory allocation blocks are not only recorded in this elder sister, but also constitute a double-stranded table, which is very convenient for traversing all the allocated memory blocks.

Struct {

Int numusedblocks, numfreeblocks, numfreeblocksmin;/* for statistical purposes only */

Tallocint numusedbytes, numfreebytes, numfreebytesmin;

} Gui_alloc;

The above structure records the [used blocks], [idle blocks], [minimum idle blocks], [allocated bytes], [remaining bytes], and [minimum idle bytes] after each memory allocation. byte]. In fact, the minimum idle block and the minimum idle byte are similar to the idle block and the remaining byte, and their values are equal.

2. Detailed description of functions for implementing dynamic memory allocation.

----- Initialization

Void gui_alloc_init (void );

It mainly initializes the overall memory allocation information structure of gui_alloc, and sets the initialized state to initialize the first memory allocation node:

Ablock [0]. size = (1 <gui_block_align);/* occupy minimum for a block */

Ablock [0]. Off = 0;

Ablock [0]. Next = 0;

Note that this node is actually used for the head node of the double-stranded table. Its size is the minimum allocation alignment granularity. The offset is 0, starting from the entire dynamic memory. It always exists and will not be released or changed. In short, it will not change. When all the blocks are allocated or released, it will be the header node. In fact, it is used to maintain the memory allocation information node double-stranded table.

----- Allocate

Gui_hmem gui_alloc_alloc (INT size );

This function actually calls _ alloc to allocate memory. This function mainly completes the following operations.

[1]. Call size2legalsize to adjust the allocated memory size to the minimum granularity alignment.

[2]. Find the node that can be used to record the allocation information of this block and complete it in findfreehandle.

[3] Looking for a region that can meet the allocation from low to high in the entire memory allocation space, completed in findhole, there are several situations that will be described in findhole.

[4] if the remaining bytes are not allocated enough, createhole is called to sort out the pre-defined fragments and createhole is used to allocate information nodes back.

[5]. Add the allocated memory allocation information node to the double-stranded table, initialize the allocated memory to 0, and update the overall allocation structure information of gui_alloc.

[size2legalsize]

Size = (size + (1 <gui_bloc

K_align)-1 ))&~ (1 <gui_block_align)-1 );

This function is mainly used to adjust the size to be allocated to the smallest granularity alignment. This alignment method is common, that is, adding a value to the value to be adjusted produces a carry with the smallest granularity, then, the remainder is cleared by the sum. The added value is the minimum granularity minus one, and the value is the minimum level minus one.

[Findfreehandle]

This function is very simple, that is, to find unused nodes from the ablock array of the memory allocation information node (that is, the. SISE value is 0). Note that it is found from node 1 and node 0 is used. If no gui_hmem_null (0) is returned, when gui_alloc_alloc detects this value, it returns the allocated memory handle as 0. When converting the memory sentence handle in gui_alloc_h2p, if the memory handle is detected to be 0, the corresponding memory address value of this memory handle is 0.

[Findhole]

Traverse all the allocated nodes in the double-stranded table to find the region of the memory to be allocated. There are two situations:

[1]. The region is located between allocated segments. This condition does not occur when the memory is initially allocated, in this case, when a new memory is allocated after the memory is released, the search is based on the offset of the last allocation node minus the offset of the previous allocation node plus the size, that is, ablock [inext]. off-(ablock [I]. off + ablock [I]. size), check whether there is a gap between the last node and the previous one, and whether the gap meets the allocation. This gap is actually caused by the release of memory blocks.

[2] after all the allocated nodes are located in the region, the appropriate region cannot be found between the allocated nodes. Therefore, the region can only be retrieved from the remaining space, if the remaining space is sufficient,-1 is returned.

When findhole finds a region that can satisfy the allocation, its return value is the memory handle of the nearest region of the allocable region. That is, the index of the allocated node's position in the number of nodes.

[Createhole]

If findhole does not find a suitable region for allocation, return-1. In this case, not a whole block of memory in all memory can meet the allocation, the reason for the failure to meet the allocation may be that too much small memory is allocated and released to form fragments. These fragments are mixed across the entire memory, therefore, the possible solution is to combine these fragments in one place to form a large memory. In the fragmented version, createhole must complete the following two tasks:

[1]. First, you need to find the gap from the allocated node. The method for finding the gap is space = ablock [inext]. off-(ablock [I]. off + ablock [I]. size), when space is smaller than the size of the memory to be allocated, it will be the object to be sorted.

[2]. to sort out the shard space, ensure that the allocated memory data and normal access are ensured. Therefore, move the data of the last two nodes with gaps forward, and adjust the offset of the last node, which is the point of attention.

[3]. Finally, the memory is after all allocated nodes. When gui_alloc_size-(ablock [I]. off + ablock [I]. size)> = the size condition is met. That is, if the remaining space obtained after the fragment is adjusted to meet the allocation condition, the I value is returned. The I value is the last node in the double-link table; if the shard cannot be allocated for several times after adjustment, the condition above is not true, then-1 is returned, that is, the allocation fails.

Summary: fragment sorting takes a long time, and this time can be uncertain every time.

----- Release

Release and shard ratio, less work is done.

[Gui_alloc_free] And gui_alloc_freeptr perform the same function with different parameters.

[1] based on the memory handle specified in the parameter, reset the size of the allocated node to 0xcc and clear the node from the double-link table.

[2]. Update the overall memory usage information recorded in gui_alloc.

----- Obtain the overall memory usage

This group of functions is relatively simple and only briefly described. Its information is basically obtained from the gui_alloc structure.

Gui_getusef8 --------------- obtain the number of bytes in used memory numusedbytes.

Gui_alloc_getnumfreebytes ---- obtain the number of remaining memory bytes numfreebytes.

Gui_alloc_getmaxsize --------- traverse all allocated nodes to find the maximum number of bytes in the remaining region among the allocated nodes, and compare it with the remaining memory after the last node to find the maximum number of remaining memory bytes.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.