Memory resource allocation method and device, electronic equipment and storage medium

By presetting a memory allocator in the electronic device to manage the memory page and allocating memory blocks or sub-blocks according to the size of the applied memory resource, the problems of low memory resource allocation efficiency and high fragmentation rate in the prior art are solved, and more efficient memory utilization is achieved.

CN119557091APending Publication Date: 2025-03-04BEIJING PIXEL SOFTWARE TECH
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202411634049.2
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-11-15
Publication Date
2025-03-04

AI Technical Summary

Technical Problem

Existing memory resource allocation methods require the creation of multiple memory allocators, resulting in increased overhead and may result in some space in the memory block being unused, resulting in memory fragmentation and reducing memory utilization.

Method used

By presetting a memory allocator in the electronic device, a memory page includes multiple memory blocks of the same size. For the memory resources applied for allocation, if the size is equal to the memory block size, it will be allocated directly; if the size is less than the memory block size, the memory block will be split to obtain the memory subblocks of the matching size and allocate them.

Benefits of technology

No need to create multiple memory allocators, reducing overhead and reducing memory fragmentation and improving memory utilization.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119557091A_ABST
    Figure CN119557091A_ABST
Patent Text Reader

Abstract

The invention relates to the technical field of memory management, and provides a memory resource allocation method and device, electronic equipment and a storage medium. The method is applied to the electronic equipment, a memory allocator used for managing a memory page is preset in the electronic equipment, the memory page comprises a plurality of memory blocks with the same size, and the method comprises the steps that when an application for allocating memory resources is received through the memory allocator, the size of the memory resources to be allocated is obtained; when the size of the to-be-allocated memory resource is equal to the size of the memory block, obtaining a target memory block, and allocating the target memory block through a memory allocator; and when the size of the to-be-allocated memory resource is smaller than that of the memory block, obtaining a target memory block, splitting the target memory block to obtain a target memory sub-block matched with the size of the to-be-allocated memory resource, and allocating the target memory sub-block through the memory allocator. Therefore, a plurality of memory distributors do not need to be created, the overhead is reduced, memory fragments are reduced, and the utilization rate of the memory is improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of memory management technology, and in particular to a memory resource allocation method, device, electronic device and storage medium. Background Art

[0002] At present, the memory resource allocation method generally allocates memory blocks of different sizes by creating multiple memory allocators. That is, when applying for memory resource allocation, a memory allocator whose managed memory block size is close to the required memory resource size will be found from these memory allocators, and the memory block will be allocated using this memory allocator. However, this method requires the creation of multiple memory allocators, which will increase the overhead, and the memory block size specifications that these memory allocators can provide are also limited. Then, it is possible that only part of the space in the memory block is used because the required memory resource size is smaller than the allocated memory block size, thereby generating memory fragmentation and causing low memory utilization. Summary of the invention

[0003] In view of this, an object of the present invention is to provide a memory resource allocation method, device, electronic device and storage medium.

[0004] In order to achieve the above object, the technical solution adopted by the present invention is as follows:

[0005] In a first aspect, the present invention provides a memory resource allocation method, which is applied to an electronic device, wherein the electronic device is preset with a memory allocator, the memory allocator is used to manage a memory page, the memory page includes a plurality of memory blocks of the same size, and the memory resource allocation method includes:

[0006] When an application for allocating memory resources is received through the memory allocator, the size of the memory resources to be allocated is obtained;

[0007] When the size of the memory resource to be allocated is equal to the size of the memory block, obtaining a target memory block, and allocating the target memory block through the memory allocator;

[0008] When the size of the memory resource to be allocated is smaller than the size of the memory block, a target memory block is obtained, and the target memory block is split to obtain a target memory sub-block matching the size of the memory resource to be allocated, and the target memory sub-block is allocated through the memory allocator.

[0009] In an optional implementation, all memory blocks in the memory page are connected via a block linked list, and a header of the block linked list is used to store an address of the first currently free memory block;

[0010] The step of obtaining the target memory block includes:

[0011] If the header of the block linked list stores an address, the first free memory block is obtained according to the address to obtain the target memory block;

[0012] If the header of the block linked list stores a preset null character, a new memory page is applied for through the memory allocator, and the new memory page is divided into multiple memory blocks of the same size, and the first memory block of the new memory page is obtained to obtain the target memory block.

[0013] In an optional implementation manner, the first k bytes of each of the memory blocks store a block pointer, and the block pointer is the address of the next memory block or the null character;

[0014] After the step of obtaining the target memory block, the memory resource allocation method further includes:

[0015] The block pointer stored in the first k bytes of the target memory block is obtained to obtain a target block pointer, and the content stored in the header of the block linked list is updated to the target block pointer.

[0016] In an optional implementation manner, the step of splitting the target memory block to obtain target memory sub-blocks matching the size of the memory resources to be allocated includes:

[0017] Using the target memory block as a memory block to be processed;

[0018] Divide the to-be-processed memory block into a front memory sub-block and a rear memory sub-block;

[0019] Creating a sub-block linked list to connect the previous memory sub-block and the next memory sub-block;

[0020] Determine whether the size of the memory resource to be allocated is less than or equal to half the size of the previous memory sub-block;

[0021] If yes, after taking the previous memory sub-block as a new memory block to be processed, repeat the step of dividing the memory block to be processed into the previous memory sub-block and the next memory sub-block;

[0022] If not, the previous memory sub-block is used as the target memory sub-block, and each sub-block linked list is obtained.

[0023] In an optional implementation, each of the sub-block linked lists has a corresponding memory size specification, and the memory resource allocation method further includes:

[0024] When receiving an application to release part of the memory resources through the memory allocator, obtaining the size and address of the memory resources to be released;

