-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmemory.c
More file actions
45 lines (35 loc) · 755 Bytes
/
memory.c
File metadata and controls
45 lines (35 loc) · 755 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <stdio.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <unistd.h>
#ifndef MAP_FAILED
#define MAP_FAILED ((caddr_t)-1)
#endif
#ifdef MAP_ANON
#define MAPFLAGS_ANON_SHARED MAP_ANON | MAP_SHARED
#else
#define MAPFLAGS_ANON_SHARED MAP_SHARED
#endif
void *memmap(int size)
{
caddr_t ptr;
int fd = -1;
#ifndef MAP_ANON
fd = open("/dev/zero", O_RDWR);
if (fd < 0)
return NULL;
#endif
ptr = mmap(NULL, size, PROT_READ | PROT_WRITE,
MAPFLAGS_ANON_SHARED,
fd, 0);
#ifndef MAP_ANON
close(fd);
#endif
if (ptr == MAP_FAILED)
return NULL;
return ptr;
}
int memunmap(void *ptr, int size) {
return munmap(ptr,size);
}