-
-
Notifications
You must be signed in to change notification settings - Fork 34.5k
bpo-32941: Add madvise() for mmap objects #6172
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
80f12a3
9d99228
9ce2db8
5fe99a4
b6d9c48
11496fc
4aff06d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| Allow :class:`mmap.mmap` objects to access the madvise() system call | ||
| (through :meth:`mmap.mmap.madvise`). |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -708,11 +708,54 @@ mmap__sizeof__method(mmap_object *self, void *unused) | |
| } | ||
| #endif | ||
|
|
||
| #ifdef HAVE_MADVISE | ||
| static PyObject * | ||
| mmap_madvise_method(mmap_object *self, PyObject *args) | ||
| { | ||
| int option; | ||
| Py_ssize_t start = 0, length; | ||
|
|
||
| CHECK_VALID(NULL); | ||
| length = self->size; | ||
|
|
||
| if (!PyArg_ParseTuple(args, "i|nn:madvise", &option, &start, &length)) { | ||
| return NULL; | ||
| } | ||
|
|
||
| if (start < 0 || start >= self->size) { | ||
| PyErr_SetString(PyExc_ValueError, "madvise start out of bounds"); | ||
| return NULL; | ||
| } | ||
| if (length < 0) { | ||
| PyErr_SetString(PyExc_ValueError, "madvise length invalid"); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here as well: perhaps "madvise length must be > 0"?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. By the way, Linux madvise() allows length to be 0 (according to its manpage).
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well, I'm not sure why one would pass a length of zero. I think it's okay to change the check to |
||
| return NULL; | ||
| } | ||
| if (PY_SSIZE_T_MAX - start < length) { | ||
| PyErr_SetString(PyExc_OverflowError, "madvise length too large"); | ||
| return NULL; | ||
| } | ||
|
|
||
| if (start + length > self->size) { | ||
|
ZackerySpytz marked this conversation as resolved.
|
||
| length = self->size - start; | ||
| } | ||
|
|
||
| if (madvise(self->data + start, length, option) != 0) { | ||
| PyErr_SetFromErrno(PyExc_OSError); | ||
| return NULL; | ||
| } | ||
|
|
||
| Py_RETURN_NONE; | ||
| } | ||
| #endif // HAVE_MADVISE | ||
|
|
||
| static struct PyMethodDef mmap_object_methods[] = { | ||
| {"close", (PyCFunction) mmap_close_method, METH_NOARGS}, | ||
| {"find", (PyCFunction) mmap_find_method, METH_VARARGS}, | ||
| {"rfind", (PyCFunction) mmap_rfind_method, METH_VARARGS}, | ||
| {"flush", (PyCFunction) mmap_flush_method, METH_VARARGS}, | ||
| #ifdef HAVE_MADVISE | ||
| {"madvise", (PyCFunction) mmap_madvise_method, METH_VARARGS}, | ||
| #endif | ||
| {"move", (PyCFunction) mmap_move_method, METH_VARARGS}, | ||
| {"read", (PyCFunction) mmap_read_method, METH_VARARGS}, | ||
| {"read_byte", (PyCFunction) mmap_read_byte_method, METH_NOARGS}, | ||
|
|
@@ -1494,5 +1537,80 @@ PyInit_mmap(void) | |
| setint(dict, "ACCESS_READ", ACCESS_READ); | ||
| setint(dict, "ACCESS_WRITE", ACCESS_WRITE); | ||
| setint(dict, "ACCESS_COPY", ACCESS_COPY); | ||
|
|
||
| #ifdef HAVE_MADVISE | ||
| // Conventional advice values | ||
| #ifdef MADV_NORMAL | ||
| setint(dict, "MADV_NORMAL", MADV_NORMAL); | ||
| #endif | ||
| #ifdef MADV_RANDOM | ||
| setint(dict, "MADV_RANDOM", MADV_RANDOM); | ||
| #endif | ||
| #ifdef MADV_SEQUENTIAL | ||
| setint(dict, "MADV_SEQUENTIAL", MADV_SEQUENTIAL); | ||
| #endif | ||
| #ifdef MADV_WILLNEED | ||
| setint(dict, "MADV_WILLNEED", MADV_WILLNEED); | ||
| #endif | ||
| #ifdef MADV_DONTNEED | ||
| setint(dict, "MADV_DONTNEED", MADV_DONTNEED); | ||
| #endif | ||
|
|
||
| // Linux-specific advice values | ||
| #ifdef MADV_REMOVE | ||
| setint(dict, "MADV_REMOVE", MADV_REMOVE); | ||
| #endif | ||
| #ifdef MADV_DONTFORK | ||
| setint(dict, "MADV_DONTFORK", MADV_DONTFORK); | ||
| #endif | ||
| #ifdef MADV_DOFORK | ||
| setint(dict, "MADV_DOFORK", MADV_DOFORK); | ||
| #endif | ||
| #ifdef MADV_HWPOISON | ||
| setint(dict, "MADV_HWPOISON", MADV_HWPOISON); | ||
| #endif | ||
| #ifdef MADV_MERGEABLE | ||
| setint(dict, "MADV_MERGEABLE", MADV_MERGEABLE); | ||
| #endif | ||
| #ifdef MADV_UNMERGEABLE | ||
| setint(dict, "MADV_UNMERGEABLE", MADV_UNMERGEABLE); | ||
| #endif | ||
| #ifdef MADV_SOFT_OFFLINE | ||
| setint(dict, "MADV_SOFT_OFFLINE", MADV_SOFT_OFFLINE); | ||
| #endif | ||
| #ifdef MADV_HUGEPAGE | ||
| setint(dict, "MADV_HUGEPAGE", MADV_HUGEPAGE); | ||
| #endif | ||
| #ifdef MADV_NOHUGEPAGE | ||
| setint(dict, "MADV_NOHUGEPAGE", MADV_NOHUGEPAGE); | ||
| #endif | ||
| #ifdef MADV_DONTDUMP | ||
| setint(dict, "MADV_DONTDUMP", MADV_DONTDUMP); | ||
| #endif | ||
| #ifdef MADV_DODUMP | ||
| setint(dict, "MADV_DODUMP", MADV_DODUMP); | ||
| #endif | ||
| #ifdef MADV_FREE // (Also present on FreeBSD and macOS.) | ||
| setint(dict, "MADV_FREE", MADV_FREE); | ||
| #endif | ||
|
|
||
| // FreeBSD-specific | ||
| #ifdef MADV_NOSYNC | ||
| setint(dict, "MADV_NOSYNC", MADV_NOSYNC); | ||
| #endif | ||
| #ifdef MADV_AUTOSYNC | ||
| setint(dict, "MADV_AUTOSYNC", MADV_AUTOSYNC); | ||
| #endif | ||
| #ifdef MADV_NOCORE | ||
| setint(dict, "MADV_NOCORE", MADV_NOCORE); | ||
| #endif | ||
| #ifdef MADV_CORE | ||
| setint(dict, "MADV_CORE", MADV_CORE); | ||
| #endif | ||
| #ifdef MADV_PROTECT | ||
| setint(dict, "MADV_PROTECT", MADV_PROTECT); | ||
| #endif | ||
| #endif // HAVE_MADVISE | ||
|
|
||
| return module; | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.