CN101030135A - Method and device for storing C++ object in shared memory - Google Patents
Method and device for storing C++ object in shared memory Download PDFInfo
- Publication number
- CN101030135A CN101030135A CN 200610067532 CN200610067532A CN101030135A CN 101030135 A CN101030135 A CN 101030135A CN 200610067532 CN200610067532 CN 200610067532 CN 200610067532 A CN200610067532 A CN 200610067532A CN 101030135 A CN101030135 A CN 101030135A
- Authority
- CN
- China
- Prior art keywords
- shared drive
- shared memory
- virtual address
- storing
- objects
- Prior art date
- Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
- Granted
Links
Images
Landscapes
- Memory System Of A Hierarchy Structure (AREA)
- Information Retrieval, Db Structures And Fs Structures Therefor (AREA)
Abstract
本发明涉及一种在共享内存中存储C++对象的方法及装置,所述方法包括A.为刷新或首次申请创建共享内存的进程设置一段共享内存,并将该共享内存的地址映射到所述进程的进程空间的某个虚拟地址上;B.所述进程通过虚拟地址访问共享内存,并将所述虚拟地址存储到该共享内存中,将所述虚拟地址定义为原始虚拟地址;C.将C++对象和/或级联的C++对象存储在共享内存中。所述装置包括:加载或刷新单元和共享内存存储单元以及对象访问单元。本发明以解决目前现有技术中无法实现共享内存中跨进程间交互访问C++对象的问题。
The present invention relates to a method and device for storing C++ objects in a shared memory. The method includes A. setting up a section of shared memory for a process that refreshes or applies for the creation of a shared memory for the first time, and maps the address of the shared memory to the process On a certain virtual address of the process space; B. the process accesses the shared memory through the virtual address, and stores the virtual address in the shared memory, and defines the virtual address as the original virtual address; C. C++ Objects and/or concatenated C++ objects are stored in shared memory. The device includes: a load or refresh unit, a shared memory storage unit and an object access unit. The invention solves the problem that the cross-process interactive access to the C++ object in the shared memory cannot be realized in the prior art.
Description
技术领域technical field
本发明涉及操作系统,特别是涉及一种在共享内存中存储C++对象的方法及装置。The invention relates to an operating system, in particular to a method and device for storing C++ objects in a shared memory.
背景技术Background technique
所述共享内存是UNIX等各种操作系统提供的一种特性,在C/C++语言中通过shmget和shmat(不同的操作系统提供API函数可能有所不同)来返回映射到本进程空间的地址。这样,就可以象操作普通的内存空间一样来操作共享内存。另外,所述共享内存主要用于多进程共享数据,是进程间通信(IPC)的一种方式,允许一个或多个进程通过同时出现在它们虚拟地址空间中的内存来通信。因为数据不需要在客户端与服务器之间复制,因此,共享内存是一种最快的IPC。The shared memory is a feature provided by various operating systems such as UNIX. In C/C++ language, shmget and shmat (different operating systems may provide different API functions) return the address mapped to the process space. In this way, the shared memory can be operated like an ordinary memory space. In addition, the shared memory is mainly used for sharing data among multiple processes, and is a way of inter-process communication (IPC), allowing one or more processes to communicate through memory that simultaneously appears in their virtual address spaces. Because data does not need to be copied between client and server, shared memory is the fastest form of IPC.
每个使用共享内存的进程必须通过系统调用将其连接到虚拟内存上,所述虚拟内存的页面出现在每个进程的页表中,这时进程会创建新的虚拟内存管理结构体(vm_area_struct)来描叙该共享内存。进程可以决定该共享内存在其虚拟地址空间的位置。因此,在不同的进程中,该共享内存映射的位置可以是不同的。新的vm_area_struct结构将被放到由共享内存描述符(shmid_ds)指向的vm_area_struct链表中。通过前结构体指针(vm_next_shared)和后结构体指针(vm_prev_shared)指针将它们连接起来。也就是说,在一台主机上的同一段共享内存,对于在该主机上运行不同的进程,其映射到该进程空间的地址是不同的。Each process that uses shared memory must connect it to virtual memory through a system call. The pages of the virtual memory appear in the page table of each process. At this time, the process will create a new virtual memory management structure (vm_area_struct) to describe the shared memory. A process can determine where this shared memory resides in its virtual address space. Therefore, in different processes, the location of the shared memory mapping may be different. The new vm_area_struct structure will be placed in the vm_area_struct linked list pointed to by the shared memory descriptor (shmid_ds). Connect them through the pointer of the previous structure pointer (vm_next_shared) and the pointer of the rear structure pointer (vm_prev_shared). That is to say, the same segment of shared memory on a host has different addresses mapped to the process space for different processes running on the host.
所有访问该共享内存的进程,必须访问一个相同的信号量,通过信号量来保证该共享内存中存储的数据的安全性。All processes accessing the shared memory must access the same semaphore, and the security of the data stored in the shared memory is guaranteed through the semaphore.
目前,现有技术中在共享内存中存储C++对象的方法主要通过下述方法来实现。At present, the methods for storing C++ objects in shared memory in the prior art are mainly realized through the following methods.
请参考图1,是一种现有在共享内存中存储C++对象的示意图,其实现的原理为:一组相同的对象(struct A)在共享内存中的顺序存储,且每个对象(Struct A)在共享内存中的占用空间是相同的。Please refer to Figure 1, which is a schematic diagram of an existing storage of C++ objects in shared memory. The principle of its implementation is: a group of identical objects (struct A) are sequentially stored in shared memory, and each object (struct A ) occupy the same space in shared memory.
由上公开的技术方案可知,这种方法对于同一段共享内存,映射到不同的进程中的地址是不同的。也就是说,该方法直接存储类似的结构体,而无法存储真正的包含C++指针属性的结构体,即便保存了含有指针属性的结构体,另一个进程在引用该对象的指针属性时,也会造成指针非法访问。It can be seen from the technical solution disclosed above that in this method, for the same segment of shared memory, the addresses mapped to different processes are different. That is to say, this method directly stores similar structures, but cannot store real structures containing C++ pointer properties. Even if a structure containing pointer properties is saved, when another process refers to the pointer properties of the object, it will also Cause pointer illegal access.
还请参考图2,是另一种在共享内存中存储C++对象的示意图,其实现的原理为:首先,通过索引数组来存取共享内存中存储的对象(比如,struct A、B、C...),且可以有多个索引数组(比如struct Idx),所述索引数组的每个成员也是一个struct。其中,所述struct Idx包括:对象类型、个数、指向对象(比如struct A、B、C...)的偏移以及搜索键。而所述struct Idx可以用多种算法(比如折半查找、冒泡查找等)来快速访问真正关心的数据(struct A、B、C...)。而所述对象(struct A、B、C...)在共享内存中以顺序的方式存储,是用户真正关心的数据,每个对象在共享内存中的占用空间可以是不相同的。Please also refer to Figure 2, which is another schematic diagram of storing C++ objects in shared memory. The principle of its implementation is as follows: first, access objects stored in shared memory through index arrays (for example, struct A, B, C. ..), and there can be multiple index arrays (such as struct Idx), and each member of the index array is also a struct. Wherein, the struct Idx includes: object type, number, offset to the object (such as struct A, B, C...) and a search key. The struct Idx can use multiple algorithms (such as binary search, bubble search, etc.) to quickly access the data that is really concerned (struct A, B, C...). The objects (struct A, B, C...) are stored sequentially in the shared memory, which is the data that the user really cares about, and the occupied space of each object in the shared memory may be different.
该方案虽然比方法一有所进步,即可以通过良好地设计结构体来实现对象之间的关联。但是,不同的进程在存取共享内存中的数据时,均需要通过索引数组中的偏移、个数等信息来访问共享内存存储的对象,而实现起来比较复杂。由此可见,该技术方案同上述方法一的缺点基本相同,即:存储的对象(结构体)不能包括指向在该共享内存中存储其他对象的指针属性(或数组指针属性),因为指针指向的值,只有在进程中才有意义,因此,即便存储了包括指针属性(或数组指针属性)的对象,另一个进程在引用该对象的指针属性(或数组指针属性)时,也会造成非法访问。即在不同进程之间,无法透明访问共享内存中存储真正的C++对象。Although this scheme is more advanced than method one, that is, the association between objects can be realized by designing the structure well. However, when different processes access data in the shared memory, they all need to access the objects stored in the shared memory through information such as offset and number in the index array, and the implementation is relatively complicated. It can be seen that this technical solution has basically the same disadvantages as the above-mentioned method 1, that is, the stored object (structure) cannot include pointer attributes (or array pointer attributes) pointing to other objects stored in the shared memory, because the pointer points to The value is meaningful only in the process. Therefore, even if an object including pointer attributes (or array pointer attributes) is stored, another process will cause illegal access when referencing the object's pointer attributes (or array pointer attributes). . That is, real C++ objects stored in shared memory cannot be transparently accessed between different processes.
发明内容Contents of the invention
本发明解决的技术问题是提供一种在共享内存中存储C++对象的方法及装置,以解决目前现有技术中无法实现共享内存中跨进程间交互访问C++对象和/或级联的C++对象的问题。The technical problem solved by the present invention is to provide a method and device for storing C++ objects in shared memory, so as to solve the problem that cross-process interactive access to C++ objects and/or cascaded C++ objects in shared memory cannot be realized in the prior art question.
为解决上述问题,本发明提供一种在共享内存中存储C++对象的方法,所述方法包括步骤:In order to solve the above problems, the present invention provides a method for storing C++ objects in shared memory, said method comprising steps:
A、为刷新或首次申请创建共享内存的进程设置一段共享内存,并将该共享内存的地址映射到所述进程的进程空间的某个虚拟地址上;A. Set up a section of shared memory for refreshing or applying for the process of creating shared memory for the first time, and map the address of the shared memory to a certain virtual address of the process space of the process;
B、所述进程通过虚拟地址访问共享内存,并将所述虚拟地址存储到该共享内存中,将所述虚拟地址定义为原始虚拟地址;B. The process accesses the shared memory through a virtual address, stores the virtual address in the shared memory, and defines the virtual address as an original virtual address;
C、将C++对象和/或级联的C++对象存储在共享内存中。C. Store C++ objects and/or concatenated C++ objects in shared memory.
所述方法还包括:客户进程通过重载C++类的成员引用操作符来访问存储在共享内存中的C++对象和/或级联的C++对象。The method further includes: the client process accesses the C++ object and/or the cascaded C++ object stored in the shared memory by overloading the member reference operator of the C++ class.
访问C++对象的地址为:指针值与共享内存映射到访问者所在进程的虚拟地址减去原始虚拟地址的差之和。The address of accessing a C++ object is: the sum of the difference between the pointer value and the virtual address of the shared memory mapped to the process where the visitor is located minus the original virtual address.
步骤B中,所述将虚拟地址存储到该共享内存中的过程具体为:将映射的访问共享内存的虚拟地址存储到共享内存中的某个固定位置上。In step B, the process of storing the virtual address in the shared memory is specifically: storing the mapped virtual address for accessing the shared memory at a certain fixed location in the shared memory.
步骤C中将C++对象和/或级联的C++对象分别通过指针相连的方式存储在共享内存中。In step C, the C++ object and/or the concatenated C++ object are respectively stored in the shared memory in the manner of linking pointers.
通过数据加载或刷新进程将C++对象和/或级联的C++对象分别通过指针相连的方式存储在共享内存中。The C++ object and/or the cascaded C++ object are respectively stored in the shared memory through pointer connection through the data loading or refreshing process.
所述存储的C++对象和/或级联的C++对象之间可以连续或不连续,且存储的每个对象所占用的空间取决于该对象是否包括数组指针属性和数组指针的个数。The stored C++ objects and/or cascaded C++ objects may be continuous or discontinuous, and the space occupied by each stored object depends on whether the object includes an array pointer attribute and the number of array pointers.
另外,本发明还提供一种在共享内存中存储C++对象的装置,所述装置包括:加载或刷新单元和共享内存存储单元,其中,In addition, the present invention also provides a device for storing C++ objects in shared memory, said device comprising: a load or refresh unit and a shared memory storage unit, wherein,
所述加载或刷新单元,用于为刷新或首次申请创建共享内存的进程设置一段共享内存,并将该共享内存的地址映射到所述进程的进程空间的某个虚拟地址上,通过该虚拟地址访问共享内存,并将所述虚拟地址存储到该共享内存存储单元中;The loading or refreshing unit is used to set a section of shared memory for the process of refreshing or applying for the creation of the shared memory for the first time, and map the address of the shared memory to a certain virtual address of the process space of the process, through the virtual address accessing shared memory, and storing the virtual address in the shared memory storage unit;
所述共享内存存储单元,与加载或刷新单元相连,用于存储C++对象和/或级联的C++对象。The shared memory storage unit is connected to the load or refresh unit and is used to store C++ objects and/or cascaded C++ objects.
所述装置还包括:The device also includes:
对象访问单元,用于通过重载C++类的成员引用操作符来访问共享内存存储单元中的C++对象和/或级联的C++对象。The object access unit is used to access the C++ object and/or the cascaded C++ object in the shared memory storage unit by overloading the member reference operator of the C++ class.
与现有技术相比,本发明具有以下有益效果:本发明在共享内存中存储真正的C++对象,可以将应用中共用的、基础的C++对象和/或级联的C++对象放入共享内存,所有需要使用该对象的进程就象访问在自己进程内创建的C++对象和/或级联的C++对象一样,这样在用C++实现大规模的OOP应用时,减少了应用开发难度,缩短了开发周期;特别是在主机上运行多个相同应用或服务进程时,减少对主机物理内存的使用;本发明所述技术方案对数据库类的应用,将共用的、访问频繁的字典参数放入共享内存中,减少对数据库的存取压力,加快访问速度。Compared with the prior art, the present invention has the following beneficial effects: the present invention stores real C++ objects in the shared memory, and can put common, basic C++ objects and/or cascaded C++ objects in the application into the shared memory, All processes that need to use this object are like accessing C++ objects and/or cascaded C++ objects created in their own processes, which reduces the difficulty of application development and shortens the development cycle when implementing large-scale OOP applications with C++ ; Especially when multiple identical applications or service processes are running on the host, the use of the physical memory of the host is reduced; the technical solution of the present invention applies to the application of the database class, and the shared and frequently accessed dictionary parameters are put into the shared memory , reduce the access pressure on the database and speed up the access speed.
附图说明Description of drawings
图1是现有技术中在共享内存中存储C++对象的一种示意图;Fig. 1 is a schematic diagram of storing C++ objects in shared memory in the prior art;
图2是现有技术中在共享内存中存储C++对象的另一种示意图;Fig. 2 is another schematic diagram of storing C++ objects in shared memory in the prior art;
图3是本发明在共享内存中存储C++对象的方法的流程图;Fig. 3 is the flowchart of the method for storing C++ object in shared memory of the present invention;
图4是本发明所述在共享内存中存储C++对象的方法的一实施例;Fig. 4 is an embodiment of the method for storing C++ objects in shared memory according to the present invention;
图5是本发明所述在共享内存中存储C++对象的方法的又一实施例;Fig. 5 is another embodiment of the method for storing C++ objects in shared memory according to the present invention;
图6是本发明所述在共享内存中存储C++对象的装置的示意图;Fig. 6 is a schematic diagram of a device for storing C++ objects in shared memory according to the present invention;
图7是本发明所述在共享内存中存储C++对象的装置的一实施例。Fig. 7 is an embodiment of the device for storing C++ objects in shared memory according to the present invention.
具体实施方式Detailed ways
本发明的核心是操作系统首先为运行在主机上的进程设置该进程所需要的共享内存,所述共享内存的定义是由内核出于在多进程间交换信息的目的而留出的一块内存区(段),如果权限设置恰当,每个要访问该内存的进程都可以将它映像到自己私有的地址空间中,如果一个进程更新了段中的数据,那么其他进程立即会看到更新,由一个进程创建的段也可以由另一个进程读写。并将该共享内存的地址映射到进程空间的一个虚拟地址,该存储C++对象和/或级联的C++对象到共享内存的进程通过该虚拟地址访问所设置的共享内存;同时,该进程将该虚拟地址存储到设置的共享内存中,这个存储到共享内存中的地址定义为原始虚拟地址;通过重载C++的成员引用方法(比如:C++类的成员引用操作符)来实现其他进程对存储在该共享内存中的C++对象的透明的访问。也就是说,所有要使用该对象的进程就象访问在自己进程内新建的C++对象一样,特别是在用C++实现大规模的OOP应用时,减少应用开发的难度,缩短开发周期。The core of the present invention is that the operating system first sets the shared memory required by the process for the process running on the host, and the definition of the shared memory is a memory area reserved by the kernel for the purpose of exchanging information between multiple processes (segment), if the permissions are set properly, each process that wants to access the memory can map it to its own private address space, if a process updates the data in the segment, then other processes will see the update immediately, by Segments created by one process can also be read and written by another process. And map the address of the shared memory to a virtual address of the process space, the process that stores the C++ object and/or the cascaded C++ object to the shared memory accesses the set shared memory through the virtual address; at the same time, the process will The virtual address is stored in the set shared memory, and the address stored in the shared memory is defined as the original virtual address; by overloading the member reference method of C++ (for example: the member reference operator of the C++ class) to realize other processes to store in Transparent access to C++ objects in this shared memory. That is to say, all processes that want to use this object are like accessing a newly created C++ object in their own process, especially when using C++ to implement large-scale OOP applications, it reduces the difficulty of application development and shortens the development cycle.
下面结合附图对本发明做进一步的说明。The present invention will be further described below in conjunction with the accompanying drawings.
请参阅图3,为本发明所述在共享内存中存储C++对象的方法的流程图。所述方法包括步骤:Please refer to FIG. 3 , which is a flow chart of the method for storing C++ objects in shared memory according to the present invention. The method comprises the steps of:
步骤S11:设置刷新或首次申请创建共享内存的进程所需的共享内存,并将该共享内存的地址映射到所述进程的进程空间的某个虚拟地址上;Step S11: setting the shared memory required by the process for refreshing or first applying for creating the shared memory, and mapping the address of the shared memory to a certain virtual address of the process space of the process;
步骤S12:所述进程通过虚拟地址访问共享内存,并将该虚拟地址存储到该共享内存中,将所述虚拟地址定义为原始虚拟地址;Step S12: The process accesses the shared memory through a virtual address, stores the virtual address in the shared memory, and defines the virtual address as an original virtual address;
步骤S13:将C++对象和/或级联的C++对象存储在共享内存中。Step S13: storing the C++ object and/or the concatenated C++ object in the shared memory.
在本发明所述技术方案中,运行在主机上的进程先向操作系统申请需要的共享内存,操作系统才为其分配相应的共享内存资源,并将其映射到自己进程空间所在的一个虚拟地址,所述进程通过该虚拟地址访问所申请的共享内存;同时,该进程将所述虚拟地址存储到申请的共享内存中,存储在共享内存的这个地址就定义为原始虚拟地址;通过重载C++的成员引用方法(即C++类的成员引用操作符)来实现其他进程对存储在该共享内存中的C++对象的透明访问。也就是说,本发明对于运行的进程都通过映射在自己进程空间的虚拟地址访问相同的一段共享内存,在该共享内存中的固定位置存储着原始虚拟地址,对于不同的进程,都能正确地取得该原始虚拟地址;再将C++对象和/或级联的C++对象装载于该共享内存中,其中,所述C++对象中包括指针或指针数组成员属性,所述指针指向其他在共享内存中存储的C++对象和/或级联的C++对象,各C++对象和/或级联的C++对象通过指针相连;进程通过映射到自己地址空间的虚拟地址访问共享内存,并通过访问共享内存以访问装载于该共享内存中的C++对象和/或级联的C++对象。其具体的实现过程还请参考图4。In the technical solution of the present invention, the process running on the host first applies to the operating system for the required shared memory, and the operating system allocates the corresponding shared memory resource for it, and maps it to a virtual address where its own process space is located. , the process accesses the applied shared memory by the virtual address; meanwhile, the process stores the virtual address in the applied shared memory, and the address stored in the shared memory is defined as the original virtual address; by overloading C++ The member reference method (that is, the member reference operator of the C++ class) realizes the transparent access of other processes to the C++ object stored in the shared memory. That is to say, the present invention accesses the same section of shared memory through the virtual address mapped in its own process space for the running process, and the original virtual address is stored in a fixed position in the shared memory, and for different processes, it can correctly Obtain the original virtual address; then load the C++ object and/or cascaded C++ object in the shared memory, wherein the C++ object includes a pointer or pointer array member attribute, and the pointer points to other objects stored in the shared memory C++ objects and/or cascaded C++ objects, each C++ object and/or cascaded C++ objects are connected through pointers; the process accesses the shared memory through the virtual address mapped to its own address space, and accesses the shared memory by accessing the shared memory. C++ objects and/or concatenated C++ objects in that shared memory. Please also refer to FIG. 4 for its specific implementation process.
如图4所示,为本发明所述共享内存中C++对象的存储方法的一实施例,其实现原理为:As shown in Figure 4, it is an embodiment of the storage method of the C++ object in the shared memory of the present invention, and its realization principle is:
第一步,先确定共享内存中的原始虚拟地址,该原始虚拟地址是数据加载进程在将C++对象加载到该共享内存时,首先要将共享内存映射到本进程空间的某个地址,该地址就定义为原始虚拟地址,并且将该地址存储在共享内存中的某个固定的位置。当其它进程从共享内存中存取对象时,通过C++的重载机制,重载访问共享内存中C++类的成员引用操作符(即operator[‘.’、‘->’])来实现C++语义上的对象访问。所述重载访问的原理主要是:存取对象地址等于指针值与访问者所在进程映射的共享内存地址减去原始虚拟地址的差之和。其公式为:对象地址=指针值+(访问者所在进程映射的共享内存地址-原始虚拟地址)。The first step is to determine the original virtual address in the shared memory. When the data loading process loads the C++ object into the shared memory, it first needs to map the shared memory to an address in the process space. It is defined as the original virtual address, and the address is stored in a fixed location in the shared memory. When other processes access objects from the shared memory, through the overloading mechanism of C++, the member reference operator (ie operator['.', '->']) of the C++ class in the shared memory is overloaded to realize the C++ semantics Object access on . The principle of the overloaded access is mainly: the address of the access object is equal to the sum of the difference between the pointer value and the shared memory address mapped by the process where the visitor is located minus the original virtual address. The formula is: object address=pointer value+(shared memory address mapped by the process where the visitor is located-original virtual address).
第二步,通过一个或多个索引数组来存取共享内存中存储的对象A,所述索引数组一般包括:存取对象的大小Size、多个存储的对象struct Idx等。也就是说,索引数组的每个成员也是一个struct,每个成员包括:指向对象A的指针,及搜索键,可以通过多种算法(比如折半查找、冒泡查找等)来快速访问真正关心的数据(比如对象A)。其中所述折半查找、冒泡查找等算法对于本领域的技术人员来已是公知技术,在此不再赘述。The second step is to access the object A stored in the shared memory through one or more index arrays. The index array generally includes: the size of the accessed object, multiple stored object struct Idx, and the like. In other words, each member of the index array is also a struct, and each member includes: a pointer to object A, and a search key, which can quickly access the real concern through various algorithms (such as binary search, bubble search, etc.) data (say object A). Algorithms such as the binary search and the bubble search are already known to those skilled in the art, and will not be repeated here.
第三步,通过索引数组存储类A对象是顺序存储一类C++对象的方式,每个对象占用的空间可以相同,也可以不同,这取决于该对象是否包含数组指针属性,及数组指针的个数。数组指针的指针个数决定了类对象的存储空间占用,个数多的占用空间多。The third step is to store class A objects through index arrays, which is a method of sequentially storing a class of C++ objects. The space occupied by each object can be the same or different, depending on whether the object contains an array pointer attribute and the individuality of the array pointer. number. The number of pointers in the array pointer determines the storage space occupied by the class object, and the larger the number, the larger the occupied space.
第四步,对于C++的一个对象在共享内存中的存储组织结构,其所有的属性在共享内存中的存储是连续的,与其在应用进程中的存储是相同的。其中,该对象的属性,可以包含有指针属性或数组指针属性,其类型是任意的C++类型,包括基础类型与应用程序自己字义的类型等。如图中所示:类A对象的属性1为int,属性2为char[20],属性3为类c,属性4为Void**(所述Void**为指向指针的指针),属性n为int等。In the fourth step, for the storage organization structure of an object of C++ in the shared memory, the storage of all its attributes in the shared memory is continuous, which is the same as the storage in the application process. Wherein, the properties of the object may include pointer properties or array pointer properties, the type of which is any C++ type, including basic types and types defined by the application itself. As shown in the figure: attribute 1 of class A object is int, attribute 2 is char[20], attribute 3 is class c, attribute 4 is Void** (the Void** is a pointer to a pointer), attribute n for int etc.
第五步,描述的是类A对象的数组指针属性的成员指针Void*所指向的类B对象,单个类B对象的存储与类A对象的存储方式类似,在此不再详细的描述。The fifth step describes the class B object pointed to by the member pointer Void* of the array pointer attribute of the class A object. The storage method of a single class B object is similar to the storage method of the class A object, and will not be described in detail here.
上述存储C++对象的过程中从第三步到第五步是递归的,其结束条件是不包含数组指针属性的对象,也就是说,不会再通过该对象去访问其他对象。The above-mentioned process of storing C++ objects is recursive from the third step to the fifth step, and its end condition is an object that does not contain an array pointer attribute, that is, it will not access other objects through this object.
由上述实现过程可知,本发明在共享内存中存储真正的C++对象的方法,可以供所有访问该共享内存的进程透明地访问该C++对象,其中,所述访问该共享内存的进程,在其本身的进程空间内映射的共享内存的地址可以相同或者不同;并且该C++对象最大的特点是可以包括指针属性或指针数组属性,指针或其成员指针指向其他在共享内存中存储的C++对象,这个过程是递归的,直到不包括指针属性或指针数组属性的C++对象为止。As can be seen from the above implementation process, the method for storing real C++ objects in the shared memory of the present invention can provide transparent access to the C++ object for all processes accessing the shared memory, wherein the process of accessing the shared memory is in itself The address of the shared memory mapped in the process space can be the same or different; and the biggest feature of the C++ object is that it can include pointer attributes or pointer array attributes, and the pointer or its member pointers point to other C++ objects stored in the shared memory. is recursive until a C++ object that does not include pointer properties or pointer-array properties.
为了能清楚的描述本发明,请再参考图5,为本发明所述方法的一个应用实例。In order to clearly describe the present invention, please refer to FIG. 5 again, which is an application example of the method of the present invention.
数据刷新进程根据应用的需要将数据库的变更更新到共享内存中,这个更新的过程会影响到共享内存的存储结构的变更;客户进程(包括客户进程1、2、3等)通过某种算法(所述算法可以是折半查找、冒泡查找等),找到自己需要的对象,比如图中所示:客户进程1找到类A对象1,客户进程2找到类A对象2,客户进程3找到类A对象3等;不同的对象其所包括的子对象是不同的,数目可以为0...n个,但是,在不同的时刻,共享内存中的对象的关联关系是变化的,即依赖于数据库中的数据的关联关系。如图所示,在时刻1时,类A对象1的子对象包括类B对象1,类A对象2的子对象包括类B对象2和类B对象3等;在时刻2时(如图中虚线所示),类A对象1的子对象包括类B对象1和类B对象2,类A对象2的子对象包括类B对象3等。The data refresh process updates the database changes to the shared memory according to the needs of the application. This update process will affect the change of the storage structure of the shared memory; the client process (including client processes 1, 2, 3, etc.) through a certain algorithm ( Described algorithm can be half search, bubbling search etc.), finds the object that oneself needs, such as shown in the figure: client process 1 finds class A object 1, client process 2 finds class A object 2, client process 3 finds class A Object 3, etc.; different objects contain different sub-objects, the number can be 0...n, but at different times, the relationship between objects in the shared memory changes, that is, it depends on the database The relationship between the data in . As shown in the figure, at time 1, the sub-objects of class A object 1 include class B object 1, and the sub-objects of class A object 2 include class B object 2 and class B object 3, etc.; As shown by the dotted line), the sub-objects of class A object 1 include class B object 1 and class B object 2, and the sub-objects of class A object 2 include class B object 3 and so on.
因此,客户进程在访问数据库中的变更信息时,不需要直接访问数据库,可通过自己所使用的对象在共享内存中与其他对象的关联关系的变更,来实时获知数据库中的变更信息。Therefore, when the client process accesses the change information in the database, it does not need to directly access the database, and can learn the change information in the database in real time through the change of the association relationship between the object it uses and other objects in the shared memory.
由上述步骤可知,本实施例的特点为:通过一个数据刷新进程将数据库中共用的数据根据应用的需要刷新到共享内存中,客户进程在使用该数据时,不需要访问数据库,直接访问共享内存即可正确的访问到需要的对象,即访问到所使用的数据;其中客户进程对共享内存的访问是非常快速的,且可以有较大的并发数;此外,客户进程在访问共享内存中的数据时,就象访问在自己进程空间创建的对象一样,加快了访问的速度。由此可见,本发明所述方案可以应用于实时的、数据库类的、面向对象的大型C++类应用。It can be seen from the above steps that the characteristics of this embodiment are: through a data refresh process, the data shared in the database is refreshed into the shared memory according to the needs of the application. When the client process uses the data, it does not need to access the database, but directly accesses the shared memory. You can correctly access the required objects, that is, access the data used; the client process’s access to the shared memory is very fast, and can have a large number of concurrency; in addition, the client process accesses the shared memory. When accessing data, it is like accessing objects created in its own process space, which speeds up the access speed. It can be seen that the solution of the present invention can be applied to real-time, database-like, and object-oriented large-scale C++ applications.
另外,本发明还提供一种在共享内存中存储C++对象的装置,所述装置的结构示意图详见图6。所述装置包括:加载或刷新单元11和共享内存存储单元12,其中,所述加载或刷新单元11,用于为刷新或首次申请创建共享内存的进程设置一段共享内存,并将该共享内存的地址映射到所述进程的进程空间的某个虚拟地址上,通过该虚拟地址访问共享内存存储单元12,并将所述虚拟地址存储到该共享内存存储单元12中;所述共享内存存储单元12,与加载或刷新单元相连11,用于存储C++对象和/或级联的C++对象。所述装置在上述基础上可以增加对象访问单元或对象存取单元13,所述对象访问单元用于通过重载C++类的成员引用操作符来访问共享内存存储单元12中的C++对象和/或级联的C++对象;所述对象存取单元,用于通过一个或多个索引数组来存取共享内存存储单元12中存储的C++对象和/或级联的C++对象。其中所述索引数组本身也是一个C++对象。In addition, the present invention also provides a device for storing C++ objects in a shared memory, and the structural diagram of the device is shown in FIG. 6 for details. The device includes: a loading or
所述装置中各个单元的具体过程与上述方法的实现过程相同,具体详见上述,在此不再赘述。The specific process of each unit in the device is the same as the implementation process of the above method, see the above for details, and will not be repeated here.
下面结合图7来说明本发明所述装置的应用实施。The application and implementation of the device of the present invention will be described below with reference to FIG. 7 .
如图7所示,操作系统根据进程的需要为其分配的共享内存的存储区域,即其享内存存储区域。该共享内存存储区域是独立的,由操作系统提供的,可同时供该主机上多个进程共同访问的物理存储区域,其映射在不同的进程中的虚拟地址可以是不同的,是由操作系统的特性决定的;共享内存加载或刷新进程,该共享内存加载或刷新进程主要用于将需要的数据以C++对象(比如图中所示的C++对象A、C++对象B、C++对象C、C++对象D)的方式存储到所申请的共享内存存储区域或对该共享内存存储区域的数据进行刷新;客户进程,是使用这些数据的进程,访问该共享内存中的C++对象,就象访问在自己进程空间内生成的对象一样地进行访问,且最关键的是此类C++对象可以包含指针或数组指针属性,并通过指针或数组指针属性关联访问存储在共享内存中的其他C++对象。本发明所述技术方案主要时提供一种借助于共享内存实现跨进程交互访问C++对象的能力,其主要应用于使用C++语言开发的应用系统。As shown in FIG. 7 , the operating system allocates a shared memory storage area for the process according to the needs of the process, that is, its shared memory storage area. The shared memory storage area is an independent physical storage area provided by the operating system and can be accessed by multiple processes on the host at the same time. The virtual addresses mapped in different processes can be different, which is determined by the operating system. Determined by the characteristics; shared memory loading or refreshing process, the shared memory loading or refreshing process is mainly used to convert the required data into C++ objects (such as C++ object A, C++ object B, C++ object C, and C++ object shown in the figure D) to store in the requested shared memory storage area or to refresh the data in the shared memory storage area; the client process is the process that uses these data, accessing the C++ object in the shared memory is like accessing the C++ object in its own process The objects generated in the space are accessed in the same way, and the most important thing is that such C++ objects can contain pointer or array pointer attributes, and associate access to other C++ objects stored in shared memory through pointer or array pointer attributes. The technical solution of the present invention mainly provides a capability of realizing cross-process interactive access to C++ objects by means of shared memory, which is mainly applied to application systems developed using C++ language.
以上所述仅是本发明的优选实施方式,应当指出,对于本技术领域的普通技术人员来说,在不脱离本发明原理的前提下,还可以做出若干改进和润饰,这些改进和润饰也应视为本发明的保护范围。The above is only a preferred embodiment of the present invention, it should be pointed out that, for those of ordinary skill in the art, without departing from the principle of the present invention, some improvements and modifications can also be made, and these improvements and modifications can also be made. It should be regarded as the protection scope of the present invention.
Claims (10)
Priority Applications (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CNB2006100675322A CN100454241C (en) | 2006-02-28 | 2006-02-28 | A method and device for storing C++ objects in shared memory |
Applications Claiming Priority (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CNB2006100675322A CN100454241C (en) | 2006-02-28 | 2006-02-28 | A method and device for storing C++ objects in shared memory |
Publications (2)
Publication Number | Publication Date |
---|---|
CN101030135A true CN101030135A (en) | 2007-09-05 |
CN100454241C CN100454241C (en) | 2009-01-21 |
Family
ID=38715510
Family Applications (1)
Application Number | Title | Priority Date | Filing Date |
---|---|---|---|
CNB2006100675322A Expired - Fee Related CN100454241C (en) | 2006-02-28 | 2006-02-28 | A method and device for storing C++ objects in shared memory |
Country Status (1)
Country | Link |
---|---|
CN (1) | CN100454241C (en) |
Cited By (8)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN102200930A (en) * | 2011-05-26 | 2011-09-28 | 华为数字技术有限公司 | Synchronous variable mapping method and device, synchronous variable freeing method and synchronous variable deleting method |
CN103197979A (en) * | 2012-01-04 | 2013-07-10 | 阿里巴巴集团控股有限公司 | Method and device for realizing data interaction access among processes |
CN104572313A (en) * | 2013-10-22 | 2015-04-29 | 华为技术有限公司 | Inter-process communication method and device |
CN105094790A (en) * | 2014-05-22 | 2015-11-25 | 腾讯科技(深圳)有限公司 | Standardized structure based application running method and system |
CN106339258A (en) * | 2016-08-10 | 2017-01-18 | 西安诺瓦电子科技有限公司 | Management method and device for shared memory of programmable logic device and microprocessor |
CN107229530A (en) * | 2017-06-29 | 2017-10-03 | 上海联影医疗科技有限公司 | Communication means, storage medium and computer equipment between process |
CN110096453A (en) * | 2019-04-11 | 2019-08-06 | 北京华三通信技术有限公司 | Data processing method and device |
CN110727514A (en) * | 2019-10-12 | 2020-01-24 | 北京无线电测量研究所 | Memory management method based on index queue and embedded equipment |
Family Cites Families (4)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
FR2762418B1 (en) * | 1997-04-17 | 1999-06-11 | Alsthom Cge Alcatel | METHOD FOR MANAGING A SHARED MEMORY |
US6684305B1 (en) * | 2001-04-24 | 2004-01-27 | Advanced Micro Devices, Inc. | Multiprocessor system implementing virtual memory using a shared memory, and a page replacement method for maintaining paged memory coherence |
CN1282926C (en) * | 2002-07-23 | 2006-11-01 | 华为技术有限公司 | A Locating Method for Using Shared Memory Objects to Record Abnormal Exits of Common Processes |
CN1331070C (en) * | 2004-02-21 | 2007-08-08 | 华为技术有限公司 | Method and equipment of data communication |
-
2006
- 2006-02-28 CN CNB2006100675322A patent/CN100454241C/en not_active Expired - Fee Related
Cited By (13)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN102200930A (en) * | 2011-05-26 | 2011-09-28 | 华为数字技术有限公司 | Synchronous variable mapping method and device, synchronous variable freeing method and synchronous variable deleting method |
CN103197979A (en) * | 2012-01-04 | 2013-07-10 | 阿里巴巴集团控股有限公司 | Method and device for realizing data interaction access among processes |
CN103197979B (en) * | 2012-01-04 | 2017-05-03 | 阿里巴巴集团控股有限公司 | Method and device for realizing data interaction access among processes |
CN104572313A (en) * | 2013-10-22 | 2015-04-29 | 华为技术有限公司 | Inter-process communication method and device |
CN104572313B (en) * | 2013-10-22 | 2018-03-13 | 华为技术有限公司 | Communication means and device between a kind of process |
CN105094790A (en) * | 2014-05-22 | 2015-11-25 | 腾讯科技(深圳)有限公司 | Standardized structure based application running method and system |
CN105094790B (en) * | 2014-05-22 | 2019-05-07 | 腾讯科技(深圳)有限公司 | A kind of operation method and system of the application program based on standardized structural body |
CN106339258B (en) * | 2016-08-10 | 2019-10-18 | 西安诺瓦星云科技股份有限公司 | The management method and device of programmable logic device and microprocessor shared drive |
CN106339258A (en) * | 2016-08-10 | 2017-01-18 | 西安诺瓦电子科技有限公司 | Management method and device for shared memory of programmable logic device and microprocessor |
CN107229530A (en) * | 2017-06-29 | 2017-10-03 | 上海联影医疗科技有限公司 | Communication means, storage medium and computer equipment between process |
CN110096453A (en) * | 2019-04-11 | 2019-08-06 | 北京华三通信技术有限公司 | Data processing method and device |
CN110096453B (en) * | 2019-04-11 | 2020-01-03 | 北京华三通信技术有限公司 | Data processing method and device |
CN110727514A (en) * | 2019-10-12 | 2020-01-24 | 北京无线电测量研究所 | Memory management method based on index queue and embedded equipment |
Also Published As
Publication number | Publication date |
---|---|
CN100454241C (en) | 2009-01-21 |
Similar Documents
Publication | Publication Date | Title |
---|---|---|
US20200364148A1 (en) | Method, device and computer program product for implementing file system | |
US7206915B2 (en) | Virtual space manager for computer having a physical address extension feature | |
EP2973018B1 (en) | A method to accelerate queries using dynamically generated alternate data formats in flash cache | |
CN101030135A (en) | Method and device for storing C++ object in shared memory | |
US8478931B1 (en) | Using non-volatile memory resources to enable a virtual buffer pool for a database application | |
CN103218312B (en) | file access method and system | |
CN109697016B (en) | Method and apparatus for improving storage performance of containers | |
US20150067283A1 (en) | Image Deduplication of Guest Virtual Machines | |
RU2641244C2 (en) | Unified access to jointly used and controlled memory | |
EP2941703A1 (en) | Immutable shareable zero-copy data and streaming | |
JP2009535725A5 (en) | ||
CN116932430B (en) | Memory access control system for RDMA network cards | |
US20170220482A1 (en) | Manipulation of virtual memory page table entries to form virtually-contiguous memory corresponding to non-contiguous real memory allocations | |
JP2009512948A (en) | Method and apparatus for increasing throughput in a storage server | |
US10884948B2 (en) | Replacing pointers with hashing in tree-based page table designs | |
EP2941704B1 (en) | Zero-copy caching | |
CN116795736A (en) | Data pre-reading method, device, electronic equipment and storage medium | |
CN1255748C (en) | Metadata hierarchy management method and system of storage virtualization system | |
CN113448897B (en) | An optimization method suitable for pure user mode remote direct memory access | |
JP2008511882A (en) | Virtual address cache and method for sharing data using unique task identifiers | |
CN110362509B (en) | Unified address conversion method and unified address space | |
Scargall | Volatile use of persistent memory | |
Chan et al. | Efficient query result retrieval over the web | |
CN115905306B (en) | A local caching method, device and medium for OLAP analysis database | |
US20250123975A1 (en) | Systems and methods for buffer management during a database backup |
Legal Events
Date | Code | Title | Description |
---|---|---|---|
C06 | Publication | ||
PB01 | Publication | ||
C10 | Entry into substantive examination | ||
SE01 | Entry into force of request for substantive examination | ||
C14 | Grant of patent or utility model | ||
GR01 | Patent grant | ||
C17 | Cessation of patent right | ||
CF01 | Termination of patent right due to non-payment of annual fee |
Granted publication date: 20090121 Termination date: 20130228 |