Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 1 | /* |
| 2 | / Author: Sam Rushing <rushing@nightmare.com> |
Andrew M. Kuchling | 10f9c07 | 2001-11-05 21:25:42 +0000 | [diff] [blame] | 3 | / Hacked for Unix by AMK |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 4 | / $Id$ |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 5 | |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 6 | / Modified to support mmap with offset - to map a 'window' of a file |
| 7 | / Author: Yotam Medini yotamm@mellanox.co.il |
| 8 | / |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 9 | / mmapmodule.cpp -- map a view of a file into memory |
| 10 | / |
| 11 | / todo: need permission flags, perhaps a 'chsize' analog |
| 12 | / not all functions check range yet!!! |
| 13 | / |
| 14 | / |
Mark Hammond | 071864a | 2000-07-30 02:46:26 +0000 | [diff] [blame] | 15 | / This version of mmapmodule.c has been changed significantly |
| 16 | / from the original mmapfile.c on which it was based. |
| 17 | / The original version of mmapfile is maintained by Sam at |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 18 | / ftp://squirl.nightmare.com/pub/python/python-ext. |
| 19 | */ |
| 20 | |
Martin v. Löwis | cfe7e09 | 2006-02-17 06:59:14 +0000 | [diff] [blame] | 21 | #define PY_SSIZE_T_CLEAN |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 22 | #include <Python.h> |
Erlend Egeberg Aasland | 74b4eda | 2020-11-03 10:38:31 +0100 | [diff] [blame] | 23 | #include "structmember.h" // PyMemberDef |
Victor Stinner | 4a21e57 | 2020-04-15 02:35:41 +0200 | [diff] [blame] | 24 | #include <stddef.h> // offsetof() |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 25 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 26 | #ifndef MS_WINDOWS |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 27 | #define UNIX |
Benjamin Peterson | f6b5cad | 2015-08-02 12:15:30 -0700 | [diff] [blame] | 28 | # ifdef HAVE_FCNTL_H |
Victor Stinner | a6cd0cf | 2011-05-02 01:05:37 +0200 | [diff] [blame] | 29 | # include <fcntl.h> |
Benjamin Peterson | f6b5cad | 2015-08-02 12:15:30 -0700 | [diff] [blame] | 30 | # endif /* HAVE_FCNTL_H */ |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 31 | #endif |
| 32 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 33 | #ifdef MS_WINDOWS |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 34 | #include <windows.h> |
Fred Drake | 145f96e | 2000-10-01 17:50:46 +0000 | [diff] [blame] | 35 | static int |
| 36 | my_getpagesize(void) |
| 37 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 38 | SYSTEM_INFO si; |
| 39 | GetSystemInfo(&si); |
| 40 | return si.dwPageSize; |
Fred Drake | 145f96e | 2000-10-01 17:50:46 +0000 | [diff] [blame] | 41 | } |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 42 | |
| 43 | static int |
| 44 | my_getallocationgranularity (void) |
| 45 | { |
| 46 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 47 | SYSTEM_INFO si; |
| 48 | GetSystemInfo(&si); |
| 49 | return si.dwAllocationGranularity; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 50 | } |
| 51 | |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 52 | #endif |
| 53 | |
| 54 | #ifdef UNIX |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 55 | #include <sys/mman.h> |
Andrew M. Kuchling | 7b9fb92 | 2000-06-17 22:41:22 +0000 | [diff] [blame] | 56 | #include <sys/stat.h> |
Guido van Rossum | 4b36e6b | 2000-09-25 13:16:15 +0000 | [diff] [blame] | 57 | |
Fred Drake | 145f96e | 2000-10-01 17:50:46 +0000 | [diff] [blame] | 58 | #if defined(HAVE_SYSCONF) && defined(_SC_PAGESIZE) |
| 59 | static int |
| 60 | my_getpagesize(void) |
| 61 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 62 | return sysconf(_SC_PAGESIZE); |
Fred Drake | 145f96e | 2000-10-01 17:50:46 +0000 | [diff] [blame] | 63 | } |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 64 | |
| 65 | #define my_getallocationgranularity my_getpagesize |
Fred Drake | 145f96e | 2000-10-01 17:50:46 +0000 | [diff] [blame] | 66 | #else |
| 67 | #define my_getpagesize getpagesize |
| 68 | #endif |
| 69 | |
Guido van Rossum | 4b36e6b | 2000-09-25 13:16:15 +0000 | [diff] [blame] | 70 | #endif /* UNIX */ |
| 71 | |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 72 | #include <string.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 73 | |
| 74 | #ifdef HAVE_SYS_TYPES_H |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 75 | #include <sys/types.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 76 | #endif /* HAVE_SYS_TYPES_H */ |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 77 | |
Neal Norwitz | 3eaf2b5 | 2006-02-16 08:08:54 +0000 | [diff] [blame] | 78 | /* Prefer MAP_ANONYMOUS since MAP_ANON is deprecated according to man page. */ |
Neal Norwitz | 0e6bc8c | 2006-02-05 05:45:43 +0000 | [diff] [blame] | 79 | #if !defined(MAP_ANONYMOUS) && defined(MAP_ANON) |
| 80 | # define MAP_ANONYMOUS MAP_ANON |
| 81 | #endif |
| 82 | |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 83 | typedef enum |
| 84 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 85 | ACCESS_DEFAULT, |
| 86 | ACCESS_READ, |
| 87 | ACCESS_WRITE, |
| 88 | ACCESS_COPY |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 89 | } access_mode; |
| 90 | |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 91 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 92 | PyObject_HEAD |
| 93 | char * data; |
Benjamin Peterson | cd04db0 | 2016-10-05 21:45:48 -0700 | [diff] [blame] | 94 | Py_ssize_t size; |
| 95 | Py_ssize_t pos; /* relative to offset */ |
Antoine Pitrou | 97696cb | 2011-02-21 23:46:27 +0000 | [diff] [blame] | 96 | #ifdef MS_WINDOWS |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 97 | long long offset; |
Antoine Pitrou | 97696cb | 2011-02-21 23:46:27 +0000 | [diff] [blame] | 98 | #else |
| 99 | off_t offset; |
| 100 | #endif |
Hai Shi | 06cd5b6 | 2019-10-21 14:31:46 +0800 | [diff] [blame] | 101 | Py_ssize_t exports; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 102 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 103 | #ifdef MS_WINDOWS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 104 | HANDLE map_handle; |
| 105 | HANDLE file_handle; |
| 106 | char * tagname; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 107 | #endif |
| 108 | |
| 109 | #ifdef UNIX |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 110 | int fd; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 111 | #endif |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 112 | |
Antoine Pitrou | c53204b | 2013-08-05 23:17:30 +0200 | [diff] [blame] | 113 | PyObject *weakreflist; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 114 | access_mode access; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 115 | } mmap_object; |
| 116 | |
Erlend Egeberg Aasland | 74b4eda | 2020-11-03 10:38:31 +0100 | [diff] [blame] | 117 | typedef struct { |
| 118 | PyTypeObject *mmap_object_type; |
| 119 | } mmap_state; |
| 120 | |
| 121 | static mmap_state * |
| 122 | get_mmap_state(PyObject *module) |
| 123 | { |
| 124 | mmap_state *state = PyModule_GetState(module); |
| 125 | assert(state); |
| 126 | return state; |
| 127 | } |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 128 | |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 129 | static void |
Fredrik Lundh | 54cf3dc | 2000-07-08 22:05:01 +0000 | [diff] [blame] | 130 | mmap_object_dealloc(mmap_object *m_obj) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 131 | { |
Erlend Egeberg Aasland | 74b4eda | 2020-11-03 10:38:31 +0100 | [diff] [blame] | 132 | PyTypeObject *tp = Py_TYPE(m_obj); |
| 133 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 134 | #ifdef MS_WINDOWS |
Davide Rizzo | dc07894 | 2019-03-06 18:08:31 +0100 | [diff] [blame] | 135 | Py_BEGIN_ALLOW_THREADS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 136 | if (m_obj->data != NULL) |
| 137 | UnmapViewOfFile (m_obj->data); |
| 138 | if (m_obj->map_handle != NULL) |
| 139 | CloseHandle (m_obj->map_handle); |
| 140 | if (m_obj->file_handle != INVALID_HANDLE_VALUE) |
| 141 | CloseHandle (m_obj->file_handle); |
Davide Rizzo | dc07894 | 2019-03-06 18:08:31 +0100 | [diff] [blame] | 142 | Py_END_ALLOW_THREADS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 143 | if (m_obj->tagname) |
| 144 | PyMem_Free(m_obj->tagname); |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 145 | #endif /* MS_WINDOWS */ |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 146 | |
| 147 | #ifdef UNIX |
Davide Rizzo | dc07894 | 2019-03-06 18:08:31 +0100 | [diff] [blame] | 148 | Py_BEGIN_ALLOW_THREADS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 149 | if (m_obj->fd >= 0) |
| 150 | (void) close(m_obj->fd); |
| 151 | if (m_obj->data!=NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 152 | munmap(m_obj->data, m_obj->size); |
| 153 | } |
Davide Rizzo | bb9593a | 2019-03-06 16:52:34 +0100 | [diff] [blame] | 154 | Py_END_ALLOW_THREADS |
Davide Rizzo | dc07894 | 2019-03-06 18:08:31 +0100 | [diff] [blame] | 155 | #endif /* UNIX */ |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 156 | |
Antoine Pitrou | c53204b | 2013-08-05 23:17:30 +0200 | [diff] [blame] | 157 | if (m_obj->weakreflist != NULL) |
| 158 | PyObject_ClearWeakRefs((PyObject *) m_obj); |
Erlend Egeberg Aasland | 74b4eda | 2020-11-03 10:38:31 +0100 | [diff] [blame] | 159 | |
| 160 | tp->tp_free(m_obj); |
| 161 | Py_DECREF(tp); |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | static PyObject * |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 165 | mmap_close_method(mmap_object *self, PyObject *unused) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 166 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 167 | if (self->exports > 0) { |
| 168 | PyErr_SetString(PyExc_BufferError, "cannot close "\ |
| 169 | "exported pointers exist"); |
| 170 | return NULL; |
| 171 | } |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 172 | #ifdef MS_WINDOWS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 173 | /* For each resource we maintain, we need to check |
| 174 | the value is valid, and if so, free the resource |
| 175 | and set the member value to an invalid value so |
| 176 | the dealloc does not attempt to resource clearing |
| 177 | again. |
| 178 | TODO - should we check for errors in the close operations??? |
| 179 | */ |
Davide Rizzo | bb9593a | 2019-03-06 16:52:34 +0100 | [diff] [blame] | 180 | HANDLE map_handle = self->map_handle; |
| 181 | HANDLE file_handle = self->file_handle; |
| 182 | char *data = self->data; |
| 183 | self->map_handle = NULL; |
| 184 | self->file_handle = INVALID_HANDLE_VALUE; |
| 185 | self->data = NULL; |
| 186 | Py_BEGIN_ALLOW_THREADS |
| 187 | if (data != NULL) { |
| 188 | UnmapViewOfFile(data); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 189 | } |
Davide Rizzo | bb9593a | 2019-03-06 16:52:34 +0100 | [diff] [blame] | 190 | if (map_handle != NULL) { |
| 191 | CloseHandle(map_handle); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 192 | } |
Davide Rizzo | bb9593a | 2019-03-06 16:52:34 +0100 | [diff] [blame] | 193 | if (file_handle != INVALID_HANDLE_VALUE) { |
| 194 | CloseHandle(file_handle); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 195 | } |
Davide Rizzo | bb9593a | 2019-03-06 16:52:34 +0100 | [diff] [blame] | 196 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 197 | #endif /* MS_WINDOWS */ |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 198 | |
| 199 | #ifdef UNIX |
Davide Rizzo | bb9593a | 2019-03-06 16:52:34 +0100 | [diff] [blame] | 200 | int fd = self->fd; |
| 201 | char *data = self->data; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 202 | self->fd = -1; |
Davide Rizzo | bb9593a | 2019-03-06 16:52:34 +0100 | [diff] [blame] | 203 | self->data = NULL; |
| 204 | Py_BEGIN_ALLOW_THREADS |
| 205 | if (0 <= fd) |
| 206 | (void) close(fd); |
| 207 | if (data != NULL) { |
| 208 | munmap(data, self->size); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 209 | } |
Davide Rizzo | bb9593a | 2019-03-06 16:52:34 +0100 | [diff] [blame] | 210 | Py_END_ALLOW_THREADS |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 211 | #endif |
| 212 | |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 213 | Py_RETURN_NONE; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 214 | } |
| 215 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 216 | #ifdef MS_WINDOWS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 217 | #define CHECK_VALID(err) \ |
| 218 | do { \ |
| 219 | if (self->map_handle == NULL) { \ |
| 220 | PyErr_SetString(PyExc_ValueError, "mmap closed or invalid"); \ |
| 221 | return err; \ |
| 222 | } \ |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 223 | } while (0) |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 224 | #endif /* MS_WINDOWS */ |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 225 | |
| 226 | #ifdef UNIX |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 227 | #define CHECK_VALID(err) \ |
| 228 | do { \ |
| 229 | if (self->data == NULL) { \ |
| 230 | PyErr_SetString(PyExc_ValueError, "mmap closed or invalid"); \ |
| 231 | return err; \ |
| 232 | } \ |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 233 | } while (0) |
| 234 | #endif /* UNIX */ |
| 235 | |
| 236 | static PyObject * |
Fredrik Lundh | 54cf3dc | 2000-07-08 22:05:01 +0000 | [diff] [blame] | 237 | mmap_read_byte_method(mmap_object *self, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 238 | PyObject *unused) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 239 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 240 | CHECK_VALID(NULL); |
Benjamin Peterson | cd04db0 | 2016-10-05 21:45:48 -0700 | [diff] [blame] | 241 | if (self->pos >= self->size) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 242 | PyErr_SetString(PyExc_ValueError, "read byte out of range"); |
| 243 | return NULL; |
| 244 | } |
Benjamin Peterson | cd04db0 | 2016-10-05 21:45:48 -0700 | [diff] [blame] | 245 | return PyLong_FromLong((unsigned char)self->data[self->pos++]); |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | static PyObject * |
Fredrik Lundh | 54cf3dc | 2000-07-08 22:05:01 +0000 | [diff] [blame] | 249 | mmap_read_line_method(mmap_object *self, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 250 | PyObject *unused) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 251 | { |
Benjamin Peterson | cd04db0 | 2016-10-05 21:45:48 -0700 | [diff] [blame] | 252 | Py_ssize_t remaining; |
| 253 | char *start, *eol; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 254 | PyObject *result; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 255 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 256 | CHECK_VALID(NULL); |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 257 | |
Benjamin Peterson | cd04db0 | 2016-10-05 21:45:48 -0700 | [diff] [blame] | 258 | remaining = (self->pos < self->size) ? self->size - self->pos : 0; |
| 259 | if (!remaining) |
| 260 | return PyBytes_FromString(""); |
| 261 | start = self->data + self->pos; |
| 262 | eol = memchr(start, '\n', remaining); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 263 | if (!eol) |
Benjamin Peterson | cd04db0 | 2016-10-05 21:45:48 -0700 | [diff] [blame] | 264 | eol = self->data + self->size; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 265 | else |
Benjamin Peterson | cd04db0 | 2016-10-05 21:45:48 -0700 | [diff] [blame] | 266 | ++eol; /* advance past newline */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 267 | result = PyBytes_FromStringAndSize(start, (eol - start)); |
| 268 | self->pos += (eol - start); |
| 269 | return result; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | static PyObject * |
Fredrik Lundh | 54cf3dc | 2000-07-08 22:05:01 +0000 | [diff] [blame] | 273 | mmap_read_method(mmap_object *self, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 274 | PyObject *args) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 275 | { |
Benjamin Peterson | 8f1cdc6 | 2016-10-05 23:32:09 -0700 | [diff] [blame] | 276 | Py_ssize_t num_bytes = PY_SSIZE_T_MAX, remaining; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 277 | PyObject *result; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 278 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 279 | CHECK_VALID(NULL); |
Serhiy Storchaka | 762bf40 | 2017-03-30 09:15:31 +0300 | [diff] [blame] | 280 | if (!PyArg_ParseTuple(args, "|O&:read", _Py_convert_optional_to_ssize_t, &num_bytes)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 281 | return(NULL); |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 282 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 283 | /* silently 'adjust' out-of-range requests */ |
Benjamin Peterson | cd04db0 | 2016-10-05 21:45:48 -0700 | [diff] [blame] | 284 | remaining = (self->pos < self->size) ? self->size - self->pos : 0; |
| 285 | if (num_bytes < 0 || num_bytes > remaining) |
| 286 | num_bytes = remaining; |
| 287 | result = PyBytes_FromStringAndSize(&self->data[self->pos], num_bytes); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 288 | self->pos += num_bytes; |
| 289 | return result; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | static PyObject * |
Georg Brandl | fceab5a | 2008-01-19 20:08:23 +0000 | [diff] [blame] | 293 | mmap_gfind(mmap_object *self, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 294 | PyObject *args, |
| 295 | int reverse) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 296 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 297 | Py_ssize_t start = self->pos; |
| 298 | Py_ssize_t end = self->size; |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 299 | Py_buffer view; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 300 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 301 | CHECK_VALID(NULL); |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 302 | if (!PyArg_ParseTuple(args, reverse ? "y*|nn:rfind" : "y*|nn:find", |
| 303 | &view, &start, &end)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 304 | return NULL; |
| 305 | } else { |
| 306 | const char *p, *start_p, *end_p; |
| 307 | int sign = reverse ? -1 : 1; |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 308 | const char *needle = view.buf; |
| 309 | Py_ssize_t len = view.len; |
Greg Stein | 834f4dd | 2001-05-14 09:32:26 +0000 | [diff] [blame] | 310 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 311 | if (start < 0) |
| 312 | start += self->size; |
| 313 | if (start < 0) |
| 314 | start = 0; |
Benjamin Peterson | cd04db0 | 2016-10-05 21:45:48 -0700 | [diff] [blame] | 315 | else if (start > self->size) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 316 | start = self->size; |
Greg Stein | 834f4dd | 2001-05-14 09:32:26 +0000 | [diff] [blame] | 317 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 318 | if (end < 0) |
| 319 | end += self->size; |
| 320 | if (end < 0) |
| 321 | end = 0; |
Benjamin Peterson | cd04db0 | 2016-10-05 21:45:48 -0700 | [diff] [blame] | 322 | else if (end > self->size) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 323 | end = self->size; |
Georg Brandl | fceab5a | 2008-01-19 20:08:23 +0000 | [diff] [blame] | 324 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 325 | start_p = self->data + start; |
| 326 | end_p = self->data + end; |
Georg Brandl | fceab5a | 2008-01-19 20:08:23 +0000 | [diff] [blame] | 327 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 328 | for (p = (reverse ? end_p - len : start_p); |
| 329 | (p >= start_p) && (p + len <= end_p); p += sign) { |
| 330 | Py_ssize_t i; |
| 331 | for (i = 0; i < len && needle[i] == p[i]; ++i) |
| 332 | /* nothing */; |
| 333 | if (i == len) { |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 334 | PyBuffer_Release(&view); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 335 | return PyLong_FromSsize_t(p - self->data); |
| 336 | } |
| 337 | } |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 338 | PyBuffer_Release(&view); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 339 | return PyLong_FromLong(-1); |
| 340 | } |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 341 | } |
| 342 | |
Georg Brandl | fceab5a | 2008-01-19 20:08:23 +0000 | [diff] [blame] | 343 | static PyObject * |
| 344 | mmap_find_method(mmap_object *self, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 345 | PyObject *args) |
Georg Brandl | fceab5a | 2008-01-19 20:08:23 +0000 | [diff] [blame] | 346 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 347 | return mmap_gfind(self, args, 0); |
Georg Brandl | fceab5a | 2008-01-19 20:08:23 +0000 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | static PyObject * |
| 351 | mmap_rfind_method(mmap_object *self, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 352 | PyObject *args) |
Georg Brandl | fceab5a | 2008-01-19 20:08:23 +0000 | [diff] [blame] | 353 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 354 | return mmap_gfind(self, args, 1); |
Georg Brandl | fceab5a | 2008-01-19 20:08:23 +0000 | [diff] [blame] | 355 | } |
| 356 | |
Tim Peters | ec0a5f0 | 2006-02-16 23:47:20 +0000 | [diff] [blame] | 357 | static int |
Sean Reifscheider | 54cf12b | 2007-09-17 17:55:36 +0000 | [diff] [blame] | 358 | is_writable(mmap_object *self) |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 359 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 360 | if (self->access != ACCESS_READ) |
| 361 | return 1; |
| 362 | PyErr_Format(PyExc_TypeError, "mmap can't modify a readonly memory map."); |
| 363 | return 0; |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 364 | } |
| 365 | |
Tim Peters | ec0a5f0 | 2006-02-16 23:47:20 +0000 | [diff] [blame] | 366 | static int |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 367 | is_resizeable(mmap_object *self) |
| 368 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 369 | if (self->exports > 0) { |
| 370 | PyErr_SetString(PyExc_BufferError, |
| 371 | "mmap can't resize with extant buffers exported."); |
| 372 | return 0; |
| 373 | } |
| 374 | if ((self->access == ACCESS_WRITE) || (self->access == ACCESS_DEFAULT)) |
| 375 | return 1; |
| 376 | PyErr_Format(PyExc_TypeError, |
| 377 | "mmap can't resize a readonly or copy-on-write memory map."); |
| 378 | return 0; |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 382 | static PyObject * |
Fredrik Lundh | 54cf3dc | 2000-07-08 22:05:01 +0000 | [diff] [blame] | 383 | mmap_write_method(mmap_object *self, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 384 | PyObject *args) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 385 | { |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 386 | Py_buffer data; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 387 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 388 | CHECK_VALID(NULL); |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 389 | if (!PyArg_ParseTuple(args, "y*:write", &data)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 390 | return(NULL); |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 391 | |
Benjamin Peterson | 3776836 | 2016-10-05 23:29:07 -0700 | [diff] [blame] | 392 | if (!is_writable(self)) { |
| 393 | PyBuffer_Release(&data); |
Benjamin Peterson | cd04db0 | 2016-10-05 21:45:48 -0700 | [diff] [blame] | 394 | return NULL; |
Benjamin Peterson | 3776836 | 2016-10-05 23:29:07 -0700 | [diff] [blame] | 395 | } |
Benjamin Peterson | cd04db0 | 2016-10-05 21:45:48 -0700 | [diff] [blame] | 396 | |
| 397 | if (self->pos > self->size || self->size - self->pos < data.len) { |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 398 | PyBuffer_Release(&data); |
Benjamin Peterson | cd04db0 | 2016-10-05 21:45:48 -0700 | [diff] [blame] | 399 | PyErr_SetString(PyExc_ValueError, "data out of range"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 400 | return NULL; |
| 401 | } |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 402 | |
Benjamin Peterson | cd04db0 | 2016-10-05 21:45:48 -0700 | [diff] [blame] | 403 | memcpy(&self->data[self->pos], data.buf, data.len); |
| 404 | self->pos += data.len; |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 405 | PyBuffer_Release(&data); |
Benjamin Peterson | 87845bc | 2016-10-05 22:54:19 -0700 | [diff] [blame] | 406 | return PyLong_FromSsize_t(data.len); |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | static PyObject * |
Fredrik Lundh | 54cf3dc | 2000-07-08 22:05:01 +0000 | [diff] [blame] | 410 | mmap_write_byte_method(mmap_object *self, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 411 | PyObject *args) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 412 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 413 | char value; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 414 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 415 | CHECK_VALID(NULL); |
| 416 | if (!PyArg_ParseTuple(args, "b:write_byte", &value)) |
| 417 | return(NULL); |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 418 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 419 | if (!is_writable(self)) |
| 420 | return NULL; |
Hirokazu Yamamoto | 39c6dea | 2009-02-28 10:56:50 +0000 | [diff] [blame] | 421 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 422 | if (self->pos < self->size) { |
Benjamin Peterson | cd04db0 | 2016-10-05 21:45:48 -0700 | [diff] [blame] | 423 | self->data[self->pos++] = value; |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 424 | Py_RETURN_NONE; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 425 | } |
| 426 | else { |
| 427 | PyErr_SetString(PyExc_ValueError, "write byte out of range"); |
| 428 | return NULL; |
| 429 | } |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 430 | } |
Tim Peters | ec0a5f0 | 2006-02-16 23:47:20 +0000 | [diff] [blame] | 431 | |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 432 | static PyObject * |
Fredrik Lundh | 54cf3dc | 2000-07-08 22:05:01 +0000 | [diff] [blame] | 433 | mmap_size_method(mmap_object *self, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 434 | PyObject *unused) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 435 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 436 | CHECK_VALID(NULL); |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 437 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 438 | #ifdef MS_WINDOWS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 439 | if (self->file_handle != INVALID_HANDLE_VALUE) { |
| 440 | DWORD low,high; |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 441 | long long size; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 442 | low = GetFileSize(self->file_handle, &high); |
| 443 | if (low == INVALID_FILE_SIZE) { |
| 444 | /* It might be that the function appears to have failed, |
| 445 | when indeed its size equals INVALID_FILE_SIZE */ |
| 446 | DWORD error = GetLastError(); |
| 447 | if (error != NO_ERROR) |
| 448 | return PyErr_SetFromWindowsErr(error); |
| 449 | } |
| 450 | if (!high && low < LONG_MAX) |
| 451 | return PyLong_FromLong((long)low); |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 452 | size = (((long long)high)<<32) + low; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 453 | return PyLong_FromLongLong(size); |
| 454 | } else { |
| 455 | return PyLong_FromSsize_t(self->size); |
| 456 | } |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 457 | #endif /* MS_WINDOWS */ |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 458 | |
| 459 | #ifdef UNIX |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 460 | { |
Victor Stinner | e134a7f | 2015-03-30 10:09:31 +0200 | [diff] [blame] | 461 | struct _Py_stat_struct status; |
| 462 | if (_Py_fstat(self->fd, &status) == -1) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 463 | return NULL; |
Antoine Pitrou | 97696cb | 2011-02-21 23:46:27 +0000 | [diff] [blame] | 464 | #ifdef HAVE_LARGEFILE_SUPPORT |
Victor Stinner | e134a7f | 2015-03-30 10:09:31 +0200 | [diff] [blame] | 465 | return PyLong_FromLongLong(status.st_size); |
Antoine Pitrou | 97696cb | 2011-02-21 23:46:27 +0000 | [diff] [blame] | 466 | #else |
Victor Stinner | e134a7f | 2015-03-30 10:09:31 +0200 | [diff] [blame] | 467 | return PyLong_FromLong(status.st_size); |
Antoine Pitrou | 97696cb | 2011-02-21 23:46:27 +0000 | [diff] [blame] | 468 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 469 | } |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 470 | #endif /* UNIX */ |
| 471 | } |
| 472 | |
| 473 | /* This assumes that you want the entire file mapped, |
| 474 | / and when recreating the map will make the new file |
| 475 | / have the new size |
| 476 | / |
| 477 | / Is this really necessary? This could easily be done |
| 478 | / from python by just closing and re-opening with the |
| 479 | / new size? |
| 480 | */ |
| 481 | |
| 482 | static PyObject * |
Fredrik Lundh | 54cf3dc | 2000-07-08 22:05:01 +0000 | [diff] [blame] | 483 | mmap_resize_method(mmap_object *self, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 484 | PyObject *args) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 485 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 486 | Py_ssize_t new_size; |
| 487 | CHECK_VALID(NULL); |
| 488 | if (!PyArg_ParseTuple(args, "n:resize", &new_size) || |
| 489 | !is_resizeable(self)) { |
| 490 | return NULL; |
Benjamin Peterson | cd04db0 | 2016-10-05 21:45:48 -0700 | [diff] [blame] | 491 | } |
| 492 | if (new_size < 0 || PY_SSIZE_T_MAX - new_size < self->offset) { |
| 493 | PyErr_SetString(PyExc_ValueError, "new size out of range"); |
| 494 | return NULL; |
| 495 | } |
| 496 | |
| 497 | { |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 498 | #ifdef MS_WINDOWS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 499 | DWORD dwErrCode = 0; |
| 500 | DWORD off_hi, off_lo, newSizeLow, newSizeHigh; |
| 501 | /* First, unmap the file view */ |
| 502 | UnmapViewOfFile(self->data); |
| 503 | self->data = NULL; |
| 504 | /* Close the mapping object */ |
| 505 | CloseHandle(self->map_handle); |
| 506 | self->map_handle = NULL; |
| 507 | /* Move to the desired EOF position */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 508 | newSizeHigh = (DWORD)((self->offset + new_size) >> 32); |
| 509 | newSizeLow = (DWORD)((self->offset + new_size) & 0xFFFFFFFF); |
| 510 | off_hi = (DWORD)(self->offset >> 32); |
| 511 | off_lo = (DWORD)(self->offset & 0xFFFFFFFF); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 512 | SetFilePointer(self->file_handle, |
| 513 | newSizeLow, &newSizeHigh, FILE_BEGIN); |
| 514 | /* Change the size of the file */ |
| 515 | SetEndOfFile(self->file_handle); |
| 516 | /* Create another mapping object and remap the file view */ |
| 517 | self->map_handle = CreateFileMapping( |
| 518 | self->file_handle, |
| 519 | NULL, |
| 520 | PAGE_READWRITE, |
| 521 | 0, |
| 522 | 0, |
| 523 | self->tagname); |
| 524 | if (self->map_handle != NULL) { |
| 525 | self->data = (char *) MapViewOfFile(self->map_handle, |
| 526 | FILE_MAP_WRITE, |
| 527 | off_hi, |
| 528 | off_lo, |
| 529 | new_size); |
| 530 | if (self->data != NULL) { |
| 531 | self->size = new_size; |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 532 | Py_RETURN_NONE; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 533 | } else { |
| 534 | dwErrCode = GetLastError(); |
| 535 | CloseHandle(self->map_handle); |
| 536 | self->map_handle = NULL; |
| 537 | } |
| 538 | } else { |
| 539 | dwErrCode = GetLastError(); |
| 540 | } |
| 541 | PyErr_SetFromWindowsErr(dwErrCode); |
| 542 | return NULL; |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 543 | #endif /* MS_WINDOWS */ |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 544 | |
| 545 | #ifdef UNIX |
Tim Peters | ec0a5f0 | 2006-02-16 23:47:20 +0000 | [diff] [blame] | 546 | #ifndef HAVE_MREMAP |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 547 | PyErr_SetString(PyExc_SystemError, |
| 548 | "mmap: resizing not available--no mremap()"); |
| 549 | return NULL; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 550 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 551 | void *newmap; |
Armin Rigo | 335ffe8 | 2005-09-20 19:04:02 +0000 | [diff] [blame] | 552 | |
Benjamin Peterson | cd04db0 | 2016-10-05 21:45:48 -0700 | [diff] [blame] | 553 | if (self->fd != -1 && ftruncate(self->fd, self->offset + new_size) == -1) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 554 | PyErr_SetFromErrno(PyExc_OSError); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 555 | return NULL; |
| 556 | } |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 557 | |
Andrew M. Kuchling | 6fef30e | 2000-06-18 14:51:21 +0000 | [diff] [blame] | 558 | #ifdef MREMAP_MAYMOVE |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 559 | newmap = mremap(self->data, self->size, new_size, MREMAP_MAYMOVE); |
Andrew M. Kuchling | 6fef30e | 2000-06-18 14:51:21 +0000 | [diff] [blame] | 560 | #else |
Benjamin Peterson | cd04db0 | 2016-10-05 21:45:48 -0700 | [diff] [blame] | 561 | #if defined(__NetBSD__) |
| 562 | newmap = mremap(self->data, self->size, self->data, new_size, 0); |
| 563 | #else |
| 564 | newmap = mremap(self->data, self->size, new_size, 0); |
| 565 | #endif /* __NetBSD__ */ |
Andrew M. Kuchling | 6fef30e | 2000-06-18 14:51:21 +0000 | [diff] [blame] | 566 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 567 | if (newmap == (void *)-1) |
| 568 | { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 569 | PyErr_SetFromErrno(PyExc_OSError); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 570 | return NULL; |
| 571 | } |
| 572 | self->data = newmap; |
| 573 | self->size = new_size; |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 574 | Py_RETURN_NONE; |
Andrew M. Kuchling | 6fef30e | 2000-06-18 14:51:21 +0000 | [diff] [blame] | 575 | #endif /* HAVE_MREMAP */ |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 576 | #endif /* UNIX */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 577 | } |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 578 | } |
| 579 | |
| 580 | static PyObject * |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 581 | mmap_tell_method(mmap_object *self, PyObject *unused) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 582 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 583 | CHECK_VALID(NULL); |
| 584 | return PyLong_FromSize_t(self->pos); |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 585 | } |
| 586 | |
| 587 | static PyObject * |
Fredrik Lundh | 54cf3dc | 2000-07-08 22:05:01 +0000 | [diff] [blame] | 588 | mmap_flush_method(mmap_object *self, PyObject *args) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 589 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 590 | Py_ssize_t offset = 0; |
| 591 | Py_ssize_t size = self->size; |
| 592 | CHECK_VALID(NULL); |
| 593 | if (!PyArg_ParseTuple(args, "|nn:flush", &offset, &size)) |
| 594 | return NULL; |
Benjamin Peterson | cd04db0 | 2016-10-05 21:45:48 -0700 | [diff] [blame] | 595 | if (size < 0 || offset < 0 || self->size - offset < size) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 596 | PyErr_SetString(PyExc_ValueError, "flush values out of range"); |
| 597 | return NULL; |
| 598 | } |
R. David Murray | e194dd6 | 2010-10-18 01:14:06 +0000 | [diff] [blame] | 599 | |
| 600 | if (self->access == ACCESS_READ || self->access == ACCESS_COPY) |
Berker Peksag | e7d4b2f | 2018-08-22 21:21:05 +0300 | [diff] [blame] | 601 | Py_RETURN_NONE; |
R. David Murray | e194dd6 | 2010-10-18 01:14:06 +0000 | [diff] [blame] | 602 | |
Christian Heimes | af98da1 | 2008-01-27 15:18:18 +0000 | [diff] [blame] | 603 | #ifdef MS_WINDOWS |
Berker Peksag | e7d4b2f | 2018-08-22 21:21:05 +0300 | [diff] [blame] | 604 | if (!FlushViewOfFile(self->data+offset, size)) { |
| 605 | PyErr_SetFromWindowsErr(GetLastError()); |
| 606 | return NULL; |
| 607 | } |
| 608 | Py_RETURN_NONE; |
Christian Heimes | af98da1 | 2008-01-27 15:18:18 +0000 | [diff] [blame] | 609 | #elif defined(UNIX) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 610 | /* XXX flags for msync? */ |
| 611 | if (-1 == msync(self->data + offset, size, MS_SYNC)) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 612 | PyErr_SetFromErrno(PyExc_OSError); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 613 | return NULL; |
| 614 | } |
Berker Peksag | e7d4b2f | 2018-08-22 21:21:05 +0300 | [diff] [blame] | 615 | Py_RETURN_NONE; |
Christian Heimes | af98da1 | 2008-01-27 15:18:18 +0000 | [diff] [blame] | 616 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 617 | PyErr_SetString(PyExc_ValueError, "flush not supported on this system"); |
| 618 | return NULL; |
Christian Heimes | af98da1 | 2008-01-27 15:18:18 +0000 | [diff] [blame] | 619 | #endif |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 620 | } |
| 621 | |
| 622 | static PyObject * |
Fredrik Lundh | 54cf3dc | 2000-07-08 22:05:01 +0000 | [diff] [blame] | 623 | mmap_seek_method(mmap_object *self, PyObject *args) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 624 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 625 | Py_ssize_t dist; |
| 626 | int how=0; |
| 627 | CHECK_VALID(NULL); |
| 628 | if (!PyArg_ParseTuple(args, "n|i:seek", &dist, &how)) |
| 629 | return NULL; |
| 630 | else { |
Benjamin Peterson | cd04db0 | 2016-10-05 21:45:48 -0700 | [diff] [blame] | 631 | Py_ssize_t where; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 632 | switch (how) { |
| 633 | case 0: /* relative to start */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 634 | where = dist; |
| 635 | break; |
| 636 | case 1: /* relative to current position */ |
Benjamin Peterson | cd04db0 | 2016-10-05 21:45:48 -0700 | [diff] [blame] | 637 | if (PY_SSIZE_T_MAX - self->pos < dist) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 638 | goto onoutofrange; |
| 639 | where = self->pos + dist; |
| 640 | break; |
| 641 | case 2: /* relative to end */ |
Benjamin Peterson | cd04db0 | 2016-10-05 21:45:48 -0700 | [diff] [blame] | 642 | if (PY_SSIZE_T_MAX - self->size < dist) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 643 | goto onoutofrange; |
| 644 | where = self->size + dist; |
| 645 | break; |
| 646 | default: |
| 647 | PyErr_SetString(PyExc_ValueError, "unknown seek type"); |
| 648 | return NULL; |
| 649 | } |
Benjamin Peterson | cd04db0 | 2016-10-05 21:45:48 -0700 | [diff] [blame] | 650 | if (where > self->size || where < 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 651 | goto onoutofrange; |
| 652 | self->pos = where; |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 653 | Py_RETURN_NONE; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 654 | } |
Andrew M. Kuchling | 70d2742 | 2000-06-18 04:45:14 +0000 | [diff] [blame] | 655 | |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 656 | onoutofrange: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 657 | PyErr_SetString(PyExc_ValueError, "seek out of range"); |
| 658 | return NULL; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 659 | } |
| 660 | |
| 661 | static PyObject * |
Fredrik Lundh | 54cf3dc | 2000-07-08 22:05:01 +0000 | [diff] [blame] | 662 | mmap_move_method(mmap_object *self, PyObject *args) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 663 | { |
Benjamin Peterson | cd04db0 | 2016-10-05 21:45:48 -0700 | [diff] [blame] | 664 | Py_ssize_t dest, src, cnt; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 665 | CHECK_VALID(NULL); |
Benjamin Peterson | cd04db0 | 2016-10-05 21:45:48 -0700 | [diff] [blame] | 666 | if (!PyArg_ParseTuple(args, "nnn:move", &dest, &src, &cnt) || |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 667 | !is_writable(self)) { |
| 668 | return NULL; |
| 669 | } else { |
| 670 | /* bounds check the values */ |
Benjamin Peterson | cd04db0 | 2016-10-05 21:45:48 -0700 | [diff] [blame] | 671 | if (dest < 0 || src < 0 || cnt < 0) |
| 672 | goto bounds; |
| 673 | if (self->size - dest < cnt || self->size - src < cnt) |
| 674 | goto bounds; |
| 675 | |
| 676 | memmove(&self->data[dest], &self->data[src], cnt); |
| 677 | |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 678 | Py_RETURN_NONE; |
Benjamin Peterson | cd04db0 | 2016-10-05 21:45:48 -0700 | [diff] [blame] | 679 | |
| 680 | bounds: |
| 681 | PyErr_SetString(PyExc_ValueError, |
| 682 | "source, destination, or count out of range"); |
| 683 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 684 | } |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 685 | } |
| 686 | |
Georg Brandl | 0bccc18 | 2010-08-01 14:50:00 +0000 | [diff] [blame] | 687 | static PyObject * |
Serhiy Storchaka | d4f9cf5 | 2018-11-27 19:34:35 +0200 | [diff] [blame] | 688 | mmap_closed_get(mmap_object *self, void *Py_UNUSED(ignored)) |
Georg Brandl | 0bccc18 | 2010-08-01 14:50:00 +0000 | [diff] [blame] | 689 | { |
| 690 | #ifdef MS_WINDOWS |
| 691 | return PyBool_FromLong(self->map_handle == NULL ? 1 : 0); |
| 692 | #elif defined(UNIX) |
| 693 | return PyBool_FromLong(self->data == NULL ? 1 : 0); |
| 694 | #endif |
| 695 | } |
| 696 | |
| 697 | static PyObject * |
| 698 | mmap__enter__method(mmap_object *self, PyObject *args) |
| 699 | { |
| 700 | CHECK_VALID(NULL); |
| 701 | |
| 702 | Py_INCREF(self); |
| 703 | return (PyObject *)self; |
| 704 | } |
| 705 | |
| 706 | static PyObject * |
| 707 | mmap__exit__method(PyObject *self, PyObject *args) |
| 708 | { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 709 | _Py_IDENTIFIER(close); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 710 | |
Jeroen Demeyer | 762f93f | 2019-07-08 10:19:25 +0200 | [diff] [blame] | 711 | return _PyObject_CallMethodIdNoArgs(self, &PyId_close); |
Georg Brandl | 0bccc18 | 2010-08-01 14:50:00 +0000 | [diff] [blame] | 712 | } |
| 713 | |
Taine Zhao | d8ca235 | 2019-10-17 18:41:35 +0800 | [diff] [blame] | 714 | static PyObject * |
| 715 | mmap__repr__method(PyObject *self) |
| 716 | { |
| 717 | mmap_object *mobj = (mmap_object *)self; |
| 718 | |
| 719 | #ifdef MS_WINDOWS |
| 720 | #define _Py_FORMAT_OFFSET "lld" |
| 721 | if (mobj->map_handle == NULL) |
| 722 | #elif defined(UNIX) |
| 723 | # ifdef HAVE_LARGEFILE_SUPPORT |
| 724 | # define _Py_FORMAT_OFFSET "lld" |
| 725 | # else |
| 726 | # define _Py_FORMAT_OFFSET "ld" |
| 727 | # endif |
| 728 | if (mobj->data == NULL) |
| 729 | #endif |
| 730 | { |
| 731 | return PyUnicode_FromFormat("<%s closed=True>", Py_TYPE(self)->tp_name); |
| 732 | } else { |
| 733 | const char *access_str; |
| 734 | |
| 735 | switch (mobj->access) { |
| 736 | case ACCESS_DEFAULT: |
| 737 | access_str = "ACCESS_DEFAULT"; |
| 738 | break; |
| 739 | case ACCESS_READ: |
| 740 | access_str = "ACCESS_READ"; |
| 741 | break; |
| 742 | case ACCESS_WRITE: |
| 743 | access_str = "ACCESS_WRITE"; |
| 744 | break; |
| 745 | case ACCESS_COPY: |
| 746 | access_str = "ACCESS_COPY"; |
| 747 | break; |
| 748 | default: |
| 749 | Py_UNREACHABLE(); |
| 750 | } |
| 751 | |
| 752 | return PyUnicode_FromFormat("<%s closed=False, access=%s, length=%zd, " |
| 753 | "pos=%zd, offset=%" _Py_FORMAT_OFFSET ">", |
| 754 | Py_TYPE(self)->tp_name, access_str, |
| 755 | mobj->size, mobj->pos, mobj->offset); |
| 756 | } |
| 757 | } |
| 758 | |
Serhiy Storchaka | 76b4765 | 2014-08-19 17:11:20 +0300 | [diff] [blame] | 759 | #ifdef MS_WINDOWS |
| 760 | static PyObject * |
| 761 | mmap__sizeof__method(mmap_object *self, void *unused) |
| 762 | { |
| 763 | Py_ssize_t res; |
| 764 | |
Serhiy Storchaka | 5c4064e | 2015-12-19 20:05:25 +0200 | [diff] [blame] | 765 | res = _PyObject_SIZE(Py_TYPE(self)); |
Serhiy Storchaka | 76b4765 | 2014-08-19 17:11:20 +0300 | [diff] [blame] | 766 | if (self->tagname) |
| 767 | res += strlen(self->tagname) + 1; |
| 768 | return PyLong_FromSsize_t(res); |
| 769 | } |
| 770 | #endif |
| 771 | |
Zackery Spytz | 02db696 | 2019-05-27 10:48:17 -0600 | [diff] [blame] | 772 | #ifdef HAVE_MADVISE |
| 773 | static PyObject * |
| 774 | mmap_madvise_method(mmap_object *self, PyObject *args) |
| 775 | { |
| 776 | int option; |
| 777 | Py_ssize_t start = 0, length; |
| 778 | |
| 779 | CHECK_VALID(NULL); |
| 780 | length = self->size; |
| 781 | |
| 782 | if (!PyArg_ParseTuple(args, "i|nn:madvise", &option, &start, &length)) { |
| 783 | return NULL; |
| 784 | } |
| 785 | |
| 786 | if (start < 0 || start >= self->size) { |
| 787 | PyErr_SetString(PyExc_ValueError, "madvise start out of bounds"); |
| 788 | return NULL; |
| 789 | } |
| 790 | if (length < 0) { |
| 791 | PyErr_SetString(PyExc_ValueError, "madvise length invalid"); |
| 792 | return NULL; |
| 793 | } |
| 794 | if (PY_SSIZE_T_MAX - start < length) { |
| 795 | PyErr_SetString(PyExc_OverflowError, "madvise length too large"); |
| 796 | return NULL; |
| 797 | } |
| 798 | |
| 799 | if (start + length > self->size) { |
| 800 | length = self->size - start; |
| 801 | } |
| 802 | |
| 803 | if (madvise(self->data + start, length, option) != 0) { |
| 804 | PyErr_SetFromErrno(PyExc_OSError); |
| 805 | return NULL; |
| 806 | } |
| 807 | |
| 808 | Py_RETURN_NONE; |
| 809 | } |
| 810 | #endif // HAVE_MADVISE |
| 811 | |
Erlend Egeberg Aasland | 74b4eda | 2020-11-03 10:38:31 +0100 | [diff] [blame] | 812 | static struct PyMemberDef mmap_object_members[] = { |
| 813 | {"__weaklistoffset__", T_PYSSIZET, offsetof(mmap_object, weakreflist), READONLY}, |
| 814 | {NULL}, |
| 815 | }; |
| 816 | |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 817 | static struct PyMethodDef mmap_object_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 818 | {"close", (PyCFunction) mmap_close_method, METH_NOARGS}, |
| 819 | {"find", (PyCFunction) mmap_find_method, METH_VARARGS}, |
| 820 | {"rfind", (PyCFunction) mmap_rfind_method, METH_VARARGS}, |
| 821 | {"flush", (PyCFunction) mmap_flush_method, METH_VARARGS}, |
Zackery Spytz | 02db696 | 2019-05-27 10:48:17 -0600 | [diff] [blame] | 822 | #ifdef HAVE_MADVISE |
| 823 | {"madvise", (PyCFunction) mmap_madvise_method, METH_VARARGS}, |
| 824 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 825 | {"move", (PyCFunction) mmap_move_method, METH_VARARGS}, |
| 826 | {"read", (PyCFunction) mmap_read_method, METH_VARARGS}, |
| 827 | {"read_byte", (PyCFunction) mmap_read_byte_method, METH_NOARGS}, |
| 828 | {"readline", (PyCFunction) mmap_read_line_method, METH_NOARGS}, |
| 829 | {"resize", (PyCFunction) mmap_resize_method, METH_VARARGS}, |
| 830 | {"seek", (PyCFunction) mmap_seek_method, METH_VARARGS}, |
| 831 | {"size", (PyCFunction) mmap_size_method, METH_NOARGS}, |
| 832 | {"tell", (PyCFunction) mmap_tell_method, METH_NOARGS}, |
| 833 | {"write", (PyCFunction) mmap_write_method, METH_VARARGS}, |
| 834 | {"write_byte", (PyCFunction) mmap_write_byte_method, METH_VARARGS}, |
Georg Brandl | 0bccc18 | 2010-08-01 14:50:00 +0000 | [diff] [blame] | 835 | {"__enter__", (PyCFunction) mmap__enter__method, METH_NOARGS}, |
| 836 | {"__exit__", (PyCFunction) mmap__exit__method, METH_VARARGS}, |
Serhiy Storchaka | 76b4765 | 2014-08-19 17:11:20 +0300 | [diff] [blame] | 837 | #ifdef MS_WINDOWS |
| 838 | {"__sizeof__", (PyCFunction) mmap__sizeof__method, METH_NOARGS}, |
| 839 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 840 | {NULL, NULL} /* sentinel */ |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 841 | }; |
| 842 | |
Georg Brandl | 0bccc18 | 2010-08-01 14:50:00 +0000 | [diff] [blame] | 843 | static PyGetSetDef mmap_object_getset[] = { |
| 844 | {"closed", (getter) mmap_closed_get, NULL, NULL}, |
| 845 | {NULL} |
| 846 | }; |
| 847 | |
| 848 | |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 849 | /* Functions for treating an mmap'ed file as a buffer */ |
| 850 | |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 851 | static int |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 852 | mmap_buffer_getbuf(mmap_object *self, Py_buffer *view, int flags) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 853 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 854 | CHECK_VALID(-1); |
| 855 | if (PyBuffer_FillInfo(view, (PyObject*)self, self->data, self->size, |
| 856 | (self->access == ACCESS_READ), flags) < 0) |
| 857 | return -1; |
| 858 | self->exports++; |
| 859 | return 0; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 860 | } |
| 861 | |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 862 | static void |
Travis E. Oliphant | 8ae62b6 | 2007-09-23 02:00:13 +0000 | [diff] [blame] | 863 | mmap_buffer_releasebuf(mmap_object *self, Py_buffer *view) |
Tim Peters | ec0a5f0 | 2006-02-16 23:47:20 +0000 | [diff] [blame] | 864 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 865 | self->exports--; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 866 | } |
| 867 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 868 | static Py_ssize_t |
Fredrik Lundh | 54cf3dc | 2000-07-08 22:05:01 +0000 | [diff] [blame] | 869 | mmap_length(mmap_object *self) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 870 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 871 | CHECK_VALID(-1); |
| 872 | return self->size; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 873 | } |
| 874 | |
| 875 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 876 | mmap_item(mmap_object *self, Py_ssize_t i) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 877 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 878 | CHECK_VALID(NULL); |
Benjamin Peterson | cd04db0 | 2016-10-05 21:45:48 -0700 | [diff] [blame] | 879 | if (i < 0 || i >= self->size) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 880 | PyErr_SetString(PyExc_IndexError, "mmap index out of range"); |
| 881 | return NULL; |
| 882 | } |
| 883 | return PyBytes_FromStringAndSize(self->data + i, 1); |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 884 | } |
| 885 | |
| 886 | static PyObject * |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 887 | mmap_subscript(mmap_object *self, PyObject *item) |
| 888 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 889 | CHECK_VALID(NULL); |
| 890 | if (PyIndex_Check(item)) { |
| 891 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
| 892 | if (i == -1 && PyErr_Occurred()) |
| 893 | return NULL; |
| 894 | if (i < 0) |
| 895 | i += self->size; |
Benjamin Peterson | cd04db0 | 2016-10-05 21:45:48 -0700 | [diff] [blame] | 896 | if (i < 0 || i >= self->size) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 897 | PyErr_SetString(PyExc_IndexError, |
| 898 | "mmap index out of range"); |
| 899 | return NULL; |
| 900 | } |
| 901 | return PyLong_FromLong(Py_CHARMASK(self->data[i])); |
| 902 | } |
| 903 | else if (PySlice_Check(item)) { |
| 904 | Py_ssize_t start, stop, step, slicelen; |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 905 | |
Serhiy Storchaka | b879fe8 | 2017-04-08 09:53:51 +0300 | [diff] [blame] | 906 | if (PySlice_Unpack(item, &start, &stop, &step) < 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 907 | return NULL; |
| 908 | } |
Serhiy Storchaka | b879fe8 | 2017-04-08 09:53:51 +0300 | [diff] [blame] | 909 | slicelen = PySlice_AdjustIndices(self->size, &start, &stop, step); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 910 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 911 | if (slicelen <= 0) |
| 912 | return PyBytes_FromStringAndSize("", 0); |
| 913 | else if (step == 1) |
| 914 | return PyBytes_FromStringAndSize(self->data + start, |
| 915 | slicelen); |
| 916 | else { |
| 917 | char *result_buf = (char *)PyMem_Malloc(slicelen); |
Zackery Spytz | 14514d9 | 2019-05-17 01:13:03 -0600 | [diff] [blame] | 918 | size_t cur; |
| 919 | Py_ssize_t i; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 920 | PyObject *result; |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 921 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 922 | if (result_buf == NULL) |
| 923 | return PyErr_NoMemory(); |
| 924 | for (cur = start, i = 0; i < slicelen; |
| 925 | cur += step, i++) { |
| 926 | result_buf[i] = self->data[cur]; |
| 927 | } |
| 928 | result = PyBytes_FromStringAndSize(result_buf, |
| 929 | slicelen); |
| 930 | PyMem_Free(result_buf); |
| 931 | return result; |
| 932 | } |
| 933 | } |
| 934 | else { |
| 935 | PyErr_SetString(PyExc_TypeError, |
| 936 | "mmap indices must be integers"); |
| 937 | return NULL; |
| 938 | } |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 939 | } |
| 940 | |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 941 | static int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 942 | mmap_ass_item(mmap_object *self, Py_ssize_t i, PyObject *v) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 943 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 944 | const char *buf; |
Tim Peters | ec0a5f0 | 2006-02-16 23:47:20 +0000 | [diff] [blame] | 945 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 946 | CHECK_VALID(-1); |
Benjamin Peterson | cd04db0 | 2016-10-05 21:45:48 -0700 | [diff] [blame] | 947 | if (i < 0 || i >= self->size) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 948 | PyErr_SetString(PyExc_IndexError, "mmap index out of range"); |
| 949 | return -1; |
| 950 | } |
| 951 | if (v == NULL) { |
| 952 | PyErr_SetString(PyExc_TypeError, |
| 953 | "mmap object doesn't support item deletion"); |
| 954 | return -1; |
| 955 | } |
| 956 | if (! (PyBytes_Check(v) && PyBytes_Size(v)==1) ) { |
| 957 | PyErr_SetString(PyExc_IndexError, |
| 958 | "mmap assignment must be length-1 bytes()"); |
| 959 | return -1; |
| 960 | } |
| 961 | if (!is_writable(self)) |
| 962 | return -1; |
| 963 | buf = PyBytes_AsString(v); |
| 964 | self->data[i] = buf[0]; |
| 965 | return 0; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 966 | } |
| 967 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 968 | static int |
| 969 | mmap_ass_subscript(mmap_object *self, PyObject *item, PyObject *value) |
| 970 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 971 | CHECK_VALID(-1); |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 972 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 973 | if (!is_writable(self)) |
| 974 | return -1; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 975 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 976 | if (PyIndex_Check(item)) { |
| 977 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
| 978 | Py_ssize_t v; |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 979 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 980 | if (i == -1 && PyErr_Occurred()) |
| 981 | return -1; |
| 982 | if (i < 0) |
| 983 | i += self->size; |
Benjamin Peterson | cd04db0 | 2016-10-05 21:45:48 -0700 | [diff] [blame] | 984 | if (i < 0 || i >= self->size) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 985 | PyErr_SetString(PyExc_IndexError, |
| 986 | "mmap index out of range"); |
| 987 | return -1; |
| 988 | } |
| 989 | if (value == NULL) { |
| 990 | PyErr_SetString(PyExc_TypeError, |
| 991 | "mmap doesn't support item deletion"); |
| 992 | return -1; |
| 993 | } |
| 994 | if (!PyIndex_Check(value)) { |
| 995 | PyErr_SetString(PyExc_TypeError, |
| 996 | "mmap item value must be an int"); |
| 997 | return -1; |
| 998 | } |
| 999 | v = PyNumber_AsSsize_t(value, PyExc_TypeError); |
| 1000 | if (v == -1 && PyErr_Occurred()) |
| 1001 | return -1; |
| 1002 | if (v < 0 || v > 255) { |
| 1003 | PyErr_SetString(PyExc_ValueError, |
| 1004 | "mmap item value must be " |
| 1005 | "in range(0, 256)"); |
| 1006 | return -1; |
| 1007 | } |
Antoine Pitrou | 22e4155 | 2010-08-15 18:07:50 +0000 | [diff] [blame] | 1008 | self->data[i] = (char) v; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1009 | return 0; |
| 1010 | } |
| 1011 | else if (PySlice_Check(item)) { |
| 1012 | Py_ssize_t start, stop, step, slicelen; |
| 1013 | Py_buffer vbuf; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1014 | |
Serhiy Storchaka | b879fe8 | 2017-04-08 09:53:51 +0300 | [diff] [blame] | 1015 | if (PySlice_Unpack(item, &start, &stop, &step) < 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1016 | return -1; |
| 1017 | } |
Serhiy Storchaka | b879fe8 | 2017-04-08 09:53:51 +0300 | [diff] [blame] | 1018 | slicelen = PySlice_AdjustIndices(self->size, &start, &stop, step); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1019 | if (value == NULL) { |
| 1020 | PyErr_SetString(PyExc_TypeError, |
| 1021 | "mmap object doesn't support slice deletion"); |
| 1022 | return -1; |
| 1023 | } |
| 1024 | if (PyObject_GetBuffer(value, &vbuf, PyBUF_SIMPLE) < 0) |
| 1025 | return -1; |
| 1026 | if (vbuf.len != slicelen) { |
| 1027 | PyErr_SetString(PyExc_IndexError, |
| 1028 | "mmap slice assignment is wrong size"); |
| 1029 | PyBuffer_Release(&vbuf); |
| 1030 | return -1; |
| 1031 | } |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1032 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1033 | if (slicelen == 0) { |
| 1034 | } |
| 1035 | else if (step == 1) { |
| 1036 | memcpy(self->data + start, vbuf.buf, slicelen); |
| 1037 | } |
| 1038 | else { |
Zackery Spytz | 14514d9 | 2019-05-17 01:13:03 -0600 | [diff] [blame] | 1039 | size_t cur; |
| 1040 | Py_ssize_t i; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1041 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1042 | for (cur = start, i = 0; |
| 1043 | i < slicelen; |
| 1044 | cur += step, i++) |
| 1045 | { |
| 1046 | self->data[cur] = ((char *)vbuf.buf)[i]; |
| 1047 | } |
| 1048 | } |
| 1049 | PyBuffer_Release(&vbuf); |
| 1050 | return 0; |
| 1051 | } |
| 1052 | else { |
| 1053 | PyErr_SetString(PyExc_TypeError, |
| 1054 | "mmap indices must be integer"); |
| 1055 | return -1; |
| 1056 | } |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1057 | } |
| 1058 | |
Georg Brandl | 86def6c | 2008-01-21 20:36:10 +0000 | [diff] [blame] | 1059 | static PyObject * |
| 1060 | new_mmap_object(PyTypeObject *type, PyObject *args, PyObject *kwdict); |
| 1061 | |
Christian Heimes | e1c9811 | 2008-01-21 11:20:28 +0000 | [diff] [blame] | 1062 | PyDoc_STRVAR(mmap_doc, |
| 1063 | "Windows: mmap(fileno, length[, tagname[, access[, offset]]])\n\ |
| 1064 | \n\ |
| 1065 | Maps length bytes from the file specified by the file handle fileno,\n\ |
| 1066 | and returns a mmap object. If length is larger than the current size\n\ |
| 1067 | of the file, the file is extended to contain length bytes. If length\n\ |
| 1068 | is 0, the maximum length of the map is the current size of the file,\n\ |
| 1069 | except that if the file is empty Windows raises an exception (you cannot\n\ |
| 1070 | create an empty mapping on Windows).\n\ |
| 1071 | \n\ |
| 1072 | Unix: mmap(fileno, length[, flags[, prot[, access[, offset]]]])\n\ |
| 1073 | \n\ |
| 1074 | Maps length bytes from the file specified by the file descriptor fileno,\n\ |
| 1075 | and returns a mmap object. If length is 0, the maximum length of the map\n\ |
| 1076 | will be the current size of the file when mmap is called.\n\ |
| 1077 | flags specifies the nature of the mapping. MAP_PRIVATE creates a\n\ |
| 1078 | private copy-on-write mapping, so changes to the contents of the mmap\n\ |
Benjamin Peterson | c4fe6f3 | 2008-08-19 18:57:56 +0000 | [diff] [blame] | 1079 | object will be private to this process, and MAP_SHARED creates a mapping\n\ |
Christian Heimes | e1c9811 | 2008-01-21 11:20:28 +0000 | [diff] [blame] | 1080 | that's shared with all other processes mapping the same areas of the file.\n\ |
| 1081 | The default value is MAP_SHARED.\n\ |
| 1082 | \n\ |
| 1083 | To map anonymous memory, pass -1 as the fileno (both versions)."); |
| 1084 | |
| 1085 | |
Erlend Egeberg Aasland | 74b4eda | 2020-11-03 10:38:31 +0100 | [diff] [blame] | 1086 | static PyType_Slot mmap_object_slots[] = { |
| 1087 | {Py_tp_new, new_mmap_object}, |
| 1088 | {Py_tp_alloc, PyType_GenericAlloc}, |
| 1089 | {Py_tp_dealloc, mmap_object_dealloc}, |
| 1090 | {Py_tp_free, PyObject_Del}, |
| 1091 | {Py_tp_repr, mmap__repr__method}, |
| 1092 | {Py_tp_doc, (void *)mmap_doc}, |
| 1093 | {Py_tp_methods, mmap_object_methods}, |
| 1094 | {Py_tp_members, mmap_object_members}, |
| 1095 | {Py_tp_getset, mmap_object_getset}, |
| 1096 | {Py_tp_getattro, PyObject_GenericGetAttr}, |
| 1097 | |
| 1098 | /* as sequence */ |
| 1099 | {Py_sq_length, mmap_length}, |
| 1100 | {Py_sq_item, mmap_item}, |
| 1101 | {Py_sq_ass_item, mmap_ass_item}, |
| 1102 | |
| 1103 | /* as mapping */ |
| 1104 | {Py_mp_length, mmap_length}, |
| 1105 | {Py_mp_subscript, mmap_subscript}, |
| 1106 | {Py_mp_ass_subscript, mmap_ass_subscript}, |
| 1107 | |
| 1108 | /* as buffer */ |
| 1109 | {Py_bf_getbuffer, mmap_buffer_getbuf}, |
| 1110 | {Py_bf_releasebuffer, mmap_buffer_releasebuf}, |
| 1111 | {0, NULL}, |
| 1112 | }; |
| 1113 | |
| 1114 | static PyType_Spec mmap_object_spec = { |
| 1115 | .name = "mmap.mmap", |
| 1116 | .basicsize = sizeof(mmap_object), |
| 1117 | .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, |
| 1118 | .slots = mmap_object_slots, |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 1119 | }; |
| 1120 | |
Andrew M. Kuchling | 70d2742 | 2000-06-18 04:45:14 +0000 | [diff] [blame] | 1121 | |
Tim Peters | ec0a5f0 | 2006-02-16 23:47:20 +0000 | [diff] [blame] | 1122 | #ifdef UNIX |
Antoine Pitrou | 97696cb | 2011-02-21 23:46:27 +0000 | [diff] [blame] | 1123 | #ifdef HAVE_LARGEFILE_SUPPORT |
| 1124 | #define _Py_PARSE_OFF_T "L" |
| 1125 | #else |
| 1126 | #define _Py_PARSE_OFF_T "l" |
| 1127 | #endif |
| 1128 | |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 1129 | static PyObject * |
Georg Brandl | 86def6c | 2008-01-21 20:36:10 +0000 | [diff] [blame] | 1130 | new_mmap_object(PyTypeObject *type, PyObject *args, PyObject *kwdict) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 1131 | { |
Victor Stinner | e134a7f | 2015-03-30 10:09:31 +0200 | [diff] [blame] | 1132 | struct _Py_stat_struct status; |
Zackery Spytz | d6e1404 | 2018-03-14 14:08:01 -0600 | [diff] [blame] | 1133 | int fstat_result = -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1134 | mmap_object *m_obj; |
Antoine Pitrou | 97696cb | 2011-02-21 23:46:27 +0000 | [diff] [blame] | 1135 | Py_ssize_t map_size; |
| 1136 | off_t offset = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1137 | int fd, flags = MAP_SHARED, prot = PROT_WRITE | PROT_READ; |
| 1138 | int devzero = -1; |
| 1139 | int access = (int)ACCESS_DEFAULT; |
| 1140 | static char *keywords[] = {"fileno", "length", |
Stefan Krah | 2318699 | 2012-03-06 15:37:36 +0100 | [diff] [blame] | 1141 | "flags", "prot", |
| 1142 | "access", "offset", NULL}; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 1143 | |
Benjamin Peterson | cd04db0 | 2016-10-05 21:45:48 -0700 | [diff] [blame] | 1144 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "in|iii" _Py_PARSE_OFF_T, keywords, |
| 1145 | &fd, &map_size, &flags, &prot, |
Antoine Pitrou | 97696cb | 2011-02-21 23:46:27 +0000 | [diff] [blame] | 1146 | &access, &offset)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1147 | return NULL; |
Benjamin Peterson | cd04db0 | 2016-10-05 21:45:48 -0700 | [diff] [blame] | 1148 | if (map_size < 0) { |
| 1149 | PyErr_SetString(PyExc_OverflowError, |
Zackery Spytz | 9308dea | 2018-03-21 00:02:37 -0600 | [diff] [blame] | 1150 | "memory mapped length must be positive"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1151 | return NULL; |
Benjamin Peterson | cd04db0 | 2016-10-05 21:45:48 -0700 | [diff] [blame] | 1152 | } |
Antoine Pitrou | 97696cb | 2011-02-21 23:46:27 +0000 | [diff] [blame] | 1153 | if (offset < 0) { |
| 1154 | PyErr_SetString(PyExc_OverflowError, |
| 1155 | "memory mapped offset must be positive"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1156 | return NULL; |
Antoine Pitrou | 97696cb | 2011-02-21 23:46:27 +0000 | [diff] [blame] | 1157 | } |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 1158 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1159 | if ((access != (int)ACCESS_DEFAULT) && |
| 1160 | ((flags != MAP_SHARED) || (prot != (PROT_WRITE | PROT_READ)))) |
| 1161 | return PyErr_Format(PyExc_ValueError, |
| 1162 | "mmap can't specify both access and flags, prot."); |
| 1163 | switch ((access_mode)access) { |
| 1164 | case ACCESS_READ: |
| 1165 | flags = MAP_SHARED; |
| 1166 | prot = PROT_READ; |
| 1167 | break; |
| 1168 | case ACCESS_WRITE: |
| 1169 | flags = MAP_SHARED; |
| 1170 | prot = PROT_READ | PROT_WRITE; |
| 1171 | break; |
| 1172 | case ACCESS_COPY: |
| 1173 | flags = MAP_PRIVATE; |
| 1174 | prot = PROT_READ | PROT_WRITE; |
| 1175 | break; |
| 1176 | case ACCESS_DEFAULT: |
Antoine Pitrou | 16a0a0b | 2011-03-06 01:11:03 +0100 | [diff] [blame] | 1177 | /* map prot to access type */ |
| 1178 | if ((prot & PROT_READ) && (prot & PROT_WRITE)) { |
| 1179 | /* ACCESS_DEFAULT */ |
| 1180 | } |
| 1181 | else if (prot & PROT_WRITE) { |
| 1182 | access = ACCESS_WRITE; |
| 1183 | } |
| 1184 | else { |
| 1185 | access = ACCESS_READ; |
| 1186 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1187 | break; |
| 1188 | default: |
| 1189 | return PyErr_Format(PyExc_ValueError, |
| 1190 | "mmap invalid access parameter."); |
| 1191 | } |
Neal Norwitz | b567392 | 2002-09-05 21:48:07 +0000 | [diff] [blame] | 1192 | |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 1193 | if (PySys_Audit("mmap.__new__", "ini" _Py_PARSE_OFF_T, |
Zackery Spytz | 08286d5 | 2019-06-21 09:31:59 -0600 | [diff] [blame] | 1194 | fd, map_size, access, offset) < 0) { |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 1195 | return NULL; |
| 1196 | } |
| 1197 | |
Victor Stinner | a6cd0cf | 2011-05-02 01:05:37 +0200 | [diff] [blame] | 1198 | #ifdef __APPLE__ |
| 1199 | /* Issue #11277: fsync(2) is not enough on OS X - a special, OS X specific |
| 1200 | fcntl(2) is necessary to force DISKSYNC and get around mmap(2) bug */ |
| 1201 | if (fd != -1) |
| 1202 | (void)fcntl(fd, F_FULLFSYNC); |
| 1203 | #endif |
Nir Soffer | 4484f9d | 2018-03-12 01:39:22 +0200 | [diff] [blame] | 1204 | |
| 1205 | if (fd != -1) { |
| 1206 | Py_BEGIN_ALLOW_THREADS |
| 1207 | fstat_result = _Py_fstat_noraise(fd, &status); |
| 1208 | Py_END_ALLOW_THREADS |
| 1209 | } |
| 1210 | |
| 1211 | if (fd != -1 && fstat_result == 0 && S_ISREG(status.st_mode)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1212 | if (map_size == 0) { |
Victor Stinner | e134a7f | 2015-03-30 10:09:31 +0200 | [diff] [blame] | 1213 | if (status.st_size == 0) { |
Jesus Cea | 941bfcc | 2012-09-10 00:27:55 +0200 | [diff] [blame] | 1214 | PyErr_SetString(PyExc_ValueError, |
| 1215 | "cannot mmap an empty file"); |
| 1216 | return NULL; |
| 1217 | } |
Victor Stinner | e134a7f | 2015-03-30 10:09:31 +0200 | [diff] [blame] | 1218 | if (offset >= status.st_size) { |
Antoine Pitrou | 305bc9e | 2011-01-20 21:07:24 +0000 | [diff] [blame] | 1219 | PyErr_SetString(PyExc_ValueError, |
| 1220 | "mmap offset is greater than file size"); |
| 1221 | return NULL; |
| 1222 | } |
Victor Stinner | e134a7f | 2015-03-30 10:09:31 +0200 | [diff] [blame] | 1223 | if (status.st_size - offset > PY_SSIZE_T_MAX) { |
Antoine Pitrou | 97696cb | 2011-02-21 23:46:27 +0000 | [diff] [blame] | 1224 | PyErr_SetString(PyExc_ValueError, |
| 1225 | "mmap length is too large"); |
Richard Oudkerk | 0d09ba8 | 2013-02-13 12:18:03 +0000 | [diff] [blame] | 1226 | return NULL; |
| 1227 | } |
Victor Stinner | e134a7f | 2015-03-30 10:09:31 +0200 | [diff] [blame] | 1228 | map_size = (Py_ssize_t) (status.st_size - offset); |
Benjamin Peterson | cd04db0 | 2016-10-05 21:45:48 -0700 | [diff] [blame] | 1229 | } else if (offset > status.st_size || status.st_size - offset < map_size) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1230 | PyErr_SetString(PyExc_ValueError, |
| 1231 | "mmap length is greater than file size"); |
| 1232 | return NULL; |
| 1233 | } |
| 1234 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1235 | m_obj = (mmap_object *)type->tp_alloc(type, 0); |
| 1236 | if (m_obj == NULL) {return NULL;} |
| 1237 | m_obj->data = NULL; |
Benjamin Peterson | cd04db0 | 2016-10-05 21:45:48 -0700 | [diff] [blame] | 1238 | m_obj->size = map_size; |
| 1239 | m_obj->pos = 0; |
Antoine Pitrou | c53204b | 2013-08-05 23:17:30 +0200 | [diff] [blame] | 1240 | m_obj->weakreflist = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1241 | m_obj->exports = 0; |
| 1242 | m_obj->offset = offset; |
| 1243 | if (fd == -1) { |
| 1244 | m_obj->fd = -1; |
| 1245 | /* Assume the caller wants to map anonymous memory. |
| 1246 | This is the same behaviour as Windows. mmap.mmap(-1, size) |
| 1247 | on both Windows and Unix map anonymous memory. |
| 1248 | */ |
Neal Norwitz | 0e6bc8c | 2006-02-05 05:45:43 +0000 | [diff] [blame] | 1249 | #ifdef MAP_ANONYMOUS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1250 | /* BSD way to map anonymous memory */ |
| 1251 | flags |= MAP_ANONYMOUS; |
Lihua Zhao | 4fb1502 | 2019-05-21 18:50:14 +0800 | [diff] [blame] | 1252 | |
| 1253 | /* VxWorks only supports MAP_ANONYMOUS with MAP_PRIVATE flag */ |
| 1254 | #ifdef __VXWORKS__ |
| 1255 | flags &= ~MAP_SHARED; |
| 1256 | flags |= MAP_PRIVATE; |
| 1257 | #endif |
| 1258 | |
Neal Norwitz | 0e6bc8c | 2006-02-05 05:45:43 +0000 | [diff] [blame] | 1259 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1260 | /* SVR4 method to map anonymous memory is to open /dev/zero */ |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 1261 | fd = devzero = _Py_open("/dev/zero", O_RDWR); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1262 | if (devzero == -1) { |
| 1263 | Py_DECREF(m_obj); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1264 | return NULL; |
| 1265 | } |
Neal Norwitz | 0e6bc8c | 2006-02-05 05:45:43 +0000 | [diff] [blame] | 1266 | #endif |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 1267 | } |
| 1268 | else { |
| 1269 | m_obj->fd = _Py_dup(fd); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1270 | if (m_obj->fd == -1) { |
| 1271 | Py_DECREF(m_obj); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1272 | return NULL; |
| 1273 | } |
| 1274 | } |
Neal Norwitz | 0e6bc8c | 2006-02-05 05:45:43 +0000 | [diff] [blame] | 1275 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1276 | m_obj->data = mmap(NULL, map_size, |
| 1277 | prot, flags, |
| 1278 | fd, offset); |
Neal Norwitz | 0e6bc8c | 2006-02-05 05:45:43 +0000 | [diff] [blame] | 1279 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1280 | if (devzero != -1) { |
| 1281 | close(devzero); |
| 1282 | } |
| 1283 | |
| 1284 | if (m_obj->data == (char *)-1) { |
| 1285 | m_obj->data = NULL; |
| 1286 | Py_DECREF(m_obj); |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1287 | PyErr_SetFromErrno(PyExc_OSError); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1288 | return NULL; |
| 1289 | } |
| 1290 | m_obj->access = (access_mode)access; |
| 1291 | return (PyObject *)m_obj; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 1292 | } |
| 1293 | #endif /* UNIX */ |
| 1294 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 1295 | #ifdef MS_WINDOWS |
Antoine Pitrou | 97696cb | 2011-02-21 23:46:27 +0000 | [diff] [blame] | 1296 | |
| 1297 | /* A note on sizes and offsets: while the actual map size must hold in a |
| 1298 | Py_ssize_t, both the total file size and the start offset can be longer |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1299 | than a Py_ssize_t, so we use long long which is always 64-bit. |
Antoine Pitrou | 97696cb | 2011-02-21 23:46:27 +0000 | [diff] [blame] | 1300 | */ |
| 1301 | |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 1302 | static PyObject * |
Georg Brandl | 86def6c | 2008-01-21 20:36:10 +0000 | [diff] [blame] | 1303 | new_mmap_object(PyTypeObject *type, PyObject *args, PyObject *kwdict) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 1304 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1305 | mmap_object *m_obj; |
Antoine Pitrou | 97696cb | 2011-02-21 23:46:27 +0000 | [diff] [blame] | 1306 | Py_ssize_t map_size; |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1307 | long long offset = 0, size; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1308 | DWORD off_hi; /* upper 32 bits of offset */ |
| 1309 | DWORD off_lo; /* lower 32 bits of offset */ |
| 1310 | DWORD size_hi; /* upper 32 bits of size */ |
| 1311 | DWORD size_lo; /* lower 32 bits of size */ |
Serhiy Storchaka | e2f92de | 2017-11-11 13:06:26 +0200 | [diff] [blame] | 1312 | const char *tagname = ""; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1313 | DWORD dwErr = 0; |
| 1314 | int fileno; |
| 1315 | HANDLE fh = 0; |
| 1316 | int access = (access_mode)ACCESS_DEFAULT; |
| 1317 | DWORD flProtect, dwDesiredAccess; |
| 1318 | static char *keywords[] = { "fileno", "length", |
Stefan Krah | 2318699 | 2012-03-06 15:37:36 +0100 | [diff] [blame] | 1319 | "tagname", |
| 1320 | "access", "offset", NULL }; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 1321 | |
Benjamin Peterson | cd04db0 | 2016-10-05 21:45:48 -0700 | [diff] [blame] | 1322 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "in|ziL", keywords, |
| 1323 | &fileno, &map_size, |
Antoine Pitrou | 97696cb | 2011-02-21 23:46:27 +0000 | [diff] [blame] | 1324 | &tagname, &access, &offset)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1325 | return NULL; |
| 1326 | } |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 1327 | |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 1328 | if (PySys_Audit("mmap.__new__", "iniL", |
| 1329 | fileno, map_size, access, offset) < 0) { |
| 1330 | return NULL; |
| 1331 | } |
| 1332 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1333 | switch((access_mode)access) { |
| 1334 | case ACCESS_READ: |
| 1335 | flProtect = PAGE_READONLY; |
| 1336 | dwDesiredAccess = FILE_MAP_READ; |
| 1337 | break; |
| 1338 | case ACCESS_DEFAULT: case ACCESS_WRITE: |
| 1339 | flProtect = PAGE_READWRITE; |
| 1340 | dwDesiredAccess = FILE_MAP_WRITE; |
| 1341 | break; |
| 1342 | case ACCESS_COPY: |
| 1343 | flProtect = PAGE_WRITECOPY; |
| 1344 | dwDesiredAccess = FILE_MAP_COPY; |
| 1345 | break; |
| 1346 | default: |
| 1347 | return PyErr_Format(PyExc_ValueError, |
| 1348 | "mmap invalid access parameter."); |
| 1349 | } |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 1350 | |
Benjamin Peterson | cd04db0 | 2016-10-05 21:45:48 -0700 | [diff] [blame] | 1351 | if (map_size < 0) { |
| 1352 | PyErr_SetString(PyExc_OverflowError, |
Zackery Spytz | 9308dea | 2018-03-21 00:02:37 -0600 | [diff] [blame] | 1353 | "memory mapped length must be positive"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1354 | return NULL; |
Benjamin Peterson | cd04db0 | 2016-10-05 21:45:48 -0700 | [diff] [blame] | 1355 | } |
Antoine Pitrou | 97696cb | 2011-02-21 23:46:27 +0000 | [diff] [blame] | 1356 | if (offset < 0) { |
| 1357 | PyErr_SetString(PyExc_OverflowError, |
| 1358 | "memory mapped offset must be positive"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1359 | return NULL; |
Antoine Pitrou | 97696cb | 2011-02-21 23:46:27 +0000 | [diff] [blame] | 1360 | } |
Tim Peters | ec0a5f0 | 2006-02-16 23:47:20 +0000 | [diff] [blame] | 1361 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1362 | /* assume -1 and 0 both mean invalid filedescriptor |
| 1363 | to 'anonymously' map memory. |
| 1364 | XXX: fileno == 0 is a valid fd, but was accepted prior to 2.5. |
| 1365 | XXX: Should this code be added? |
| 1366 | if (fileno == 0) |
| 1367 | PyErr_WarnEx(PyExc_DeprecationWarning, |
| 1368 | "don't use 0 for anonymous memory", |
| 1369 | 1); |
| 1370 | */ |
| 1371 | if (fileno != -1 && fileno != 0) { |
Brian Curtin | ea47eaa | 2010-08-01 15:26:26 +0000 | [diff] [blame] | 1372 | /* Ensure that fileno is within the CRT's valid range */ |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 1373 | _Py_BEGIN_SUPPRESS_IPH |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1374 | fh = (HANDLE)_get_osfhandle(fileno); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 1375 | _Py_END_SUPPRESS_IPH |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1376 | if (fh==(HANDLE)-1) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1377 | PyErr_SetFromErrno(PyExc_OSError); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1378 | return NULL; |
| 1379 | } |
| 1380 | /* Win9x appears to need us seeked to zero */ |
| 1381 | lseek(fileno, 0, SEEK_SET); |
| 1382 | } |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 1383 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1384 | m_obj = (mmap_object *)type->tp_alloc(type, 0); |
| 1385 | if (m_obj == NULL) |
| 1386 | return NULL; |
| 1387 | /* Set every field to an invalid marker, so we can safely |
| 1388 | destruct the object in the face of failure */ |
| 1389 | m_obj->data = NULL; |
| 1390 | m_obj->file_handle = INVALID_HANDLE_VALUE; |
| 1391 | m_obj->map_handle = NULL; |
| 1392 | m_obj->tagname = NULL; |
| 1393 | m_obj->offset = offset; |
Mark Hammond | 2cbed00 | 2000-07-30 02:22:43 +0000 | [diff] [blame] | 1394 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1395 | if (fh) { |
| 1396 | /* It is necessary to duplicate the handle, so the |
| 1397 | Python code can close it on us */ |
| 1398 | if (!DuplicateHandle( |
| 1399 | GetCurrentProcess(), /* source process handle */ |
| 1400 | fh, /* handle to be duplicated */ |
| 1401 | GetCurrentProcess(), /* target proc handle */ |
| 1402 | (LPHANDLE)&m_obj->file_handle, /* result */ |
| 1403 | 0, /* access - ignored due to options value */ |
| 1404 | FALSE, /* inherited by child processes? */ |
| 1405 | DUPLICATE_SAME_ACCESS)) { /* options */ |
| 1406 | dwErr = GetLastError(); |
| 1407 | Py_DECREF(m_obj); |
| 1408 | PyErr_SetFromWindowsErr(dwErr); |
| 1409 | return NULL; |
| 1410 | } |
| 1411 | if (!map_size) { |
| 1412 | DWORD low,high; |
| 1413 | low = GetFileSize(fh, &high); |
| 1414 | /* low might just happen to have the value INVALID_FILE_SIZE; |
| 1415 | so we need to check the last error also. */ |
| 1416 | if (low == INVALID_FILE_SIZE && |
| 1417 | (dwErr = GetLastError()) != NO_ERROR) { |
| 1418 | Py_DECREF(m_obj); |
| 1419 | return PyErr_SetFromWindowsErr(dwErr); |
| 1420 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1421 | |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1422 | size = (((long long) high) << 32) + low; |
Jesus Cea | 1f2799b | 2012-09-10 22:49:50 +0200 | [diff] [blame] | 1423 | if (size == 0) { |
| 1424 | PyErr_SetString(PyExc_ValueError, |
| 1425 | "cannot mmap an empty file"); |
Jesus Cea | e8db356 | 2012-09-10 22:58:07 +0200 | [diff] [blame] | 1426 | Py_DECREF(m_obj); |
Jesus Cea | 1f2799b | 2012-09-10 22:49:50 +0200 | [diff] [blame] | 1427 | return NULL; |
| 1428 | } |
Antoine Pitrou | 97696cb | 2011-02-21 23:46:27 +0000 | [diff] [blame] | 1429 | if (offset >= size) { |
Antoine Pitrou | 305bc9e | 2011-01-20 21:07:24 +0000 | [diff] [blame] | 1430 | PyErr_SetString(PyExc_ValueError, |
| 1431 | "mmap offset is greater than file size"); |
| 1432 | Py_DECREF(m_obj); |
| 1433 | return NULL; |
| 1434 | } |
Richard Oudkerk | 0d09ba8 | 2013-02-13 12:18:03 +0000 | [diff] [blame] | 1435 | if (size - offset > PY_SSIZE_T_MAX) { |
| 1436 | PyErr_SetString(PyExc_ValueError, |
| 1437 | "mmap length is too large"); |
| 1438 | Py_DECREF(m_obj); |
| 1439 | return NULL; |
| 1440 | } |
| 1441 | m_obj->size = (Py_ssize_t) (size - offset); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1442 | } else { |
| 1443 | m_obj->size = map_size; |
Antoine Pitrou | 97696cb | 2011-02-21 23:46:27 +0000 | [diff] [blame] | 1444 | size = offset + map_size; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1445 | } |
| 1446 | } |
| 1447 | else { |
| 1448 | m_obj->size = map_size; |
Antoine Pitrou | 97696cb | 2011-02-21 23:46:27 +0000 | [diff] [blame] | 1449 | size = offset + map_size; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1450 | } |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 1451 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1452 | /* set the initial position */ |
| 1453 | m_obj->pos = (size_t) 0; |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 1454 | |
Antoine Pitrou | c53204b | 2013-08-05 23:17:30 +0200 | [diff] [blame] | 1455 | m_obj->weakreflist = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1456 | m_obj->exports = 0; |
| 1457 | /* set the tag name */ |
| 1458 | if (tagname != NULL && *tagname != '\0') { |
| 1459 | m_obj->tagname = PyMem_Malloc(strlen(tagname)+1); |
| 1460 | if (m_obj->tagname == NULL) { |
| 1461 | PyErr_NoMemory(); |
| 1462 | Py_DECREF(m_obj); |
| 1463 | return NULL; |
| 1464 | } |
| 1465 | strcpy(m_obj->tagname, tagname); |
| 1466 | } |
| 1467 | else |
| 1468 | m_obj->tagname = NULL; |
Mark Hammond | 2cbed00 | 2000-07-30 02:22:43 +0000 | [diff] [blame] | 1469 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1470 | m_obj->access = (access_mode)access; |
Antoine Pitrou | 97696cb | 2011-02-21 23:46:27 +0000 | [diff] [blame] | 1471 | size_hi = (DWORD)(size >> 32); |
| 1472 | size_lo = (DWORD)(size & 0xFFFFFFFF); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1473 | off_hi = (DWORD)(offset >> 32); |
| 1474 | off_lo = (DWORD)(offset & 0xFFFFFFFF); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1475 | /* For files, it would be sufficient to pass 0 as size. |
| 1476 | For anonymous maps, we have to pass the size explicitly. */ |
| 1477 | m_obj->map_handle = CreateFileMapping(m_obj->file_handle, |
| 1478 | NULL, |
| 1479 | flProtect, |
| 1480 | size_hi, |
| 1481 | size_lo, |
| 1482 | m_obj->tagname); |
| 1483 | if (m_obj->map_handle != NULL) { |
| 1484 | m_obj->data = (char *) MapViewOfFile(m_obj->map_handle, |
| 1485 | dwDesiredAccess, |
| 1486 | off_hi, |
| 1487 | off_lo, |
| 1488 | m_obj->size); |
| 1489 | if (m_obj->data != NULL) |
| 1490 | return (PyObject *)m_obj; |
| 1491 | else { |
| 1492 | dwErr = GetLastError(); |
| 1493 | CloseHandle(m_obj->map_handle); |
| 1494 | m_obj->map_handle = NULL; |
| 1495 | } |
| 1496 | } else |
| 1497 | dwErr = GetLastError(); |
| 1498 | Py_DECREF(m_obj); |
| 1499 | PyErr_SetFromWindowsErr(dwErr); |
| 1500 | return NULL; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 1501 | } |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 1502 | #endif /* MS_WINDOWS */ |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 1503 | |
Dong-hee Na | 3ad52e3 | 2020-06-06 00:01:02 +0900 | [diff] [blame] | 1504 | static int |
Erlend Egeberg Aasland | 74b4eda | 2020-11-03 10:38:31 +0100 | [diff] [blame] | 1505 | mmap_traverse(PyObject *module, visitproc visit, void *arg) |
| 1506 | { |
| 1507 | mmap_state *state = get_mmap_state(module); |
| 1508 | Py_VISIT(state->mmap_object_type); |
| 1509 | return 0; |
| 1510 | } |
| 1511 | |
| 1512 | static int |
| 1513 | mmap_clear(PyObject *module) |
| 1514 | { |
| 1515 | mmap_state *state = get_mmap_state(module); |
| 1516 | Py_CLEAR(state->mmap_object_type); |
| 1517 | return 0; |
| 1518 | } |
| 1519 | |
| 1520 | static void |
| 1521 | mmap_free(void *module) |
| 1522 | { |
| 1523 | mmap_clear((PyObject *)module); |
| 1524 | } |
| 1525 | |
| 1526 | static int |
Dong-hee Na | 3ad52e3 | 2020-06-06 00:01:02 +0900 | [diff] [blame] | 1527 | mmap_exec(PyObject *module) |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 1528 | { |
Erlend Egeberg Aasland | 74b4eda | 2020-11-03 10:38:31 +0100 | [diff] [blame] | 1529 | mmap_state *state = get_mmap_state(module); |
Dong-hee Na | 3ad52e3 | 2020-06-06 00:01:02 +0900 | [diff] [blame] | 1530 | |
| 1531 | Py_INCREF(PyExc_OSError); |
| 1532 | if (PyModule_AddObject(module, "error", PyExc_OSError) < 0) { |
| 1533 | Py_DECREF(PyExc_OSError); |
| 1534 | return -1; |
| 1535 | } |
Erlend Egeberg Aasland | 74b4eda | 2020-11-03 10:38:31 +0100 | [diff] [blame] | 1536 | |
| 1537 | state->mmap_object_type = (PyTypeObject *)PyType_FromModuleAndSpec(module, |
| 1538 | &mmap_object_spec, |
| 1539 | NULL); |
| 1540 | if (state->mmap_object_type == NULL) { |
| 1541 | return -1; |
| 1542 | } |
| 1543 | if (PyModule_AddType(module, state->mmap_object_type) < 0) { |
Dong-hee Na | 3ad52e3 | 2020-06-06 00:01:02 +0900 | [diff] [blame] | 1544 | return -1; |
| 1545 | } |
| 1546 | |
| 1547 | #define ADD_INT_MACRO(module, constant) \ |
| 1548 | do { \ |
| 1549 | if (PyModule_AddIntConstant(module, #constant, constant) < 0) { \ |
| 1550 | return -1; \ |
| 1551 | } \ |
| 1552 | } while (0) |
| 1553 | |
| 1554 | #ifdef PROT_EXEC |
| 1555 | ADD_INT_MACRO(module, PROT_EXEC); |
| 1556 | #endif |
| 1557 | #ifdef PROT_READ |
| 1558 | ADD_INT_MACRO(module, PROT_READ); |
| 1559 | #endif |
| 1560 | #ifdef PROT_WRITE |
| 1561 | ADD_INT_MACRO(module, PROT_WRITE); |
| 1562 | #endif |
| 1563 | |
| 1564 | #ifdef MAP_SHARED |
| 1565 | ADD_INT_MACRO(module, MAP_SHARED); |
| 1566 | #endif |
| 1567 | #ifdef MAP_PRIVATE |
| 1568 | ADD_INT_MACRO(module, MAP_PRIVATE); |
| 1569 | #endif |
| 1570 | #ifdef MAP_DENYWRITE |
| 1571 | ADD_INT_MACRO(module, MAP_DENYWRITE); |
| 1572 | #endif |
| 1573 | #ifdef MAP_EXECUTABLE |
| 1574 | ADD_INT_MACRO(module, MAP_EXECUTABLE); |
| 1575 | #endif |
| 1576 | #ifdef MAP_ANONYMOUS |
| 1577 | if (PyModule_AddIntConstant(module, "MAP_ANON", MAP_ANONYMOUS) < 0 ) { |
| 1578 | return -1; |
| 1579 | } |
| 1580 | ADD_INT_MACRO(module, MAP_ANONYMOUS); |
| 1581 | #endif |
| 1582 | #ifdef MAP_POPULATE |
| 1583 | ADD_INT_MACRO(module, MAP_POPULATE); |
| 1584 | #endif |
| 1585 | if (PyModule_AddIntConstant(module, "PAGESIZE", (long)my_getpagesize()) < 0 ) { |
| 1586 | return -1; |
| 1587 | } |
| 1588 | |
| 1589 | if (PyModule_AddIntConstant(module, "ALLOCATIONGRANULARITY", (long)my_getallocationgranularity()) < 0 ) { |
| 1590 | return -1; |
| 1591 | } |
| 1592 | |
| 1593 | ADD_INT_MACRO(module, ACCESS_DEFAULT); |
| 1594 | ADD_INT_MACRO(module, ACCESS_READ); |
| 1595 | ADD_INT_MACRO(module, ACCESS_WRITE); |
| 1596 | ADD_INT_MACRO(module, ACCESS_COPY); |
| 1597 | |
| 1598 | #ifdef HAVE_MADVISE |
| 1599 | // Conventional advice values |
| 1600 | #ifdef MADV_NORMAL |
| 1601 | ADD_INT_MACRO(module, MADV_NORMAL); |
| 1602 | #endif |
| 1603 | #ifdef MADV_RANDOM |
| 1604 | ADD_INT_MACRO(module, MADV_RANDOM); |
| 1605 | #endif |
| 1606 | #ifdef MADV_SEQUENTIAL |
| 1607 | ADD_INT_MACRO(module, MADV_SEQUENTIAL); |
| 1608 | #endif |
| 1609 | #ifdef MADV_WILLNEED |
| 1610 | ADD_INT_MACRO(module, MADV_WILLNEED); |
| 1611 | #endif |
| 1612 | #ifdef MADV_DONTNEED |
| 1613 | ADD_INT_MACRO(module, MADV_DONTNEED); |
| 1614 | #endif |
| 1615 | |
| 1616 | // Linux-specific advice values |
| 1617 | #ifdef MADV_REMOVE |
| 1618 | ADD_INT_MACRO(module, MADV_REMOVE); |
| 1619 | #endif |
| 1620 | #ifdef MADV_DONTFORK |
| 1621 | ADD_INT_MACRO(module, MADV_DONTFORK); |
| 1622 | #endif |
| 1623 | #ifdef MADV_DOFORK |
| 1624 | ADD_INT_MACRO(module, MADV_DOFORK); |
| 1625 | #endif |
| 1626 | #ifdef MADV_HWPOISON |
| 1627 | ADD_INT_MACRO(module, MADV_HWPOISON); |
| 1628 | #endif |
| 1629 | #ifdef MADV_MERGEABLE |
| 1630 | ADD_INT_MACRO(module, MADV_MERGEABLE); |
| 1631 | #endif |
| 1632 | #ifdef MADV_UNMERGEABLE |
| 1633 | ADD_INT_MACRO(module, MADV_UNMERGEABLE); |
| 1634 | #endif |
| 1635 | #ifdef MADV_SOFT_OFFLINE |
| 1636 | ADD_INT_MACRO(module, MADV_SOFT_OFFLINE); |
| 1637 | #endif |
| 1638 | #ifdef MADV_HUGEPAGE |
| 1639 | ADD_INT_MACRO(module, MADV_HUGEPAGE); |
| 1640 | #endif |
| 1641 | #ifdef MADV_NOHUGEPAGE |
| 1642 | ADD_INT_MACRO(module, MADV_NOHUGEPAGE); |
| 1643 | #endif |
| 1644 | #ifdef MADV_DONTDUMP |
| 1645 | ADD_INT_MACRO(module, MADV_DONTDUMP); |
| 1646 | #endif |
| 1647 | #ifdef MADV_DODUMP |
| 1648 | ADD_INT_MACRO(module, MADV_DODUMP); |
| 1649 | #endif |
| 1650 | #ifdef MADV_FREE // (Also present on FreeBSD and macOS.) |
| 1651 | ADD_INT_MACRO(module, MADV_FREE); |
| 1652 | #endif |
| 1653 | |
| 1654 | // FreeBSD-specific |
| 1655 | #ifdef MADV_NOSYNC |
| 1656 | ADD_INT_MACRO(module, MADV_NOSYNC); |
| 1657 | #endif |
| 1658 | #ifdef MADV_AUTOSYNC |
| 1659 | ADD_INT_MACRO(module, MADV_AUTOSYNC); |
| 1660 | #endif |
| 1661 | #ifdef MADV_NOCORE |
| 1662 | ADD_INT_MACRO(module, MADV_NOCORE); |
| 1663 | #endif |
| 1664 | #ifdef MADV_CORE |
| 1665 | ADD_INT_MACRO(module, MADV_CORE); |
| 1666 | #endif |
| 1667 | #ifdef MADV_PROTECT |
| 1668 | ADD_INT_MACRO(module, MADV_PROTECT); |
| 1669 | #endif |
David CARLIER | 0e62efc | 2020-11-21 11:39:56 +0000 | [diff] [blame] | 1670 | |
| 1671 | // Darwin-specific |
| 1672 | #ifdef MADV_FREE_REUSABLE // (As MADV_FREE but reclaims more faithful for task_info/Activity Monitor...) |
| 1673 | ADD_INT_MACRO(module, MADV_FREE_REUSABLE); |
| 1674 | #endif |
| 1675 | #ifdef MADV_FREE_REUSE // (Reuse pages previously tagged as reusable) |
| 1676 | ADD_INT_MACRO(module, MADV_FREE_REUSE); |
| 1677 | #endif |
Dong-hee Na | 3ad52e3 | 2020-06-06 00:01:02 +0900 | [diff] [blame] | 1678 | #endif // HAVE_MADVISE |
| 1679 | return 0; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 1680 | } |
| 1681 | |
Dong-hee Na | 3ad52e3 | 2020-06-06 00:01:02 +0900 | [diff] [blame] | 1682 | static PyModuleDef_Slot mmap_slots[] = { |
| 1683 | {Py_mod_exec, mmap_exec}, |
| 1684 | {0, NULL} |
| 1685 | }; |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1686 | |
| 1687 | static struct PyModuleDef mmapmodule = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1688 | PyModuleDef_HEAD_INIT, |
Dong-hee Na | 3ad52e3 | 2020-06-06 00:01:02 +0900 | [diff] [blame] | 1689 | .m_name = "mmap", |
Erlend Egeberg Aasland | 74b4eda | 2020-11-03 10:38:31 +0100 | [diff] [blame] | 1690 | .m_size = sizeof(mmap_state), |
Dong-hee Na | 3ad52e3 | 2020-06-06 00:01:02 +0900 | [diff] [blame] | 1691 | .m_slots = mmap_slots, |
Erlend Egeberg Aasland | 74b4eda | 2020-11-03 10:38:31 +0100 | [diff] [blame] | 1692 | .m_traverse = mmap_traverse, |
| 1693 | .m_clear = mmap_clear, |
| 1694 | .m_free = mmap_free, |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1695 | }; |
| 1696 | |
Mark Hammond | 62b1ab1 | 2002-07-23 06:31:15 +0000 | [diff] [blame] | 1697 | PyMODINIT_FUNC |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1698 | PyInit_mmap(void) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 1699 | { |
Dong-hee Na | 3ad52e3 | 2020-06-06 00:01:02 +0900 | [diff] [blame] | 1700 | return PyModuleDef_Init(&mmapmodule); |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 1701 | } |