A kind of EMS memory management process and system
Technical field
The present invention relates to computer realm, particularly relate to a kind of EMS memory management process and system.
Background technology
System is in operation to be needed to apply for frequently and releasing memory, prior art is generally called by system command and is distributed and releasing memory, but easily like this there is memory fragmentation, memory usage is low, long-time running can cause system unstable, and working time is longer, system more easily goes wrong, even more and more slower.
Summary of the invention
The technical problem to be solved in the present invention is, for the above-mentioned defect easily occurring that memory fragmentation, memory usage are low, system is unstable of prior art, provides a kind of and not easily occurs that fragment, memory usage are high, the EMS memory management process of system stability.
The technical solution adopted for the present invention to solve the technical problems is: construct a kind of EMS memory management process, comprising:
S11. in available memory block, set the size of internal storage location to be allocated according to user's request and determine the total n of the internal storage location that need distribute, and the call number of n internal storage location is set to 0 ~ n-1 successively;
S12. when initialization, the total n of the first address of call number corresponding respectively for a described n internal storage location, a described n internal storage location, internal storage location, the quantity of idle internal storage location are preserved respectively, and 0 assignment is given the call number of first idle internal storage location, n-1 assignment is given the call number of the internal storage location of last free time;
S13., after receiving Memory Allocation request, from a described n internal storage location, idle available internal storage location is distributed to user.
In EMS memory management process of the present invention, described step S13 comprises:
S131. Memory Allocation request is received;
S132. to judge in memory block whether available free internal storage location, if so, then carry out step S133; If not, then terminate;
S133. judge whether the size of idle internal storage location needs to mate with user, if so, then carries out step S134; If not, then terminate;
S134. judge whether the quantity of idle internal storage location equals 1, if so, then performs S136; If not, then step S135 is carried out;
S135. first idle internal storage location is dispensed, remove the call number of the internal storage location distributed simultaneously, and the quantity of the internal storage location of free time is successively decreased 1, and the call number assignment of next idle internal storage location is given the call number of first idle internal storage location;
S136. first idle internal storage location is dispensed, remove the call number of the internal storage location distributed simultaneously, and the quantity of the internal storage location of free time is successively decreased 1, and by end mark respectively assignment give the call number of first idle call number of internal storage location and the internal storage location of last free time.
In EMS memory management process of the present invention, after described step S13, also comprise:
S14., after receiving internal memory releasing request, successively distributed internal storage location is reclaimed.
In EMS memory management process of the present invention, in described step S14, the step that distributed internal storage location carries out reclaiming is comprised:
S141. the call number of the internal storage location discharged is calculated according to the first address of discharged internal storage location;
Whether the quantity S142. judging idle internal storage location is 0, if so, then carries out step S143; If not, then step S144 is carried out;
S143. by calculated call number respectively assignment give the call number of first idle call number of internal storage location and the internal storage location of last free time, and by the increased number 1 of the internal storage location of free time, and the call number of the call number of first idle internal storage location and last first idle internal storage location is preserved respectively;
S144. be the call number of the internal storage location of last free time by calculated call number assignment, and by the increased number 1 of the internal storage location of free time, and the call number of last first idle internal storage location is preserved.
The present invention also constructs a kind of internal storage management system, comprising:
Setting module, for setting the size of internal storage location to be allocated according to user's request and determine the total n of the internal storage location that need distribute in available memory block, and is set to 0 ~ n-1 successively by the call number of n internal storage location;
Initialization module, for when initialization, the total n of the first address of call number corresponding respectively for a described n internal storage location, a described n internal storage location, internal storage location, the quantity of idle internal storage location are preserved respectively, and 0 assignment is given the call number of first idle internal storage location, n-1 assignment is given the call number of the internal storage location of last free time;
Distribution module, for after receiving Memory Allocation request, distributes idle available internal storage location to user from a described n internal storage location.
In internal storage management system of the present invention, described distribution module comprises:
Request receiving submodule, for receiving Memory Allocation request;
Whether available free first judges submodule, for judging in memory block internal storage location;
Second judges submodule, during for internal storage location available free in memory block, judges whether the size of idle internal storage location needs to mate with user;
3rd judges submodule, when size and user for the internal storage location in the free time need to mate, judges whether the quantity of idle internal storage location equals 1;
First distribution sub module, when quantity for the internal storage location in the free time is not equal to 1, first idle internal storage location is dispensed, remove the call number of the internal storage location distributed simultaneously, and the quantity of the internal storage location of free time is successively decreased 1, and the call number assignment of next idle internal storage location is given the call number of first idle internal storage location;
Second distribution sub module, when quantity for the internal storage location in the free time equals 1, first idle internal storage location is dispensed, remove the call number of the internal storage location distributed simultaneously, and the quantity of the internal storage location of free time is successively decreased 1, and by end mark respectively assignment give the call number of first idle call number of internal storage location and the internal storage location of last free time.
In internal storage management system of the present invention, described system also comprises:
Release module, for after receiving internal memory releasing request, reclaims distributed internal storage location successively.
In internal storage management system of the present invention, described release module comprises:
Calculating sub module, for calculating the call number of the internal storage location discharged according to the first address of discharged internal storage location;
4th judges submodule, for judging whether the quantity of idle internal storage location is 0;
First release submodule, when quantity for the internal storage location in the free time is 0, by calculated call number respectively assignment give the call number of first idle call number of internal storage location and the internal storage location of last free time, and by the increased number 1 of the internal storage location of free time, and the call number of the call number of first idle internal storage location and last first idle internal storage location is preserved respectively;
Second release submodule, when quantity for the internal storage location in the free time is not 0, by the call number that calculated call number assignment is the internal storage location of last free time, and by the increased number 1 of the internal storage location of free time, and the call number of last first idle internal storage location is preserved.
Implement technical scheme of the present invention, the problem of memory fragmentation can not be produced, and there is the advantage that memory usage is high, system stability, dispensing rate are fast.
Accompanying drawing explanation
Below in conjunction with drawings and Examples, the invention will be further described, in accompanying drawing:
Fig. 1 is the process flow diagram of EMS memory management process embodiment one of the present invention;
Fig. 2 is the process flow diagram of step S13 preferred embodiment in Fig. 1;
Fig. 3 is the process flow diagram of EMS memory management process embodiment two of the present invention;
Fig. 4 is the process flow diagram of step S14 preferred embodiment in Fig. 3;
Fig. 5 is the building-block of logic of internal storage management system embodiment one of the present invention;
Fig. 6 is the building-block of logic of distribution module preferred embodiment in Fig. 5;
Fig. 7 is the building-block of logic of internal storage management system embodiment two of the present invention;
The building-block of logic of release module preferred embodiment in Fig. 8 Fig. 7.
Embodiment
As shown in Figure 1, in the process flow diagram of EMS memory management process embodiment one of the present invention, first illustrate, this EMS memory management process can be applicable in data queue, chained list and other similar data structure.This EMS memory management process comprises step S11-S13, illustrates below:
In step s 11, in available memory block, set the size of internal storage location to be allocated according to user's request and determine the total n of the internal storage location that need distribute, and the call number of n internal storage location is set to 0 ~ n-1 successively.Such as, the data structure definition that memory management uses is carried out as follows:
typedefstructmempool_tag{
intsize;
intnelts;
intbounds;
intfirst;
intlast;
int*freenode;
void*freemem;
void**free_array;
}mempool;
Parameter declaration:
Size: for each internal storage location size of distributing;
Nelts: remaining can storage allocation element number;
Bonds: internal storage location total quantity;
The call number of first: the first piece of free memory unit;
Last: the call number of last block free memory unit;
Freenode: the call number of preserving whole free memory unit;
Freemem: the memory block first address distributed;
Free_array: the address of depositing each internal storage location distributed.
In step s 12, when carrying out initialization, the total n of the first address of call number corresponding respectively for a described n internal storage location, a described n internal storage location, internal storage location, the quantity of idle internal storage location are preserved respectively, and 0 assignment is given the call number of first idle internal storage location, n-1 assignment is given the call number of the internal storage location of last free time.Such as, can by stating n internal storage location call number corresponding respectively, the first address of a described n internal storage location, the total n of internal storage location, the quantity of the internal storage location of free time carries out being kept in predetermined array or buffer unit respectively.In addition, in one example in which, initialization is carried out by mode below:
for(i=0,i<n;i++)
{
Free_array [i]=freemem+i*size; //free_array preserves the first address of each internal storage location
Freenode [i]=i+1; //freenode preserves the call number of the free memory unit be not assigned with
}
First=0; // the first internal storage location call number assignment be not assigned with is 0
Last=n-1; // last internal storage location call number assignment be not assigned with is n-1
Nelts=n; // internal storage location the total quantity that is not assigned with
Bonds=n; The internal storage location total quantity split in // memory block.
In step s 13, after receiving Memory Allocation request, from a described n internal storage location, distribute idle available internal storage location to user.
Fig. 2 shows the preferred embodiment of step S13, and in this embodiment, step S13 comprises further:
S131. Memory Allocation request is received;
S132. to judge in memory block whether available free internal storage location, that is, judge whether nelts is greater than 0, if so, then carry out step S133; If not, then terminate;
S133. judge whether the size of idle internal storage location needs to mate with user, if so, then carries out step S134; If not, then terminate;
S134. judge whether the quantity of idle internal storage location equals 1, that is, judge whether nelts is 1, if so, then performs S136; If not, then step S135 is carried out;
S135. first idle internal storage location is dispensed, remove the call number of the internal storage location distributed simultaneously, the call number being about to the internal storage location distributed is removed from predetermined array or buffer unit, and by the quantity of the internal storage location of free time (such as, nelts) 1 is successively decreased, and the call number assignment of next idle internal storage location is given the call number (such as, first) of first idle internal storage location;
S136. first idle internal storage location is dispensed, remove the call number of the internal storage location distributed simultaneously, and by the quantity of the internal storage location of free time (such as, nelts) successively decrease 1, and by end mark respectively assignment give the call number of the call number (such as, first) of first idle internal storage location and the internal storage location of last free time (such as, last), then terminate, in an object lesson, end mark is-1.
Fig. 3 is the process flow diagram of EMS memory management process embodiment two of the present invention, compare the embodiment one shown in Fig. 1, this embodiment difference place is only after step s 13, this EMS memory management process also comprises step S14, after receiving internal memory releasing request, successively distributed internal storage location is reclaimed.
Fig. 4 shows the preferred embodiment of step S14, in the step S14 of this embodiment, the step that distributed internal storage location carries out reclaiming is comprised further:
S141. calculate the call number of the internal storage location discharged according to the first address of discharged internal storage location, suppose that this call number is pos;
Whether the quantity S142. judging idle internal storage location is 0, if so, then carries out step S143; If not, then step S144 is carried out;
S143. by calculated call number respectively assignment give the call number (first=pos of first idle call number of internal storage location and the internal storage location of last free time, last=pos), and by the increased number 1 of the internal storage location of free time, and the call number of the call number of first idle internal storage location and last first idle internal storage location is preserved respectively, then terminate;
S144. be the call number (last=pos) of the internal storage location of last free time by calculated call number assignment, and by the increased number 1 of the internal storage location of free time, and the call number of last first idle internal storage location is preserved, then terminate.
It should be noted that step S141-S143 illustrate only the flow process of an internal storage location release, the release principle should understanding other internal storage location is similar with it, repeats no more.
Fig. 5 is the building-block of logic of internal storage management system embodiment one of the present invention, this internal storage management system comprises the setting module 10, initialization module 20 and the distribution module 30 that connect successively, wherein, the call number of n internal storage location for setting the size of internal storage location to be allocated according to user's request and determine the total n of the internal storage location that need distribute in available memory block, and is set to 0 ~ n-1 by setting module 10 successively; Initialization module 20 is for when initialization, the total n of the first address of call number corresponding respectively for a described n internal storage location, a described n internal storage location, internal storage location, the quantity of idle internal storage location are preserved respectively, and 0 assignment is given the call number of first idle internal storage location, n-1 assignment is given the call number of the internal storage location of last free time; Distribution module 30, for after receiving Memory Allocation request, distributes idle available internal storage location to user from a described n internal storage location.
In a preferred embodiment, as shown in Figure 6, distribution module comprises request receiving submodule 31, first further and judges that submodule 32, second judges that submodule 33, the 3rd judges submodule 34, first distribution sub module 35 and the second distribution sub module 36.Wherein, request receiving submodule 31 is for receiving Memory Allocation request; First judges that whether available free submodule 32 for judging in memory block internal storage location; Second when judging submodule 33 for internal storage location available free in memory block, judges whether the size of idle internal storage location needs to mate with user; 3rd when judging that submodule 34 needs to mate with user for the size of the internal storage location in the free time, judges whether the quantity of idle internal storage location equals 1; When first distribution sub module 35 is not equal to 1 for the quantity of the internal storage location in the free time, first idle internal storage location is dispensed, remove the call number of the internal storage location distributed simultaneously, and the quantity of the internal storage location of free time is successively decreased 1, and the call number assignment of next idle internal storage location is given the call number of first idle internal storage location; When second distribution sub module 36 equals 1 for the quantity of the internal storage location in the free time, first idle internal storage location is dispensed, remove the call number of the internal storage location distributed simultaneously, and the quantity of the internal storage location of free time is successively decreased 1, and by end mark respectively assignment give the call number of first idle call number of internal storage location and the internal storage location of last free time.
Fig. 7 is the building-block of logic of internal storage management system embodiment two of the present invention, this embodiment compares the embodiment one shown in Fig. 5, this internal storage management system also comprises release module 40, and distributed internal storage location, for after receiving internal memory releasing request, reclaims by this release module 40 successively.
In a preferred embodiment, as shown in Figure 8, release module comprises calculating sub module 41, the 4th further and judges that submodule 42, first discharges submodule 43 and the second release submodule 44.Wherein, calculating sub module 41 is for calculating the call number of the internal storage location discharged according to the first address of discharged internal storage location; 4th judges that submodule 42 is for judging whether the quantity of idle internal storage location is 0; When first release submodule 43 is 0 for the quantity of the internal storage location in the free time, by calculated call number respectively assignment give the call number of first idle call number of internal storage location and the internal storage location of last free time, and by the increased number 1 of the internal storage location of free time, and the call number of the call number of first idle internal storage location and last first idle internal storage location is preserved respectively; When second release submodule 44 is not 0 for the quantity of the internal storage location in the free time, by the call number that calculated call number assignment is the internal storage location of last free time, and by the increased number 1 of the internal storage location of free time, and the call number of last first idle internal storage location is preserved.
The foregoing is only the preferred embodiments of the present invention, be not limited to the present invention, for a person skilled in the art, the present invention can have various modifications and variations.Within the spirit and principles in the present invention all, any amendment done, equivalent replacement, improvement etc., all should be included within right of the present invention.