A dynamic memory allocation method, device, medium and product
By dividing the storage space into grid cells of equal size and mapping them to physical addresses, the memory waste problem caused by the fixed size of the bucket memory manager is solved, and flexible storage space allocation and efficient memory management are achieved.
Patent Information
- Application Number
- CN202511300444.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2025-09-12
- Publication Date
- 2026-01-27
- Estimated Expiration
- 2045-09-12
AI Technical Summary
Traditional bucket-type memory managers, due to their fixed bucket size, struggle to adapt to dynamic changes in metadata size, leading to severe internal fragmentation and memory waste.
The storage space is pre-divided into multiple grid cells of equal size, and the storage space is allocated on demand through physical address mapping. Continuous target grid cells are selected to complete the space allocation.
It effectively reduces internal fragmentation of storage space, avoids memory waste, and improves memory utilization and allocation efficiency.
Smart Images

Figure CN120821439B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of storage technology, and in particular to a dynamic memory allocation method, device, medium, and product. Background Technology
[0002] Storage systems place a high value on data integrity. Non-Volatile Memory Express (NVMe) solid-state drives (SSDs) commonly employ Protection Information (PI) as a metadata embedding mechanism, relying on the T10-DIF / DIX standard to achieve end-to-end data protection. This mechanism appends a certain size of integrity metadata to each data block, encompassing verification functions. Traditional methods for managing this metadata often employ a bucket-based memory management strategy.
[0003] Figure 1 This is a schematic diagram of the bucket-type memory management provided by the present invention. Figure 1 As shown, dividing memory into multiple buckets, each responsible for managing memory blocks of a specific size, can effectively reduce memory fragmentation and improve the efficiency of memory allocation and deallocation. However, due to the dynamic changes in metadata size, and the fact that most metadata is less than 256 bytes, traditional bucket managers suffer from severe internal fragmentation and memory waste due to fixed bucket sizes.
[0004] Given the above, how to solve the problem of traditional bucket managers, which have fixed bucket sizes and cannot adapt to dynamic changes in metadata sizes, leading to serious internal fragmentation and memory waste, is an urgent issue for researchers in this field. Summary of the Invention
[0005] This invention provides a dynamic memory allocation method, device, medium, and product to at least solve the problem of traditional bucket managers, which, due to their fixed bucket size, are unable to adapt to dynamic changes in metadata size, leading to severe internal fragmentation and memory waste.
[0006] This invention provides a dynamic memory allocation method, comprising:
[0007] When a storage space allocation request is received, the size of the space to be allocated is determined based on the storage space allocation request;
[0008] Based on the size of the space to be allocated and the size of the grid cells in the storage space, a continuous target grid cell is selected in the storage space; wherein, the continuous storage area of the storage space is pre-divided into multiple grid cells, and each grid cell is of equal size;
[0009] Perform physical address mapping on the target grid cell to complete the allocation of storage space.
[0010] The present invention also provides an electronic device, comprising: a memory for storing a computer program; and a processor for implementing the steps of any of the above-described dynamic memory allocation methods when executing the computer program.
[0011] The present invention also provides a computer-readable storage medium storing a computer program, wherein the computer program, when executed by a processor, implements the steps of any of the above-described dynamic memory allocation methods.
[0012] The present invention also provides a computer program product, including a computer program that, when executed by a processor, implements the steps of any of the above-described dynamic memory allocation methods.
[0013] The beneficial effects of this invention are that the continuous storage area of the storage space is pre-divided into multiple grid cells of equal size. The size of these grid cells can be configured, taking into account both flexibility and scalability. Different numbers of grid cells can be used to allocate storage space of different sizes. When a storage space allocation request is received, the size of the space to be allocated is determined according to the storage space allocation request, and a corresponding number of target grid cells are selected according to the size of the space to be allocated. Physical address mapping is then performed, realizing on-demand allocation of storage space, effectively reducing fragmentation within the storage space, and thus avoiding memory waste.
[0014] In addition, the present invention also provides an electronic device, a medium, and a product, with the same effects as above. Attached Figure Description
[0015] To more clearly illustrate the embodiments of the present invention, the accompanying drawings used in the embodiments will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present invention. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0016] Figure 1 A schematic diagram of the bucket-type memory management provided for this invention;
[0017] Figure 2 A flowchart of a dynamic memory allocation method provided in an embodiment of the present invention;
[0018] Figure 3 A schematic diagram illustrating the principle of dynamic memory allocation provided in an embodiment of the present invention;
[0019] Figure 4 A schematic diagram of the physical address mapping format provided in an embodiment of the present invention;
[0020] Figure 5 This is a schematic diagram of a regional fence layout provided in an embodiment of the present invention;
[0021] Figure 6 This is a schematic diagram of a dynamic memory allocation device provided in an embodiment of the present invention. Detailed Implementation
[0022] The technical solutions of the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of the present invention, and not all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those of ordinary skill in the art without creative effort are within the protection scope of the present invention.
[0023] It should be noted that, in the description of this invention, the terms "comprising," "including," or any other variations thereof are intended to cover non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements includes not only those elements but also other elements not expressly listed, or elements inherent to such a process, method, article, or apparatus. The terms "first," "second," etc., used in this invention are used to distinguish similar objects and are not used to describe a specific order or sequence.
[0024] To enable those skilled in the art to better understand the present invention, the present invention will be further described in detail below with reference to the accompanying drawings and specific embodiments.
[0025] Bucket-based memory management is a technique for optimizing memory allocation and management. It divides memory into multiple buckets, each responsible for managing memory blocks of a specific size range. However, the bucket size is determined during initialization, and improper bucket size settings can lead to inflexible memory allocation. Furthermore, in storage array controller applications, the granularity of metadata storage varies widely, from 8B / 16B to tens of KB, while most application requirements are below 256B. This results in significant waste due to low storage space utilization in buckets larger than KB. Therefore, to address these issues, this invention provides a dynamic memory allocation method. It is understood that the method provided by this invention is applicable to scenarios where devices, hosts, or systems allocate storage space.
[0026] Figure 2 This is a flowchart illustrating a dynamic memory allocation method provided in an embodiment of the present invention. Figure 2 As shown, the method includes:
[0027] S10: When a storage space allocation request is received, determine the size of the space to be allocated based on the storage space allocation request.
[0028] Specifically, when a storage space allocation request is received, the size of the space to be allocated needs to be determined based on the request. This embodiment does not limit the content of the storage space allocation request; it must at least include the size of the space to be allocated, and may also include data type or usage descriptions, performance requirements, redundancy requirements, allocation strategies, and other specific requirements to ensure that the system can allocate storage resources reasonably according to specific needs.
[0029] S11: Select consecutive target grid cells in the storage space based on the size of the space to be allocated and the size of the grid cells in the storage space.
[0030] It is worth noting that in this embodiment, the contiguous storage area of the storage space is pre-divided into multiple grid cells, each of equal size and with corresponding contiguous physical addresses. This method ensures the continuity and efficient management of the storage space. When a user requests storage space, storage space is allocated to the user based on the size of the grid cell. This embodiment does not limit the specific process of dividing the contiguous storage area of the storage space or the organization method of each grid cell; it can be determined according to the specific implementation.
[0031] Therefore, after determining the size of the space to be allocated, consecutive target grid cells are selected in the storage space based on the size of the space to be allocated and the size of the grid cells in the storage space. It is understood that the number of target grid cells can be one or more, depending on the size of the space to be allocated and the size of the grid cells; the total size of all target grid cells should not be smaller than the size of the space to be allocated.
[0032] S12: Perform physical address mapping on the target grid cell to complete the allocation of storage space.
[0033] Finally, physical address mapping is performed on the target grid cell, which involves translating the starting physical address to complete the allocation of storage space. This embodiment does not limit the specific process of performing physical address mapping on the target grid cell; it depends on the specific implementation.
[0034] In this embodiment, the continuous storage area of the storage space is pre-divided into multiple grid cells of equal size. The size of these grid cells can be configured to balance flexibility and scalability. Different numbers of grid cells can be used to allocate storage space of different sizes. When a storage space allocation request is received, the size of the space to be allocated is determined according to the storage space allocation request, and a corresponding number of target grid cells are selected according to the size of the space to be allocated. Physical address mapping is then performed, which realizes on-demand allocation of storage space, effectively reduces internal fragmentation, and thus avoids memory waste.
[0035] Based on the above embodiments, in some embodiments, the process of dividing the contiguous storage region of the storage space includes:
[0036] S101: Determine the size of the grid cell, the number of columns in the grid cell, and the number of rows in the grid cell.
[0037] S102: Divide the storage space into multiple grid cells based on the size of the grid cells.
[0038] S103: Organize all grid cells into a two-dimensional matrix based on the number of columns and rows.
[0039] In this two-dimensional matrix, each grid cell is ordered according to its corresponding physical address, and the physical addresses of grid cells in each row are continuous, while the physical addresses of grid cells between rows are connected end to end.
[0040] To achieve uniform division of contiguous storage areas, this embodiment specifically organizes the contiguous storage areas into a two-dimensional matrix containing multiple grid cells. First, the size of each grid cell is determined. r Byte (i.e., storage space granularity), number of columns in the grid cell 2 m and row number 2 c It should be noted that r, m, and c are all integer values. (Column number 2) m It is also the number of grid cells per row, with 2 rows. c This also refers to the number of grid cells arranged vertically. In this embodiment, the values of r, m, and c are not limited; for example, they can be 3, 4, 5, etc., depending on the specific implementation. The granularity of the storage space mapped by the grid cells is configured as follows: 2. r Bytes can achieve different total managed storage space sizes: 2 m ×2 c ×2 r This design strikes a good balance between flexibility and efficiency, making it suitable for a variety of storage management scenarios.
[0041] Furthermore, the storage space is divided into multiple grid cells based on the size of the grid cells. For example, when the storage space size is 16 bytes and the grid cell size is 2 bytes, the storage space can be divided into 8 grid cells. Finally, based on the number of columns 2... m and row number 2 c All grid cells are organized into a two-dimensional matrix.
[0042] Table 1 Two-dimensional matrix table
[0043]
[0044] As shown in Table 1, storage space management divides contiguous storage areas into fixed-size grid cells, organized in a two-dimensional matrix. It's important to note that the grid cells in the two-dimensional matrix are ordered according to their corresponding physical addresses, with the physical addresses of grid cells within each row being contiguous and the physical addresses of grid cells between rows being consecutive. In other words, in the two-dimensional matrix, the physical addresses of grid cells increase in row priority, with the physical addresses of grid cells in each row increasing sequentially from left to right, and the physical addresses of grid cells in adjacent rows (i.e., the last grid cell in the previous row and the first grid cell in the next row) also increasing sequentially. This organization method ensures the continuity of storage space and efficient management.
[0045] Based on the above embodiments, in some embodiments, after dividing the storage space into contiguous storage regions, the method further includes:
[0046] S111: Configure the bitmap data corresponding to each grid cell in the two-dimensional matrix according to the preset period; wherein, the bitmap data represents the allocation status of the corresponding grid cell, including allocated and unallocated.
[0047] S112: Configure the summary information of each row in the two-dimensional matrix according to the preset period.
[0048] The summary information includes at least the row summation and the maximum consecutive unallocated count; the row summation is the number of grid cells to be allocated within a row, and the maximum consecutive unallocated count is the maximum number of consecutive unallocated grid cells in the corresponding row and the preceding row.
[0049] To better manage the grid cells in the two-dimensional matrix and determine the allocation of each grid cell and each row of grid cells, in this specific implementation, after dividing the storage space into contiguous storage areas, bitmap data corresponding to each grid cell in the two-dimensional matrix is configured according to a preset period. It should be noted that the bitmap data represents the allocation status of the corresponding grid cell, including allocated and unallocated. For example, the allocation status of each grid cell can be represented by a single binary digit, where "0" indicates allocated and "1" indicates unallocated. Furthermore, this embodiment does not limit the size of the preset period; it depends on the specific implementation.
[0050] Furthermore, the summary information for each row in the two-dimensional matrix is configured according to a preset period. It should be noted that the summary information includes at least the row sum (RS) and the maximum consecutive unallocated count. The row sum RS represents the number of unallocated grid cells within a row, and the maximum consecutive unallocated count is the maximum number of consecutive unallocated grid cells in the corresponding row and the preceding row; that is, the maximum consecutive unallocated count supports cross-row accumulation. Since bitmap data of "1" indicates unallocated, the maximum consecutive unallocated count can also be called the maximum consecutive one count (MCOC). As shown in Table 1, the bitmap data of the last two grid cells in row 11 is "1" and consecutive, and the bitmap data of the first three grid cells in row 12 is "1" and consecutive. Since the physical addresses of the grid cells between rows are contiguous, the maximum consecutive unallocated count for row 12 accumulates to 5 across rows.
[0051] In this embodiment, by configuring the bitmap data corresponding to each grid cell in the two-dimensional matrix and the summary information corresponding to each row, the allocation of each grid cell in the two-dimensional matrix is clarified, thereby realizing fine-grained management of storage space and significantly optimizing the performance of the storage system.
[0052] Figure 3 This is a schematic diagram illustrating the principle of dynamic memory allocation provided in embodiments of the present invention. Based on the above embodiments, in some embodiments, such as... Figure 3 As shown, based on the size of the space to be allocated and the size of the grid cells in the storage space, continuous target grid cells are selected in the storage space, including:
[0053] S121: Determine the quotient between the size of the space to be allocated and the size of the grid cells, and round the quotient up to obtain the number of target grid cells.
[0054] S122: Based on the summary information of each row, determine the target row whose number of grid cells to be assigned is not less than the number of target grid cells.
[0055] S123: Initiate a row lock request for the target row and read the bitmap data of the target row.
[0056] S124: Determine the target grid cells to be assigned within the target row based on the corresponding bitmap data.
[0057] like Figure 3 As shown, the allocation module is mainly responsible for calculating the number of consecutive target grid cells required after receiving a storage space allocation request of size X. Specifically, it determines the quotient between the size of the space to be allocated and the size of the grid cells, and rounds the quotient up to obtain the number of target grid cells. The formula is as follows:
[0058] ;
[0059] Where n is the number of target mesh cells, X is the size of the space to be allocated, 2 r This represents the size of the grid cell.
[0060] Further, based on the summary information of each row, namely the row summation and the maximum consecutive unallocated count, the target row with a corresponding number of unallocated grid cells not less than the target number of grid cells is determined. This embodiment does not restrict the specific process for determining the target row. For example, rows with a corresponding maximum consecutive unallocated count not less than the size of the unallocated space can be directly selected as target rows, or rows that meet the requirements can be filtered using row summation, depending on the specific implementation.
[0061] Subsequently, a row lock request is initiated for the target row, and the bitmap data of the target row is read. It is important to note that in the specific implementation, the grid matrix module stores and maintains the bitmap data of the two-dimensional matrix, and manages the read and write operations of the grid bitmap through an internal row-based grid lock mechanism. All requests for grid cells require arbitration through a unique grid lock to ensure that read and write operations on related rows are performed in an orderly manner. This embodiment does not limit the specific process of initiating a row lock request for the target row and reading the bitmap data of the target row; it depends on the specific implementation.
[0062] Finally, the target grid cells to be assigned are determined within the target row based on the corresponding bitmap data. It can be understood that the number of target grid cells can be one or more; for example, when multiple target grid cells exist, the corresponding row numbers can be represented as L1…L… w This enables the accurate selection of target mesh cells, facilitating the allocation of those cells.
[0063] In addition, after determining the target grid cell to be allocated in the target row based on the corresponding bitmap data, the bitmap data corresponding to the target grid cell can be modified to the allocated "0" and written into the two-dimensional matrix. At the same time, the summary information corresponding to the target row is updated, thereby ensuring the timely update of the bitmap data and summary information in the two-dimensional matrix.
[0064] To minimize memory fragmentation, based on the above embodiments, in some embodiments, a target row is determined according to the summary information of each row, where the number of corresponding grid cells to be allocated is not less than the target number of grid cells. This includes:
[0065] S131: Determine candidate rows in each row whose maximum consecutive unassigned count is not less than the number of target grid cells.
[0066] S132: Determine whether there are multiple rows to be selected and whether the number of target grid cells is not greater than the number of columns; if yes, proceed to step S133; if no, proceed to step S134.
[0067] S133: The row with the smallest sum of corresponding rows among all candidate rows is selected as the target row.
[0068] S134: Determine the target row as the row with the smallest maximum consecutive unallocated count among all candidate rows.
[0069] Specifically, first, in each row, we determine the candidate rows whose maximum consecutive unassigned count is not less than the number of target grid cells. Then, we determine whether there are multiple candidate rows and whether the number of target grid cells is not greater than the number of columns. It can be understood that determining whether the number of target grid cells is not greater than the number of columns is equivalent to determining whether the number of target grid cells is not greater than the number of grid cells in each row of the two-dimensional matrix.
[0070] If there are multiple candidate rows and the number of target grid cells is no greater than the number of columns, then multiple candidate rows that meet the requirements exist. In this case, to minimize memory fragmentation, the candidate row with the smallest sum of corresponding rows (RS) is selected as the target row. If there are no multiple candidate rows and / or the number of target grid cells is greater than the number of columns, then only one candidate row that meets the requirements exists, and / or the selection of target grid cells needs to span multiple rows. In this case, to minimize memory fragmentation, the candidate row with the smallest maximum consecutive unallocated count is selected as the target row.
[0071] In this embodiment, by determining candidate rows in each row whose corresponding maximum consecutive unallocated count is not less than the number of target grid cells, and filtering the candidate rows according to the number of candidate rows, the number of target grid cells, and the number of columns, the target rows can minimize memory space fragmentation.
[0072] Based on the above embodiments, in some embodiments, a row locking request is initiated to the target row, and the bitmap data of the target row is read, including:
[0073] S141: Query the read / write lock flags of the target row.
[0074] S142: Determine whether the read / write lock flag is set to locked; if yes, wait for a preset time and return to step S142; if no, proceed to step S143.
[0075] S143: Set the read / write lock flag to locked.
[0076] S144: When the read / write lock flag is confirmed to be locked and the setting operation is successful, read the bitmap data of the target row.
[0077] In practice, the grid matrix module responds to row locking requests for the target row. It should be noted that in a two-dimensional matrix, the smallest unit for read / write locking is the row, and each row has a one-bit read / write lock flag, for example, 0b indicates unlocked and 1b indicates locked.
[0078] When responding to a row lock request for a target row, the grid matrix module first queries the read / write lock flag of the target row and determines whether the read / write lock flag is set to "locked 1b". If the read / write lock flag is confirmed to be "locked 1b", it waits for a preset time and then returns to the step of determining whether the read / write lock flag is set to "locked" until it is "unlocked 0b". If the read / write lock flag is confirmed to be "unlocked 0b", it sets the read / write lock flag to "locked 1b" to facilitate read / write operations on the target row. When the read / write lock flag is confirmed to be "locked 1b" and the setting operation is successful, the bitmap data of the target row is read. It should be noted that all write requests from the recycling module, read / write requests from the allocation module, and read requests from the digest module must be arbitrated through a unique grid lock to ensure that read / write operations on the relevant rows are performed in an orderly manner.
[0079] In this way, the read and write operations of bitmap data in grid cells are managed through the grid lock mechanism, ensuring efficient collaboration between modules while avoiding data conflicts and inconsistencies.
[0080] Based on the above embodiments, in some embodiments, physical address mapping is performed on the target mesh cell, including:
[0081] S151: Map the first target grid cell in all target grid cells to the starting physical address to complete the allocation of storage space.
[0082] To complete the allocation of storage space, after the target grid cells are selected, the first target grid cell among all target grid cells is mapped to the starting physical address (SA). It should be noted that the calculation of the starting physical address mapping of grid cells in this embodiment follows the following format.
[0083] Figure 4 This is a schematic diagram of the physical address mapping format provided in an embodiment of the present invention. For example... Figure 4As shown, the physical address mapping format is as follows: data bits, column number, row number, and physical memory base address are arranged sequentially from least significant bit to most significant bit. Taking a 64-bit absolute address as an example, the fields are arranged from least significant bit to most significant bit. First, the least significant r bits (binary) are all zeros, followed by m bits (binary) of the grid cell's column number and c bits (binary) of the grid cell's row number, and finally, the physical memory base address configured by the software. This achieves physical address mapping for the target grid cell. The row-first physical address arrangement ensures the contiguousness of storage space and supports continuous allocation across rows.
[0084] To better manage the summary information of each row of a two-dimensional matrix, based on the above embodiments, in some embodiments, such as... Figure 3 As shown, the summary information for each row of the two-dimensional matrix is configured according to a preset period, including:
[0085] S152: Scan the two-dimensional matrix row by row according to the preset period, and calculate and update the summary information of each row through the multiplexed computing circuit.
[0086] Reusing computing circuits is a circuit design that performs multiple computing tasks by sharing and reusing hardware resources. This design approach aims to improve the utilization of hardware resources, reduce power consumption, and reduce chip area. In this embodiment, the digest module scans the two-dimensional matrix row by row according to a preset period, and the reused computing circuit calculates and updates the digest information of each row, namely the RS and MCOC of each row. This effectively reduces the frequency of access to the grid cell bitmap data by the allocation module during the allocation search process, thereby reducing the probability of possible locking conflicts encountered when reclaiming grid cells. At the same time, to accelerate allocation decisions, the digest module also undertakes the numerical sorting calculation of RS and MCOC, which can quickly respond to the query requests of the allocation module, provide feedback on the optimal result that meets the requirements, and improve allocation efficiency.
[0087] To achieve the recycling of grid cells, based on the above embodiments, in some embodiments, such as... Figure 3 As shown, the method also includes:
[0088] S161: When a storage space reclamation request is received, determine the starting physical address and the length of the reclaimed space based on the storage space reclamation request.
[0089] S162: Determine the row and column numbers of the grid cells to be recycled in the two-dimensional matrix based on the starting physical address, the length of the recycled space, and the size of the grid cells.
[0090] S163: Perform the recycling operation on the grid cells to be recycled based on their row and column numbers.
[0091] The recycling of grid cells is mainly implemented by the recycling module, which translates a continuous storage space into the row and column numbers of several continuous grid cells and sends a "1" request to the two-dimensional matrix to mark the above-mentioned several continuous grid cells as being in a state of pending allocation.
[0092] Specifically, upon receiving a storage space reclamation request, the reclamation module first determines the starting physical address and the length of the reclamation space based on the request. Then, based on the starting physical address, the length of the reclamation space, and the size of the grid cells, it determines the row and column numbers of the grid cells to be reclaimed in the two-dimensional matrix. Finally, based on the row and column numbers of the grid cells to be reclaimed, the reclamation operation is performed on those grid cells. An example is provided below:
[0093] When a reclaimed cell performs grid cell reclamation, the starting physical address SA directly points to a starting physical memory cell. For example... Figure 4 As shown, bits r to m+r-1 of the starting physical address SA are extracted as the column number of the first grid cell to be reclaimed, and bits m+r to c+m+r-1 are extracted as the row number of the first grid cell to be reclaimed. Meanwhile, the reclaimed space length p is a granularity of 2. r If the value is an integer multiple of the given value, then the starting physical address is SA+2. r After obtaining a new starting physical address SA', bits r to m+r-1 of the starting physical address SA' are the column numbers of the second grid cell to be recycled. Bits m+r to c+m+r-1 are the row numbers of the second grid cell to be recycled. This process is repeated p-1 times until all the grid cells to be recycled are obtained. The grid cell recycling is then performed based on the row and column numbers of all the grid cells to be recycled.
[0094] In this embodiment, the row and column numbers of the grid cells to be recycled in the two-dimensional matrix are determined based on the starting physical address, the length of the recycled space, and the size of the grid cells. The recycling operation of the grid cells to be recycled is then performed based on the row and column numbers, thereby improving the utilization rate of the storage space.
[0095] Since the bitmap data and the summary information of the row containing the grid cell need to be updated after recycling, based on the above embodiments, in some embodiments, the recycling operation of the grid cell to be recycled is performed according to the row number and column number of the grid cell to be recycled, including:
[0096] S171: Determine the location of the grid cell to be recycled based on its row and column numbers.
[0097] S172: Based on the location of the grid cell to be reclaimed, update the bitmap data of the grid cell to be reclaimed to be assigned, and update the summary information of the row corresponding to the grid cell to be reclaimed.
[0098] Unlike the interaction between the allocation module and the grid matrix module, the interaction between the reclamation module and the grid matrix module is very simple. No row locking is required; the row and column numbers of the grid cells to be reclaimed are simply passed to the grid matrix module. The grid matrix module is responsible for updating the bitmap data and summary information. Specifically, it determines the location of the grid cell to be reclaimed based on its row and column numbers, updates the bitmap data of the grid cell to be reclaimed to "awaiting allocation," and updates the summary information of the corresponding row of the grid cell to be reclaimed. This simplifies the reclamation process while ensuring that the grid matrix module can efficiently manage the state updates of the storage space.
[0099] To flexibly adapt to different application scenarios and further effectively suppress the fragmentation problem caused by memory allocation, based on the above embodiments, in some embodiments, continuous target grid cells are selected in the storage space according to the size of the space to be allocated and the size of the grid cells in the storage space, including:
[0100] S181: Determine the target area fence corresponding to the storage space allocation request among all area fences.
[0101] Among them, the area fence is a contiguous storage area that is pre-divided based on a two-dimensional matrix and contains multiple rows of grid cells; there are multiple area fences, and each area fence corresponds to different task requirements.
[0102] S182: Select consecutive target grid cells in the target area fence based on the size of the space to be allocated and the size of the grid cells in the storage space.
[0103] This embodiment introduces the concept of an enclosure. An enclosure is a contiguous storage area divided into grid units, and its configuration can be dynamically adjusted via software. The hardware selects available free space within the corresponding enclosure based on the size of the space required by the current task. Figure 5 This is a schematic diagram illustrating a regional fence layout provided in an embodiment of the present invention. Figure 5As shown, the fencing strategy can include the following three types: Mutually exclusive arrangement, where each fencing zone is independent. This arrangement is similar to a bucket management strategy, ensuring that tasks with different resource requirements do not conflict with each other. Compared to traditional bucket memory allocation, this strategy is more flexible in length configuration and avoids internal fragmentation. Overlapping arrangement, where fencing zones overlap, is mainly suitable for scenarios with limited storage space. Adjacent fencing zones have some overlapping areas. Although this method sacrifices defraction to some extent, it significantly improves space utilization, especially suitable for resource-constrained environments. Hybrid arrangement, where some fencing zones are independent and others overlap. This method combines the advantages of both mutually exclusive and overlapping arrangements, allowing for flexible configuration based on specific application scenarios. It achieves a balance between avoiding conflicts and improving space utilization, taking advantage of both.
[0104] Therefore, when selecting contiguous target grid cells, the target region fence corresponding to the storage space allocation request can be determined from the pre-built total region fences. Then, based on the size of the space to be allocated and the size of the grid cells in the storage space, contiguous target grid cells are selected within the target region fence. By setting region fences, it is helpful to localize the allocation of small spaces while preserving as much contiguous free space as possible, thereby optimizing memory utilization.
[0105] Furthermore, based on dividing the two-dimensional matrix into different regional fences according to different task requirements, and considering data security when multiple users share storage space, the two-dimensional matrix can be further divided into different regional fences, each corresponding to a different user. That is, each regional fence only responds to the storage space allocation requests of its corresponding user, thereby achieving isolated storage of data from different users and ensuring data storage security. Simultaneously, the regional fences can also be arranged in a hybrid configuration to enable partial storage space sharing among different users, improving the efficiency of storage space utilization.
[0106] Through the above description of the embodiments, those skilled in the art can clearly understand that the methods according to the above embodiments can be implemented by means of software plus necessary general-purpose hardware platforms. Of course, they can also be implemented by hardware, but in many cases the former is a better implementation method.
[0107] Figure 6 This is a schematic diagram of a dynamic memory allocation device provided in an embodiment of the present invention. Figure 6 As shown, the device includes:
[0108] The determination module 10 is used to determine the size of the space to be allocated based on the storage space allocation request when a storage space allocation request is received.
[0109] The selection module 11 is used to select continuous target grid cells in the storage space according to the size of the space to be allocated and the size of the grid cells in the storage space; wherein, the continuous storage area of the storage space is pre-divided into multiple grid cells, and each grid cell is of equal size.
[0110] Mapping module 12 is used to perform physical address mapping on the target grid cell to complete the allocation of storage space.
[0111] In some embodiments, the process of dividing a contiguous storage area of the storage space includes: determining the size of a grid cell, the number of columns and rows of the grid cell; dividing the storage space into multiple grid cells based on the size of the grid cell; and organizing all grid cells into a two-dimensional matrix based on the number of columns and rows; wherein, in the two-dimensional matrix, each grid cell is ordered according to its corresponding physical address, and the physical addresses of grid cells in each row are continuous, and the physical addresses of grid cells between rows are connected end to end.
[0112] In some embodiments, after dividing the storage space into contiguous storage regions, the method further includes: configuring bitmap data corresponding to each grid cell in the two-dimensional matrix according to a preset period; wherein the bitmap data represents the allocation status of the corresponding grid cell, including allocated and unallocated; configuring summary information for each row in the two-dimensional matrix according to a preset period; wherein the summary information includes at least row summation and maximum consecutive unallocated count; the row summation is the number of grid cells to be allocated in the row, and the maximum consecutive unallocated count is the maximum number of consecutive unallocated grid cells in the corresponding row and the preceding row.
[0113] In some embodiments, selecting module 11 includes:
[0114] The quantity determination module is used to determine the quotient between the size of the space to be allocated and the size of the grid cells, and round the quotient up to obtain the number of target grid cells;
[0115] The target row determination module is used to determine the target row whose number of grid cells to be assigned is not less than the number of target grid cells, based on the summary information of each row.
[0116] The request module is used to initiate a row lock request to the target row and read the bitmap data of the target row;
[0117] The first target grid cell determination module is used to determine the target grid cells to be assigned within the target row based on the corresponding bitmap data.
[0118] In some embodiments, the target row determination module includes:
[0119] The candidate row determination module is used to determine candidate rows in each row whose maximum consecutive unassigned count is not less than the number of target grid cells;
[0120] The first judgment module is used to determine whether there are multiple candidate rows and whether the number of target grid cells is no greater than the number of columns; if yes, the first target row determination submodule is triggered; if no, the second target row determination submodule is triggered.
[0121] The first target row determination submodule is used to determine the target row as the row with the smallest sum of corresponding rows among all candidate rows.
[0122] The second target row determination submodule is used to determine the target row as the candidate row with the smallest corresponding maximum consecutive unassigned count among all candidate rows.
[0123] In some embodiments, the request module includes:
[0124] The query module is used to query the read / write lock flags of the target row;
[0125] The second judgment module is used to determine whether the read / write lock flag is set to locked; if so, it waits for a preset time and then returns to the step of determining whether the read / write lock flag is set to locked; if not, it triggers the setting module.
[0126] The setting module is used to set the read / write lock flag to locked.
[0127] The read module is used to read the bitmap data of the target row when the read / write lock flag is confirmed to be locked and the setting operation is successful.
[0128] In some embodiments, it also includes:
[0129] The modification module is used to modify the bitmap data corresponding to the target grid cell to the already allocated data and write it into a two-dimensional matrix;
[0130] The first update module is used to update the summary information corresponding to the target row.
[0131] In some embodiments, the mapping module 12 includes:
[0132] The mapping submodule is used to map the first target grid cell in all target grid cells to the starting physical address in order to complete the allocation of storage space;
[0133] The physical address mapping format consists of data bits, column number, row number, and physical memory base address arranged sequentially from the least significant bit to the most significant bit.
[0134] In some embodiments, configuring the summary information of each row in a two-dimensional matrix according to a preset period includes: scanning the two-dimensional matrix row by row according to the preset period, and calculating and updating the summary information of each row through a multiplexed computing circuit.
[0135] In some embodiments, it also includes:
[0136] The recycling determination module is used to determine the starting physical address and the length of the space to be recycled based on the storage space recycling request when a storage space recycling request is received.
[0137] The row and column numbering module is used to determine the row and column numbers of the grid cells to be recycled in a two-dimensional matrix based on the starting physical address, the length of the recycling space, and the size of the grid cells.
[0138] The recycling execution module is used to perform recycling operations on the grid cells to be recycled based on their row and column numbers.
[0139] In some embodiments, the recycling execution module includes:
[0140] The location determination module is used to determine the location of the grid cell to be recycled based on its row and column numbers.
[0141] The second update module is used to update the bitmap data of the grid cells to be reclaimed to be assigned based on the location of the grid cells to be reclaimed, and to update the summary information of the corresponding row of the grid cells to be reclaimed.
[0142] In some embodiments, selecting module 11 includes:
[0143] The target area fence determination module is used to determine the target area fence corresponding to the storage space allocation request among all area fences; wherein, the area fence is a contiguous storage area pre-divided based on a two-dimensional matrix and containing multiple rows of grid cells; there are multiple area fences, and each area fence corresponds to different task requirements;
[0144] The second target grid cell determination module is used to select continuous target grid cells in the target area fence based on the size of the space to be allocated and the size of the grid cells in the storage space.
[0145] For a description of the features in the embodiments corresponding to the dynamic memory allocation device, please refer to the relevant descriptions in the embodiments corresponding to the dynamic memory allocation method, which will not be repeated here.
[0146] Embodiments of the present invention also provide an electronic device, including a memory and a processor, wherein the memory stores a computer program and the processor is configured to run the computer program to perform the steps in any of the above embodiments of the dynamic memory allocation method.
[0147] Embodiments of the present invention also provide a computer-readable storage medium storing a computer program, wherein the computer program is configured to execute the steps in any of the above-described embodiments of the dynamic memory allocation method at runtime.
[0148] In one exemplary embodiment, the aforementioned computer-readable storage medium may include, but is not limited to, various media capable of storing computer programs, such as a USB flash drive, read-only memory (ROM), random access memory (RAM), portable hard disk, magnetic disk, or optical disk.
[0149] Embodiments of the present invention also provide a computer program product, which includes a computer program that, when executed by a processor, implements the steps in any of the above-described embodiments of the dynamic memory allocation method.
[0150] Embodiments of the present invention also provide another computer program product, including a non-volatile computer-readable storage medium storing a computer program, which, when executed by a processor, implements the steps in any of the above-described dynamic memory allocation method embodiments.
[0151] Those skilled in the art will further recognize that the units and algorithm steps of the various examples described in conjunction with the embodiments disclosed herein can be implemented in electronic hardware, computer software, or a combination of both. To clearly illustrate the interchangeability of hardware and software, the components and steps of the various examples have been generally described in terms of functionality in the foregoing description. Whether these functions are implemented in hardware or software depends on the specific application and design constraints of the technical solution. Those skilled in the art can use different methods to implement the described functions for each specific application, but such implementations should not be considered beyond the scope of this invention.
[0152] The present invention has provided a detailed description of a dynamic memory allocation method, device, medium, and product. Specific examples have been used to illustrate the principles and implementation methods of the invention. The descriptions of these embodiments are only intended to aid in understanding the method and core ideas of the invention. It should be noted that those skilled in the art can make various improvements and modifications to the invention without departing from its principles, and these improvements and modifications also fall within the scope of protection of the claims of the present invention.
Claims
1. A dynamic memory allocation method, characterized in that, include: When a storage space allocation request is received, the size of the space to be allocated is determined based on the storage space allocation request; Based on the size of the space to be allocated and the size of the grid cells in the storage space, a continuous target grid cell is selected in the storage space; wherein, the continuous storage area of the storage space is pre-divided into multiple grid cells, and each grid cell is of equal size; Perform physical address mapping on the target grid cell to complete the allocation of storage space; After dividing the storage space into contiguous storage regions, the method further includes: The bitmap data corresponding to each grid cell in the two-dimensional matrix is configured according to a preset period; wherein, the bitmap data represents the allocation status of the corresponding grid cell, including allocated and unallocated; the two-dimensional matrix is generated by organizing all the grid cells. The summary information of each row in the two-dimensional matrix is configured according to a preset period; The summary information includes at least row summation and maximum consecutive unallocated count; the row summation is the number of grid cells to be allocated within a row, and the maximum consecutive unallocated count is the maximum number of consecutive unallocated grid cells in the corresponding row and the preceding row. Based on the size of the space to be allocated and the size of the grid cells in the storage space, select consecutive target grid cells in the storage space, including: Determine the quotient between the size of the space to be allocated and the size of the grid cell, and round the quotient up to obtain the number of target grid cells; Based on the summary information of each row, determine the target row where the number of grid cells to be assigned is not less than the number of target grid cells; Initiate a row lock request to the target row and read the bitmap data of the target row; The target grid cell to be assigned is determined within the target row based on the corresponding bitmap data.
2. The dynamic memory allocation method according to claim 1, characterized in that, The process of dividing the contiguous storage regions of the storage space includes: Determine the size of the grid cell, the number of columns, and the number of rows of the grid cell; The storage space is divided into multiple grid cells based on the size of the grid cells; All the grid cells are organized into a two-dimensional matrix based on the number of columns and the number of rows; In the two-dimensional matrix, each grid cell is ordered according to its corresponding physical address, and the physical addresses of the grid cells in each row are continuous, while the physical addresses of the grid cells in the inter-row are connected end to end.
3. The dynamic memory allocation method according to claim 2, characterized in that, Based on the summary information of each row, determine the target row where the number of grid cells to be assigned is not less than the number of target grid cells, including: In each row, determine the candidate rows whose maximum consecutive unassigned count is not less than the number of target grid cells; Determine whether there are multiple candidate rows and whether the number of target grid cells is not greater than the number of columns; If it is confirmed that there are multiple candidate rows and the number of target grid cells is not greater than the number of columns, then the candidate row with the smallest sum of the corresponding rows among all the candidate rows is determined as the target row; If it is confirmed that the number of candidate rows is not multiple, and / or the number of target grid cells is greater than the number of columns, then the candidate row with the smallest maximum consecutive unassigned count among all candidate rows is determined as the target row.
4. The dynamic memory allocation method according to claim 1, characterized in that, Initiating a row lock request to the target row and reading the bitmap data of the target row, including: Query the read / write lock flag of the target row; Determine whether the read / write lock flag is set to locked; If so, wait for a preset time and then return to the step of determining whether the read / write lock flag is set to locked; If not, then set the read / write lock flag to locked; When it is confirmed that the read / write lock flag is locked and the setting operation is successful, the bitmap data of the target row is read.
5. The dynamic memory allocation method according to claim 1, characterized in that, After determining the target grid cell to be allocated within the target row based on the corresponding bitmap data, the method further includes: Modify the bitmap data corresponding to the target grid cell to indicate that it has been allocated, and write it into the two-dimensional matrix; Update the summary information corresponding to the target row.
6. The dynamic memory allocation method according to claim 1, characterized in that, Performing physical address mapping on the target mesh cell includes: The first target grid cell in all the target grid cells is mapped to the starting physical address to complete the allocation of storage space; The physical address mapping format consists of data bits, column number, row number, and physical memory base address arranged sequentially from the least significant bit to the most significant bit.
7. The dynamic memory allocation method according to claim 1, characterized in that, The summary information of each row in the two-dimensional matrix is configured according to a preset period, including: The two-dimensional matrix is scanned row by row according to a preset period, and the summary information of each row is calculated and updated by a multiplexed computing circuit.
8. The dynamic memory allocation method according to claim 1, characterized in that, Also includes: When a storage space reclamation request is received, the starting physical address and the length of the space to be reclaimed are determined based on the storage space reclamation request; Based on the starting physical address, the length of the reclaimed space, and the size of the grid cell, determine the row and column numbers of the grid cell to be reclaimed in the two-dimensional matrix; The recycling operation is performed on the grid cells to be recycled based on their row and column numbers.
9. The dynamic memory allocation method according to claim 8, characterized in that, Based on the row and column numbers of the grid cells to be recycled, perform a recycling operation on the grid cells to be recycled, including: The location of the grid cell to be recycled is determined based on its row and column numbers; Based on the location of the grid cell to be reclaimed, the bitmap data of the grid cell to be reclaimed is updated to be allocated, and the summary information of the corresponding row of the grid cell to be reclaimed is updated.
10. The dynamic memory allocation method according to any one of claims 1 to 9, characterized in that, Based on the size of the space to be allocated and the size of the grid cells in the storage space, select consecutive target grid cells in the storage space, including: The target region fence corresponding to the storage space allocation request is determined from all region fences; wherein, the region fence is a contiguous storage region pre-divided based on a two-dimensional matrix and containing multiple rows of the grid cells; there are multiple region fences, and each region fence corresponds to a different task requirement; Based on the size of the space to be allocated and the size of the grid cells in the storage space, consecutive target grid cells are selected in the target area fence.
11. An electronic device, characterized in that, include: Memory, used to store computer programs; A processor, configured to implement the steps of the dynamic memory allocation method as described in any one of claims 1 to 10 when executing the computer program.
12. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores a computer program, wherein the computer program, when executed by a processor, implements the steps of the dynamic memory allocation method as described in any one of claims 1 to 10.
13. A computer program product, comprising a computer program, characterized in that, When the computer program is executed by a processor, it implements the steps of the dynamic memory allocation method as described in any one of claims 1 to 10.
Citation Information
Patent Citations
Linux system memory allocation method, system and device and storage medium
CN108920276A
Storage media data structure system and method
US20050223156A1