CN108108307A - A kind of method for processing resource and terminal - Google Patents
A kind of method for processing resource and terminal Download PDFInfo
- Publication number
- CN108108307A CN108108307A CN201611044955.2A CN201611044955A CN108108307A CN 108108307 A CN108108307 A CN 108108307A CN 201611044955 A CN201611044955 A CN 201611044955A CN 108108307 A CN108108307 A CN 108108307A
- Authority
- CN
- China
- Prior art keywords
- resource block
- space length
- free
- idle
- address information
- 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; CALCULATING OR COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F12/00—Accessing, addressing or allocating within memory systems or architectures
- G06F12/02—Addressing or allocation; Relocation
- G06F12/0223—User address space allocation, e.g. contiguous or non contiguous base addressing
- G06F12/023—Free address space management
- G06F12/0253—Garbage collection, i.e. reclamation of unreferenced memory
Landscapes
- Engineering & Computer Science (AREA)
- Theoretical Computer Science (AREA)
- Physics & Mathematics (AREA)
- General Engineering & Computer Science (AREA)
- General Physics & Mathematics (AREA)
- Mobile Radio Communication Systems (AREA)
Abstract
Description
技术领域technical field
本发明涉及计算机领域中的存储技术,尤其涉及一种资源处理方法及终端。The present invention relates to storage technology in the computer field, in particular to a resource processing method and a terminal.
背景技术Background technique
存储器是现代信息技术中用于保存程序和数据等信息的记忆设备,计算机可以将输入的原始数据、计算机程序、计算的中间结果和最终运行结果等信息存储至存储器中,所以,存储器的资源是否充足决定了计算机处理速度的快慢,近年来,随着网络的发展越来越迅速,计算机承载了越来越庞大的计算量和数据信息,而存储器存储的资源是固定且非常有限的,所以对存储器的资源进行管理时,可以采用一些特殊的方法,以提高存储器的运行效率,减少存储空间的浪费。Memory is a memory device used to save information such as programs and data in modern information technology. Computers can store information such as input raw data, computer programs, intermediate results of calculations, and final operating results into memory. Therefore, whether the resources of memory Sufficiency determines the speed of computer processing. In recent years, with the rapid development of the network, the computer has carried more and more calculations and data information, and the resources stored in the memory are fixed and very limited, so for When managing memory resources, some special methods can be adopted to improve the operating efficiency of the memory and reduce the waste of storage space.
目前,终端的操作系统可以通过空闲块链表的形式对存储器进行管理,如图1所示,存储器中的所有空闲块以空闲块链表的形式存储,存储器分配资源时,存储器通过遍历链表来查找合适的空闲块;存储器回收资源时,存储器通过遍历链表来查找合适的插入地址插入回收的资源块。At present, the operating system of the terminal can manage the memory in the form of a free block list. As shown in Figure 1, all free blocks in the memory are stored in the form of a free block list. When the memory reclaims resources, the memory searches for a suitable insertion address to insert the reclaimed resource block by traversing the linked list.
然而,现有的空闲块链表管理方式在进行了大量的分配和回收操作之后产生大量的空间碎片,这些长度非常小的空间碎片不能满足分配要求,但链表中的碎片之和又大于待分配空间,导致了空间碎片过多、资源浪费的问题。However, the existing free block linked list management method produces a large number of space fragments after a large number of allocation and recovery operations. These very small space fragments cannot meet the allocation requirements, but the sum of the fragments in the linked list is greater than the space to be allocated , leading to the problems of excessive space debris and waste of resources.
发明内容Contents of the invention
为解决上述技术问题,本发明实施例期望提供一种资源处理方法及终端,能够减少空间碎片,降低资源浪费。In order to solve the above technical problems, the embodiments of the present invention expect to provide a resource processing method and a terminal, which can reduce space fragments and reduce waste of resources.
本发明的技术方案是这样实现的:Technical scheme of the present invention is realized like this:
本发明实施例提供一种资源处理方法,包括:An embodiment of the present invention provides a resource processing method, including:
在传输第一数据时,获取所述第一数据的第一空间长度;When transmitting the first data, acquire the first space length of the first data;
将所述第一空间长度与预设空间长度进行对比;comparing the first space length with a preset space length;
当所述第一空间长度小于所述预设空间长度时,按照所述预设空间长度遍历空闲资源块,确定第一空闲资源块,所述第一空闲资源块大于等于所述预设空间长度;When the first space length is less than the preset space length, traverse the free resource blocks according to the preset space length to determine a first free resource block, and the first free resource block is greater than or equal to the preset space length ;
采用所述第一空闲资源块给所述第一数据分配存储资源。Allocating storage resources to the first data by using the first idle resource block.
在上述方案中,所述将所述第一空间长度与所述预设空间长度进行对比之后,所述采用所述第一空闲资源块给所述第一数据分配存储资源之前,所述方法还包括:In the above solution, after comparing the first space length with the preset space length, before using the first free resource block to allocate storage resources to the first data, the method further include:
当所述第一空间长度大于等于所述预设空间长度时,按照所述第一空间长度遍历所述空闲资源块,确定所述第一空闲资源块,所述第一空闲资源块大于等于所述第一空间长度。When the first space length is greater than or equal to the preset space length, traverse the idle resource blocks according to the first space length to determine the first idle resource block, and the first idle resource block is greater than or equal to the preset space length the first space length.
在上述方案中,所述采用所述第一空闲资源块给所述第一数据分配存储资源,包括:In the above solution, the allocation of storage resources to the first data by using the first free resource block includes:
当两个所述预设空间长度之和,大于所述第一空闲资源块的长度时,确定所述第一空闲资源块为所述第一数据的存储资源块;When the sum of the two preset space lengths is greater than the length of the first free resource block, determine that the first free resource block is a storage resource block for the first data;
当两个所述预设空间长度之和,小于等于所述第一空闲资源块的长度时,从所述第一空闲资源块中确定长度为所述预设空间长度的待分配资源块,所述待分配资源块为所述第一数据的存储资源块。When the sum of the two preset space lengths is less than or equal to the length of the first free resource block, determine a resource block to be allocated whose length is the preset space length from the first free resource block, and The resource block to be allocated is a resource block for storing the first data.
在上述方案中,所述采用所述第一空闲资源块给所述第一数据分配存储资源,包括:In the above solution, the allocation of storage resources to the first data by using the first free resource block includes:
当所述第一空间长度与所述预设空间长度之和,大于所述第一空闲资源块的长度时,确定所述第一空闲资源块为所述第一数据的存储资源块;When the sum of the first space length and the preset space length is greater than the length of the first free resource block, determine that the first free resource block is a storage resource block for the first data;
当所述第一空间长度与所述预设空间长度之和,小于等于所述第一空闲资源块的长度时,从所述第一空闲资源块中确定长度为所述第一空间长度的待分配资源块,所述待分配资源块为所述第一数据的存储资源块。When the sum of the first space length and the preset space length is less than or equal to the length of the first free resource block, determine from the first free resource block a waiting space whose length is the first space length Allocating a resource block, where the resource block to be allocated is a storage resource block for the first data.
在上述方案中,所述从所述第一空闲资源块中确定长度为所述预设空间长度的待分配资源块,包括:In the above solution, the determining the resource block to be allocated whose length is the preset space length from the first free resource block includes:
在所述第一空闲资源块中,从所述第一空闲资源块的终止地址开始,读取长度为所述预设空间长度的第二空闲资源块,所述第二空闲资源块属于所述第一空闲资源块;In the first free resource block, starting from the end address of the first free resource block, read a second free resource block whose length is the preset space length, and the second free resource block belongs to the the first free resource block;
将所述第二空闲资源块确定为所述待分配资源块。Determining the second idle resource block as the resource block to be allocated.
在上述方案中,所述从所述第一空闲资源块中确定长度为所述第一空间长度的待分配资源块,包括:In the above solution, the determining the resource block to be allocated whose length is the first space length from the first free resource block includes:
在所述第一空闲资源块中,从所述第一空闲资源块的终止地址开始,读取长度为所述第一空间长度的第三空闲资源块,所述第三空闲资源块属于所述第一空闲资源块;In the first free resource block, starting from the end address of the first free resource block, read a third free resource block whose length is the first space length, and the third free resource block belongs to the the first free resource block;
将所述第三空闲资源块确定为所述待分配资源块。Determining the third idle resource block as the resource block to be allocated.
在上述方案中,所述按照所述预设空间长度遍历空闲资源块,确定第一空闲资源块,包括:In the above solution, traversing the idle resource blocks according to the preset space length to determine the first idle resource block includes:
从起始空闲资源块开始遍历所述空闲资源块;traversing the free resource blocks from the initial free resource block;
将所述预设空间长度与当前空闲资源块的第二空间长度进行比较,所述当前空闲资源块为所述空闲资源块中的一个空闲资源块;comparing the preset space length with a second space length of a current free resource block, where the current free resource block is one of the free resource blocks;
直至确定所述第二空间长度大于所述预设空间长度的所述当前空闲资源块为所述第一空闲资源块。Until it is determined that the current idle resource block whose second space length is greater than the preset space length is the first idle resource block.
在上述方案中,所述将所述预设空间长度与当前空闲资源块的第二空间长度进行比较之后,所述方法还包括:In the above solution, after comparing the preset space length with the second space length of the current idle resource block, the method further includes:
当遍历到终止空闲资源块都没有确定所述第一空闲资源块时,结束将所述预设空间长度与所述当前空闲资源块的所述第二空间长度进行比较的流程并返回确定失败指令。When traversing to the end of the free resource block does not determine the first free resource block, end the process of comparing the preset space length with the second space length of the current free resource block and return the determination failure instruction .
在上述方案中,所述采用所述第一空闲资源块给所述第一数据分配存储资源之后,所述方法还包括:In the above solution, after allocating storage resources to the first data by using the first free resource block, the method further includes:
在传输完成所述第一数据时,释放所述第一数据,并获取释放完所述第一数据的存储资源块,所述存储资源块包括第一地址信息;When the transmission of the first data is completed, releasing the first data, and obtaining a storage resource block after releasing the first data, the storage resource block includes first address information;
根据所述第一地址信息和预设判定条件,遍历所述空闲资源块,确定与所述存储资源块相邻的第四空闲资源块;Traverse the idle resource blocks according to the first address information and preset determination conditions, and determine a fourth idle resource block adjacent to the storage resource block;
根据所述第一地址信息和第二地址信息对所述存储资源块与所述第四空闲资源块进行判定,所述第四空闲资源块为所述空闲资源块中与所述存储资源块相邻的空闲资源块,所述第二地址信息为所述第四空闲资源块的地址信息;Determine the storage resource block and the fourth idle resource block according to the first address information and the second address information, and the fourth idle resource block is one of the idle resource blocks corresponding to the storage resource block. Adjacent idle resource blocks, the second address information is address information of the fourth idle resource block;
当判定所述第四空闲资源块与所述存储资源块连续时,将所述第四空闲资源块与所述存储资源块合并成新的所述第四空闲资源块。When it is determined that the fourth idle resource block is continuous with the storage resource block, combining the fourth idle resource block and the storage resource block into a new fourth idle resource block.
在上述方案中,所述根据所述第一地址信息和预设判定条件,遍历所述空闲资源块,确定与所述存储资源块相邻的第四空闲资源块,包括:In the above solution, the step of traversing the idle resource blocks according to the first address information and preset determination conditions, and determining the fourth idle resource block adjacent to the storage resource block includes:
从起始空闲资源块开始遍历所述空闲资源块;traversing the free resource blocks from the initial free resource block;
将所述第一地址信息与所述当前空闲资源块的第二地址信息进行比较;comparing the first address information with the second address information of the current free resource block;
当确定所述第二地址信息大于所述第一地址信息,且所述第二地址信息与所述第一地址信息之差最小时,将当前空闲资源块确定为第五空闲资源块,所述第五空闲资源块为与所述存储资源块右相邻的所述第四空闲资源块;When it is determined that the second address information is greater than the first address information, and the difference between the second address information and the first address information is the smallest, determine the current idle resource block as the fifth idle resource block, the The fifth idle resource block is the fourth idle resource block right adjacent to the storage resource block;
当确定所述第二地址信息小于所述第一地址信息,且所述第二地址信息与所述第一地址信息之差最小时,将当前空闲资源块确定为第六空闲资源块,所述第六空闲资源块为与所述存储资源块左相邻的所述第四空闲资源块。When it is determined that the second address information is smaller than the first address information, and the difference between the second address information and the first address information is the smallest, determine the current idle resource block as the sixth idle resource block, the The sixth idle resource block is the fourth idle resource block left adjacent to the storage resource block.
在上述方案中,所述根据所述第一地址信息和预设判定条件,遍历所述空闲资源块,确定与所述存储资源块相邻的第四空闲资源块之后,所述方法还包括:In the above solution, after traversing the idle resource blocks according to the first address information and preset determination conditions, and determining the fourth idle resource block adjacent to the storage resource block, the method further includes:
当所述当前空闲资源块为终止空闲资源块,且所述第二地址信息小于所述第一地址信息时,结束遍历所述空闲资源块的流程。When the current idle resource block is a terminated idle resource block and the second address information is smaller than the first address information, the process of traversing the idle resource block ends.
在上述方案中,所述存储资源块包括第三空间长度,所述当所述第四空闲资源块与所述存储资源块连续时,将所述第四空闲资源块与所述存储资源块合并成新的所述第四空闲资源块,包括:In the above solution, the storage resource block includes a third space length, and when the fourth free resource block is continuous with the storage resource block, combining the fourth free resource block with the storage resource block forming a new fourth free resource block, including:
当所述存储资源块与所述第五空闲资源块连续时,将所述第五空闲资源块的空间长度与所述第三空间长度合并为新的第五空闲资源块的空间长度,将所述第一地址信息作为第一指针指向的下一个空闲资源块的地址信息,将第二指针指向的下一个空闲资源块的地址信息作为新的第三指针指向的下一个空闲资源块的地址信息,所述第一指针指向的下一个空闲资源块的地址信息为所述第六空闲资源块对应的指针信息,所述第二指针指向的下一个空闲资源块的地址信息为所述存储资源块对应的指针信息,所述第三指针指向的下一个空闲资源块的地址信息为所述第五空闲资源块对应的指针信息;When the storage resource block is continuous with the fifth idle resource block, combining the space length of the fifth idle resource block and the third space length into a new space length of the fifth idle resource block, and combining the space length of the fifth idle resource block The first address information is used as the address information of the next free resource block pointed to by the first pointer, and the address information of the next free resource block pointed to by the second pointer is used as the address information of the next free resource block pointed to by the new third pointer The address information of the next free resource block pointed to by the first pointer is the pointer information corresponding to the sixth free resource block, and the address information of the next free resource block pointed to by the second pointer is the storage resource block Corresponding pointer information, where the address information of the next free resource block pointed to by the third pointer is the pointer information corresponding to the fifth free resource block;
当所述存储资源块与所述第六空闲资源块连续时,将第六空闲资源块的空间长度与所述第二空间长度合并为新的第六空闲资源块的空间长度。When the storage resource block is continuous with the sixth free resource block, combining the space length of the sixth free resource block and the second space length into a new space length of the sixth free resource block.
在上述方案中,所述根据所述第一地址信息和第二地址信息对所述存储资源块与所述第四空闲资源块进行判定之后,所述方法还包括:In the above solution, after the determination of the storage resource block and the fourth idle resource block according to the first address information and the second address information, the method further includes:
当判定所述存储空闲资源块与所述第五空闲资源块和所述第六空闲资源块都不连续时,将所述第一地址信息作为所述第一指针指向的下一个空闲资源块的地址信息,将所述第一指针指向的下一个空闲资源块的地址信息作为所述第三指针指向的下一个空闲资源块的地址信息。When it is determined that the stored free resource block is not continuous with the fifth free resource block and the sixth free resource block, using the first address information as the address of the next free resource block pointed to by the first pointer Address information, using the address information of the next free resource block pointed to by the first pointer as the address information of the next free resource block pointed to by the third pointer.
本发明实施例提供一种终端,所述终端包括:An embodiment of the present invention provides a terminal, and the terminal includes:
获取单元,用于在传输第一数据时,获取所述第一数据的第一空间长度;an acquiring unit, configured to acquire a first space length of the first data when transmitting the first data;
对比单元,用于将所述第一空间长度与预设空间长度进行对比;a comparison unit, configured to compare the first space length with a preset space length;
确定单元,用于当所述第一空间长度小于所述预设空间长度时,按照所述预设空间长度遍历空闲资源块,确定第一空闲资源块,所述第一空闲资源块大于等于所述预设空间长度;A determining unit, configured to traverse idle resource blocks according to the preset space length when the first space length is less than the preset space length, and determine a first idle resource block, where the first idle resource block is greater than or equal to the preset space length the preset space length;
分配单元,用于采用所述第一空闲资源块给所述第一数据分配存储资源。An allocating unit, configured to allocate storage resources to the first data by using the first free resource block.
在上述终端中,所述确定单元,还用于当所述第一空间长度大于等于所述预设空间长度时,按照所述第一空间长度遍历所述空闲资源块,确定所述第一空闲资源块,所述第一空闲资源块大于等于所述第一空间长度。In the above terminal, the determining unit is further configured to traverse the idle resource blocks according to the first space length and determine the first idle resource block when the first space length is greater than or equal to the preset space length A resource block, the first idle resource block is greater than or equal to the first space length.
在上述终端中,所述确定单元,还用于当两个所述预设空间长度之和,大于所述第一空闲资源块的长度时,确定所述第一空闲资源块为所述第一数据的存储资源块;当两个所述预设空间长度之和,小于等于所述第一空闲资源块的长度时,从所述第一空闲资源块中确定长度为所述预设空间长度的待分配资源块,所述待分配资源块为所述第一数据的存储资源块。In the above terminal, the determining unit is further configured to determine that the first idle resource block is the first idle resource block when the sum of the two preset space lengths is greater than the length of the first idle resource block. A data storage resource block; when the sum of the two preset space lengths is less than or equal to the length of the first free resource block, determine from the first free resource block the length of the preset space length A resource block to be allocated is a resource block for storing the first data.
在上述终端中,所述确定单元,还用于当所述第一空间长度与所述预设空间长度之和,大于所述第一空闲资源块的长度时,确定所述第一空闲资源块为所述第一数据的存储资源块;当所述第一空间长度与所述预设空间长度之和,小于等于所述第一空闲资源块的长度时,从所述第一空闲资源块中确定长度为所述第一空间长度的待分配资源块,所述待分配资源块为所述第一数据的存储资源块。In the above terminal, the determining unit is further configured to determine the first idle resource block when the sum of the first space length and the preset space length is greater than the length of the first idle resource block storage resource block for the first data; when the sum of the first space length and the preset space length is less than or equal to the length of the first free resource block, select from the first free resource block Determine a resource block to be allocated whose length is the first space length, and the resource block to be allocated is a resource block for storing the first data.
在上述终端中,所述终端还包括:读取单元;In the above terminal, the terminal further includes: a reading unit;
所述读取单元,用于在所述第一空闲资源块中,从所述第一空闲资源块的终止地址开始,读取长度为所述预设空间长度的第二空闲资源块,所述第二空闲资源块属于所述第一空闲资源块;The reading unit is configured to, in the first free resource block, start from the end address of the first free resource block, and read a second free resource block whose length is the preset space length, the the second idle resource block belongs to the first idle resource block;
所述确定单元,还用于将所述第二空闲资源块确定为所述待分配资源块。The determining unit is further configured to determine the second idle resource block as the resource block to be allocated.
在上述终端中,所述读取单元,还用于在所述第一空闲资源块中,从所述第一空闲资源块的终止地址开始,读取长度为所述第一空间长度的第三空闲资源块,所述第三空闲资源块属于所述第一空闲资源块;In the above-mentioned terminal, the reading unit is further configured to, in the first free resource block, start from the termination address of the first free resource block, and read the third part whose length is the first space length. an idle resource block, the third idle resource block belongs to the first idle resource block;
所述确定单元,还用于将所述第三空闲资源块确定为所述待分配资源块。The determining unit is further configured to determine the third idle resource block as the resource block to be allocated.
在上述终端中,所述终端还包括:遍历单元和比较单元;In the above terminal, the terminal further includes: a traversal unit and a comparison unit;
所述遍历单元,用于从起始空闲资源块开始遍历所述空闲资源块;The traversal unit is configured to traverse the idle resource block from the initial idle resource block;
所述比较单元,用于将所述预设空间长度与当前空闲资源块的第二空间长度进行比较,所述当前空闲资源块为所述空闲资源块中的一个空闲资源块;The comparison unit is configured to compare the preset space length with a second space length of a current idle resource block, where the current idle resource block is one of the idle resource blocks;
所述确定单元,具体用于直至确定所述第二空间长度大于所述预设空间长度的所述当前空闲资源块为所述第一空闲资源块。The determining unit is specifically configured to determine that the current idle resource block whose second space length is greater than the preset space length is the first idle resource block.
在上述终端中,所述终端还包括:结束单元;In the above terminal, the terminal further includes: an end unit;
所述结束单元,用于当遍历到终止空闲资源块都没有确定所述第一空闲资源块时,结束将所述预设空间长度与所述当前空闲资源块的所述第二空间长度进行比较的流程并返回确定失败指令。The end unit is configured to end comparing the preset space length with the second space length of the current free resource block when the first free resource block is not determined after traversing to the end of the free resource block The process and return to confirm failure instruction.
在上述终端中,所述终端还包括:判定单元和合并单元;In the above terminal, the terminal further includes: a determining unit and a combining unit;
所述获取单元,还用于在传输完成所述第一数据时,释放所述第一数据,并获取释放完所述第一数据的存储资源块,所述存储资源块包括第一地址信息;The obtaining unit is further configured to release the first data when the transmission of the first data is completed, and obtain a storage resource block that has released the first data, and the storage resource block includes first address information;
所述确定单元,还用于根据所述第一地址信息和预设判定条件,遍历所述空闲资源块,确定与所述存储资源块相邻的第四空闲资源块;The determining unit is further configured to traverse the idle resource blocks according to the first address information and preset determination conditions, and determine a fourth idle resource block adjacent to the storage resource block;
所述判定单元,用于根据所述第一地址信息和第二地址信息对所述存储资源块与所述第四空闲资源块进行判定,所述第四空闲资源块为所述空闲资源块中与所述存储资源块相邻的空闲资源块,所述第二地址信息为所述第四空闲资源块的地址信息;The determination unit is configured to determine the storage resource block and the fourth idle resource block according to the first address information and the second address information, and the fourth idle resource block is one of the idle resource blocks For an idle resource block adjacent to the storage resource block, the second address information is address information of the fourth idle resource block;
所述合并单元,用于当判定所述第四空闲资源块与所述存储资源块连续时,将所述第四空闲资源块与所述存储资源块合并成新的所述第四空闲资源块。The combining unit is configured to combine the fourth idle resource block and the storage resource block into a new fourth idle resource block when it is determined that the fourth idle resource block is continuous with the storage resource block .
在上述终端中,所述遍历单元,还用于从起始空闲资源块开始遍历所述空闲资源块;In the above terminal, the traversal unit is further configured to traverse the idle resource block from the initial idle resource block;
所述比较单元,还将所述第一地址信息与所述当前空闲资源块的第二地址信息进行比较;The comparing unit further compares the first address information with the second address information of the current idle resource block;
所述确定单元,还用于当确定所述第二地址信息大于所述第一地址信息,且所述第二地址信息与所述第一地址信息之差最小时,将当前空闲资源块确定为第五空闲资源块,所述第五空闲资源块为与所述存储资源块右相邻的所述第四空闲资源块;当确定所述第二地址信息小于所述第一地址信息,且所述第二地址信息与所述第一地址信息之差最小时,将当前空闲资源块确定为第六空闲资源块,所述第六空闲资源块为与所述存储资源块左相邻的所述第四空闲资源块。The determining unit is further configured to determine the current idle resource block as A fifth idle resource block, where the fifth idle resource block is the fourth idle resource block right adjacent to the storage resource block; when it is determined that the second address information is smaller than the first address information, and the When the difference between the second address information and the first address information is the smallest, determine the current free resource block as the sixth free resource block, and the sixth free resource block is the left adjacent to the storage resource block. A fourth free resource block.
在上述终端中,所述结束单元,还用于当所述当前空闲资源块为终止空闲资源块,且所述第二地址信息小于所述第一地址信息时,结束遍历所述空闲资源块的流程。In the above-mentioned terminal, the ending unit is further configured to end traversing the idle resource block when the current idle resource block is a terminated idle resource block and the second address information is smaller than the first address information process.
在上述终端中,所述合并单元,具体用于当所述存储资源块与所述第五空闲资源块连续时,将所述第五空闲资源块的空间长度与所述第三空间长度合并为新的第五空闲资源块的空间长度,将所述第一地址信息作为第一指针指向的下一个空闲资源块的地址信息,将第二指针指向的下一个空闲资源块的地址信息作为新的第三指针指向的下一个空闲资源块的地址信息,所述第一指针指向的下一个空闲资源块的地址信息为所述第六空闲资源块对应的指针信息,所述第二指针指向的下一个空闲资源块的地址信息为所述存储资源块对应的指针信息,所述第三指针指向的下一个空闲资源块的地址信息为所述第五空闲资源块对应的指针信息;当所述存储资源块与所述第六空闲资源块连续时,将第六空闲资源块的空间长度与所述第二空间长度合并为新的第六空闲资源块的空间长度。In the above terminal, the combining unit is specifically configured to combine the space length of the fifth free resource block and the third space length into The space length of the new fifth free resource block, using the first address information as the address information of the next free resource block pointed to by the first pointer, and using the address information of the next free resource block pointed to by the second pointer as the new The address information of the next free resource block pointed to by the third pointer, the address information of the next free resource block pointed to by the first pointer is the pointer information corresponding to the sixth free resource block, and the next free resource block pointed to by the second pointer The address information of a free resource block is the pointer information corresponding to the storage resource block, and the address information of the next free resource block pointed to by the third pointer is the pointer information corresponding to the fifth free resource block; when the storage When the resource block is continuous with the sixth idle resource block, combining the space length of the sixth idle resource block and the second space length into a new space length of the sixth idle resource block.
在上述终端中,所述终端还包括:更新单元;In the above terminal, the terminal further includes: an updating unit;
所述更新单元,用于当判定所述存储空闲资源块与所述第五空闲资源块和所述第六空闲资源块都不连续时,将所述第一地址信息作为所述第一指针指向的下一个空闲资源块的地址信息,将所述第一指针指向的下一个空闲资源块的地址信息作为所述第三指针指向的下一个空闲资源块的地址信息。The update unit is configured to use the first address information as the first pointer to The address information of the next free resource block pointed to by the first pointer is used as the address information of the next free resource block pointed to by the third pointer.
本发明实施例提供了一种资源处理方法及终端,通过在传输第一数据时,获取第一数据的第一空间长度;将第一空间长度与预设空间长度进行对比;当第一空间长度小于预设空间长度时,按照预设空间长度遍历空闲资源块,确定第一空闲资源块,第一空闲资源块大于等于预设空间长度;采用第一空闲资源块给第一数据分配存储资源。采用上述技术实现方案,能够在分配存储器资源的时候,设置最小存储资源,使得分配的空闲资源块和剩余的空闲资源块不小于最小存储资源,能够减少空间碎片,降低资源浪费。An embodiment of the present invention provides a resource processing method and a terminal, by acquiring the first space length of the first data when transmitting the first data; comparing the first space length with the preset space length; when the first space length When it is less than the preset space length, traverse the free resource blocks according to the preset space length to determine the first free resource block, the first free resource block is greater than or equal to the preset space length; use the first free resource block to allocate storage resources to the first data. By adopting the above technical implementation scheme, the minimum storage resource can be set when allocating memory resources, so that the allocated idle resource blocks and the remaining idle resource blocks are not smaller than the minimum storage resource, which can reduce space fragmentation and waste of resources.
附图说明Description of drawings
图1为现有技术中存储器对空闲块进行分配和回收示意图;FIG. 1 is a schematic diagram of memory allocation and recovery of idle blocks in the prior art;
图2为本发明实施例提供的一种资源处理方法流程图一;FIG. 2 is a flow chart 1 of a resource processing method provided by an embodiment of the present invention;
图3为本发明实施例提供的一种示例性的空闲块的结构示意图;FIG. 3 is a schematic structural diagram of an exemplary free block provided by an embodiment of the present invention;
图4为本发明实施例提供的一种示例性的已分配的空闲块的结构示意图;FIG. 4 is a schematic structural diagram of an exemplary allocated free block provided by an embodiment of the present invention;
图5为本发明实施例提供的一种示例性的空闲块被分配的示意图一;FIG. 5 is a schematic diagram 1 of an exemplary allocation of free blocks provided by an embodiment of the present invention;
图6为本发明实施例提供的一种示例性的空闲块被分配的示意图二;FIG. 6 is a schematic diagram 2 of an exemplary allocation of free blocks provided by an embodiment of the present invention;
图7为本发明实施例提供的一种示例性的空闲块被分配的示意图三;FIG. 7 is a schematic diagram 3 of an exemplary allocation of free blocks provided by an embodiment of the present invention;
图8为本发明实施例提供的一种资源处理方法流程图二;FIG. 8 is a second flowchart of a resource processing method provided by an embodiment of the present invention;
图9为本发明实施例提供的一种示例性的已分配空闲块被回收的示意图一;FIG. 9 is a schematic diagram 1 of an exemplary allocated free block being reclaimed according to an embodiment of the present invention;
图10为本发明实施例提供的一种示例性的已分配空闲块被回收的示意图二;FIG. 10 is a schematic diagram 2 of an exemplary allocated free block being reclaimed according to an embodiment of the present invention;
图11为本发明实施例提供的一种示例性的已分配空闲块被回收的示意图三;Fig. 11 is a schematic diagram 3 of an exemplary allocated free block being reclaimed according to an embodiment of the present invention;
图12为本发明实施例提供的一种资源处理方法流程图三;FIG. 12 is a third flowchart of a resource processing method provided by an embodiment of the present invention;
图13为本发明实施例提供的一种资源处理方法流程图四;FIG. 13 is a fourth flowchart of a resource processing method provided by an embodiment of the present invention;
图14为本发明实施例提供的一种终端1的结构示意图一;FIG. 14 is a first structural schematic diagram of a terminal 1 provided by an embodiment of the present invention;
图15为本发明实施例提供的一种终端1的结构示意图二;FIG. 15 is a second structural schematic diagram of a terminal 1 provided by an embodiment of the present invention;
图16为本发明实施例提供的一种终端1的结构示意图三;FIG. 16 is a third structural schematic diagram of a terminal 1 provided by an embodiment of the present invention;
图17为本发明实施例提供的一种终端1的结构示意图四;FIG. 17 is a fourth schematic structural diagram of a terminal 1 provided by an embodiment of the present invention;
图18为本发明实施例提供的一种终端1的结构示意图五;FIG. 18 is a fifth structural schematic diagram of a terminal 1 provided by an embodiment of the present invention;
图19为本发明实施例提供的一种终端1的结构示意图六;FIG. 19 is a sixth structural schematic diagram of a terminal 1 provided by an embodiment of the present invention;
图20为本发明实施例提供的一种终端1的结构示意图七。FIG. 20 is a seventh structural schematic diagram of a terminal 1 provided by an embodiment of the present invention.
具体实施方式Detailed ways
下面将结合本发明实施例中的附图,对本发明实施例中的技术方案进行清楚、完整地描述。The following will clearly and completely describe the technical solutions in the embodiments of the present invention with reference to the drawings in the embodiments of the present invention.
实施例一Embodiment one
本发明实施例提供一种资源处理方法,如图2所示,该方法可以包括:An embodiment of the present invention provides a resource processing method, as shown in Figure 2, the method may include:
S101、在传输第一数据时,获取第一数据的第一空间长度。S101. When transmitting first data, acquire a first space length of the first data.
本发明实施例提供的一种资源处理方法适用于对存储器中的空闲资源进行分配操作的场景下。A resource processing method provided by an embodiment of the present invention is applicable to a scenario in which idle resources in a memory are allocated.
本发明实施例中,当终端进行第一数据的传输时,终端操作系统需要调用存储器中的空闲资源来存储第一数据,此时,存储器获取到第一数据所需要占用的第一空间长度。In the embodiment of the present invention, when the terminal transmits the first data, the terminal operating system needs to call idle resources in the memory to store the first data, and at this time, the memory obtains the first space length required by the first data.
本发明实施例中,存储器中的空闲资源块是以空闲块链表的形式进行存储的,如图3所示,每一个空闲资源块由3个部分组成,分别是LEN、Next Addr和Space,其中,LEN表示的是该空闲资源块可用空间的长度,LEN的值为Next Addr和Space的值之和、Next Addr表示的是下一块空闲资源块的起始地址,且当该空闲资源块为最后一块空闲资源块时,该值为NULL、Space表示的是该空闲资源块实际可以分配的长度。In the embodiment of the present invention, the free resource blocks in the memory are stored in the form of a free block linked list, as shown in Figure 3, each free resource block is composed of three parts, namely LEN, Next Addr and Space, wherein , LEN represents the length of the available space of the free resource block, the value of LEN is the sum of the values of Next Addr and Space, Next Addr represents the starting address of the next free resource block, and when the free resource block is the last For a free resource block, the value is NULL, and Space indicates the length that the free resource block can actually allocate.
本发明实施例中,LEN和Next Addr所占用的长度可以根据存储器大小来设定。In the embodiment of the present invention, the length occupied by LEN and Next Addr can be set according to the memory size.
示例性的,设定LEN和Next Addr所占用的长度和为2个字节时,可以管理64K大小的空闲资源块;设定LEN和Next Addr所占用的长度和为4个字节时,可以管理4G大小的空闲资源块。Exemplarily, when the length sum occupied by LEN and Next Addr is set to 2 bytes, a 64K free resource block can be managed; when the length sum occupied by LEN and Next Addr is set to 4 bytes, it can Manage free resource blocks of 4G size.
S102、将第一空间长度与预设空间长度进行对比。S102. Compare the first space length with a preset space length.
当存储器获取到第一数据的第一空间长度之后,就要判定该第一空间长度是否小于存储器设定的最小长度了。After the memory acquires the first space length of the first data, it is necessary to determine whether the first space length is smaller than the minimum length set by the memory.
本发明实施例中存储器中设定有预设空间长度,当存储器获取到终端需要占用第一空间时,存储器将第一空间长度与预设空间长度进行对比,判断第一空间长度是否大于等于预设空间长度。In the embodiment of the present invention, a preset space length is set in the memory. When the memory acquires that the terminal needs to occupy the first space, the memory compares the first space length with the preset space length to determine whether the first space length is greater than or equal to the preset space length. Set the length of the space.
S103、当第一空间长度小于预设空间长度时,按照预设空间长度遍历空闲资源块,确定第一空闲资源块,该第一空闲资源块大于等于该预设空间长度。S103. When the first space length is less than the preset space length, traverse the free resource blocks according to the preset space length to determine a first free resource block, where the first free resource block is greater than or equal to the preset space length.
当第一空间长度与预设空间长度的对比结果是第一空间长度小于预设空间长度时,需要在存储器的空闲资源块中寻找大于等于预设空间长度的第一空闲资源块。When the result of the comparison between the first space length and the preset space length is that the first space length is smaller than the preset space length, it is necessary to search for a first free resource block greater than or equal to the preset space length among free resource blocks in the memory.
本发明实施例中,当第一空间长度小于预设空间长度时,表征终端需要的空间长度小于存储器预设的最低分配的空间长度,此时,存储器按照预设空间长度从空闲资源块中寻找合适的第一空闲资源块。In the embodiment of the present invention, when the first space length is less than the preset space length, it means that the space length required by the terminal is less than the minimum allocated space length preset by the memory. At this time, the memory searches for free resource blocks according to the preset space length A suitable first free resource block.
进一步地,当第一空间长度大于等于预设空间长度时,表征第一空间长度满足存储器的最低分配空间的长度,此时,存储器从空闲资源块中寻找大于等于第一空间长度的第一空闲资源块。Further, when the first space length is greater than or equal to the preset space length, it indicates that the first space length satisfies the length of the minimum allocated space of the memory. At this time, the memory searches for a first free resource block greater than or equal to the first space length from the free resource blocks. resource blocks.
可以理解的是,将第一空间长度与存储器设置的预设空间长度进行比较,避免申请存储器分配的空间长度小于存储器设置的预设空间长度,可以有效的避免空间碎片的生成。It can be understood that by comparing the first space length with the preset space length set by the memory, it is avoided that the space length applied for memory allocation is smaller than the preset space length set by the memory, which can effectively avoid the generation of space fragments.
S104、采用第一空闲资源块给第一数据分配存储资源。S104. Allocate storage resources to the first data by using the first free resource block.
当存储器确定了待分配的第一空闲资源块时,就要将第一空闲资源块按照存储器确定的分配空间长度进行分离,并将分离后的待分配空闲资源块分配给第一数据存储资源。When the memory determines the first free resource block to be allocated, it separates the first free resource block according to the allocated space length determined by the memory, and allocates the separated free resource block to the first data storage resource.
本发明实施例中,当第一空间长度小于预设空间长度时,比较两个预设空间长度之和是否大于第一空闲资源块的长度,如果两个预设空间长度之和大于等于第一空闲资源块的长度,表征第一空闲资源块在分离出预设空间长度的空闲资源块之后,剩余的空闲资源块小于预设空间长度,此时,将整个第一空闲资源块全部分配给第一数据进行数据的存储;如果两个预设空间长度之和小于第一空闲资源块的长度,表征第一空闲资源块在分离出预设空间长度的空闲资源块之后,剩余的空闲资源块大于预设空间长度,此时,将第一空闲资源块从尾部截取预设空间长度的空闲资源块并分配给第一数据进行数据的存储。In the embodiment of the present invention, when the first space length is less than the preset space length, it is compared whether the sum of the two preset space lengths is greater than the length of the first free resource block, and if the sum of the two preset space lengths is greater than or equal to the first The length of the free resource block indicates that after the first free resource block is separated from the free resource block with a preset space length, the remaining free resource blocks are smaller than the preset space length. At this time, the entire first free resource block is allocated to the first free resource block One data stores data; if the sum of the two preset space lengths is less than the length of the first free resource block, it means that after the first free resource block separates the free resource blocks with the preset space length, the remaining free resource blocks are greater than The space length is preset. At this time, the first free resource block is intercepted from the end of the free resource block of the preset space length and allocated to the first data for data storage.
进一步地,当第一空间长度大于等于预设空间长度时,比较第一空间长度与预设空间长度之和是否大于第一空闲资源块的长度,如果第一空间长度与预设空间长度与预设空间长度之和大于等于第一空闲资源块的长度,表征第一空闲资源块在分离出第一空间长度的空闲资源块之后,剩余的空闲资源块小于预设空间长度,此时,将整个第一空闲资源块全部分配给第一数据进行数据的存储;如果第一空间长度与预设空间长度与预设空间长度之和小于第一空闲资源块的长度,表征第一空闲资源块在分离出第一空间长度的空闲资源块之后,剩余的空闲资源块大于预设空间长度,此时,将第一空闲资源块从尾部截取第一空间长度的空闲资源块并分配给第一数据进行数据的存储。Further, when the first space length is greater than or equal to the preset space length, compare whether the sum of the first space length and the preset space length is greater than the length of the first idle resource block, if the first space length and the preset space length are the same as the preset space length Let the sum of the space lengths be greater than or equal to the length of the first free resource block, which means that after the first free resource block is separated from the free resource blocks of the first space length, the remaining free resource blocks are smaller than the preset space length. At this time, the entire The first free resource block is all allocated to the first data for data storage; if the sum of the first space length and the preset space length and the preset space length is less than the length of the first free resource block, it indicates that the first free resource block is separating After the free resource blocks of the first space length are found, the remaining free resource blocks are larger than the preset space length. At this time, the first free resource blocks are intercepted from the end of the first free resource blocks and allocated to the first data for data processing. storage.
本发明实施例中,已分配的资源块的数据结构如图4所示,由LEN和Addr两个部分组成,其中,LEN用来指出分配空间的实际长度,LEN的值为Next Addr和Space的值之和、Addr用来记录已分配资源块的起始地址。In the embodiment of the present invention, the data structure of the allocated resource block is as shown in Figure 4, and consists of two parts, LEN and Addr, wherein, LEN is used to indicate the actual length of the allocated space, and the value of LEN is the value of Next Addr and Space The sum of the values, Addr is used to record the starting address of the allocated resource block.
本发明实施例中,第一空闲资源块从尾部截取指定空间长度的空闲资源块,如图5所示,空闲资源块在分配前由LEN1、Next Addr和Space三个部分组成,当需要从该空闲资源块中分配长度为64K资源时,从Space尾部截取64K作为分配的资源,在分配的资源前再分配2个字节存储该资源的LEN2和Addr,此时空闲资源块的LEN1值更改为LEN1’,其中,LEN1’的值为LEN1的值与64k之差。In the embodiment of the present invention, the first idle resource block intercepts an idle resource block with a specified space length from the tail. As shown in FIG. 5, the idle resource block is composed of LEN1, Next Addr and Space before allocation. When a resource with a length of 64K is allocated in the free resource block, 64K is intercepted from the end of Space as the allocated resource, and 2 bytes are allocated before the allocated resource to store the LEN2 and Addr of the resource. At this time, the value of LEN1 of the free resource block is changed to LEN1', where the value of LEN1' is the difference between the value of LEN1 and 64k.
示例性的,如图6所示,存储器中有L1、L2、L3三块空闲资源块,以链表的形式进行存储,当存储器将L2空闲资源块全部分配给第一数据进行存储操作时,L2空闲资源块中的Next Addr的值更改为L3空闲资源块中的Next Addr的值,L1空闲资源块中的Next Addr的值更改为L2空闲资源块中的Next Addr的值,将L2空闲资源块分配给第一数据。Exemplarily, as shown in FIG. 6, there are three free resource blocks L1, L2, and L3 in the memory, which are stored in the form of a linked list. When the memory allocates all the free resource blocks of L2 to the first data for storage operations, L2 The value of Next Addr in the free resource block is changed to the value of Next Addr in the L3 free resource block, the value of Next Addr in the L1 free resource block is changed to the value of Next Addr in the L2 free resource block, and the L2 free resource block assigned to the first data.
示例性的,如图7所示,存储器中有L1、L2两块空闲资源块,以链表的形式进行存储,当存储器将L2空闲资源块中的部分空闲资源块分配给第一数据进行存储操作时,L2空闲资源块从尾部截取根据第一数据确定的指定空闲资源块L3,在L3资源块前添加表征L3资源块的长度和地址的L3和Addr字段,此时,L2空闲资源块中表征可分配长度的L2值更改为L2’,其中,L2’的值为L2值与L3值之差。Exemplarily, as shown in FIG. 7, there are two free resource blocks L1 and L2 in the memory, which are stored in the form of a linked list. When the memory allocates part of the free resource blocks in the L2 free resource blocks to the first data storage operation , the L2 idle resource block intercepts the specified idle resource block L3 determined according to the first data from the end, and adds the L3 and Addr fields representing the length and address of the L3 resource block before the L3 resource block. At this time, the L2 idle resource block represents The L2 value of the assignable length is changed to L2', where the value of L2' is the difference between the L2 value and the L3 value.
可以理解的是,在对空闲资源块分配给第一数据时,保证分配出去的第一空闲资源块和剩余的空闲资源块不小于存储器预设的最小值,这样,能够减少在资源分配时,产生较小的空间资源块,导致不能继续进行之后的资源分配,能够减少空间碎片,降低资源浪费。It can be understood that when allocating the free resource blocks to the first data, it is ensured that the allocated first free resource blocks and the remaining free resource blocks are not less than the minimum value preset by the memory, thus reducing resource allocation. Smaller space resource blocks are generated, resulting in the impossibility of subsequent resource allocation, which can reduce space debris and waste of resources.
实施例二Embodiment two
如图8所示,基于实施例一,本发明实施例还提供一种资源回收方法,在步骤S104之后,具体步骤包括:As shown in FIG. 8, based on the first embodiment, the embodiment of the present invention also provides a resource recovery method. After step S104, the specific steps include:
S201、在传输完成所述第一数据时,释放第一数据,并获取释放完第一数据的存储资源块,存储资源块包括第一地址信息。S201. When the transmission of the first data is completed, release the first data, and acquire a storage resource block for which the first data has been released, where the storage resource block includes first address information.
本发明实施例提供的一种资源处理方法适用于对已分配的资源块回收至存储器的场景下。A resource processing method provided by an embodiment of the present invention is applicable to a scenario where allocated resource blocks are reclaimed to a memory.
本发明实施例中,终端对第一数据传输完成时,终端就要将存储第一数据相关信息的存储资源块进行释放,此时,存储器获取到该存储资源块,并将该存储资源块回收至空闲资源块链表的指定地址处。In the embodiment of the present invention, when the terminal completes the transmission of the first data, the terminal will release the storage resource block storing the relevant information of the first data. At this time, the memory acquires the storage resource block and recycles the storage resource block to the specified address of the free resource block linked list.
本发明实施例中,已分配的资源块的数据结构如图4所示,由LEN和Addr两个部分组成,其中,LEN用来指出分配空间的实际长度,LEN的值为Next Addr和Space的值之和、Addr用来记录已分配资源块的起始地址。In the embodiment of the present invention, the data structure of the allocated resource block is as shown in Figure 4, and consists of two parts, LEN and Addr, wherein, LEN is used to indicate the actual length of the allocated space, and the value of LEN is the value of Next Addr and Space The sum of the values, Addr is used to record the starting address of the allocated resource block.
S202、根据第一地址信息和预设判定条件,遍历空闲资源块,确定与存储资源块相邻的第四空闲资源块。S202. Traverse the idle resource blocks according to the first address information and preset determination conditions, and determine a fourth idle resource block adjacent to the storage resource block.
当存储器将存储资源块回收至空闲资源块链表中之后,存储器就要在空闲资源块链表中寻找与存储资源块相邻的空闲资源块,来判定是否可以将存储资源块与相邻的空闲资源块进行合并了。After the memory reclaims the storage resource block to the free resource block linked list, the memory will search for the free resource block adjacent to the storage resource block in the free resource block linked list to determine whether the storage resource block can be combined with the adjacent free resource block. The blocks are merged.
本发明实施例中,存储资源块包括Addr字段表示的第一地址信息和LEN字段表示的第三空间长度。In the embodiment of the present invention, the storage resource block includes the first address information represented by the Addr field and the third space length represented by the LEN field.
本发明实施例中,存储器读取存储资源块的LEN字段和Addr字段,然后依次读取空闲资源块链表中的Next Addr字段,当空闲资源块链表中第五空闲资源块的Next Addr大于Addr的值,且第五空闲资源块的Next Addr与Addr的值之差最小时,确定第五空闲资源块为与存储资源块右相邻的空闲资源块;当空闲资源块链表中的第六空闲资源块的Next Addr小于Addr的值,且第六空闲资源块的Next Addr与Addr的值之差最小时,确定第六空闲资源块为与存储资源块左相邻的空闲资源块。In the embodiment of the present invention, the memory reads the LEN field and the Addr field of the storage resource block, and then sequentially reads the Next Addr field in the free resource block linked list, when the Next Addr of the fifth free resource block in the free resource block linked list is greater than the value of Addr value, and the value difference between Next Addr and Addr of the fifth free resource block is the smallest, determine that the fifth free resource block is the free resource block right adjacent to the storage resource block; when the sixth free resource in the free resource block linked list When the Next Addr of the block is less than the value of Addr, and the difference between the value of Next Addr and Addr of the sixth idle resource block is the smallest, determine that the sixth idle resource block is an idle resource block left adjacent to the storage resource block.
本发明实施例中,存储资源块插入存储器的空闲块链表之后,存储资源块的NextAddr字段值的为第六空闲资源块的Next Addr字段的值,第六空闲资源块的Next Addr字段的值为存储资源块的Addr字段的值。In the embodiment of the present invention, after the storage resource block is inserted into the free block linked list of the memory, the value of the NextAddr field of the storage resource block is the value of the Next Addr field of the sixth free resource block, and the value of the Next Addr field of the sixth free resource block is Stores the value of the Addr field of the resource block.
本发明实施例中,当存储器遍历到最后一块空闲资源块没有找到大于Addr的空闲资源块时,表征存储资源块为空闲资源块链表中的最后一块空闲资源块,此时,存储资源块的Next Addr的值为Null,最后一块空闲资源块的Next Addr的值为存储资源块的Addr的值。In the embodiment of the present invention, when the memory traverses to the last free resource block and does not find a free resource block greater than Addr, it indicates that the storage resource block is the last free resource block in the free resource block linked list. At this time, the Next of the storage resource block The value of Addr is Null, and the value of Next Addr of the last free resource block is the value of Addr of the storage resource block.
示例性的,如图9所示,空闲资源块链表中有L1和L3两个空闲资源块,现在要将L2资源块回收至L1和L3之间的地址,回收之后,L2资源块的Next Addr的值为L1空闲资源块的Next Addr的值,L1空闲资源块的Next Addr的值更改为L2资源块的Addr的值。Exemplarily, as shown in Figure 9, there are two free resource blocks L1 and L3 in the free resource block linked list, and now the L2 resource block should be reclaimed to an address between L1 and L3, after reclaiming, the Next Addr of the L2 resource block The value of is the value of the Next Addr of the L1 idle resource block, and the value of the Next Addr of the L1 idle resource block is changed to the value of the Addr of the L2 resource block.
S203、根据第一地址信息和第二地址信息对存储资源块与第四空闲资源块进行判定,第四空闲资源块为空闲资源块中与存储资源块相邻的空闲资源块,第二地址信息为第四空闲资源块的地址信息。S203. Determine the storage resource block and the fourth idle resource block according to the first address information and the second address information, the fourth idle resource block is an idle resource block adjacent to the storage resource block among the idle resource blocks, and the second address information is the address information of the fourth free resource block.
当存储资源块在空闲资源块链表中找到与其相邻的空闲资源块时,就要判断该存储资源块是否与其相邻的空闲资源块连续了。When the storage resource block finds its adjacent free resource block in the free resource block linked list, it is necessary to judge whether the storage resource block is continuous with its adjacent free resource block.
本发明实施例中,存储器分别确定与存储资源块右相邻的第五空闲资源块,和与存储资源块左相邻的第六空闲资源块之后,分别获取存储资源块的Addr字段和LEN字段的值、第五空闲资源块和第六空闲资源块的Next Addr字段和LEN字段的值。In the embodiment of the present invention, after the memory determines the fifth free resource block adjacent to the right of the storage resource block and the sixth free resource block adjacent to the left of the storage resource block, respectively, the Addr field and the LEN field of the storage resource block are respectively obtained value of , the values of the Next Addr field and the LEN field of the fifth free resource block and the sixth free resource block.
本发明实施例中,存储器通过比较存储资源块的Addr字段和LEN字段值之和与第六空闲资源块的Next Addr的值来判定存储资源块与第五空闲资源块是否连续;存储器通过比较存储资源块的Addr字段与指向第六空闲资源块地址的Next Addr的值和第六空闲资源块LEN字段的值来判定存储资源块与第六空闲资源块是否连续。In the embodiment of the present invention, the memory determines whether the storage resource block is continuous with the fifth free resource block by comparing the sum of the Addr field and the LEN field value of the storage resource block with the value of Next Addr of the sixth free resource block; The Addr field of the resource block, the value of the Next Addr pointing to the address of the sixth free resource block, and the value of the LEN field of the sixth free resource block determine whether the storage resource block is continuous with the sixth free resource block.
S204、当判定第四空闲资源块与存储资源块连续时,将第四空闲资源块与存储资源块合并成新的第四空闲资源块。S204. When it is determined that the fourth idle resource block is continuous with the storage resource block, combine the fourth idle resource block and the storage resource block into a new fourth idle resource block.
存储器对第一地址信息和第二地址信息进行比较,并根据比较结果判定是否将存储资源块和第四空闲资源块合并成为新的第四空闲资源块。The memory compares the first address information with the second address information, and determines whether to combine the storage resource block and the fourth free resource block into a new fourth free resource block according to the comparison result.
本发明实施例中,当存储资源块的Addr字段和LEN字段值之和与第六空闲资源块的Next Addr的值连续时,表征第五空闲资源块与存储资源块连续,此时,将存储资源块的LEN字段的值更改为存储资源块的LEN值与第五空闲资源块的LEN值之和,存储资源块的Next Addr字段的值更改为第五空闲资源块的Next Addr值,而第六空闲资源块的NextAddr字段的值更改为存储资源块的Addr值;当存储资源块的Addr字段和LEN字段值之和与第六空闲资源块的Next Addr的值不连续时,表征第五空闲资源块与存储资源块不连续,此时,将存储资源块的Addr字段的值作为新的第六空闲资源块的Next Addr字段的值,将第六空闲资源块的Next Addr字段的值作为存储资源块的Next Addr的值。In the embodiment of the present invention, when the sum of the Addr field and the LEN field value of the storage resource block is continuous with the Next Addr value of the sixth free resource block, it indicates that the fifth free resource block is continuous with the storage resource block, and at this time, the storage The value of the LEN field of the resource block is changed to the sum of the LEN value of the storage resource block and the LEN value of the fifth free resource block, the value of the Next Addr field of the storage resource block is changed to the Next Addr value of the fifth free resource block, and The value of the NextAddr field of the six free resource blocks is changed to the Addr value of the storage resource block; when the sum of the Addr field and the LEN field value of the storage resource block is not continuous with the value of the Next Addr of the sixth free resource block, it represents the fifth free resource block The resource block is not continuous with the storage resource block. At this time, the value of the Addr field of the storage resource block is used as the value of the Next Addr field of the new sixth free resource block, and the value of the Next Addr field of the sixth free resource block is used as the value of the storage resource block. The value of the Next Addr of the resource block.
示例性的,如图10所示,空闲资源块链表中有L1、L3两块空闲资源块和L2存储资源块,其中L2在L1和L3之间且L2和L3连续,此时,将L2和L3合并成L2’,合并后的空闲资源块L2’可分配空间的长度为L2+L3,L2’的Next Addr的值为L3的Next Addr值,LI空闲资源块的Next Addr的值为L2的Addr值。Exemplarily, as shown in FIG. 10 , there are two free resource blocks L1 and L3 in the free resource block linked list and L2 storage resource blocks, wherein L2 is between L1 and L3 and L2 and L3 are continuous. At this time, L2 and L3 is merged into L2', the length of the allocated space of the merged idle resource block L2' is L2+L3, the Next Addr value of L2' is the Next Addr value of L3, and the Next Addr value of the LI idle resource block is L2 Addr value.
本发明实施例中,当存储资源块的Addr字段与指向第六空闲资源块的Next Addr的值和第六空闲资源块的LEN字段值之和连续时,表征第六空闲资源块与存储资源块连续,此时,将第六空闲资源块的LEN字段的值更改为存储资源块的LEN值与第六空闲资源块的LEN值之和;当存储资源块的Addr字段与指向第六空闲资源块的Next Addr的值和第六空闲资源块的LEN字段值之和不连续式时,表征第六空闲资源块与存储资源块不连续,此时,将存储资源块的Addr字段的值作为新的第六空闲资源块的Next Addr字段的值,将第六空闲资源块的Next Addr字段的值作为存储资源块的Next Addr的值。In the embodiment of the present invention, when the Addr field of the storage resource block is continuous with the value of the Next Addr pointing to the sixth free resource block and the value of the LEN field of the sixth free resource block, it indicates that the sixth free resource block and the storage resource block Continuously, at this time, change the value of the LEN field of the sixth free resource block to the sum of the LEN value of the storage resource block and the LEN value of the sixth free resource block; when the Addr field of the storage resource block points to the sixth free resource block When the value of the Next Addr and the value of the LEN field of the sixth free resource block are discontinuous, it indicates that the sixth free resource block is not continuous with the storage resource block. At this time, the value of the Addr field of the storage resource block is used as the new For the value of the Next Addr field of the sixth free resource block, the value of the Next Addr field of the sixth free resource block is used as the value of the Next Addr of the stored resource block.
示例性的,如图11所示,空闲资源块链表中有L1、L2两块空闲资源块和L存储资源块,其中L在L2之后且L2和L连续,此时,将L2和L合并成L2’,合并后的空闲资源块L2’可分配空间的长度为L2+L。Exemplarily, as shown in Figure 11, there are two free resource blocks L1 and L2 and L storage resource blocks in the free resource block linked list, where L is after L2 and L2 and L are continuous, at this time, L2 and L are combined into L2', the length of the allocated space of the combined idle resource block L2' is L2+L.
可以理解的是,在对已分配的资源块回收至空闲资源块链表时,将与该以分配的资源块相邻的空闲资源块合并成一个新的空闲资源块,能够减少空间碎片,降低资源浪费。It can be understood that when the allocated resource blocks are recycled to the free resource block linked list, the free resource blocks adjacent to the allocated resource blocks are merged into a new free resource block, which can reduce space fragmentation and reduce resources. waste.
需要说明的是,本发明实施例提供的一种适用于对已分配的资源块回收至存储器的资源处理方法也可以单独使用。It should be noted that a resource processing method suitable for reclaiming allocated resource blocks to memory provided by the embodiment of the present invention may also be used independently.
实施例三Embodiment Three
本发明实施例提供一种资源处理方法,如图12所示,该方法可以包括:An embodiment of the present invention provides a resource processing method, as shown in FIG. 12 , the method may include:
S301、在终端传输第一数据时,存储器获取第一数据的第一空间长度。S301. When the terminal transmits the first data, the memory acquires a first space length of the first data.
本发明实施例提供的一种资源处理方法适用于对存储器中的空闲资源进行分配操作的场景下。A resource processing method provided by an embodiment of the present invention is applicable to a scenario in which idle resources in a memory are allocated.
本发明实施例中,当终端进行第一数据的传输时,终端操作系统需要调用存储器中的空闲资源来存储第一数据,此时,存储器获取到第一数据所需要占用的第一空间长度。In the embodiment of the present invention, when the terminal transmits the first data, the terminal operating system needs to call idle resources in the memory to store the first data, and at this time, the memory obtains the first space length required by the first data.
本发明实施例中,存储器中的空闲资源块是以空闲块链表的形式进行存储的,如图3所示,每一个空闲资源块由3个部分组成,分别是LEN、Next Addr和Space,其中,LEN表示的是该空闲资源块可用空间的长度,LEN的值为Next Addr和Space的值之和、Next Addr表示的是下一块空闲资源块的起始地址,且当该空闲资源块为最后一块空闲资源块时,该值为NULL、Space表示的是该空闲资源块实际可以分配的长度。In the embodiment of the present invention, the free resource blocks in the memory are stored in the form of a free block linked list, as shown in Figure 3, each free resource block is composed of three parts, namely LEN, Next Addr and Space, wherein , LEN represents the length of the available space of the free resource block, the value of LEN is the sum of the values of Next Addr and Space, Next Addr represents the starting address of the next free resource block, and when the free resource block is the last For a free resource block, the value is NULL, and Space indicates the length that the free resource block can actually allocate.
本发明实施例中,LEN和Next Addr所占用的长度可以根据存储器大小来设定。In the embodiment of the present invention, the length occupied by LEN and Next Addr can be set according to the memory size.
示例性的,设定LEN和Next Addr所占用的长度和为2个字节时,可以管理64K大小的空闲资源块;设定LEN和Next Addr所占用的长度和为4个字节时,可以管理4G大小的空闲资源块。Exemplarily, when the length sum occupied by LEN and Next Addr is set to 2 bytes, a 64K free resource block can be managed; when the length sum occupied by LEN and Next Addr is set to 4 bytes, it can Manage free resource blocks of 4G size.
S302、存储器将第一空间长度与预设空间长度进行对比。S302. The memory compares the first space length with a preset space length.
当存储器获取到第一数据的第一空间长度之后,就要判定该第一空间长度是否小于存储器设定的最小长度了。After the memory acquires the first space length of the first data, it is necessary to determine whether the first space length is smaller than the minimum length set by the memory.
本发明实施例中存储器中设定有预设空间长度,当存储器获取到终端需要占用第一空间时,存储器将第一空间长度与预设空间长度进行对比,判断第一空间长度是否大于等于预设空间长度。In the embodiment of the present invention, a preset space length is set in the memory. When the memory acquires that the terminal needs to occupy the first space, the memory compares the first space length with the preset space length to determine whether the first space length is greater than or equal to the preset space length. Set the length of the space.
S303、当第一空间长度小于预设空间长度时,存储器从起始空闲资源块开始遍历空闲资源块。S303. When the first space length is less than the preset space length, the memory traverses the free resource blocks from the initial free resource block.
当第一空间长度小于预设空间长度时,表征终端向存储器申请的空间长度没有达到存储器的最低申请标准,此时,存储器会分配给终端预设空间长度,预设空间长度为存储器分配空间长度的最低标准。When the first space length is less than the preset space length, it means that the space length applied by the terminal to the memory does not meet the minimum application standard of the memory. At this time, the memory will be allocated to the terminal with a preset space length, and the preset space length is the memory allocation space length minimum standard.
本发明实施例中,当终端申请的第一空间长度小于存储器设定的最低标准时,存储器按照分配空间长度的最低标准预设空间长度来遍历空闲资源块链表,从中确定合适的待分配空闲资源块。In the embodiment of the present invention, when the first space length requested by the terminal is less than the minimum standard set by the memory, the memory traverses the free resource block linked list according to the minimum standard preset space length of the allocated space length, and determines a suitable free resource block to be allocated. .
S304、存储器将预设空间长度与当前空闲资源块的第二空间长度进行比较,当前空闲资源块为空闲资源块中的一个空闲资源块。S304. The memory compares the preset space length with the second space length of the current free resource block, where the current free resource block is one of the free resource blocks.
存储器根据预设空间长度与空闲资源块链表中各个空闲资源块的第二空间长度来确定待分配的第一空闲资源块。The memory determines the first free resource block to be allocated according to the preset space length and the second space length of each free resource block in the free resource block linked list.
本发明实施例中,存储器将预设空间块长度依次与空闲资源块链表中的当前空闲资源块的第二空间长度进行比较,从而确定合适的待分配空闲资源块。In the embodiment of the present invention, the memory sequentially compares the length of the preset space block with the second space length of the current free resource block in the free resource block linked list, so as to determine a suitable free resource block to be allocated.
S305、存储器确定第二空间长度大于预设空间长度的当前空闲资源块为第一空闲资源块。S305. The memory determines that the current idle resource block whose second space length is greater than the preset space length is the first idle resource block.
当第二空间长度大于预设空间长度时,存储器将第二空间长度对应的当前空闲资源块作为第一空闲资源块,以供存储器向终端分配存储空间。When the second space length is greater than the preset space length, the memory uses the current free resource block corresponding to the second space length as the first free resource block for the memory to allocate storage space to the terminal.
本发明实施例中,当第二空间长度大于预设空间长度时,表征第二空间长度对应的当前空闲资源块满足待分配空闲资源块的要求,此时,将满足待分配空闲资源块的当前空闲资源块确定为第一空闲资源块,以供存储器从第一空闲资源块中分配存储空间。In the embodiment of the present invention, when the second space length is greater than the preset space length, it indicates that the current idle resource block corresponding to the second space length meets the requirements of the idle resource block to be allocated. At this time, the current idle resource block to be allocated will meet the The idle resource block is determined as the first idle resource block for the memory to allocate storage space from the first idle resource block.
可以理解的是,将第一空间长度与存储器设置的预设空间长度进行比较,避免申请存储器分配的空间长度小于存储器设置的预设空间长度,可以有效的避免空间碎片的生成。It can be understood that by comparing the first space length with the preset space length set by the memory, it is avoided that the space length applied for memory allocation is smaller than the preset space length set by the memory, which can effectively avoid the generation of space fragments.
S306、当存储器遍历到终止空闲资源块都没有确定第一空闲资源块时,结束将预设空间长度与当前空闲资源块的第二空间长度进行比较的流程并返回确定失败指令。S306. When the first free resource block is not determined after traversing the memory until the end of the free resource block, end the process of comparing the preset space length with the second space length of the current free resource block and return the determination failure instruction.
当存储器遍历到终止空闲资源块都没有确定第一空闲资源块时,说明该空闲资源块链表中没有大于预设空间长度的空闲资源块,不能进行分配存储空间至终端的流程,此时需要结束该流程并返回确定失败指令。When the first free resource block is not determined after memory traversal to the end of the free resource block, it means that there is no free resource block larger than the preset space length in the free resource block linked list, and the process of allocating storage space to the terminal cannot be performed, and it needs to end at this time The process returns a confirm failure command.
本发明实施例中,存储器从空闲资源块链表的起始空闲资源块的空间长度开始依次与预设空间长度进行比较,当直到空闲资源块链表的终止空闲资源块的空间长度小于与预设空间长度时,表征该空闲资源块链表中没有大于预设空间长度的空闲资源块,此时结束将预设空间长度与当前空闲资源块的第二空间长度进行比较的流程并返回确定失败指令。In the embodiment of the present invention, the memory starts from the space length of the initial free resource block of the free resource block linked list and compares it with the preset space length sequentially. length, it indicates that there is no free resource block larger than the preset space length in the free resource block linked list. At this point, the process of comparing the preset space length with the second space length of the current free resource block is ended and the determination failure instruction is returned.
S307、当两个预设空间长度之和,大于第一空闲资源块的长度时,存储器确定第一空闲资源块为第一数据的存储资源块。S307. When the sum of the two preset space lengths is greater than the length of the first free resource block, the memory determines the first free resource block as a resource block for storing the first data.
当存储器确定了待分配的第一空闲资源块时,就要将第一空闲资源块按照存储器确定的分配空间长度进行分离,并将分离后的待分配空间资源块分配给第一数据存储资源了。When the memory determines the first free resource block to be allocated, the first free resource block is separated according to the allocated space length determined by the memory, and the separated space resource block to be allocated is allocated to the first data storage resource .
本发明实施例中,存储器需要判断除预设空间长度外第一空闲资源块的剩余长度是否大于预设空间长度。In the embodiment of the present invention, the memory needs to determine whether the remaining length of the first idle resource block is greater than the preset space length except for the preset space length.
本发明实施例中,当两个预设空间长度之和大于第一空闲资源块的长度时,表征除预设空间长度外第一空闲资源块的剩余长度小于预设空间长度,此时,存储器将整个第一空闲资源块分配给第一数据进行资源存储。In the embodiment of the present invention, when the sum of the two preset space lengths is greater than the length of the first free resource block, it indicates that the remaining length of the first free resource block other than the preset space length is less than the preset space length. At this time, the memory Allocating the entire first free resource block to the first data for resource storage.
示例性的,如图6所示,存储器中有L1、L2、L3三块空闲资源块,以链表的形式进行存储,当存储器将L2空闲资源块全部分配给第一数据进行存储操作时,L2空闲资源块中的Next Addr的值更改为L3空闲资源块中的Next Addr的值,L1空闲资源块中的Next Addr的值更改为L2空闲资源块中的Next Addr的值,将L2空闲资源块分配给第一数据。Exemplarily, as shown in FIG. 6, there are three free resource blocks L1, L2, and L3 in the memory, which are stored in the form of a linked list. When the memory allocates all the free resource blocks of L2 to the first data for storage operations, L2 The value of Next Addr in the free resource block is changed to the value of Next Addr in the L3 free resource block, the value of Next Addr in the L1 free resource block is changed to the value of Next Addr in the L2 free resource block, and the L2 free resource block assigned to the first data.
S308、当两个预设空间长度之和,小于等于第一空闲资源块的长度时,存储器从第一空闲资源块中确定长度为预设空间长度的待分配资源块,待分配资源块为第一数据的存储资源块。S308. When the sum of the two preset space lengths is less than or equal to the length of the first free resource block, the memory determines a resource block to be allocated whose length is the preset space length from the first free resource block, and the resource block to be allocated is the first A data storage resource block.
当存储器确定了待分配的第一空闲资源块时,就要将第一空闲资源块按照存储器确定的分配空间长度进行分离,并将分离后的待分配空间资源块分配给第一数据存储资源了。When the memory determines the first free resource block to be allocated, the first free resource block is separated according to the allocated space length determined by the memory, and the separated space resource block to be allocated is allocated to the first data storage resource .
本发明实施例中,当两个预设空间长度之和小于第一空闲资源块的长度时,表征除预设空间长度外第一空闲资源块的剩余长度大于预设空间长度,此时,存储器将第一空闲资源块从尾部截取预设空间长度的空闲资源块并分配给第一数据进行数据的存储。In the embodiment of the present invention, when the sum of the two preset space lengths is less than the length of the first free resource block, it indicates that the remaining length of the first free resource block other than the preset space length is greater than the preset space length. At this time, the memory The first idle resource block is intercepted from the tail and the idle resource block with a preset space length is allocated to the first data for data storage.
本发明实施例中,已分配的资源块的数据结构如图4所示,由LEN和Addr两个部分组成,其中,LEN用来指出分配空间的实际长度,LEN的值为Next Addr和Space的值之和、Addr用来记录已分配资源块的起始地址。In the embodiment of the present invention, the data structure of the allocated resource block is as shown in Figure 4, and consists of two parts, LEN and Addr, wherein, LEN is used to indicate the actual length of the allocated space, and the value of LEN is the value of Next Addr and Space The sum of the values, Addr is used to record the starting address of the allocated resource block.
本发明实施例中,第一空闲资源块从尾部截取指定空间长度的空闲资源块,如图5所示,空闲资源块在分配前由LEN1、Next Addr和Space三个部分组成,当需要从该空闲资源块中分配长度为64K资源时,从Space尾部截取64K作为分配的资源,在分配的资源前再分配2个字节存储该资源的LEN2和Addr,此时空闲资源块的LEN1值更改为LEN1’,其中,LEN1’的值为LEN1的值与64k之差。In the embodiment of the present invention, the first idle resource block intercepts an idle resource block with a specified space length from the tail. As shown in FIG. 5, the idle resource block is composed of LEN1, Next Addr and Space before allocation. When a resource with a length of 64K is allocated in the free resource block, 64K is intercepted from the end of Space as the allocated resource, and 2 bytes are allocated before the allocated resource to store the LEN2 and Addr of the resource. At this time, the value of LEN1 of the free resource block is changed to LEN1', where the value of LEN1' is the difference between the value of LEN1 and 64k.
示例性的,如图7所示,存储器中有L1、L2两块空闲资源块,以链表的形式进行存储,当存储器将L2空闲资源块中的部分空闲资源块分配给第一数据进行存储操作时,L2空闲资源块从尾部截取根据第一数据确定的指定空闲资源块L3,在L3资源块前添加表征L3资源块的长度和地址的L3和Addr字段,此时,L2空闲资源块中表征可分配长度的L2值更改为L2’,其中,L2’的值为L2值与L3值之差。Exemplarily, as shown in FIG. 7, there are two free resource blocks L1 and L2 in the memory, which are stored in the form of a linked list. When the memory allocates part of the free resource blocks in the L2 free resource blocks to the first data storage operation , the L2 idle resource block intercepts the specified idle resource block L3 determined according to the first data from the end, and adds the L3 and Addr fields representing the length and address of the L3 resource block before the L3 resource block. At this time, the L2 idle resource block represents The L2 value of the assignable length is changed to L2', where the value of L2' is the difference between the L2 value and the L3 value.
需要说明的是,S307和S308为并列的两个步骤,具体的根据两个预设长度之和与第一空闲资源块的空间长度的比较结果来执行相应的步骤,本发明实施例不做具体限定。It should be noted that S307 and S308 are two parallel steps, and the corresponding steps are specifically performed according to the comparison result of the sum of the two preset lengths and the space length of the first free resource block, which will not be described in detail in this embodiment of the present invention. limited.
S309、当第一空间长度大于等于预设空间长度时,存储器按照第一空间长度遍历空闲资源块,确定第一空闲资源块,第一空闲资源块大于等于第一空间长度。S309. When the first space length is greater than or equal to the preset space length, the memory traverses the free resource blocks according to the first space length to determine the first free resource block, and the first free resource block is greater than or equal to the first space length.
当第一空间长度大于预设空间长度时,表征终端向存储器申请的空间长度达到了存储器的最低申请标准,此时,存储器会分配给终端第一空间长度,预设空间长度为存储器分配空间长度的最低标准。When the first space length is greater than the preset space length, it means that the space length applied by the terminal to the memory has reached the minimum application standard of the memory. At this time, the memory will be allocated to the terminal for the first space length, and the preset space length is the memory allocation space length minimum standard.
本发明实施例中,当终端申请的第一空间长度大于存储器设定的最低标准时,存储器按照第一空间长度来遍历空闲资源块链表,依次将第一空间长度与空闲资源块链表中的第二空间长度进行比较,当第二空间长度大于第一空间长度时,将第二空间长度对应的当前空闲资源块确定为第一空闲资源块,以供存储器从第一空闲资源块中分配存储空间至终端。In the embodiment of the present invention, when the first space length requested by the terminal is greater than the minimum standard set by the memory, the memory traverses the free resource block linked list according to the first space length, and sequentially combines the first space length with the second space in the free resource block linked list. Space lengths are compared, when the second space length is greater than the first space length, the current free resource block corresponding to the second space length is determined as the first free resource block, so that the memory can allocate storage space from the first free resource block to terminal.
需要说明的是,S303和S309为S302之后的两个并列的步骤,具体的根据判定条件执行,本发明实施例不做具体限定。It should be noted that S303 and S309 are two parallel steps after S302, which are specifically executed according to the determination conditions, and are not specifically limited in this embodiment of the present invention.
S310、当第一空间长度与预设空间长度之和,大于第一空闲资源块的长度时,存储器确定第一空闲资源块为第一数据的存储资源块。S310. When the sum of the first space length and the preset space length is greater than the length of the first free resource block, the memory determines that the first free resource block is a resource block for storing the first data.
当存储器确定了待分配的第一空闲资源块时,就要将第一空闲资源块按照存储器确定的分配空间长度进行分离,并将分离后的待分配空间资源块分配给第一数据存储资源了。When the memory determines the first free resource block to be allocated, the first free resource block is separated according to the allocated space length determined by the memory, and the separated space resource block to be allocated is allocated to the first data storage resource .
本发明实施例中,存储器需要判断除第一空间长度外第一空闲资源块的剩余长度是否大于预设空间长度。In the embodiment of the present invention, the memory needs to judge whether the remaining length of the first idle resource block is greater than the preset space length except for the first space length.
本发明实施例中,当预设空间长度与第一空间长度之和大于第一空闲资源块的长度时,表征除第一空间长度外第一空闲资源块的剩余长度小于预设空间长度,此时,存储器将整个第一空闲资源块分配给第一数据进行资源存储。In the embodiment of the present invention, when the sum of the preset space length and the first space length is greater than the length of the first free resource block, it means that the remaining length of the first free resource block other than the first space length is less than the preset space length. , the memory allocates the entire first free resource block to the first data for resource storage.
S311、当第一空间长度与预设空间长度之和,小于等于第一空闲资源块的长度时,存储器从第一空闲资源块中确定长度为预设空间长度的待分配资源块,待分配资源块为第一数据的存储资源块。S311. When the sum of the first space length and the preset space length is less than or equal to the length of the first free resource block, the memory determines resource blocks to be allocated whose length is the preset space length from the first free resource block, and resources to be allocated The block is a storage resource block of the first data.
当存储器确定了待分配的第一空闲资源块时,就要将第一空闲资源块按照存储器确定的分配空间长度进行分离,并将分离后的待分配空间资源块分配给第一数据存储资源了。When the memory determines the first free resource block to be allocated, the first free resource block is separated according to the allocated space length determined by the memory, and the separated space resource block to be allocated is allocated to the first data storage resource .
本发明实施例中,当预设空间长度与第一空间长度之和小于第一空闲资源块的长度时,表征除第一空间长度外第一空闲资源块的剩余长度大于预设空间长度,此时,存储器将第一空闲资源块从尾部截取第一空间长度的空闲资源块并分配给第一数据进行数据的存储。In the embodiment of the present invention, when the sum of the preset space length and the first space length is less than the length of the first free resource block, it indicates that the remaining length of the first free resource block other than the first space length is greater than the preset space length. , the memory intercepts the idle resource block of the first space length from the end of the first idle resource block and allocates it to the first data for data storage.
本发明实施例中,存储器从所述第一空闲资源块的终止地址开始,读取长度为第一空间长度的第二空闲资源块,该第二空闲资源块属于第一空闲资源块,并将第二空闲资源块确定为待分配资源块。In the embodiment of the present invention, the memory starts from the end address of the first free resource block, reads the second free resource block whose length is the first space length, the second free resource block belongs to the first free resource block, and reads The second idle resource block is determined as the resource block to be allocated.
需要说明的是,S310和S311为两个并列的步骤,具体的根据预设长度与第一空间长度之和与第二空间长度的比较结果来执行相应的步骤,本发明实施例不做具体限定。It should be noted that S310 and S311 are two parallel steps, and the corresponding steps are specifically executed according to the comparison result of the sum of the preset length, the first space length, and the second space length, which is not specifically limited in this embodiment of the present invention. .
可以理解的是,在对空闲资源块分配给第一数据时,保证分配出去的第一空闲资源块和剩余的空闲资源块不小于存储器预设的最小值,这样,能够减少在资源分配时,产生较小的空间资源块,导致不能继续进行之后的资源分配,能够减少空间碎片,降低资源浪费。It can be understood that when allocating the free resource blocks to the first data, it is ensured that the allocated first free resource blocks and the remaining free resource blocks are not less than the minimum value preset by the memory, thus reducing resource allocation. Smaller space resource blocks are generated, resulting in the impossibility of subsequent resource allocation, which can reduce space debris and waste of resources.
实施例四Embodiment four
基于实施例三,如图13所示,本发明实施例提供还提供一种资源回收方法,在步骤S309之后,具体步骤包括:Based on the third embodiment, as shown in FIG. 13 , the embodiment of the present invention also provides a resource recycling method. After step S309, the specific steps include:
S401、在终端传输完成第一数据时,存储器释放第一数据,并获取释放完第一数据的存储资源块,存储资源块包括第一地址信息。S401. When the terminal finishes transmitting the first data, the memory releases the first data, and acquires a storage resource block that has released the first data, where the storage resource block includes first address information.
本发明实施例提供的一种资源处理方法适用于对已分配的资源块回收至存储器的场景下。A resource processing method provided by an embodiment of the present invention is applicable to a scenario where allocated resource blocks are reclaimed to a memory.
本发明实施例中,终端对第一数据传输完成时,终端就要将存储第一数据相关信息的存储资源块进行释放,此时,存储器获取到该存储资源块,并将该存储资源块回收至空闲资源块链表的指定地址处。In the embodiment of the present invention, when the terminal completes the transmission of the first data, the terminal will release the storage resource block storing the relevant information of the first data. At this time, the memory acquires the storage resource block and recycles the storage resource block to the specified address of the free resource block linked list.
本发明实施例中,已分配的资源块的数据结构如图4所示,由LEN和Addr两个部分组成,其中,LEN用来指出分配空间的实际长度,LEN的值为Next Addr和Space的值之和、Addr用来记录已分配资源块的起始地址。In the embodiment of the present invention, the data structure of the allocated resource block is as shown in Figure 4, and consists of two parts, LEN and Addr, wherein, LEN is used to indicate the actual length of the allocated space, and the value of LEN is the value of Next Addr and Space The sum of the values, Addr is used to record the starting address of the allocated resource block.
S402、存储器从起始空闲资源块开始遍历空闲资源块。S402. The memory traverses the free resource blocks from the initial free resource block.
存储器在获取到存储资源块的第一地址信息之后,就要在空闲资源块链表中确定与存储资源块相邻的空闲资源块了。After the memory acquires the first address information of the storage resource block, it needs to determine the free resource block adjacent to the storage resource block in the free resource block linked list.
本发明实施例中,存储器从空闲资源块链表的起始空闲资源块开始寻找与存储资源块相邻的空闲资源块。In the embodiment of the present invention, the memory searches for an idle resource block adjacent to the storage resource block from the initial idle resource block of the idle resource block linked list.
S403、存储器将第一地址信息与当前空闲资源块的第二地址信息进行比较。S403. The memory compares the first address information with the second address information of the current free resource block.
存储器通过比较第一地址信息与空闲资源块链表中的当前空闲资源块的第二地址信息来确定与存储资源块相邻的空闲资源块。The memory determines the free resource block adjacent to the storage resource block by comparing the first address information with the second address information of the current free resource block in the free resource block linked list.
本发明实施例中,存储资源块包括Addr字段表示的第一地址信息和LEN字段表示的第三空间长度。In the embodiment of the present invention, the storage resource block includes the first address information represented by the Addr field and the third space length represented by the LEN field.
本发明实施例中,存储器读取存储资源块的LEN字段和Addr字段,然后依次与空闲资源块链表中的Next Addr字段进行比较。In the embodiment of the present invention, the memory reads the LEN field and the Addr field of the storage resource block, and then compares them with the Next Addr field in the free resource block linked list in sequence.
S404、当存储器确定第二地址信息大于第一地址信息,且第二地址信息与第一地址信息之差最小时,将当前空闲资源块确定为第五空闲资源块,第五空闲资源块为与存储资源块右相邻的第四空闲资源块,第四空闲资源块为空闲资源块中与存储资源块相邻的空闲资源块。S404. When the memory determines that the second address information is greater than the first address information, and the difference between the second address information and the first address information is the smallest, determine the current free resource block as the fifth free resource block, and the fifth free resource block is the same as A fourth idle resource block adjacent to the right of the storage resource block is stored, and the fourth idle resource block is an idle resource block adjacent to the storage resource block among the idle resource blocks.
存储器根据第一地址信息与第二地址信息之间的关系确定与存储资源块右相邻的第五空闲资源块。The memory determines a fifth free resource block right adjacent to the storage resource block according to the relationship between the first address information and the second address information.
本发明实施例中,当第五空闲资源块的Next Addr的值大于Addr的值,且第五空闲资源块的Next Addr与Addr的值之差最小时,确定第五空闲资源块为与存储资源块右相邻的空闲资源块。In the embodiment of the present invention, when the value of Next Addr of the fifth idle resource block is greater than the value of Addr, and the difference between the values of Next Addr and Addr of the fifth idle resource block is the smallest, it is determined that the fifth idle resource block is the same as the storage resource Free resource blocks adjacent to the right of the block.
S405、当存储器确定第二地址信息小于第一地址信息,且第二地址信息与第一地址信息之差最小时,将当前空闲资源块确定为第六空闲资源块,第六空闲资源块为与存储资源块左相邻的第四空闲资源块。S405. When the memory determines that the second address information is smaller than the first address information, and the difference between the second address information and the first address information is the smallest, determine the current free resource block as the sixth free resource block, and the sixth free resource block is the same as The fourth free resource block adjacent to the left of the resource block is stored.
存储器根据第一地址信息与第二地址信息之间的关系确定与存储资源块右相邻的第五空闲资源块。The memory determines a fifth free resource block right adjacent to the storage resource block according to the relationship between the first address information and the second address information.
本发明实施例中,当空闲资源块链表中的第六空闲资源块的Next Addr小于Addr的值,且第六空闲资源块的Next Addr与Addr的值之差最小时,确定第六空闲资源块为与存储资源块左相邻的空闲资源块。In the embodiment of the present invention, when the Next Addr of the sixth idle resource block in the idle resource block linked list is less than the value of Addr, and the difference between the values of Next Addr and Addr of the sixth idle resource block is the smallest, determine the sixth idle resource block is the free resource block left adjacent to the storage resource block.
本发明实施例中,存储资源块插入存储器的空闲块链表之后,存储资源块的NextAddr字段值的为第六空闲资源块的Next Addr字段的值,第六空闲资源块的Next Addr字段的值为存储资源块的Addr字段的值。In the embodiment of the present invention, after the storage resource block is inserted into the free block linked list of the memory, the value of the NextAddr field of the storage resource block is the value of the Next Addr field of the sixth free resource block, and the value of the Next Addr field of the sixth free resource block is Stores the value of the Addr field of the resource block.
示例性的,如图9所示,空闲资源块链表中有L1和L3两个空闲资源块,现在要将L2资源块回收至L1和L3之间的地址,回收之后,L2资源块的Next Addr的值为L1空闲资源块的Next Addr的值,L1空闲资源块的Next Addr的值更改为L2资源块的Addr的值。Exemplarily, as shown in Figure 9, there are two free resource blocks L1 and L3 in the free resource block linked list, and now the L2 resource block should be reclaimed to an address between L1 and L3, after reclaiming, the Next Addr of the L2 resource block The value of is the value of the Next Addr of the L1 idle resource block, and the value of the Next Addr of the L1 idle resource block is changed to the value of the Addr of the L2 resource block.
需要说明的是,S404和S405为S403之后的两个并列的步骤,具体的根据判定条件执行相应的步骤,本发明实施例不做具体以限定。It should be noted that S404 and S405 are two parallel steps after S403, and specific corresponding steps are executed according to the determination conditions, which are not specifically limited in this embodiment of the present invention.
S406、当当前空闲资源块为终止空闲资源块,且第二地址信息小于第一地址信息时,存储器结束遍历空闲资源块的流程。S406. When the current free resource block is a terminated free resource block, and the second address information is smaller than the first address information, the memory ends the process of traversing the free resource block.
当对空闲资源块链表的终止空闲资源块比较,且没有找到大于第一地址信息的第二地址信息时,存储器遍历完成空闲资源块链表。When the terminal free resource block of the free resource block linked list is compared and no second address information greater than the first address information is found, the memory traverses the free resource block linked list.
本发明实施例中,当存储器遍历到最后一块空闲资源块没有找到大于Addr的空闲资源块时,表征存储资源块为空闲资源块链表中的最后一块空闲资源块,此时,存储资源块的Next Addr的值为Null,最后一块空闲资源块的Next Addr的值为存储资源块的Addr的值。In the embodiment of the present invention, when the memory traverses to the last free resource block and does not find a free resource block greater than Addr, it indicates that the storage resource block is the last free resource block in the free resource block linked list. At this time, the Next of the storage resource block The value of Addr is Null, and the value of Next Addr of the last free resource block is the value of Addr of the storage resource block.
S407、存储器根据第一地址信息和第二地址信息对存储资源块与第四空闲资源块进行判定,第二地址信息为第四空闲资源块的地址信息。S407. The memory determines the storage resource block and the fourth free resource block according to the first address information and the second address information, where the second address information is address information of the fourth free resource block.
存储器对第一地址信息和第二地址信息进行比较,判定存储资源块与第五空闲资源块和第六空闲资源块是否连续。The memory compares the first address information with the second address information, and determines whether the storage resource block is continuous with the fifth free resource block and the sixth free resource block.
本发明实施例中,存储器通过判断存储资源块的Addr字段和LEN字段值之和与第六空闲资源块的Next Addr的值来判断存储资源块与第五空闲资源块是否连续,存储器通过判断存储资源块的Addr字段与指向第六空闲资源块的Next Addr的值和第六空闲资源块的LEN字段值之和来判断存储资源块与第六空闲资源块是否连续。In the embodiment of the present invention, the memory judges whether the storage resource block is continuous with the fifth free resource block by judging the sum of the Addr field and the LEN field value of the storage resource block and the Next Addr value of the sixth free resource block, and the memory judges whether the storage resource block is continuous with the fifth free resource block. The sum of the Addr field of the resource block, the value of Next Addr pointing to the sixth free resource block and the value of the LEN field of the sixth free resource block is used to determine whether the storage resource block and the sixth free resource block are continuous.
S408、当存储器判定第四空闲资源块与存储资源块连续时,将第四空闲资源块与存储资源块合并成新的第四空闲资源块。S408. When the memory determines that the fourth free resource block is continuous with the storage resource block, combine the fourth free resource block and the storage resource block into a new fourth free resource block.
存储器对第一地址信息和第二地址信息进行比较,并根据比较结果判定是否将存储资源块和第四空闲资源块合并成为新的第四空闲资源块。The memory compares the first address information with the second address information, and determines whether to combine the storage resource block and the fourth free resource block into a new fourth free resource block according to the comparison result.
本发明实施例中,当存储资源块与第五空闲资源块连续时,将第五空闲资源块的空间长度与第三空间长度合并为新的第五空闲资源块的空间长度,将第一地址信息作为第一指针指向的下一个空闲资源块的地址信息,将第二指针指向的下一个空闲资源块的地址信息作为新的第三指针指向的下一个空闲资源块的地址信息,第一指针指向的下一个空闲资源块的地址信息为第六空闲资源块对应的指针信息,第二指针指向的下一个空闲资源块的地址信息为存储资源块对应的指针信息,第三指针指向的下一个空闲资源块的地址信息为第五空闲资源块对应的指针信息。In the embodiment of the present invention, when the storage resource block is continuous with the fifth idle resource block, the space length of the fifth idle resource block and the third space length are combined into a new space length of the fifth idle resource block, and the first address The information is used as the address information of the next free resource block pointed to by the first pointer, and the address information of the next free resource block pointed to by the second pointer is used as the address information of the next free resource block pointed to by the new third pointer, and the first pointer The address information of the next free resource block pointed to is the pointer information corresponding to the sixth free resource block, the address information of the next free resource block pointed to by the second pointer is the pointer information corresponding to the storage resource block, and the next The address information of the idle resource block is pointer information corresponding to the fifth idle resource block.
本发明实施例中,当存储资源块与第六空闲资源块连续时,将第六空闲资源块的空间长度与第二空间长度合并为新的第六空闲资源块的空间长度。In the embodiment of the present invention, when the storage resource block is continuous with the sixth free resource block, the space length of the sixth free resource block and the second space length are combined into a new space length of the sixth free resource block.
本发明实施例中,当存储资源块的Addr字段和LEN字段值之和与第六空闲资源块的Next Addr的值连续时,表征第五空闲资源块与存储资源块连续,此时,将存储资源块的LEN字段的值更改为存储资源块的LEN值与第五空闲资源块的LEN值之和,存储资源块的Next Addr字段的值更改为第五空闲资源块的Next Addr值,而第六空闲资源块的NextAddr字段的值更改为存储资源块的Addr值;当存储资源块的Addr字段和LEN字段值之和与第六空闲资源块的Next Addr的值不连续时,表征第五空闲资源块与存储资源块不连续,此时,将存储资源块的Addr字段的值作为新的第六空闲资源块的Next Addr字段的值,将第六空闲资源块的Next Addr字段的值作为存储资源块的Next Addr的值。In the embodiment of the present invention, when the sum of the Addr field and the LEN field value of the storage resource block is continuous with the Next Addr value of the sixth free resource block, it indicates that the fifth free resource block is continuous with the storage resource block, and at this time, the storage The value of the LEN field of the resource block is changed to the sum of the LEN value of the storage resource block and the LEN value of the fifth free resource block, the value of the Next Addr field of the storage resource block is changed to the Next Addr value of the fifth free resource block, and The value of the NextAddr field of the six free resource blocks is changed to the Addr value of the storage resource block; when the sum of the Addr field and the LEN field value of the storage resource block is not continuous with the value of the Next Addr of the sixth free resource block, it represents the fifth free resource block The resource block is not continuous with the storage resource block. At this time, the value of the Addr field of the storage resource block is used as the value of the Next Addr field of the new sixth free resource block, and the value of the Next Addr field of the sixth free resource block is used as the value of the storage resource block. The value of the Next Addr of the resource block.
示例性的,如图10所示,空闲资源块链表中有L1、L3两块空闲资源块和L2存储资源块,其中L2在L1和L3之间且L2和L3连续,此时,将L2和L3合并成L2’,合并后的空闲资源块L2’可分配空间的长度为L2+L3,L2’的Next Addr的值为L3的Next Addr值,LI空闲资源块的Next Addr的值为L2的Addr值。Exemplarily, as shown in FIG. 10 , there are two free resource blocks L1 and L3 in the free resource block linked list and L2 storage resource blocks, wherein L2 is between L1 and L3 and L2 and L3 are continuous. At this time, L2 and L3 is merged into L2', the length of the allocated space of the merged idle resource block L2' is L2+L3, the Next Addr value of L2' is the Next Addr value of L3, and the Next Addr value of the LI idle resource block is L2 Addr value.
本发明实施例中,当存储资源块的Addr字段与指向第六空闲资源块的Next Addr的值和第六空闲资源块的LEN字段值之和连续时,表征第六空闲资源块与存储资源块连续,此时,将第六空闲资源块的LEN字段的值更改为存储资源块的LEN值与第六空闲资源块的LEN值之和;当存储资源块的Addr字段与指向第六空闲资源块的Next Addr的值和第六空闲资源块的LEN字段值之和不连续式时,表征第六空闲资源块与存储资源块不连续,此时,将存储资源块的Addr字段的值作为新的第六空闲资源块的Next Addr字段的值,将第六空闲资源块的Next Addr字段的值作为存储资源块的Next Addr的值。In the embodiment of the present invention, when the Addr field of the storage resource block is continuous with the value of the Next Addr pointing to the sixth free resource block and the value of the LEN field of the sixth free resource block, it indicates that the sixth free resource block and the storage resource block Continuously, at this time, change the value of the LEN field of the sixth free resource block to the sum of the LEN value of the storage resource block and the LEN value of the sixth free resource block; when the Addr field of the storage resource block points to the sixth free resource block When the value of the Next Addr and the value of the LEN field of the sixth free resource block are discontinuous, it indicates that the sixth free resource block is not continuous with the storage resource block. At this time, the value of the Addr field of the storage resource block is used as the new For the value of the Next Addr field of the sixth free resource block, the value of the Next Addr field of the sixth free resource block is used as the value of the Next Addr of the stored resource block.
示例性的,如图11所示,空闲资源块链表中有L1、L2两块空闲资源块和L存储资源块,其中L在L2之后且L2和L连续,此时,将L2和L合并成L2’,合并后的空闲资源块L2’可分配空间的长度为L2+L。Exemplarily, as shown in Figure 11, there are two free resource blocks L1 and L2 and L storage resource blocks in the free resource block linked list, where L is after L2 and L2 and L are continuous, at this time, L2 and L are combined into L2', the length of the allocated space of the combined idle resource block L2' is L2+L.
进一步地,当存储器判定存储空闲资源块与第五空闲资源块和第六空闲资源块都不连续时,将第一地址信息作为第一指针指向的下一个空闲资源块的地址信息,将第一指针指向的下一个空闲资源块的地址信息作为第三指针指向的下一个空闲资源块的地址信息。Further, when the memory determines that the storage of the free resource block is not continuous with the fifth free resource block and the sixth free resource block, the first address information is used as the address information of the next free resource block pointed to by the first pointer, and the first The address information of the next free resource block pointed to by the pointer is used as the address information of the next free resource block pointed to by the third pointer.
需要说明的是,本发明实施例提供的一种适用于对已分配的资源块回收至存储器的资源处理方法也可以单独使用。It should be noted that a resource processing method suitable for reclaiming allocated resource blocks to memory provided by the embodiment of the present invention may also be used independently.
可以理解的是,在存储器分配空闲资源时,设置一个最小值,使得分配的空闲资源长度和剩余的空闲资源长度不小于该最小值,避免了长度过小的碎片的产生,能够减少空间碎片,降低资源浪费。It can be understood that when the memory allocates idle resources, a minimum value is set, so that the length of the allocated idle resources and the length of the remaining idle resources are not less than the minimum value, avoiding the generation of fragments with too small lengths, and reducing space fragmentation. Reduce waste of resources.
实施例五Embodiment five
如图14所示,本发明实施例提供一种终端1,给终端1可以包括:As shown in Figure 14, the embodiment of the present invention provides a terminal 1, which may include:
获取单元10,用于在传输第一数据时,获取所述第一数据的第一空间长度。The acquiring unit 10 is configured to acquire a first space length of the first data when transmitting the first data.
对比单元11,用于将所述第一空间长度与预设空间长度进行对比。A comparing unit 11, configured to compare the first space length with a preset space length.
确定单元12,用于当所述第一空间长度小于所述预设空间长度时,按照所述预设空间长度遍历空闲资源块,确定第一空闲资源块,所述第一空闲资源块大于等于所述预设空间长度。The determining unit 12 is configured to traverse the idle resource blocks according to the preset space length when the first space length is less than the preset space length, and determine a first idle resource block, and the first idle resource block is greater than or equal to The preset space length.
分配单元13,用于采用所述第一空闲资源块给所述第一数据分配存储资源。The allocating unit 13 is configured to allocate storage resources to the first data by using the first free resource block.
可选的,所述确定单元12,还用于当所述第一空间长度大于等于所述预设空间长度时,按照所述第一空间长度遍历所述空闲资源块,确定所述第一空闲资源块,所述第一空闲资源块大于等于所述第一空间长度。Optionally, the determining unit 12 is further configured to traverse the free resource blocks according to the first space length to determine the first free resource block when the first space length is greater than or equal to the preset space length. A resource block, the first idle resource block is greater than or equal to the first space length.
可选的,所述确定单元12,还用于当两个所述预设空间长度之和,大于所述第一空闲资源块的长度时,确定所述第一空闲资源块为所述第一数据的存储资源块;当两个所述预设空间长度之和,小于等于所述第一空闲资源块的长度时,从所述第一空闲资源块中确定长度为所述预设空间长度的待分配资源块,所述待分配资源块为所述第一数据的存储资源块。Optionally, the determining unit 12 is further configured to determine that the first idle resource block is the first idle resource block when the sum of the two preset space lengths is greater than the length of the first idle resource block. A data storage resource block; when the sum of the two preset space lengths is less than or equal to the length of the first free resource block, determine from the first free resource block the length of the preset space length A resource block to be allocated is a resource block for storing the first data.
可选的,所述确定单元12,还用于当所述第一空间长度与所述预设空间长度之和,大于所述第一空闲资源块的长度时,确定所述第一空闲资源块为所述第一数据的存储资源块;当所述第一空间长度与所述预设空间长度之和,小于等于所述第一空闲资源块的长度时,从所述第一空闲资源块中确定长度为所述第一空间长度的待分配资源块,所述待分配资源块为所述第一数据的存储资源块。Optionally, the determining unit 12 is further configured to determine the first idle resource block when the sum of the first space length and the preset space length is greater than the length of the first idle resource block storage resource block for the first data; when the sum of the first space length and the preset space length is less than or equal to the length of the first free resource block, select from the first free resource block Determine a resource block to be allocated whose length is the first space length, and the resource block to be allocated is a resource block for storing the first data.
可选的,基于图14如图15所示,该终端1还包括:读取单元14。Optionally, as shown in FIG. 15 based on FIG. 14 , the terminal 1 further includes: a reading unit 14 .
所述读取单元14,用于在所述第一空闲资源块中,从所述第一空闲资源块的终止地址开始,读取长度为所述预设空间长度的第二空闲资源块,所述第二空闲资源块属于所述第一空闲资源块。The reading unit 14 is configured to, in the first free resource block, start from the termination address of the first free resource block, and read a second free resource block whose length is the preset space length, so The second idle resource block belongs to the first idle resource block.
所述确定单元12,还用于将所述第二空闲资源块确定为所述待分配资源块。The determining unit 12 is further configured to determine the second idle resource block as the resource block to be allocated.
可选的,所述读取单元14,还用于在所述第一空闲资源块中,从所述第一空闲资源块的终止地址开始,读取长度为所述第一空间长度的第三空闲资源块,所述第三空闲资源块属于所述第一空闲资源块。Optionally, the reading unit 14 is further configured to, in the first free resource block, start from the end address of the first free resource block, and read the third resource block whose length is the first space length. An idle resource block, the third idle resource block belongs to the first idle resource block.
所述确定单元12,还用于将所述第三空闲资源块确定为所述待分配资源块。The determining unit 12 is further configured to determine the third idle resource block as the resource block to be allocated.
可选的,基于图14如图16所示,该终端1还包括:遍历单元15和比较单元16。Optionally, as shown in FIG. 16 based on FIG. 14 , the terminal 1 further includes: a traversing unit 15 and a comparing unit 16 .
所述遍历单元15,用于从起始空闲资源块开始遍历所述空闲资源块。The traversing unit 15 is configured to start traversing the idle resource block from the initial idle resource block.
所述比较单元16,用于将所述预设空间长度与当前空闲资源块的第二空间长度进行比较,所述当前空闲资源块为所述空闲资源块中的一个空闲资源块。The comparing unit 16 is configured to compare the preset space length with a second space length of a current free resource block, where the current free resource block is one of the free resource blocks.
所述确定单元12,具体用于直至确定所述第二空间长度大于所述预设空间长度的所述当前空闲资源块为所述第一空闲资源块。The determining unit 12 is specifically configured to determine that the current idle resource block whose second space length is greater than the preset space length is the first idle resource block.
可选的,基于图16如图17所示,该终端1还包括:结束单元17。Optionally, as shown in FIG. 17 based on FIG. 16 , the terminal 1 further includes: an end unit 17 .
所述结束单元17,用于当遍历到终止空闲资源块都没有确定所述第一空闲资源块时,结束将所述预设空间长度与所述当前空闲资源块的所述第二空间长度进行比较的流程并返回确定失败指令。The end unit 17 is configured to end the comparison between the preset space length and the second space length of the current free resource block when the first free resource block is not determined after traversing to the end of the free resource block. The flow of the comparison and returns the OK failure instruction.
可选的,基于图14如图18所示,该终端1还包括:判定单元18和合并单元19。Optionally, as shown in FIG. 18 based on FIG. 14 , the terminal 1 further includes: a determining unit 18 and a combining unit 19 .
所述获取单元10,还用于在传输完成所述第一数据时,释放所述第一数据,并获取释放完所述第一数据的存储资源块,所述存储资源块包括第一地址信息。The obtaining unit 10 is further configured to release the first data when the transmission of the first data is completed, and obtain a storage resource block that has released the first data, and the storage resource block includes first address information .
所述确定单元12,还用于根据所述第一地址信息和预设判定条件,遍历所述空闲资源块,确定与所述存储资源块相邻的第四空闲资源块。The determining unit 12 is further configured to traverse the free resource blocks according to the first address information and preset determination conditions, and determine a fourth free resource block adjacent to the storage resource block.
所述判定单元18,用于根据所述第一地址信息和第二地址信息对所述存储资源块与所述第四空闲资源块进行判定,所述第四空闲资源块为所述空闲资源块中与所述存储资源块相邻的空闲资源块,所述第二地址信息为所述第四空闲资源块的地址信息。The determination unit 18 is configured to determine the storage resource block and the fourth idle resource block according to the first address information and the second address information, and the fourth idle resource block is the idle resource block In the idle resource block adjacent to the storage resource block, the second address information is the address information of the fourth idle resource block.
所述合并单元19,用于当判定所述第四空闲资源块与所述存储资源块连续时,将所述第四空闲资源块与所述存储资源块合并成新的所述第四空闲资源块。The combining unit 19 is configured to combine the fourth idle resource block and the storage resource block into a new fourth idle resource when it is determined that the fourth idle resource block is continuous with the storage resource block piece.
可选的,所述遍历单元15,还用于从起始空闲资源块开始遍历所述空闲资源块。Optionally, the traversing unit 15 is further configured to start traversing the idle resource block from the initial idle resource block.
所述比较单元16,还将所述第一地址信息与所述当前空闲资源块的第二地址信息进行比较。The comparing unit 16 also compares the first address information with the second address information of the current idle resource block.
所述确定单元12,还用于当确定所述第二地址信息大于所述第一地址信息,且所述第二地址信息与所述第一地址信息之差最小时,将当前空闲资源块确定为第五空闲资源块,所述第五空闲资源块为与所述存储资源块右相邻的所述第四空闲资源块;当确定所述第二地址信息小于所述第一地址信息,且所述第二地址信息与所述第一地址信息之差最小时,将当前空闲资源块确定为第六空闲资源块,所述第六空闲资源块为与所述存储资源块左相邻的所述第四空闲资源块。The determining unit 12 is further configured to determine the current idle resource block when it is determined that the second address information is greater than the first address information and the difference between the second address information and the first address information is the smallest. is the fifth idle resource block, the fifth idle resource block is the fourth idle resource block right adjacent to the storage resource block; when it is determined that the second address information is smaller than the first address information, and When the difference between the second address information and the first address information is the smallest, determine the current idle resource block as the sixth idle resource block, and the sixth idle resource block is all left adjacent to the storage resource block The fourth free resource block.
可选的,所述结束单元17,用于当所述当前空闲资源块为终止空闲资源块,且所述第二地址信息小于所述第一地址信息时,结束遍历所述空闲资源块的流程。Optionally, the ending unit 17 is configured to end the process of traversing the idle resource block when the current idle resource block is a terminated idle resource block and the second address information is smaller than the first address information .
可选的,所述合并单元19,具体用于当所述存储资源块与所述第五空闲资源块连续时,将所述第五空闲资源块的空间长度与所述第三空间长度合并为新的第五空闲资源块的空间长度,将所述第一地址信息作为第一指针指向的下一个空闲资源块的地址信息,将第二指针指向的下一个空闲资源块的地址信息作为新的第三指针指向的下一个空闲资源块的地址信息,所述第一指针指向的下一个空闲资源块的地址信息为所述第六空闲资源块对应的指针信息,所述第二指针指向的下一个空闲资源块的地址信息为所述存储资源块对应的指针信息,所述第三指针指向的下一个空闲资源块的地址信息为所述第五空闲资源块对应的指针信息;当所述存储资源块与所述第六空闲资源块连续时,将第六空闲资源块的空间长度与所述第二空间长度合并为新的第六空闲资源块的空间长度。Optionally, the combining unit 19 is specifically configured to combine the space length of the fifth free resource block and the third space length into The space length of the new fifth free resource block, using the first address information as the address information of the next free resource block pointed to by the first pointer, and using the address information of the next free resource block pointed to by the second pointer as the new The address information of the next free resource block pointed to by the third pointer, the address information of the next free resource block pointed to by the first pointer is the pointer information corresponding to the sixth free resource block, and the next free resource block pointed to by the second pointer The address information of a free resource block is the pointer information corresponding to the storage resource block, and the address information of the next free resource block pointed to by the third pointer is the pointer information corresponding to the fifth free resource block; when the storage When the resource block is continuous with the sixth idle resource block, combining the space length of the sixth idle resource block and the second space length into a new space length of the sixth idle resource block.
可选的,基于图18如图19所示,所述终端1还包括:更新单元110。Optionally, as shown in FIG. 19 based on FIG. 18 , the terminal 1 further includes: an updating unit 110 .
所述更新单元111,用于当判定所述存储空闲资源块与所述第五空闲资源块和所述第六空闲资源块都不连续时,将所述第一地址信息作为所述第一指针指向的下一个空闲资源块的地址信息,将所述第一指针指向的下一个空闲资源块的地址信息作为所述第三指针指向的下一个空闲资源块的地址信息。The updating unit 111 is configured to use the first address information as the first pointer when it is determined that the stored free resource block is not continuous with the fifth free resource block and the sixth free resource block The address information of the next free resource block pointed to by the first pointer is used as the address information of the next free resource block pointed to by the third pointer.
如图20所示,在实际应用中,上述获取单元10、对比单元11、确定单元12、分配单元13、读取单元14、遍历单元15、比较单元16、结束单元17、判定单元18、合并单元19和更新单元110都可由位于服务器1上的处理器111实现,可以为中央处理器(CPU)、微处理器(MPU)、数字信号处理器(DSP)或现场可编程门阵列(FPGA)等实现,,该服务器还可以包括存储器112实现,该存储器112可以与处理器111连接,其中,存储器112用于存储可执行程序代码,该程序代码包括计算机操作指令,存储器112可能包含高速RAM存储器,也可能还包括非易失性存储器,例如,至少一个磁盘存储器。As shown in Figure 20, in practical applications, the above acquisition unit 10, comparison unit 11, determination unit 12, allocation unit 13, reading unit 14, traversal unit 15, comparison unit 16, end unit 17, determination unit 18, merge Both the unit 19 and the update unit 110 can be realized by a processor 111 located on the server 1, which can be a central processing unit (CPU), a microprocessor (MPU), a digital signal processor (DSP) or a field programmable gate array (FPGA) etc., the server may also include a memory 112, which may be connected to the processor 111, wherein the memory 112 is used to store executable program codes, the program codes include computer operation instructions, and the memory 112 may include a high-speed RAM memory , and may also include non-volatile memory, eg, at least one disk memory.
本领域内的技术人员应明白,本发明的实施例可提供为方法、系统、或计算机程序产品。因此,本发明可采用硬件实施例、软件实施例、或结合软件和硬件方面的实施例的形式。而且,本发明可采用在一个或多个其中包含有计算机可用程序代码的计算机可用存储介质(包括但不限于磁盘存储器和光学存储器等)上实施的计算机程序产品的形式。Those skilled in the art should understand that the embodiments of the present invention may be provided as methods, systems, or computer program products. Accordingly, the present invention can take the form of a hardware embodiment, a software embodiment, or an embodiment combining software and hardware aspects. Furthermore, the present invention may take the form of a computer program product embodied on one or more computer-usable storage media (including but not limited to disk storage and optical storage, etc.) having computer-usable program code embodied therein.
本发明是参照根据本发明实施例的方法、设备(系统)、和计算机程序产品的流程图和/或方框图来描述的。应理解可由计算机程序指令实现流程图和/或方框图中的每一流程和/或方框、以及流程图和/或方框图中的流程和/或方框的结合。可提供这些计算机程序指令到通用计算机、专用计算机、嵌入式处理机或其他可编程数据处理设备的处理器以产生一个机器,使得通过计算机或其他可编程数据处理设备的处理器执行的指令产生用于实现在流程图一个流程或多个流程和/或方框图一个方框或多个方框中指定的功能的装置。The present invention is described with reference to flowchart illustrations and/or block diagrams of methods, apparatus (systems), and computer program products according to embodiments of the invention. It should be understood that each procedure and/or block in the flowchart and/or block diagram, and a combination of procedures and/or blocks in the flowchart and/or block diagram can be realized by computer program instructions. These computer program instructions may be provided to a general purpose computer, special purpose computer, embedded processor, or processor of other programmable data processing equipment to produce a machine such that the instructions executed by the processor of the computer or other programmable data processing equipment produce a An apparatus for realizing the functions specified in one or more procedures of the flowchart and/or one or more blocks of the block diagram.
这些计算机程序指令也可存储在能引导计算机或其他可编程数据处理设备以特定方式工作的计算机可读存储器中,使得存储在该计算机可读存储器中的指令产生包括指令装置的制造品,该指令装置实现在流程图一个流程或多个流程和/或方框图一个方框或多个方框中指定的功能。These computer program instructions may also be stored in a computer-readable memory capable of directing a computer or other programmable data processing apparatus to operate in a specific manner, such that the instructions stored in the computer-readable memory produce an article of manufacture comprising instruction means, the instructions The device realizes the function specified in one or more procedures of the flowchart and/or one or more blocks of the block diagram.
这些计算机程序指令也可装载到计算机或其他可编程数据处理设备上,使得在计算机或其他可编程设备上执行一系列操作步骤以产生计算机实现的处理,从而在计算机或其他可编程设备上执行的指令提供用于实现在流程图一个流程或多个流程和/或方框图一个方框或多个方框中指定的功能的步骤。These computer program instructions can also be loaded onto a computer or other programmable data processing device, causing a series of operational steps to be performed on the computer or other programmable device to produce a computer-implemented process, thereby The instructions provide steps for implementing the functions specified in the flow chart or blocks of the flowchart and/or the block or blocks of the block diagrams.
以上所述,仅为本发明的较佳实施例而已,并非用于限定本发明的保护范围。The above descriptions are only preferred embodiments of the present invention, and are not intended to limit the protection scope of the present invention.
Claims (26)
Priority Applications (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN201611044955.2A CN108108307A (en) | 2016-11-24 | 2016-11-24 | A kind of method for processing resource and terminal |
Applications Claiming Priority (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN201611044955.2A CN108108307A (en) | 2016-11-24 | 2016-11-24 | A kind of method for processing resource and terminal |
Publications (1)
Publication Number | Publication Date |
---|---|
CN108108307A true CN108108307A (en) | 2018-06-01 |
Family
ID=62203798
Family Applications (1)
Application Number | Title | Priority Date | Filing Date |
---|---|---|---|
CN201611044955.2A Pending CN108108307A (en) | 2016-11-24 | 2016-11-24 | A kind of method for processing resource and terminal |
Country Status (1)
Country | Link |
---|---|
CN (1) | CN108108307A (en) |
Cited By (1)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN110069215A (en) * | 2019-03-27 | 2019-07-30 | 浙江宇视科技有限公司 | A kind of method and device of the dynamic adjustment storage unit based on block storage |
Citations (20)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
US5652864A (en) * | 1994-09-23 | 1997-07-29 | Ibm | Concurrent storage allocations or returns without need to lock free storage chain |
US5784698A (en) * | 1995-12-05 | 1998-07-21 | International Business Machines Corporation | Dynamic memory allocation that enalbes efficient use of buffer pool memory segments |
CN1320863A (en) * | 2000-06-13 | 2001-11-07 | 深圳市中兴通讯股份有限公司 | Method for quick memory allocation and release in real-time system |
CN1963788A (en) * | 2005-11-08 | 2007-05-16 | 中兴通讯股份有限公司 | A managing method for EMS memory |
CN101281491A (en) * | 2008-05-12 | 2008-10-08 | 北京邮电大学 | VxWorks-based Memory Module and Management Method of Space Robot Central Processor |
CN101329655A (en) * | 2008-07-31 | 2008-12-24 | 北京天碁科技有限公司 | Memory management method and device |
CN101470665A (en) * | 2007-12-27 | 2009-07-01 | Tcl集团股份有限公司 | Method and system for internal memory management of application system without MMU platform |
CN101630992A (en) * | 2008-07-14 | 2010-01-20 | 中兴通讯股份有限公司 | Method for managing shared memory |
CN101853210A (en) * | 2010-05-25 | 2010-10-06 | 惠州华阳通用电子有限公司 | Memory management method and device |
CN102156675A (en) * | 2010-02-12 | 2011-08-17 | 中兴通讯股份有限公司 | Method and device for allocating memory |
CN102866954A (en) * | 2012-08-31 | 2013-01-09 | 华为技术有限公司 | Method and device for allocating internal memory |
CN102915276A (en) * | 2012-09-25 | 2013-02-06 | 武汉邮电科学研究院 | Memory control method for embedded systems |
CN103150257A (en) * | 2013-02-28 | 2013-06-12 | 天脉聚源(北京)传媒科技有限公司 | Memory management method and memory management device |
CN103186469A (en) * | 2011-12-27 | 2013-07-03 | 上海博泰悦臻电子设备制造有限公司 | Memory allocation method and device, and memory recovery method |
CN103389947A (en) * | 2013-08-06 | 2013-11-13 | 哈尔滨工业大学 | Memory manager and management method for VxWorks-based photo-etching machine double workpiece table control system |
CN103699497A (en) * | 2013-12-19 | 2014-04-02 | 京信通信系统(中国)有限公司 | Cache allocation method and device |
CN104182356A (en) * | 2014-09-19 | 2014-12-03 | 深圳市茁壮网络股份有限公司 | Memory management method and device and terminal device |
CN104199781A (en) * | 2014-08-14 | 2014-12-10 | 深圳百科信息技术有限公司 | Memory fragment allocation method and device based on shared memory |
CN105302738A (en) * | 2015-12-09 | 2016-02-03 | 北京东土科技股份有限公司 | Method and device for distributing memory |
CN105404590A (en) * | 2015-11-10 | 2016-03-16 | 浪潮(北京)电子信息产业有限公司 | Memory block combination method and system |
-
2016
- 2016-11-24 CN CN201611044955.2A patent/CN108108307A/en active Pending
Patent Citations (20)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
US5652864A (en) * | 1994-09-23 | 1997-07-29 | Ibm | Concurrent storage allocations or returns without need to lock free storage chain |
US5784698A (en) * | 1995-12-05 | 1998-07-21 | International Business Machines Corporation | Dynamic memory allocation that enalbes efficient use of buffer pool memory segments |
CN1320863A (en) * | 2000-06-13 | 2001-11-07 | 深圳市中兴通讯股份有限公司 | Method for quick memory allocation and release in real-time system |
CN1963788A (en) * | 2005-11-08 | 2007-05-16 | 中兴通讯股份有限公司 | A managing method for EMS memory |
CN101470665A (en) * | 2007-12-27 | 2009-07-01 | Tcl集团股份有限公司 | Method and system for internal memory management of application system without MMU platform |
CN101281491A (en) * | 2008-05-12 | 2008-10-08 | 北京邮电大学 | VxWorks-based Memory Module and Management Method of Space Robot Central Processor |
CN101630992A (en) * | 2008-07-14 | 2010-01-20 | 中兴通讯股份有限公司 | Method for managing shared memory |
CN101329655A (en) * | 2008-07-31 | 2008-12-24 | 北京天碁科技有限公司 | Memory management method and device |
CN102156675A (en) * | 2010-02-12 | 2011-08-17 | 中兴通讯股份有限公司 | Method and device for allocating memory |
CN101853210A (en) * | 2010-05-25 | 2010-10-06 | 惠州华阳通用电子有限公司 | Memory management method and device |
CN103186469A (en) * | 2011-12-27 | 2013-07-03 | 上海博泰悦臻电子设备制造有限公司 | Memory allocation method and device, and memory recovery method |
CN102866954A (en) * | 2012-08-31 | 2013-01-09 | 华为技术有限公司 | Method and device for allocating internal memory |
CN102915276A (en) * | 2012-09-25 | 2013-02-06 | 武汉邮电科学研究院 | Memory control method for embedded systems |
CN103150257A (en) * | 2013-02-28 | 2013-06-12 | 天脉聚源(北京)传媒科技有限公司 | Memory management method and memory management device |
CN103389947A (en) * | 2013-08-06 | 2013-11-13 | 哈尔滨工业大学 | Memory manager and management method for VxWorks-based photo-etching machine double workpiece table control system |
CN103699497A (en) * | 2013-12-19 | 2014-04-02 | 京信通信系统(中国)有限公司 | Cache allocation method and device |
CN104199781A (en) * | 2014-08-14 | 2014-12-10 | 深圳百科信息技术有限公司 | Memory fragment allocation method and device based on shared memory |
CN104182356A (en) * | 2014-09-19 | 2014-12-03 | 深圳市茁壮网络股份有限公司 | Memory management method and device and terminal device |
CN105404590A (en) * | 2015-11-10 | 2016-03-16 | 浪潮(北京)电子信息产业有限公司 | Memory block combination method and system |
CN105302738A (en) * | 2015-12-09 | 2016-02-03 | 北京东土科技股份有限公司 | Method and device for distributing memory |
Non-Patent Citations (2)
Title |
---|
M. MASMANO ET AL.: ""Implementation of a constant-time dynamic storage allocator"", 《SOFTWARE-PRACTICE&EXPERIENCE》 * |
李彦峰等: ""VxWorks实时操作系统内存分配算法优化"", 《电子世界》 * |
Cited By (1)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN110069215A (en) * | 2019-03-27 | 2019-07-30 | 浙江宇视科技有限公司 | A kind of method and device of the dynamic adjustment storage unit based on block storage |
Similar Documents
Publication | Publication Date | Title |
---|---|---|
KR101791594B1 (en) | Management of storage in a storage network | |
CN108681565B (en) | Block chain data parallel processing method, device, equipment and storage medium | |
US9009273B2 (en) | Address server | |
CN107368260A (en) | Memory space method for sorting, apparatus and system based on distributed system | |
US11126607B1 (en) | Memory-aware system and method for identifying matching portions of two sets of data in a multiprocessor system | |
CN109902032B (en) | Out-of-heap memory management method, device, medium and electronic equipment | |
US10073648B2 (en) | Repartitioning data in a distributed computing system | |
US7877570B2 (en) | Consolidation of matching memory pages | |
CN106790525A (en) | File downloading method and device | |
EP3196767A1 (en) | Method for writing data into flash memory device, flash memory device and storage system | |
CN104731569A (en) | Data processing method and relevant equipment | |
KR102114245B1 (en) | Graphics state manage apparatus and method | |
CN114327917A (en) | Memory management method, computing device and readable storage medium | |
CN112685333B (en) | Heap memory management method and device | |
CN105718319A (en) | A memory pool layout analysis method and a memory pool device | |
CN112650577A (en) | Memory management method and device | |
CN111666150B (en) | Storage space allocation method and device, terminal and computer readable storage medium | |
CN108108307A (en) | A kind of method for processing resource and terminal | |
CN111414339B (en) | File processing method, system, device, equipment and medium | |
US20170255548A1 (en) | Method and system for dynamically updating data fields of buffers | |
CN112799832A (en) | A memory allocation control method, device, device and storage medium | |
CN109165305B (en) | Characteristic value storage and retrieval method and device | |
CN117331498A (en) | Method, device, equipment and medium for constructing mapping table of solid state disk | |
CN115390864A (en) | Data processing method and device in flash, readable storage medium and terminal | |
CN116955212A (en) | Methods, devices, equipment and storage media for defragmenting memory |
Legal Events
Date | Code | Title | Description |
---|---|---|---|
PB01 | Publication | ||
PB01 | Publication | ||
SE01 | Entry into force of request for substantive examination | ||
SE01 | Entry into force of request for substantive examination | ||
CB02 | Change of applicant information |
Address after: 310012 building A01, 1600 yuhangtang Road, Wuchang Street, Yuhang District, Hangzhou City, Zhejiang Province Applicant after: CHINA MOBILE (HANGZHOU) INFORMATION TECHNOLOGY Co.,Ltd. Applicant after: China Mobile Communications Corp. Address before: 310012, No. 14, building three, Chang Torch Hotel, No. 259, Wensanlu Road, Xihu District, Zhejiang, Hangzhou Applicant before: CHINA MOBILE (HANGZHOU) INFORMATION TECHNOLOGY Co.,Ltd. Applicant before: China Mobile Communications Corp. |
|
CB02 | Change of applicant information | ||
RJ01 | Rejection of invention patent application after publication |
Application publication date: 20180601 |
|
RJ01 | Rejection of invention patent application after publication |