Summary of the invention
An object of the present invention is to propose the creation method of a kind of universal memory entity, its energy
Solve the problem that during entity storage in software development, memory consumption is the biggest.
The two of the purpose of the present invention are to propose the access method of a kind of universal memory entity, its energy
Solve the problem of access entity attribute inefficiency in software development.
One of in order to achieve the above object, the technical solution adopted in the present invention is as follows:
The invention provides the creation method of a kind of universal memory entity, including creating entity description
Step and create entity step, the step of described establishment entity description includes:
S101: initialize entity description;
S102: obtain first attribute to be added, preserves first attribute to be added to described
In entity description;Wherein, attribute to be added includes attribute length and attribute excursion amount, first
The attribute excursion amount of attribute to be added is zero;
S103: obtain next attribute to be added and be designated as current attribute to be added, will currently treat
Adding attribute to preserve to described entity description, then proceeding to perform S103, until being needed
Add attribute to preserve to described entity description;Wherein, during solid data size is entity description
The attribute length sum of all properties preserved, the attribute excursion amount of current attribute to be added is
Solid data size corresponding to current entity description;
The method of described establishment entity includes:
S201: according to the solid data size obtained from entity description and distributed by system
The size of pointer to system application one memory block, the size of this memory block be solid data size with
The size sum of pointer;
S202: described pointer is saved in the original position of described memory block;
S203: obtain first attribute from entity description, and be designated as current attribute, then obtain
Take the attribute excursion amount of described current attribute and be calculated the attribute address of current attribute;
S204: according to the attribute address of described current attribute and get from system current
Current attribute is carried out initializing structure and being stored in described interior by the attribute data that Attribute Relative is answered
In counterfoil;
S205: obtain next attribute from entity description, and be designated as current attribute, then obtain
Take the attribute excursion amount of this current attribute and be calculated the attribute address of current attribute, performing
S204;Until all properties in entity description all being initialized structure be stored in described memory block
In.
Preferably, described attribute to be added also includes Property Name;Will in described step S102
When first attribute to be added preserves to described entity description, it is judged that first attribute to be added
Property Name be whether empty, when described Property Name be sky, then exit.
Preferably, described in described step S103, current attribute is preserved to described attribute array
Time middle, it is judged that whether the Property Name of current attribute is empty or Property Name has existed
In described entity description, when the entitled sky of attribute or Property Name have existed and described entity
Time in description, then exit.
Preferably, pointer described in described S201 is for pointing to the address of described entity description.
Preferably, described attribute address is the memory address of entity, attribute excursion amount and the size of pointer
Three's sum.
Two in order to achieve the above object, the technical solution adopted in the present invention is as follows:
Present invention also offers the access method of a kind of universal memory entity, this access method is should
For the creation method of described universal memory entity provided by the present invention created general in
Deposit entity, including:
It is inclined that attribute to be visited according to entity gets the attribute of attribute to be visited in entity description
Shifting amount;
The attribute excursion amount of block address memory, the size of pointer and attribute to be visited according to entity
Obtain the attribute address of this attribute to be visited;
Treat access attribute by the attribute address of described attribute to be visited and be written and read operation.
There is advantages that the structure of entity in the entity that the present invention is created
Describing the data with entity to be stored separately, the structure of entity describes and only initializes once, entity
Data can preserve many parts according to actual situation, in the case of entity object is relatively more significantly
Save internal memory.In the efficiency of access entity, owing to entity attributes size and order are fixing,
The direct memory address being obtained attribute by the side-play amount of attribute, the then attribute of access entity,
Substantially increase the efficiency of access entity.
Detailed description of the invention
Below, in conjunction with accompanying drawing and detailed description of the invention, the present invention is described further:
The invention provides the creation method of a kind of universal memory entity, entity refers to by a series of
The data of attribute composition, the entity that the method is created is by entity description and entity separation, also
Being i.e. the data that the attribute description in entity is concrete with entity to be stored separately, entity description is only
Needing to initialize once, that is to say its only one of which, the data of entity can divide multiple entity to enter
Row storage, and in physical memory block, preserve a pointer and this pointer is pointed to the ground of entity description
Location, thus entity description is associated with the data of entity, so more at entity object
In the case of, it is greatly saved the memory space of internal memory.Described creation method includes that creating entity retouches
The step stated and the step of establishment entity, as it is shown in figure 1, the step of described establishment entity description
Including:
S101: initialize entity description.
S102: obtain first attribute to be added, preserves first attribute to be added to described
In entity description;Wherein, attribute to be added includes attribute length and attribute excursion amount, first
The attribute excursion amount of attribute to be added is zero.
Wherein, Property Name, attribute length, genus are at least included for each attribute description
Property type and attribute excursion amount, attribute excursion amount refer to Attribute Relative in the position of entity, therefore
Also be not added with attribute in this entity description, then the attribute excursion amount of first attribute to be added is i.e.
Zero.Described attribute type refers to the data type of the value of this attribute, such as a student's
The value of attribute " name " is character string type.It addition, for the feature of attribute: attribute-name
Can not be called empty and can not repeat, therefore add described first attribute to be added to this reality
Time in body description, should first judge whether the Property Name of described first attribute to be added is empty,
The most then illustrate that this entity attributes is illegal, then report an error or exit.
S103: obtain next attribute to be added and be designated as current attribute to be added, will currently treat
Adding attribute to preserve to described entity description, then proceeding to perform S103, until being needed
Add attribute to preserve to described entity description;Wherein, during solid data size is entity description
Having preserved the attribute length sum of all properties, the attribute excursion amount of current attribute to be added is for working as
Solid data size corresponding to front entity description.
Array or team can be passed through preferably for its preserving type of the attribute in entity description
The form of row preserves.
Wherein, described solid data size is the attribute of all properties preserved in entity description
Length sum.When getting an attribute A to be added, then the attribute of this attribute A to be added is inclined
Shifting amount is to that is to say the solid data size corresponding to current entity description, and described here is current
Entity description refers to that this current attribute to be added is not also saved in described entity description.Equally
, if the Property Name of this attribute A to be added is empty, then reports an error or exit.It addition,
Owing to the Property Name of attribute can not repeat, if the Property Name of attribute A to be added and entity
Description has been added the Property Name of attribute identical time, then this attribute A can not be preserved to entity
In description.
As in figure 2 it is shown, the step of described establishment entity includes:
S201: according to the solid data size obtained from entity description and distributed by system
The size of pointer to system application one memory block, the size of this memory block be solid data size with
The size sum of pointer.Wherein, solid data size refers to the genus of all properties in entity description
Property length sum.Described pointer is automatically to be distributed by system, the size of different its pointers of machine
Difference, is generally calculated automatically from by compiler, such as in the system of 32, pointer
Size is 4 bytes, and in the system of 64, the size of pointer is 8 bytes.Work as needs
When creating entity, from entity description, first get all of attribute, then according to attribute
Attribute length draws solid data size, and the size further according to solid data size and pointer draws
The size of entity committed memory, to internal memory application one memory block, the size of this memory block is entity
The size of committed memory, that is to say the size sum of solid data size and pointer.
S202: described pointer is saved in the original position of described memory block.In this step,
The original position of memory block preserves described pointer, and this pointer is for pointing to the interior of described entity description
Deposit address, that is to say that entity description is associated by this pointer with entity.
S203: obtain first attribute from entity description, and be designated as current attribute, then obtain
Take the attribute excursion amount of described current attribute and be calculated the attribute address of current attribute.
S204: according to described attribute address and the attribute data that gets from system to currently
Attribute carries out initializing structure and being stored in described memory block;
S205: obtain next attribute from entity description, and be designated as current attribute, then obtain
Take the attribute excursion amount of this current attribute and be calculated the attribute address of current attribute, performing
S204, until initializing structure all properties in entity description to be stored in described memory block
In.
Wherein, step S203 and S204 be the attribute preserved according to entity description successively and from
The attribute data got in system initializes and constructs all of attribute.That is to say successively from reality
One attribute of acquisition in body description, and obtain the attribute excursion amount of this attribute, then according to this genus
The size of attribute excursion amount, the memory address of entity and the pointer of property calculates the genus of this attribute
Property address, carry out initializing structure further according to the attribute address of this attribute and attribute data and obtain
This attribute is also stored in described memory block.The memory address of described entity refers to that system is divided automatically
The memory address of dispensing entity.When the attribute obtained from entity description is last, then
All of attribute all construction completes are described, then exit.
Such as a class, then the entity description only one of which created, but for a class
Object have a lot, when to preserve such object, it is only necessary to according to such entity description
Its corresponding entity is set up for each object.Such as " student " class, entity description is only
There is one, but " student " may have a lot of objects, as " little in " little quick " " Xiao Ming "
Lee " etc., a corresponding entity can be created for each object, each object
Entity be all to be associated by the entity description of a pointer with described class.For actual utilization
In, if when the object of class is more, this universal memory entity can greatly reduce entity storage
Time internal memory consumption.
As it is shown on figure 3, present invention also offers the access method of a kind of universal memory entity, should
Method is that the universal memory being applied to be created such as the creation method of described universal memory entity is real
Body, comprising:
S1: get the attribute of attribute to be visited in entity description according to the attribute to be visited of entity
Side-play amount.Wherein, entity description is inquired about attribute to be visited, and obtains attribute to be visited
Property Name, attribute length, attribute excursion amount and attribute type etc..
S2: big according to attribute excursion amount, the memory address of entity and the pointer of attribute to be visited
The little attribute address drawing attribute to be visited.Calculate the attribute address of attribute to be visited, should
Attribute address is the attribute excursion amount of attribute to be visited, the memory address of entity and the size of pointer
Three's sum.Wherein, the memory address of entity gets from system.
S3: treat access attribute by the attribute address of attribute to be visited and be written and read operation.Root
Treat access attribute according to attribute address obtained above and be written and read operation.
Such as, for entity class " student ", there is attribute " student number " " name " " age ",
The attribute length of three attributes is 12,13,10, then add three attributes to entity successively
In description, and show that the attribute excursion amount of three attributes is respectively 0,12,25, and this is real
Volume data size is 35." student " should there is object entity " Xiao Li " " Xiao Ming " " little quick ".
Nowadays need to access " name " this attribute of " Xiao Li ", first retouch from entity
In stating, the attribute excursion amount of " name " this attribute is (due to entity " Xiao Li " and " student "
Entity description between by pointer association, be therefore easy to draw the entity description of this entity),
Memory address and the size of pointer of this entity " Xiao Li " is may know that from system, interior by this
The size depositing address, attribute excursion amount and pointer draws the attribute address of this attribute " name ",
Then it is written and read operating to attribute " name " by this attribute address and attribute type.
It addition, attribute description preserves when, not only preserve Property Name, attribute length and genus
Property side-play amount, attribute type to be preserved.Obtaining attribute excursion amount when, obtain attribute
Corresponding attribute type, so when carrying out data access, it is easy to knows the number of this attribute
According to type, then attribute it is read out or writes.
Due to creating entity description and entity when, for entity attributes size and order
It is all fixing, therefore can access entity attributes by the attribute excursion amount of this attribute, because of
This this access method is greatly improved in terms of access efficiency.
It addition, based on this universal memory entity, present invention also offers a kind of based on universal memory
The destroying method of entity, comprising:
First from the beginning of the first of entity attribute, in order attribute is processed, and obtain
Taking the attribute address of each attribute, attribute address is the size of the memory address of entity, pointer
And attribute excursion amount three's sum of attribute.
Then according to the attribute address of each attribute, it is carried out destructed, until all of genus
Property is the most destructed to be completed.
Finally the memory block of entity is returned to system, complete the destruction of whole solid data.
This destroying method has only to the attribute address of each attribute according to entity to be destroyed
Carry out destructed one by one, then the memory block of entity is returned to system, be not related to reality
Body description and other entity, it is ensured that the safety of system.
For a person skilled in the art, can technical scheme as described above and structure
Think, make other various corresponding changes and deformation, and all these changes and deformation
Within all should belonging to the protection domain of the claims in the present invention.