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