[0025] Compare the size of the memory resource to be released with the size of the memory block;

[0026] When the size of the memory resource to be released is equal to the size of the memory block, the memory resource to be released is released by the memory allocator to obtain a free memory block, and the free memory block is inserted into the block linked list according to the address of the memory resource to be released;

[0027] When the size of the memory resource to be released is smaller than the size of the memory block, selecting a target sub-block linked list with a memory size specification matching the size of the memory resource to be released from all sub-block linked lists, and querying in the target sub-block linked list whether there is an associated memory sub-block adjacent to the address of the memory resource to be released;

[0028] If so, the memory resource to be released and the associated memory sub-block are combined into a new memory resource to be released and the size and address of the memory resource to be released are obtained, and then the step of comparing the size of the memory resource to be released with the size of the memory block is performed again;

[0029] If not, the memory resource to be released is released by the memory allocator to obtain a free memory sub-block, and the free memory sub-block is inserted into the target sub-block linked list according to the address of the memory resource to be released.

[0030] In an optional implementation, there are multiple memory pages and all the memory pages are connected through a page linked list, the header of the page linked list is used to store the address of the currently released memory page, the first k bytes of each of the memory pages store a page pointer, and the page pointer is the address of the previous memory page or a preset null character, and the memory resource allocation method also includes:

[0031] When an application for releasing all memory resources is received through the memory allocator, the content stored in the header of the page linked list is obtained to obtain the content to be processed;

[0032] When the content to be processed is an address, a currently released memory page is obtained according to the address to obtain a target memory page, and a page pointer stored in the first k bytes of the target memory page is obtained to obtain a target page pointer;

[0033] The content stored in the header of the page linked list is updated to the target page pointer, and after the target memory page is released by the memory allocator, the step of obtaining the content stored in the header of the page linked list to obtain the content to be processed is repeated until the content to be processed is the null character, and all memory pages are released.

[0034] In a second aspect, the present invention provides a memory resource allocation device, which is applied to an electronic device, wherein the electronic device is preset with a memory allocator, the memory allocator is used to manage a memory page, the memory page includes a plurality of memory blocks of the same size, and the memory resource allocation device includes:

[0035] A receiving module, configured to obtain the size of the memory resources to be allocated when an application for allocation of memory resources is received through the memory allocator;

[0036] An allocation module, configured to obtain a target memory block when the size of the memory resource to be allocated is equal to the size of the memory block, and allocate the target memory block through the memory allocator;

[0037] When the size of the memory resource to be allocated is smaller than the size of the memory block, a target memory block is obtained, and the target memory block is split to obtain a target memory sub-block matching the size of the memory resource to be allocated, and the target memory sub-block is allocated through the memory allocator.

[0038] In an optional implementation, all memory blocks in the memory page are connected via a block linked list, a header of the block linked list is used to store an address of a current first free memory block, and the allocation module is further used to:

[0039] If the header of the block linked list stores an address, the first free memory block is obtained according to the address to obtain the target memory block;

[0040] If the header of the block linked list stores a preset null character, a new memory page is applied for through the memory allocator, and the new memory page is divided into multiple memory blocks of the same size, and the first memory block of the new memory page is obtained to obtain the target memory block.

[0041] In a third aspect, the present invention provides an electronic device, including a processor and a memory, wherein the memory stores a computer program, and when the processor executes the computer program, the memory resource allocation method described in any one of the aforementioned embodiments is implemented.

[0042] In a fourth aspect, the present invention provides a storage medium having a computer program stored thereon, and when the computer program is executed by a processor, the memory resource allocation method described in any one of the aforementioned embodiments is implemented.

[0043] The memory resource allocation method, device, electronic device and storage medium provided by the present invention are applied to electronic devices, the electronic devices are preset with a memory allocator for managing memory pages, the memory pages include multiple memory blocks of the same size, and the method includes: when an application for memory resource allocation is received through the memory allocator, the size of the memory resource to be allocated is obtained; when the size of the memory resource to be allocated is equal to the size of the memory block, the target memory block is obtained, and the target memory block is allocated through the memory allocator; when the size of the memory resource to be allocated is less than the size of the memory block, the target memory block is obtained, and the target memory block is split to obtain a target memory sub-block matching the size of the memory resource to be allocated, and the target memory sub-block is allocated through the memory allocator. There is no need to create multiple memory allocators, which reduces overhead, reduces memory fragmentation, and improves memory utilization.

[0044] In order to make the above-mentioned objects, features and advantages of the present invention more obvious and easy to understand, preferred embodiments are given below and described in detail with reference to the accompanying drawings. BRIEF DESCRIPTION OF THE DRAWINGS

[0045] In order to more clearly illustrate the technical solutions of the embodiments of the present invention, the drawings required for use in the embodiments are briefly introduced below. It should be understood that the following drawings only show certain embodiments of the present invention and therefore should not be regarded as limiting the scope. For ordinary technicians in this field, other related drawings can be obtained based on these drawings without creative work.

[0046] Figure 1 A block diagram of an electronic device provided by an embodiment of the present invention is shown;

[0047] Figure 2 One of the flow diagrams of the memory resource allocation method provided in an embodiment of the present invention is shown;

[0048] Figure 3 One of the example diagrams of the memory resource allocation method provided by an embodiment of the present invention is shown;

[0049] Figure 4 A second example diagram of a method for allocating memory resources provided by an embodiment of the present invention is shown;

[0050] Figure 5 A third example diagram of a memory resource allocation method provided by an embodiment of the present invention is shown;

[0051] Figure 6 The second flowchart of the memory resource allocation method provided by the embodiment of the present invention is shown;

