Internal memory distributes and the method that discharges fast in a kind of real-time system
The present invention relates to a kind of calculator memory administrative skill, be specifically related to a kind of method that in real-time system, realizes memory management fast.
Real-time system requires to execute the task of defined in the time that limits.At present, this system has a wide range of applications, as process control, data acquisition, communication, multimedia messages processing etc. to the more sensitive occasion of time ratio.Weighing a real-time weak index of real-time system is that this real-time system executes the function of regulation and the length of response external asynchronous event required time, so some outstanding real-time systems all try hard to accomplish minimum on system overheads such as task context switching, system call, interrupt response, memory management.Memory management is one of most important component of real-time system, and one of them important goal of design real-time system is exactly to improve the efficient of memory management, and then improves the utilization factor of CPU.
A kind of general Memory Allocation strategy is a variable length Memory Allocation strategy, but makes in this way real-time system through after the long period operation, can produce a large amount of memory fragmentations, causes a large amount of wastes of internal memory, even causes system crash; The shortcoming that also has of this method is the extremely uncertainty of internal memory application and release, thus in the real-time system of reality general this method of less use.
In fact, in real-time system, adopt a kind of Memory Allocation strategy of regular length usually.This method is earlier internal memory to be divided into several districts, each district is divided into the identical memory block of size again then, memory block size between the different districts is unequal, and the size of general memory piece is 2 power power, i.e. 16 bytes, 32 bytes, 64 bytes ... 1024 bytes ... or the like; When each application size is the memory block of size, each district of scanning earlier always, find the district at the minimum memory block place that is not less than size, taking out a memory block then from this district returns, if the memory block in this district uses up, just return sky, rather than from other district, get bigger memory block and return, during each releasing memory, according to the pointer that is released memory block, scan each district once more, seek this memory block and belong to which district, and then after having found this district, just memory block is returned in this district.This method no longer causes memory fragmentation, but because each application all needs to scan each memory field with releasing memory, this scanning is all relatively reached jump instruction and is formed by many in realization, and memory management module is one of module that usage frequency is the highest in the real-time system, the efficient of memory management module will badly influence the efficient of whole real-time system, so just cause a large amount of wastes of cpu resource; In addition, the memory management expense of this method is also relevant with the quantity of memory field in the real-time system, particularly when the internal memory of a real-time system was divided into by 4 bytes to many different memory field that the memory block of 64K byte is formed, it will badly influence memory management module so that the efficient of whole real-time system.
Purpose of the present invention just is to provide a kind of quick EMS memory management process that solves problem in the above-mentioned EMS memory management process, this method has significantly improved the speed of internal memory application and release in the real-time system, it can be located the size of this memory block when each application and releasing memory piece fast, and do not need to scan several memory fields at every turn, carry out several times and obtain a result after relatively, thereby greatly improved the speed and the efficient of memory management in the real-time system.
Internal memory distributes and the method that discharges fast in a kind of real-time system provided by the invention, and its technical scheme is achieved in that
The method of the applied Memory Allocation of a kind of real-time system, the memory field of this real-time system, an all corresponding memory control unit, the size of this unit record memory block, pointer and quantity, wherein, make this all memory control unit form an array, label is carried out with call number n in the address of this each unit; And to set pairing this memory block size in this unit be 2
nByte the method is characterized in that,
A) reserve a pointer space at the head of this memory block, point to the address of this memory control unit;
B) with the parameter input real-time system of the size of required memory piece;
C) judge the legitimacy of this parameter;
D) if legal, seek the call number n of this address, determine pairing this control module according to this parameter.
Described memory allocation method, wherein, described step d) comprises
E) after being deducted 1, the pairing byte number in this unit converts binary number to;
F) figure place with this binary number is defined as address n.
Described memory allocation method wherein, also comprises the following steps
G) memory block of one free time of taking-up in this control module of looking for.
Described memory allocation method, wherein, the address of described control module is 16 to the maximum.
The applied internal memory releasing method of a kind of real-time system, the all corresponding memory control unit in the memory field of this real-time system, this unit record is size, pointer and the quantity of memory block wherein, wherein, this all memory control unit is formed an array, and label is carried out with call number n in the address of this each unit; And to set pairing this memory block size in this unit be 2
nByte the method is characterized in that,
H) this memory block is carried out initialization;
I) reserve a pointer space at the head of this each memory block.
Described internal memory method for releasing is wherein, at execution in step i) also comprise following processing afterwards
J) in the pointer space of this reservation, deposit and point to the pairing internal memory control of this memory block
The pointer of system unit.
Described internal memory method for releasing wherein, also comprises
K), judge the legitimacy of this memory block pointer according to the fixed offset value of depositing in this headspace;
L) if legal, the memory block of this memory block pointer indication is returned to according in the stored numerical value of this headspace this memory control unit pointed.
Because EMS memory management process provided by the invention has been saved a large amount of jump instructions that relatively reach on memory management module, in various real-time systems, not only can reduce expense, the efficient of raising system when internal memory application and release of memory management, and the quantity configuration-independent of memory management expense and memory field, therefore this real-time system can not lower efficiency because of the variation of concrete application program and load, thereby has improved the stability of system high efficiency rate work.Method provided by the invention is for the CPU of instruction with pipelining, and the raising of its performance is particularly evident.
Below in conjunction with accompanying drawing, by preferred embodiment of the present invention is described in detail, will make above-mentioned purpose of the present invention and other advantages apparent, wherein,
Fig. 1 is the data structure synoptic diagram of memory management module in the real-time system;
Fig. 2 is the process flow diagram of internal memory fast distribution method of the present invention;
Fig. 3 is the process flow diagram of internal memory snap-out release method of the present invention.
Fig. 1 has described the data structure synoptic diagram of memory management module in the real-time system.The 101st, the array that memory control unit is formed, each unit record the information such as pointer of the size of the memory block in its pairing memory field, quantity, free memory block.There is following relation: bsize=2 in memory block size bsize in control module call number n (being the unit number of control module array) the inherence district corresponding with this unit
nByte; Each memory block is also reserved a pointer space at its head, shown in 102,103 etc.; This pointed the address of its pairing memory control unit, as the pointed in memory block 102 headspaces control module 0, and the pointed in memory block 103 headspaces control module 1, and the like, as shown in Figure 1.
Fig. 2 has described the quick allocation flow figure of internal memory that utilizes the inventive method design.Beginning from step 201, internal memory application service routine is called.In step 202 the parameter imported into, i.e. the memory size that need apply for, assignment is given size.In step 203, judge the legitimacy of size, just return sky, finish to step 206 if size is an illegal value; Just forward in the step 204 if size is a legal value, in step 204, calculate the call number n of memory control unit according to the value of size, according to the number of significant digit that concerns n=binary number (size-1), this corresponding relation can be finished by a command adapted thereto in the existing cpu instruction system.In step 205, can navigate to control module according to the unit number n that previous step obtains, if the pairing memory block of this control module is not empty, then taking out a free memory block returns, otherwise just return sky, enter into step 206 end, thereby can navigate to the position of the actual memory block of obtaining fast.
Fig. 3 has described the internal memory snap-out release process flow diagram that utilizes the inventive method design.Beginning from step 301, internal memory discharge service routine and are called.In step 302,, be about to d/d memory block pointer assignment and give p1 the parameter of importing into.In step 303, oppositely pointed to the pointer p2 of memory control unit according to the fixed offset value of headspace in the memory block.In step 304, can judge the legitimacy of memory block pointer p1 according to the information of memory control unit, just jump to step 306 end if p1 is an illegal value; Just forward in the step 305 if p1 is a legal value, in step 305, be returned to p1 memory block pointed in the p2 memory control unit pointed after, finish in the step 306.
What deserves to be explained is, utilize method of the present invention, can on various platforms, develop memory management module at an easy rate, and use the memory management module flow process basically identical of the inventive method design, be on specific implementation, the instruction type difference that may use; If the compiler of higher level lanquage can't generate this instruction, just need in higher level lanquage, embed assembly language and realize.
For example, with the memory management module that method of the present invention realizes on this risc chip of Motorola PowerPC860 (hereinafter to be referred as MPC860), the raising of its efficient is just apparent in view.Because the instruction of risc chip relatively reaches jump instruction with pipelining and significantly reduced in the inventive method.On MPC860, when needing to calculate the number of bits of a number, use instruction cntlzw rA rS, the number of leading 0 among the register rS is at first taken out in this instruction, and assignment gives register rA, and then just obtained the number of bits of register rS Central Plains number with 32 values that deduct rA.Test on the MPC860 of 50MHz shows that the average operating time to internal memory application and release in the prior art is 12us, and uses the time of this method to be 2us.Compare with memory allocation method in the past, the speed of Memory Allocation of the present invention and release is significantly increased, and the raising of this efficient can finally cause the raising of the performance of whole real-time system.
Should be understood that the one of ordinary skilled in the art can make various suitable changes or replacement under the premise of not departing from the present invention, but these changes or replacement all should belong to protection scope of the present invention.