WO2025181573A1 - 内存操作方法、装置、设备、存储介质及程序产品 - Google Patents
内存操作方法、装置、设备、存储介质及程序产品Info
- Publication number
- WO2025181573A1 WO2025181573A1 PCT/IB2025/050581 IB2025050581W WO2025181573A1 WO 2025181573 A1 WO2025181573 A1 WO 2025181573A1 IB 2025050581 W IB2025050581 W IB 2025050581W WO 2025181573 A1 WO2025181573 A1 WO 2025181573A1
- Authority
- WO
- WIPO (PCT)
- Prior art keywords
- memory
- granularity
- memory block
- block information
- blocks
- Prior art date
- Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
- Pending
Links
Classifications
-
- G—PHYSICS
- G06—COMPUTING OR CALCULATING; COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F3/00—Input arrangements for transferring data to be processed into a form capable of being handled by the computer; Output arrangements for transferring data from processing unit to output unit, e.g. interface arrangements
- G06F3/06—Digital input from, or digital output to, record carriers, e.g. RAID, emulated record carriers or networked record carriers
- G06F3/0601—Interfaces specially adapted for storage systems
- G06F3/0628—Interfaces specially adapted for storage systems making use of a particular technique
- G06F3/0638—Organizing or formatting or addressing of data
- G06F3/064—Management of blocks
-
- G—PHYSICS
- G06—COMPUTING OR CALCULATING; COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F12/00—Accessing, addressing or allocating within memory systems or architectures
- G06F12/02—Addressing or allocation; Relocation
-
- G—PHYSICS
- G06—COMPUTING OR CALCULATING; COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F3/00—Input arrangements for transferring data to be processed into a form capable of being handled by the computer; Output arrangements for transferring data from processing unit to output unit, e.g. interface arrangements
- G06F3/06—Digital input from, or digital output to, record carriers, e.g. RAID, emulated record carriers or networked record carriers
-
- G—PHYSICS
- G06—COMPUTING OR CALCULATING; COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F3/00—Input arrangements for transferring data to be processed into a form capable of being handled by the computer; Output arrangements for transferring data from processing unit to output unit, e.g. interface arrangements
- G06F3/06—Digital input from, or digital output to, record carriers, e.g. RAID, emulated record carriers or networked record carriers
- G06F3/0601—Interfaces specially adapted for storage systems
- G06F3/0602—Interfaces specially adapted for storage systems specifically adapted to achieve a particular effect
- G06F3/0608—Saving storage space on storage systems
-
- G—PHYSICS
- G06—COMPUTING OR CALCULATING; COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F3/00—Input arrangements for transferring data to be processed into a form capable of being handled by the computer; Output arrangements for transferring data from processing unit to output unit, e.g. interface arrangements
- G06F3/06—Digital input from, or digital output to, record carriers, e.g. RAID, emulated record carriers or networked record carriers
- G06F3/0601—Interfaces specially adapted for storage systems
- G06F3/0628—Interfaces specially adapted for storage systems making use of a particular technique
- G06F3/0638—Organizing or formatting or addressing of data
- G06F3/064—Management of blocks
- G06F3/0641—De-duplication techniques
-
- G—PHYSICS
- G06—COMPUTING OR CALCULATING; COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F9/00—Arrangements for program control, e.g. control units
- G06F9/06—Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
- G06F9/46—Multiprogramming arrangements
- G06F9/50—Allocation of resources, e.g. of the central processing unit [CPU]
-
- G—PHYSICS
- G06—COMPUTING OR CALCULATING; COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F9/00—Arrangements for program control, e.g. control units
- G06F9/06—Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
- G06F9/46—Multiprogramming arrangements
- G06F9/54—Interprogram communication
Definitions
- memory may be divided into multiple memory blocks according to a first memory granularity (e.g., 4K (kilo) granularity) for memory operations; in other scenarios, memory may be divided into multiple memory blocks according to a second memory granularity (e.g., 2M (mega) granularity) for memory operations.
- a first memory granularity e.g. 4K (kilo) granularity
- a second memory granularity e.g., 2M (mega) granularity
- an embodiment of the present disclosure provides a memory operation method, comprising: obtaining a memory operation request for a memory, the memory operation request being used to request memory allocation or memory release, the memory including multiple memory blocks corresponding to at least two memory granularities; determining memory block information corresponding to at least one memory granularity, the memory block information including a memory granularity value, a memory block status, and memory block usage information; and processing the memory operation request based on the memory block information corresponding to the at least one memory granularity to allocate or release memory.
- the memory operation request is used to request memory allocation, and the memory operation request
- the memory operation request includes a requested memory granularity and a memory request amount corresponding to the requested memory granularity; processing the memory operation request based on memory block information corresponding to at least one memory granularity to perform memory allocation includes: determining memory block information corresponding to the requested memory granularity; determining a target memory block corresponding to the requested memory granularity based on the memory block information corresponding to the requested memory granularity and the memory request amount, wherein the memory granularity of the target memory block is the requested memory granularity and the memory block status of the target memory block is idle; and allocating the target memory block corresponding to the requested memory granularity.
- the requested memory granularity is smaller than the maximum memory granularity; determining memory block information corresponding to the requested memory granularity includes: determining a higher-level memory granularity corresponding to the requested memory granularity, wherein the memory block corresponding to the higher-level memory granularity is divided into multiple memory blocks corresponding to the requested memory granularity; determining a first linked list address in the memory block information corresponding to the higher-level memory granularity; and determining the memory block information corresponding to the requested memory granularity based on the first linked list address.
- determining the target memory block based on the memory block information corresponding to the applied memory granularity and the memory application amount includes: judging whether the applied memory granularity is the maximum memory granularity; if so, determining the target memory block in at least one memory block corresponding to the applied memory granularity when the total amount of memory corresponding to the applied memory granularity is greater than or equal to the memory application amount; if not, determining the target memory block in at least one memory block corresponding to the applied memory granularity when the total amount of memory corresponding to the applied memory granularity is greater than or equal to the memory application amount; and updating the at least one memory block when the total amount of memory corresponding to the applied memory granularity is less than the memory application amount, and determining the target memory block in the at least one updated memory block.
- updating the at least one memory block includes: determining a first memory block based on the requested memory granularity, wherein the memory granularity of the first memory block is greater than the requested memory granularity, and the memory block state of the first memory block is an idle state; splitting the first memory block to obtain multiple second memory blocks, wherein the memory granularity of the second memory block is the requested memory granularity; and determining that the at least one updated memory block includes the at least one memory block and the multiple second memory blocks.
- the method further includes: determining a first memory granularity corresponding to the first memory block, determining first memory block information corresponding to the first memory granularity, and updating the first memory block information based on the first memory block; determining second memory block information corresponding to the requested memory granularity, and updating the second memory block information based on the multiple second memory blocks.
- determining the target memory block in the at least one memory block corresponding to the requested memory granularity includes: Determine a first number of target memory blocks based on the memory request amount and the memory request granularity; determine whether a first number of contiguous memory blocks exist in the at least one memory block; if so, determine the first number of contiguous memory blocks as the target memory blocks; if not, when the memory operation request indicates that discontinuous memory allocation is permitted, determine the first number of discontinuous memory blocks in the at least one memory block as the target memory blocks, and the sum of the memory of the discontinuous memory blocks is greater than or equal to the memory request amount.
- the method further includes: updating memory block information corresponding to the memory request granularity based on the target memory block.
- the memory operation request is used to request memory release; the memory operation request includes a memory granularity to be released and a memory address corresponding to each memory granularity to be released; and processing the memory operation request based on memory block information corresponding to at least one memory granularity to release memory includes: determining at least one to-be-released memory block corresponding to the memory granularity based on the memory block information corresponding to the memory granularity to be released and the memory address; and clearing the at least one to-be-released memory block to release the at least one memory block.
- the method further includes: updating memory block information corresponding to the memory granularity to be released based on the at least one memory block.
- the method further includes: if the memory granularity to be released is smaller than the maximum memory granularity, and if the memory block status in third memory block information corresponding to the memory granularity is all in an idle state, merging multiple memory blocks based on the third memory block information to obtain a third memory block, where the memory granularity of the third memory block is the maximum memory granularity; updating the memory block information corresponding to the maximum memory granularity based on the third memory block, and deleting the third memory block information.
- an embodiment of the present disclosure provides a memory operation device, comprising: an acquisition module, a determination module, and a processing module, wherein the acquisition module is used to acquire a memory operation request for a memory, the memory operation request is used to request memory allocation or memory release, the memory includes multiple memory blocks, and the multiple memory blocks correspond to at least one memory granularity; the determination module is used to determine memory block information corresponding to at least one memory granularity, the memory block information includes a memory granularity value, a memory block status, and memory block usage information; the processing module is used to process the memory operation request according to the memory block information corresponding to at least one memory granularity to allocate memory or release memory.
- the memory operation request is used to request memory allocation, the memory operation request includes a requested memory granularity and a memory request amount corresponding to the requested memory granularity; the processing module is specifically used to: determine the memory block information corresponding to the requested memory granularity; determine the memory block information corresponding to the requested memory granularity based on the memory block information and the memory request amount corresponding to the requested memory granularity a target memory block, the memory granularity of the target memory block being the requested memory granularity, and the memory block status of the target memory block being an idle state; allocating the target memory block corresponding to the requested memory granularity.
- the requested memory granularity is smaller than the maximum memory granularity; the processing module is specifically configured to: determine a previous-level memory granularity corresponding to the requested memory granularity, wherein the memory block corresponding to the previous-level memory granularity is divided into multiple memory blocks corresponding to the requested memory granularity; determine a first linked list address in the memory block information corresponding to the previous-level memory granularity; and determine the memory block information corresponding to the requested memory granularity based on the first linked list address.
- the processing module is specifically configured to: determine whether the requested memory granularity is the maximum memory granularity; if so, determine the target memory block in at least one memory block corresponding to the requested memory granularity when the total amount of memory corresponding to the requested memory granularity is greater than or equal to the requested memory amount; if not, determine the target memory block in at least one memory block corresponding to the requested memory granularity when the total amount of memory corresponding to the requested memory granularity is greater than or equal to the requested memory amount; and if the total amount of memory in at least one memory block corresponding to the requested memory granularity is less than the requested memory amount, update the at least one memory block and determine the target memory block in the updated at least one memory block.
- the processing module is specifically configured to: determine a first memory block based on the requested memory granularity, the memory granularity of the first memory block being greater than the requested memory granularity and the memory block status of the first memory block being idle; split the first memory block to obtain multiple second memory blocks, the memory granularity of the second memory block being the requested memory granularity; and determine that the updated at least one memory block includes the at least one memory block and the multiple second memory blocks.
- the apparatus further includes: an update module, the update module being configured to: determine a first memory granularity corresponding to the first memory block, determine first memory block information corresponding to the first memory granularity, and update the first memory block information based on the first memory block; determine second memory block information corresponding to the requested memory granularity, and update the second memory block information based on the plurality of second memory blocks.
- the processing module is specifically configured to: determine a first number of target memory blocks based on the memory request amount and the requested memory granularity; determine whether a first number of contiguous memory blocks exist in the at least one memory block; if so, determine the first number of contiguous memory blocks as the target memory blocks; if not, when the memory operation request indicates that discontinuous memory allocation is permitted, determine a first number of discontinuous memory blocks in the at least one memory block as the target memory blocks, where the sum of the memory of the discontinuous memory blocks is greater than or equal to the memory request amount.
- the update module is further configured to: update memory block information corresponding to the requested memory granularity based on the target memory block.
- the memory operation request is used to request memory release; the memory operation request includes the released memory granularity and the memory address corresponding to the released memory granularity; the processing module is specifically configured to: determine at least one to-be-released memory block corresponding to the released memory granularity based on the memory block information corresponding to the released memory granularity and the memory address; and clear the at least one to-be-released memory block to release the at least one memory block.
- the update module is further configured to: update memory block information corresponding to the released memory granularity based on the at least one memory block.
- the processing module is further configured to, if the released memory granularity is smaller than the maximum memory granularity and if the memory block states in the third memory block information corresponding to the released memory granularity are all in the idle state, merge multiple memory blocks according to the third memory block information to obtain a third memory block, wherein the memory granularity of the third memory block is the maximum memory granularity; and the updating module is further configured to update the memory block information corresponding to the maximum memory granularity according to the third memory block and delete the third memory block information.
- an embodiment of the present disclosure provides an electronic device comprising: a memory and a processor; the memory storing computer-executable instructions; the processor executing the computer-executable instructions stored in the memory, causing the processor to perform any of the methods described in the first aspect.
- an embodiment of the present disclosure provides a computer-readable storage medium storing computer-executable instructions, which, when executed by the processor, are used to implement any of the methods described in the first aspect.
- an embodiment of the present disclosure provides a computer program product comprising a computer program, which, when executed by the processor, implements any of the methods described in the first aspect.
- the embodiments of the present disclosure provide a memory operation method, apparatus, device, storage medium, and program product.
- the electronic device can obtain a memory operation request for the memory and determine the memory block information corresponding to at least one memory granularity. Then, the memory operation request can be processed according to the memory block information corresponding to at least one memory granularity to allocate or release memory. Since the memory may include multiple memory blocks corresponding to at least two memory granularities, and each memory granularity has corresponding memory block information. When allocating or releasing memory, the multiple memory blocks corresponding to any memory granularity in the memory can be operated according to the memory block information according to actual needs. Compared with the prior art in which the memory only includes multiple memory blocks corresponding to one memory granularity and the memory is only operated according to this memory granularity, the flexibility of memory operation is improved.
- Figure 1 is a schematic diagram of a scenario provided by an exemplary embodiment of the present disclosure
- Figure 2 is a schematic diagram of a memory operation flow provided by an exemplary embodiment of the present disclosure
- Figure 3 is a schematic diagram of memory block information corresponding to at least one memory granularity provided by an exemplary embodiment of the present disclosure
- Figure 4 is a first schematic diagram of a first linked list and a second linked list provided by an exemplary embodiment of the present disclosure
- Figure 5 is a schematic diagram of a flow chart of another memory operation method provided by an exemplary embodiment of the present disclosure
- Figure 6 is a second schematic diagram of a first linked list and a second linked list provided by an exemplary embodiment of the present disclosure
- Figure 7 is a schematic diagram of a flow chart of yet another memory operation method provided by an
- the user information including but not limited to user device information, user personal information, etc.
- data including but not limited to data used for analysis, storage, and display
- the collection, use, and processing of the relevant data must comply with relevant laws, regulations, and standards, and corresponding operation portals are provided for users to choose to authorize or reject.
- the technical solutions of this disclosure will be described clearly and completely below in conjunction with specific embodiments of this disclosure and the corresponding drawings. Obviously, the described embodiments are only some of the embodiments of this disclosure, and are not exhaustive.
- a memory may include multiple memory blocks.
- the multiple memory blocks may be memory block 1, memory block 2, ..., memory block n.
- the multiple memory blocks may correspond to at least one memory granularity.
- the at least one memory granularity may include at least one of the following: 1G (giga) granularity, 2M granularity, 16K granularity, 4K granularity, etc.
- the memory granularity corresponding to memory block 1 and memory block 2 may be 2M granularity, i.e., the size of memory block 1 and memory block 2 is 2M; the memory granularity corresponding to memory block 3 may be 4K granularity, i.e., the size of memory block 3 is 4K.
- the electronic device may receive a memory operation request and, based on the memory operation request, perform memory allocation or memory release processing on multiple memory blocks corresponding to the at least one memory granularity. For example, if the memory operation request requests allocation of 1G memory and the memory granularity is 2M granularity, 512 memory blocks of 2M granularity may be determined in the memory based on the memory operation request to allocate the 512 memory blocks.
- the memory can be divided into multiple memory blocks according to a first memory granularity (e.g., 4K granularity) to perform memory operations; in other scenarios, the memory can also be divided into multiple memory blocks according to a second memory granularity (e.g., 2M granularity) to perform memory operations.
- a first memory granularity e.g. 4K granularity
- a second memory granularity e.g., 2M granularity
- the memory may include multiple memory blocks corresponding to at least two memory granularities.
- the electronic device may process the memory operation request based on memory block information corresponding to at least one memory granularity to perform operations such as allocating or releasing memory. Because the memory may include multiple memory blocks corresponding to at least two memory granularities, and each memory granularity has corresponding memory block information, when allocating or releasing memory, operations can be performed on the multiple memory blocks corresponding to any one of the memory granularities in the memory based on the memory block information, as needed.
- FIG2 is a schematic diagram of a memory operation flow diagram provided by an exemplary embodiment of the present disclosure. Referring to FIG2 , the method may include:
- a memory may include multiple memory blocks, each of which may correspond to at least one memory granularity.
- the memory may include n memory blocks (n is an integer greater than or equal to 2), where the memory granularity of memory block 1 and memory block 2 may be 2M, and the memory granularity of memory block 3 may be 4K.
- a memory operation request may be used to request memory allocation or memory release.
- the electronic device may obtain a memory operation request for a memory in response to a user operation on the memory.
- memory operation request 1 may request allocation of 1.5 GB of memory with a memory granularity of 2 MB
- memory operation request 2 may request allocation of 2.5 MB of memory with a memory granularity of 4 MB
- memory operation request 3 may request release of a memory block with a memory address of address B with a granularity of 2 MB.
- FIG3 is a schematic diagram of memory block information corresponding to at least one memory granularity provided by an exemplary embodiment of the present disclosure.
- the memory block information 1 corresponding to the 2M granularity may include the information of the n memory blocks.
- memory block 2 is divided into multiple memory blocks of 4K granularity, then the memory block information 2 corresponding to the 4K granularity may include the information of the n memory blocks.
- This information includes information about multiple 4K memory blocks corresponding to memory block 2.
- memory block information 3 may include information about multiple 4K memory blocks corresponding to memory block 5.
- Memory block information 4 may include information about multiple 4K memory blocks corresponding to memory block 8.
- a 2M granularity may correspond to memory block information 1
- a 4K granularity may correspond to three pieces of memory block information: memory block information 2, memory block information 3, and memory block information 4.
- the memory block information may include the memory granularity value, memory block status, and memory block usage information.
- the memory block information may also include information about the memory range and the current allocated address. For each memory block, a corresponding memory block status may be assigned. Memory block statuses may include idle, used, error, reserved, and split. Each memory block status may be represented by a corresponding number.
- the idle state can be represented by "0,” the used state by "1,” the error state by "2,” the reserved state by "3,” and the split state by "4.”
- the memory block states of multiple memory blocks corresponding to any memory granularity can be represented by an array.
- the memory block states of these n 2M memory blocks can be represented as the array [1, 4, 0, 0, 4, 0, 2, 4, 0, 3, 0, 0, 0, 2, ... , where the first bit being 1 indicates that the memory block state of memory block 1 is in use, and the second bit being 4 indicates that the memory block state of memory block 2 is split.
- Memory block usage information can include the number of memory blocks corresponding to each memory block state.
- a memory range can be represented by the start and end addresses of the memory.
- the starting address of memory 1 may be 0x00000000, and the ending address may be 0x400000000.
- the memory range may also be represented by the starting address and memory length of the memory.
- the starting address of memory 1 may be 0x00000000, and the memory length may be 1GB.
- the current allocation address may be used to record the starting address of the memory block in the allocated idle state. The initial value of the current allocation address is 0 and changes as memory is allocated or released.
- the memory block information may be presented in the form of a data table.
- the memory block information corresponding to the memory granularity may also include the address of the next memory block information corresponding to the next level of memory granularity, and an index number.
- the index number may be used to indicate the memory block identifier corresponding to the previous level of memory granularity corresponding to the multiple memory blocks corresponding to the memory block information.
- Table 1 Address 3 may be the address of memory block information 3.
- the memory block information corresponding to the memory granularity may further include a first linked list address and a second linked list address.
- the first linked list address may be used to determine a first linked list.
- the first linked list may include at least one memory block information corresponding to the next level of memory granularity.
- each memory block information item has an idle state, that is, there is an idle memory block among the multiple memory blocks corresponding to each memory block information item.
- the first linked list may be a partial linked list.
- the second linked list address may be used to determine a second linked list.
- the second linked list may include at least one memory block information corresponding to the next level of memory granularity.
- each memory block information item has no idle state, that is, there is no idle memory block among the multiple memory blocks corresponding to each memory block information item.
- the second linked list may be a full linked list.
- the memory granularity is 2M and the minimum memory granularity is 4K
- the memory block information 1 corresponding to the 2M granularity may be as shown in Table 2: Table 2
- Table 2 The first linked list and the second linked list are described below in conjunction with FIG4 .
- FIG4 is a schematic diagram of the first linked list and the second linked list provided in an exemplary embodiment of the present disclosure.
- memory block information 1 corresponding to a 2M granularity
- memory block information 2 includes the address of the next memory block information as address 3
- memory block The address of the next memory block information included in memory block information 3 and memory block information 4 is empty.
- the first linked list address in memory block information 1, i.e., address 1 may be the address of memory block information 2, and the address of the next memory block information in memory block information 2, i.e., address 3, may be the address of memory block information 3.
- the first linked list may include memory block information 2 and memory block information 3.
- the second linked list address in memory block information 1, i.e., address 2 may be the address of memory block information 4.
- the second linked list may include memory block information 4.
- the electronic device may determine memory block information 1 corresponding to the 2MB granularity based on memory operation request 1; and may determine memory block information 2, memory block information 3, and memory block information 4 corresponding to the 4KB granularity based on memory operation request 2.
- S203 Process the memory operation request based on the memory block information corresponding to at least one memory granularity to allocate or release memory. Because the memory block information corresponding to any memory granularity includes memory block statuses of multiple memory blocks, the electronic device can read the memory block information and process the memory operation request based on the memory block statuses of the multiple memory blocks to allocate or release memory.
- the electronic device may determine 768 2 MB memory blocks in the idle state from memory block information 1, and allocate the 768 2 MB memory blocks to process memory operation request 1; and may determine 640 4 KB memory blocks in the idle state from memory block information 2, memory block information 3, and memory block information 4, and the electronic device may allocate the 640 4 KB memory blocks to process memory operation request 2.
- an electronic device can obtain a memory operation request for memory and determine memory block information corresponding to at least one memory granularity. The electronic device can then process the memory operation request based on the memory block information corresponding to the at least one memory granularity to allocate or release memory. Because the memory may include multiple memory blocks corresponding to at least two memory granularities, and each memory granularity has corresponding memory block information, when allocating or releasing memory, operations can be performed on multiple memory blocks corresponding to any memory granularity in the memory based on the memory block information, as needed.
- FIG. 5 is a flow chart of another memory operation method provided in an exemplary embodiment of the present disclosure. Referring to FIG. 5 , the method may include:
- the memory operation request can be used to request memory allocation.
- the memory operation request includes the memory granularity and the memory size.
- Memory granularity corresponds to the amount of memory requested.
- the memory granularity requested can be any of the following: 1G granularity, 2M granularity, 16K granularity, 4K granularity, etc.
- the electronic device can obtain a memory operation request in response to a user's memory operation.
- the electronic device can obtain memory operation request 1 requesting the allocation of 1.5G memory with a memory granularity of 2M, and memory operation request 2 requesting the allocation of 2.5M memory with a memory granularity of 4K.
- the memory granularity requested in memory operation request 1 is 2M granularity
- the memory request amount is 1.5G
- the memory granularity requested in memory operation request 2 is 4K granularity
- the memory request amount is 2.5M.
- step S502 Determine memory block information corresponding to at least one memory granularity. It should be noted that the execution process of step S502 can refer to step S202 and is not further described here.
- determining the memory block information corresponding to the requested memory granularity may include the following two cases: Case 1: The requested memory granularity is the maximum memory granularity. For example, if the memory includes two memory granularities, 2M and 4K, the maximum memory granularity is 2M, and there is memory block information 1 corresponding to the 2M granularity (as shown in Table 2), if the requested memory granularity is 2M, the electronic device may determine that the memory block information corresponding to the requested memory granularity 2M is memory block information 1. Case 2: The requested memory granularity is smaller than the maximum memory granularity.
- the memory block information corresponding to the requested memory granularity may be determined in the following manner: determining the upper-level memory granularity corresponding to the requested memory granularity; determining a first linked list address in the memory block information corresponding to the upper-level memory granularity; and determining the memory block information corresponding to the requested memory granularity based on the first linked list address. For example, if the memory includes two memory granularities, 2M and 4K, the maximum memory granularity is 2M, and the requested memory granularity is 4K, then for the requested memory granularity of 4K, the upper-level memory granularity is 2M.
- some memory blocks are divided into multiple memory blocks corresponding to the requested memory granularity.
- memory block 3 among the multiple memory blocks corresponding to the 2M granularity, memory block 3, memory block 5, and memory block 8 are divided into multiple memory blocks corresponding to the 4K granularity.
- FIG4 if there is memory block information 1 corresponding to a 2M granularity, and memory block information 2, memory block information 3, and memory block information 4 corresponding to a 4K granularity, and if the requested memory granularity is 4K, the electronic device can determine the first linked list address, i.e., address 1, from memory block information 1.
- the electronic device can determine memory block information 2 corresponding to the requested memory granularity of 4K. Furthermore, based on address 3 in memory block information 2, the electronic device can determine memory block information 3. It should be noted that since the memory operation request is a request for memory allocation, and each piece of memory block information in the first linked list corresponds to a plurality of memory blocks that are in an idle state, it is sufficient to determine the plurality of memory block information in the first linked list based on the first linked list address. Since each piece of memory block information in the second linked list corresponds to a plurality of memory blocks that are in an idle state, it is not necessary to determine the plurality of memory block information in the second linked list based on the second linked list address.
- S504. Determine whether the requested memory granularity is the maximum memory granularity. If so, execute S505; if not, execute S506. For example, if the memory includes two memory granularities, 2M and 4K, and if the requested memory granularity is 2M, it can be determined that the requested memory granularity 2M is the maximum memory granularity 2M, and then execute S505; if the requested memory granularity is 4K, It can be determined that the memory granularity of 4K is less than the maximum memory granularity of 2M, and S506o can be executed.
- S505 When the total amount of memory corresponding to the requested memory granularity is greater than or equal to the requested memory amount, determine a target memory block from at least one memory block corresponding to the requested memory granularity. Since the memory includes at least one memory granularity, and the at least one memory granularity includes the requested memory granularity, the total amount of memory corresponding to the requested memory granularity can be determined in the memory. When the total amount of memory corresponding to the requested memory granularity is greater than or equal to the requested memory amount, determine the target memory block from at least one memory block corresponding to the requested memory granularity. The memory granularity of the target memory block can be the requested memory granularity, and the memory block status of the target memory block can be idle.
- memory block 3 and memory block 4 can be determined as the target memory blocks.
- the requested memory granularity is the maximum memory granularity of 2M and the requested memory amount is 1.5G, and the total memory amount corresponding to 2M is 5G, then because the total memory amount of 5G corresponding to the 2M granularity is greater than the requested memory amount of 1.5G, multiple target memory blocks can be determined in at least one memory block corresponding to the 2M granularity.
- determining the target memory block in at least one memory block corresponding to the requested memory granularity may include the following two cases: Case 11: If the memory operation request indicates that memory be allocated according to an aligned mode. In an aligned mode, the current allocation address and memory length must be aligned when allocating memory. In this case, the electronic device may determine the current allocation address from the memory block information corresponding to the requested memory granularity and determine whether the current allocation address is aligned. If so, then the memory block status of each memory block can be traversed starting from the current allocation address. Multiple target memory blocks corresponding to the requested memory granularity can then be determined from at least one memory block corresponding to the requested memory granularity based on the memory block status.
- an aligned allocation address can be determined. Starting from the aligned allocation address, the memory block status of each memory block can be traversed. Multiple target memory blocks corresponding to the requested memory granularity can then be determined from at least one memory block corresponding to the requested memory granularity based on the memory block status.
- the at least one target memory block can correspond to at least one memory length, and at least one memory length is aligned. If not, that is, the current allocation address is not aligned, and if there is no aligned allocation address and/or aligned memory length, then the memory operation request cannot be satisfied, and the allocation result can be determined as an allocation failure.
- the target memory block can be determined in at least one memory block corresponding to the memory application granularity in the following manner: determining a first number of target memory blocks based on the memory application amount and the memory application granularity; determining whether a first number of continuous memory blocks exists in the at least one memory block; if so, determining the first number of continuous memory blocks as the target memory blocks; if not, when the memory operation request indicates that discontinuous memory allocation is allowed, determining the first number of discontinuous memory blocks in the at least one memory block as the target memory blocks, and the sum of the memory of the discontinuous memory blocks is greater than or equal to the memory application amount.
- the first number can be represented by M, and M can be a positive integer.
- the M target memory blocks corresponding to the memory application granularity when determining the M target memory blocks corresponding to the memory application granularity, it can be pre-determined based on the memory application amount that the M target memory blocks include a total of K continuous first target memory blocks corresponding to the first memory length, and/or Or MK second target memory blocks corresponding to the second memory length.
- the electronic device may determine K consecutive first target memory blocks corresponding to the first memory length, and then determine MK second target memory blocks corresponding to the second memory length.
- the MK second target memory blocks may be consecutive or discontinuous with the K consecutive first target memory blocks.
- K is an integer greater than or equal to 0.
- the electronic device may predetermine, based on the memory request amount of 1.5GB, that 768 target memory blocks are required.
- the 768 target memory blocks include 512 consecutive first target memory blocks corresponding to the first memory length of 1GB and 256 second target memory blocks corresponding to the second memory length of 2MB. The electronic device may then determine in memory block information 1 that the current allocated address is address A and determine whether address A is aligned to 1GB.
- the memory block status of the n 2M memory blocks can be traversed starting from address A. If not aligned, the alignment allocation address 1 can be determined. Assuming that the alignment allocation address 1 is address A1, the memory block status of the n 2M memory blocks can be traversed starting from address A1. Based on the memory block status of the n 2M memory blocks, the electronic device can prioritize 512 free, contiguous first target memory blocks and determine these 512 first target memory blocks as the 512 target memory blocks. The memory length corresponding to these 512 target memory blocks is aligned to 1G.
- the electronic device can continue to determine 256 free, contiguous second target memory blocks based on the memory block status of the n 2M memory blocks according to the current allocation address A2 after determining the 521st target memory block.
- the electronic device can determine a total of 768 contiguous 2M target memory blocks. If the memory operation request indicates that non-contiguous memory allocation is permitted, the electronic device may also prioritize determining a total of 512 contiguous first target memory blocks corresponding to a first memory length of 1GB, and then determine 256 2MB target memory blocks.
- the electronic device may determine whether the current allocation address is 2MB aligned. If so, the electronic device may traverse the memory block status of n 2MB memory blocks starting from the current allocation address. If not, the electronic device may determine an aligned allocation address and traverse the memory block status of n 2MB memory blocks starting from the aligned allocation address to determine 256 free memory blocks, which are then determined as the 256 target memory blocks.
- the 256 target memory blocks may be non-contiguous. The memory length of each memory block is 2MB aligned. The electronic device may then determine a total of 768 target memory blocks. Of these 768 target memory blocks, 512 may be contiguous, while 256 may be non-contiguous.
- Case 12 If the memory operation request indicates that the alignment mode does not need to be followed.
- the electronic device can determine the current allocation address in the memory block information corresponding to the applied memory granularity, and starting from the current allocation address, traverse the memory block status of each memory block, and then determine at least one target memory block corresponding to the applied memory granularity according to the memory block status.
- the target memory block can be determined in at least one memory block corresponding to the applied memory granularity in the following manner: determine a first number M of target memory blocks based on the memory application amount and the applied memory granularity; determine whether there are M consecutive memory blocks in at least one memory block; if so, determine the M consecutive memory blocks as target memory blocks; if not, When the memory operation request indicates that discontinuous memory allocation is allowed, M discontinuous memory blocks in the at least one memory block are determined as target memory blocks, and the sum of the memory of the discontinuous memory blocks is greater than or equal to the memory application amount.
- the electronic device can determine address A based on the memory block information 1, and traverse the memory block status of n 2M memory blocks starting from address A to determine 768 free continuous memory blocks, and determine the 768 free continuous memory blocks as 768 target memory blocks; if the memory operation request 1 indicates that discontinuous memory allocation is allowed, the electronic device can determine address A based on the memory block information 1, and traverse the memory block status of n 2M memory blocks starting from address A to determine 768 free, discontinuous memory blocks, and determine the 768 free, discontinuous memory blocks as the 768 target memory blocks.
- the allocation may be determined to have failed. It should be noted that when determining the M discontinuous memory blocks in the at least one target memory block, the process may start from the current allocation address and traverse backward through multiple consecutive memory blocks; then, move from the current allocation address to the next suitable allocation address, and continue traversing backward through multiple consecutive memory blocks from the current allocation address; until M memory blocks are determined.
- determining the target memory block from at least one memory block corresponding to the requested memory granularity may include the following two cases: Case 21: The total amount of memory corresponding to the requested memory granularity is greater than or equal to the requested memory amount.
- the target memory block may be determined from at least one memory block corresponding to the requested memory granularity.
- contiguous memory blocks may be preferentially determined from the at least one memory block corresponding to the requested memory granularity. If contiguous memory blocks are insufficient, discontiguous memory blocks may be determined as the target memory blocks. For example, if the requested memory granularity is 4K and the requested memory amount is 2.5MB, and the total memory amount corresponding to the 4K granularity is 10MB, then because the total memory amount corresponding to the 4K granularity (10MB) is greater than the requested memory amount (2.5MB), multiple target memory blocks can be determined in at least one memory block corresponding to the 4K granularity.
- Case 22 The total memory amount corresponding to the requested memory granularity is less than the requested memory amount.
- at least one memory block can be updated, and the target memory block can be determined in the updated at least one memory block.
- the at least one memory block can be updated in the following manner: determining a first memory block based on the requested memory granularity; dividing the first memory block into multiple second memory blocks; and determining that the updated at least one memory block includes at least one memory block and multiple second memory blocks.
- the memory block status of the first memory block is idle.
- the memory granularity of the first memory block is greater than the requested memory granularity.
- the memory granularity of the first memory block may be the memory granularity of the previous level corresponding to the requested memory granularity.
- the memory granularity of the second memory block is the requested memory granularity. For example, if the requested memory granularity is 4K, and the total memory amount corresponding to the 4K requested memory granularity is 2MB, which is less than the total memory amount of 2.5MB, the first memory block can be determined based on the 4K requested memory granularity.
- the memory granularity of the first memory block may be 2MB, and the first memory block is in an idle state.
- the 2MB first memory block can be split to obtain 512 4KB second memory blocks.
- the at least one updated memory block can then be determined to include multiple memory blocks corresponding to the total memory amount of 2MB and the 512 4KB second memory blocks.
- target memory blocks can then be determined from the at least one updated memory block, i.e., the 1024 memory blocks.
- a first memory granularity corresponding to the first memory block can be determined, first memory block information corresponding to the first memory granularity can be determined, and the first memory block information can be updated based on the first memory block.
- Second memory block information corresponding to the requested memory granularity can be determined, and the second memory block information can be updated based on the multiple second memory blocks.
- the first memory block information corresponding to the 2M granularity i.e., memory block information 1
- the memory block status corresponding to the first memory block i.e., memory block 6
- memory block information 5 corresponding to the multiple second memory blocks can be newly added based on the multiple second memory blocks.
- the index number in memory block information 5 can be 6, and the memory block status can all be in the idle state.
- the process of determining the target memory block corresponding to the requested memory granularity of 4K is substantially the same as the process of determining the target memory block corresponding to the 2M granularity in step 505.
- the electronic device may determine the current allocation address in the memory block information corresponding to the requested memory granularity, and determine whether the current allocation address is aligned.
- the electronic device may traverse the memory block status of each memory block starting from the current allocation address, and then determine multiple target memory blocks corresponding to the requested memory granularity in the at least one memory block corresponding to the requested memory granularity based on the memory block status. If not, the electronic device may determine an aligned allocation address, traverse the memory block status of each memory block starting from the aligned allocation address, and then determine multiple target memory blocks corresponding to the requested memory granularity in the at least one memory block corresponding to the requested memory granularity based on the memory block status.
- the target memory block can be determined in the at least one memory block corresponding to the memory application granularity in the following manner: determining a first number M of target memory blocks based on the memory application amount and the memory application granularity; determining whether there are M contiguous memory blocks in the at least one memory block; if so, determining the M contiguous memory blocks as the target memory blocks; if not, when the memory operation request indicates that discontinuous memory allocation is permitted, determining M discontinuous memory blocks in the at least one memory block as the target memory blocks, where the sum of the memory of the discontinuous memory blocks is greater than or equal to the memory application amount.
- the M target memory blocks corresponding to the memory application granularity when determining the M target memory blocks corresponding to the memory application granularity, it can be predetermined, based on the memory application amount, that the M target memory blocks include a total of K consecutive first target memory blocks corresponding to the first memory length and/or M K second target memory blocks corresponding to the second memory length.
- the electronic device can determine the K consecutive first target memory blocks corresponding to the first memory length, and then determine M K second target memory blocks corresponding to the second memory length.
- the M K second target memory blocks may be contiguous or discontiguous with the K consecutive first target memory blocks.
- the electronic device may predetermine, based on the requested memory size of 2.5MB, that 640 target memory blocks are required. These 640 target memory blocks include 512 target memory blocks corresponding to the first memory length of 2MB and 128 target memory blocks corresponding to the second memory length of 4MB. The electronic device may then determine in memory block information 2 that the current allocation address is address C, and determine whether address C is aligned with 2MB.
- the electronic device may traverse multiple memory block states in memory block information 2 starting from address C. If not, the electronic device may determine an aligned allocation address C1, and traverse multiple memory block states in memory block information 2 starting from address C1. Based on the states of multiple memory blocks in memory block information 2, the electronic device can prioritize 512 free, contiguous first target memory blocks and designate these 512 first target memory blocks as the 512 target memory blocks. The corresponding memory lengths of these 512 target memory blocks are aligned to 2MB. If these 512 target memory blocks do not exist in memory block information 2, the electronic device can prioritize 512 target memory blocks based on memory block information 3 or memory block information 5 in the same manner.
- the electronic device can determine 512 target memory blocks based on memory block information 5, then, since the memory lengths of the other 128 target memory blocks are less than 2MB, the electronic device can determine 128 4KB target memory blocks based on memory block information 2 and/or memory block information 3.
- the electronic device can determine whether the currently allocated address is aligned to 4MB. If aligned, the memory block status in memory block information 2 can be traversed starting from the current allocation address. If not aligned, the aligned allocation address can be determined, and the memory block status in memory block information 2 can be traversed starting from the aligned allocation address to determine multiple free memory blocks, which are then identified as multiple target memory blocks.
- the electronic device can determine a total of 640 free, discontinuous memory blocks based on memory block information 2, memory block information 3, and memory block information 5, and determine the 640 free, discontinuous memory blocks as 640 target memory blocks. S507. Allocate a target memory block corresponding to the requested memory granularity.
- memory operation request 1 requests allocation of 1.5 GB of memory with a memory granularity of 2 MB
- the electronic device can allocate the 768 contiguous 2 MB target memory blocks. If the electronic device determines 768 discontinuous 2 MB target memory blocks, including 512 contiguous 2 MB target memory blocks and 256 contiguous 2 MB target memory blocks, the electronic device can first allocate the 512 contiguous 2 MB target memory blocks, and then allocate the 256 contiguous 2 MB target memory blocks.
- memory operation request 2 requests allocation of 2.5MB of memory with a memory granularity of 4KB
- the electronic device may first allocate the 512 contiguous 4KB target memory blocks, and then allocate the 128 non-contiguous 4KB target memory blocks.
- the memory operation request indicates that memory should be allocated according to an alignment mode
- the returned allocation end address may be checked for each allocation to determine whether the allocation end address meets the alignment requirement. If the alignment requirement is met, the target memory block is allocated.
- the memory block information corresponding to the requested memory granularity can be updated based on the target memory block. Specifically, the memory block status, memory usage information, and current allocation address in the memory block information can be updated.
- the electronic device allocates 768 memory blocks of 2MB granularity based on memory block information 1, then based on these 768 memory blocks, the corresponding memory block status in memory block information 1 can be updated to a used state, and the memory usage information and current allocation address can be updated to address B.
- the requested memory granularity is 4 KB
- the electronic device allocates 60 target memory blocks of 4 KB granularity according to memory block information 2
- the corresponding memory block status of the 60 target memory blocks in memory block information 2 may be updated to a used status
- the memory usage information and the currently allocated address may be updated to address D.
- the electronic device allocates 68 target memory blocks of 4 KB granularity according to memory block information 3
- the memory block status corresponding to the 68 target memory blocks may be updated to a used status
- the memory usage information and the currently allocated address may be updated to address F in memory block information 3.
- 512 target memory blocks of 4 KB granularity are allocated according to memory block information 5
- the memory block status corresponding to the 512 target memory blocks may be updated to a used status
- the memory usage information and the currently allocated address may be updated to address H in memory block information 5.
- FIG6 is a second schematic diagram of the first linked list and the second linked list provided in an exemplary embodiment of the present disclosure.
- the electronic device allocates a total of 640 4K granularity memory blocks according to memory block information 2, memory block information 3, and memory block information 5,
- the first and second linked lists are as shown in Figure 4, it can be determined whether there is still an idle state in memory block information 2. If not, memory block information 2 can be moved to the second linked list, and the address of the next memory block information in memory block information 4 can be updated to address 1. If there is an idle state in memory block information 3, there is no need to move memory block information 3 to the second linked list; memory block information 3 remains in the first linked list. Since memory block information 2 has been moved to the second linked list, the first linked list address in memory block information 1 can be updated to address 3.
- memory block information 5 can be added to the second linked list, and the address of the next memory block information in memory block information 2 can be updated to address 5, resulting in the new first and second linked lists, as shown in Figure 6. It should be noted that if the memory operation request includes a specified memory node, steps S502 to S508 may be performed in the specified memory node. If the memory operation request does not include a specified memory node, and if the electronic device includes multiple memory nodes, it may be determined whether the remaining capacity of the first memory node is greater than or equal to the total memory request (the sum of the memory request capacities corresponding to the requested memory granularities).
- an electronic device may obtain a memory operation request for a memory and determine memory block information corresponding to at least one memory granularity. The electronic device may determine the memory block information corresponding to the requested memory granularity and determine whether the requested memory granularity is the maximum memory granularity.
- a target memory block can be determined from at least one memory block corresponding to the requested memory granularity. If not, when the total amount of memory corresponding to the requested memory granularity is greater than or equal to the requested memory amount, a target memory block can be determined from at least one memory block corresponding to the requested memory granularity. When the total amount of memory corresponding to the requested memory granularity is less than the requested memory amount, at least one memory block is updated, and the target memory block is determined from the updated memory block.
- the electronic device can allocate a target memory block corresponding to the requested memory granularity and, based on the target memory block, update the memory block information corresponding to each requested memory granularity. Since the memory can include multiple memory blocks corresponding to at least two memory granularities, and each memory granularity has corresponding memory block information, when allocating memory, operations can be performed on the multiple memory blocks corresponding to any one memory granularity in the memory according to actual needs and based on the memory block information. This improves the flexibility of memory operations compared to the prior art, where the memory includes multiple memory blocks corresponding to only one memory granularity and operations are performed based only on that one memory granularity.
- Figure 7 is a flow chart of another memory operation method provided by an exemplary embodiment of the present disclosure. Referring to FIG. 7 , the method may include:
- the memory operation request is used to request the release of memory.
- the memory operation request may include the memory release granularity and the memory address corresponding to the memory release granularity.
- the memory release granularity may be any of the following: 1G granularity, 2M granularity, 16K granularity, 4K granularity, etc.
- the memory address may include at least one set of a release start address and a release end address.
- the electronic device may obtain a memory operation request in response to a user operation on the memory.
- the electronic device may obtain a memory operation request 3 requesting the release of a 2M granularity memory block with memory addresses from address A to address B, a memory operation request 4, and a memory operation request 5.
- Request 4 is a request to release the 4K memory blocks with memory addresses C to D, E to F, and G to H.
- step S702 Determine memory block information corresponding to at least one memory granularity. It should be noted that the execution process of step S702 can refer to step S202 and is not further described here.
- the electronic device may determine the memory block information corresponding to the released memory granularity and, based on the memory block information and memory address, determine at least one memory block to be released corresponding to the released memory granularity. For example, if the released memory granularity is 2MB, if there is memory block information 1 corresponding to the 2MB granularity, and if the memory addresses are from address A to address B, the electronic device may determine memory block information 1 and, based on memory block information 1 and addresses A to address B, determine at least one memory block to be released.
- 768 memory blocks to be released with a 2MB granularity can be determined. For example, if the memory release granularity is 4 KB, if there are memory block information 2, memory block information 3, memory block information 4, and memory block information 5 corresponding to the 4 KB granularity, and if the memory addresses are from address C to address D, from address E to address F, and from address G to address H, then the electronic device can determine at least one memory block to be released based on the four memory block information and the three memory addresses.
- memory block information 2 based on addresses C to address D
- 68 memory blocks to be released with a 4 KB granularity can be determined in memory block information 3 based on addresses E to address F
- 512 memory blocks to be released with a 4 KB granularity can be determined in memory block information 5 based on addresses G to address H.
- At least one memory block to be released to release the at least one memory block For example, if the electronic device determines 768 memory blocks to be released with a granularity of 2M, clear the 768 memory blocks to be released with a granularity of 2M to release to release the 768 memory blocks to be released with a granularity of 2M. For example, if the electronic device determines 640 memory blocks to be released with a granularity of 4K to release, clear the 640 memory blocks to be released with a granularity of 4K to release to release the 640 memory blocks to be released with a granularity of 4K.
- the memory block information corresponding to the released memory granularity may be updated based on the at least one memory block. Specifically, the memory block status, memory usage information, and current allocation address in the memory block information may be updated.
- the electronic device releases 768 memory blocks of 2MB granularity based on memory block information 1 and addresses A to B, then based on these 768 memory blocks, the corresponding memory block status in memory block information 1 may be updated to an idle state, and the memory usage information and current allocation address may be updated to address A.
- the released memory granularity is 4K
- the electronic device releases the 60 memory blocks of 4K granularity according to the memory block information 2 and the address C to the address D
- the corresponding memory block status in the memory block information 2 can be updated to the idle state according to the 60 memory blocks, and the memory usage information and the current allocated address can be updated to the address C
- the electronic device releases 68 memory blocks of 4K granularity to be released according to the memory block information 3 and the address E to the address F
- the corresponding memory block status in memory block information 3 can be updated to free, and the memory usage information and the current allocation address can be updated to address E.
- the corresponding memory block status in memory block information 5 can be updated to free, and the memory usage information and the current allocation address can be updated to address G.
- the released memory granularity is smaller than the maximum memory granularity, and if the memory block states in the third memory block information corresponding to the released memory granularity are all in the idle state, multiple memory blocks are merged according to the third memory block information to obtain a third memory block.
- the memory granularity of the third memory block is the maximum memory granularity.
- memory block information 5 is the third memory block information. Then, based on memory block information 5, the multiple 4 KB memory blocks corresponding to memory block information 5 can be merged to obtain the third memory block. Assume that the third memory block is a memory block of 2 MB granularity.
- S707 Update the memory block information corresponding to the maximum memory granularity based on the third memory block, and delete the third memory block information. Since the memory block status of the third memory block has changed and the memory granularity of the third memory block is the maximum memory granularity, the memory block information corresponding to the maximum memory granularity can be updated based on the third memory block. Optionally, since the multiple memory blocks corresponding to the released memory granularity no longer exist after merging, the memory block information corresponding to the multiple memory blocks can be deleted. For example, if the third memory block is memory block 6 with a 2M granularity, memory block information 1 corresponding to the 2M granularity can be updated based on the third memory block.
- memory block information 1 the memory block status corresponding to the third memory block, i.e., memory block 6, is updated to an idle state, and the memory usage information in memory block information 1 is updated. Since the multiple 4K memory blocks used to merge the third memory block no longer exist, memory block information 5 can be deleted.
- the released memory granularity is smaller than the maximum memory granularity, and before releasing a memory block, any memory block information corresponding to the released memory granularity is in the second linked list, then after releasing the memory block, if the memory block information contains an idle state, the memory block information can be moved to the first linked list.
- an electronic device can obtain a memory operation request for a memory and determine the memory block information corresponding to at least one memory granularity.
- the electronic device may determine at least one memory block to be released corresponding to the released memory granularity based on the memory block information and memory address corresponding to the released memory granularity, and clear the at least one memory block to be released to release the at least one memory block.
- the electronic device may update the memory block information corresponding to the released memory granularity based on the at least one memory block.
- the electronic device can merge multiple memory blocks based on the third memory block information to obtain a third memory block, update the memory block information corresponding to the maximum memory granularity based on the third memory block, and delete the third memory block information. Because the memory can include multiple memory blocks corresponding to at least two memory granularities, and each memory granularity has corresponding memory block information, when releasing memory, operations can be performed on the multiple memory blocks corresponding to any memory granularity in the memory based on the memory block information as needed.
- the determination module 12 is configured to determine memory block information corresponding to the at least one memory granularity, wherein the memory block information includes a memory granularity value, a memory block status, and memory block usage information.
- the processing module 13 is configured to process the memory operation request based on the memory block information corresponding to the at least one memory granularity to allocate or release memory.
- the memory operation device provided in the embodiments of the present disclosure can implement the technical solutions shown in the above-described method embodiments. Its implementation principles and beneficial effects are similar and are not further described here.
- the memory operation request is used to request memory allocation, and the memory operation request includes a requested memory granularity and a memory request amount corresponding to the requested memory granularity.
- the processing module 13 is specifically configured to: determine memory block information corresponding to the requested memory granularity; determine a target memory block corresponding to the requested memory granularity based on the memory block information and the memory request amount, wherein the memory granularity of the target memory block is the requested memory granularity and the memory block status of the target memory block is an idle state; and allocate the target memory block corresponding to the requested memory granularity.
- the requested memory granularity is smaller than the maximum memory granularity; the processing module 13 is specifically configured to: determine a previous-level memory granularity corresponding to the requested memory granularity, wherein the memory block corresponding to the previous-level memory granularity is divided into multiple memory blocks corresponding to the requested memory granularity; determine a first linked list address in the memory block information corresponding to the previous-level memory granularity; and determine the memory block information corresponding to the requested memory granularity based on the first linked list address.
- the processing module 13 is specifically configured to: determine whether the memory granularity applied is the maximum memory granularity; if so, when the total amount of memory corresponding to the memory granularity applied is greater than or equal to the memory application amount, The target memory block is determined from at least one memory block corresponding to the requested memory granularity. If not, when the total amount of memory corresponding to the requested memory granularity is greater than or equal to the requested memory amount, the target memory block is determined from at least one memory block corresponding to the requested memory granularity. When the total amount of memory corresponding to the requested memory granularity is less than the requested memory amount, the at least one memory block is updated and the target memory block is determined from the updated at least one memory block.
- the processing module 13 is specifically configured to: determine a first memory block based on the requested memory granularity, wherein the memory granularity of the first memory block is greater than the requested memory granularity and the memory block status of the first memory block is idle; split the first memory block to obtain multiple second memory blocks, wherein the memory granularity of the second memory block is the requested memory granularity; and determine that the updated at least one memory block includes the at least one memory block and the multiple second memory blocks.
- the memory operation device provided in the embodiment of the present disclosure can implement the technical solution shown in the above-mentioned method embodiment. Its implementation principles and beneficial effects are similar and are not further described here.
- Figure 9 is a schematic structural diagram of another memory operation device provided in the embodiment of the present disclosure.
- the memory operation device 10 may further include: an update module 14 configured to determine a first memory granularity corresponding to the first memory block, determine first memory block information corresponding to the first memory granularity, and update the first memory block information based on the first memory block; and an update module 14 configured to determine second memory block information corresponding to the requested memory granularity and update the second memory block information based on the plurality of second memory blocks.
- the memory operation device provided in the embodiments of the present disclosure can implement the technical solutions shown in the above-described method embodiments. The implementation principles and beneficial effects thereof are similar and are not further described herein.
- the processing module 13 is specifically used to: determine the first number of target memory blocks based on the memory application amount and the application memory granularity; determine whether there are a first number of continuous memory blocks in the at least one memory block; if so, determine the first number of continuous memory blocks as the target memory block; if not, when the memory operation request indicates that discontinuous memory allocation is allowed, determine the first number of discontinuous memory blocks in the at least one memory block as the target memory block, and the sum of the memory of the discontinuous memory blocks is greater than or equal to the memory application amount.
- the update module 14 is also used to: update the memory block information corresponding to the application memory granularity based on the target memory block.
- the memory operation request is used to request the release of memory; the memory operation request includes the released memory granularity and the memory address corresponding to the released memory granularity; the processing module 13 is specifically used to: determine at least one to-be-released memory block corresponding to the released memory granularity based on the memory block information corresponding to the released memory granularity and the memory address; The at least one memory block to be released is cleared to release the at least one memory block.
- the update module 14 is further configured to update the memory block information corresponding to the released memory granularity based on the at least one memory block.
- the computer program When executed by a processor, the computer program can implement the method described in the above method embodiment.
- Those skilled in the art will appreciate that embodiments of the present disclosure may be provided as methods, systems, or computer program products. Therefore, the present disclosure may take the form of an entirely hardware embodiment, an entirely software embodiment, or an embodiment combining software and hardware aspects. Furthermore, the present disclosure may take the form of a computer program product implemented on one or more computer-usable storage media (including but not limited to disk storage, CD-ROMs, optical storage, etc.) containing computer-usable program code.
- the present disclosure is described with reference to flowcharts and/or block diagrams of methods, devices (systems), and computer program products according to embodiments of the present disclosure.
- each process and/or block in the flowcharts and/or block diagrams, as well as combinations of processes and/or blocks in the flowcharts and/or block diagrams, can be implemented by computer program instructions.
- These computer program instructions can be provided to a processor of a general-purpose computer, a special-purpose computer, an embedded processor, or other programmable data processing device to produce a machine, such that execution of the instructions by the processor of the computer or other programmable data processing device produces means for implementing the functions specified in one or more processes in the flowcharts and/or one or more blocks in the block diagrams.
- These computer program instructions can also be stored in a computer program that directs the computer or other programmable data processing device to operate in a specific manner.
- the computer program instructions may be loaded into a computer-readable memory operable in a manner such that the instructions stored in the computer-readable memory produce an article of manufacture including instruction means that implement the functions specified in one or more flows in a flowchart and/or one or more blocks in a block diagram.
- These computer program instructions may also be loaded onto a computer or other programmable data processing device, causing the computer or other programmable device to execute a series of operational steps to produce a computer-implemented process, such that the instructions executed on the computer or other programmable device provide steps for implementing the functions specified in one or more flows in a flowchart and/or one or more blocks in a block diagram.
- a computing device includes one or more processors (CPUs), input/output interfaces, network interfaces, and memory.
- Memory may include non-volatile memory in a computer-readable medium, random access memory (RAM), and/or non-volatile memory, such as read-only memory (ROM) or flash memory.
- RAM random access memory
- ROM read-only memory
- Memory is an example of a computer-readable medium.
- Computer-readable media including both permanent and non-permanent, removable and non-removable media, may be implemented using any method or technology for information storage.
- Information may be computer-readable instructions, data structures, program modules, or other data.
Landscapes
- Engineering & Computer Science (AREA)
- Theoretical Computer Science (AREA)
- Physics & Mathematics (AREA)
- General Engineering & Computer Science (AREA)
- General Physics & Mathematics (AREA)
- Human Computer Interaction (AREA)
- Software Systems (AREA)
- Memory System (AREA)
Abstract
本公开提供一种内存操作方法、装置、设备、存储介质及程序产品,所述方法可以包括:获取对内存的内存操作请求,内存操作请求用于请求分配内存或者请求释放内存,内存中包括多个内存块,多个内存块对应至少两种内存粒度;确定至少一种内存粒度对应的内存块信息,内存块信息中包括内存粒度值、内存块状态、以及内存块使用信息;根据至少一种内存粒度对应的内存块信息对内存操作请求进行处理,以进行分配内存或者释放内存。本公开的方法提高了对内存进行操作的灵活性。
Description
内存操作 方法、 装置、 设备、 存储介质及程序产品 本公开要求于 2024年 02月 26 日提交中国专利局、 申请号为 202410211042.3、 申请 名称为 “内存操作方法、 装置、 设备、 存储介质及程序产品 ” 的中国专利申请的优先权, 其全部内容通过引用结合在本公开中。 技术领域 本公开涉及存储领域, 尤其涉及一种内存操作方法、 装置、 设备、 存储介质及程 序产品。 背景技术 电子设备中可以设有 内存。 在电子设备中可以按照内存粒度对内存进行管理。 在相关技术中, 在一些场景下, 可以按照第一内存粒度 (例如, 4K (千) 粒度) 将内存划分 为多个内存块, 以对内存进行操作; 在另一些场景下, 也可以按照第二内 存粒度 (例如, 2M(兆)粒度)将内存划分为多个内存块, 以对内存进行操作。然而, 在上述 2种方式中, 按照第一内存粒度对内存进行操作时, 无法兼顾到需使用第二内 存粒度的场 景; 按照第二内存粒度对内存进行操作时, 无法兼顾到需使用第一内存粒 度的场景 。 由上可知, 相关技术中, 对内存进行操作的灵活性差。 发明内容 本公开 的多个方面提供一种内存操作方法、 装置、 设备、 存储介质及程序产品, 用以 提高对内存进行操作的灵活性。 第一方面, 本公开实施例提供一种内存操作方法, 包括: 获取对 内存的内存操作请求,所述内存操作请求用于请求分配内存或者请求释放内存 , 所述内存中包括多个内存块, 所述多个内存块对应至少两种内存粒度; 确定至少一种 内存粒度对应的内存块信息, 所述内存块信息中包括内存粒度值、 内存 块状态、 以及内存块使用信息; 根据至 少一种内存粒度对应的内存块信息对所述内存操作请求进行处理, 以进行分配 内存或者释放内存。 在一种可 能的实施方式中, 所述内存操作请求用于请求分配内存, 所述内存操作请求
中包括申请内存粒度、 以及所述申请内存粒度对应的内存申请量; 根据至少一种内存粒度 对应的内存块信息对所述内存操作请求进行处理, 以进行内存分配, 包括: 确定所述 申请内存粒度对应的内存块信息; 根据所述 申请内存粒度对应的内存块信息和内存申请量,确定所述申请内存粒度对应 的目标内存块, 所述目标内存块的内存粒度为所述申请内存粒度, 所述目标内存块的内存 块状态为空闲状态; 分配所述 申请内存粒度对应的目标内存块。 在一种可能的实施方式 中, 所述申请内存粒度小于最大内存粒度; 确定所述申请内存 粒度对应的内存块信息, 包括: 确定所述 申请内存粒度对应的上一级内存粒度,所述上一级内存粒度对应的内存块被 划分为多个所述申请内存粒度对应的内存块; 在所述上一级 内存粒度对应的内存块信息中确定第一链表地址; 根据所述 第一链表地址确定所述申请内存粒度对应的内存块信息。 在一种可能的实施方式 中,根据所述申请内存粒度对应的内存块信息和所述内存申请 量, 确定所述目标内存块, 包括: 判断所述 申请内存粒度是否为最大内存粒度; 若是, 则在所述申请内存粒度对应的内存总量大于或等于所述内存申请量时, 在所述 申请内存粒度对应的至少一个内存块中确定所述目标内存块; 若否, 则在所述申请内存粒度对应的内存总量大于或等于所述内存申请量时, 在所述 申请内存粒度对应的至少一个内存块中确定所述目标内存块;在所述申请内存粒度对应的 内存总量小于所述内存申请量时, 更新所述至少一个内存块, 并在更新后的至少一个内存 块中确定所述目标内存块。 在一种可能的实施方式 中, 更新所述至少一个内存块, 包括: 根据所述 申请内存粒度, 确定第一内存块, 所述第一内存块的内存粒度大于所述申请 内存粒度, 所述第一内存块的内存块状态为空闲状态; 对所述 第一内存块进行切分处理, 得到多个第二内存块, 所述第二内存块的内存粒度 为所述申请内存粒度; 确定更新后的至少一个 内存块包括所述至少一个内存块和所述多个第二内存块。 在一种可能的实施方式 中, 所述方法还包括: 确定所述第一 内存块对应的第一内存粒度,确定所述第一内存粒度对应的第一内存块 信息, 并根据所述第一内存块更新所述第一内存块信息; 确定所述 申请内存粒度对应的第二内存块信息,并根据所述多个第二内存块更新所述 第二内存块信息。 在一种可能的实施方式 中,在所述申请内存粒度对应的至少一个内存块中确定所述目 标内存块, 包括:
根据所述 内存申请量和所述申请内存粒度, 确定所述目标内存块的第一数量; 判 断所述至少一个内存块中, 是否存在第一数量个连续内存块; 若是 , 则将所述第一数量个连续内存块确定为所述目标内存块; 若否 ,则在所述内存操作请求指示允许分配内存不连续时,将所述至少一个内存块中、 第一数量个不连续的内存块确定为所述目标内存块,所述不连续的内存块的内存之和大于 或等于所述内存申请量。 在一种 可能的实施方式中, 所述方法还包括: 根据所述 目标内存块, 更新所述申请内存粒度对应的内存块信息。 在一种 可能的实施方式中, 所述内存操作请求用于请求释放内存; 所述内存操作请求 中包括释放内存粒度、 以及每种释放内存粒度对应的内存地址; 根据至少一个内存粒度对 应的内存块信息对所述内存操作请求进行处理, 以进行释放内存, 包括: 根据所述释放 内存粒度对应的内存块信息和所述内存地址,确定所述释放内存粒度对 应的至少一个待释放内存块; 对所述 至少一个待释放内存块进行清零处理, 以释放所述至少一个内存块。 在一种 可能的实施方式中, 所述方法还包括: 根据所述 至少一个内存块, 更新所述释放内存粒度对应的内存块信息。 在一种 可能的实施方式中, 所述方法还包括: 若所述释放 内存粒度小于最大内存粒度,且若所述释放内存粒度对应的第三内存块信 息中内存块状态均为空闲状态, 则根据所述第三内存块信息对多个内存块进行合并处理, 得到第三内存块, 所述第三内存块的内存粒度为最大内存粒度; 根据所述 第三内存块更新所述最大内存粒度对应的内存块信息,并删除所述第三内存 块信息。 第二方面, 本公开实施例提供一种内存操作装置, 包括: 获取模块、 确定模块和处理 模块, 其中, 所述获取模块 用于, 获取对内存的内存操作请求, 所述内存操作请求用于请求分配内 存或者请求释放内存, 所述内存中包括多个内存块, 所述多个内存块对应至少一种内存粒 度; 所述确定模块 用于, 确定至少一种内存粒度对应的内存块信息, 所述内存块信息中包 括内存粒度值、 内存块状态、 以及内存块使用信息; 所述处理模块 用于,根据至少一种内存粒度对应的内存块信息对所述内存操作请求进 行处理, 以进行分配内存或者释放内存。 在一种 可能的实施方式中, 所述内存操作请求用于请求分配内存, 所述内存操作请求 中包括申请内存粒度、以及所述申请内存粒度对应的内存申请量;所述处理模块具体用于 : 确定所述 申请内存粒度对应的内存块信息; 根据所述 申请内存粒度对应的内存块信息和内存申请量,确定所述申请内存粒度对应
的目标内存块, 所述目标内存块的内存粒度为所述申请内存粒度, 所述目标内存块的内存 块状态为空闲状态; 分配所述 申请内存粒度对应的目标内存块。 在一种可能的实施方式 中, 所述申请内存粒度小于最大内存粒度; 所述处理模块具体 用于: 确定所述 申请内存粒度对应的上一级内存粒度,所述上一级内存粒度对应的内存块被 划分为多个所述申请内存粒度对应的内存块; 在所述上一级 内存粒度对应的内存块信息中确定第一链表地址; 根据所述 第一链表地址确定所述申请内存粒度对应的内存块信息。 在一种可能的实施方式 中, 所述处理模块具体用于: 判断所述 申请内存粒度是否为最大内存粒度; 若是, 则在所述申请内存粒度对应的内存总量大于或等于所述内存申请量时, 在所述 申请内存粒度对应的至少一个内存块中确定所述目标内存块; 若否, 则在所述申请内存粒度对应的内存总量大于或等于所述内存申请量时, 在所述 申请内存粒度对应至少一个内存块中确定所述目标内存块;在所述申请内存粒度对应的至 少一个内存块的内存总量小于所述内存申请量时, 更新所述至少一个内存块, 并在更新后 的至少一个内存块中确定所述目标内存块。 在一种可能的实施方式 中, 所述处理模块具体用于: 根据所述 申请内存粒度, 确定第一内存块, 所述第一内存块的内存粒度大于所述申请 内存粒度, 所述第一内存块的内存块状态为空闲状态; 对所述 第一内存块进行切分处理, 得到多个第二内存块, 所述第二内存块的内存粒度 为所述申请内存粒度; 确定更新后的至少一个 内存块包括所述至少一个内存块和所述多个第二内存块。 在一种可能的实施方式 中, 所述装置还包括: 更新模块, 所述更新模块用于: 确定所述第一 内存块对应的第一内存粒度,确定所述第一内存粒度对应的第一内存块 信息, 并根据所述第一内存块更新所述第一内存块信息; 确定所述 申请内存粒度对应的第二内存块信息,并根据所述多个第二内存块更新所述 第二内存块信息。 在一种可能的实施方式 中, 所述处理模块具体用于: 根据所述 内存申请量和所述申请内存粒度, 确定所述目标内存块的第一数量; 判断所述至少一个 内存块中, 是否存在第一数量个连续内存块; 若是, 则将所述第一数量个连续内存块确定为所述目标内存块; 若否,则在所述内存操作请求指示允许分配内存不连续时,将所述至少一个内存块中、 第一数量个不连续的内存块确定为所述目标内存块,所述不连续的内存块的内存之和大于 或等于所述内存申请量。
在一种可 能的实施方式中, 所述更新模块还用于: 根据所述 目标内存块, 更新所述申请内存粒度对应的内存块信息。 在一种可 能的实施方式中, 所述内存操作请求用于请求释放内存; 所述内存操作请求 中包括释放内存粒度、 以及所述释放内存粒度对应的内存地址; 所述处理模块具体用于: 根据所述释放 内存粒度对应的内存块信息和所述内存地址,确定所述释放内存粒度对 应的至少一个待释放内存块; 对所述至 少一个待释放内存块进行清零处理, 以释放所述至少一个内存块。 在一种可 能的实施方式中, 所述更新模块还用于: 根据所述至 少一个内存块, 更新所述释放内存粒度对应的内存块信息。 在一种可 能的实施方式中, 所述处理模块还 用于, 若所述释放内存粒度小于最大内存粒度, 且若所述释放内存粒 度对应的第三内存块信息中内存块状态均为空闲状态, 则根据所述第三内存块信息对多个 内存块进行合并处理, 得到第三内存块, 所述第三内存块的内存粒度为最大内存粒度; 所述更新模块还 用于,根据所述第三内存块更新所述最大内存粒度对应的内存块信息, 并删除所述第三内存块信息。 第三方面, 本公开实施例提供一种电子设备, 包括: 存储器和处理器; 所述存储器存储计算机执 行指令; 所述处理器执行所述存储 器存储的计算机执行指令 , 使得所述处理器执行第一方 面任一项所 述的方法。 第四方面, 本公开实施例提供一种计算机可读存储介质 , 所述计算机可读存储介 质中存储有 计算机执行指令, 当所述计算机执行指令被处理器执行时 用于实现第一方 面任一项所 述的方法。 第五方面, 本公开实施例提供一种计算机程序产品, 包括计算机程序, 该计算机程序 被处理器执行时实现第一方面任一项所示的方法。 本公开实施例提供一种 内存操作方法、 装置、 设备、 存储介质及程序产品, 电子设备 可以获取对内存的内存操作请求, 并确定至少一种内存粒度对应的内存块信息, 进而可以 根据至少一种内存粒度对应的内存块信息对内存操作请求进行处理, 以进行分配内存或者 释放内存。 由于内存中可以包括至少两种内存粒度对应的多个内存块, 且每种内存粒度有 对应的内存块信息。在分配内存或释放内存时, 可以按照实际需求根据内存块信息对内存 中任意一种内存粒度对应的多个内存块进行操作,相比现有技术中内存只包括一种内存粒 度对应的多个内存块, 且只根据这一种内存粒度对内存进行操作, 提高了对内存进行操作 的灵活性。 附图说明 此处所说 明的附图用来提供对本公开的进一步理解, 构成本公开的一部分, 本公开的
示意性实施例及其说明用于解释本公开, 并不构成对本公开的不当限定。 在附图中: 图 1为本公开示例性实施例提供的一种场景示意图; 图 2为本公开示例性实施例提供的一种内存操作的流程示意图; 图 3为本公开示例性实施例提供的至少一种内存粒度对应的内存块信息的示意图; 图 4为本公开示例性实施例提供的第一链表和第二链表的示意图一; 图 5为本公开示例性实施例提供的另一种内存操作方法的流程示意图; 图 6为本公开示例性实施例提供的第一链表和第二链表的示意图二; 图 7为本公开示例性实施例提供的又一种内存操作方法的流程示意图; 图 8为本公开实施例提供一种内存操作装置的结构示意图; 图 9为本公开实施例提供的另一种内存操作装置的结构示意图; 图 10为本公开示例性实施例提供的一种电子设备的结构示意图。 具体实施方 式 需要说明的是, 本公开所涉及的用户信息 (包括但不限于用户设备信息、 用户个人信 息等) 和数据 (包括但不限于用于分析的数据、 存储的数据、 展示的数据等) , 均为经用 户授权或者经过各方充分授权的信息和数据, 并且相关数据的收集、使用和处理需要遵守 相关法律法规和标准, 并提供有相应的操作入口, 供用户选择授权或者拒绝。 为使本公开的目的、 技术方案和优点更加清楚, 下面将结合本公开具体实施例及相应 的附图对本公开技术方案进行清楚、 完整地描述。 显然, 所描述的实施例仅是本公开一部 分实施例, 而不是全部的实施例。 基于本公开中的实施例, 本领域普通技术人员在没有做 出创造性劳动前提下所获得的所有其他实施例, 都属于本公开保护的范围。 图 1为本公开示例性实施例提供的一种场景示意图。 请参见图 1, 内存中可以包括多 个内存块。 该多个内存块可以分别为内存块 1、 内存块 2、 ……、 内存块 n。 该 多个内存块可以对应至少一种内存粒度。例如, 至少一种内存粒度可以包括如下至 少一种: 1G (千兆) 粒度、 2M粒度、 16K粒度、 4K粒度等。 例如 , 内存块 1和内存块 2对应的内存粒度可以为 2M粒度, 即内存块 1和内存块 2 的大小为 2M; 内存块 3对应的内存粒度可以为 4K粒度, 即内存块 3的大小为 4K。 电子设备可以获取内存操作请求, 根据内存操作请求, 对至少一种内存粒度对应的多 个内存块进行内存分配或释放内存等处理。 例如, 若内存操作请求为请求分配 1G内存, 内存粒度为 2M粒度, 则可以根据该内 存操作请求, 在内存中确定 512个 2M粒度的内存块, 以分配该 512个内存块。 在相关技术中, 在一些场景下, 可以按照第一内存粒度 (例如, 4K粒度) 将内 存划分为 多个内存块, 以对内存进行操作; 在另一些场景下, 也可以按照第二内存粒 度 (例如, 2M粒度) 将内存划分为多个内存块, 以对内存进行操作。 然而, 在上述 2 种方式中 , 按照第一内存粒度对内存进行操作时, 无法兼顾到需使用第二内存粒度的
场景;按照第二内存粒度对 内存进行操作时,无法兼顾到需使用第一 内存粒度的场景 O 因此在相 关技术中, 对内存进行操作的灵活性差。 在本公开实施例 中, 内存中可以包括至少两种内存粒度对应的多个内存块。 电子设备 获取内存操作请求之后, 可以根据至少一种内存粒度对应的内存块信息, 对内存操作请求 进行处理, 以进行分配内存或者释放内存等操作。 由于内存中可以包括至少两种内存粒度 对应的多个内存块, 且每种内存粒度有对应的内存块信息。 在分配内存或释放内存时, 可 以按照实际需求根据内存块信息对内存中任意一种 内存粒度对应的多个内存块进行操作, 相比现有技术中内存只包括一种内存粒度对应的多个内存块,且只根据这一种内存粒度对 内存进行操作, 提高了对内存进行操作的灵活性。 下 面, 通过具体实施例对本公开所示的技术方案进行详细说明。 需要说明的是, 下面 几个实施例可以单独存在, 也可以相互结合, 对于相同或相似的内容, 在不同的实施例中 不再重复说明。 图 2为本公开示例性实施例提供的一种内存操作的流程示意图。 请参见图 2, 所述方 法可以包括:
5201、 获取对内存的内存操作请求。 本公开实施例 的执行主体可以为电子设备,也可以为设置在电子设备中的内存操作装 置。 内存操作装置可以通过软件实现, 也可以通过软件和硬件的结合实现。 内存操作装置 可以为电子设备中的处理器。 为了便于理解, 在下文中, 以执行主体为电子设备为例进行 说明。 内存中可以包括多个内存块, 该多个内存块可以对应至少一种内存粒度。 例如, 如图 1所示, 内存可以包括 n个内存块 (n为大于或等于 2的整数) , 其中, 内存块 1和内存 块 2的内存粒度可以为 2M粒度, 内存块 3的内存粒度可以为 4K粒度。 内存操作请求可以用于请求分配内存或者请求释放内存。 电子设备可以响应于用户对内存的操作, 获取对内存的内存操作请求。 例如, 内存操 作请求 1可以为请求分配内存粒度为 2M的 1.5G内存、 内存操作请求 2可以为请求分配 内存粒度为 4K的 2.5M内存; 内存操作请求 3可以为请求释放内存地址为地址 B的 2M 粒度的内存块。
5202、 确定至少一种内存粒度对应的内存块信息。 由于内存中可以包括至少一种内存粒度对应的多个内存块, 因此可以按照内存粒度, 记录多个内存块的信息, 得到至少一种内存粒度对应的内存块信息。 下 面, 结合图 3, 对至少一种内存粒度对应的内存块信息进行说明。 图 3为本公开示例性实施例提供的至少一种内存粒度对应的内存块信息的示意图。请 参见图 3, 若内存中包括 n个 2M粒度的内存块, 分别为内存块 1、 内存块 2、 .、 内存 块 n, 则 2M粒度对应的内存块信息 1中可以包括该 n个内存块的信息。 若 内存块 2被切分为多个 4K粒度的内存块, 则 4K粒度对应的内存块信息 2中可以
包括内存块 2对应的多个 4K内存块的信息; 同样的, 内存块信息 3中可以包括内存块 5 对应的多个 4K内存块的信息; 内存块信息 4中可以包括内存块 8对应的多个 4K内存块 的信息。 因此, 如图 3所示, 2M粒度可以对应有内存块信息 1 , 4K粒度可以对应有 3个 内存块信息, 分别为内存块信息 2、 内存块信息 3和内存块信息 4。 内存块信息中可以包括内存粒度值、 内存块状态、 以及内存块使用信息。 可选地, 内 存块信息中还可以包括内存范围、 当前分配地址等。 针对任 意一个内存块, 内存块可以具有对应的内存块状态, 内存块状态可以包括空闲 状态、 使用状态、 错误状态、 保留状态以及切分状态等。 针对任意一种内存块状态, 可以 通过对应的数字进行表示。 例如, 空闲状态可以通过 “0”表示, 使用状态可以通过 “ 1 ” 表示, 错误状态可以通过 “2”表示, 保留状态可以通过 “3 ”表示等, 切分状态可以通过 “4”表示。 由于任意一种内存块状态可以通过数字表示, 因此针对任意一种内存粒度对应的多个 内存块, 该多个内存块的内存块状态可以通过数组表示。 例如, 如图 3所示, 若内存中存 在 n 个 2M 粒度的内存块, 则该 n 个 2M 粒度的内存块的内存块状态可以表示为数组 [1,4, 0,0, 4, 0,2, 4, 0,3, 0,0, 0,2, …… , 其中, 第 1位为 1表示内存块 1的内存块状态为使用状 态, 第 2位为 4表示内存块 2的内存块状态为切分状态, 。 内存块使用信息中可以包括每种内存块状态对应的多个内存块的数量。 内存范围可以通过内存的起始地址和结束地址表示。 例如, 内存 1的起始地址可以为 0x00000000, 结束地址可以为 0x40000000。 可选地, 内存范围还可以通过内存的起始地址和内存长度表示。 例如, 内存 1的起始 地址可以为 0x00000000, 内存长度可以为 1G。 当前分配地址可以用于记录分配空闲状态的内存块的起始地址。 当前分配地址的初始 值为 0, 随着内存分配或释放内存而变化。 可选地, 内存块信息可以通过数据表的形式展现。 可选地, 若内存粒度为最小内存粒度, 则该内存粒度对应的内存块信息中还可以包括 下一级内存粒度对应的下一个内存块信息的地址、 以及索引号。 索引号可以用于表示该内存块信息对应的多个内存块对应的上一级 内存粒度对应的 内存块标识。 例如, 若内存粒度为 4K, 最小内存粒度为 4K, 则 4K粒度对应的内存块信息 2可以 如表 1所示: 表 1
其 中, 地址 3可以为内存块信息 3的地址。 可选地 , 若内存粒度为非最小内存粒度, 则该内存粒度对应的内存块信息中还可以包 括第一链表地址和第二链表地址。 第一链表地址可以用于确定第一链表。第一链表中可以包括下一级内存粒度对应的至 少一个内存块信息。 在第一链表中, 每个内存块信息中的内存块状态存在空闲状态, 即每 个内存块信息所对应的多个内存块中存在空闲状态的内存块。 例如, 第一链表可以为部分 (partial) 链表。 第二链表地址可以用于确定第二链表。第二链表中可以包括下一级内存粒度对应的至 少一个内存块信息。 在第二链表中, 每个内存块信息中的内存块状态不存在空闲状态, 即 每个内存块信息所对应的多个内存块中不存在空闲状态的内存块。 例如, 第二链表可以为 全满 (full) 链表。 例如, 若内存粒度为 2M, 最小内存粒度为 4K, 则 2M粒度对应的内存块信息 1可以 如表 2所示: 表 2
下 面, 结合图 4, 对第一链表和第二链表进行说明。 图 4为本公开示例性实施例提供的第一链表和第二链表的示意图一。 请参见图 4, 例 如, 若如图 3中所示, 存在 2M粒度对应的内存块信息 1, 4K粒度对应的内存块信息 2、 内存块信息 3、 以及内存块信息 4o 若内存块信息 1 中包括第一链表地址为地址 1、 以及 第二链表地址为地址 2; 内存块信息 2中包括下一个内存块信息的地址为地址 3, 内存块
信息 3和内存块信息 4中包括的下一个内存块信息的地址为空。若内存块信息 2和内存块 信息 3中的内存块状态存在空闲状态, 则内存块信息 1中第一链表地址即地址 1可以是内 存块信息 2的地址, 内存块信息 2中下一个内存块信息的地址即地址 3可以是内存块信息 3的地址, 第一链表中可以包括内存块信息 2和内存块信息 3 ; 若内存块信息 4中的内存 块状态不存在空闲状态, 则内存块信息 1中的第二链表地址即地址 2可以是内存块信息 4 的地址, 第二链表中可以包括内存块信息 4。 例如 , 若内存操作请求 1为请求分配内存粒度为 2M的 1.5G内存, 内存操作请求 2 为请求分配内存粒度为 4K的 2.5M内存,则电子设备可以根据内存操作请求 1确定 2M粒 度对应的内存块信息 1 ;可以根据内存操作请求 2确定 4K粒度对应的内存块信息 2、 内存 块信息 3和内存块信息 4o
S203、根据至少一种内存粒度对应的内存块信息对内存操作请求进行处理, 以进行分 酉己内存或者释放内存。 由于任意一种内存粒度对应的内存块信息中包括多个内存块的内存块状态, 因此电子 设备可以读取内存块信息, 根据多个内存块的内存块状态, 对内存操作请求进行处理, 以 进行分配内存或者释放内存。 例如, 若内存操作请求 1为请求分配内存粒度为 2M的 1.5G内存; 内存操作请求 2为 请求分配内存粒度为 4K的 2.5M内存, 假设存在 2M粒度对应的内存块信息 1、 以及 4K 粒度对应的内存块信息 2、 内存块信息 3和内存块信息 4, 则电子设备可以在内存块信息 1 中确定内存块状态为空闲状态的 768个 2M内存块, 并分配该 768个 2M内存块, 以对 内存操作请求 1进行处理; 可以在内存块信息 2、 内存块信息 3和内存块信息 4中确定内 存块状态为空闲状态的 640个 4K内存块, 则电子设备可以分配该 640个 4K内存块, 以 对内存操作请求 2进行处理。 在本公开实施例 中, 电子设备可以获取对内存的内存操作请求, 并确定至少一种内存 粒度对应的内存块信息,进而可以根据至少一种内存粒度对应的内存块信息对内存操作请 求进行处理, 以进行分配内存或者释放内存。 由于内存中可以包括至少两种内存粒度对应 的多个内存块, 且每种内存粒度有对应的内存块信息。 在分配内存或释放内存时, 可以按 照实际需求根据内存块信息对内存中任意一种内存粒度对应的多个内存块进行操作,相比 现有技术中内存只包括一种内存粒度对应的多个内存块,且只根据这一种内存粒度对内存 进行操作, 提高了对内存进行操作的灵活性。 下 面,在图 2所示实施例的基础上,结合图 5,对请求分配内存的过程进行详细说明; 结合图 7对请求释放内存的过程进行详细说明。 图 5 为本公开示例性实施例提供的另一种内存操作方法的流程示意图。 请参见图 5, 所述方法可以包括:
S501、 获取对内存的内存操作请求。 内存操作请求可以用于请求分配内存。 内存操作请求中包括申请内存粒度、 以及申请
内存粒度对应的内存申请量。 例如, 申请内存粒度可以为如下任意一种: 1G粒度、 2M粒 度、 16K粒度、 4K粒度等。 电子设备可以响应于用户对内存的操作, 获取内存操作请求。 例如, 电子设备可以获 取内存操作请求 1为请求分配内存粒度为 2M的 1.5G内存、 内存操作请求 2为请求分配 内存粒度为 4K的 2.5M内存, 则内存操作请求 1中的申请内存粒度为 2M粒度, 内存申 请量为 1.5G; 内存操作请求 2中的申请内存粒度为 4K粒度, 内存申请量为 2.5Mo
5502、 确定至少一种内存粒度对应的内存块信息。 需要说明的是, 步骤 S502的执行过程可以参见步骤 S202, 此处不再进行赘述。
5503、 确定申请内存粒度对应的内存块信息。 可选地, 确定申请内存粒度对应的内存块信息, 可以包括如下 2种情况: 情况 1: 申请内存粒度为最大内存粒度。 例如 , 若内存中包括 2种内存粒度, 分别为 2M和 4K, 最大内存粒度为 2M, 存在 2M 粒度对应的内存块信息 1 (如表 2所示) , 若申请内存粒度为 2M, 则电子设备可以确 定申请内存粒度 2M对应的内存块信息为内存块信息 1。 情况 2: 申请内存粒度小于最大内存粒度。 在该种 情况下, 可以通过如下方式, 确定申请内存粒度对应的内存块信息: 确定申请 内存粒度对应的上一级内存粒度;在上一级内存粒度对应的内存块信息中确定第一链表地 址; 根据第一链表地址确定申请内存粒度对应的内存块信息。 例如 , 若内存中包括 2种内存粒度, 分别为 2M和 4K, 最大内存粒度为 2M, 申请内 存粒度为 4K, 则对于申请内存粒度 4K, 上一级内存粒度为 2M粒度。 在上一级 内存粒度对应的多个内存块中,存在部分内存块被划分为多个申请内存粒度 对应的内存块。 例如, 如图 3所示, 在 2M粒度对应的多个内存块中, 内存块 3、 内存块 5和 内存块 8被划分为 4K粒度对应的多个内存块。 例如, 若如图 4中所示存在 2M粒度对应的内存块信息 1、 以及 4K粒度对应的内存 块信息 2、 内存块信息 3和内存块信息 4, 若申请内存粒度为 4K, 则电子设备可以在内存 块信息 1,确定第一链表地址即地址 1, 则可以根据地址 1确定申请内存粒度 4K对应的内 存块信息 2, 进而可以根据内存块信息 2中的地址 3确定内存块信息 3。 需要说明的是, 由于内存操作请求是请求分配内存, 且第一链表中的每个内存块信息 所对应的多个内存块存在空闲状态的内存块, 因此只需根据第一链表地址确定第一链表中 的多个内存块信息即可; 由于第二链表中的每个内存块信息所对应的多个内存块不存在空 闲状态的内存块, 因此无需根据第二链表地址确定第二链表中的多个内存块信息。
5504、 判断申请内存粒度是否为最大内存粒度。 若是 , 则执行 S505 ; 若否, 则执行 S506o 例如 , 若内存中包括 2种内存粒度, 分别为 2M和 4K, 若申请内存粒度为 2M, 则可 以判断申请内存粒度 2M为最大内存粒度 2M,则可以执行 S505 ;若申请内存粒度为 4K,
则可以判断申请内存粒度 4K小于最大内存粒度 2M, 则可以执行 S506o
S505、在申请内存粒度对应的内存总量大于或等于内存申请量时, 在申请内存粒度对 应的至少一个内存块中确定目标内存块。 由于内存中包括至少一种内存粒度, 至少一种内存粒度包括申请内存粒度, 因此可以 在内存中确定申请内存粒度对应的内存总量,在申请内存粒度对应的内存总量大于或等于 内存申请量时, 在申请内存粒度对应的至少一个内存块中确定目标内存块。 目标内存块的内存粒度可以为申请内存粒度, 目标内存块的内存块状态可以为空闲状 态。 例如, 申请内存粒度为 2M, 若内存块 3、 内存块 4的内存粒度为 2M, 且内存块状态 为空闲状态, 则可以确定内存块 3和内存块 4为目标内存块。 例如 , 若申请内存粒度为最大内存粒度 2M, 内存申请量为 1.5G, 若 2M对应的内存 总量为 5G, 则由于 2M粒度对应的内存总量 5G大于内存申请量 1.5G, 则可以在 2M粒度 对应的至少一个内存块中确定多个目标内存块。 可选地 , 在申请内存粒度对应的至少一个内存块中确定目标内存块, 可以包括如下 2 种情况: 情况 11 : 若内存操作请求指示按照对齐模式分配内存。 在对齐模 式下, 进行分配内存时当前分配地址和内存长度需对齐。 在该种情况下 , 电子设备可以在申请内存粒度对应的内存块信息中确定当前分配地址, 并确定当前分配地址是否对齐。 若是 , 则可以从当前分配地址开始, 遍历每个内存块的内存块状态, 进而根据内存块 状态,在申请内存粒度对应的至少一个内存块中确定申请内存粒度对应的多个目标内存块; 若否, 则可以确定对齐分配地址, 从对齐分配地址开始, 遍历每个内存块的内存块状态, 进而根据内存块状态,在申请内存粒度对应的至少一个内存块中确定申请内存粒度对应的 多个目标内存块。 可选地, 该至少一个目标内存块可以对应至少一个内存长度, 至少一个 内存长度是对齐的。 若否 , 即当前分配地址不对齐, 且若不存在对齐分配地址和 /或对齐的内存长度, 则 可以无法满足内存操作请求, 则可以确定分配结果为分配失败。 在一可选实施例 中, 可以通过如下方式, 在申请内存粒度对应的至少一个内存块中确 定目标内存块: 根据内存申请量和申请内存粒度, 确定目标内存块的第一数量; 判断至少 一个内存块中, 是否存在第一数量个连续内存块; 若是, 则将第一数量个连续内存块确定 为目标内存块; 若否, 则在内存操作请求指示允许分配内存不连续时, 将至少一个内存块 中、 第一数量个不连续的内存块确定为目标内存块, 不连续的内存块的内存之和大于或等 于内存申请量。 可选地 , 第一数量可以通过 M表示, M可以为正整数。 具体地 , 在确定申请内存粒度对应的 M个目标内存块时, 可以根据内存申请量预先 确定 M个目标内存块中包括第一内存长度总共对应的 K个连续的第一目标内存块, 和/
或第二内存长度对应的 M-K个第二目标内存块。 电子设备可以确定第一内存长度对应的 K 个连续的第一目标内存块,再确定第二内存长度对应的 M-K个第二目标内存块。该 M-K 个第二目标内存块与 K个连续的第一 目标内存块可以连续, 也可以不连续。其中, K为大 于或等于 0的整数。 例如 , 若申请内存粒度为 2M, 存在 2M粒度对应的内存块信息 1如表 2所示, 若内 存操作请求 1为请求分配内存粒度为 2M的 1.5G内存, 且指示按照对齐模式分配内存且 指示分配内存连续时, 则电子设备可以根据内存申请量 1.5G预先确定需 768个目标内存 块,该 768个目标内存块中包括第一内存长度 1G总共对应的 512连续的第一目标内存块、 以及第二内存长度 2M对应的 256个第二目标内存块, 则电子设备可以在内存块信息 1中 确定当前分配地址为地址 A, 并确定地址 A是否为 1G对齐。 若对齐, 则可以从地址 A开 始遍历 n个 2M内存块的内存块状态; 若未对齐, 则可以确定对齐分配地址 1 , 假设对齐 分配地址 1为地址 A1 , 则可以从地址 A1开始遍历 n个 2M内存块的内存块状态。 电子设 备可以根据该 n个 2M内存块的内存块状态, 优先确定 512个空闲的、 连续的第一目标内 存块, 并将该 512个第一目标内存块确定为 512个目标内存块, 该 512个目标内存块对应 的内存长度是 1G对齐的。 接着 , 由于另外 256个目标内存块对应的内存长度不足 1G, 且由于内存操作请求指 示分配内存连续, 因此电子设备可以根据确定 521 个目标内存块后所处的当前分配地址 A2 继续根据该 n个 2M内存块的内存块状态, 确定 256个空闲的、 连续的第二目标内存 块。 电子设备可以总共确定 768个连续的 2M目标内存块。 若 内存操作请求指示允许分配内存不连续,则可以电子设备可以同样优先确定第一内 存长度 1G总共对应的 512个连续的第一目标内存块, 再确定 256个 2M 目标内存块。 在 确定该 256个目标内存块中的任意一个目标内存块时, 电子设备可以确定当前分配地址是 否为 2M对齐。若对齐,则可以从当前分配地址开始,遍历 n个 2M内存块的内存块状态; 若未对齐, 则可以确定对齐分配地址, 从对齐分配地址开始遍历 n个 2M内存块的内存块 状态,以确定 256个空闲的内存块,并将该 256个空闲的内存块确定为 256个目标内存块。 该 256个目标内存块可以不连续。 其中, 每个内存块的内存长度是 2M对齐的。 则电子设 备总共可以确定 768个目标内存块。在该 768个目标内存块中, 其中 512个目标内存块可 以是连续的, 256个目标内存块可以是不连续的。 情况 12: 若内存操作请求指示无需按照对齐模式。 在该种 情况下, 电子设备可以在申请内存粒度对应的内存块信息中确定当前分配地址, 并从当前分配地址开始, 遍历每个内存块的内存块状态, 进而根据内存块状态确定申请内 存粒度对应的至少一个目标内存块。 同样的, 可以通过如下方式, 在申请内存粒度对应的至少一个内存块中确定目标内存 块: 根据内存申请量和申请内存粒度, 确定目标内存块的第一数量 M; 判断至少一个内存 块中,是否存在 M个连续内存块;若是,则将 M个连续内存块确定为目标内存块;若否,
则在内存操作请求指示允许分配内存不连续时, 将至少一个内存块中、 M个不连续的内存 块确定为目标内存块, 不连续的内存块的内存之和大于或等于内存申请量。 例如 , 若申请内存粒度为 2M, 存在 2M粒度对应的内存块信息 1如表 2所示, 若内 存操作请求 1为请求分配内存粒度为 2M的 1.5G内存, 且指示无需按照对齐模式分配内 存, 若内存操作请求 1指示分配内存需连续时, 则电子设备可以根据内存块信息 1 , 确定 地址 A, 并从地址 A开始遍历 n个 2M内存块的内存块状态, 确定 768个空闲的连续内存 块, 并将该 768个空闲的连续内存块确定为 768个目标内存块; 若内存操作请求 1指示允 许分配内存不连续, 则电子设备可以根据内存块信息 1, 确定地址 A, 并从地址 A开始遍 历 n个 2M内存块的内存块状态, 确定 768个空闲的、 不连续的内存块, 并将该 768个空 闲的、 不连续的内存块确定为 768个目标内存块。 可选地 , 若至少一个目标内存块中不存在 M个连续内存块, 且若内存操作请求指示 不允许分配内存不连续时, 则可以确定分配失败。 需要说明的是, 在至少一个目标内存块中, 确定 M个不连续的内存块时, 可以从当 前分配地址开始, 向后遍历多个连续的内存块; 接着移动到当前分配地址至下一个合适分 配地址, 继续从当前分配地址, 向后遍历多个连续的内存块; >; 直至确定出 M个内 存块。
S506、在申请内存粒度对应的内存总量大于或等于内存申请量时, 在申请内存粒度对 应的至少一个内存块中确定目标内存块;在申请内存粒度对应的内存总量小于内存申请量 时, 更新至少一个内存块, 并在更新后的内存块中确定目标内存块。 可选地 , 在申请内存粒度小于最大内存粒度时, 在申请内存粒度对应的至少一个内存 块中确定目标内存块可以包括如下 2种情况: 情况 21 : 申请内存粒度对应的内存总量大于或等于内存申请量。 在该种情况下 , 可以在申请内存粒度对应的至少一个内存块中确定目标内存块。 可选地 , 可以优先在申请内存粒度对应的至少一个内存块中确定连续内存块。 当连续 内存块不够时, 可以将不连续的内存块确定为目标内存块。 例如 ,若申请内存粒度为 4K,内存申请量为 2.5M,若 4K粒度对应的内存总量为 10M, 则由于 4K粒度对应的内存总量 10M大于内存申请量 2.5M,则可以在 4K粒度对应的至少 一个内存块中确定多个目标内存块。 情况 22 : 申请内存粒度对应的内存总量小于内存申请量。 在该种情况下 , 可以更新至少一个内存块, 并在更新后的至少一个内存块中确定目标 内存块。 在一 可选实施例中, 可以通过如下方式, 更新至少一个内存块: 根据申请内存粒度, 确定第一内存块; 对第一内存块进行切分处理, 得到多个第二内存块; 确定更新后的至少 一个内存块包括至少一个内存块和多个第二内存块。 第一内存块的内存块状态为空闲状态。 第一内存块的内存粒度大于申请内存粒度。 例
如, 第一内存块的内存粒度可以为申请内存粒度对应的上一级内存粒度。 第二内存块的内存粒度为申请内存粒度。 例如 , 若申请内存粒度为 4K, 若申请内存粒度 4K对应的内存总量为 2M, 小于内存 申请总量 2.5M, 则可以根据申请内存粒度 4K, 确定第一内存块, 第一内存块的内存粒度 可以为 2M, 且第一内存块为空闲状态, 则可以对 2M粒度的第一内存块进行切分处理, 可以得到 512个 4K粒度的第二内存块, 则可以确定更新后的至少一个内存块包括内存总 量 2M对应的多个内存块、 以及 512个 4K粒度的第二内存块, 则可以在更新后的至少一 个内存块即 1024个内存块中确定 640个目标内存块。 可选地 , 在对第一内存块进行切分处理之后, 可以确定第一内存块对应的第一内存粒 度, 确定第一内存粒度对应的第一内存块信息, 并根据第一内存块更新第一内存块信息; 可以确定申请内存粒度对应的第二内存块信息,并根据多个第二内存块更新第二内存块信 息。 例如 , 若第一内存块为 2M粒度的内存块 6, 则可以确定 2M粒度对应的第一内存块 信息即内存块信息 1, 由于第一内存块被切分为多个 4K粒度的第二内存块, 则可以在内 存块信息 1中将第一内存块即内存块 6对应的内存块状态更新为切分状态; 由于新增了第 一内存块对应的多个第二内存块, 因此可以根据该多个第二内存块, 新增该多个第二内存 块对应的内存块信息 5, 内存块信息 5中的索引号可以为 6、 内存块状态中可以均为空闲 状态。 需要说明的是, 无论针对上述情况 21 , 在申请内存粒度对应的至少一个内存块中确 定目标内存块, 还是针对上述情况 22, 在更新后的至少一个内存块中确定目标内存块, 确定申请内存粒度 4K对应的目标内存块的过程与步骤 505中确定 2M粒度对应的目标内 存块的过程基本相同, 具体如下: 若 内存操作请求指示按照对齐模式分配内存, 则电子设备可以在申请内存粒度对应的 内存块信息中确定当前分配地址, 并确定当前分配地址是否对齐。 若是, 则可以从当前分 配地址开始, 遍历每个内存块的内存块状态, 进而根据内存块状态, 在申请内存粒度对应 的至少一个内存块中确定申请内存粒度对应的多个目标内存块; 若否, 则可以确定对齐分 配地址, 从对齐分配地址开始, 遍历每个内存块的内存块状态, 进而根据内存块状态, 在 申请内存粒度对应的至少一个内存块中确定申请内存粒度对应的多个 目标内存块。 若否, 即当前分配地址不对齐, 且若不存在对齐分配地址和 /或对齐的内存长度, 则可以无法满 足内存操作请求, 则可以确定分配结果为分配失败。 在一可选实施例 中, 可以通过如下方式, 在申请内存粒度对应的至少一个内存块中确 定目标内存块: 根据内存申请量和申请内存粒度, 确定目标内存块的第一数量 M; 判断至 少一个内存块中, 是否存在 M个连续内存块; 若是, 则将 M个连续内存块确定为目标内 存块; 若否, 则在内存操作请求指示允许分配内存不连续时, 将至少一个内存块中、 M个 不连续的内存块确定为目标内存块, 不连续的内存块的内存之和大于或等于内存申请量。
具体地 , 在确定申请内存粒度对应的 M个目标内存块时, 可以根据内存申请量预先 确定 M个目标内存块中包括第一内存长度总共对应的 K个连续的第一目标内存块, 和/ 或第二内存长度对应的 M-K个第二目标内存块。 电子设备可以确定第一内存长度对应的 K 个连续的第一目标内存块,再确定第二内存长度对应的 M-K个第二目标内存块。该 M-K 个第二目标内存块与 K个连续的第一目标内存块可以连续, 也可以不连续。 例如, 若申请内存粒度为 4K, 存在 4K粒度对应的内存块信息 2、 内存块信息 3和内 存块信息 5, 若内存操作请求 2可以为请求分配内存粒度为 4K的 2.5M内存, 且指示按照 对齐模式分配内存且指示允许分配内存不连续时, 则电子设备可以根据内存申请量 2.5M 预先确定需 640个目标内存块, 该 640个目标内存块中包括第一内存长度 2M总共对应的 512个目标内存块、 以及第二内存长度 4K对应的 128个目标内存块, 则电子设备可以在 内存块信息 2中确定当前分配地址为地址 C, 并确定地址 C是否为 2M对齐。 若对齐, 则 可以从地址 C开始, 遍历内存块信息 2中的多个内存块状态; 若未对齐, 则可以确定对齐 分配地址 C1 , 从地址 C1开始, 遍历内存块信息 2中的多个内存块状态。 电子设备可以根 据内存块信息 2中的多个内存块状态, 优先确定 512个空闲的、 连续的第一目标内存块, 并将该 512个第一目标内存块确定为 512个目标内存块,该 512个目标内存块对应的内存 长度是 2M对齐的。 若内存块信息 2中不存在, 则可以按照同样的方式根据内存块信息 3 或内存块信息 5优先确定 512个目标内存块。 假设 电子设备可以根据内存块信息 5确定 512个目标内存块, 则接着, 由于另外 128 个目标内存块对应的内存长度不足 2M, 因此电子设备可以根据内存块信息 2和/或内存块 信息 3确定 128个 4K目标内存块。 在确定该 128个目标内存块中的任意一个目标内存块时, 电子设备可以确定当前分配 地址是否为 4K对齐。 若对齐, 则可以从当前分配地址开始, 遍历内存块信息 2的内存块 状态; 若未对齐, 则可以确定对齐分配地址, 从对齐分配地址开始遍历内存块信息 2的内 存块状, 以确定多个空闲的内存块, 并将该多个空闲的内存块确定为多个目标内存块。 假 设电子设备可以根据内存块信息 2确定 60个目标内存块; 同样的, 电子设备可以按照同 样的方式, 根据内存块信息 3确定剩余的 68个目标内存块。 其中, 每个内存块的内存长 度是 4K对齐的。 则电子设备总共可以确定 512+128=640个目标内存块。 若 内存操作请求指示无需按照对齐模式,则电子设备可以在申请内存粒度对应的内存 块信息中确定当前分配地址, 并从当前分配地址开始, 遍历每个内存块的内存块状态, 进 而根据内存块状态确定申请内存粒度对应的至少一个目标内存块。 例如, 若申请内存粒度为 4K, 存在 4K粒度对应的内存块信息 2、 内存块信息 3和内 存块信息 5, 若内存操作请求 2可以为请求分配内存粒度为 4K的 2.5M内存, 且指示无需 按照对齐模式分配内存, 若内存操作请求 2指示允许分配内存不连续, 则电子设备可以根 据内存块信息 2、内存块信息 3和内存块信息 5总共确定 640个空闲的、不连续的内存块, 并该 640个空闲的、 不连续的内存块确定为 640个目标内存块。
5507、 分配申请内存粒度对应的目标内存块。 例如 , 若内存操作请求 1为请求分配内存粒度为 2M的 1.5G内存, 若电子设备可以 确定 768个连续的 2M目标内存块,则电子设备可以分配该 768个连续的 2M目标内存块; 若电子设备确定了 768个不连续的 2M目标内存块, 包括 512个连续的 2M目标内存块和 256个连续的 2M 目标内存块, 则可以依次先分配 512个连续的 2M 目标内存块, 再分配 256个连续的 2M 目标内存块。 例如, 若内存操作请求 2为请求分配内存粒度为 4K的 2.5M内存, 若电子设备确定 了 640个不连续的 4K目标内存块, 包括 512个连续的 4K目标内存块、 以及 128个不连 续的 4K 目标内存块, 则电子设备可以依次先分配 512个连续的 4K 目标内存块, 再分配 128个不连续的 4K目标内存块。 需要说明的是, 若内存操作请求指示按照对齐模式分配内存, 则针对每次分配, 可以 对返回的分配结束地址进行校验, 判断分配结束地址是否满足对齐要求。在满足对齐要求 时, 再对目标内存块进行分配。
5508、 根据目标内存块, 更新每种申请内存粒度对应的内存块信息。 可选地, 针对申请内存粒度, 由于电子设备根据该申请内存粒度对应的内存块信息分 配了多个目标内存块,因此可以根据目标内存块,更新该申请内存粒度对应的内存块信息 O 具体的, 可以更新内存块信息中的内存块状态、 内存使用信息、 以及当前分配地址。 例如, 若申请内存粒度为 2M, 若电子设备根据内存块信息 1 , 分配了 768个 2M粒 度的内存块, 则可以根据该 768个内存块, 在内存块信息 1中, 将对应的内存块状态更新 为使用状态, 并更新内存使用信息、 以及当前分配地址为地址 B。 例如, 若申请内存粒度为 4K, 若电子设备根据内存块信息 2分配了 60个 4K粒度的 目标内存块, 则可以根据该 60个目标内存块, 在内存块信息 2中, 将对应的内存块状态 更新为使用状态, 并更新内存使用信息、 以及当前分配地址为地址 D ; 同样的, 若电子设 备根据内存块信息 3分配了 68个 4K粒度的目标内存块, 则可以在内存块信息 3中,将该 68 个目标内存块对应的内存块状态更新为使用状态, 并更新内存使用信息、 以及当前分 配地址为地址 F; 若根据内存块信息 5分配了 512个 4K粒度的目标内存块, 则可以在内 存块信息 5中, 将该 512个目标内存块对应的内存块状态更新为使用状态, 并更新内存使 用信息、 以及当前分配地址为地址 H。 可选地, 对 4K粒度的目标内存块分配完成之后, 针对 4K粒度对应的任意一个内存 块信息, 可以判断该内存块信息中的内存块状态是否存在空闲状态。 若否, 则可以将该内 存块信息移动至第二链表; 若存在新增的内存块信息, 且内存块信息中存在空闲状态, 则 可以将该内存块信息加入第一链表。 下面, 结合图 6, 对内存块信息的移动进行说明。 图 6为本公开示例性实施例提供的第一链表和第二链表的示意图二。 请参见图 6, 若 电子设备根据内存块信息 2、 内存块信息 3、 以及内存块信息 5总共分配 640个 4K粒度的
目标内存块之后, 若第一链表和第二链表如图 4所示, 则可以确定内存块信息 2中是否还 存在空闲状态, 若不存在, 则可以将内存块信息 2移动至第二链表, 并在内存块信息 4中 将下一个内存块信息的地址更新为地址 1; 若内存块信息 3中存在空闲状态, 则无需移动 内存块信息 3至第二链表, 内存块信息 3仍在第一链表中。 由于已将内存块信息 2移动至 第二链表, 则可以在内存块信息 1中将第一链表地址更新为地址 3 ; 若内存块信息 5中不 存在空闲状态, 因此可以将内存块信息 5加入第二链表, 并将内存块信息 2中下一个内存 块信息的地址更新为地址 5, 得到新的第一链表和第二链表, 如图 6中所示。 需要说明的是, 若内存操作请求中包括指定内存节点, 则可以在指定内存节点中执行 步骤 S502至步骤 S508 ; 若内存操作请求中不包括指定内存节点, 若电子设备中包括多个 内存节点, 则可以判断第 1个内存节点的剩余容量是否大于或等于内存申请总量(各申请 内存粒度对应的内存申请量之和) , 若否, 则可以根据不同内存节点之间的距离, 优先选 择靠近第 1内存节点, 且剩余容量大于或等于内存申请总量的内存节点, 以在该内存节点 中执行步骤 S502至步骤 S508o 在本公开实施例 中, 电子设备可以获取对内存的内存操作请求, 并确定至少一种内存 粒度对应的内存块信息。 电子设备可以确定申请内存粒度对应的内存块信息, 并判断申请 内存粒度是否为最大内存粒度。 若是, 则可以在申请内存粒度对应的内存总量大于或等于 内存申请量时, 在申请内存粒度对应的至少一个内存块中确定目标内存块; 若否, 则可以 在申请内存粒度对应的内存总量大于或等于内存申请量时,在申请内存粒度对应的至少一 个内存块中确定目标内存块; 在申请内存粒度对应的内存总量小于内存申请量时, 更新至 少一个内存块, 并在更新后的内存块中确定目标内存块。 电子设备可以分配申请内存粒度 对应的目标内存块, 并根据目标内存块, 更新每种申请内存粒度对应的内存块信息。 由于 内存中可以包括至少两种内存粒度对应的多个内存块,且每种内存粒度有对应的内存块信 息。在分配内存时, 可以按照实际需求根据内存块信息对内存中任意一种内存粒度对应的 多个内存块进行操作, 相比现有技术中内存只包括一种内存粒度对应的多个内存块, 且只 根据这一种内存粒度对内存进行操作, 提高了对内存进行操作的灵活性 图 7 为本公开示例性实施例提供的又一种内存操作方法的流程示意图。 请参见图 7, 所述方法可以包括:
S701、 获取对内存的内存操作请求。 内存操作请求用于请求释放内存。 内存操作请求中可以包括释放内存粒度、 以及释放 内存粒度对应的内存地址。 例如, 释放内存粒度可以为如下任意一种: 1G粒度、 2M粒度、 16K粒度、 4K粒度 等。 可选地 , 内存地址可以包括至少一组释放起始地址和释放结束地址。 电子设备可以响应于用户对内存的操作, 获取内存操作请求。 例如, 电子设备可以获 取内存操作请求 3为请求释放内存地址为地址 A至地址 B的 2M粒度的内存块、 内存操
作请求 4为请求释放内存地址为地址 C至地址 D、地址 E至地址 F、 以及地址 G至地址 H 的 4K粒度的内存块。
5702、 确定至少一种内存粒度对应的内存块信息。 需要说明的是, 步骤 S702的执行过程可以参见步骤 S202, 此处不再进行赘述。
5703、根据释放内存粒度对应的内存块信息和内存地址, 确定释放内存粒度对应的至 少一个待释放内存块。 针对释放 内存粒度, 电子设备可以确定释放内存粒度对应的内存块信息, 并根据内存 块信息和内存地址, 确定该释放内存粒度对应的至少一个待释放内存块。 例如 , 若释放内存粒度为 2M, 若存在 2M粒度对应的内存块信息 1 , 若内存地址为 地址 A至地址 B, 则电子设备可以确定内存块信息 1 , 并根据内存块信息 1和地址 A至地 址 B, 确定至少一个待释放内存块。 假设可以确定 768个 2M粒度的待释放内存块。 例如, 若释放内存粒度为 4K, 若存在 4K粒度对应的内存块信息 2、 内存块信息 3、 内存块信息 4和内存块信息 5, 若内存地址为地址 C至地址 D、 地址 E至地址 F、 以及地 址 G至地址 H, 则电子设备可以根据该 4个内存块信息和该 3个内存地址,确定至少一个 待释放内存块。 假设可以根据地址 C-地址 D在内存块信息 2中确定 60个 4K粒度的待释 放内存块; 可以根据地址 E至地址 F在内存块信息 3中确定 68个 4K粒度的待释放内存 块;可以根据地址 G至地址 H在内存块信息 5中确定 512个 4K粒度的待释放内存块, 总 共确定了 640个 4K粒度的待释放内存块。
5704、 对至少一个待释放内存块进行清零处理, 以释放至少一个内存块。 例如, 若电子设备确定了 768个 2M粒度的待释放内存块, 则可以对该 768个 2M粒 度的待释放内存块进行清零处理, 以释放该 768个 2M粒度的内存块。 例如, 若电子设备确定了 640个 4K粒度的待释放内存块, 则可以对该 640个 4K粒 度的待释放内存块进行清零处理, 以释放该 640个 4K粒度的内存块。
5705、 根据至少一个内存块, 更新释放内存粒度对应的内存块信息。 可选地 , 针对任意一种释放内存粒度, 释放至少一个内存块之后, 由于至少一个内存 块的内存块状态发生了变更, 因此可以根据至少一个内存块, 更新该释放内存粒度对应的 内存块信息。 具体地, 可以更新内存块信息中的内存块状态、 内存使用信息、 以及当前分 配地址。 例如 , 若释放内存粒度为 2M, 若电子设备根据内存块信息 1和地址 A至地址 B, 释 放了 768个 2M粒度的内存块, 则可以根据该 768个内存块, 在内存块信息 1中, 将对应 的内存块状态更新为空闲状态, 并更新内存使用信息、 以及当前分配地址为地址 A。 例如 , 若释放内存粒度为 4K, 若电子设备根据内存块信息 2和地址 C-地址 D, 释放 该 60个 4K粒度的内存块, 则可以根据该 60个内存块, 在内存块信息 2中将对应的内存 块状态更新为空闲状态, 并更新内存使用信息、 以及当前分配地址为地址 C ; 同样的, 若 电子设备根据内存块信息 3和地址 E至地址 F释放了 68个 4K粒度的待释放内存块, 则
可以在内存块信息 3中将对应的内存块状态更新为空闲状态, 并更新内存使用信息、 以及 当前分配地址为地址 E ;若根据内存块信息 5和地址 G至地址 H释放了 512个 4K粒度的 待释放内存块, 则可以在内存块信息 5中将对应的内存块状态更新为空闲状态, 并更新内 存使用信息、 以及当前分配地址为地址 G。
5706、 若释放内存粒度小于最大内存粒度, 且若释放内存粒度对应的第三内存块信息 中内存块状态均为空闲状态, 则根据第三内存块信息对多个内存块进行合并处理, 得到第 三内存块。 第三内存块的内存粒度为最大内存粒度。 例如 , 若释放内存粒度为 4K, 小于最大内存粒度 2M, 若电子设备根据内存块信息 5 和地址 G至地址 H, 释放了 512个 4K粒度的内存块, 若释放内存块之后, 内存块信息 5 中的内存块状态均为空闲状态, 则内存块信息 5为第三内存块信息, 则可以根据内存块信 息 5, 对内存块信息 5对应的多个 4K内存块进行合并处理, 得到第三内存块。 假设第三 内存块为 2M粒度的内存块 6o
5707、根据第三内存块更新最大内存粒度对应的内存块信息,并删除第三内存块信息。 由于第三内存块的内存块状态发生了变更,且第三内存块的内存粒度为最大内存粒度, 则可以根据第三内存块更新最大内存粒度对应的内存块信息。 可选地, 由于对释放内存粒度对应的多个内存块进行合并处理之后, 该多个内存块不 存在了, 因此可以删除该多个内存块对应的内存块信息。 例如 , 若第三内存块为 2M粒度的内存块 6, 则可以根据第三内存块更新 2M粒度对 应的内存块信息 1 , 在内存块信息 1中, 将第三内存块即内存块 6对应的内存块状态更新 为空闲状态, 并更新内存块信息 1 中的内存使用信息。 由于用于合并第三内存块的多个 4K 内存块不存在了, 因此可以删除内存块信息 5。 可选地, 若释放内存粒度小于最大内存粒度, 且在释放内存块之前, 释放内存粒度对 应的任一内存块信息处于第二链表中, 则在释放内存块之后, 该内存块信息中存在内存块 状态为空闲状态, 则可以将该内存块信息移动至第一链表。 例如 , 针对释放内存粒度 4K, 若内存块信息 2和内存块信息 5处于第二链表中, 如 图 6所示, 由于根据内存块信息 2释放 60个 4K粒度的内存块之后, 内存块信息 2的内存 块状态中存在 60个空闲状态, 则可以将内存块信息 2移动至第一链表。 若第一链表和第 二链表如图 6所示, 则删除内存块信息 5、 将内存块信息 2移动至第一链表后, 可以得到 如图 4所示的第一链表和第二链表。 在 本公开实施例中, 电子设备可以获取对内存的内存操作请求, 并确定至少一种内存 粒度对应的内存块信息。 电子设备可以根据释放内存粒度对应的内存块信息和内存地址, 确定释放内存粒度对应的至少一个待释放内存块,并对至少一个待释放内存块进行清零处 理, 以释放至少一个内存块。 电子设备可以根据至少一个内存块, 更新释放内存粒度对应 的内存块信息。 若释放内存粒度小于最大内存粒度, 且若释放内存粒度对应的第三内存块
信息中内存块状态均为空闲状态,则电子设备可以根据第三内存块信息对多个内存块进行 合并处理, 得到第三内存块, 并根据第三内存块更新最大内存粒度对应的内存块信息, 并 删除第三内存块信息。 由于内存中可以包括至少两种内存粒度对应的多个内存块, 且每种 内存粒度有对应的内存块信息。在释放内存时, 可以按照实际需求根据内存块信息对内存 中任意一种内存粒度对应的多个内存块进行操作,相比现有技术中内存只包括一种内存粒 度对应的多个内存块, 且只根据这一种内存粒度对内存进行操作, 提高了对内存进行操作 的灵活性。 图 8为本公开实施例提供一种内存操作装置的结构示意图。 请参见图 8, 所述内存操 作装置 10可以包括: 获取模块 11、 确定模块 12和处理模块 13 , 其中, 所述获取模块 11 用于, 获取对内存的内存操作请求, 所述内存操作请求用于请求分 酉己内存或者请求释放内存, 所述内存中包括多个内存块, 所述多个内存块对应至少一种内 存粒度; 所述确定模块 12用于, 确定至少一种内存粒度对应的内存块信息, 所述内存块信息 中包括内存粒度值、 内存块状态、 以及内存块使用信息; 所述处理模块 13 用于, 根据至少一种内存粒度对应的内存块信息对所述内存操作请 求进行处理, 以进行分配内存或者释放内存。 本公开实施例提供的 内存操作装置可以执行上述方法实施例 所示的技术方案, 其 实现原理 以及有益效果类似, 此处不再进行赘述。 在一种可 能的实施方式中, 所述内存操作请求用于请求分配内存, 所述内存操作请求 中包括申请内存粒度、 以及所述申请内存粒度对应的内存申请量; 所述处理模块 13具体 用于: 确定所述 申请内存粒度对应的内存块信息; 根据所述 申请内存粒度对应的内存块信息和内存申请量,确定所述申请内存粒度对应 的目标内存块, 所述目标内存块的内存粒度为所述申请内存粒度, 所述目标内存块的内存 块状态为空闲状态; 分配所述 申请内存粒度对应的目标内存块。 在一种 可能的实施方式中, 所述申请内存粒度小于最大内存粒度; 所述处理模块 13 具体用于: 确定所述 申请内存粒度对应的上一级内存粒度,所述上一级内存粒度对应的内存块被 划分为多个所述申请内存粒度对应的内存块; 在所述上一级 内存粒度对应的内存块信息中确定第一链表地址; 根据所述 第一链表地址确定所述申请内存粒度对应的内存块信息。 在一种可 能的实施方式中, 所述处理模块 13具体用于: 判断所述 申请内存粒度是否为最大内存粒度; 若是 , 则在所述申请内存粒度对应的内存总量大于或等于所述内存申请量时, 在所述
申请内存粒度对应的至少一个内存块中确定所述目标内存块; 若否, 则在所述申请内存粒度对应的内存总量大于或等于所述内存申请量时, 在所述 申请内存粒度对应的至少一个内存块中确定所述目标内存块;在所述申请内存粒度对应的 内存总量小于所述内存申请量时, 更新所述至少一个内存块, 并在更新后的至少一个内存 块中确定所述目标内存块。 在一种可能的实施方式 中, 所述处理模块 13具体用于: 根据所述 申请内存粒度, 确定第一内存块, 所述第一内存块的内存粒度大于所述申请 内存粒度, 所述第一内存块的内存块状态为空闲状态; 对所述 第一内存块进行切分处理, 得到多个第二内存块, 所述第二内存块的内存粒度 为所述申请内存粒度; 确定更新后的至少一个 内存块包括所述至少一个内存块和所述多个第二内存块。 本公开实施例提供的 内存操作装置可以执行上述方法实施例所 示的技术方案, 其 实现原理 以及有益效果类似, 此处不再进行赘述。 图 9为本公开实施例提供的 另一种内存操作装置的结构示意图。 请参见图 9, 在图 8所示实施例的基础上, 所述内存操作装置 10还可以包括: 更新模块 14, 所述更新模块 14用于, 确定所述第一内存块对应的第一内存粒度, 确定所述第一内 存粒度对应的第一内存块信息, 并根据所述第一内存块更新所述第一内存块信息; 所述更新模块 14用于, 确定所述申请内存粒度对应的第二内存块信息, 并根据所述 多个第二内存块更新所述第二内存块信息。 本公开实施例提供的 内存操作装置可以执行上述方法实施例所示的技术方案,其实现 原理以及有益效果类似, 此处不再进行赘述。 在一种可能的实施方式 中, 所述处理模块 13具体用于: 根据所述 内存申请量和所述申请内存粒度, 确定所述目标内存块的第一数量; 判断所述至少一个 内存块中, 是否存在第一数量个连续内存块; 若是, 则将所述第一数量个连续内存块确定为所述目标内存块; 若否,则在所述内存操作请求指示允许分配内存不连续时,将所述至少一个内存块中、 第一数量个不连续的内存块确定为所述目标内存块,所述不连续的内存块的内存之和大于 或等于所述内存申请量。 在一种可能的实施方式 中, 所述更新模块 14还用于: 根据所述 目标内存块, 更新所述申请内存粒度对应的内存块信息。 在一种可能的实施方式 中, 所述内存操作请求用于请求释放内存; 所述内存操作请求 中包括释放内存粒度、 以及所述释放内存粒度对应的内存地址; 所述处理模块 13具体用 于: 根据所述释放 内存粒度对应的内存块信息和所述内存地址,确定所述释放内存粒度对 应的至少一个待释放内存块;
对所述至 少一个待释放内存块进行清零处理, 以释放所述至少一个内存块。 在一种可 能的实施方式中, 所述更新模块 14还用于: 根据所述至 少一个内存块, 更新所述释放内存粒度对应的内存块信息。 在一种可 能的实施方式中, 所述处理模块 13还用于, 若所述释放内存粒度小于最大内存粒度, 且若所述释放内 存粒度对应的第三内存块信息中内存块状态均为空闲状态, 则根据所述第三内存块信息对 多个内存块进行合并处理,得到第三内存块,所述第三内存块的内存粒度为最大内存粒度; 所述更新模块 14还用于, 根据所述第三内存块更新所述最大内存粒度对应的内存块 信息, 并删除所述第三内存块信息。 本公开实施例提供的 内存操作装置可以执行上述方法实施例 所示的技术方案, 其 实现原理 以及有益效果类似, 此处不再进行赘述。 本公开示例性实施例提供一种 电子设备的结构示意图, 请参见图 10, 该电子设备 20 可以包括处理器 21和存储器 22。 示例性地, 处理器 21、 存储器 22, 各部分之间通过总 线 23相互连接。 所述存储器 22存储计算机执行指令; 所述处理器 21执行所述存储器 22存储的计算机执行指令, 使得所述处理器 21执行 如上述方法实施例所示的方法。 相应地, 本公开实施例提供一种计算机可读存储介质, 所述计算机可读存储介质 中存储有计算 机执行指令, 当所述计算机执行指令被处理器执行时用于 实现上述方法 实施例所述 的方法。 相应地, 本公开实施例还可以提供一种计算机程序产品 , 包括计算机程序, 该计 算机程序被 处理器执行时, 可实现上述方法实施例所示的方法。 本领域 内的技术人员应明白, 本公开的实施例可提供为方法、 系统、 或计算机程序产 品。 因此, 本公开可采用完全硬件实施例、 完全软件实施例、 或结合软件和硬件方面的实 施例的形式。 而且, 本公开可采用在一个或多个其中包含有计算机可用程序代码的计算机 可用存储介质 (包括但不限于磁盘存储器、 CD-ROM, 光学存储器等) 上实施的计算机程 序产品的形式。 本公开是参照根据本公开实施例 的方法、 设备 (系统) 、 和计算机程序产品的流程图 和/ 或方框图来描述的。应理解可由计算机程序指令实现流程图和/或方框图中的每一流 程和 /或方框、 以及流程图和 /或方框图中的流程和 /或方框的结合。 可提供这些计算机 程序指令到通用计算机、 专用计算机、嵌入式处理机或其他可编程数据处理设备的处理器 以产生一个机器,使得通过计算机或其他可编程数据处理设备的处理器执行的指令产生用 于实现在流程图一个流程或 多个流程和 /或方框图一个方框或多个方框中指定的功能的 装置。 这 些计算机程序指令也可存储在能引导计算机或其他可编程数据处理设备 以特定方
式工作的计算机可读存储器中,使得存储在该计算机可读存储器中的指令产生包括指令装 置的制造品,该指令装置实现在流程图一个流程或多个流程和 /或方框图一个方框或多个 方框中指定的功能。 这些计算机程序指令也可装载到计算机或其他可编程数据处理设备上,使得在计算机 或其他可编程设备上执行一系列操作步骤以产生计算机实现的处理,从而在计算机或其他 可编程设备上执行的指令提供 用于实现在流程图一个流程或多个流程和 /或方框图一个 方框或多个方框中指定的功能的步骤。 在一个典型 的配置中, 计算设备包括一个或多个处理器 (CPU)、 输入 /输出接口、 网络 接口和内存。 内存可能包括计算机可读介质中的非永久性存储器,随机存取存储器 (RAM)和/或非易 失性内存等形式,如只读存储器 (ROM)或闪存 (flash RAM) o内存是计算机可读介质的示例。 计算机可读介质 包括永久性和非永久性、可移动和非可移动媒体可以由任何方法或技 术来实现信息存储。 信息可以是计算机可读指令、 数据结构、 程序的模块或其他数据。 计 算机的存储介质的例子包括, 但不限于相变内存 (PRAM), 静态随机存取存储器 (SRAM)、 动态随机存取存储器 (DRAM)、 其他类型的随机存取存储器 (RAM)、 只读存储器 (ROM)、 电可擦除可编程只读存储器 (EEPROM), 快闪记忆体或其他内存技术、 只读光盘只读存储 器 (CD-ROM), 数字多功能光盘 (DVD)或其他光学存储、 磁盒式磁带, 磁带磁盘存储或其 他磁性存储设备或任何其他非传输介质, 可用于存储可以被计算设备访问的信息。按照本 文中的界定, 计算机可读介质不包括暂存电脑可读媒体 (transitory media) ,如调制的数据信 号和载波。 还 需要说明的是,术语“包括”、“包含”或者其任何其他变体意在涵盖非排他性的包含, 从而使得包括一系列要素的过程、 方法、 商品或者设备不仅包括那些要素, 而且还包括没 有明确列出的其他要素, 或者是还包括为这种过程、 方法、 商品或者设备所固有的要素。 在没有更多限制的情况下, 由语句 “包括一个 ..... ”限定的要素, 并不排除在包括所述要素 的过程、 方法、 商品或者设备中还存在另外的相同要素。 以上所述仅为本公开的实施例而已,并不用于限制本公开。对于本领域技术人员来说, 本公开可以有各种更改和变化。凡在本公开的精神和原理之内所作的任何修改、等同替换、 改进等, 均应包含在本公开的权利要求范围之内。
Claims
1、 一种内存操作方法, 其中, 包括: 获取对 内存的内存操作请求,所述内存操作请求用于请求分配内存或者请求释放内存 , 所述内存中包括多个内存块, 所述多个内存块对应至少两种内存粒度; 确定至少一种 内存粒度对应的内存块信息, 所述内存块信息中包括内存粒度值、 内存 块状态、 以及内存块使用信息; 根据至少一种 内存粒度对应的内存块信息对所述内存操作请求进行处理, 以进行分配 内存或者释放内存。
2、 根据权利要求 1所述的方法, 其中, 所述内存操作请求用于请求分配内存, 所述 内存操作请求中包括申请内存粒度、 以及所述申请内存粒度对应的内存申请量; 根据至少 一种内存粒度对应的内存块信息对所述内存操作请求进行处理, 以进行内存分配, 包括: 确定所述 申请内存粒度对应的内存块信息; 根据所述 申请内存粒度对应的内存块信息和内存申请量,确定所述申请内存粒度对应 的目标内存块, 所述目标内存块的内存粒度为所述申请内存粒度, 所述目标内存块的内存 块状态为空闲状态; 分配所述 申请内存粒度对应的目标内存块。
3、 根据权利要求 2所述的方法, 其中, 所述申请内存粒度小于最大内存粒度; 确定 所述申请内存粒度对应的内存块信息, 包括: 确定所述 申请内存粒度对应的上一级内存粒度,所述上一级内存粒度对应的内存块被 划分为多个所述申请内存粒度对应的内存块; 在所述上一级 内存粒度对应的内存块信息中确定第一链表地址; 根据所述 第一链表地址确定所述申请内存粒度对应的内存块信息。
4、 根据权利要求 2或 3所述的方法, 其中, 根据所述申请内存粒度对应的内存块信 息和所述内存申请量, 确定所述目标内存块, 包括: 判断所述 申请内存粒度是否为最大内存粒度; 若是, 则在所述申请内存粒度对应的内存总量大于或等于所述内存申请量时, 在所述 申请内存粒度对应的至少一个内存块中确定所述目标内存块; 若否, 则在所述申请内存粒度对应的内存总量大于或等于所述内存申请量时, 在所述 申请内存粒度对应的至少一个内存块中确定所述目标内存块;在所述申请内存粒度对应的 内存总量小于所述内存申请量时, 更新所述至少一个内存块, 并在更新后的至少一个内存 块中确定所述目标内存块。
5、 根据权利要求 4所述的方法, 其中, 更新所述至少一个内存块, 包括: 根据所述 申请内存粒度, 确定第一内存块, 所述第一内存块的内存粒度大于所述申请 内存粒度, 所述第一内存块的内存块状态为空闲状态; 对所述 第一内存块进行切分处理, 得到多个第二内存块, 所述第二内存块的内存粒度
25
为所述申请内存粒度; 确定更新后的至少一个 内存块包括所述至少一个内存块和所述多个第二内存块。
6、 根据权利要求 5所述的方法, 其中, 所述方法还包括: 确定所述第一 内存块对应的第一内存粒度,确定所述第一内存粒度对应的第一内存块 信息, 并根据所述第一内存块更新所述第一内存块信息; 确定所述 申请内存粒度对应的第二内存块信息,并根据所述多个第二内存块更新所述 第二内存块信息。
7、 根据权利要求 4-6任一项所述的方法, 其中, 在所述申请内存粒度对应的至少一 个内存块中确定所述目标内存块, 包括: 根据所述 内存申请量和所述申请内存粒度, 确定所述目标内存块的第一数量; 判断所述至少一个 内存块中, 是否存在第一数量个连续内存块; 若是, 则将所述第一数量个连续内存块确定为所述目标内存块; 若否,则在所述内存操作请求指示允许分配内存不连续时,将所述至少一个内存块中、 第一数量个不连续的内存块确定为所述目标内存块,所述不连续的内存块的内存之和大于 或等于所述内存申请量。
8、 根据权利要求 2-7任一项所述的方法, 其中, 所述方法还包括: 根据所述 目标内存块, 更新所述申请内存粒度对应的内存块信息。
9、 根据权利要求 1-8任一项所述的方法, 其中, 所述内存操作请求用于请求释放内 存; 所述内存操作请求中包括释放内存粒度、 以及所述释放内存粒度对应的内存地址; 根 据至少一种内存粒度对应的内存块信息对所述内存操作请求进行处理, 以进行释放内存, 包括: 根据所述释放 内存粒度对应的内存块信息和所述内存地址,确定所述释放内存粒度对 应的至少一个待释放内存块; 对所述至少一个待释放 内存块进行清零处理, 以释放所述至少一个内存块。
10、 根据权利要求 9所述的方法, 其中, 所述方法还包括: 根据所述至少一个 内存块, 更新所述释放内存粒度对应的内存块信息。
11、 根据权利要求 9或 10所述的方法, 其中, 所述方法还包括: 若所述释放 内存粒度小于最大内存粒度,且若所述释放内存粒度对应的第三内存块信 息中内存块状态均为空闲状态, 则根据所述第三内存块信息对多个内存块进行合并处理, 得到第三内存块, 所述第三内存块的内存粒度为最大内存粒度; 根据所述 第三内存块更新所述最大内存粒度对应的内存块信息,并删除所述第三内存 块信息。
12、 一种内存操作装置, 其中, 包括: 获取模块、 确定模块和处理模块, 其中, 所述获取模块 用于, 获取对内存的内存操作请求, 所述内存操作请求用于请求分配内 存或者请求释放内存, 所述内存中包括多个内存块, 所述多个内存块对应至少一种内存粒
度; 所述确定模块 用于, 确定至少一种内存粒度对应的内存块信息, 所述内存块信息中包 括内存粒度、 内存块状态、 以及内存块使用信息; 所述处理模块 用于,根据至少一种内存粒度对应的内存块信息对所述内存操作请求进 行处理, 以进行分配内存或者释放内存。
13、 一种电子设备, 其中, 包括: 至 少一个处理器; 以及 与所述至少一个处理器通信连接的存储器; 其 中, 所述存储器存储有可被所述至少一个处理器执行的指令, 所述指令被所述至少 一个处理器执行, 以使所述电子设备执行权利要求 1-11任一项所述的方法。
14、 一种计算机可读存储介质, 其中, 所述计算机可读存储介质中存储有计算机执行 指令, 当处理器执行所述计算机执行指令时, 实现如权利要求 1-11任一项所述的方法。
15、 一种计算机程序产品, 包括计算机程序, 其中, 该计算机程序被处理器执行时实 现如权利要求 1-11任一项所述的方法。
Applications Claiming Priority (2)
| Application Number | Priority Date | Filing Date | Title |
|---|---|---|---|
| CN202410211042.3 | 2024-02-26 | ||
| CN202410211042.3A CN120540576A (zh) | 2024-02-26 | 2024-02-26 | 内存操作方法、装置、设备、存储介质及程序产品 |
Publications (1)
| Publication Number | Publication Date |
|---|---|
| WO2025181573A1 true WO2025181573A1 (zh) | 2025-09-04 |
Family
ID=96790274
Family Applications (1)
| Application Number | Title | Priority Date | Filing Date |
|---|---|---|---|
| PCT/IB2025/050581 Pending WO2025181573A1 (zh) | 2024-02-26 | 2025-01-21 | 内存操作方法、装置、设备、存储介质及程序产品 |
Country Status (2)
| Country | Link |
|---|---|
| CN (1) | CN120540576A (zh) |
| WO (1) | WO2025181573A1 (zh) |
Citations (5)
| Publication number | Priority date | Publication date | Assignee | Title |
|---|---|---|---|---|
| CN104679477A (zh) * | 2015-02-15 | 2015-06-03 | 中国科学院计算技术研究所 | 一种面向可变粒度内存系统的二进制文件重写方法 |
| CN115599544A (zh) * | 2022-10-12 | 2023-01-13 | 阿里巴巴(中国)有限公司(Cn) | 内存管理方法、装置、计算机设备及存储介质 |
| CN116225693A (zh) * | 2022-12-26 | 2023-06-06 | 阿里云计算有限公司 | 元数据管理方法、装置、计算机设备及存储介质 |
| CN116302550A (zh) * | 2023-03-20 | 2023-06-23 | 阿里云计算有限公司 | 内存交换方法、装置、计算机设备及存储介质 |
| CN116661990A (zh) * | 2023-04-23 | 2023-08-29 | 吉林动画学院 | JavaScript编程环境下释放3D渲染资源内存的多粒度方法 |
-
2024
- 2024-02-26 CN CN202410211042.3A patent/CN120540576A/zh active Pending
-
2025
- 2025-01-21 WO PCT/IB2025/050581 patent/WO2025181573A1/zh active Pending
Patent Citations (5)
| Publication number | Priority date | Publication date | Assignee | Title |
|---|---|---|---|---|
| CN104679477A (zh) * | 2015-02-15 | 2015-06-03 | 中国科学院计算技术研究所 | 一种面向可变粒度内存系统的二进制文件重写方法 |
| CN115599544A (zh) * | 2022-10-12 | 2023-01-13 | 阿里巴巴(中国)有限公司(Cn) | 内存管理方法、装置、计算机设备及存储介质 |
| CN116225693A (zh) * | 2022-12-26 | 2023-06-06 | 阿里云计算有限公司 | 元数据管理方法、装置、计算机设备及存储介质 |
| CN116302550A (zh) * | 2023-03-20 | 2023-06-23 | 阿里云计算有限公司 | 内存交换方法、装置、计算机设备及存储介质 |
| CN116661990A (zh) * | 2023-04-23 | 2023-08-29 | 吉林动画学院 | JavaScript编程环境下释放3D渲染资源内存的多粒度方法 |
Also Published As
| Publication number | Publication date |
|---|---|
| CN120540576A (zh) | 2025-08-26 |
Similar Documents
| Publication | Publication Date | Title |
|---|---|---|
| US10983955B2 (en) | Data unit cloning in memory-based file systems | |
| CN110858162B (zh) | 内存管理方法及装置、服务器 | |
| KR101620773B1 (ko) | 복합식 비휘발성 저장 디바이스를 위한 데이터 이송 | |
| CN103714013B (zh) | 一种文件系统的存储空间的配置方法及装置 | |
| CN113672171B (zh) | 分布式对象存储方法、装置、系统和元数据服务器 | |
| CN105579977A (zh) | 访问文件的方法、装置及存储系统 | |
| KR20190052546A (ko) | 키-밸류 스토리지 장치 및 상기 키-밸류 스토리지 장치의 동작 방법 | |
| CN108052541B (zh) | 基于多级页表目录结构的文件系统的实现、访问方法、终端 | |
| CN110083307A (zh) | 数据存储方法、存储器和服务器 | |
| CN107844436B (zh) | 一种缓存中脏数据的组织管理方法、系统及存储系统 | |
| CN106294189B (zh) | 内存碎片整理方法及装置 | |
| CN109947667B (zh) | 数据访问预测方法和装置 | |
| WO2024239835A1 (zh) | 记录内存状态的方法、装置、计算机设备及存储介质 | |
| CN112115521A (zh) | 数据访问方法及装置 | |
| US11586353B2 (en) | Optimized access to high-speed storage device | |
| CN118276771A (zh) | 一种numa架构下nvm文件并行访问方法 | |
| CN109960662A (zh) | 一种内存回收方法及设备 | |
| CN101783814A (zh) | 海量存储系统的元数据存储方法 | |
| CN114816240A (zh) | 一种数据写入方法及一种数据读取方法 | |
| CN114253456B (zh) | 一种缓存负载均衡方法和装置 | |
| CN115904211A (zh) | 一种存储系统、数据处理方法及相关设备 | |
| CN107102898B (zh) | 一种基于numa架构的内存管理、构建数据结构的方法及装置 | |
| CN110286858A (zh) | 一种数据处理方法及相关设备 | |
| CN111159438B (zh) | 一种数据的存储和检索方法、电子设备及存储介质 | |
| CN120540576A (zh) | 内存操作方法、装置、设备、存储介质及程序产品 |
Legal Events
| Date | Code | Title | Description |
|---|---|---|---|
| 121 | Ep: the epo has been informed by wipo that ep was designated in this application |
Ref document number: 25761756 Country of ref document: EP Kind code of ref document: A1 |