[0052] Figure 7 The third flowchart of the memory resource allocation method provided by the embodiment of the present invention is shown;

[0053] Figure 8 A functional module diagram of a memory resource allocation device provided in an embodiment of the present invention is shown.

[0054] Icons: 100 - electronic device; 110 - processor; 120 - memory; 130 - communication module; 300 - memory resource allocation device; 310 - receiving module; 330 - allocation module; 350 - release module. DETAILED DESCRIPTION

[0055] The following will be combined with the accompanying drawings in the embodiments of the present invention to clearly and completely describe the technical solutions in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Generally, the components of the embodiments of the present invention described and shown in the drawings here can be arranged and designed in various different configurations.

[0056] Therefore, the following detailed description of the embodiments of the present invention provided in the accompanying drawings is not intended to limit the scope of the invention claimed for protection, but merely represents selected embodiments of the present invention. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without making creative work are within the scope of protection of the present invention.

[0057] It should be noted that relational terms such as "first" and "second" are used only to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Moreover, the terms "include", "comprise" or any other variants thereof are intended to cover non-exclusive inclusion, so that a process, method, article or device including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, article or device. In the absence of further restrictions, the elements defined by the sentence "comprise a ..." do not exclude the existence of other identical elements in the process, method, article or device including the elements.

[0058] See also Figure 1 , is a block diagram of an electronic device provided by an embodiment of the present invention. The electronic device 100 includes a processor 110, a memory 120, and a communication module 130, and each component is directly or indirectly electrically connected to each other to achieve data transmission or interaction. For example, these components can be electrically connected to each other through one or more communication buses or signal lines.

[0059] The processor 110 is used to read / write data or programs stored in the memory 120 and execute corresponding functions. It can be a general-purpose processor, including a CPU (Central Processing Unit), NP (Network Processor), etc.; it can also be a DSP digital signal processor, an ASIC application-specific integrated circuit, an FPGA off-the-shelf programmable gate array or other programmable logic devices, discrete gates or transistor logic devices, and discrete hardware components.

[0060] The memory 120 is used to store programs or data. The memory 120 can be a RAM (Random Access Memory), a ROM (Read Only Memory), a PROM (Programmable Read-Only Memory), an EPROM (Erasable Programmable Read-Only Memory), an EEPROM (Electric Erasable Programmable Read-Only Memory), etc.

[0061] The communication module 130 is used to perform signaling or data communications with other devices.

[0062] Understandably, Figure 1 The structure shown is only a schematic diagram of the structure of the electronic device 100. The electronic device 100 may also include Figure 1 More or fewer components as shown, or with Figure 1 Different configurations are shown. Figure 1 Each component shown in the figure can be implemented by hardware, software or a combination thereof.

[0063] The above-mentioned electronic device 100 will be used as an execution subject to execute each step in each method provided in the embodiment of the present invention and achieve corresponding technical effects.

[0064] See also Figure 2 , is a flow chart of a memory resource allocation method provided in an embodiment of the present invention.

[0065] Step S202: when an application for allocating memory resources is received through the memory allocator, the size of the memory resources to be allocated is obtained.

[0066] In this embodiment, a memory allocator is pre-set in the electronic device, and the memory allocator is used to manage memory pages, and a memory page includes multiple memory blocks of the same size. When an application for allocation of memory resources is received through the memory allocator, the size of the memory resources to be allocated can be obtained, that is, the size of the memory resources to be allocated can be obtained, such as represented by Size.

[0067] Step S204, when the size of the memory resource to be allocated is equal to the size of the memory block, obtain the target memory block, and allocate the target memory block through the memory allocator.

[0068] Step S206, when the size of the memory resource to be allocated is smaller than the size of the memory block, obtain the target memory block, split the target memory block, obtain target memory sub-blocks matching the size of the memory resource to be allocated, and allocate the target memory sub-blocks through the memory allocator.

[0069] In this embodiment, the size of the memory block can be preset, such as represented by N. The size of the memory resource to be allocated is compared with the size of the memory block. If the size of the memory resource to be allocated is exactly equal to the size of the memory block, that is, Size = N, it means that the memory resource applied for allocation can just occupy a memory block, then a free memory block is obtained to obtain the target memory block, and the target memory block is allocated through the memory allocator.

[0070] If the size of the memory resources to be allocated is smaller than the size of the memory block, that is, Size<N, it means that the memory resources applied for allocation this time do not occupy a memory block. Then, a free memory block is obtained to obtain the target memory block, and the target memory block is split to obtain the target memory sub-block that matches the size of the memory resources to be allocated, and then the target memory sub-block is allocated through the memory allocator.

[0071] It can be understood that, in the embodiment of the present invention, when the size of the memory resource applied for allocation is smaller than the size of the memory block, the memory block is split to obtain memory sub-blocks of matching sizes, and a memory allocator is used to allocate memory of different sizes. Therefore, there is no need to create multiple memory allocators, which reduces overhead, reduces memory fragmentation, and improves memory utilization.

[0072] It can be seen that based on the above steps, when an application for memory resource allocation is received through the memory allocator, the size of the memory resource to be allocated is obtained; when the size of the memory resource to be allocated is equal to the size of the memory block, the target memory block is obtained, and the target memory block is allocated through the memory allocator; when the size of the memory resource to be allocated is smaller than the size of the memory block, the target memory block is obtained, and the target memory block is split to obtain a target memory sub-block that matches the size of the memory resource to be allocated, and the target memory sub-block is allocated through the memory allocator. Therefore, there is no need to create multiple memory allocators, which reduces overhead, reduces memory fragmentation, and improves memory utilization.

