Object creating method, terminal device and storage mediumTechnical Field
The present invention relates to the field of application development, and in particular, to an object creation method, a terminal device, and a storage medium.
Background
During application development, classes and objects are often used. When a large number of objects need to be created, the creation process is time-consuming.
At present, the most common object creation mode is to directly call a new operator, and to directly create an object through the new operator is to randomly acquire a memory space, so that when a large amount of memory fragments exist in a system, the creation efficiency is low.
Disclosure of Invention
In view of this, embodiments of the present invention provide an object creating method, a terminal apparatus, and a storage medium, which are used to solve the problem that the memory is not optimized in the current object creating manner, so that the object creating efficiency is low.
In a first aspect of the embodiments of the present invention, there is provided an object creating method, including:
redefining the interface of the object memory allocation function and the interface of the specified address calling construction function;
calling an interface function of the object memory allocation function to apply for a memory space with a preset size;
setting pre-stored data byte alignment in the memory space through an internal instruction of a system compiler;
and setting the first address of the memory space as a specified memory address by using the interface function of the specified address calling construction function, and creating an object at the specified address.
In a second aspect of embodiments of the present invention, there is provided a terminal apparatus, including:
the definition module is used for redefining the interface of the object memory allocation function and the interface of the specified address calling construction function;
the application module is used for calling the interface function of the object memory allocation function to apply for a memory space with a preset size;
the alignment module is used for setting alignment of pre-stored data bytes in the memory space through an internal instruction of a system compiler;
and the setting module is used for setting the first address of the memory space as the specified memory address by using the interface function of the specified address calling construction function, and creating an object at the specified address.
In a third aspect of the embodiments of the present invention, there is provided a terminal apparatus, including a memory, a processor, and a computer program stored in the memory and executable on the processor, where the processor executes the computer program to implement the steps of the method according to the first aspect of the embodiments of the present invention.
In a fourth aspect of the embodiments of the present invention, a computer-readable storage medium is provided, in which a computer program is stored, and the computer program, when executed by a processor, implements the steps of the method provided in the first aspect of the embodiments of the present invention.
In a fifth aspect of embodiments of the present invention, a computer program product is provided, the computer program product comprising a computer program that, when executed by one or more processors, performs the steps of the method provided in the first aspect of embodiments of the present invention.
In the embodiment of the invention, the memory allocation interface is redefined and the function call interface is constructed, after the memory is applied, the byte alignment of the data stored in the memory is set, and then the memory address created by the object is specified. The creation of the object can be carried out on a specified memory, and after byte alignment, the access times of a CPU (central processing unit) to the memory can be reduced, so that the data reading efficiency, namely the object creation efficiency, is improved.
Drawings
In order to more clearly illustrate the technical solutions in the embodiments of the present invention, the drawings needed to be used in the embodiments or the prior art descriptions will be briefly described below, and it is obvious that the drawings in the following description are only some embodiments of the present invention, and it is obvious for those skilled in the art to obtain other drawings based on these drawings without inventive exercise.
Fig. 1 is a flowchart of an embodiment of an object creating method according to an embodiment of the present invention;
fig. 2 is a flowchart of another embodiment of an object creating method according to an embodiment of the present invention;
fig. 3 is a schematic structural diagram of an object creation terminal apparatus according to an embodiment of the present invention;
fig. 4 is a schematic structural diagram of a terminal device according to an embodiment of the present invention.
Detailed Description
The embodiment of the invention provides an object creating method, a terminal device and a storage medium, which are used for improving the object creating efficiency.
In order to make the objects, features and advantages of the present invention more obvious and understandable, the technical solutions in the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings in the embodiments of the present invention, and it is obvious that the embodiments described below are only a part of the embodiments of the present invention, and not all of the embodiments. All other embodiments, which can be derived by a person skilled in the art from the embodiments given herein without making any creative effort, shall fall within the protection scope of the present invention.
The first embodiment is as follows:
referring to fig. 1, a flow chart of an object creating method according to an embodiment of the present invention includes the following steps:
s101, redefining an interface of a memory allocation function of an object and calling an interface of a constructed function by an appointed address;
the object memory allocation function is used for allocating memory space for the created object, and the specified address calls the callback function to generate the object on the specified memory through the construction function. Generally, in the process of creating an object, that is, when the object is created by a new method, a memory space is randomly allocated to a stack or a stack area object in a system memory, and then a construction method is called to generate the object. In the embodiment of the present invention, the memory space of the object is a pre-application, and the memory space of a predetermined size can be applied by the object memory allocation function.
Preferably, the interface of the object memory allocation function is an operator new function interface, and the interface of the specified address call structure function is a place new interface.
S102, calling an interface of the object memory allocation function to apply for a memory space with a preset size;
and after applying for the memory space through the object memory allocation function, returning a memory pointer, wherein the memory pointer points to the initial address of the applied memory space. Specifically, the operator new function is called to apply for a static storage space with a predetermined size, and a memory address of the static storage space is returned.
S103, setting pre-stored data byte alignment in the memory space through an internal instruction of a system compiler;
the byte alignment means that data bytes are arranged on a memory space according to a certain rule, and in order to enable a CPU to quickly access memory data, a start address of the stored data has a predetermined characteristic. For example, 4 bytes of int-type data, the start address can be located at the boundary of 4 bytes, i.e. the start address can be divided by 4, under 32-bit cpu, it is assumed that the address of an integer variable is 0x00000004 (which is a multiple of 4), i.e. it is aligned, and if it is 0x00000002 (which is not a multiple of 4), it is not aligned. When the variable data are in the alignment position, the variable data can be taken out at one time, and the reading efficiency is improved.
Specifically, with the declspec command and align command, the aligned byte length is set. And calculating the maximum alignment value of the pre-alignment data type, structure body or class, and comparing the maximum alignment value with the specified alignment value to take a smaller value as the aligned byte length.
S104, calling an interface function of a construction function by using the specified address to set a first address of the memory space as a specified memory address, and creating an object at the specified address.
The interface function of the specified address calling construction function comprises parameters for setting the creation address of an object, the construction function calling of the specified address can be realized by taking a pre-applied memory address as the creation address, the construction function is used for initializing the object when the object is created, and the object creation and initialization of the specified address can be completed by calling the interface function of the construction function through the specified address. Specifically, calling the place new function to create an object at the specified address and perform object initialization operation.
In the above steps, by applying for the memory in advance and setting byte alignment in the memory, the POD characteristics (storage space continuity) of the memory address characters are ensured, memory fragments are reduced, and the object efficiency of creating the designated address can be effectively improved.
Example two
On the basis of fig. 1, a specific process of the method for improving the creation efficiency of the created object is detailed in conjunction with fig. 2, as follows:
s201, redefining an operator new function and a place new function;
when creating an object, the object is generally generated directly through a new operator, and in the new operator, two procedures are included: memory allocation and constructor invocation. And the function of the allocated memory in the new operator is operator new, and the function of the constructed object in the allocated memory is placement new.
Since the memory allocation of the object creation process is already completed during the program compilation, the object memory application operation can be reduced by redefining the operatornew function and the placement new function, and then the memory is applied in advance in combination with the redefined function.
S202, applying for a memory space;
and setting a memory space with a preset application size in the redefined operator new function for the subsequent object initialization operation.
Exemplarily, by void random memory operator new (size _ t size); a static memory area with size memory space can be applied for the parameter size _ t, and an unprocessed address pointer is returned.
S203, byte alignment;
in static memory, memory alignment is set and the compiler's internal instructions decspec and alignff may be used. Memory alignment is as follows: each member aligns according to its type size and the smaller of the specified alignment parameters (default 8 bytes), and the length size of the entire structure must be set to align for the rule of an integer multiple of all alignment parameters used in the structure. Preferably, a 32-bit byte alignment is set in embodiments of the present invention.
S204, specifying a memory address.
The memory address is set in the redefined placement new function as the applied memory address, that is, the first address of the memory space applied in S202, and after the memory address is specified, the object creation process is performed in the memory space.
The new (buffer) Object () interface is called to create and initialize the Object. Since the object creation address conforms to the POD property (a continuous storage space), the CPU access efficiency is improved, and further the object creation efficiency and the initialization efficiency on the memory are improved.
It should be understood that, the sequence numbers of the steps in the foregoing embodiments do not imply an execution sequence, and the execution sequence of each process should be determined by its function and inherent logic, and should not constitute any limitation to the implementation process of the embodiments of the present invention.
EXAMPLE III
The object creation method has been mainly described above, and a terminal apparatus for object creation will be described in detail below.
Fig. 3 shows a schematic structural diagram of a terminal device for object creation, including:
the definition module 310: an interface used for redefining the memory allocation function of the object and an interface used for calling a constructor by an appointed address;
the object memory allocation function is used for allocating memory space for the created object, and the specified address calls the callback function to generate the object on the specified memory through the construction function. Generally, in the process of creating an object, that is, when the object is created by a new method, a memory space is randomly allocated to a stack or a stack area object in a system memory, and then a construction method is called to generate the object. In the embodiment of the present invention, the memory space of the object is a pre-application, and the memory space of a predetermined size can be applied by the object memory allocation function.
Optionally, the interface of the object memory allocation function is an operator new function interface, and the interface of the specified address call structure function is a place new function interface.
Optionally, the interface of the object memory allocation function is an operator new function interface, further including:
and calling the operator new function to apply for a static storage space with a preset size, and returning the memory address of the static storage space.
Application module 320: an interface function for calling the object memory allocation function applies for a memory space with a preset size;
and after applying for the memory space through the object memory allocation function, returning a memory pointer, wherein the memory pointer points to the initial address of the applied memory space. Specifically, the operator new function is called to apply for a static storage space with a predetermined size, and a memory address of the static storage space is returned.
The alignment module 330: the system is used for setting the alignment of pre-stored data bytes in the memory space through an internal instruction of a system compiler;
the byte alignment means that data bytes are arranged on a memory space according to a certain rule, and in order to enable a CPU to quickly access memory data, a start address of the stored data has a predetermined characteristic.
Optionally, thealignment module 330 includes:
and the setting unit is used for setting the aligned byte length by using the declpsec command and the align command.
The setting module 340: and setting the first address of the memory space as a specified memory address by using an interface function for calling a construction function by using the specified address, and creating an object at the specified address.
The interface function of the specified address calling construction function comprises parameters for setting the creation address of an object, the construction function calling of the specified address can be realized by taking a pre-applied memory address as the creation address, the construction function is used for initializing the object when the object is created, and the object creation and initialization of the specified address can be completed by calling the interface function of the construction function through the specified address.
Optionally, the creating an object at the specified address specifically includes:
and calling a place new function to create an object at the specified address and carrying out object initialization operation.
In the embodiment of the present invention, the terminal device sets data byte alignment by applying for a memory in advance, so that the memory area is continuous, the CPU data access efficiency is improved, and the object creation efficiency is further improved.
Example four:
Fig. 4 is a schematic diagram of a structure of a terminal device for dynamic library loading according to an embodiment of the present invention. The terminal device is a mobile computer device with a touch screen, including but not limited to a smart phone, a smart watch, a notebook, a tablet computer, a POS machine, and even a vehicle-mounted computer. As shown in fig. 4, theterminal apparatus 4 of this embodiment includes: amemory 410, aprocessor 420, and asystem bus 430, thememory 410 including anexecutable program 4101 stored thereon, it being understood by those skilled in the art that the server architecture shown in FIG. 4 does not constitute a limitation of a server and may include more or fewer components than shown, or some components in combination, or a different arrangement of components.
The following specifically describes each constituent component of the terminal apparatus with reference to fig. 4:
thememory 410 may be used to store software programs and modules, and theprocessor 420 performs various functional applications of the terminal and data processing by operating the software programs and modules stored in thememory 410. Thememory 410 may mainly include a storage program area and a storage data area, wherein the storage program area may store an operating system, an application program required by at least one function (such as a sound playing function, an image playing function, etc.), and the like; the storage data area may store data (such as audio data, a phonebook, etc.) created according to the use of the server, and the like. Further, thememory 410 may include high speed random access memory, and may also include non-volatile memory, such as at least one magnetic disk storage device, flash memory device, or other volatile solid state storage device.
Anexecutable program 4101 of a network request method is contained on amemory 410, theexecutable program 4101 can be divided into one or more modules/units, which are stored in thememory 410 and executed by aprocessor 420 to improve object creation efficiency, and the one or more modules/units can be a series of computer program instruction segments for describing the execution process of thecomputer program 4101 in theserver 4, which can accomplish specific functions. For example, thecomputer program 4101 may be divided into a definition module, an application module, an alignment module, and a setting module.
Theprocessor 420 is a control center of the server, connects various parts of the entire terminal device using various interfaces and lines, performs various functions of the terminal and processes data by operating or executing software programs and/or modules stored in thememory 410 and calling data stored in thememory 410, thereby integrally monitoring the terminal. Alternatively,processor 420 may include one or more processing units; preferably, theprocessor 420 may integrate an application processor, which mainly handles operating systems, application programs, etc., and a modem processor, which mainly handles wireless communications. It will be appreciated that the modem processor described above may not be integrated intoprocessor 420.
Thesystem bus 430 is used to connect functional units inside the computer, and can transmit data information, address information, and control information, and may be, for example, a PCI bus, an ISA bus, a VESA bus, etc. The instructions of theprocessor 420 are transmitted to thememory 410 through the bus, thememory 410 feeds data back to theprocessor 420, and thesystem bus 430 is responsible for data and instruction interaction between theprocessor 420 and thememory 410. Of course, thesystem bus 430 may also access other devices such as network interfaces, display devices, and the like.
The terminal device at least comprises a network card, an output device and the like, and other components are not described in detail herein.
In this embodiment of the present invention, the executable program executed by theprocessor 420 included in the terminal device specifically includes:
an object creation method, comprising:
redefining the interface of the object memory allocation function and the interface of the specified address calling construction function;
calling the object memory allocation function to apply for a memory space with a preset size;
setting pre-stored data byte alignment in the memory space through an internal instruction of a system compiler;
and setting the first address of the memory space as a specified memory address by using the interface function of the specified address calling construction function, and creating an object at the specified address.
Further, the interface of the object memory allocation function is an operator new function interface, and the interface of the specified address call structure function is a place new function interface.
Further, the interface of the object memory allocation function is an operator new function interface, which further includes:
and calling the operator new function to apply for a static storage space with a preset size, and returning the memory address of the static storage space.
Further, the setting of the alignment of the pre-stored data bytes in the memory space through the internal instruction of the system compiler specifically includes:
with the declspec command and align command, the aligned byte length is set.
Further, the creating an object at the specified address specifically includes:
and calling a place new function to create an object at the specified address and carrying out object initialization operation.
It is clear to those skilled in the art that, for convenience and brevity of description, the specific working processes of the above-described systems, apparatuses and units may refer to the corresponding processes in the foregoing method embodiments, and are not described herein again.
In the above embodiments, the descriptions of the respective embodiments have respective emphasis, and reference may be made to the related descriptions of other embodiments for parts that are not described or illustrated in a certain embodiment.
Those of ordinary skill in the art would appreciate that the modules, elements, and/or method steps of the various embodiments described in connection with the embodiments disclosed herein may be implemented as electronic hardware, or combinations of computer software and electronic hardware. Whether such functionality is implemented as hardware or software depends upon the particular application and design constraints imposed on the implementation. Skilled artisans may implement the described functionality in varying ways for each particular application, but such implementation decisions should not be interpreted as causing a departure from the scope of the present invention.
In the several embodiments provided in the present application, it should be understood that the disclosed system, apparatus and method may be implemented in other manners. For example, the above-described apparatus embodiments are merely illustrative, and for example, the division of the units is only one logical division, and other divisions may be realized in practice, for example, a plurality of units or components may be combined or integrated into another system, or some features may be omitted, or not executed. In addition, the shown or discussed mutual coupling or direct coupling or communication connection may be an indirect coupling or communication connection through some interfaces, devices or units, and may be in an electrical, mechanical or other form.
The units described as separate parts may or may not be physically separate, and parts displayed as units may or may not be physical units, may be located in one place, or may be distributed on a plurality of network units. Some or all of the units can be selected according to actual needs to achieve the purpose of the solution of the embodiment.
In addition, functional units in the embodiments of the present invention may be integrated into one processing unit, or each unit may exist alone physically, or two or more units are integrated into one unit. The integrated unit can be realized in a form of hardware, and can also be realized in a form of a software functional unit.
The integrated unit, if implemented in the form of a software functional unit and sold or used as a stand-alone product, may be stored in a computer readable storage medium. Based on such understanding, all or part of the flow of the method according to the embodiments of the present invention may also be implemented by a computer program, which may be stored in a computer-readable storage medium, and when the computer program is executed by a processor, the steps of the method embodiments may be implemented. . Wherein the computer program comprises computer program code, which may be in the form of source code, object code, an executable file or some intermediate form, etc. The computer-readable medium may include: any entity or device capable of carrying the computer program code, recording medium, usb disk, removable hard disk, magnetic disk, optical disk, computer Memory, Read-Only Memory (ROM), Random Access Memory (RAM), electrical carrier wave signals, telecommunications signals, software distribution medium, etc. It should be noted that the computer readable medium may contain content that is subject to appropriate increase or decrease as required by legislation and patent practice in jurisdictions, for example, in some jurisdictions, computer readable media does not include electrical carrier signals and telecommunications signals as is required by legislation and patent practice.
The above-mentioned embodiments are only used for illustrating the technical solutions of the present invention, and not for limiting the same; although the present invention has been described in detail with reference to the foregoing embodiments, it will be understood by those of ordinary skill in the art that: the technical solutions described in the foregoing embodiments may still be modified, or some technical features may be equivalently replaced; and such modifications or substitutions do not depart from the spirit and scope of the corresponding technical solutions of the embodiments of the present invention.