A kind of implementation method and device for saving memory
Technical field
The present invention relates to Bluetooth technology field, more particularly to a kind of implementation method and device for saving memory.
Background technology
Bluetooth technology has the history of more than ten years, is largely used to the fields such as mobile phone, wireless headset.In recent years since bluetooth is assisted
Bluetooth technology is extended to more extensive inexpensive application field, such as instrument monitoring by the publication of view 4.0, wherein low-power consumption specification
Deng.The memory for reducing bluetooth protocol repertory, not only reduces the hardware cost of equipment, and can reduce equipment power dissipation, extends equipment
Usage time, have great significance for Bluetooth Low Energy equipment.In the prior art, the reality of the memory of bluetooth protocol repertory is reduced
Existing mode is usually to modify on original protocol stack, such as realized by recombinant technique, i.e. every layer in bluetooth stack, in advance
Distribution recombinates required memory, and each layer recombinates respective memory, but is needed if carrying out a large amount of recombination a large amount of
Memory can not be really achieved the effect of memory optimization.Although the existing realization method modified on original protocol stack
Development risk can be reduced, but internal memory optimization effect is not obvious, while so that internal memory optimization effect is reached preferably also
The performance for not influencing equipment is prior art urgent problem to be solved.
Invention content
In order to influence equipment performance when solving the problems, such as to improve internal memory optimization effect in the prior art, the present invention carries
A kind of implementation method and device for saving memory is supplied.The technical solution is as follows:
The technical solution adopted by the present invention:
A kind of implementation method for saving memory, including:
When receiving fragment, judge whether to be stored in reassembly buffer area with the fragment received for same type
Fragment is combined the fragment of the same type stored in the reassembly buffer area and the fragment received, such as group
Combination is then obtained data packet for a complete data packet and is sent to upper layer by conjunction result.
Further, in the above-mentioned methods, to be received point if the combined result is not a complete data packet etc.
Piece;
Such as judge in reassembly buffer area the not fragment with the fragment same type received, is then received described
In fragment storage to the reassembly buffer area, fragment to be received is waited.
Further, described to judge point with the fragment received for same type whether is stored in reassembly buffer area
Further include before piece:Judge whether the fragment received is a complete data packet, is to terminate, otherwise continues.
Further, described to be specially in the fragment storage to the reassembly buffer area that received:Call Lock
The fragment received is stored the tail portion to reassembly buffer area by function.
Further, the fragment by the same type stored in the reassembly buffer area and the fragment received into
Row combination, specially:After calling Unlock functions to take out the fragment of the same type stored in the reassembly buffer area again with institute
The fragment received is stated to be combined.
Further, the fragment by the same type stored in the reassembly buffer area and the fragment received into
Row combination, specially:
A, one aray variable is set, using the fragment of the same type stored in the reassembly buffer area as current slice;
B, judge whether the data length of current slice is more than the length of the aray variable, be to execute E, otherwise execute
C;
C, the data of current slice are read from the reassembly buffer area, and are written into the aray variable, it will
Fragment after current slice is moved at the position of current slice;
D, the data in the aray variable are read and are written into point received described in the reassembly buffer area
Before the stem of piece;
E, the data with aray variable equal length are read from the current slice in the reassembly buffer area, and will be read
Data be written in the aray variable, the fragment after current slice is moved in current slice and has read data
At position;
F, the data in the aray variable are read and are written into the fragment received in the reassembly buffer area
Before stem, current slice is updated with the remaining data of current slice in the reassembly buffer area, returns to B.
A kind of realization device for saving memory, including:
Receiving module, for receiving fragment;
Second judgment module, for judging whether to be stored in reassembly buffer area with the fragment received for same type
Fragment, be to trigger composite module;
The composite module, for by the fragment of the same type stored in the reassembly buffer area with it is described receive point
Piece is combined;
Third judgment module, for judging whether the combined result of the composite module is a complete data packet, is
The sending module is then triggered, the receiving module is otherwise triggered;
The sending module obtains data packet and is sent to upper layer for that will combine.
Further, above-mentioned apparatus further includes:Memory module, for when second judgment module is judged as NO by institute
It states in the fragment storage to the reassembly buffer area received;It is the triggering reception mould that the third judgment module, which is judged as NO,
Block.
Further, above-mentioned apparatus further includes:First judgment module, for judging whether the fragment received is one complete
Whole data packet is to terminate, and otherwise triggers second judgment module.
Further, the composite module includes:
Setting unit makees the fragment of the same type stored in the reassembly buffer area for an aray variable to be arranged
For current slice;
First judging unit, for judge current slice data length whether be more than the aray variable length, be
Then writing unit is read in triggering second, is otherwise triggered first and is read writing unit;
It is described first read writing unit, for from the reassembly buffer area read current slice data, and by its
It is written in the aray variable;
First movement unit, for the fragment after current slice to be moved at the position of current slice;
Described second reads writing unit, for reading the data in the aray variable and being written into the recombination
Before the stem of the fragment received in buffering area;
Third reads writing unit, for being read and aray variable length from the current slice in the reassembly buffer area
Equal data, and the data of reading are written in the aray variable;
Second mobile unit, for the fragment after current slice to be moved in current slice to the position for having read data
Set place;
4th reads writing unit, for reading the data in the aray variable and being written into the reassembly buffer
Before the stem of the fragment received in area, current slice is updated with the remaining data of current slice in the reassembly buffer area.
The advantageous effect that technical solution provided in an embodiment of the present invention is brought is:
Technical scheme of the present invention first determines whether have in reassembly buffer area when receiving a fragment during realization
There is no the fragment of same type, if there is being then combined the fragment received and the same type fragment of storage, if combination knot
Fruit is that a complete data packet then delivers a packet to upper layer, makes each layer in bluetooth protocol repertory that can all carry out fragment weight
Group, and all recombinations are all realized in same buffering area, are saved memory, not will increase the consumption of other collaboration software memories,
Also ensure the quality standards such as the original performance of product and compatibility.
Description of the drawings
To describe the technical solutions in the embodiments of the present invention more clearly, make required in being described below to embodiment
Attached drawing is briefly described, it should be apparent that, drawings in the following description are only some embodiments of the invention, for
For those of ordinary skill in the art, without creative efforts, other are can also be obtained according to these attached drawings
Attached drawing.
Fig. 1 is a kind of implementation method flow chart for saving memory that the embodiment of the present invention one provides;
Fig. 2 is the realization process flow diagram flow chart being combined in step S4 in the embodiment of the present invention one;
Fig. 3, which is that one of the bluetooth stack in embodiment one is conventional, realizes structure chart;
Fig. 4 is the realization process schematic for the specific example that the embodiment of the present invention one provides;
Fig. 5 is a kind of realization device block diagram for saving memory provided by Embodiment 2 of the present invention;
Fig. 6 is the block diagram of composite module provided by Embodiment 2 of the present invention.
Specific implementation mode
To make the object, technical solutions and advantages of the present invention clearer, below in conjunction with attached drawing to embodiment party of the present invention
Formula is described in further detail.
Embodiment one
The embodiment of the present invention one provides a kind of implementation method for saving memory, specifically by recombinating common memory come real
It is existing, that is, it recombinates and shares a buffering area, the method for the present embodiment is as shown in Figure 1, include:
Step S0:When receiving fragment, judge whether the fragment received is a complete data packet, is to tie
Beam, it is no to then follow the steps S1;
In the present embodiment, as judged, it is a complete data packet to receive fragment, then need not recombinate, as judged to connect
It is a complete data packet to receive fragment not, will initiate restructuring request to server when receiving fragment, and application is heavy using this
Group buffering area realizes that recombination shares process.
Step S1:Judge that the fragment for whether being stored with and receiving in reassembly buffer area is to hold for the fragment of same type
Row step S3, it is no to then follow the steps S2;
Fragment is the hair piece that lower layer sends in the present embodiment;As there is the fragment of same type in reassembly buffer area, then illustrate this
The preceding fragment for having received same type receives a same type fragment and just needs the fragment that will be received and storage again at this time
Same type fragment merges;If there is no the fragment of same type in reassembly buffer area, then illustrate that the fragment received is this class
First fragment of type, needs to store and merges recombination when receiving same type fragment next time;
Step S2:The fragment received is stored into reassembly buffer area, return to step S1;
Specifically, in the present embodiment, call Lock functions that the fragment received is stored to the tail portion to reassembly buffer area,
The byte number that reassembly buffer area occupies increases, with regard to needing to carry out with the fragment specifically stored after the fragment that next time receives same type
Merger and reorganization;
Step S3:The fragment of the same type stored in reassembly buffer area and the fragment received are combined, sentenced
Whether disconnected combined result is a data packet, is, combination, which is obtained data packet, is sent to upper layer, return to step S1;Otherwise directly
Return to step S1;
Specifically, in the present embodiment, Unlock functions is called to take the fragment of the same type stored in reassembly buffer area
It is combined again with the fragment received after going out, fragment behind after the fragment of the same type stored in reassembly buffer area is removed
It is moved forward to fill up the fragment and be taken up space, finally data packet that combination obtains is taken out and sent out from reassembly buffer area
Upper layer is given, to which the byte number that reassembly buffer area occupies reduces.
In the present embodiment, data are coated with to the several fragments being divided into, are received every time after a fragment all with upper one
The secondary fragment stored, which merges recombination, becomes a new fragment, when receiving the last one fragment, with regard to this point
Piece and the fragment of storage merge be reassembled as a complete data packet after be sent to upper layer.At this packet combining recombination
After the completion of reason, begin to merge recombination to next data packet, realization method is identical, and details are not described herein.
As shown in Fig. 2, the reality that the fragment of the same type stored in reassembly buffer area and the fragment received are combined
Now process includes:
A, one aray variable is set, using the fragment of the same type stored in reassembly buffer area as current slice;
B, judge whether the data length of current slice is more than the length of aray variable, be to execute E, otherwise execute C;
C, the data of current slice are read from reassembly buffer area, and are written into aray variable, by current slice
Fragment later is moved at the position of current slice;
D, the data in aray variable are read and are written into before the stem of the fragment received in reassembly buffer area;
E, read from the current slice in reassembly buffer area with the data of aray variable equal length, and by the number of reading
According to being written in aray variable, the fragment after current slice is moved in current slice and has been read at the position of data;
F, the data in aray variable are read and are written into before the stem of the fragment received in reassembly buffer area,
Current slice is updated with the remaining data of current slice in reassembly buffer area, returns to B.
In the present embodiment, the parameter of Lock/unlock functions is handle, during reassembly buffer area, for not
The fragment of same layer can move freely, but since the value of handle is fixed, so data that can be in accurate access buffer area;And
All recombinations are all realized in same buffering area, are saved memory, will not be increased the consumption of other collaboration software memories, moreover it is possible to
Ensure the quality standards such as the original performance of product and compatibility.
The realization process for illustrating the present embodiment method with reference to specific example is one of bluetooth stack referring to Fig. 3
It is conventional to realize, wherein there is a large amount of reassembly buffer:Reassembly buffer area a is needed multiple HCI(Full name in English:Host
Controller Interface, Chinese name:Host Controler Interface)The frament reassembling of transmission is at a L2CAP(It is English complete
Claim:Logical Link Control and Adaptation Protocol, Chinese name:Logic link control and adaptation are assisted
View)Packet, reassembly buffer area b are that some profile coatings of itself are divided into multiple, then need the buffering reconfigured
Area.
The processing procedure of fragment is received as shown in figure 4, will be in its sequential storage to packet buffer after receiving fragment;
(1)There are one fragment A for the storage of reassembly buffer area, receive fragment 1;
(2)Due to the not no fragment with 1 same type of fragment in reassembly buffer area, so it is slow that the fragment 1 is temporarily stored in recombination
It rushes in area, i.e., fragment 1 is buffered in reassembly buffer area tail portion, reassembly buffer area increases at this time;
(3)As the different type fragment C for receiving lower layer's transmission and kept in reassembly buffer area;
(4)When receiving fragment 2, lock functions is called to store fragment 2 into reassembly buffer area;
(5)Due to the fragment 1 stored in reassembly buffer area with fragment 2 is same type, so calling Unlock letters
Number, itself and fragment 2 are combined as new fragment 2+ by taking-up fragment 1 from reassembly buffer area;In weight buffering area, fragment C is forward
The mobile position for having filled up fragment 1.
(6)If not completing to recombinate, fragment 2+ is temporarily stored in reassembly buffer area.
Specifically, taking out the specific implementation side that it is reassembled as new fragment 2+ by fragment 1 with fragment 2 from reassembly buffer area
Formula includes:
A, an aray variable is defined;Aray variable length in the present embodiment can self-defining, such as be defined as 16 words
Section, aray variable are distributed on running software stack, can be discharged automatically.
B, the data isometric with aray variable of 1 tail portion of fragment are read out and is written in aray variable.
C, fragment C is moved to the tail portion of fragment 1;
D, the data in aray variable are read out before being written to 2 stem of fragment;
If e, the data length of fragment 1 is not more than the length of aray variable, d is executed and has been achieved that later by fragment
1 process being combined with fragment 2;If the data length of fragment 1 is more than the length of aray variable, then repeatedly b-d, until complete
At.
(6)Judge to synthesize whether fragment 2+ is a data packet, is to complete to recombinate, fragment 2+ is otherwise temporarily stored in recombination
In buffering area, S1 is returned.
Embodiment two
Second embodiment of the present invention provides a kind of realization devices for saving memory, as shown in figure 5, including:
Receiving module 501, for receiving fragment;
First judgment module 502 is to terminate for judging whether the fragment received is a complete data packet,
Otherwise the second judgment module 503 is triggered;
Second judgment module 503, the fragment for judging whether to be stored with and receive in reassembly buffer area is same type
Fragment, be to trigger composite module 505, otherwise trigger memory module 504;
Memory module 504, for storing the fragment received into reassembly buffer area;
Specifically, in the present embodiment, memory module 504 is by calling Lock functions to store the fragment received to weight
The tail portion of group buffering area, the byte number that reassembly buffer area occupies increase;
Composite module 505, for by the fragment of the same type stored in reassembly buffer area and the fragment received into
Row combination;
Specifically, in the present embodiment, composite module 505 is by calling Unlock functions that will be stored in reassembly buffer area
The fragment of same type is combined with the fragment received again after taking out, and the fragment of the same type stored in reassembly buffer area is taken
Fragment is moved forward to fill up the fragment and is taken up space behind after going out;
Third judgment module 506, for judging whether the combined result of composite module is a complete data packet, is then
Sending module 507 is triggered, receiving module 501 is otherwise triggered;
Sending module 507 obtains data packet and is sent to upper layer for that will combine.
As shown in fig. 6, composite module 505 specifically includes:
Setting unit 5051 makees the fragment of the same type stored in reassembly buffer area for an aray variable to be arranged
For current slice;
First judging unit 5052, for judge current slice data length whether be more than aray variable length, be
Then writing unit 5055 is read in triggering second, is otherwise triggered first and is read writing unit 5053;
First reads writing unit 5053, the data for reading current slice from reassembly buffer area, and is written into
Into aray variable;
First movement unit 5054, for the fragment after current slice to be moved at the position of current slice;
Second reads writing unit 5055, for reading the data in aray variable and being written into reassembly buffer area
Before the stem of the fragment received;
Third reads writing unit 5056, for being read and aray variable length from the current slice in reassembly buffer area
Equal data, and the data of reading are written in aray variable;
Second mobile unit 5057 has read data for the fragment after current slice to be moved in current slice
Position at;
4th reads writing unit 5058, for reading the data in aray variable and being written into reassembly buffer area
Before the stem of the fragment received, current slice is updated with the remaining data of current slice in reassembly buffer area.
One of ordinary skill in the art will appreciate that realizing that all or part of step of above-described embodiment can pass through hardware
It completes, relevant hardware can also be instructed to complete by program, the program can be stored in a kind of computer-readable
In storage medium, storage medium mentioned above can be read-only memory, disk or CD etc..
The foregoing is merely presently preferred embodiments of the present invention, is not intended to limit the invention, it is all the present invention spirit and
Within principle, any modification, equivalent replacement, improvement and so on should all be included in the protection scope of the present invention.