[0073] Optionally, for the process of obtaining the target memory block in step S204 and step S206, an embodiment of the present invention provides a possible implementation method.

[0074] Step S208A, if the header of the block linked list stores an address, the first free memory block is obtained according to the address to obtain the target memory block.

[0075] Step S208B, if the header of the block linked list stores a preset null character, a new memory page is applied for through the memory allocator, and the new memory page is divided into multiple memory blocks of the same size, and the first memory block of the new memory page is obtained to obtain the target memory block.

[0076] It can be understood that all memory blocks in a memory page are connected through a block linked list, and the header of the block linked list is used to store the address of the first free memory block. Then the target memory block can be obtained according to the block linked list. The block linked list can be represented by Freeblock.

[0077] In this embodiment, the content stored in the header of the block linked list Freeblock is obtained. If the content is an address, the first free memory block is obtained according to the address, and the target memory block is obtained. If the content is a preset null character such as Nullptr, it means that there is no free memory block at this time, so a new memory page is applied through the memory allocator, and the new memory page is divided into multiple memory blocks of size N, and then the first memory block of the new memory page is obtained to obtain the target memory block.

[0078] Optionally, after acquiring the target memory block, an embodiment of the present invention also provides a possible implementation method, namely: acquiring the block pointer stored in the first k bytes of the target memory block to obtain the target block pointer, and updating the content stored in the header of the block linked list to the target block pointer.

[0079] It can be understood that the first k bytes of each memory block in a memory page store a block pointer, which is the address of the next memory block or a null character. That is, for the last memory block in a memory page, since there is no memory block after it, its block pointer is a null character; for other memory blocks in a memory page, since there is a next memory block after them, their block pointers are the addresses of the next memory blocks.

[0080] In this embodiment, since the header of the block list Freeblock is used to store the address of the current first free memory block, after the target memory block is allocated, the content stored in the header of the block list Freeblock needs to be updated. That is, the block pointer stored in the first k bytes of the target memory block is obtained, that is, the target block pointer is obtained, and the content stored in the header of the block list Freeblock is updated to the target block pointer.

[0081] If the target memory block is the last memory block, the target block pointer is a null character, and the content stored in the header of the block list Freeblock is updated to a null character. If the target memory block is not the last memory block, the target block pointer is the address of the next memory block after the target memory block, and the content stored in the header of the block list Freeblock is updated to the address of the next free memory.

[0082] It should be understood that, since a memory block uses k bytes to store a block pointer, when a memory block is allocated through a memory allocator, the first k bytes of the memory block need to be released first to obtain a blank memory block, and then the blank memory block is allocated.

[0083] Optionally, for the process of splitting the target memory block in step S206 to obtain target memory sub-blocks matching the size of the memory resources to be allocated, an embodiment of the present invention provides a possible implementation manner.

[0084] Step S206-1, taking the target memory block as the memory block to be processed.

[0085] Step S206-3, dividing the memory block to be processed into a front memory sub-block and a rear memory sub-block.

[0086] Step S206-5: create a sub-block linked list to connect the previous memory sub-block and the next memory sub-block.

[0087] Step S206-7, determining whether the size of the memory resource to be allocated is less than or equal to half the size of the previous memory sub-block.

[0088] Step S206-9A: If yes, then take the previous memory sub-block as a new memory block to be processed and repeat step S206-1.

[0089] Step S206-9B, if not, use the previous memory sub-block as the target memory sub-block, and obtain each sub-block linked list.

[0090] For ease of understanding, the embodiment of the present invention provides an example. For example, the size of the memory resource to be allocated, i.e., Size, is 32 bytes, and the memory page includes 8 memory blocks, such as memory block a to memory block h. The sizes of these 8 memory blocks, i.e., N, are all 128 bytes, and their addresses are 0, 127, 255, ... 895, respectively. Moreover, the first 8 bytes of each memory block are used to store a block pointer, such as Figure 3 As shown, the shaded area represents the space in the memory block used to store the block pointer.

[0091] Assuming that the header of the block linked list Freeblock currently stores the address and is 0, the target memory block is obtained by obtaining memory block a. Since the size of the memory resource to be allocated, i.e. 32 bytes, is smaller than the size of the memory block, i.e. 128 bytes, i.e. Size < N, memory block 1 needs to be split, and memory block a is used as the memory block to be processed.

[0092] First, release the first 8 bytes of memory block a, and divide memory block a into two memory sub-blocks of 64 bytes, so that the first memory sub-block, memory sub-block a1, and the second memory sub-block, memory sub-block a2, are obtained. The address of memory sub-block a1 is 0, and the first 8 bytes of memory sub-block a1 are used to store the block pointer, which is the address of memory sub-block a2; the address of memory sub-block a2 is 63, and the first 8 bytes of memory sub-block a2 are used to store the block pointer, which is a null character; Figure 4 As shown, the shaded area represents the space in the memory sub-block for storing block pointers.

[0093] And create a sub-block linked list such as Freelist_1 to connect memory sub-block a1 and memory sub-block a2, and the header of sub-block linked list Freelist_1 is used to store the current first free memory sub-block of size 64 bytes, that is, the header of sub-block linked list Freelist_1 currently stores the address of memory sub-block a1.

[0094] Then, it is determined whether the size of the memory resource to be allocated is less than or equal to half the size of the memory sub-block a1. Since the size of the memory resource to be allocated, i.e., 32 bytes, is equal to half the size of the memory sub-block a1, i.e., 32 bytes, the memory sub-block a1 needs to be split, and the memory sub-block a1 is used as a new memory block to be processed.

