Summary of the invention
The object of the embodiment of the present invention is to propose a kind of memory fragmentation distribution method based on shared drive, be intended to solve prior art message transmit with processing procedure in repeatedly copy, consume system resources, affects the problem of software systems efficiency.
The embodiment of the present invention is achieved in that a kind of memory fragmentation distribution method based on shared drive, said method comprising the steps of:
According to the long L of the message of message to be stored, from first to last travel through FREE EXTENT FRAGMENTATION IN chained list, search the free memory fragment that fragment overall length is greater than " fragment head length+L ";
Judge whether to find fragment overall length to be greater than the free memory fragment of " fragment head length+L ", if do not find fragment overall length to be greater than the free memory fragment of " fragment head length+L ", adjacent idle memory fragmentation is recombinated, return to step and " according to the long L of the message of message to be stored; from first to last travel through FREE EXTENT FRAGMENTATION IN chained list, search the free memory fragment that fragment overall length is greater than " fragment head length+L " ";
If find fragment overall length to be greater than the free memory fragment of " fragment head length+L ",
The fragment overall length that judges whether described free memory fragment is greater than " two fragment heads of L+ are long ", if, described free memory fragment is removed from idle chained list, split into two memory fragmentations, last length is the memory fragmentation of " fragment head length+L ", being more made as memory fragmentation to be allocated after new fragment header structure, be inserted into the last-of-chain of using fragment chained list; A rear length is the memory fragmentation of " described fragment overall length-fragment head length-L ", inserts FREE EXTENT FRAGMENTATION IN chained list afterbody after fragment header structure is set; Otherwise directly described FREE EXTENT FRAGMENTATION IN is removed from FREE EXTENT FRAGMENTATION IN chained list, and be inserted into and use fragment chained list afterbody.
Another object of the embodiment of the present invention is to propose a kind of memory fragmentation distributor based on shared drive, and described device comprises:
FREE EXTENT FRAGMENTATION IN chained list spider module, for according to the long L of the message of message to be stored, from first to last travels through FREE EXTENT FRAGMENTATION IN chained list, searches the free memory fragment that fragment overall length is greater than " fragment head length+L ";
The first judge module, finds fragment overall length to be greater than the free memory fragment of " fragment head length+L " for judging whether, if so, enters the second judge module, otherwise, enter fragment recombination module,
The second judge module, for judging whether that the fragment overall length of described free memory fragment is greater than " two fragment heads of L+ are long ", if so, enters fragment division module, otherwise enter, fragment removes and insert module;
Fragment recombination module, for adjacent idle memory fragmentation is recombinated, then returns to FREE EXTENT FRAGMENTATION IN chained list spider module;
Fragment division module, for described free memory fragment is removed from idle chained list, split into two memory fragmentations, last length is the memory fragmentation of " fragment head length+L ", being more made as memory fragmentation to be allocated after new fragment header structure, be inserted into the last-of-chain of using fragment chained list; A rear length is the memory fragmentation of " described fragment overall length-fragment head length-L ", inserts FREE EXTENT FRAGMENTATION IN chained list afterbody after fragment header structure is set;
Fragment removes and insert module, for directly described FREE EXTENT FRAGMENTATION IN being removed from FREE EXTENT FRAGMENTATION IN chained list, and is inserted into and uses fragment chained list afterbody.
Beneficial effect of the present invention
The present invention improves the repeatedly problem of copy in message transmission and processing procedure, message buffering queue is moved on to shared drive from trusted parties, and proposed to create message queue and memory fragmentation distribution method based on shared drive; The present invention can reduce the repeatedly copy in message transmission and processing procedure, improves system effectiveness, improves the concurrent number of the webserver, io is separated with logical process simultaneously, can improve ability extending transversely and the dirigibility of the webserver.
Embodiment
In order to make object of the present invention, technical scheme and advantage clearer, below in conjunction with drawings and Examples, the present invention is further elaborated, for convenience of explanation, only show the part relevant to the embodiment of the present invention.Should be appreciated that the specific embodiment that this place is described, only for explaining the present invention, not in order to limit the present invention.
The embodiment of the present invention is improved the repeatedly problem of copy in message transmission and processing procedure, message buffering queue is moved on to shared drive from trusted parties, and proposed to create message queue and memory fragmentation distribution method based on shared drive; The embodiment of the present invention can reduce the repeatedly copy in message transmission and processing procedure, improves system effectiveness, improves the concurrent number of the webserver, io is separated with logical process simultaneously, can improve ability extending transversely and the dirigibility of the webserver.
Embodiment mono-
Fig. 1 is a kind of memory fragmentation distribution method process flow diagram based on shared drive of the preferred embodiment of the present invention; Said method comprising the steps of:
S101, according to the long L of the message of message to be stored, from first to last travels through FREE EXTENT FRAGMENTATION IN chained list, searches the free memory fragment that fragment overall length is greater than " fragment head length+L ";
Preferred embodiment of the present invention memory fragmentation structural representation as shown in Figure 2;
Memory fragmentation is the storage space of the certain length that distributes from shared drive of read or write;
Memory fragmentation comprises fragment head and fragment entity; Fragment head comprises fragment overall length, uses length, last fragment pointer, a rear fragment pointer, 5 fields of use state; Fragment entity is the internal memory of actual allocated, is the carrier of message stores, carries a message in a fragment entity;
Fragment overall length is the total length (i.e. " length of fragment head length+fragment entity ") of each memory fragmentation;
The length of having used for each memory fragmentation by length (being the length of the message of its storage, is also " length of the length+message body of message header ");
Use state comprises the free time and has used two states;
Last fragment pointer is for indicating at affiliated fragment chained list, the previous memory fragmentation of current memory fragmentation, if the gauge outfit that current memory fragmentation is chained list, last fragment pointed itself;
A rear fragment pointer is for indicating at affiliated fragment chained list, a rear memory fragmentation of current memory fragmentation, if the table tail that current memory fragmentation is chained list, after a fragment pointed itself;
Fragment chained list comprises uses fragment chained list and FREE EXTENT FRAGMENTATION IN chained list;
By fragment chained list pointed, used fragment chained list gauge outfit, all is associated with the last fragment pointer in the fragment head of memory fragmentation and a rear fragment pointer by each with memory fragmentation, forms and has used fragment chained list;
FREE EXTENT FRAGMENTATION IN chained list pointed FREE EXTENT FRAGMENTATION IN chained list gauge outfit; FREE EXTENT FRAGMENTATION IN chained list pointed FREE EXTENT FRAGMENTATION IN chained list gauge outfit, all free memory fragments are associated by the last fragment pointer in the fragment head of each free memory fragment and a rear fragment pointer, form FREE EXTENT FRAGMENTATION IN chained list;
Message comprises message header and message body; Message header is for storing message long (being the length of the length+message body of message header) and the associated web socket handle (and IO handle) of message, the migration of described web socket handle for guiding message.Message body is the particular content of message, and it can passage or picture etc.
S102, judges whether to find fragment overall length to be greater than the free memory fragment of " fragment head length+L ", if so, enters step S103; Otherwise adjacent idle memory fragmentation is recombinated, return to step S101;
In embodiment of the present invention step S101-S102, if carried out after first round fragment restructuring, from first to last travel through for the second time FREE EXTENT FRAGMENTATION IN chained list and still can not find the free memory fragment that fragment overall length is greater than " fragment head length+L ", return to memory allocation error.In the searching loop of step S101-S102, the number of times of searching loop also can be set according to demand, if still can not find the free memory fragment that fragment overall length is greater than " fragment head length+L ", then return to memory allocation error.
The adjacent idle memory fragmentation method of (merging of free memory fragment) of recombinating is specially to (being illustrated in figure 3 the method flow diagram that adjacent idle memory fragmentation is recombinated):
S1020, the first address of content regions is set to the start address of traversal;
S1021, travels through content regions, searches two free memory fragments of adjacent (physical address is continuous);
S1022, if find two adjacent free memory fragments, enters step S1023; Otherwise enter step S1020;
S1023 removes described two adjacent free memory fragments from free memory chained list, then merges, then the gauge outfit of adding FREE EXTENT FRAGMENTATION IN chained list to; The method that merges described two adjacent free memory fragments is:
First, the fragment entity of the free memory fragment after merging using " the fragment entity of the fragment head of the fragment entity of last memory fragmentation+rear memory fragmentation+rear memory fragmentation " in adjacent idle memory fragmentation;
Then, the chip length in the fragment head of the last memory fragmentation of corresponding modify, last fragment pointer, a rear fragment pointer field;
Finally, the fragment entity of the free memory fragment by the fragment head of amended last memory fragmentation and after merging combines, and forms the free memory fragment after merging.
S1024, the start address of traversal is set to next physical address of the free memory fragment after merging, returns to step S1021;
In step S1020-S1024, though adjacent memory fragment length may be different, physical address is continuous, joins end to end; The first address of known memory pool content regions and tail address, start traversal from first address, searches two adjacent FREE EXTENT FRAGMENTATION IN of front and back, after finding, first described two adjacent FREE EXTENT FRAGMENTATION IN remove from free memory chained list, and then they merge, then the gauge outfit of adding FREE EXTENT FRAGMENTATION IN chained list to.With this rule, travel through whole memory pool content area backward, all two adjacent memory fragmentations are merged.Preliminary merging through the method is processed, and little memory fragmentation is integrated into larger memory fragmentation, for distributing large memory fragmentation to prepare.
A part of physical memory that shared drive is shared by a plurality of processes; Memory pool is open up in shared drive one section fixedly continuous physical memory.Memory pool comprises directorial area and content regions; Directorial area storage comprises first fragment pointer, tail fragment pointer, uses fragment chain list index, FREE EXTENT FRAGMENTATION IN chain list index, 5 signs of content regions length.Content regions is for storing all memory fragmentations, and described memory fragmentation comprises uses memory fragmentation and free memory fragment; When original state, in memory pool, only have a length to equal the memory fragmentation of content regions overall length.
Be illustrated in figure 4 the structural drawing of memory pool; Frag1, frag2, frag3 that the content regions storage of described memory pool is arranged in order ... fragN+1 is N fragment altogether, first fragment pointed fragment frag1, tail fragment pointed fragment fragN+1;
Supposing has 5 with memory fragmentation in content regions: frag2, frag3, frag5, frag8, frag12, these 5 have formed and have used fragment chained list with memory fragmentation, frag2 is gauge outfit, frag12 is table tail, now in directorial area, uses fragment chained list pointed memory fragmentation frag2 (having used fragment chained list gauge outfit); And last fragment pointed itself in the fragment head of memory fragmentation frag2, a rear fragment pointed frag3; Last fragment pointed frag2 in the fragment head of memory fragmentation frag3, a rear fragment pointed frag5, in like manner, can derive last fragment pointer in the fragment head of frag5, frag8, frag12 and the sensing of a rear fragment pointer, not repeat them here.
And free memory fragment has N-5: frag1, frag4, frag6, frag7, frag9, frag10, frag11, frag13, frag14 ... fragN+1, this N-5 free memory fragment formed and used fragment chained list, frag1 is gauge outfit, fragN+1 is table tail, now in directorial area, uses fragment chained list pointed memory fragmentation frag1 (being FREE EXTENT FRAGMENTATION IN chained list gauge outfit); And last fragment pointed itself in the fragment head of memory fragmentation frag1, a rear fragment pointed frag4; Last fragment pointed frag1 in the fragment head of memory fragmentation frag4, a rear fragment pointed frag6, in like manner, can derive frag6, frag7, frag9, frag10, frag11, frag13, frag14 ... last fragment pointer in the fragment head of fragN+1 and the sensing of a rear fragment pointer, do not repeat them here.
S103, judges whether that the fragment overall length of described free memory fragment is greater than " fragment head length+L+ fragment head length " (i.e. " two fragment heads of L+ are long "), if so, enters step S104, otherwise enters step S105;
S104, described free memory fragment is removed from idle chained list, split into two memory fragmentations, last length is the memory fragmentation of " fragment head length+L ", being more made as memory fragmentation to be allocated after new fragment header structure, be inserted into the last-of-chain of using fragment chained list; A rear length is the memory fragmentation of " described fragment overall length-fragment head length-L ", inserts FREE EXTENT FRAGMENTATION IN chained list afterbody after fragment header structure is set.
The described method that splits into two memory fragmentations is specially:
The fragment physical length of the last memory fragmentation after division is set to L, the described message to be stored of its fragment entity carrying, fragment overall length in the fragment head of the last memory fragmentation after corresponding modify division, with length, last fragment pointer, a rear fragment pointer, use mode field, the fragment head of the last memory fragmentation after amended division and fragment entity are combined, form the new memory fragmentation of using.
By the fragment physical length of the rear memory fragmentation after division, be to be set to " the fragment overall length-fragment head length-L before division ", its fragment entity carrying content is empty, fragment overall length in the fragment head of the rear memory fragmentation after relative set division, with length, last fragment pointer, a rear fragment pointer, use mode field, the fragment head of the rear memory fragmentation after amended division and fragment entity are combined, form new free memory fragment.
S105, does not divide, and directly described FREE EXTENT FRAGMENTATION IN is removed from FREE EXTENT FRAGMENTATION IN chained list, and is inserted into and uses fragment chained list afterbody.
Embodiment bis-
Fig. 5 is a kind of memory fragmentation distributor structural drawing based on shared drive of the preferred embodiment of the present invention.Described device comprises:
FREE EXTENT FRAGMENTATION IN chained list spider module, for according to the long L of the message of message to be stored, from first to last travels through FREE EXTENT FRAGMENTATION IN chained list, searches the free memory fragment that fragment overall length is greater than " fragment head length+L ";
The first judge module, finds fragment overall length to be greater than the free memory fragment of " fragment head length+L " for judging whether, if so, enters the second judge module, otherwise, enter fragment recombination module,
The second judge module, for judging whether that the fragment overall length of described free memory fragment is greater than " fragment head length+L+ fragment head length " (i.e. " two fragment heads of L+ are long "), if so, enter fragment division module, otherwise enter, fragment removes and insert module;
Fragment recombination module, for adjacent idle memory fragmentation is recombinated, then returns to FREE EXTENT FRAGMENTATION IN chained list spider module;
Fragment division module, for described free memory fragment is removed from idle chained list, split into two memory fragmentations, last length is the memory fragmentation of " fragment head length+L ", being more made as memory fragmentation to be allocated after new fragment header structure, be inserted into the last-of-chain of using fragment chained list; A rear length is the memory fragmentation of " described fragment overall length-fragment head length-L ", inserts FREE EXTENT FRAGMENTATION IN chained list afterbody after fragment header structure is set.
The described method that splits into two memory fragmentations is specially:
The fragment physical length of the last memory fragmentation after division is set to L, the described message to be stored of its fragment entity carrying, fragment overall length in the fragment head of the last memory fragmentation after corresponding modify division, with length, last fragment pointer, a rear fragment pointer, use mode field, the fragment head of the last memory fragmentation after amended division and fragment entity are combined, form the new memory fragmentation of using.
By the fragment physical length of the rear memory fragmentation after division, be to be set to " the fragment overall length-fragment head length-L before division ", its fragment entity carrying content is empty, fragment overall length in the fragment head of the rear memory fragmentation after relative set division, with length, last fragment pointer, a rear fragment pointer, use mode field, the fragment head of the rear memory fragmentation after amended division and fragment entity are combined, form new free memory fragment.
Fragment removes and insert module, for directly described FREE EXTENT FRAGMENTATION IN being removed from FREE EXTENT FRAGMENTATION IN chained list, and is inserted into and uses fragment chained list afterbody.
Further, described fragment recombination module also comprises (structural drawing that shown in Fig. 6 is fragment recombination module):
Traversal start address initialization module, for initialization, the first address of content regions is set to the start address of traversal;
Content regions spider module, travels through content regions, searches two free memory fragments of adjacent (physical address is continuous);
The 3rd judge module, finds two adjacent free memory fragments for judging whether, if enter fragment, merges module; Otherwise return to traversal start address initialization module;
Fragment merges module, for described two adjacent free memory fragments are removed from free memory chained list, then merges, then the gauge outfit of adding FREE EXTENT FRAGMENTATION IN chained list to;
The method that merges described two adjacent free memory fragments is:
First, the fragment entity of the free memory fragment after merging using " the fragment entity of the fragment head of the fragment entity of last memory fragmentation+rear memory fragmentation+rear memory fragmentation " in adjacent idle memory fragmentation;
Then, the chip length in the fragment head of the last memory fragmentation of corresponding modify, last fragment pointer, a rear fragment pointer field;
Finally, the fragment entity of the free memory fragment by the fragment head of amended last memory fragmentation and after merging combines, and forms the free memory fragment after merging.
Next physical address arranges module, is set to next physical address of the free memory fragment after merging, then returned content district spider module for the start address of traversal;
Those having ordinary skill in the art will appreciate that, the all or part of step realizing in above-described embodiment method can complete by programmed instruction related hardware, described program can be stored in a computer read/write memory medium, and described storage medium can be ROM, RAM, disk, CD etc.
The foregoing is only preferred embodiment of the present invention, not in order to limit the present invention, all any modifications of doing within the spirit and principles in the present invention, be equal to and replace and improvement etc., within all should being included in protection scope of the present invention.