Background technology
In the embedded OS field, memory source all compares valuable usually.Yet, owing to use application and releasing memory continually, make and caused a large amount of memory fragmentations, just must reduce memory fragmentation so will make full use of memory source.
In order to reduce memory fragmentation, commonplace way is Installed System Memory to be carried out memory pool divide at present.This method is at first applied for a big internal memory, and it is divided into several region, again each district is subdivided into some memory blocks of equal size then, and the memory block in each district varies in size.Here said district is exactly a memory pool.
When the application internal memory, search for each memory pool according to using the memory size of being applied for, find contained memory block size to apply for the memory pool of memory size just greater than equaling, therefrom distribute a free memory block to return to the applicant, and be equipped with the zone bit of pass, if no free memory block just returns failure.
When releasing memory, the memory block pointer that provides according to application directly finds memory block and affiliated memory pool thereof, and is equipped with the pass zone bit.
All there is following shortcoming usually in this method: when application need application internal memory, often need to search for each memory pool according to the size of application internal memory, therefrom find sizeable memory block, normally circulation of process of search memory pool, because it is lower that efficiency ratio carried out in loop statement, so this method can have influence on the performance of system to a certain extent,, and realize complicated even used reasonable searching algorithm efficient still not high.Particularly in requiring than higher communication system to real-time, need frequent application memory block to come messaging, the application method of inefficiency is certain to have influence on the handling capacity of traffic.
Therefore, in internal memory pool managing mechanism, need a kind of method that can improve internal memory application efficient.
Summary of the invention
The invention provides a kind of method of quick-speed application EMS memory, this method may further comprise the steps: step S102, power on when carrying out initialization in system, and divide memory pool and also create corresponding management structure; Step S104 creates the memory pool concordance list according to the memory pool of being divided; And step S106, apply for internal memory according to memory pool of being divided and memory pool concordance list.
In addition, step S102 also comprises following processing: power on when carrying out initialization in system, the bulk memory of being applied for is divided into a plurality of memory pools, and each memory pool is divided into a plurality of memory blocks; A control domain is created for each memory pool by system; And with free memory block chained list of the composition of the free memory block in each memory pool.
Wherein, a plurality of memory blocks in each memory pool big or small identical, the varying in size of the memory block in the different memory pools, and the size of the memory block in the different memory pool satisfies k * 2
n, wherein, n and k are natural number.
An information header is created for each memory block by system, information header is positioned at the front of memory block, be used to write down the information of memory block, wherein, information header comprises the control domain pointer of memory block place memory pool and the information header pointer of the next free memory block in the free memory block chained list during as free memory block.
Control domain is used for the memory block in managing internal memory pond, and control domain comprises the information of the tail pointer of free memory block chained list in the head pointer of free memory block chained list in the quantity, place memory pool of the number of the size of the memory block that is comprised about the place memory pool, memory block that the place memory pool is comprised, free memory block that the place memory pool is comprised and the place memory pool.
When using the application internal memory, begin the storage allocation piece from the beginning of free memory block chained list, and the head pointer of the free memory block chained list in the control domain is revised as next free memory block pointer; The memory block pointer that will newly insert during releasing memory is appended to free memory block chained list afterbody, and the tail pointer of the free memory block chained list in the control domain is revised as the memory block pointer of new insertion.
Preferably, the memory pool concordance list is an one-dimension array, and each array element in the array is the control domain pointer.Wherein, in this array, the corresponding relation of array index index and array element element is: element=f ((index+1) * 2
n).Here, function f (x) expression application size is the control domain pointer of the affiliated memory pool of the memory block of x.
In addition, step S106 also comprises following processing: the control domain pointer that obtains the memory pool that will apply for according to predetermined operation; And judge whether to exist assignable memory block according to the information that writes down in the control domain pointer, if have then return the memory block of the head pointer indication of the free memory chained list that writes down in the control domain, and head pointer is revised as next memory block pointer, if not then return failed message.
Wherein, predetermined operation is: the memory size that will need the to apply for n position that moves to right, the array that the displacement back is obtained be as the subscript of the array of memory pool concordance list, obtains the control domain pointer of the memory pool that will apply for then according to subscript.
By of the present invention, can solve the inefficient problem of internal memory application in the internal memory pool managing mechanism.
Other features and advantages of the present invention will be set forth in the following description, and, partly from instructions, become apparent, perhaps understand by implementing the present invention.Purpose of the present invention and other advantages can realize and obtain by specifically noted structure in the instructions of being write, claims and accompanying drawing.
Embodiment
Below in conjunction with accompanying drawing the preferred embodiments of the present invention are described, should be appreciated that preferred embodiment described herein only is used for description and interpretation the present invention, and be not used in qualification the present invention.
Below, the present invention will be described in conjunction with the accompanying drawings.
Fig. 1 is the process flow diagram that illustrates according to the method for quick-speed application EMS memory of the present invention.
With reference to Fig. 1, this method may further comprise the steps: step S102, power on when carrying out initialization in system, and divide memory pool and also create corresponding management structure; Step S104 creates the memory pool concordance list according to the memory pool of being divided; And step S106, apply for internal memory according to memory pool of being divided and memory pool concordance list.
In addition, step S102 also comprises following processing: power on when carrying out initialization in system, the bulk memory of being applied for is divided into a plurality of memory pools, and each memory pool is divided into a plurality of memory blocks; A control domain is created for each memory pool by system; And with free memory block chained list of the composition of the free memory block in each memory pool.
Wherein, a plurality of memory blocks in each memory pool big or small identical, the varying in size of the memory block in the different memory pools, and the size of the memory block in the different memory pool satisfies k * 2
n, wherein, n and k are natural number.
An information header is created for each memory block by system, information header is positioned at the front of memory block, be used to write down the information of memory block, wherein, information header comprises the control domain pointer of memory block place memory pool and the information header pointer of the next free memory block in the free memory block chained list during as free memory block.
Control domain is used for the memory block in managing internal memory pond, and control domain comprises the information of the tail pointer of free memory block chained list in the head pointer of free memory block chained list in the quantity, place memory pool of the number of the size of the memory block that is comprised about the place memory pool, memory block that the place memory pool is comprised, free memory block that the place memory pool is comprised and the place memory pool.
When using the application internal memory, begin the storage allocation piece from the beginning of free memory block chained list, and the head pointer of the free memory block chained list in the control domain is revised as next free memory block pointer; The memory block pointer that will newly insert during releasing memory is appended to free memory block chained list afterbody, and the tail pointer of the free memory block chained list in the control domain is revised as the memory block pointer of new insertion.。
Preferably, the memory pool concordance list is an one-dimension array, and each array element in the array is the control domain pointer.Wherein, in this array, the corresponding relation of array index index and array element element is: element=f ((index+1) * 2
n).Here, function f (x) expression application size is the control domain pointer of the affiliated memory pool of the memory block of x.
In addition, step S106 also comprises following processing: the control domain pointer that obtains the memory pool that will apply for according to predetermined operation; And judge whether to exist assignable memory block according to the information that writes down in the control domain pointer, if have then return the memory block of the head pointer indication of the free memory chained list that writes down in the control domain, and head pointer is revised as next memory block pointer, if not then return failed message.
Wherein, predetermined operation is: the memory size that will need the to apply for n position that moves to right, the array that the displacement back is obtained be as the subscript of the array of memory pool concordance list, obtains the control domain pointer of the memory pool that will apply for then according to subscript.
Below, describe embodiments of the invention in detail in conjunction with Fig. 2 to Fig. 4.
Power on when carrying out initialization in system, partly apply for bulk memory in internal memory initialization, and it is divided into several memory pools.Comprise several equal size internal memory pieces in each memory pool again, memory block size (size) difference that is comprised in the different memory pools, but all to meet certain requirement:
size=k×2
n
Here, variable k and n are natural numbers, and variable k is used for embodying the memory block that different memory pools comprise and varies in size.The value of n is decided according to application demand, in communication system, selects 6 proper (that is: the size of memory block all must be 64 multiple in the different memory pools) usually.
The structure of memory pool as shown in the figure, has been divided 8 memory pools as shown in Figure 2 in the present embodiment, and the byte number of memory block is respectively in each memory pool: 64,128,256,512, and 1K, 2K, 4K, 8K.This shows that in the present embodiment, the n value is 6, the value of k is respectively 1,2, and 4,8,16,32,64,128.
As shown in Figure 2, when dividing memory block, system creates information header (head) for simultaneously each memory block, and the front that it is positioned at memory block is used to write down the information of memory block, comprising in have: the PoolMgt pointer of this memory pool; And during as free memory block in this memory pool the head pointer of the next free memory block in the free memory block chained list.
Then, system creates control domain (PoolMgt) for each memory pool, and it is used for the memory block in managing internal memory pond, comprising information have: the size of the contained memory block of this memory pool; The number of the contained memory block of this memory pool; The quantity of this memory pool free memory block; The head pointer of this memory pool free memory block chained list; And the tail pointer of this memory pool free memory block chained list.
Then, the free memory block in the memory pool is formed a chained list.Just from linked list head storage allocation piece, and the head pointer of the chained list among the modification PoolMgt is next free memory block during internal memory in application; Just be appended to the chained list tail during releasing memory, and the tail pointer of the chained list among the modification PoolMgt is the new memory block pointer that inserts.
Subsequently, create the memory pool concordance list according to the memory pool of being divided.Here, concordance list is an one-dimension array, and what each element was deposited in the array is the PoolMgt pointer.The corresponding relation of array index (Index) and array element (element) is:
element=f((Index+1)×2
n);
Wherein, function f (x) expression application size is the PoolMgt pointer of the affiliated memory pool of the memory block of x.
In the present embodiment, the n value is 6, then 2
nBe exactly 64.Because memory block maximum in the present embodiment is 8K (64 * 128), then the length of concordance list is exactly 128.Figure 3 illustrates the concordance list of present embodiment.
Fig. 4 is the process flow diagram that illustrates according to the application internal memory of the embodiment of the invention.When using the application memory block, earlier the memory size of the using application n position that moves to right, again the subscript of the numerical value of displacement back acquisition as the memory pool array of indexes, obtain the numerical value of array of indexes respective element then, this numerical value is exactly the PoolMgt pointer of using the memory pool under the memory block that will apply for.
In the present embodiment, the value of n is 6, if use application in save as 100 bytes, moving to right 100 6 so earlier, to obtain a temp value be 1, and again 1 subscript as array of indexes, the Ptr of acquisition points to PoolMgt2, that is the PoolMgt pointer of second memory pool.
Quantity according to this memory pool free memory block that writes down among the PoolMgt2 judges whether to exist assignable free memory block, if not then return failure, otherwise return the memory block of head pointer indication of the free memory block chained list of this memory pool that writes down among the PoolMgt2, and to upgrade this head pointer be its next memory block in idle chained list.
This shows that the present invention is by dividing the memory pool size dexterously, and in conjunction with index technology and displacement skill efficiently, realize the memory pool under the memory block of quick position application application, thereby effectively avoided the time delay brought because of the cyclic search memory pool.The present invention is simply efficient, in requiring than higher embedded OS to real-time very high using value is arranged.
Be the preferred embodiments of the present invention only below, be not limited to the present invention, for a person skilled in the art, the present invention can have various changes and variation.Within the spirit and principles in the present invention all, any modification of being done, be equal to replacement, improvement etc., all should be included within protection scope of the present invention.