[0095] Release the first 8 bytes of memory sub-block a1, and divide memory sub-block a1 into two memory sub-blocks of 32 bytes, then get the first memory sub-block, memory sub-block a11, and the second memory sub-block, memory sub-block a12. The address of memory sub-block a11 is 0, and the first 8 bytes of memory sub-block a11 are used to store the block pointer, which is the address of memory sub-block a12; the address of memory sub-block a12 is 31, and the first 8 bytes of memory sub-block a12 are used to store the block pointer, which is a null character; Figure 5 As shown, the shaded area represents the space in the memory sub-block for storing block pointers.

[0096] And create a sub-block linked list such as Freelist_2 to connect memory sub-block a11 and memory sub-block a12, and the header of sub-block linked list Freelist_2 is used to store the current first free memory sub-block of size 32 bytes, that is, the header of sub-block linked list Freelist_2 currently stores the address of memory sub-block a11.

[0097] Next, it is determined whether the size of the memory resource to be allocated is less than or equal to half the size of the memory sub-block a11. Since the size of the memory resource to be allocated, i.e., 32 bytes, is greater than half the size of the memory sub-block a1, i.e., 16 bytes, the memory sub-block a1 is used as the target memory sub-block, and each sub-block linked list is obtained.

[0098] For the sub-block linked list Freelist_2, since the memory sub-block a11 in the sub-block linked list Freelist_2 has been allocated, the content stored in the header of the sub-block linked list Freelist_2 is updated to the address of the next memory sub-block after the memory sub-block a11, that is, the memory sub-block a12. And the sub-block linked list Freelist_2 only includes the memory sub-block a12.

[0099] For the sub-block linked list Freelist_1, since the memory sub-block a1 in the sub-block linked list Freelist_1 has been split, the content stored in the header of the sub-block linked list Freelist_1 is updated to the address of the next memory block after the memory sub-block a1, that is, the memory sub-block a2. And the sub-block linked list Freelist_1 only includes the memory sub-block a2.

[0100] For the block list Freeblock, since the memory block a in the block list Freeblock has been split as the target memory, the content stored in the header of the block list Freeblock is updated to the address of the next memory block after the memory block a, that is, the memory block b. And the block list Freeblock includes the memory blocks b to the memory blocks h.

[0101] It should be understood that the above splitting generates a memory sub-block of 64 bytes and a memory sub-block of 32 bytes. If the size of the memory resource to be allocated next time is not greater than 64 bytes, the memory can be directly allocated according to the memory sub-blocks in the sub-block linked list Freelist_1 and the memory sub-blocks in the sub-block linked list Freelist_2.

[0102] It should be noted that the size N of the memory block and the number of bytes k occupied by the block pointer can be set according to actual conditions and are not limited in the embodiment of the present invention. In addition, since the split memory sub-blocks need to use k bytes to store the block pointer, the allocation range of the memory resources applicable to the embodiment of the present invention is k to N bytes, and N is a multiple of k.

[0103] It can be understood that the embodiment of the present invention obtains and allocates memory sub-blocks matching the size of the applied memory resources by splitting the memory blocks, so only one memory allocator is needed to provide memory of different sizes for allocation, thereby reducing overhead, reducing memory fragmentation, and improving memory utilization.

[0104] Optionally, after the above step S204 or step S206, the embodiment of the present invention further provides a possible implementation method, see Figure 6 .

[0105] Step S210, when an application for releasing part of the memory resources is received through the memory allocator, the size and address of the memory resources to be released are obtained.

[0106] Step S212: compare the size of the memory resource to be released with the size of the memory block.

[0107] Step S214, when the size of the memory resource to be released is equal to the size of the memory block, the memory resource to be released is released by the memory allocator to obtain a free memory block, and the free memory block is inserted into the block linked list according to the address of the memory resource to be released.

[0108] Step S216, when the size of the memory resource to be released is smaller than the size of the memory block, a target sub-block linked list whose memory size specification matches the size of the memory resource to be released is selected from all sub-block linked lists, and a query is made in the target sub-block linked list to determine whether there is an associated memory sub-block adjacent to the address of the memory resource to be released.

[0109] Step S218A: if it exists, the memory resource to be released and the associated memory sub-block are combined into a new memory resource to be released and the size and address of the memory resource to be released are obtained, and then step S212 is executed again.

[0110] Step S218B: if it does not exist, the memory resource to be released is released by the memory allocator to obtain a free memory sub-block, and the free memory sub-block is inserted into the target sub-block linked list according to the address of the memory resource to be released.

[0111] In this embodiment, each sub-block linked list has a corresponding memory size specification. For ease of understanding, the above example is continued for explanation. Since the block linked list Freeblock is connected to a memory block of 128 bytes in size, the memory size specification corresponding to the block linked list Freeblock is 128 bytes. Since the sub-block linked list Freelist_1 is connected to a memory sub-block of 64 bytes in size, the memory size specification corresponding to the word block linked list Freelist_1 is 64 bytes. Since the sub-block linked list Freelist_2 is connected to a memory sub-block of 32 bytes in size, the memory size specification corresponding to the word block linked list Freelist_2 is 32 bytes.

[0112] When a request to release part of the memory resources is received through the memory allocator, the size of the memory resources to be released can be obtained, that is, the size and address of the memory resources to be released can be obtained. For example, if the size of the memory resources to be released is 32 bytes and the address is 0, the size of the memory resources to be released is compared with the size of the memory block.

[0113] Since the size of the memory resource to be released, i.e., 32 bytes, is smaller than the size of the memory block, i.e., 128 bytes, a target sub-block linked list whose memory size specification matches the size of the memory resource to be released is selected from all sub-block linked lists, i.e., the target sub-block linked list is the sub-block linked list Freelist_2. In addition, a query is made in the sub-block linked list Freelist_2 to determine whether there is an associated memory sub-block adjacent to the address of the memory resource to be released, i.e., 0.

