-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmapfile.c
83 lines (62 loc) · 1.57 KB
/
mapfile.c
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// lib*nix mapfile
// memory mapping of files
#include "libnix.h"
int lnix_mmapFile (char *filename, lnix_mapFileStruct *mapfile) {
int x;
struct stat statbuf;
mapfile->fd = -1;
if ((mapfile->fd = open (filename, mapfile->o_flag)) == -1) {
return -1; // open failed
}
if (flock (mapfile->fd, mapfile->lock) == -1) {
do {
x = close (mapfile->fd);
} while ((x != 0) && (errno == EINTR));
return -2; // locking failed
}
if (!fstat (mapfile->fd, &statbuf) == 0) {
do {
x = close (mapfile->fd);
} while ((x != 0) && (errno == EINTR));
return -3; // fstat failed
}
mapfile->len = statbuf.st_size;
mapfile->addr = mmap (NULL, mapfile->len, mapfile->m_prot,
mapfile->m_flag, mapfile->fd,
mapfile->offset);
if (!(mapfile->addr)) {
do {
x = close (mapfile->fd);
} while ((x != 0) && (errno == EINTR));
return -4; // mmap failed
}
return 0;
}
int lnix_munmapFile (lnix_mapFileStruct *mapfile) {
int x;
if (!munmap (mapfile->addr, mapfile->len)) {
return -1; // munmap failed
}
do {
x = close (mapfile->fd);
} while ((x != 0) && (errno == EINTR));
return 0;
}
int lnix_mremapFile (off_t newSize, lnix_mapFileStruct *mapfile) {
int status = 0;
if (!ftruncate (mapfile->fd, newSize)) {
return -1; // ftruncate failed
}
void *addr = mmap (NULL, newSize, mapfile->m_prot,
mapfile->m_flag, mapfile->fd,
mapfile->offset);
if (!addr) {
return -2; // mmap new range failed
}
if (!munmap (mapfile->addr, mapfile->len)) {
status = 1; // munmap old range failed
}
mapfile->addr = addr;
mapfile->len = newSize;
return status;
}