forked from JeremyGrosser/fincore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfincore.c
More file actions
67 lines (54 loc) · 1.56 KB
/
Copy pathfincore.c
File metadata and controls
67 lines (54 loc) · 1.56 KB
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
#include <Python.h>
#include <unistd.h>
#include <sys/mman.h>
// fincore.fincore(fileno)
static PyObject *fincore_fincore(PyObject *self, PyObject *args) {
PyObject *ret;
int fd;
void *file_mmap;
unsigned char *mincore_vec;
struct stat file_stat;
ssize_t page_size = getpagesize();
ssize_t vec_size;
if(!PyArg_ParseTuple(args, "i", &fd)) {
return NULL;
}
if(fstat(fd, &file_stat) < 0) {
PyErr_SetFromErrno(PyExc_IOError);
return NULL;
}
file_mmap = mmap((void *)0, file_stat.st_size, PROT_NONE, MAP_SHARED, fd, 0);
if(file_mmap == MAP_FAILED) {
PyErr_SetFromErrno(PyExc_IOError);
return NULL;
}
vec_size = (file_stat.st_size + page_size - 1) / page_size;
mincore_vec = calloc(1, vec_size);
if(mincore_vec == NULL) {
PyErr_SetFromErrno(PyExc_IOError);
return NULL;
}
if(mincore(file_mmap, file_stat.st_size, mincore_vec) != 0) {
PyErr_SetFromErrno(PyExc_OSError);
return NULL;
}
ret = Py_BuildValue("s#", mincore_vec, vec_size);
free(mincore_vec);
munmap(file_mmap, file_stat.st_size);
return ret;
}
static PyMethodDef FincoreMethods[] = {
{"fincore", fincore_fincore, METH_VARARGS, "Return the mincore structure for the given file."},
{NULL, NULL, 0, NULL}
};
static struct PyModuleDef cModPyDem =
{
PyModuleDef_HEAD_INIT,
"fincore", /* name of module */
"", /* module documentation, may be NULL */
-1, /* size of per-interpreter state of the module, or -1 if the module keeps state in global variables. */
FincoreMethods
};
PyMODINIT_FUNC PyInit_fincore(void) {
return PyModule_Create(&cModPyDem);
}