C + + uses the Mmap/munmap function to allocate memory

Source: Internet
Author: User

There are a lot of ways to allocate and manage memory commonly used in C + +, such as smart pointers, STL containers, New/delete, Malloc/free, BRK, SBRK, and so on, and recently studied a kind of memory management method of Unix's lower level mmap/munmap, You need to maintain the allocated virtual memory entirely yourself, without any other auxiliary data structures to help maintain the memory space.

First, enter the man mmap in the terminal you can view the API documentation for this function, which is described in the following detail:

void *mmap (void *start,size_t length,int prot,int flags,int fd,off_t offsize);
Specific parameter meaning
Start: points to the starting address of the memory to be mapped, usually set to NULL, which means that the system automatically selects the address, which is returned when the mapping succeeds.
Length: Represents how much part of the file is mapped to memory.
Prot: How the map area is protected. Can be combined in several ways:
Prot_exec map area can be executed
Prot_read map area can be read
Prot_write mapped area can be written
Prot_none map area cannot be accessed
Flags: Affects the various features of the mapped area. You must specify map_shared or map_private when calling Mmap ().
Map_fixed if the address that the parameter start refers to cannot successfully establish the mapping, discard the mapping and do not fix the address. This flag is generally discouraged.
Map_shared writes data to a mapped region is copied back into the file, and other processes that map the file are allowed to be shared.
Map_private writes to a mapped region will result in a copy of the mapping file, which means that any changes made to this area by the private copy on write will not be written back to the original file contents.
map_anonymous establish an anonymous mapping. The parameter FD is ignored, the file is not involved, and the mapped area cannot be shared with other processes.
Map_denywrite only allows write operations to the mapped region, and other operations that write directly to the file will be rejected.
map_locked locks the mapped area, which means that the region will not be displaced (swap).
FD: The file descriptor to map to memory. If anonymous memory mapping is used, MAP_ANONYMOUS,FD is set to-1 in flags. Some systems do not support anonymous memory mappings, you can use fopen to open the/dev/zero file.
The file can then be mapped to the same effect as an anonymous memory map.
Offset: File-mapped offsets, usually set to 0, represent the corresponding starting from the front of the file, and offset must be an integer multiple of page_size.

return value:
If the mapping succeeds, it returns the memory start address of the mapping area, otherwise map_failed (-1) is returned, and the reason for the error is stored in errno.

Error code:
EBADF parameter FD is not a valid file description word
Eacces access rights are incorrect. If the file must be readable in the case of map_private, use map_shared to have prot_write and the file to be writable.
EINVAL parameter start, length, or offset has an illegal.
The Eagain file is locked, or too much memory is locked.
ENOMEM Insufficient memory.
The user layer of the call is very simple, its specific function is directly mapped to the physical memory directly to the user virtual memory, so that user space can be directly to the physical space operation. But for the kernel layer, its concrete implementation is more complicated.

Second, the example procedure is as follows

  main.cpp//////  Created by Chengchao on 14-9-27.//  Copyright (c) 2014 CC. All rights reserved.//#include <iostream> #include <sys/mman.h> #include <unistd.h> #include < Stdlib.h> #include <stdio.h>int main (int argc, const char * argv[]) {    //Request memory    int* arr = static_cast<in T*> (mmap (                    NULL,                   //Assigned first address                    getpagesize (),          //Allocate memory size (must be an integer multiple of the page, 32-bit 1-page =4k)                    Prot_read | Prot_write,//Map Zone protection rights: Read | Write                    Map_anon | map_shared,  ///anonymous mapping (not involving file IO), followed by two parameters ignoring                    0,                      /////to map to in-memory file descriptor                    0                       //file-map offset, usually set to 0, must be an integer multiple of the page                ));    printf ("Request Memory size =%dk\n", sizeof (arr));        *arr = ten;    * (arr + 1) =;    * (arr + 2) =;        printf ("arr[2]=%d\n", arr[2]);        Release the pointer to the memory area that arr points to, and develop a freed memory size of    Munmap (arr, getpagesize ());        return 0;}

I was compiled under 64-bit MAC OS x system, the size of 64-bit system memory page is 8k, 32 bit is 4k

C + + uses the Mmap/munmap function to allocate memory

Related Article

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.