mmap

Segfault while using mmap in C for reading binary files

◇◆丶佛笑我妖孽 提交于 2019-12-05 14:07:20
I am trying to use mmap in C just to see how it exactly works. Currently I am try to read a binary file byte by byte using mmap. My code is like this: #include <unistd.h> #include <sys/types.h> #include <sys/mman.h> #include <stdio.h> #include <stdlib.h> #include <fcntl.h> int main(int argc, char *argv[]) { int fd; char *data; for ( int i = 1; i<argc; i++) { if(strcmp(argv[i],"-i")==0) fd = open(argv[i+1],O_RDONLY); } data = mmap(NULL, 4000, PROT_READ, MAP_SHARED, fd, 8000); int i = 0; notation = data [i]; // ...... } My problem occurs when I try notation = data[0] and I get a segfault . I am

mmap(): resetting old memory to a zero'd non-resident state

折月煮酒 提交于 2019-12-05 12:55:34
I'm writing a memory allocation routine, and it's currently running smoothly. I get my memory from the OS with mmap() in 4096-byte pages. When I start my memory allocator I allocate 1gig of virtual address space with mmap(), and then as allocations are made I divide it up into hunks according to the specifics of my allocation algorithm. I feel safe allocating as much as a 1gig of memory on a whim because I know mmap() doesn't actually put pages into physical memory until I actually write to them. Now, the program using my allocator might have a spurt where it needs a lot of memory, and in this

mmap vs O_DIRECT for random reads (what are the buffers involved?)

邮差的信 提交于 2019-12-05 12:33:34
I am implementing a disk based hashtable supporting large amount of keys (26+ million). The value is deserialized. Reads are essentially random throughout the file, values are less than the page size, and I am optimising for SSDs. Safety/consistency are not such huge issues (performance matters). My current solution involves using a mmap() file with MADV_RANDOM | MADV_DONTNEED set to disable prefetching by the kernel and only load data as needed on-demand. I get the idea that the kernel reads from disk to memory buffer, and I deserialize from there. What about O_DIRECT ? If I call read() , I'm

Change an mmap'd memory region from MAP_SHARED to MAP_PRIVATE

僤鯓⒐⒋嵵緔 提交于 2019-12-05 11:01:11
So I have a region of memory that I have allocated with mmap() similar to the code below: void * ptr = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0); The key here is that I'm using the MAP_SHARED flag. Unlike this related question where it was sufficient to simply call mmap() again to get MAP_PRIVATE and Copy-on-Write semantics, I can't have the kernel allocate me a different range of virtual addresses. In addition, I do not want to invoke munmap() and risk the kernel giving part/all of that address range to something else within the process before I can call mmap() again. Is there

Mapping non-contiguous blocks from a file into contiguous memory addresses

时光怂恿深爱的人放手 提交于 2019-12-05 10:38:12
问题 I am interested in the prospect of using memory mapped IO, preferably exploiting the facilities in boost::interprocess for cross-platform support, to map non-contiguous system-page-size blocks in a file into a contiguous address space in memory. A simplified concrete scenario: I've a number of 'plain-old-data' structures, each of a fixed length (less than the system page size.) These structures are concatenated into a (very long) stream with the type & location of structures determined by the

Is there a practical limit on the number of memory mapped files in iOS?

谁说胖子不能爱 提交于 2019-12-05 10:31:49
I have an application that can potentially have hundreds of memory mapped, i.e., mmap() , files opened at any point in time. I'm looking for some help understanding what, if any, the practical limit is on the number of opened memory mapped files is. I create these mmap files like: void* map = mmap(0, *capacity, PROT_READ | PROT_WRITE, MAP_SHARED, file, 0); iOS kernel allocates around 700mb of virtual memory per process. So that will be your limit. The limit you have on RAM will differ as the kernel pages data into RAM from virtual memory as you touch on the mapped data. When the RAM itself

How to mmap a file in linux kernel space?

点点圈 提交于 2019-12-05 09:39:39
I try to mmap a file in a linux kernel module. I have tried to use the function do_mmap_pgoff . But the address returned is memory virtual address in current process' user space, i.e., below the kernel boundary. Instead, I want to map the file in the kernel space and get the kernel virtual address of the mapped region. Is there any kernel API in Linux support this operation? Thanks 来源: https://stackoverflow.com/questions/13465095/how-to-mmap-a-file-in-linux-kernel-space

PCI-e memory space access with mmap

穿精又带淫゛_ 提交于 2019-12-05 08:50:34
I'm using PCI-e port on Freescale MPC8308 processor (which is based on PowerPC architecture) and I have some problems when trying to use it. The endpoint PCI-e device has memory space equal to 256 MB. I can easily read and write configuration space of the endpoint device by using "pciutils" package. After writing correct values in configuration registers and getting the permission to access the memory space; I tried to access memory space by using "mmap()" function in C and used the file descriptor located at : "/sys/devices/pci0000:00/0000:00:00.0/resource0" which was exactly 256 MB (equal to

Does mremap “initialize” memory on growth?

戏子无情 提交于 2019-12-05 08:40:32
If I've mmap () some PRIVATE and ANONYMOUS pages and then extend them with mremap (), does the new space also get initialized to zeroes? I've tried reading the code for mremap ( mm/mremap.c ) in the linux source but it requires some domain-specific knowledge that I don't currently have (vma_### stuff). Not even sure that's the right place to look... But, from what I've gathered I think that mremap ()ed memory would be cleared, is this correct? Allocation is done like this list = mmap(NULL, newSize, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0) and then remap is done like this

loading file in memory using Python

若如初见. 提交于 2019-12-05 07:21:23
I try to load a file in memory with this: import mmap with open(path+fileinput+'example.txt', 'rb') as f: fileinput = mmap.mmap(f.fileno(), 0, prot=mmap.PROT_READ) When I run the code the error: AttributeError: 'module' object has no attribute 'PROT_READ' The PROT_READ and PROT_WRITE are Unix-specific. You're likely looking for: mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ) The mmap page actually has different entries for Unix/Windows version. I recently got the same error message with my test program mmap.py. Renaming my test program to something else (mmap_test.py) fixed the name