A method and device for realizing data exchange between business layer and so library
By using off-heap memory blocks for data exchange between the business layer and the so library, and using the shared memory mechanism and protocol headers, the problem that the parameters passed by the Jni interface are easily decompiled is solved, the safe transmission and exchange of data is realized, and the Data security.
Patent Information
- Application Number
- CN202011434701.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2020-12-10
- Publication Date
- 2024-10-25
- Estimated Expiration
- 2040-12-10
AI Technical Summary
In application development, when using the Jni interface to call the so library, the passed parameters are easily decompiled, which poses security risks, and dynamic breakpoint debugging may capture the passed data, threatening data security.
By using off-heap fixed memory blocks and off-heap dynamic memory blocks for data exchange between the business layer and the so library, the shared memory mechanism is used without exposing the data transfer interface, and fixed memory protocol headers and dynamic memory protocol headers are defined to ensure that the data is Specific format exchange in memory to enhance data security.
It effectively guarantees the security of data, avoids the data transfer interface from being exposed, and ensures the security and integrity of data during the transfer process.
Smart Images

Figure CN112559203B_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of communications, and in particular to a technology for implementing data exchange between a business layer and an SO (shared object) library. Background Art
[0002] In application development, the Jni (Java Native Interface) interface is used to call the so library. Currently, the industry generally defines the Jni interface and passes data through interface parameters or return values. These passed parameters can easily be decompiled and viewed through the interface, and then the dynamic breakpoint debugging is used to capture the passed data, which poses a huge security risk. Summary of the invention
[0003] One purpose of the present application is to provide a method and device for realizing data exchange between a business layer and an SO library.
[0004] According to one aspect of the present application, a method for implementing data exchange with an so library in a business layer is provided, wherein the method comprises:
[0005] Apply for a heap of external fixed memory blocks, call the JNI interface of the corresponding so library, and pass the address of the external fixed memory block to the so library;
[0006] Apply for an off-heap dynamic memory block, write the business data agreed with the so library into the off-heap dynamic memory block, and write the address of the off-heap dynamic memory block into the off-heap fixed memory block;
[0007] When it is detected that the content of the off-heap fixed memory block has changed, the address of the off-heap dynamic memory block is parsed from the off-heap fixed memory block, wherein the off-heap dynamic memory block has been changed by the so library;
[0008] According to the address of the off-heap dynamic memory block, read the content in the off-heap dynamic memory block, and parse the business type and data according to a predetermined protocol, wherein the business type and data are business results obtained by the so library executing the corresponding business;
[0009] Destroy the off-heap dynamic memory block.
[0010] According to another aspect of the present application, a method for implementing data exchange with a business layer in an so library is also provided, wherein the method comprises:
[0011] Receive the address of the fixed memory block outside the heap passed by the corresponding business layer by calling the Jni interface;
[0012] When it is detected that the content of the off-heap fixed memory block changes, the address of the corresponding off-heap dynamic memory block is parsed from the off-heap fixed memory block;
[0013] According to the address of the off-heap dynamic memory block, read the content in the off-heap dynamic memory block, parse the service type and data according to a predetermined protocol, and execute the service to obtain a corresponding service result;
[0014] Destroy the off-heap dynamic memory block and re-apply for a new off-heap dynamic memory block;
[0015] The business result is written into the new off-heap dynamic memory block, and the address of the new off-heap dynamic memory block is written into the off-heap fixed memory block.
[0016] According to another aspect of the present application, a device for implementing data exchange with an so library in a business layer is also provided, wherein the device includes:
[0017] A first application device is used to apply for a fixed memory block outside the heap, call the JNI interface of the corresponding so library, and pass the address of the fixed memory block outside the heap to the so library;
[0018] A second application device is used to apply for an off-heap dynamic memory block, write the business data agreed with the so library into the off-heap dynamic memory block, and write the address of the off-heap dynamic memory block into the off-heap fixed memory block;
[0019] A first parsing device is used for parsing the address of the off-heap dynamic memory block from the off-heap fixed memory block when it is detected that the content of the off-heap fixed memory block has changed, wherein the off-heap dynamic memory block has been changed by the so library;
[0020] A first reading device is used to read the content in the off-heap dynamic memory block according to the address of the off-heap dynamic memory block, and parse out the business type and data according to a predetermined protocol, wherein the business type and data are business results obtained by the so library executing the corresponding business;
[0021] A destroying device is used to destroy the off-heap dynamic memory block.
[0022] According to another aspect of the present application, a device for implementing data exchange with a business layer in an so library is also provided, wherein the device includes:
[0023] A receiving device, used to receive the address of the off-heap fixed memory block transmitted by the corresponding business layer by calling the Jni interface;
[0024] A second parsing device is used to parse the address of the corresponding off-heap dynamic memory block from the off-heap fixed memory block when it is detected that the content of the off-heap fixed memory block changes;
[0025] A second reading device is used to read the content in the off-heap dynamic memory block according to the address of the off-heap dynamic memory block, parse the service type and data according to a predetermined protocol, and execute the service to obtain a corresponding service result;
[0026] A third application device is used to destroy the off-heap dynamic memory block and re-apply for a new off-heap dynamic memory block;
[0027] A writing device is used to write the business result into the new off-heap dynamic memory block, and write the address of the new off-heap dynamic memory block into the off-heap fixed memory block.
[0028] According to another aspect of the present application, a device for implementing data exchange with a service layer is also provided, wherein the device includes:
[0029] processor, and
[0030] A memory arranged to store computer executable instructions which, when executed, cause the processor to perform a method as described in any one of the above.
[0031] According to another aspect of the present application, a computer-readable medium storing instructions is also provided. When the instructions are executed by a computer, the computer performs the operation of any of the above methods.
[0032] Compared with the prior art, the present application provides a method for data exchange between a business layer and a so library, which completes data exchange and transmission through shared memory, does not expose the data transmission interface, and can well ensure data security. Furthermore, by defining a fixed memory protocol header and a dynamic memory protocol header, the business layer and the so library can exchange data in a specific format in memory, further ensuring data security. BRIEF DESCRIPTION OF THE DRAWINGS
[0033] Other features, objects and advantages of the present application will become more apparent by reading the detailed description of non-limiting embodiments made with reference to the following drawings:
[0034] Figure 1 A flow chart of a method for implementing data exchange with an so library in a business layer according to an embodiment of the present application is shown;
[0035] Figure 2 A flow chart of a method for implementing data exchange with a business layer in an so library according to an embodiment of the present application is shown;
[0036] Figure 3 A structural diagram of a device for implementing data exchange with an so library in a business layer according to an embodiment of the present application is shown;
[0037] Figure 4 A structural diagram of a device for implementing data exchange with a business layer in an so library according to an embodiment of the present application is shown;
[0038] Figure 5 A schematic diagram showing a process of data exchange between the business layer and the so library according to an embodiment of the present application;
[0039] Figure 6 An exemplary system is shown that can be used to implement the various embodiments described in this application.
[0040] The same or similar reference numerals in the drawings represent the same or similar components. DETAILED DESCRIPTION
[0041] The present application is described in further detail below in conjunction with the accompanying drawings.
[0042] In a typical configuration of the present application, the terminal, the device of the service network and the trusted party all include one or more processors (eg, a central processing unit (CPU)), an input / output interface, a network interface and a memory.
[0043] Memory may include non-permanent memory in a computer-readable medium, random access memory (RAM) and / or non-volatile memory in the form of read-only memory (ROM) or flash memory. Memory is an example of a computer-readable medium.
[0044] Computer readable media include permanent and non-permanent, removable and non-removable media that can be used to store information by any method or technology. Information can be computer readable instructions, data structures, modules of programs or other data. Examples of computer storage media include, but are not limited to, Phase-Change Memory (PCM), Programmable Random Access Memory (PRAM), Static Random Access Memory (SRAM), Dynamic Random Access Memory (DRAM), other types of random access memory (RAM), Read-Only Memory (ROM), Electrically-Erasable Programmable Read-Only Memory (EEPROM), Flash memory or other memory technology, Compact Disc Read-Only Memory (CD-ROM), Digital Versatile Disc (DVD) or other optical storage, magnetic cassettes, tape disk storage or other magnetic storage devices or any other non-transmission media that can be used to store information that can be accessed by a computing device.
[0045] The devices referred to in this application include but are not limited to user devices, network devices, or devices formed by integrating user devices and network devices through a network. The user devices include but are not limited to any mobile electronic products that can interact with users (for example, interact with users through a touchpad), such as smart phones, tablet computers, etc. The mobile electronic products can use any operating system, such as Android operating system, iOS operating system, etc. Among them, the network device includes an electronic device that can automatically perform numerical calculations and information processing according to pre-set or stored instructions, and its hardware includes but is not limited to microprocessors, application specific integrated circuits (Application Specific Integrated Circuit, ASIC), programmable logic devices (Programmable Logic Device, PLD), field programmable gate arrays (Field Programmable Gate Array, FPGA), digital signal processors (Digital Signal Processor, DSP), embedded devices, etc. The network devices include but are not limited to computers, network hosts, single network servers, multiple network server sets or multiple servers. The cloud is composed of a large number of computers or network servers based on cloud computing (Cloud Computing), wherein cloud computing is a type of distributed computing, a virtual supercomputer composed of a group of loosely coupled computer sets. The network includes but is not limited to the Internet, a wide area network, a metropolitan area network, a local area network, a VPN network, a wireless self-organizing network (Ad Hoc network), etc. Preferably, the device may also be a program running on the user device, the network device, or a device formed by integrating the user device and the network device, the network device, the touch terminal, or the network device and the touch terminal through a network.
[0046] Of course, those skilled in the art should understand that the above-mentioned devices are only examples, and other existing or future devices that are applicable to the present application should also be included in the scope of protection of the present application and are included here by reference.
[0047] In the description of the present application, “plurality” means two or more, unless otherwise clearly and specifically defined.
[0048] Figure 1A flow chart of a method for implementing data exchange with an so library in a business layer according to an embodiment of the present application is shown. The method includes step S11, step S12, step S13, step S14, and step S15. In step S11, the business layer applies for a fixed off-heap memory block, calls the JNI interface of the corresponding so library, and passes the address of the fixed off-heap memory block to the so library; in step S12, the business layer applies for a dynamic off-heap memory block, writes the business data agreed with the so library into the dynamic off-heap memory block, and writes the address of the dynamic off-heap memory block into the fixed off-heap memory block; in step S13, when the business layer detects that the content of the fixed off-heap memory block has changed, it parses the address of the dynamic off-heap memory block from the fixed off-heap memory block, wherein the dynamic off-heap memory block has been changed by the so library; in step S14, the business layer reads the content of the dynamic off-heap memory block according to the address of the dynamic off-heap memory block, and parses the business type and data according to a predetermined protocol, wherein the business type and data are the business results obtained by the so library executing the corresponding business; in step S15, the business layer destroys the dynamic off-heap memory block.
[0049] In step S11, when an application, such as an Android application, requires the support of the NDK (Native Development Kit) so library, the business layer applies for a fixed memory block outside the heap, and calls the Jni interface of the corresponding so library to pass the address of the fixed memory block outside the heap to the so library. The size of the fixed memory block outside the heap can be, for example, 12 bytes, of which 4 bytes store the identifier and 8 bytes store the address of the dynamic memory block outside the heap, which will be described in detail below. The so library writes a Jni interface with a long integer parameter to pass the address of the fixed memory block outside the heap.
[0050] Those skilled in the art should understand that the sizes and stored contents of the above-mentioned off-heap fixed memory blocks are only examples and should not be regarded as limitations on the present embodiment. Other existing or future off-heap fixed memory block sizes and stored contents, if applicable to the present embodiment, should also be included in the protection scope of the present embodiment and are included herein by reference.
[0051] In step S12, when the upper layer business of the application needs the corresponding so library to process, the business layer applies for a heap of external dynamic memory blocks, and writes the business data agreed with the so library into the said external dynamic memory blocks. For example, if the business layer needs the so library to encrypt a business data, then when there is such a business demand, the business layer applies for a heap of external dynamic memory blocks, and writes the business data to be encrypted into the external dynamic memory blocks, and then writes the address of the external dynamic memory blocks into the external fixed memory blocks applied for in step S11.
[0052] In some embodiments, in step S12, the business layer encapsulates the business data agreed with the so library according to the dynamic memory protocol header, and writes the encapsulated business data into the off-heap dynamic memory block; encapsulates the address of the off-heap dynamic memory block according to the fixed memory protocol header, and writes the encapsulated address of the off-heap dynamic memory block into the off-heap fixed memory block.
[0053] Here, a dynamic memory protocol header is defined. The business layer encapsulates the business data agreed with the so library according to the dynamic memory protocol header, and then writes the encapsulated business data into the off-heap dynamic memory block. In this way, when the business data needs to be read and parsed from the off-heap dynamic memory block later, it can also be done according to the dynamic memory protocol header.
[0054] Here, a fixed memory protocol header is defined, and the business layer encapsulates the address of the dynamic memory block according to the fixed memory protocol header, and writes the encapsulated address into the fixed memory block outside the heap. In this way, when the address of the dynamic memory block needs to be read and parsed from the fixed memory block outside the heap later, it can also be done according to the fixed memory protocol header.
[0055] In some embodiments, the dynamic memory protocol header includes a task index of a predetermined byte, a task type of a predetermined byte, a number of data structures of a predetermined byte, and business data content of a predetermined byte; the fixed memory protocol header includes an identification bit of a predetermined byte and a memory address of a predetermined byte.
[0056] For example, the dynamic memory protocol header includes a 4-byte task index, a 4-byte task type, a 4-byte data structure number, and several bytes of service data binary content. The fixed memory protocol header includes a 4-byte identification bit and an 8-byte memory address.
[0057] In some embodiments, the business data content included in the dynamic memory protocol header includes several copies of the following content: a business length value of a predetermined byte, a data type of a predetermined byte, and business content of several bytes. For example, the business data content includes a business data length value of 4 bytes, a data type of 2 bytes, and business content of several bytes. Alternatively, the business data content may include multiple business data, such as an example, the business data content includes: a business data length value of 4 bytes + a data type of 2 bytes + a business content of N1 bytes; a business data length value of 4 bytes + a data type of 2 bytes + a business content of N2 bytes + ... + a business data length value of 4 bytes + a data type of 2 bytes + a business content of Nx bytes.
[0058] Those skilled in the art should understand that the above-mentioned dynamic memory protocol header and fixed memory protocol header are only examples and should not be regarded as limitations on the present embodiment. Other existing or future dynamic memory protocol headers and fixed memory protocol headers, if applicable to the present embodiment, should also be included in the protection scope of the present embodiment and are included here by reference.
[0059] In step S13, when the business layer detects that the content of the off-heap fixed memory block has changed, it indicates that the address of the off-heap dynamic memory block stored therein has changed. Therefore, the business layer parses the address of the off-heap dynamic memory block from the off-heap fixed memory block, for example, according to the aforementioned fixed memory protocol header, the address of the off-heap dynamic memory block is parsed from the off-heap fixed memory block, wherein the off-heap dynamic memory block has been changed by the so library. Here, after the so library executes the business and obtains the corresponding business result, it destroys the original off-heap dynamic memory block, and re-applies for a new off-heap dynamic memory block, writes the business result into the new off-heap dynamic memory block, and writes the address of the new off-heap dynamic memory block into the off-heap fixed memory block. Therefore, in step S13, the business layer can detect that the content of the off-heap fixed memory block has changed, and parses a new off-heap dynamic memory block therefrom.
[0060] In some embodiments, after the business layer applies for the off-heap fixed memory block, an initial value is assigned to the off-heap fixed memory block; then, the business layer starts a monitoring thread to monitor whether the data in the off-heap fixed memory block has changed compared with the initial value.
[0061] Here, the business layer assigns an initial value to the fixed memory block outside the heap after applying for the fixed memory block outside the heap, so as to judge whether the fixed memory block outside the heap has changed in the future. When the business layer has business data that needs to be sent to the corresponding so library for processing, the monitoring thread can be turned on to monitor the fixed memory block outside the heap. Since the so library will update the dynamic memory block outside the heap after completing the processing of the business data, and then update the fixed memory block outside the heap, the business layer needs to turn on the monitoring thread to monitor whether the data in the fixed memory block outside the heap has changed compared with the initial value. Of course, when the business layer has business data sent to the so library for processing next time, what the monitoring thread monitors is whether the data in the fixed memory block outside the heap has changed compared with the last data. The monitoring thread can be always kept open, and it can be turned on after the fixed memory block outside the heap is successfully applied, or it can be turned on again after the business layer has business sent to the so library for processing.
[0062] In step S14, the business layer reads the content in the off-heap dynamic memory block according to the address of the off-heap dynamic memory block parsed in step S13, and parses the business type and data according to a predetermined protocol, for example, according to the aforementioned dynamic memory protocol header, wherein the business type and data are the business results obtained by the so library executing the corresponding business. Following the aforementioned description of step S13, after the so library executes the business to obtain the corresponding business result, the original off-heap dynamic memory block is destroyed, and a new off-heap dynamic memory block is reapplied, the business result is written into the new off-heap dynamic memory block, and the address of the new off-heap dynamic memory block is written into the fixed memory block outside the heap. Therefore, in step S13, the business layer can detect that the content of the fixed memory block outside the heap has changed, and parse the new off-heap dynamic memory block therefrom. In step S14, the business layer reads the content from the new off-heap dynamic memory block again, and parses the business type and data therefrom, and the business type and data are the business results obtained by the so library executing the business.
[0063] In step S15, when the business layer obtains the parsed business type and data, that is, obtains the business result obtained by the so library executing the corresponding business, it marks the complete end of a business, and the business layer destroys the off-heap dynamic memory block.
[0064] The above embodiment uses shared memory to complete the exchange and transfer of data between the business layer and the so library, does not expose the data transfer interface, and can well ensure the security of data. Furthermore, by defining a fixed memory protocol header and a dynamic memory protocol header, the business layer and the so library can exchange data in a specific format in the memory, further ensuring the security of the data.
[0065] In a preferred embodiment, when there is a new business, the business layer re-applies for a new off-heap dynamic memory block and repeats the operations of the aforementioned steps S12 to S15.
[0066] Here, since the business layer has applied for an off-heap fixed memory block, and the address of the off-heap fixed memory block is passed to the so library by calling the Jni interface, when there is a new business, the business layer does not need to reapply for a new off-heap fixed memory block. Since the business layer has destroyed the off-heap dynamic memory block after the last business was completed, when there is a new business this time, the business layer needs to reapply for a new off-heap dynamic memory block, write the business data of this time into the off-heap dynamic memory block, and then write the address of the off-heap dynamic memory block into the off-heap fixed memory block. Subsequently, the so library detects that the content of the off-heap fixed memory block has changed, parses the address of the new off-heap dynamic memory block from it, and then parses the business data of this time from the new off-heap dynamic memory block, executes the business and obtains the business result, and then reapplies for a new off-heap dynamic memory block and destroys the previous off-heap dynamic memory block; then, the so library writes the business result into the new off-heap dynamic memory block applied by the so library, and writes the address of the new off-heap dynamic memory block into the off-heap fixed memory block. Subsequently, the business layer detects that the content of the off-heap fixed memory block has changed, and then parses the address of the off-heap dynamic memory block that has been changed by the so library from the off-heap fixed memory block. According to the address of the off-heap dynamic memory block, the business result of the so library in the off-heap dynamic memory block is read; once again, the business is completed, and the business layer destroys the off-heap dynamic memory block again.
[0067] Figure 2 A flow chart of a method for implementing data exchange with a business layer in a so library according to an embodiment of the present application is shown, and the method includes steps S21, S22, S23, S24, and S25. In step S21, the so library receives the address of the off-heap fixed memory block transmitted by the corresponding business layer by calling the Jni interface; in step S22, when the so library detects that the content of the off-heap fixed memory block has changed, the address of the corresponding off-heap dynamic memory block is parsed from the off-heap fixed memory block; in step S23, the so library reads the content of the off-heap dynamic memory block according to the address of the off-heap dynamic memory block, parses the business type and data according to the predetermined protocol, and executes the business to obtain the corresponding business result; in step S24, the so library destroys the off-heap dynamic memory block and reapplies for a new off-heap dynamic memory block; in step S25, the so library writes the business result into the new off-heap dynamic memory block, and writes the address of the new off-heap dynamic memory block into the off-heap fixed memory block.
[0068] In step S21, when an application, such as an Android application, requires support from the NDK (Native Development Kit) so library, the business layer applies for a heap of external fixed memory blocks and calls the corresponding Jni interface of the so library to pass the address of the external fixed memory block to the so library. The so library receives the address of the external fixed memory block passed by the corresponding business layer by calling the Jni interface.
[0069] In some embodiments, the so library writes a Jni interface, and the parameter of the Jni interface is a long integer, which is used to pass the address of the fixed memory block outside the heap.
[0070] In step S22, since the corresponding business layer applies for an off-heap dynamic memory block and encapsulates the address of the off-heap dynamic memory block and writes it into the off-heap fixed memory block, the so library can detect that the content of the off-heap fixed memory block has changed. When the so library detects the change, it parses the address of the corresponding off-heap dynamic memory block from the off-heap fixed memory block. Here, the fixed memory protocol header and dynamic memory protocol header according to which the encapsulation and parsing are based can refer to the description of this in the aforementioned embodiment.
[0071] In some embodiments, the so library starts a monitoring thread to monitor whether the data in the off-heap fixed memory block changes. The so library can start a monitoring thread to monitor the changes of the off-heap fixed memory block after receiving the address of the off-heap fixed memory block.
[0072] In step S23, the so library reads the content in the off-heap dynamic memory block according to the address of the off-heap dynamic memory block, and parses the business type and data according to a predetermined protocol, for example, the dynamic memory protocol header mentioned above. Subsequently, the so library executes the corresponding business according to the business type and data to obtain the business result.
[0073] In step S24, the so library destroys the current off-heap dynamic memory block and re-applies for a new off-heap dynamic memory block so that the two have the same name.
[0074] In step S25, the so library writes the business results obtained by executing the business in step S23 into the new off-heap dynamic memory block, and writes the address of the new off-heap dynamic memory block into the off-heap fixed memory block. Correspondingly, the business layer detects that the content of the off-heap fixed memory block has changed, and parses the address of the off-heap dynamic memory block re-applied by the so library from the off-heap fixed memory block. According to the address of the new off-heap dynamic memory block, the business results of the so library in the new off-heap dynamic memory block are read, indicating that a business has been completely completed, and the business layer destroys the off-heap dynamic memory block.
[0075] In some embodiments, in step S25, the so library encapsulates the business results obtained by executing the business according to the dynamic memory protocol header, and writes the encapsulated business results into the new off-heap dynamic memory block; encapsulates the address of the new off-heap dynamic memory block according to the fixed memory protocol header, and writes the address of the encapsulated new off-heap dynamic memory block into the off-heap fixed memory block.
[0076] The dynamic memory protocol header and the fixed memory protocol header here are the same or substantially the same as the dynamic memory protocol header and the fixed memory protocol header in the aforementioned embodiment, so they are not described again here and are included here by reference.
[0077] A business layer interacts with the so library to implement data exchange, for example Figure 5 As shown:
[0078] After the so library business is written, the so library writes a Jni interface initJni(long), that is, the parameter of the Jni interface is a long integer, in which the address of the off-heap fixed memory block that the Android business layer is about to send is stored;
[0079] After the Android layer business is written, when the Android application needs the support of the so library, the business layer applies for a 12-byte off-heap fixed memory block, assigns an initial value to the off-heap fixed memory block, and calls the corresponding so library's Jni interface to pass the address of the off-heap fixed memory block to the so library;
[0080] When the upper-layer business of the Android application needs to be processed by the SO library, a heap off-heap dynamic memory block is requested, the business data agreed with the SO library is encapsulated according to the dynamic memory protocol header, and the encapsulated business data is written into the off-heap dynamic memory block. At the same time, the data is encapsulated according to the fixed memory protocol header, and the address of the off-heap dynamic memory block is written into the off-heap fixed memory block;
[0081] The business layer starts a monitoring thread to monitor the content of the off-heap dynamic memory block and compares it with the initial value or the address of the previously encapsulated off-heap dynamic memory block.
[0082] Correspondingly, after receiving the address of the off-heap fixed memory block, the so library starts a monitoring thread to monitor whether the data in the off-heap dynamic memory block has changed compared with the initial value;
[0083] When the monitoring thread in the so library detects that the content of the off-heap dynamic memory block has changed, it reads the address of the off-heap dynamic memory block from the off-heap fixed memory block according to the definition of the fixed memory protocol header;
[0084] The so library accesses the off-heap dynamic memory block based on the address, reads the binary data in it, and parses the business type and data based on the dynamic memory protocol header, performs the corresponding business operation according to the business agreement, and obtains the business result;
[0085] After the so library completes the business operation, it destroys the current off-heap dynamic memory block and re-applies for a new off-heap dynamic memory block with the same name. It then encapsulates the business result according to the dynamic memory protocol header and writes it into the new off-heap dynamic memory block.
[0086] The so library then encapsulates the address of the new off-heap dynamic memory block according to the fixed memory protocol header and writes it into the off-heap fixed memory block;
[0087] Correspondingly, when the monitoring thread in the business layer monitors that the content of the off-heap fixed memory block has changed, it reads the address of the new off-heap dynamic memory block according to the definition of the fixed memory protocol header;
[0088] The business layer reads the binary data in the off-heap dynamic memory block and parses the business type and data according to the definition of the dynamic memory protocol header;
[0089] After the business layer obtains the parsed business type and data, it marks the complete end of a business and destroys the off-heap dynamic memory block.
[0090] Here, the Figure 5 The terms such as memory block 1 and off-heap memory block 1 recorded in the specification refer to off-heap fixed memory blocks, and the terms such as memory block 2 and dynamic memory 2 recorded in the specification refer to off-heap dynamic memory blocks.
[0091] Figure 3A structural diagram of a device for implementing data exchange with an so library in a business layer according to an embodiment of the present application is shown. Device 1 is located in the business layer, and includes a first application device 31, a second application device 32, a first parsing device 33, a first reading device 34 and a destruction device 35. The first application device 31 applies for a fixed memory block outside the heap, calls the Jni interface of the corresponding so library, and passes the address of the fixed memory block outside the heap to the so library; the second application device 32 applies for a dynamic memory block outside the heap, writes the business data agreed with the so library into the dynamic memory block outside the heap, and writes the address of the dynamic memory block outside the heap into the fixed memory block outside the heap; the first parsing device 33 parses the address of the dynamic memory block outside the heap from the fixed memory block outside the heap when it is detected that the content of the fixed memory block outside the heap has changed, wherein the dynamic memory block outside the heap has been changed by the so library; the first reading device 34 reads the content of the dynamic memory block outside the heap according to the address of the dynamic memory block outside the heap, and parses the business type and data according to a predetermined protocol, wherein the business type and data are the business results obtained by the so library executing the corresponding business; the destruction device 35 destroys the dynamic memory block outside the heap. Here, the Figure 3 The specific implementations corresponding to the first application device 31, the second application device 32, the first parsing device 33, the first reading device 34 and the destruction device 35 are respectively the same as or similar to the specific implementations of the aforementioned steps S11, S12, S13, S14 and S15, so they are not repeated here and are included herein by reference.
[0092] In some embodiments, after the second application device 32 applies for the off-heap fixed memory block, the second application device 32 assigns an initial value to the off-heap fixed memory block; the device 1 further includes a first monitoring device (not shown). The first monitoring device starts a monitoring thread to monitor whether the data in the off-heap fixed memory block has changed compared with the initial value.
[0093] Here, the second application device 32 assigns an initial value to the fixed memory block outside the heap after applying for the fixed memory block outside the heap, so as to judge whether the fixed memory block outside the heap has changed in the follow-up. When the business layer has business data that needs to be sent to the corresponding so library for processing, the first monitoring device can start the monitoring thread to monitor the fixed memory block outside the heap, because the so library will update the dynamic memory block outside the heap after completing the processing of the business data, and then update the fixed memory block outside the heap, therefore, the first monitoring device in the business layer needs to start the monitoring thread to monitor whether the data in the fixed memory block outside the heap has changed compared with the initial value. Of course, when the business layer has business data sent to the so library for processing next time, what the monitoring thread monitors is whether the data in the fixed memory block outside the heap has changed compared with the last data. The monitoring thread can be always kept open, and it can be opened by the first monitoring device after the fixed memory block outside the heap is successfully applied, or it can be opened by the first monitoring device after the business layer has business sent to the so library for processing.
[0094] In some embodiments, the second application device 32 further includes a first encapsulation unit (not shown) and a second encapsulation unit (not shown). The first encapsulation unit encapsulates the business data agreed with the so library according to the dynamic memory protocol header, and writes the encapsulated business data into the off-heap dynamic memory block; the second encapsulation unit encapsulates the address of the off-heap dynamic memory block according to the fixed memory protocol header, and writes the encapsulated address of the off-heap dynamic memory block into the off-heap fixed memory block.
[0095] Here, a dynamic memory protocol header is defined, and the first encapsulation unit encapsulates the business data agreed with the so library according to the dynamic memory protocol header, and then writes the encapsulated business data into the off-heap dynamic memory block. In this way, when the business data needs to be read and parsed from the off-heap dynamic memory block later, it can also be done according to the dynamic memory protocol header.
[0096] Here, a fixed memory protocol header is defined, and the second encapsulation unit encapsulates the address of the dynamic memory block according to the fixed memory protocol header, and writes the encapsulated address into the fixed memory block outside the heap. In this way, when the address of the dynamic memory block needs to be read and parsed from the fixed memory block outside the heap later, it can also be done according to the fixed memory protocol header.
[0097] The dynamic memory protocol header and the fixed memory protocol header here are the same or substantially the same as the dynamic memory protocol header and the fixed memory protocol header in the aforementioned embodiment, so they are not described again here and are included here by reference.
[0098] In a preferred embodiment, the device 1 further comprises a calling device (not shown). When there is a new business, the calling device re-applies for a new off-heap dynamic memory block and calls the aforementioned second applying device 32, the first parsing device 33, the first reading device 34 and the destroying device 35 to repeat their operations.
[0099] Here, since the first application device 31 of the business layer has applied for an off-heap fixed memory block, and the address of the off-heap fixed memory block has been passed to the so library by calling the Jni interface, when there is a new business, the business layer does not need to reapply for a new off-heap fixed memory block. And since the destruction device 35 of the business layer has destroyed the off-heap dynamic memory block after the last business was completely completed, when there is a new business this time, the second application device 32 of the business layer needs to reapply for a new off-heap dynamic memory block, write the business data of this time into the off-heap dynamic memory block, and then write the address of the off-heap dynamic memory block into the off-heap fixed memory block. Subsequently, the so library detects that the content of the off-heap fixed memory block has changed, and parses out the address of the new off-heap dynamic memory block, and then parses out the business data of this time from the new off-heap dynamic memory block, executes the business and obtains the business result, and then re-applies for a new off-heap dynamic memory block and destroys the previous off-heap dynamic memory block; then, the so library writes the business result into the new off-heap dynamic memory block applied by the so library, and writes the address of the new off-heap dynamic memory block into the off-heap fixed memory block. Subsequently, the first monitoring device of the business layer detects that the content of the off-heap fixed memory block has changed, and the first parsing device 33 parses out the address of the off-heap dynamic memory block that has been changed by the so library from the off-heap fixed memory block, and the first reading device 34 reads the business result of the so library in the off-heap dynamic memory block according to the address of the off-heap dynamic memory block; once again, the business is completely completed, and the destruction device 35 of the business layer once again destroys the off-heap dynamic memory block.
[0100] Figure 4A structural diagram of a device for implementing data exchange with a business layer in an so library according to an embodiment of the present application is shown; the device 2 is located in the so library, and includes a receiving device 41, a second parsing device 42, a second reading device 43, a third application device 44 and a writing device 45. The receiving device 41 receives the address of an off-heap fixed memory block transmitted by the corresponding business layer by calling the Jni interface; the second parsing device 42 parses the address of the corresponding off-heap dynamic memory block from the off-heap fixed memory block when it detects that the content of the off-heap fixed memory block has changed; the second reading device 43 reads the content of the off-heap dynamic memory block according to the address of the off-heap dynamic memory block, parses the business type and data according to a predetermined protocol, executes the business and obtains the corresponding business result; the third application device 44 destroys the off-heap dynamic memory block and reapplies for a new off-heap dynamic memory block; the writing device 45 writes the business result into the new off-heap dynamic memory block, and writes the address of the new off-heap dynamic memory block into the off-heap fixed memory block. Here, the Figure 4 The specific implementations corresponding to the shown receiving device 41, the second parsing device 42, the second reading device 43, the third application device 44 and the writing device 45 are respectively the same as or similar to the specific implementations of the aforementioned steps S21, S22, S23, S24 and S25, so they are not repeated here and are included here by reference.
[0101] In some embodiments, the device 2 further comprises a writing device (not shown), which writes a Jni interface, wherein the parameter of the Jni interface is a long integer, and is used to transfer the address of the fixed memory block outside the heap.
[0102] In some embodiments, the device 2 further includes a second monitoring device (not shown). The second monitoring device starts a monitoring thread to monitor whether the data in the off-heap fixed memory block changes. The second monitoring device can start a monitoring thread to monitor changes in the off-heap fixed memory block after the so library receives the address of the off-heap fixed memory block.
[0103] In some embodiments, the writing device 45 further includes a third encapsulation unit (not shown) and a fourth encapsulation unit (not shown). The third encapsulation unit encapsulates the business result obtained by executing the business according to the dynamic memory protocol header, and writes the encapsulated business result into the new off-heap dynamic memory block; the second encapsulation unit encapsulates the address of the new off-heap dynamic memory block according to the fixed memory protocol header, and writes the encapsulated address of the new off-heap dynamic memory block into the off-heap fixed memory block.
[0104] The dynamic memory protocol header and the fixed memory protocol header here are the same or substantially the same as the dynamic memory protocol header and the fixed memory protocol header in the aforementioned embodiment, so they are not described again here and are included here by reference.
[0105] Figure 6 An exemplary system that can be used to implement various embodiments described in this application is shown;
[0106] like Figure 6 In some embodiments shown, the system 300 can be used as any of the devices in the various described embodiments. In some embodiments, the system 300 may include one or more computer-readable media (e.g., system memory or NVM / storage device 320) with instructions and one or more processors (e.g., (one or more) processors 305) coupled to the one or more computer-readable media and configured to execute instructions to implement modules to perform the actions described in this application.
[0107] For one embodiment, system control module 310 may include any suitable interface controller to provide any suitable interface to at least one of processor(s) 305 and / or any suitable device or component in communication with system control module 310 .
[0108] The system control module 310 may include a memory controller module 330 to provide an interface to the system memory 315. The memory controller module 330 may be a hardware module, a software module, and / or a firmware module.
[0109] The system memory 315 may be used, for example, to load and store data and / or instructions for the system 300. For one embodiment, the system memory 315 may include any suitable volatile memory, such as a suitable DRAM. In some embodiments, the system memory 315 may include double data rate type four synchronous dynamic random access memory (DDR4 SDRAM).
[0110] For one embodiment, system control module 310 may include one or more input / output (I / O) controllers to provide interfaces to NVM / storage device 320 and communication interface(s) 325 .
[0111] For example, NVM / storage device 320 may be used to store data and / or instructions. NVM / storage device 320 may include any suitable non-volatile memory (e.g., flash memory) and / or may include any suitable non-volatile storage device(s) (e.g., one or more hard disk drives (HDDs), one or more compact disk (CD) drives, and / or one or more digital versatile disk (DVD) drives).
[0112] NVM / storage device 320 may include storage resources that are physically part of the device on which system 300 is installed, or it may be accessible to the device without being part of the device. For example, NVM / storage device 320 may be accessed over a network via communication interface(s) 325.
[0113] Communication interface(s) 325 may provide an interface for system 300 to communicate over one or more networks and / or with any other suitable devices. System 300 may wirelessly communicate with one or more components of a wireless network in accordance with any of one or more wireless network standards and / or protocols.
[0114] For one embodiment, at least one of the processor(s) 305 may be packaged together with the logic of one or more controllers of the system control module 310 (e.g., the memory controller module 330). For one embodiment, at least one of the processor(s) 305 may be packaged together with the logic of one or more controllers of the system control module 310 to form a system-in-package (SiP). For one embodiment, at least one of the processor(s) 305 may be integrated on the same die with the logic of one or more controllers of the system control module 310. For one embodiment, at least one of the processor(s) 305 may be integrated on the same die with the logic of one or more controllers of the system control module 310 to form a system on chip (SoC).
[0115] In various embodiments, the system 300 may be, but is not limited to: a server, a workstation, a desktop computing device, or a mobile computing device (e.g., a laptop computing device, a handheld computing device, a tablet computer, a netbook, etc.). In various embodiments, the system 300 may have more or fewer components and / or a different architecture. For example, in some embodiments, the system 300 includes one or more cameras, a keyboard, a liquid crystal display (LCD) screen (including a touch screen display), a non-volatile memory port, multiple antennas, a graphics chip, an application specific integrated circuit (ASIC), and a speaker.
[0116] In addition to the methods and devices described in the above embodiments, the present application also provides a computer-readable storage medium, which stores computer code. When the computer code is executed, the method described in any of the preceding items is executed.
[0117] The present application also provides a computer program product. When the computer program product is executed by a computer device, the method described in any of the preceding items is executed.
[0118] The present application also provides a computer device, the computer device comprising:
[0119] one or more processors;
[0120] a memory for storing one or more computer programs;
[0121] When the one or more computer programs are executed by the one or more processors, the one or more processors are caused to implement the method as described in any of the preceding items.
[0122] It should be noted that the present application can be implemented in software and / or a combination of software and hardware, for example, can be implemented using an application specific integrated circuit (ASIC), a general purpose computer or any other similar hardware device. In one embodiment, the software program of the present application can be executed by a processor to implement the steps or functions described above. Similarly, the software program of the present application (including relevant data structures) can be stored in a computer-readable recording medium, for example, a RAM memory, a magnetic or optical drive or a floppy disk and similar devices. In addition, some steps or functions of the present application can be implemented using hardware, for example, as a circuit that cooperates with a processor to perform each step or function.
[0123] In addition, a part of the present application may be applied as a computer program product, such as a computer program instruction, which, when executed by a computer, can call or provide the method and / or technical solution according to the present application through the operation of the computer. Those skilled in the art should understand that the existence of computer program instructions in computer-readable media includes but is not limited to source files, executable files, installation package files, etc., and accordingly, the way in which computer program instructions are executed by a computer includes but is not limited to: the computer directly executes the instruction, or the computer compiles the instruction and then executes the corresponding compiled program, or the computer reads and executes the instruction, or the computer reads and installs the instruction and then executes the corresponding installed program. Here, the computer-readable medium can be any available computer-readable storage medium or communication medium accessible to the computer.
[0124] Communication media include media by which communication signals containing, for example, computer readable instructions, data structures, program modules, or other data are transmitted from one system to another. Communication media may include guided transmission media such as cables and wires (e.g., fiber optic, coaxial, etc.) and wireless (unguided transmission) media that can propagate energy waves, such as acoustic, electromagnetic, RF, microwave, and infrared. Computer readable instructions, data structures, program modules, or other data may be embodied as a modulated data signal in, for example, a wireless medium such as a carrier wave or similar mechanism such as embodied as part of spread spectrum technology. The term "modulated data signal" refers to a signal whose one or more characteristics are changed or set in such a manner as to encode information in the signal. Modulation may be analog, digital, or a hybrid modulation technique.
[0125] By way of example and not limitation, computer-readable storage media may include volatile and nonvolatile, removable and non-removable media implemented in any method or technology for storage of information such as computer-readable instructions, data structures, program modules or other data. For example, computer-readable storage media include, but are not limited to, volatile memory, such as random access memory (RAM, DRAM, SRAM); and non-volatile memory, such as flash memory, various read-only memories (ROM, PROM, EPROM, EEPROM), magnetic and ferromagnetic / ferroelectric memories (MRAM, FeRAM); and magnetic and optical storage devices (hard disks, magnetic tapes, CDs, DVDs); or other media now known or later developed that can store computer-readable information / data for use by a computer system.
[0126] Here, according to an embodiment of the present application, a device is included, which includes a memory for storing computer program instructions and a processor for executing the program instructions, wherein, when the computer program instructions are executed by the processor, the device is triggered to run the methods and / or technical solutions based on the aforementioned multiple embodiments of the present application.
[0127] It is obvious to those skilled in the art that the present application is not limited to the details of the above exemplary embodiments, and that the present application can be implemented in other specific forms without departing from the spirit or basic features of the present application. Therefore, from any point of view, the embodiments should be regarded as exemplary and non-restrictive, and the scope of the present application is limited by the attached claims rather than the above description, so it is intended to include all changes that fall within the meaning and scope of the equivalent elements of the claims in the present application. Any figure mark in the claims should not be regarded as limiting the claims involved. In addition, it is obvious that the word "comprising" does not exclude other units or steps, and the singular does not exclude the plural. Multiple units or devices stated in the device claim can also be implemented by one unit or device through software or hardware. The words first, second, etc. are used to indicate names, and do not indicate any particular order.
Claims
1. A method for implementing data exchange with so library in the business layer, wherein: The method comprises: Apply for a heap of external fixed memory blocks, call the JNI interface of the corresponding so library, and pass the address of the external fixed memory block to the so library; Apply for an off-heap dynamic memory block, write the business data agreed with the so library into the off-heap dynamic memory block, and write the address of the off-heap dynamic memory block into the off-heap fixed memory block; When it is detected that the content of the off-heap fixed memory block has changed, the address of the off-heap dynamic memory block is parsed from the off-heap fixed memory block, wherein the off-heap dynamic memory block has been changed by the so library; According to the address of the off-heap dynamic memory block, read the content in the off-heap dynamic memory block, and parse the business type and data according to a predetermined protocol, wherein the business type and data are business results obtained by the so library executing the corresponding business; Destroy the off-heap dynamic memory block.
2. The method according to claim 1, wherein: The applying for a bunch of external fixed memory blocks also includes: Assigning an initial value to the off-heap fixed memory block; The method, when detecting that the content of the off-heap fixed memory block has changed, parses the address of the off-heap dynamic memory block from the off-heap fixed memory block, wherein the off-heap dynamic memory block has been changed by the so library before, further comprising: A monitoring thread is started to monitor whether the data in the fixed memory block outside the heap changes compared with the initial value.
3. The method according to claim 1 or 2, wherein: The step of applying for an off-heap dynamic memory block, writing the business data agreed with the so library into the off-heap dynamic memory block, and writing the address of the off-heap dynamic memory block into the off-heap fixed memory block also includes: Encapsulate the business data agreed with the so library according to the dynamic memory protocol header, and write the encapsulated business data into the off-heap dynamic memory block; The address of the off-heap dynamic memory block is encapsulated according to the fixed memory protocol header, and the encapsulated address of the off-heap dynamic memory block is written into the off-heap fixed memory block.
4. The method according to claim 3, wherein: The dynamic memory protocol header includes a task index of a predetermined byte, a task type of a predetermined byte, a data structure number of a predetermined byte, and business data content of several bytes; the fixed memory protocol header includes an identification bit of a predetermined byte and a memory address of a predetermined byte.
5. The method according to claim 4, wherein: The service data content includes several parts of the following content: a service length value of a predetermined byte, a data type of a predetermined byte, and service content of several bytes.
6. The method according to claim 1, wherein: The method further includes: When a new business comes, a new off-heap dynamic memory block is re-applied and the following operations are repeated: Apply for an off-heap dynamic memory block, write the business data agreed with the so library into the off-heap dynamic memory block, and write the address of the off-heap dynamic memory block into the off-heap fixed memory block; when it is detected that the content of the off-heap fixed memory block has changed, parse the address of the off-heap dynamic memory block from the off-heap fixed memory block, wherein the off-heap dynamic memory block has been changed by the so library; read the content of the off-heap dynamic memory block according to the address of the off-heap dynamic memory block, and parse the business type and data according to a predetermined protocol, wherein the business type and data are the business results obtained by the so library executing the corresponding business; destroy the off-heap dynamic memory block.
7. A method for implementing data exchange with a business layer in an so library, wherein: The method includes: Receive the address of the fixed memory block outside the heap passed by the corresponding business layer by calling the Jni interface; When it is detected that the content of the off-heap fixed memory block changes, the address of the corresponding off-heap dynamic memory block is parsed from the off-heap fixed memory block; According to the address of the off-heap dynamic memory block, read the content in the off-heap dynamic memory block, parse the service type and data according to a predetermined protocol, and execute the service to obtain a corresponding service result; Destroy the off-heap dynamic memory block and re-apply for a new off-heap dynamic memory block; The business result is written into the new off-heap dynamic memory block, and the address of the new off-heap dynamic memory block is written into the off-heap fixed memory block.
8. The method according to claim 7, wherein: When it is detected that the content of the off-heap fixed memory block changes, before parsing the address of the corresponding off-heap dynamic memory block from the off-heap fixed memory block, the method further includes: A monitoring thread is started to monitor whether the data in the off-heap fixed memory block changes.
9. The method according to claim 7 or 8, wherein: The step of writing the business result into the new off-heap dynamic memory block and writing the address of the new off-heap dynamic memory block into the off-heap fixed memory block includes: Encapsulate the business result according to the dynamic memory protocol header, and write the encapsulated business result into the new off-heap dynamic memory block; The address of the new off-heap dynamic memory block is encapsulated according to the fixed memory protocol header, and the encapsulated address of the new off-heap dynamic memory block is written into the off-heap fixed memory block.
10. The method according to claim 9, wherein: The dynamic memory protocol header includes a task index of a predetermined byte, a task type of a predetermined byte, a data structure number of a predetermined byte, and business data content of several bytes; the fixed memory protocol header includes an identification bit of a predetermined byte and a memory address of a predetermined byte.
11. The method according to claim 10, wherein: The service data content includes several parts of the following content: a service length value of a predetermined byte, a data type of a predetermined byte, and service content of several bytes.
12. The method according to claim 7, wherein: The method also includes, before receiving the address of the off-heap fixed memory block transmitted by the corresponding business layer through calling the Jni interface: Write a Jni interface, the parameter of which is a long integer, which is used to transfer the address of the fixed memory block outside the heap.
13. A device for implementing data exchange with a business layer, wherein: The device comprises: processor, and A memory arranged to store computer executable instructions which, when executed, cause the processor to perform a method as claimed in any one of claims 1 to 6.
14. A device for implementing data exchange with a business layer, wherein: The device comprises: processor, and A memory arranged to store computer executable instructions which, when executed, cause the processor to perform a method as claimed in any one of claims 7 to 12.
15. A computer-readable medium storing instructions, which when executed by a computer cause the computer to perform the operations of the method of any one of claims 1 to 12.