[0114] Assuming that there is an associated memory sub-block adjacent to the address 0 in the sub-block linked list Freelist_2, such as a memory sub-block with an address of 31, the memory resource to be released is combined with the memory sub-block with an address of 31 to form a new memory resource to be released. After obtaining the size of the new memory resource to be released, which is 64 and the address is 0, the size of the memory resource to be released is compared with the size of the memory block again.

[0115] Since the size of the resource memory to be released, i.e., 64 bytes, is smaller than the size of the memory block, i.e., 128 bytes, a target sub-block linked list whose memory size specification matches the size of the memory resource to be released is selected from all sub-block linked lists, i.e., the target sub-block linked list is the sub-block linked list Freelist_1. In addition, a query is made in the sub-block linked list Freelist_1 to determine whether there is an associated memory sub-block adjacent to the address 0 of the memory resource to be released.

[0116] Assuming that there is an associated memory sub-block adjacent to the address 0 in the sub-block linked list Freelist_1, such as a memory sub-block with an address of 63, the memory resource to be released is combined with the memory sub-block with an address of 63 to form a new memory resource to be released. After obtaining the size of the new memory resource to be released, which is 128 and the address is 0, the size of the memory resource to be released is compared with the size of the memory block again.

[0117] Since the size of the memory resource to be released, i.e., 128 bytes, is equal to the size of the memory block, i.e., 128 bytes, the memory resource to be released is released through the memory allocator to obtain a free memory block, and the free memory block is inserted into the block list Freeblock according to the address of the resource to be released, i.e., 0. Assume that the first memory block in the block list Freeblock is the memory block with address 127, since the address of the free memory block, i.e., 0, is before address 127, the free memory block is inserted before the memory block with address 127, and the content stored in the header of the block list Freeblock is updated to the address of the free memory block, i.e., 0.

[0118] It can be understood that the embodiment of the present invention is to query whether there is memory with adjacent addresses in the corresponding linked list according to the size and address of the memory resources to be released during the process of releasing memory resources, and if found, the memory resources to be released are combined with the memory with adjacent addresses to form a memory with a larger specification, thereby reducing memory fragmentation and improving memory utilization.

[0119] Optionally, after the above step S204 or step S206, the embodiment of the present invention further provides another possible implementation method, see Figure 7 .

[0120] Step S220, when the memory allocator receives an application to release all memory resources, the content stored in the header of the page linked list is obtained to obtain the content to be processed.

[0121] Step S222, when the content to be processed is an address, the currently released memory page is obtained according to the address to obtain the target memory page, and the page pointer stored in the first k bytes of the target memory page is obtained to obtain the target page pointer.

[0122] Step S224, updates the content stored in the header of the page linked list to the target page pointer, and after releasing the target memory page through the memory allocator, repeats the step of obtaining the content stored in the header of the page linked list to obtain the content to be processed, until the content to be processed is a null character, and all memory pages are released.

[0123] It can be understood that there are multiple memory pages and all memory pages are connected through a page list. The header of the page list is used to store the address of the currently released memory page. The page list can be represented by Pagelist. And the first k bytes of each memory page store a page pointer, which is the address of the previous memory page or a null character. That is, for the first memory page, since there is no memory page before it, its page pointer is a null character; for other memory pages, since there is a previous memory page before them, their page pointer is the address of the previous memory page.

[0124] It can be understood that the memory page size of the embodiment of the present invention is m*N+k bytes, where m represents the total number of memory blocks contained in a memory page, N represents the size of a memory block, k represents the number of bytes used to store page pointers, and N is a multiple of k.

[0125] In this embodiment, when a request to release all memory resources is received through the memory allocator, the content stored in the header of the page list Pagelist is obtained to obtain the content to be processed. When the content to be processed is an address, it means that there are still unreleased memory pages, then the memory page to be released is obtained according to the address, that is, the target memory page is obtained, and the page pointer stored in the first k bytes of the target memory page is obtained to obtain the target page pointer.

[0126] Then the content stored in the header of the page list Pagelist is updated to the target page pointer, and the target memory page is released through the memory allocator. If the target memory page is not the first memory page, the target page pointer is the address of the previous memory page of the target memory page, so the content stored in the header of the page list Pagelist is updated to the address of the previous memory page, which means that there are still unreleased memory pages, so continue to obtain the current memory page to be released and release it, and cycle in sequence.

[0127] If the target memory page is the first memory page, the target page pointer is a null character, and the content stored in the header of the page list Pagelist is updated to a null character. This means that there are no unreleased memory pages, and all memory pages have been released, that is, all memory resources managed by the memory allocator have been released.

[0128] In order to execute the corresponding steps in the above embodiments and various possible methods, a method for implementing a memory resource allocation device is provided below. Figure 8 , is a functional module diagram of a memory resource allocation device 300 provided in an embodiment of the present invention. It should be noted that the basic principle and technical effects of the memory resource allocation device 300 provided in this embodiment are the same as those of the above embodiment. For the sake of brief description, for matters not mentioned in this embodiment, reference may be made to the corresponding contents in the above embodiment. The memory resource allocation device 300 includes:

[0129] The receiving module 310 is used to obtain the size of the memory resource to be allocated when receiving an application for allocating memory resources through the memory allocator.

[0130] The allocation module 330 is used to obtain the target memory block when the size of the memory resource to be allocated is equal to the size of the memory block, and allocate the target memory block through the memory allocator. When the size of the memory resource to be allocated is smaller than the size of the memory block, the target memory block is obtained, and the target memory block is split to obtain target memory sub-blocks matching the size of the memory resource to be allocated, and the target memory sub-blocks are allocated through the memory allocator.

