Linux uses memory ing files for Memory Pool

Source: Internet
Author: User

Because a temporary data high-speed storage needs to be implemented in recent projects, I recently learned about the memory ing file and wrote it out to share with you, if your personal skills are limited, you may be asked to correct such problems.

Mmap is a linux memory ing file, which is a method of ing files into memory address space. In fact, the method is very simple.

memfd = open(MEMFILE, O_RDWR | O_CREAT, S_IWUSR | S_IRUSR);memd = mmap(NULL, (sizeof(Type)) * size, PROT_WRITE | PROT_READ,                MAP_SHARED, memfd, 0);

Our program now has a certain address space and the Code also obtains a pointer to the first address. How should we use it? In fact, you can use it whenever you want, but we still use some methods to manage these address spaces and define the struct to manage the memory in a structured manner:

/*** \ Struct NodeHeader * \ brief node header */struct NodeHeader {unsigned int size;/*** <memory size */bool isActive; /** <whether to use */unsigned int refCount;/** <reference quantity */NodeHeader * next ;}; /*** \ struct Node * \ brief Node */struct Node {NodeHeader header;/** <Node header */char * data;/** <data */}; /*** \ struct NodeList * \ brief node list */struct NodeList {NodeList * next; unsigned int size;/*** <node size in the list */NodeHeader * header; /** <node */};/*** \ struct NodeContext * \ brief node list directory */struct NodeContext {bool isInit; /** <whether initialization */unsigned int refCount;/** <reference counter */NodeList * list;/** <node list header */};

Note that the above struct does not define the space for actually storing data, but points to the data space through the data Pointer of Node. I usually move the pointer down the corresponding value after the Node based on the size of the data space. This is what the brk () function needs to do in the implementation of the malloc function. We can simulate the allocation process by ourselves.

Initializing a node is simple:

Node * allocNode (NodeList * list) {Node * node = (Node *) nodeMem; node-> header. size = list-> size; if (! NodeContext-> isInit) {node-> header. refCount = 0; node-> header. isActive = false;} node-> data = nodeMem + sizeof (Node); nodeMem = nodeMem + sizeof (Node) + list-> size; // note, here, the pointer is moved to a certain value to reserve the corresponding space for the actual data. return node ;}

We split these consecutive address spaces through a series of initialization processes, so that we can access these memory blocks through custom interfaces. However, we would like to remind you that the IP addresses mapped between processes are different. Therefore, if we place the addresses in the ing file, the address access may be out of bounds. Therefore, the above pointer-based implementation can only be used in the process, but you can only save data in the ing file and access it through the same memory structure in different processes. Another way is to simulate the cpu Address Change Addressing and the underlying virtual memory of Linux. This gives you structured data and the flexibility of pointer access.

How can we get data? We noticed that I used POD data above, which means they will not be optimized by the C ++ Compiler (if they are used in C, this problem will not exist ), here, offset is used to obtain nodes from data:

Node* getNode(char *data){    return (Node *)(&(*data) - sizeof(NodeHeader));}

All of the above processes are simple movement of the memory area. I remember someone saying, "programming is just an addition or moving the data in the memory from one place to another ".

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.