[0131] Optionally, the allocation module 330 is also used for: if the header of the block linked list stores an address, then obtaining the current first free memory block according to the address to obtain the target memory block; if the header of the block linked list stores a preset null character, then applying for a new memory page through the memory allocator, and dividing the new memory page into multiple memory blocks of the same size, and obtaining the first memory block of the new memory page to obtain the target memory block.

[0132] Optionally, the allocation module 330 is further used to: obtain the block pointer stored in the first k bytes of the target memory block to obtain the target block pointer, and update the content stored in the header of the block linked list to the target block pointer.

[0133] Optionally, the allocation module 330 is also used to: use the target memory block as the memory block to be processed; divide the memory block to be processed into a previous memory sub-block and a subsequent memory sub-block; create a sub-block linked list to connect the previous memory sub-block and the subsequent memory sub-block; determine whether the size of the memory resource to be allocated is less than or equal to half the size of the previous memory sub-block; if so, use the previous memory sub-block as the new memory block to be processed, and repeat the step of dividing the memory block to be processed into the previous memory sub-block and the subsequent memory sub-block; if not, use the previous memory sub-block as the target memory sub-block, and obtain each sub-block linked list.

[0134] Optionally, the memory resource allocation device 300 further includes a release module 350, and the receiving module 310 is further used to: when an application for releasing part of the memory resources is received through the memory allocator, obtain the size and address of the memory resources to be released; the release module 350 is used to: compare the size of the memory resources to be released with the size of the memory block; when the size of the memory resources to be released is equal to the size of the memory block, release the memory resources to be released through the memory allocator to obtain a free memory block, and insert the free memory block into the block linked list according to the address of the memory resources to be released; when the size of the memory resources to be released is smaller than the size of the memory block, from all sub-block linked lists, A target sub-block linked list whose memory size specification matches the size of the memory resource to be released is selected, and a query is made in the target sub-block linked list to determine whether there is an associated memory sub-block adjacent to the address of the memory resource to be released; if so, the memory resource to be released and the associated memory sub-block are combined into a new memory resource to be released, and after obtaining the size and address of the memory resource to be released, the step of comparing the size of the memory resource to be released with the size of the memory block is re-executed; if not, the memory resource to be released is released through a memory allocator to obtain a free memory sub-block, and the free memory sub-block is inserted into the target sub-block linked list according to the address of the memory resource to be released.

[0135] Optionally, the receiving module 310 is also used for: when an application to release all memory resources is received through the memory allocator, obtaining the content stored in the header of the page linked list to obtain the content to be processed; the releasing module 350 is also used for: when the content to be processed is an address, obtaining the target memory page according to the address, and obtaining the page pointer stored in the first k bytes of the target memory page to obtain the target page pointer; updating the content stored in the header of the page linked list to the target page pointer, and after releasing the target memory page through the memory allocator, repeating the step of obtaining the content stored in the header of the page linked list to obtain the content to be processed, until the content to be processed is a null character, and all memory pages are released.

[0136] An embodiment of the present invention further provides an electronic device, including a processor and a memory, wherein the memory stores a computer program, and when the processor executes the computer program, the memory resource allocation method disclosed in the embodiment of the present invention is implemented.

[0137] The embodiment of the present invention further provides a storage medium on which a computer program is stored. When the computer program is executed by a processor, the memory resource allocation method disclosed in the embodiment of the present invention is implemented.

[0138] In several embodiments provided by the present invention, it should be understood that the disclosed devices and methods can also be implemented in other ways. The device embodiments described above are merely schematic. For example, the flowcharts and block diagrams in the accompanying drawings show the possible architecture, functions and operations of the devices, methods and computer program products according to multiple embodiments of the present invention. In this regard, each box in the flowchart or block diagram can represent a module, a program segment or a part of a code, and the module, a program segment or a part of a code contains one or more executable instructions for implementing the specified logical functions. It should also be noted that in some alternative implementations, the functions marked in the box can also occur in a different order from the order marked in the accompanying drawings. For example, two consecutive boxes can actually be executed substantially in parallel, and they can sometimes be executed in the opposite order, depending on the functions involved. It should also be noted that each box in the block diagram and / or flowchart, and the combination of boxes in the block diagram and / or flowchart can be implemented with a dedicated hardware-based system that performs a specified function or action, or can be implemented with a combination of dedicated hardware and computer instructions.

[0139] In addition, the functional modules in the various embodiments of the present invention may be integrated together to form an independent part, or each module may exist independently, or two or more modules may be integrated to form an independent part.

[0140] If the functions are implemented in the form of software function modules and sold or used as independent products, they can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present invention, or the part that contributes to the prior art or the part of the technical solution, can be embodied in the form of a software product. The computer software product is stored in a storage medium, including several instructions for enabling a computer device (which can be a personal computer, a server, or a network device, etc.) to perform all or part of the steps of the methods described in each embodiment of the present invention. The aforementioned storage medium includes: various media that can store program codes, such as a USB flash drive, a mobile hard disk, a read-only memory (ROM), a random access memory (RAM), a magnetic disk or an optical disk.

[0141] The above description is only a preferred embodiment of the present invention and is not intended to limit the present invention. For those skilled in the art, the present invention may have various modifications and variations. Any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present invention shall be included in the protection scope of the present invention.

Claims

1. A memory resource allocation method, characterized in that: Applied to an electronic device, the electronic device is preset with a memory allocator, the memory allocator is used to manage memory pages, the memory pages include multiple memory blocks of the same size, and the memory resource allocation method includes: When an application for allocating memory resources is received through the memory allocator, the size of the memory resources to be allocated is obtained; When the size of the memory resource to be allocated is equal to the size of the memory block, obtaining a target memory block, and allocating the target memory block through the memory allocator; When the size of the memory resource to be allocated is smaller than the size of the memory block, a target memory block is obtained, and the target memory block is split to obtain a target memory sub-block matching the size of the memory resource to be allocated, and the target memory sub-block is allocated through the memory allocator.

2. The memory resource allocation method according to claim 1, characterized in that: All memory blocks in the memory page are connected via a block linked list, and a header of the block linked list is used to store the address of the first free memory block at present; The step of obtaining the target memory block includes: If the header of the block linked list stores an address, the first free memory block is obtained according to the address to obtain the target memory block; If the header of the block linked list stores a preset null character, a new memory page is applied for through the memory allocator, and the new memory page is divided into multiple memory blocks of the same size, and the first memory block of the new memory page is obtained to obtain the target memory block.

3. The memory resource allocation method according to claim 2, characterized in that: The first k bytes of each memory block store a block pointer, and the block pointer is the address of the next memory block or the null character; After the step of obtaining the target memory block, the memory resource allocation method further includes: The block pointer stored in the first k bytes of the target memory block is obtained to obtain a target block pointer, and the content stored in the header of the block linked list is updated to the target block pointer.

4. The memory resource allocation method according to claim 2, characterized in that: The step of splitting the target memory block to obtain target memory sub-blocks matching the size of the memory resources to be allocated comprises: Using the target memory block as a memory block to be processed; Dividing the to-be-processed memory block into a front memory sub-block and a rear memory sub-block; Creating a sub-block linked list to connect the previous memory sub-block and the next memory sub-block; Determine whether the size of the memory resource to be allocated is less than or equal to half the size of the previous memory sub-block; If yes, after taking the previous memory sub-block as a new memory block to be processed, repeat the step of dividing the memory block to be processed into the previous memory sub-block and the next memory sub-block; If not, the previous memory sub-block is used as the target memory sub-block, and each sub-block linked list is obtained.

5. The memory resource allocation method according to claim 4, characterized in that: Each of the sub-block linked lists has a corresponding memory size specification, and the memory resource allocation method further includes: When receiving an application to release part of the memory resources through the memory allocator, obtaining the size and address of the memory resources to be released; Compare the size of the memory resource to be released with the size of the memory block; When the size of the memory resource to be released is equal to the size of the memory block, the memory resource to be released is released by the memory allocator to obtain a free memory block, and the free memory block is inserted into the block linked list according to the address of the memory resource to be released; When the size of the memory resource to be released is smaller than the size of the memory block, selecting a target sub-block linked list with a memory size specification matching the size of the memory resource to be released from all sub-block linked lists, and querying in the target sub-block linked list whether there is an associated memory sub-block adjacent to the address of the memory resource to be released; If so, the memory resource to be released and the associated memory sub-block are combined into a new memory resource to be released and the size and address of the memory resource to be released are obtained, and then the step of comparing the size of the memory resource to be released with the size of the memory block is performed again; If not, the memory resource to be released is released by the memory allocator to obtain a free memory sub-block, and the free memory sub-block is inserted into the target sub-block linked list according to the address of the memory resource to be released.

6. The memory resource allocation method according to claim 1, characterized in that: There are multiple memory pages and all the memory pages are connected through a page linked list, the header of the page linked list is used to store the address of the currently released memory page, the first k bytes of each memory page store a page pointer, and the page pointer is the address of the previous memory page or a preset null character, and the memory resource allocation method also includes: When an application for releasing all memory resources is received through the memory allocator, the content stored in the header of the page linked list is obtained to obtain the content to be processed; When the content to be processed is an address, a currently released memory page is obtained according to the address to obtain a target memory page, and a page pointer stored in the first k bytes of the target memory page is obtained to obtain a target page pointer; The content stored in the header of the page linked list is updated to the target page pointer, and after the target memory page is released by the memory allocator, the step of obtaining the content stored in the header of the page linked list to obtain the content to be processed is repeated until the content to be processed is the null character, and all memory pages are released.

7. A memory resource allocation device, characterized in that: Applied to an electronic device, the electronic device is preset with a memory allocator, the memory allocator is used to manage memory pages, the memory pages include multiple memory blocks of the same size, and the memory resource allocation device includes: A receiving module, configured to obtain the size of the memory resources to be allocated when an application for allocation of memory resources is received through the memory allocator; An allocation module, configured to obtain a target memory block when the size of the memory resource to be allocated is equal to the size of the memory block, and allocate the target memory block through the memory allocator; When the size of the memory resource to be allocated is smaller than the size of the memory block, a target memory block is obtained, and the target memory block is split to obtain a target memory sub-block matching the size of the memory resource to be allocated, and the target memory sub-block is allocated through the memory allocator.

8. The memory resource allocation device according to claim 7, characterized in that: All memory blocks in the memory page are connected via a block linked list, a header of the block linked list is used to store the address of the first free memory block currently, and the allocation module is further used to: If the header of the block linked list stores an address, the first free memory block is obtained according to the address to obtain the target memory block; If the header of the block linked list stores a preset null character, a new memory page is applied for through the memory allocator, and the new memory page is divided into multiple memory blocks of the same size, and the first memory block of the new memory page is obtained to obtain the target memory block.

9. An electronic device, characterized in that: The method comprises a processor and a memory, wherein the memory stores a computer program, and when the processor executes the computer program, the memory resource allocation method according to any one of claims 1 to 6 is implemented.

10. A storage medium, characterized in that: The storage medium stores a computer program, which, when executed by a processor, implements the memory resource allocation method according to any one of claims 1 to 6.