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 | |
Travis E. Oliphant | 8feafab | 2007-10-23 02:40:56 +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> |
| 23 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 24 | #ifndef MS_WINDOWS |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 25 | #define UNIX |
| 26 | #endif |
| 27 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 28 | #ifdef MS_WINDOWS |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 29 | #include <windows.h> |
Fred Drake | 145f96e | 2000-10-01 17:50:46 +0000 | [diff] [blame] | 30 | static int |
| 31 | my_getpagesize(void) |
| 32 | { |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 33 | SYSTEM_INFO si; |
| 34 | GetSystemInfo(&si); |
| 35 | return si.dwPageSize; |
Fred Drake | 145f96e | 2000-10-01 17:50:46 +0000 | [diff] [blame] | 36 | } |
Travis E. Oliphant | 8feafab | 2007-10-23 02:40:56 +0000 | [diff] [blame] | 37 | |
| 38 | static int |
| 39 | my_getallocationgranularity (void) |
| 40 | { |
| 41 | |
| 42 | SYSTEM_INFO si; |
| 43 | GetSystemInfo(&si); |
| 44 | return si.dwAllocationGranularity; |
| 45 | } |
| 46 | |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 47 | #endif |
| 48 | |
| 49 | #ifdef UNIX |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 50 | #include <sys/mman.h> |
Andrew M. Kuchling | 7b9fb92 | 2000-06-17 22:41:22 +0000 | [diff] [blame] | 51 | #include <sys/stat.h> |
Guido van Rossum | 4b36e6b | 2000-09-25 13:16:15 +0000 | [diff] [blame] | 52 | |
Fred Drake | 145f96e | 2000-10-01 17:50:46 +0000 | [diff] [blame] | 53 | #if defined(HAVE_SYSCONF) && defined(_SC_PAGESIZE) |
| 54 | static int |
| 55 | my_getpagesize(void) |
| 56 | { |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 57 | return sysconf(_SC_PAGESIZE); |
Fred Drake | 145f96e | 2000-10-01 17:50:46 +0000 | [diff] [blame] | 58 | } |
Travis E. Oliphant | 8feafab | 2007-10-23 02:40:56 +0000 | [diff] [blame] | 59 | |
| 60 | #define my_getallocationgranularity my_getpagesize |
Fred Drake | 145f96e | 2000-10-01 17:50:46 +0000 | [diff] [blame] | 61 | #else |
| 62 | #define my_getpagesize getpagesize |
| 63 | #endif |
| 64 | |
Guido van Rossum | 4b36e6b | 2000-09-25 13:16:15 +0000 | [diff] [blame] | 65 | #endif /* UNIX */ |
| 66 | |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 67 | #include <string.h> |
Martin v. Löwis | 0e8bd7e | 2006-06-10 12:23:46 +0000 | [diff] [blame] | 68 | |
| 69 | #ifdef HAVE_SYS_TYPES_H |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 70 | #include <sys/types.h> |
Martin v. Löwis | 0e8bd7e | 2006-06-10 12:23:46 +0000 | [diff] [blame] | 71 | #endif /* HAVE_SYS_TYPES_H */ |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 72 | |
Neal Norwitz | 3eaf2b5 | 2006-02-16 08:08:54 +0000 | [diff] [blame] | 73 | /* Prefer MAP_ANONYMOUS since MAP_ANON is deprecated according to man page. */ |
Neal Norwitz | 0e6bc8c | 2006-02-05 05:45:43 +0000 | [diff] [blame] | 74 | #if !defined(MAP_ANONYMOUS) && defined(MAP_ANON) |
| 75 | # define MAP_ANONYMOUS MAP_ANON |
| 76 | #endif |
| 77 | |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 78 | static PyObject *mmap_module_error; |
| 79 | |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 80 | typedef enum |
| 81 | { |
| 82 | ACCESS_DEFAULT, |
| 83 | ACCESS_READ, |
| 84 | ACCESS_WRITE, |
| 85 | ACCESS_COPY |
| 86 | } access_mode; |
| 87 | |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 88 | typedef struct { |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 89 | PyObject_HEAD |
| 90 | char * data; |
| 91 | size_t size; |
Travis E. Oliphant | 8feafab | 2007-10-23 02:40:56 +0000 | [diff] [blame] | 92 | size_t pos; /* relative to offset */ |
| 93 | size_t offset; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 94 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 95 | #ifdef MS_WINDOWS |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 96 | HANDLE map_handle; |
Mark Hammond | 071864a | 2000-07-30 02:46:26 +0000 | [diff] [blame] | 97 | HANDLE file_handle; |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 98 | char * tagname; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 99 | #endif |
| 100 | |
| 101 | #ifdef UNIX |
Andrew M. Kuchling | 7b9fb92 | 2000-06-17 22:41:22 +0000 | [diff] [blame] | 102 | int fd; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 103 | #endif |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 104 | |
| 105 | access_mode access; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 106 | } mmap_object; |
| 107 | |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 108 | |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 109 | static void |
Fredrik Lundh | 54cf3dc | 2000-07-08 22:05:01 +0000 | [diff] [blame] | 110 | mmap_object_dealloc(mmap_object *m_obj) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 111 | { |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 112 | #ifdef MS_WINDOWS |
Mark Hammond | 2cbed00 | 2000-07-30 02:22:43 +0000 | [diff] [blame] | 113 | if (m_obj->data != NULL) |
| 114 | UnmapViewOfFile (m_obj->data); |
Hirokazu Yamamoto | 264fc12 | 2009-03-05 14:21:12 +0000 | [diff] [blame] | 115 | if (m_obj->map_handle != NULL) |
Mark Hammond | 2cbed00 | 2000-07-30 02:22:43 +0000 | [diff] [blame] | 116 | CloseHandle (m_obj->map_handle); |
Mark Hammond | 071864a | 2000-07-30 02:46:26 +0000 | [diff] [blame] | 117 | if (m_obj->file_handle != INVALID_HANDLE_VALUE) |
| 118 | CloseHandle (m_obj->file_handle); |
Mark Hammond | 2cbed00 | 2000-07-30 02:22:43 +0000 | [diff] [blame] | 119 | if (m_obj->tagname) |
| 120 | PyMem_Free(m_obj->tagname); |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 121 | #endif /* MS_WINDOWS */ |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 122 | |
| 123 | #ifdef UNIX |
Neal Norwitz | 6eac200 | 2005-11-02 05:36:37 +0000 | [diff] [blame] | 124 | if (m_obj->fd >= 0) |
| 125 | (void) close(m_obj->fd); |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 126 | if (m_obj->data!=NULL) { |
Andrew M. Kuchling | 9bc5f33 | 2000-06-18 04:25:08 +0000 | [diff] [blame] | 127 | msync(m_obj->data, m_obj->size, MS_SYNC); |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 128 | munmap(m_obj->data, m_obj->size); |
| 129 | } |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 130 | #endif /* UNIX */ |
| 131 | |
Christian Heimes | 501dbbf | 2008-01-23 14:00:25 +0000 | [diff] [blame] | 132 | Py_TYPE(m_obj)->tp_free((PyObject*)m_obj); |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | static PyObject * |
Georg Brandl | 96a8c39 | 2006-05-29 21:04:52 +0000 | [diff] [blame] | 136 | mmap_close_method(mmap_object *self, PyObject *unused) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 137 | { |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 138 | #ifdef MS_WINDOWS |
Mark Hammond | 071864a | 2000-07-30 02:46:26 +0000 | [diff] [blame] | 139 | /* For each resource we maintain, we need to check |
Tim Peters | ec0a5f0 | 2006-02-16 23:47:20 +0000 | [diff] [blame] | 140 | the value is valid, and if so, free the resource |
Mark Hammond | 071864a | 2000-07-30 02:46:26 +0000 | [diff] [blame] | 141 | and set the member value to an invalid value so |
| 142 | the dealloc does not attempt to resource clearing |
| 143 | again. |
| 144 | TODO - should we check for errors in the close operations??? |
| 145 | */ |
| 146 | if (self->data != NULL) { |
Tim Peters | 8f9cc29 | 2006-02-17 00:00:20 +0000 | [diff] [blame] | 147 | UnmapViewOfFile(self->data); |
Mark Hammond | 071864a | 2000-07-30 02:46:26 +0000 | [diff] [blame] | 148 | self->data = NULL; |
| 149 | } |
Hirokazu Yamamoto | 264fc12 | 2009-03-05 14:21:12 +0000 | [diff] [blame] | 150 | if (self->map_handle != NULL) { |
Tim Peters | 8f9cc29 | 2006-02-17 00:00:20 +0000 | [diff] [blame] | 151 | CloseHandle(self->map_handle); |
Hirokazu Yamamoto | 264fc12 | 2009-03-05 14:21:12 +0000 | [diff] [blame] | 152 | self->map_handle = NULL; |
Mark Hammond | 071864a | 2000-07-30 02:46:26 +0000 | [diff] [blame] | 153 | } |
| 154 | if (self->file_handle != INVALID_HANDLE_VALUE) { |
Tim Peters | 8f9cc29 | 2006-02-17 00:00:20 +0000 | [diff] [blame] | 155 | CloseHandle(self->file_handle); |
Mark Hammond | 071864a | 2000-07-30 02:46:26 +0000 | [diff] [blame] | 156 | self->file_handle = INVALID_HANDLE_VALUE; |
| 157 | } |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 158 | #endif /* MS_WINDOWS */ |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 159 | |
| 160 | #ifdef UNIX |
Neal Norwitz | 6eac200 | 2005-11-02 05:36:37 +0000 | [diff] [blame] | 161 | (void) close(self->fd); |
| 162 | self->fd = -1; |
Neal Norwitz | e604c02 | 2003-01-10 20:52:16 +0000 | [diff] [blame] | 163 | if (self->data != NULL) { |
| 164 | munmap(self->data, self->size); |
| 165 | self->data = NULL; |
| 166 | } |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 167 | #endif |
| 168 | |
Tim Peters | 8f9cc29 | 2006-02-17 00:00:20 +0000 | [diff] [blame] | 169 | Py_INCREF(Py_None); |
Tim Peters | 23721ee | 2006-02-16 23:50:16 +0000 | [diff] [blame] | 170 | return Py_None; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 171 | } |
| 172 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 173 | #ifdef MS_WINDOWS |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 174 | #define CHECK_VALID(err) \ |
| 175 | do { \ |
Hirokazu Yamamoto | 264fc12 | 2009-03-05 14:21:12 +0000 | [diff] [blame] | 176 | if (self->map_handle == NULL) { \ |
Tim Peters | 8f9cc29 | 2006-02-17 00:00:20 +0000 | [diff] [blame] | 177 | PyErr_SetString(PyExc_ValueError, "mmap closed or invalid"); \ |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 178 | return err; \ |
| 179 | } \ |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 180 | } while (0) |
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 |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 184 | #define CHECK_VALID(err) \ |
| 185 | do { \ |
| 186 | if (self->data == NULL) { \ |
Tim Peters | 8f9cc29 | 2006-02-17 00:00:20 +0000 | [diff] [blame] | 187 | PyErr_SetString(PyExc_ValueError, "mmap closed or invalid"); \ |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 188 | return err; \ |
| 189 | } \ |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 190 | } while (0) |
| 191 | #endif /* UNIX */ |
| 192 | |
| 193 | static PyObject * |
Fredrik Lundh | 54cf3dc | 2000-07-08 22:05:01 +0000 | [diff] [blame] | 194 | mmap_read_byte_method(mmap_object *self, |
Georg Brandl | 96a8c39 | 2006-05-29 21:04:52 +0000 | [diff] [blame] | 195 | PyObject *unused) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 196 | { |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 197 | CHECK_VALID(NULL); |
Andrew M. Kuchling | 70d2742 | 2000-06-18 04:45:14 +0000 | [diff] [blame] | 198 | if (self->pos < self->size) { |
Tim Peters | d6283b8 | 2001-05-09 18:48:26 +0000 | [diff] [blame] | 199 | char value = self->data[self->pos]; |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 200 | self->pos += 1; |
Tim Peters | d6283b8 | 2001-05-09 18:48:26 +0000 | [diff] [blame] | 201 | return Py_BuildValue("c", value); |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 202 | } else { |
Tim Peters | 8f9cc29 | 2006-02-17 00:00:20 +0000 | [diff] [blame] | 203 | PyErr_SetString(PyExc_ValueError, "read byte out of range"); |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 204 | return NULL; |
| 205 | } |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | static PyObject * |
Fredrik Lundh | 54cf3dc | 2000-07-08 22:05:01 +0000 | [diff] [blame] | 209 | mmap_read_line_method(mmap_object *self, |
Georg Brandl | 96a8c39 | 2006-05-29 21:04:52 +0000 | [diff] [blame] | 210 | PyObject *unused) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 211 | { |
Fredrik Lundh | 54cf3dc | 2000-07-08 22:05:01 +0000 | [diff] [blame] | 212 | char *start = self->data+self->pos; |
| 213 | char *eof = self->data+self->size; |
| 214 | char *eol; |
| 215 | PyObject *result; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 216 | |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 217 | CHECK_VALID(NULL); |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 218 | |
Fred Drake | 56a87a0 | 2000-04-04 18:17:35 +0000 | [diff] [blame] | 219 | eol = memchr(start, '\n', self->size - self->pos); |
| 220 | if (!eol) |
| 221 | eol = eof; |
| 222 | else |
| 223 | ++eol; /* we're interested in the position after the |
| 224 | newline. */ |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 225 | result = PyString_FromStringAndSize(start, (eol - start)); |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 226 | self->pos += (eol - start); |
Tim Peters | 23721ee | 2006-02-16 23:50:16 +0000 | [diff] [blame] | 227 | return result; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | static PyObject * |
Fredrik Lundh | 54cf3dc | 2000-07-08 22:05:01 +0000 | [diff] [blame] | 231 | mmap_read_method(mmap_object *self, |
| 232 | PyObject *args) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 233 | { |
Martin v. Löwis | ad0a462 | 2006-02-16 14:30:23 +0000 | [diff] [blame] | 234 | Py_ssize_t num_bytes; |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 235 | PyObject *result; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 236 | |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 237 | CHECK_VALID(NULL); |
Martin v. Löwis | ad0a462 | 2006-02-16 14:30:23 +0000 | [diff] [blame] | 238 | if (!PyArg_ParseTuple(args, "n:read", &num_bytes)) |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 239 | return(NULL); |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 240 | |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 241 | /* silently 'adjust' out-of-range requests */ |
Neal Norwitz | e7d8be8 | 2008-07-31 17:17:14 +0000 | [diff] [blame] | 242 | if (num_bytes > self->size - self->pos) { |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 243 | num_bytes -= (self->pos+num_bytes) - self->size; |
| 244 | } |
| 245 | result = Py_BuildValue("s#", self->data+self->pos, num_bytes); |
Tim Peters | ec0a5f0 | 2006-02-16 23:47:20 +0000 | [diff] [blame] | 246 | self->pos += num_bytes; |
Tim Peters | 23721ee | 2006-02-16 23:50:16 +0000 | [diff] [blame] | 247 | return result; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | static PyObject * |
Andrew M. Kuchling | 5c60bfc | 2008-01-19 18:18:41 +0000 | [diff] [blame] | 251 | mmap_gfind(mmap_object *self, |
| 252 | PyObject *args, |
| 253 | int reverse) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 254 | { |
Martin v. Löwis | ad0a462 | 2006-02-16 14:30:23 +0000 | [diff] [blame] | 255 | Py_ssize_t start = self->pos; |
Neal Norwitz | 739a3c4 | 2008-01-26 20:24:36 +0000 | [diff] [blame] | 256 | Py_ssize_t end = self->size; |
| 257 | const char *needle; |
Martin v. Löwis | cfe7e09 | 2006-02-17 06:59:14 +0000 | [diff] [blame] | 258 | Py_ssize_t len; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 259 | |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 260 | CHECK_VALID(NULL); |
Andrew M. Kuchling | 5c60bfc | 2008-01-19 18:18:41 +0000 | [diff] [blame] | 261 | if (!PyArg_ParseTuple(args, reverse ? "s#|nn:rfind" : "s#|nn:find", |
| 262 | &needle, &len, &start, &end)) { |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 263 | return NULL; |
| 264 | } else { |
Neal Norwitz | 739a3c4 | 2008-01-26 20:24:36 +0000 | [diff] [blame] | 265 | const char *p, *start_p, *end_p; |
Neal Norwitz | e1027f9 | 2008-01-27 07:37:38 +0000 | [diff] [blame] | 266 | int sign = reverse ? -1 : 1; |
Greg Stein | 834f4dd | 2001-05-14 09:32:26 +0000 | [diff] [blame] | 267 | |
| 268 | if (start < 0) |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 269 | start += self->size; |
Greg Stein | 834f4dd | 2001-05-14 09:32:26 +0000 | [diff] [blame] | 270 | if (start < 0) |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 271 | start = 0; |
Tim Peters | d401edd | 2001-05-14 23:19:12 +0000 | [diff] [blame] | 272 | else if ((size_t)start > self->size) |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 273 | start = self->size; |
Greg Stein | 834f4dd | 2001-05-14 09:32:26 +0000 | [diff] [blame] | 274 | |
Andrew M. Kuchling | 5c60bfc | 2008-01-19 18:18:41 +0000 | [diff] [blame] | 275 | if (end < 0) |
| 276 | end += self->size; |
| 277 | if (end < 0) |
| 278 | end = 0; |
| 279 | else if ((size_t)end > self->size) |
| 280 | end = self->size; |
| 281 | |
Neal Norwitz | 739a3c4 | 2008-01-26 20:24:36 +0000 | [diff] [blame] | 282 | start_p = self->data + start; |
| 283 | end_p = self->data + end; |
Andrew M. Kuchling | 5c60bfc | 2008-01-19 18:18:41 +0000 | [diff] [blame] | 284 | |
Neal Norwitz | 739a3c4 | 2008-01-26 20:24:36 +0000 | [diff] [blame] | 285 | for (p = (reverse ? end_p - len : start_p); |
| 286 | (p >= start_p) && (p + len <= end_p); p += sign) { |
Martin v. Löwis | cfe7e09 | 2006-02-17 06:59:14 +0000 | [diff] [blame] | 287 | Py_ssize_t i; |
Tim Peters | c9ffa06 | 2002-03-08 05:43:32 +0000 | [diff] [blame] | 288 | for (i = 0; i < len && needle[i] == p[i]; ++i) |
| 289 | /* nothing */; |
| 290 | if (i == len) { |
Martin v. Löwis | cfe7e09 | 2006-02-17 06:59:14 +0000 | [diff] [blame] | 291 | return PyInt_FromSsize_t(p - self->data); |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 292 | } |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 293 | } |
Martin v. Löwis | cfe7e09 | 2006-02-17 06:59:14 +0000 | [diff] [blame] | 294 | return PyInt_FromLong(-1); |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 295 | } |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 296 | } |
| 297 | |
Andrew M. Kuchling | 5c60bfc | 2008-01-19 18:18:41 +0000 | [diff] [blame] | 298 | static PyObject * |
| 299 | mmap_find_method(mmap_object *self, |
| 300 | PyObject *args) |
| 301 | { |
| 302 | return mmap_gfind(self, args, 0); |
| 303 | } |
| 304 | |
| 305 | static PyObject * |
| 306 | mmap_rfind_method(mmap_object *self, |
| 307 | PyObject *args) |
| 308 | { |
| 309 | return mmap_gfind(self, args, 1); |
| 310 | } |
| 311 | |
Tim Peters | ec0a5f0 | 2006-02-16 23:47:20 +0000 | [diff] [blame] | 312 | static int |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 313 | is_writeable(mmap_object *self) |
| 314 | { |
| 315 | if (self->access != ACCESS_READ) |
Tim Peters | ec0a5f0 | 2006-02-16 23:47:20 +0000 | [diff] [blame] | 316 | return 1; |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 317 | PyErr_Format(PyExc_TypeError, "mmap can't modify a readonly memory map."); |
| 318 | return 0; |
| 319 | } |
| 320 | |
Tim Peters | ec0a5f0 | 2006-02-16 23:47:20 +0000 | [diff] [blame] | 321 | static int |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 322 | is_resizeable(mmap_object *self) |
| 323 | { |
| 324 | if ((self->access == ACCESS_WRITE) || (self->access == ACCESS_DEFAULT)) |
Tim Peters | ec0a5f0 | 2006-02-16 23:47:20 +0000 | [diff] [blame] | 325 | return 1; |
| 326 | PyErr_Format(PyExc_TypeError, |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 327 | "mmap can't resize a readonly or copy-on-write memory map."); |
| 328 | return 0; |
| 329 | } |
| 330 | |
| 331 | |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 332 | static PyObject * |
Fredrik Lundh | 54cf3dc | 2000-07-08 22:05:01 +0000 | [diff] [blame] | 333 | mmap_write_method(mmap_object *self, |
| 334 | PyObject *args) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 335 | { |
Martin v. Löwis | cfe7e09 | 2006-02-17 06:59:14 +0000 | [diff] [blame] | 336 | Py_ssize_t length; |
Fredrik Lundh | 54cf3dc | 2000-07-08 22:05:01 +0000 | [diff] [blame] | 337 | char *data; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 338 | |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 339 | CHECK_VALID(NULL); |
Tim Peters | 8f9cc29 | 2006-02-17 00:00:20 +0000 | [diff] [blame] | 340 | if (!PyArg_ParseTuple(args, "s#:write", &data, &length)) |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 341 | return(NULL); |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 342 | |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 343 | if (!is_writeable(self)) |
| 344 | return NULL; |
| 345 | |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 346 | if ((self->pos + length) > self->size) { |
Tim Peters | 8f9cc29 | 2006-02-17 00:00:20 +0000 | [diff] [blame] | 347 | PyErr_SetString(PyExc_ValueError, "data out of range"); |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 348 | return NULL; |
| 349 | } |
Tim Peters | 8f9cc29 | 2006-02-17 00:00:20 +0000 | [diff] [blame] | 350 | memcpy(self->data+self->pos, data, length); |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 351 | self->pos = self->pos+length; |
Tim Peters | 8f9cc29 | 2006-02-17 00:00:20 +0000 | [diff] [blame] | 352 | Py_INCREF(Py_None); |
Tim Peters | 23721ee | 2006-02-16 23:50:16 +0000 | [diff] [blame] | 353 | return Py_None; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | static PyObject * |
Fredrik Lundh | 54cf3dc | 2000-07-08 22:05:01 +0000 | [diff] [blame] | 357 | mmap_write_byte_method(mmap_object *self, |
| 358 | PyObject *args) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 359 | { |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 360 | char value; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 361 | |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 362 | CHECK_VALID(NULL); |
Tim Peters | 8f9cc29 | 2006-02-17 00:00:20 +0000 | [diff] [blame] | 363 | if (!PyArg_ParseTuple(args, "c:write_byte", &value)) |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 364 | return(NULL); |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 365 | |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 366 | if (!is_writeable(self)) |
| 367 | return NULL; |
Hirokazu Yamamoto | f2dc885 | 2009-02-28 10:31:54 +0000 | [diff] [blame] | 368 | |
| 369 | if (self->pos < self->size) { |
| 370 | *(self->data+self->pos) = value; |
| 371 | self->pos += 1; |
| 372 | Py_INCREF(Py_None); |
| 373 | return Py_None; |
| 374 | } |
| 375 | else { |
| 376 | PyErr_SetString(PyExc_ValueError, "write byte out of range"); |
| 377 | return NULL; |
| 378 | } |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 379 | } |
Tim Peters | ec0a5f0 | 2006-02-16 23:47:20 +0000 | [diff] [blame] | 380 | |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 381 | static PyObject * |
Fredrik Lundh | 54cf3dc | 2000-07-08 22:05:01 +0000 | [diff] [blame] | 382 | mmap_size_method(mmap_object *self, |
Georg Brandl | 96a8c39 | 2006-05-29 21:04:52 +0000 | [diff] [blame] | 383 | PyObject *unused) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 384 | { |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 385 | CHECK_VALID(NULL); |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 386 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 387 | #ifdef MS_WINDOWS |
Mark Hammond | 071864a | 2000-07-30 02:46:26 +0000 | [diff] [blame] | 388 | if (self->file_handle != INVALID_HANDLE_VALUE) { |
Martin v. Löwis | 1518607 | 2006-02-18 12:38:35 +0000 | [diff] [blame] | 389 | DWORD low,high; |
| 390 | PY_LONG_LONG size; |
| 391 | low = GetFileSize(self->file_handle, &high); |
| 392 | if (low == INVALID_FILE_SIZE) { |
| 393 | /* It might be that the function appears to have failed, |
| 394 | when indeed its size equals INVALID_FILE_SIZE */ |
| 395 | DWORD error = GetLastError(); |
| 396 | if (error != NO_ERROR) |
| 397 | return PyErr_SetFromWindowsErr(error); |
| 398 | } |
| 399 | if (!high && low < LONG_MAX) |
| 400 | return PyInt_FromLong((long)low); |
| 401 | size = (((PY_LONG_LONG)high)<<32) + low; |
| 402 | return PyLong_FromLongLong(size); |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 403 | } else { |
Martin v. Löwis | 1518607 | 2006-02-18 12:38:35 +0000 | [diff] [blame] | 404 | return PyInt_FromSsize_t(self->size); |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 405 | } |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 406 | #endif /* MS_WINDOWS */ |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 407 | |
| 408 | #ifdef UNIX |
Andrew M. Kuchling | 7b9fb92 | 2000-06-17 22:41:22 +0000 | [diff] [blame] | 409 | { |
| 410 | struct stat buf; |
| 411 | if (-1 == fstat(self->fd, &buf)) { |
| 412 | PyErr_SetFromErrno(mmap_module_error); |
| 413 | return NULL; |
| 414 | } |
Martin v. Löwis | 1518607 | 2006-02-18 12:38:35 +0000 | [diff] [blame] | 415 | return PyInt_FromSsize_t(buf.st_size); |
Andrew M. Kuchling | 7b9fb92 | 2000-06-17 22:41:22 +0000 | [diff] [blame] | 416 | } |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 417 | #endif /* UNIX */ |
| 418 | } |
| 419 | |
| 420 | /* This assumes that you want the entire file mapped, |
| 421 | / and when recreating the map will make the new file |
| 422 | / have the new size |
| 423 | / |
| 424 | / Is this really necessary? This could easily be done |
| 425 | / from python by just closing and re-opening with the |
| 426 | / new size? |
| 427 | */ |
| 428 | |
| 429 | static PyObject * |
Fredrik Lundh | 54cf3dc | 2000-07-08 22:05:01 +0000 | [diff] [blame] | 430 | mmap_resize_method(mmap_object *self, |
| 431 | PyObject *args) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 432 | { |
Martin v. Löwis | cfe7e09 | 2006-02-17 06:59:14 +0000 | [diff] [blame] | 433 | Py_ssize_t new_size; |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 434 | CHECK_VALID(NULL); |
Martin v. Löwis | cfe7e09 | 2006-02-17 06:59:14 +0000 | [diff] [blame] | 435 | if (!PyArg_ParseTuple(args, "n:resize", &new_size) || |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 436 | !is_resizeable(self)) { |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 437 | return NULL; |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 438 | #ifdef MS_WINDOWS |
Tim Peters | ec0a5f0 | 2006-02-16 23:47:20 +0000 | [diff] [blame] | 439 | } else { |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 440 | DWORD dwErrCode = 0; |
Travis E. Oliphant | 8feafab | 2007-10-23 02:40:56 +0000 | [diff] [blame] | 441 | DWORD off_hi, off_lo, newSizeLow, newSizeHigh; |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 442 | /* First, unmap the file view */ |
Tim Peters | 8f9cc29 | 2006-02-17 00:00:20 +0000 | [diff] [blame] | 443 | UnmapViewOfFile(self->data); |
Hirokazu Yamamoto | 264fc12 | 2009-03-05 14:21:12 +0000 | [diff] [blame] | 444 | self->data = NULL; |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 445 | /* Close the mapping object */ |
Tim Peters | 8f9cc29 | 2006-02-17 00:00:20 +0000 | [diff] [blame] | 446 | CloseHandle(self->map_handle); |
Hirokazu Yamamoto | 264fc12 | 2009-03-05 14:21:12 +0000 | [diff] [blame] | 447 | self->map_handle = NULL; |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 448 | /* Move to the desired EOF position */ |
Martin v. Löwis | 1518607 | 2006-02-18 12:38:35 +0000 | [diff] [blame] | 449 | #if SIZEOF_SIZE_T > 4 |
Travis E. Oliphant | 8feafab | 2007-10-23 02:40:56 +0000 | [diff] [blame] | 450 | newSizeHigh = (DWORD)((self->offset + new_size) >> 32); |
| 451 | newSizeLow = (DWORD)((self->offset + new_size) & 0xFFFFFFFF); |
| 452 | off_hi = (DWORD)(self->offset >> 32); |
| 453 | off_lo = (DWORD)(self->offset & 0xFFFFFFFF); |
Martin v. Löwis | 1518607 | 2006-02-18 12:38:35 +0000 | [diff] [blame] | 454 | #else |
| 455 | newSizeHigh = 0; |
Hirokazu Yamamoto | 17a837e | 2009-02-17 13:17:26 +0000 | [diff] [blame] | 456 | newSizeLow = (DWORD)(self->offset + new_size); |
Travis E. Oliphant | 8feafab | 2007-10-23 02:40:56 +0000 | [diff] [blame] | 457 | off_hi = 0; |
| 458 | off_lo = (DWORD)self->offset; |
Martin v. Löwis | 1518607 | 2006-02-18 12:38:35 +0000 | [diff] [blame] | 459 | #endif |
Tim Peters | 8f9cc29 | 2006-02-17 00:00:20 +0000 | [diff] [blame] | 460 | SetFilePointer(self->file_handle, |
Martin v. Löwis | 1518607 | 2006-02-18 12:38:35 +0000 | [diff] [blame] | 461 | newSizeLow, &newSizeHigh, FILE_BEGIN); |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 462 | /* Change the size of the file */ |
Tim Peters | 8f9cc29 | 2006-02-17 00:00:20 +0000 | [diff] [blame] | 463 | SetEndOfFile(self->file_handle); |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 464 | /* Create another mapping object and remap the file view */ |
Tim Peters | 8f9cc29 | 2006-02-17 00:00:20 +0000 | [diff] [blame] | 465 | self->map_handle = CreateFileMapping( |
Mark Hammond | 071864a | 2000-07-30 02:46:26 +0000 | [diff] [blame] | 466 | self->file_handle, |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 467 | NULL, |
| 468 | PAGE_READWRITE, |
Travis E. Oliphant | 8feafab | 2007-10-23 02:40:56 +0000 | [diff] [blame] | 469 | 0, |
| 470 | 0, |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 471 | self->tagname); |
| 472 | if (self->map_handle != NULL) { |
Tim Peters | 8f9cc29 | 2006-02-17 00:00:20 +0000 | [diff] [blame] | 473 | self->data = (char *) MapViewOfFile(self->map_handle, |
| 474 | FILE_MAP_WRITE, |
Travis E. Oliphant | 8feafab | 2007-10-23 02:40:56 +0000 | [diff] [blame] | 475 | off_hi, |
| 476 | off_lo, |
| 477 | new_size); |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 478 | if (self->data != NULL) { |
| 479 | self->size = new_size; |
Tim Peters | 8f9cc29 | 2006-02-17 00:00:20 +0000 | [diff] [blame] | 480 | Py_INCREF(Py_None); |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 481 | return Py_None; |
| 482 | } else { |
| 483 | dwErrCode = GetLastError(); |
Hirokazu Yamamoto | 264fc12 | 2009-03-05 14:21:12 +0000 | [diff] [blame] | 484 | CloseHandle(self->map_handle); |
| 485 | self->map_handle = NULL; |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 486 | } |
| 487 | } else { |
| 488 | dwErrCode = GetLastError(); |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 489 | } |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 490 | PyErr_SetFromWindowsErr(dwErrCode); |
Tim Peters | 23721ee | 2006-02-16 23:50:16 +0000 | [diff] [blame] | 491 | return NULL; |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 492 | #endif /* MS_WINDOWS */ |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 493 | |
| 494 | #ifdef UNIX |
Tim Peters | ec0a5f0 | 2006-02-16 23:47:20 +0000 | [diff] [blame] | 495 | #ifndef HAVE_MREMAP |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 496 | } else { |
| 497 | PyErr_SetString(PyExc_SystemError, |
| 498 | "mmap: resizing not available--no mremap()"); |
| 499 | return NULL; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 500 | #else |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 501 | } else { |
Armin Rigo | 335ffe8 | 2005-09-20 19:04:02 +0000 | [diff] [blame] | 502 | void *newmap; |
| 503 | |
Hirokazu Yamamoto | 17a837e | 2009-02-17 13:17:26 +0000 | [diff] [blame] | 504 | if (ftruncate(self->fd, self->offset + new_size) == -1) { |
Georg Brandl | 38387b8 | 2005-08-24 07:17:40 +0000 | [diff] [blame] | 505 | PyErr_SetFromErrno(mmap_module_error); |
| 506 | return NULL; |
| 507 | } |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 508 | |
Andrew M. Kuchling | 6fef30e | 2000-06-18 14:51:21 +0000 | [diff] [blame] | 509 | #ifdef MREMAP_MAYMOVE |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 510 | newmap = mremap(self->data, self->size, new_size, MREMAP_MAYMOVE); |
Andrew M. Kuchling | 6fef30e | 2000-06-18 14:51:21 +0000 | [diff] [blame] | 511 | #else |
Jesse Noller | 40a6164 | 2009-03-31 18:12:35 +0000 | [diff] [blame] | 512 | #if defined(__NetBSD__) |
| 513 | newmap = mremap(self->data, self->size, self->data, new_size, 0); |
| 514 | #else |
| 515 | newmap = mremap(self->data, self->size, new_size, 0); |
| 516 | #endif /* __NetBSD__ */ |
Andrew M. Kuchling | 6fef30e | 2000-06-18 14:51:21 +0000 | [diff] [blame] | 517 | #endif |
Tim Peters | ec0a5f0 | 2006-02-16 23:47:20 +0000 | [diff] [blame] | 518 | if (newmap == (void *)-1) |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 519 | { |
| 520 | PyErr_SetFromErrno(mmap_module_error); |
| 521 | return NULL; |
| 522 | } |
| 523 | self->data = newmap; |
| 524 | self->size = new_size; |
| 525 | Py_INCREF(Py_None); |
| 526 | return Py_None; |
Andrew M. Kuchling | 6fef30e | 2000-06-18 14:51:21 +0000 | [diff] [blame] | 527 | #endif /* HAVE_MREMAP */ |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 528 | #endif /* UNIX */ |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 529 | } |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 530 | } |
| 531 | |
| 532 | static PyObject * |
Georg Brandl | 96a8c39 | 2006-05-29 21:04:52 +0000 | [diff] [blame] | 533 | mmap_tell_method(mmap_object *self, PyObject *unused) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 534 | { |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 535 | CHECK_VALID(NULL); |
Neal Norwitz | 670f875 | 2006-08-22 13:56:56 +0000 | [diff] [blame] | 536 | return PyInt_FromSize_t(self->pos); |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 537 | } |
| 538 | |
| 539 | static PyObject * |
Fredrik Lundh | 54cf3dc | 2000-07-08 22:05:01 +0000 | [diff] [blame] | 540 | mmap_flush_method(mmap_object *self, PyObject *args) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 541 | { |
Martin v. Löwis | ad0a462 | 2006-02-16 14:30:23 +0000 | [diff] [blame] | 542 | Py_ssize_t offset = 0; |
| 543 | Py_ssize_t size = self->size; |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 544 | CHECK_VALID(NULL); |
Tim Peters | f288295 | 2006-02-17 01:07:39 +0000 | [diff] [blame] | 545 | if (!PyArg_ParseTuple(args, "|nn:flush", &offset, &size)) |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 546 | return NULL; |
Tim Peters | f288295 | 2006-02-17 01:07:39 +0000 | [diff] [blame] | 547 | if ((size_t)(offset + size) > self->size) { |
Tim Peters | 8f9cc29 | 2006-02-17 00:00:20 +0000 | [diff] [blame] | 548 | PyErr_SetString(PyExc_ValueError, "flush values out of range"); |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 549 | return NULL; |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 550 | } |
Neal Norwitz | 448654f | 2008-01-27 07:36:03 +0000 | [diff] [blame] | 551 | #ifdef MS_WINDOWS |
| 552 | return PyInt_FromLong((long) FlushViewOfFile(self->data+offset, size)); |
| 553 | #elif defined(UNIX) |
| 554 | /* XXX semantics of return value? */ |
| 555 | /* XXX flags for msync? */ |
| 556 | if (-1 == msync(self->data + offset, size, MS_SYNC)) { |
| 557 | PyErr_SetFromErrno(mmap_module_error); |
| 558 | return NULL; |
| 559 | } |
| 560 | return PyInt_FromLong(0); |
| 561 | #else |
| 562 | PyErr_SetString(PyExc_ValueError, "flush not supported on this system"); |
| 563 | return NULL; |
| 564 | #endif |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 565 | } |
| 566 | |
| 567 | static PyObject * |
Fredrik Lundh | 54cf3dc | 2000-07-08 22:05:01 +0000 | [diff] [blame] | 568 | mmap_seek_method(mmap_object *self, PyObject *args) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 569 | { |
Martin v. Löwis | cfe7e09 | 2006-02-17 06:59:14 +0000 | [diff] [blame] | 570 | Py_ssize_t dist; |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 571 | int how=0; |
| 572 | CHECK_VALID(NULL); |
Martin v. Löwis | cfe7e09 | 2006-02-17 06:59:14 +0000 | [diff] [blame] | 573 | if (!PyArg_ParseTuple(args, "n|i:seek", &dist, &how)) |
Tim Peters | 8f9cc29 | 2006-02-17 00:00:20 +0000 | [diff] [blame] | 574 | return NULL; |
| 575 | else { |
Andrew M. Kuchling | 70d2742 | 2000-06-18 04:45:14 +0000 | [diff] [blame] | 576 | size_t where; |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 577 | switch (how) { |
Andrew M. Kuchling | 70d2742 | 2000-06-18 04:45:14 +0000 | [diff] [blame] | 578 | case 0: /* relative to start */ |
| 579 | if (dist < 0) |
| 580 | goto onoutofrange; |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 581 | where = dist; |
| 582 | break; |
Andrew M. Kuchling | 70d2742 | 2000-06-18 04:45:14 +0000 | [diff] [blame] | 583 | case 1: /* relative to current position */ |
Martin v. Löwis | cfe7e09 | 2006-02-17 06:59:14 +0000 | [diff] [blame] | 584 | if ((Py_ssize_t)self->pos + dist < 0) |
Andrew M. Kuchling | 70d2742 | 2000-06-18 04:45:14 +0000 | [diff] [blame] | 585 | goto onoutofrange; |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 586 | where = self->pos + dist; |
| 587 | break; |
Andrew M. Kuchling | 70d2742 | 2000-06-18 04:45:14 +0000 | [diff] [blame] | 588 | case 2: /* relative to end */ |
Martin v. Löwis | cfe7e09 | 2006-02-17 06:59:14 +0000 | [diff] [blame] | 589 | if ((Py_ssize_t)self->size + dist < 0) |
Andrew M. Kuchling | 70d2742 | 2000-06-18 04:45:14 +0000 | [diff] [blame] | 590 | goto onoutofrange; |
| 591 | where = self->size + dist; |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 592 | break; |
| 593 | default: |
Tim Peters | 8f9cc29 | 2006-02-17 00:00:20 +0000 | [diff] [blame] | 594 | PyErr_SetString(PyExc_ValueError, "unknown seek type"); |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 595 | return NULL; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 596 | } |
Andrew M. Kuchling | 70d2742 | 2000-06-18 04:45:14 +0000 | [diff] [blame] | 597 | if (where > self->size) |
| 598 | goto onoutofrange; |
| 599 | self->pos = where; |
Tim Peters | 8f9cc29 | 2006-02-17 00:00:20 +0000 | [diff] [blame] | 600 | Py_INCREF(Py_None); |
Tim Peters | 23721ee | 2006-02-16 23:50:16 +0000 | [diff] [blame] | 601 | return Py_None; |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 602 | } |
Andrew M. Kuchling | 70d2742 | 2000-06-18 04:45:14 +0000 | [diff] [blame] | 603 | |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 604 | onoutofrange: |
Tim Peters | 8f9cc29 | 2006-02-17 00:00:20 +0000 | [diff] [blame] | 605 | PyErr_SetString(PyExc_ValueError, "seek out of range"); |
Andrew M. Kuchling | 70d2742 | 2000-06-18 04:45:14 +0000 | [diff] [blame] | 606 | return NULL; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 607 | } |
| 608 | |
| 609 | static PyObject * |
Fredrik Lundh | 54cf3dc | 2000-07-08 22:05:01 +0000 | [diff] [blame] | 610 | mmap_move_method(mmap_object *self, PyObject *args) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 611 | { |
Jack Diederich | 2ecd3c3 | 2009-04-01 20:26:13 +0000 | [diff] [blame] | 612 | unsigned long dest, src, cnt; |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 613 | CHECK_VALID(NULL); |
Jack Diederich | 2ecd3c3 | 2009-04-01 20:26:13 +0000 | [diff] [blame] | 614 | if (!PyArg_ParseTuple(args, "kkk:move", &dest, &src, &cnt) || |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 615 | !is_writeable(self)) { |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 616 | return NULL; |
| 617 | } else { |
| 618 | /* bounds check the values */ |
Jack Diederich | 2ecd3c3 | 2009-04-01 20:26:13 +0000 | [diff] [blame] | 619 | if (cnt < 0 || (cnt + dest) < cnt || (cnt + src) < cnt || |
| 620 | src < 0 || src > self->size || (src + cnt) > self->size || |
| 621 | dest < 0 || dest > self->size || (dest + cnt) > self->size) { |
Tim Peters | 8f9cc29 | 2006-02-17 00:00:20 +0000 | [diff] [blame] | 622 | PyErr_SetString(PyExc_ValueError, |
Jack Diederich | 2ecd3c3 | 2009-04-01 20:26:13 +0000 | [diff] [blame] | 623 | "source, destination, or count out of range"); |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 624 | return NULL; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 625 | } |
Jack Diederich | 2ecd3c3 | 2009-04-01 20:26:13 +0000 | [diff] [blame] | 626 | memmove(self->data+dest, self->data+src, cnt); |
| 627 | Py_INCREF(Py_None); |
| 628 | return Py_None; |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 629 | } |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 630 | } |
| 631 | |
| 632 | static struct PyMethodDef mmap_object_methods[] = { |
Georg Brandl | 96a8c39 | 2006-05-29 21:04:52 +0000 | [diff] [blame] | 633 | {"close", (PyCFunction) mmap_close_method, METH_NOARGS}, |
Martin v. Löwis | 43b936d | 2002-01-17 23:15:58 +0000 | [diff] [blame] | 634 | {"find", (PyCFunction) mmap_find_method, METH_VARARGS}, |
Andrew M. Kuchling | 5c60bfc | 2008-01-19 18:18:41 +0000 | [diff] [blame] | 635 | {"rfind", (PyCFunction) mmap_rfind_method, METH_VARARGS}, |
Martin v. Löwis | 43b936d | 2002-01-17 23:15:58 +0000 | [diff] [blame] | 636 | {"flush", (PyCFunction) mmap_flush_method, METH_VARARGS}, |
| 637 | {"move", (PyCFunction) mmap_move_method, METH_VARARGS}, |
| 638 | {"read", (PyCFunction) mmap_read_method, METH_VARARGS}, |
Georg Brandl | 96a8c39 | 2006-05-29 21:04:52 +0000 | [diff] [blame] | 639 | {"read_byte", (PyCFunction) mmap_read_byte_method, METH_NOARGS}, |
| 640 | {"readline", (PyCFunction) mmap_read_line_method, METH_NOARGS}, |
Martin v. Löwis | 43b936d | 2002-01-17 23:15:58 +0000 | [diff] [blame] | 641 | {"resize", (PyCFunction) mmap_resize_method, METH_VARARGS}, |
| 642 | {"seek", (PyCFunction) mmap_seek_method, METH_VARARGS}, |
Georg Brandl | 96a8c39 | 2006-05-29 21:04:52 +0000 | [diff] [blame] | 643 | {"size", (PyCFunction) mmap_size_method, METH_NOARGS}, |
| 644 | {"tell", (PyCFunction) mmap_tell_method, METH_NOARGS}, |
Martin v. Löwis | 43b936d | 2002-01-17 23:15:58 +0000 | [diff] [blame] | 645 | {"write", (PyCFunction) mmap_write_method, METH_VARARGS}, |
| 646 | {"write_byte", (PyCFunction) mmap_write_byte_method, METH_VARARGS}, |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 647 | {NULL, NULL} /* sentinel */ |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 648 | }; |
| 649 | |
| 650 | /* Functions for treating an mmap'ed file as a buffer */ |
| 651 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 652 | static Py_ssize_t |
| 653 | mmap_buffer_getreadbuf(mmap_object *self, Py_ssize_t index, const void **ptr) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 654 | { |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 655 | CHECK_VALID(-1); |
Tim Peters | 8f9cc29 | 2006-02-17 00:00:20 +0000 | [diff] [blame] | 656 | if (index != 0) { |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 657 | PyErr_SetString(PyExc_SystemError, |
| 658 | "Accessing non-existent mmap segment"); |
| 659 | return -1; |
| 660 | } |
| 661 | *ptr = self->data; |
| 662 | return self->size; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 663 | } |
| 664 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 665 | static Py_ssize_t |
| 666 | mmap_buffer_getwritebuf(mmap_object *self, Py_ssize_t index, const void **ptr) |
Tim Peters | ec0a5f0 | 2006-02-16 23:47:20 +0000 | [diff] [blame] | 667 | { |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 668 | CHECK_VALID(-1); |
Tim Peters | 8f9cc29 | 2006-02-17 00:00:20 +0000 | [diff] [blame] | 669 | if (index != 0) { |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 670 | PyErr_SetString(PyExc_SystemError, |
| 671 | "Accessing non-existent mmap segment"); |
| 672 | return -1; |
| 673 | } |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 674 | if (!is_writeable(self)) |
| 675 | return -1; |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 676 | *ptr = self->data; |
| 677 | return self->size; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 678 | } |
| 679 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 680 | static Py_ssize_t |
| 681 | mmap_buffer_getsegcount(mmap_object *self, Py_ssize_t *lenp) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 682 | { |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 683 | CHECK_VALID(-1); |
Tim Peters | ec0a5f0 | 2006-02-16 23:47:20 +0000 | [diff] [blame] | 684 | if (lenp) |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 685 | *lenp = self->size; |
| 686 | return 1; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 687 | } |
| 688 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 689 | static Py_ssize_t |
| 690 | mmap_buffer_getcharbuffer(mmap_object *self, Py_ssize_t index, const void **ptr) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 691 | { |
Tim Peters | 8f9cc29 | 2006-02-17 00:00:20 +0000 | [diff] [blame] | 692 | if (index != 0) { |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 693 | PyErr_SetString(PyExc_SystemError, |
| 694 | "accessing non-existent buffer segment"); |
| 695 | return -1; |
| 696 | } |
| 697 | *ptr = (const char *)self->data; |
| 698 | return self->size; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 699 | } |
| 700 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 701 | static Py_ssize_t |
Fredrik Lundh | 54cf3dc | 2000-07-08 22:05:01 +0000 | [diff] [blame] | 702 | mmap_length(mmap_object *self) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 703 | { |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 704 | CHECK_VALID(-1); |
| 705 | return self->size; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 706 | } |
| 707 | |
| 708 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 709 | mmap_item(mmap_object *self, Py_ssize_t i) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 710 | { |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 711 | CHECK_VALID(NULL); |
Guido van Rossum | ce8e1dc | 2000-07-01 00:51:51 +0000 | [diff] [blame] | 712 | if (i < 0 || (size_t)i >= self->size) { |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 713 | PyErr_SetString(PyExc_IndexError, "mmap index out of range"); |
| 714 | return NULL; |
| 715 | } |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 716 | return PyString_FromStringAndSize(self->data + i, 1); |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 717 | } |
| 718 | |
| 719 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 720 | mmap_slice(mmap_object *self, Py_ssize_t ilow, Py_ssize_t ihigh) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 721 | { |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 722 | CHECK_VALID(NULL); |
| 723 | if (ilow < 0) |
| 724 | ilow = 0; |
Guido van Rossum | ce8e1dc | 2000-07-01 00:51:51 +0000 | [diff] [blame] | 725 | else if ((size_t)ilow > self->size) |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 726 | ilow = self->size; |
| 727 | if (ihigh < 0) |
| 728 | ihigh = 0; |
| 729 | if (ihigh < ilow) |
| 730 | ihigh = ilow; |
Guido van Rossum | ce8e1dc | 2000-07-01 00:51:51 +0000 | [diff] [blame] | 731 | else if ((size_t)ihigh > self->size) |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 732 | ihigh = self->size; |
Tim Peters | ec0a5f0 | 2006-02-16 23:47:20 +0000 | [diff] [blame] | 733 | |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 734 | return PyString_FromStringAndSize(self->data + ilow, ihigh-ilow); |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 735 | } |
| 736 | |
| 737 | static PyObject * |
Thomas Wouters | 3ccec68 | 2007-08-28 15:28:19 +0000 | [diff] [blame] | 738 | mmap_subscript(mmap_object *self, PyObject *item) |
| 739 | { |
| 740 | CHECK_VALID(NULL); |
| 741 | if (PyIndex_Check(item)) { |
| 742 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
| 743 | if (i == -1 && PyErr_Occurred()) |
| 744 | return NULL; |
| 745 | if (i < 0) |
| 746 | i += self->size; |
Hirokazu Yamamoto | f6bbd0e | 2009-02-17 10:12:10 +0000 | [diff] [blame] | 747 | if (i < 0 || (size_t)i >= self->size) { |
Thomas Wouters | 3ccec68 | 2007-08-28 15:28:19 +0000 | [diff] [blame] | 748 | PyErr_SetString(PyExc_IndexError, |
| 749 | "mmap index out of range"); |
| 750 | return NULL; |
| 751 | } |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 752 | return PyString_FromStringAndSize(self->data + i, 1); |
Thomas Wouters | 3ccec68 | 2007-08-28 15:28:19 +0000 | [diff] [blame] | 753 | } |
| 754 | else if (PySlice_Check(item)) { |
| 755 | Py_ssize_t start, stop, step, slicelen; |
| 756 | |
| 757 | if (PySlice_GetIndicesEx((PySliceObject *)item, self->size, |
| 758 | &start, &stop, &step, &slicelen) < 0) { |
| 759 | return NULL; |
| 760 | } |
| 761 | |
| 762 | if (slicelen <= 0) |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 763 | return PyString_FromStringAndSize("", 0); |
Thomas Wouters | 3ccec68 | 2007-08-28 15:28:19 +0000 | [diff] [blame] | 764 | else if (step == 1) |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 765 | return PyString_FromStringAndSize(self->data + start, |
Thomas Wouters | 3ccec68 | 2007-08-28 15:28:19 +0000 | [diff] [blame] | 766 | slicelen); |
| 767 | else { |
| 768 | char *result_buf = (char *)PyMem_Malloc(slicelen); |
| 769 | Py_ssize_t cur, i; |
| 770 | PyObject *result; |
| 771 | |
| 772 | if (result_buf == NULL) |
| 773 | return PyErr_NoMemory(); |
| 774 | for (cur = start, i = 0; i < slicelen; |
| 775 | cur += step, i++) { |
| 776 | result_buf[i] = self->data[cur]; |
| 777 | } |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 778 | result = PyString_FromStringAndSize(result_buf, |
Thomas Wouters | 3ccec68 | 2007-08-28 15:28:19 +0000 | [diff] [blame] | 779 | slicelen); |
| 780 | PyMem_Free(result_buf); |
| 781 | return result; |
| 782 | } |
| 783 | } |
| 784 | else { |
| 785 | PyErr_SetString(PyExc_TypeError, |
| 786 | "mmap indices must be integers"); |
| 787 | return NULL; |
| 788 | } |
| 789 | } |
| 790 | |
| 791 | static PyObject * |
Fredrik Lundh | 54cf3dc | 2000-07-08 22:05:01 +0000 | [diff] [blame] | 792 | mmap_concat(mmap_object *self, PyObject *bb) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 793 | { |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 794 | CHECK_VALID(NULL); |
| 795 | PyErr_SetString(PyExc_SystemError, |
| 796 | "mmaps don't support concatenation"); |
| 797 | return NULL; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 798 | } |
| 799 | |
| 800 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 801 | mmap_repeat(mmap_object *self, Py_ssize_t n) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 802 | { |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 803 | CHECK_VALID(NULL); |
| 804 | PyErr_SetString(PyExc_SystemError, |
| 805 | "mmaps don't support repeat operation"); |
| 806 | return NULL; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 807 | } |
| 808 | |
| 809 | static int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 810 | mmap_ass_slice(mmap_object *self, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 811 | { |
Guido van Rossum | 36d4f8b | 2000-04-10 21:34:37 +0000 | [diff] [blame] | 812 | const char *buf; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 813 | |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 814 | CHECK_VALID(-1); |
| 815 | if (ilow < 0) |
| 816 | ilow = 0; |
Guido van Rossum | ce8e1dc | 2000-07-01 00:51:51 +0000 | [diff] [blame] | 817 | else if ((size_t)ilow > self->size) |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 818 | ilow = self->size; |
| 819 | if (ihigh < 0) |
| 820 | ihigh = 0; |
| 821 | if (ihigh < ilow) |
| 822 | ihigh = ilow; |
Guido van Rossum | ce8e1dc | 2000-07-01 00:51:51 +0000 | [diff] [blame] | 823 | else if ((size_t)ihigh > self->size) |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 824 | ihigh = self->size; |
Tim Peters | ec0a5f0 | 2006-02-16 23:47:20 +0000 | [diff] [blame] | 825 | |
Thomas Wouters | 1baac72 | 2001-07-16 15:47:36 +0000 | [diff] [blame] | 826 | if (v == NULL) { |
| 827 | PyErr_SetString(PyExc_TypeError, |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 828 | "mmap object doesn't support slice deletion"); |
Thomas Wouters | 1baac72 | 2001-07-16 15:47:36 +0000 | [diff] [blame] | 829 | return -1; |
| 830 | } |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 831 | if (! (PyString_Check(v)) ) { |
Tim Peters | ec0a5f0 | 2006-02-16 23:47:20 +0000 | [diff] [blame] | 832 | PyErr_SetString(PyExc_IndexError, |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 833 | "mmap slice assignment must be a string"); |
| 834 | return -1; |
| 835 | } |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 836 | if (PyString_Size(v) != (ihigh - ilow)) { |
Tim Peters | ec0a5f0 | 2006-02-16 23:47:20 +0000 | [diff] [blame] | 837 | PyErr_SetString(PyExc_IndexError, |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 838 | "mmap slice assignment is wrong size"); |
| 839 | return -1; |
| 840 | } |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 841 | if (!is_writeable(self)) |
| 842 | return -1; |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 843 | buf = PyString_AsString(v); |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 844 | memcpy(self->data + ilow, buf, ihigh-ilow); |
| 845 | return 0; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 846 | } |
| 847 | |
| 848 | static int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 849 | 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] | 850 | { |
Guido van Rossum | 36d4f8b | 2000-04-10 21:34:37 +0000 | [diff] [blame] | 851 | const char *buf; |
Tim Peters | ec0a5f0 | 2006-02-16 23:47:20 +0000 | [diff] [blame] | 852 | |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 853 | CHECK_VALID(-1); |
Guido van Rossum | ce8e1dc | 2000-07-01 00:51:51 +0000 | [diff] [blame] | 854 | if (i < 0 || (size_t)i >= self->size) { |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 855 | PyErr_SetString(PyExc_IndexError, "mmap index out of range"); |
| 856 | return -1; |
| 857 | } |
Thomas Wouters | 1baac72 | 2001-07-16 15:47:36 +0000 | [diff] [blame] | 858 | if (v == NULL) { |
| 859 | PyErr_SetString(PyExc_TypeError, |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 860 | "mmap object doesn't support item deletion"); |
Thomas Wouters | 1baac72 | 2001-07-16 15:47:36 +0000 | [diff] [blame] | 861 | return -1; |
| 862 | } |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 863 | if (! (PyString_Check(v) && PyString_Size(v)==1) ) { |
Tim Peters | ec0a5f0 | 2006-02-16 23:47:20 +0000 | [diff] [blame] | 864 | PyErr_SetString(PyExc_IndexError, |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 865 | "mmap assignment must be single-character string"); |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 866 | return -1; |
| 867 | } |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 868 | if (!is_writeable(self)) |
| 869 | return -1; |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 870 | buf = PyString_AsString(v); |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 871 | self->data[i] = buf[0]; |
| 872 | return 0; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 873 | } |
| 874 | |
Thomas Wouters | 3ccec68 | 2007-08-28 15:28:19 +0000 | [diff] [blame] | 875 | static int |
| 876 | mmap_ass_subscript(mmap_object *self, PyObject *item, PyObject *value) |
| 877 | { |
| 878 | CHECK_VALID(-1); |
| 879 | |
| 880 | if (PyIndex_Check(item)) { |
| 881 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
| 882 | const char *buf; |
| 883 | |
| 884 | if (i == -1 && PyErr_Occurred()) |
| 885 | return -1; |
| 886 | if (i < 0) |
| 887 | i += self->size; |
Hirokazu Yamamoto | f6bbd0e | 2009-02-17 10:12:10 +0000 | [diff] [blame] | 888 | if (i < 0 || (size_t)i >= self->size) { |
Thomas Wouters | 3ccec68 | 2007-08-28 15:28:19 +0000 | [diff] [blame] | 889 | PyErr_SetString(PyExc_IndexError, |
| 890 | "mmap index out of range"); |
| 891 | return -1; |
| 892 | } |
| 893 | if (value == NULL) { |
| 894 | PyErr_SetString(PyExc_TypeError, |
| 895 | "mmap object doesn't support item deletion"); |
| 896 | return -1; |
| 897 | } |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 898 | if (!PyString_Check(value) || PyString_Size(value) != 1) { |
Thomas Wouters | 3ccec68 | 2007-08-28 15:28:19 +0000 | [diff] [blame] | 899 | PyErr_SetString(PyExc_IndexError, |
| 900 | "mmap assignment must be single-character string"); |
| 901 | return -1; |
| 902 | } |
| 903 | if (!is_writeable(self)) |
| 904 | return -1; |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 905 | buf = PyString_AsString(value); |
Thomas Wouters | 3ccec68 | 2007-08-28 15:28:19 +0000 | [diff] [blame] | 906 | self->data[i] = buf[0]; |
| 907 | return 0; |
| 908 | } |
| 909 | else if (PySlice_Check(item)) { |
| 910 | Py_ssize_t start, stop, step, slicelen; |
| 911 | |
| 912 | if (PySlice_GetIndicesEx((PySliceObject *)item, |
| 913 | self->size, &start, &stop, |
| 914 | &step, &slicelen) < 0) { |
| 915 | return -1; |
| 916 | } |
| 917 | if (value == NULL) { |
| 918 | PyErr_SetString(PyExc_TypeError, |
| 919 | "mmap object doesn't support slice deletion"); |
| 920 | return -1; |
| 921 | } |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 922 | if (!PyString_Check(value)) { |
Thomas Wouters | 3ccec68 | 2007-08-28 15:28:19 +0000 | [diff] [blame] | 923 | PyErr_SetString(PyExc_IndexError, |
| 924 | "mmap slice assignment must be a string"); |
| 925 | return -1; |
| 926 | } |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 927 | if (PyString_Size(value) != slicelen) { |
Thomas Wouters | 3ccec68 | 2007-08-28 15:28:19 +0000 | [diff] [blame] | 928 | PyErr_SetString(PyExc_IndexError, |
| 929 | "mmap slice assignment is wrong size"); |
| 930 | return -1; |
| 931 | } |
| 932 | if (!is_writeable(self)) |
| 933 | return -1; |
| 934 | |
| 935 | if (slicelen == 0) |
| 936 | return 0; |
| 937 | else if (step == 1) { |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 938 | const char *buf = PyString_AsString(value); |
Thomas Wouters | 3ccec68 | 2007-08-28 15:28:19 +0000 | [diff] [blame] | 939 | |
| 940 | if (buf == NULL) |
| 941 | return -1; |
| 942 | memcpy(self->data + start, buf, slicelen); |
| 943 | return 0; |
| 944 | } |
| 945 | else { |
| 946 | Py_ssize_t cur, i; |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 947 | const char *buf = PyString_AsString(value); |
Thomas Wouters | 3ccec68 | 2007-08-28 15:28:19 +0000 | [diff] [blame] | 948 | |
| 949 | if (buf == NULL) |
| 950 | return -1; |
| 951 | for (cur = start, i = 0; i < slicelen; |
| 952 | cur += step, i++) { |
| 953 | self->data[cur] = buf[i]; |
| 954 | } |
| 955 | return 0; |
| 956 | } |
| 957 | } |
| 958 | else { |
| 959 | PyErr_SetString(PyExc_TypeError, |
| 960 | "mmap indices must be integer"); |
| 961 | return -1; |
| 962 | } |
| 963 | } |
| 964 | |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 965 | static PySequenceMethods mmap_as_sequence = { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 966 | (lenfunc)mmap_length, /*sq_length*/ |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 967 | (binaryfunc)mmap_concat, /*sq_concat*/ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 968 | (ssizeargfunc)mmap_repeat, /*sq_repeat*/ |
| 969 | (ssizeargfunc)mmap_item, /*sq_item*/ |
| 970 | (ssizessizeargfunc)mmap_slice, /*sq_slice*/ |
| 971 | (ssizeobjargproc)mmap_ass_item, /*sq_ass_item*/ |
| 972 | (ssizessizeobjargproc)mmap_ass_slice, /*sq_ass_slice*/ |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 973 | }; |
| 974 | |
Thomas Wouters | 3ccec68 | 2007-08-28 15:28:19 +0000 | [diff] [blame] | 975 | static PyMappingMethods mmap_as_mapping = { |
| 976 | (lenfunc)mmap_length, |
| 977 | (binaryfunc)mmap_subscript, |
| 978 | (objobjargproc)mmap_ass_subscript, |
| 979 | }; |
| 980 | |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 981 | static PyBufferProcs mmap_as_buffer = { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 982 | (readbufferproc)mmap_buffer_getreadbuf, |
| 983 | (writebufferproc)mmap_buffer_getwritebuf, |
| 984 | (segcountproc)mmap_buffer_getsegcount, |
| 985 | (charbufferproc)mmap_buffer_getcharbuffer, |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 986 | }; |
| 987 | |
Georg Brandl | 845c403 | 2008-01-21 14:16:46 +0000 | [diff] [blame] | 988 | static PyObject * |
| 989 | new_mmap_object(PyTypeObject *type, PyObject *args, PyObject *kwdict); |
| 990 | |
Georg Brandl | ef92802 | 2008-01-20 14:50:05 +0000 | [diff] [blame] | 991 | PyDoc_STRVAR(mmap_doc, |
| 992 | "Windows: mmap(fileno, length[, tagname[, access[, offset]]])\n\ |
| 993 | \n\ |
| 994 | Maps length bytes from the file specified by the file handle fileno,\n\ |
| 995 | and returns a mmap object. If length is larger than the current size\n\ |
| 996 | of the file, the file is extended to contain length bytes. If length\n\ |
| 997 | is 0, the maximum length of the map is the current size of the file,\n\ |
| 998 | except that if the file is empty Windows raises an exception (you cannot\n\ |
| 999 | create an empty mapping on Windows).\n\ |
| 1000 | \n\ |
| 1001 | Unix: mmap(fileno, length[, flags[, prot[, access[, offset]]]])\n\ |
| 1002 | \n\ |
| 1003 | Maps length bytes from the file specified by the file descriptor fileno,\n\ |
| 1004 | and returns a mmap object. If length is 0, the maximum length of the map\n\ |
| 1005 | will be the current size of the file when mmap is called.\n\ |
| 1006 | flags specifies the nature of the mapping. MAP_PRIVATE creates a\n\ |
| 1007 | private copy-on-write mapping, so changes to the contents of the mmap\n\ |
Thomas Heller | 6626099 | 2008-08-19 17:47:13 +0000 | [diff] [blame] | 1008 | object will be private to this process, and MAP_SHARED creates a mapping\n\ |
Georg Brandl | ef92802 | 2008-01-20 14:50:05 +0000 | [diff] [blame] | 1009 | that's shared with all other processes mapping the same areas of the file.\n\ |
| 1010 | The default value is MAP_SHARED.\n\ |
| 1011 | \n\ |
| 1012 | To map anonymous memory, pass -1 as the fileno (both versions)."); |
| 1013 | |
| 1014 | |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 1015 | static PyTypeObject mmap_object_type = { |
Georg Brandl | 845c403 | 2008-01-21 14:16:46 +0000 | [diff] [blame] | 1016 | PyVarObject_HEAD_INIT(NULL, 0) |
Guido van Rossum | 1464839 | 2001-12-08 18:02:58 +0000 | [diff] [blame] | 1017 | "mmap.mmap", /* tp_name */ |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 1018 | sizeof(mmap_object), /* tp_size */ |
| 1019 | 0, /* tp_itemsize */ |
| 1020 | /* methods */ |
| 1021 | (destructor) mmap_object_dealloc, /* tp_dealloc */ |
| 1022 | 0, /* tp_print */ |
Georg Brandl | ef92802 | 2008-01-20 14:50:05 +0000 | [diff] [blame] | 1023 | 0, /* tp_getattr */ |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 1024 | 0, /* tp_setattr */ |
| 1025 | 0, /* tp_compare */ |
| 1026 | 0, /* tp_repr */ |
| 1027 | 0, /* tp_as_number */ |
| 1028 | &mmap_as_sequence, /*tp_as_sequence*/ |
Thomas Wouters | 3ccec68 | 2007-08-28 15:28:19 +0000 | [diff] [blame] | 1029 | &mmap_as_mapping, /*tp_as_mapping*/ |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 1030 | 0, /*tp_hash*/ |
| 1031 | 0, /*tp_call*/ |
| 1032 | 0, /*tp_str*/ |
Georg Brandl | ef92802 | 2008-01-20 14:50:05 +0000 | [diff] [blame] | 1033 | PyObject_GenericGetAttr, /*tp_getattro*/ |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 1034 | 0, /*tp_setattro*/ |
| 1035 | &mmap_as_buffer, /*tp_as_buffer*/ |
Georg Brandl | 845c403 | 2008-01-21 14:16:46 +0000 | [diff] [blame] | 1036 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GETCHARBUFFER, /*tp_flags*/ |
Georg Brandl | ef92802 | 2008-01-20 14:50:05 +0000 | [diff] [blame] | 1037 | mmap_doc, /*tp_doc*/ |
| 1038 | 0, /* tp_traverse */ |
| 1039 | 0, /* tp_clear */ |
| 1040 | 0, /* tp_richcompare */ |
| 1041 | 0, /* tp_weaklistoffset */ |
Georg Brandl | 845c403 | 2008-01-21 14:16:46 +0000 | [diff] [blame] | 1042 | 0, /* tp_iter */ |
| 1043 | 0, /* tp_iternext */ |
Georg Brandl | ef92802 | 2008-01-20 14:50:05 +0000 | [diff] [blame] | 1044 | mmap_object_methods, /* tp_methods */ |
Georg Brandl | 845c403 | 2008-01-21 14:16:46 +0000 | [diff] [blame] | 1045 | 0, /* tp_members */ |
| 1046 | 0, /* tp_getset */ |
| 1047 | 0, /* tp_base */ |
| 1048 | 0, /* tp_dict */ |
| 1049 | 0, /* tp_descr_get */ |
| 1050 | 0, /* tp_descr_set */ |
| 1051 | 0, /* tp_dictoffset */ |
| 1052 | 0, /* tp_init */ |
| 1053 | PyType_GenericAlloc, /* tp_alloc */ |
| 1054 | new_mmap_object, /* tp_new */ |
| 1055 | PyObject_Del, /* tp_free */ |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 1056 | }; |
| 1057 | |
Andrew M. Kuchling | 70d2742 | 2000-06-18 04:45:14 +0000 | [diff] [blame] | 1058 | |
| 1059 | /* extract the map size from the given PyObject |
| 1060 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 1061 | Returns -1 on error, with an appropriate Python exception raised. On |
Andrew M. Kuchling | 70d2742 | 2000-06-18 04:45:14 +0000 | [diff] [blame] | 1062 | success, the map size is returned. */ |
Martin v. Löwis | cfe7e09 | 2006-02-17 06:59:14 +0000 | [diff] [blame] | 1063 | static Py_ssize_t |
Travis E. Oliphant | 8feafab | 2007-10-23 02:40:56 +0000 | [diff] [blame] | 1064 | _GetMapSize(PyObject *o, const char* param) |
Andrew M. Kuchling | 70d2742 | 2000-06-18 04:45:14 +0000 | [diff] [blame] | 1065 | { |
Travis E. Oliphant | 8feafab | 2007-10-23 02:40:56 +0000 | [diff] [blame] | 1066 | if (o == NULL) |
| 1067 | return 0; |
Neal Norwitz | 8a87f5d | 2006-08-12 17:03:09 +0000 | [diff] [blame] | 1068 | if (PyIndex_Check(o)) { |
| 1069 | Py_ssize_t i = PyNumber_AsSsize_t(o, PyExc_OverflowError); |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 1070 | if (i==-1 && PyErr_Occurred()) |
Andrew M. Kuchling | 70d2742 | 2000-06-18 04:45:14 +0000 | [diff] [blame] | 1071 | return -1; |
Neal Norwitz | 8a87f5d | 2006-08-12 17:03:09 +0000 | [diff] [blame] | 1072 | if (i < 0) { |
Travis E. Oliphant | 8feafab | 2007-10-23 02:40:56 +0000 | [diff] [blame] | 1073 | PyErr_Format(PyExc_OverflowError, |
| 1074 | "memory mapped %s must be positive", |
| 1075 | param); |
Neal Norwitz | 8a87f5d | 2006-08-12 17:03:09 +0000 | [diff] [blame] | 1076 | return -1; |
| 1077 | } |
Martin v. Löwis | cfe7e09 | 2006-02-17 06:59:14 +0000 | [diff] [blame] | 1078 | return i; |
Andrew M. Kuchling | 70d2742 | 2000-06-18 04:45:14 +0000 | [diff] [blame] | 1079 | } |
Andrew M. Kuchling | 70d2742 | 2000-06-18 04:45:14 +0000 | [diff] [blame] | 1080 | |
Neal Norwitz | 8a87f5d | 2006-08-12 17:03:09 +0000 | [diff] [blame] | 1081 | PyErr_SetString(PyExc_TypeError, "map size must be an integral value"); |
Andrew M. Kuchling | 70d2742 | 2000-06-18 04:45:14 +0000 | [diff] [blame] | 1082 | return -1; |
| 1083 | } |
| 1084 | |
Tim Peters | ec0a5f0 | 2006-02-16 23:47:20 +0000 | [diff] [blame] | 1085 | #ifdef UNIX |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 1086 | static PyObject * |
Georg Brandl | 845c403 | 2008-01-21 14:16:46 +0000 | [diff] [blame] | 1087 | new_mmap_object(PyTypeObject *type, PyObject *args, PyObject *kwdict) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 1088 | { |
Neal Norwitz | b567392 | 2002-09-05 21:48:07 +0000 | [diff] [blame] | 1089 | #ifdef HAVE_FSTAT |
| 1090 | struct stat st; |
| 1091 | #endif |
Fredrik Lundh | 54cf3dc | 2000-07-08 22:05:01 +0000 | [diff] [blame] | 1092 | mmap_object *m_obj; |
Travis E. Oliphant | 8feafab | 2007-10-23 02:40:56 +0000 | [diff] [blame] | 1093 | PyObject *map_size_obj = NULL, *offset_obj = NULL; |
| 1094 | Py_ssize_t map_size, offset; |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 1095 | int fd, flags = MAP_SHARED, prot = PROT_WRITE | PROT_READ; |
Neal Norwitz | 0e6bc8c | 2006-02-05 05:45:43 +0000 | [diff] [blame] | 1096 | int devzero = -1; |
Neal Norwitz | 8856fb7 | 2005-12-18 03:34:22 +0000 | [diff] [blame] | 1097 | int access = (int)ACCESS_DEFAULT; |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 1098 | static char *keywords[] = {"fileno", "length", |
Tim Peters | ec0a5f0 | 2006-02-16 23:47:20 +0000 | [diff] [blame] | 1099 | "flags", "prot", |
Travis E. Oliphant | 8feafab | 2007-10-23 02:40:56 +0000 | [diff] [blame] | 1100 | "access", "offset", NULL}; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 1101 | |
Travis E. Oliphant | 8feafab | 2007-10-23 02:40:56 +0000 | [diff] [blame] | 1102 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iO|iiiO", keywords, |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 1103 | &fd, &map_size_obj, &flags, &prot, |
Travis E. Oliphant | 8feafab | 2007-10-23 02:40:56 +0000 | [diff] [blame] | 1104 | &access, &offset_obj)) |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 1105 | return NULL; |
Travis E. Oliphant | 8feafab | 2007-10-23 02:40:56 +0000 | [diff] [blame] | 1106 | map_size = _GetMapSize(map_size_obj, "size"); |
Andrew M. Kuchling | 70d2742 | 2000-06-18 04:45:14 +0000 | [diff] [blame] | 1107 | if (map_size < 0) |
| 1108 | return NULL; |
Travis E. Oliphant | 8feafab | 2007-10-23 02:40:56 +0000 | [diff] [blame] | 1109 | offset = _GetMapSize(offset_obj, "offset"); |
| 1110 | if (offset < 0) |
| 1111 | return NULL; |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 1112 | |
Tim Peters | ec0a5f0 | 2006-02-16 23:47:20 +0000 | [diff] [blame] | 1113 | if ((access != (int)ACCESS_DEFAULT) && |
Neal Norwitz | d1cfc8a | 2006-02-05 03:36:57 +0000 | [diff] [blame] | 1114 | ((flags != MAP_SHARED) || (prot != (PROT_WRITE | PROT_READ)))) |
Tim Peters | ec0a5f0 | 2006-02-16 23:47:20 +0000 | [diff] [blame] | 1115 | return PyErr_Format(PyExc_ValueError, |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 1116 | "mmap can't specify both access and flags, prot."); |
Neal Norwitz | d1cfc8a | 2006-02-05 03:36:57 +0000 | [diff] [blame] | 1117 | switch ((access_mode)access) { |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 1118 | case ACCESS_READ: |
| 1119 | flags = MAP_SHARED; |
| 1120 | prot = PROT_READ; |
| 1121 | break; |
| 1122 | case ACCESS_WRITE: |
| 1123 | flags = MAP_SHARED; |
| 1124 | prot = PROT_READ | PROT_WRITE; |
| 1125 | break; |
| 1126 | case ACCESS_COPY: |
| 1127 | flags = MAP_PRIVATE; |
| 1128 | prot = PROT_READ | PROT_WRITE; |
| 1129 | break; |
Tim Peters | ec0a5f0 | 2006-02-16 23:47:20 +0000 | [diff] [blame] | 1130 | case ACCESS_DEFAULT: |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 1131 | /* use the specified or default values of flags and prot */ |
| 1132 | break; |
| 1133 | default: |
Tim Peters | ec0a5f0 | 2006-02-16 23:47:20 +0000 | [diff] [blame] | 1134 | return PyErr_Format(PyExc_ValueError, |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 1135 | "mmap invalid access parameter."); |
| 1136 | } |
Neal Norwitz | b567392 | 2002-09-05 21:48:07 +0000 | [diff] [blame] | 1137 | |
Christian Heimes | 7adfad8 | 2008-02-15 08:20:11 +0000 | [diff] [blame] | 1138 | if (prot == PROT_READ) { |
| 1139 | access = ACCESS_READ; |
| 1140 | } |
| 1141 | |
Neal Norwitz | b567392 | 2002-09-05 21:48:07 +0000 | [diff] [blame] | 1142 | #ifdef HAVE_FSTAT |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 1143 | # ifdef __VMS |
| 1144 | /* on OpenVMS we must ensure that all bytes are written to the file */ |
Andrew M. Kuchling | 7c22ccc | 2008-01-10 13:37:12 +0000 | [diff] [blame] | 1145 | if (fd != -1) { |
| 1146 | fsync(fd); |
| 1147 | } |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 1148 | # endif |
Andrew M. Kuchling | 7c22ccc | 2008-01-10 13:37:12 +0000 | [diff] [blame] | 1149 | if (fd != -1 && fstat(fd, &st) == 0 && S_ISREG(st.st_mode)) { |
Martin v. Löwis | 7fe60c0 | 2005-03-03 11:22:44 +0000 | [diff] [blame] | 1150 | if (map_size == 0) { |
Martin v. Löwis | cfe7e09 | 2006-02-17 06:59:14 +0000 | [diff] [blame] | 1151 | map_size = st.st_size; |
Travis E. Oliphant | 8feafab | 2007-10-23 02:40:56 +0000 | [diff] [blame] | 1152 | } else if ((size_t)offset + (size_t)map_size > st.st_size) { |
Tim Peters | ec0a5f0 | 2006-02-16 23:47:20 +0000 | [diff] [blame] | 1153 | PyErr_SetString(PyExc_ValueError, |
Martin v. Löwis | 7fe60c0 | 2005-03-03 11:22:44 +0000 | [diff] [blame] | 1154 | "mmap length is greater than file size"); |
| 1155 | return NULL; |
| 1156 | } |
Neal Norwitz | b567392 | 2002-09-05 21:48:07 +0000 | [diff] [blame] | 1157 | } |
| 1158 | #endif |
Georg Brandl | 845c403 | 2008-01-21 14:16:46 +0000 | [diff] [blame] | 1159 | m_obj = (mmap_object *)type->tp_alloc(type, 0); |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 1160 | if (m_obj == NULL) {return NULL;} |
Neal Norwitz | 3b4fff8 | 2006-01-11 08:54:45 +0000 | [diff] [blame] | 1161 | m_obj->data = NULL; |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 1162 | m_obj->size = (size_t) map_size; |
| 1163 | m_obj->pos = (size_t) 0; |
Travis E. Oliphant | 8feafab | 2007-10-23 02:40:56 +0000 | [diff] [blame] | 1164 | m_obj->offset = offset; |
Neal Norwitz | 0e6bc8c | 2006-02-05 05:45:43 +0000 | [diff] [blame] | 1165 | if (fd == -1) { |
| 1166 | m_obj->fd = -1; |
| 1167 | /* Assume the caller wants to map anonymous memory. |
| 1168 | This is the same behaviour as Windows. mmap.mmap(-1, size) |
| 1169 | on both Windows and Unix map anonymous memory. |
| 1170 | */ |
| 1171 | #ifdef MAP_ANONYMOUS |
| 1172 | /* BSD way to map anonymous memory */ |
| 1173 | flags |= MAP_ANONYMOUS; |
| 1174 | #else |
| 1175 | /* SVR4 method to map anonymous memory is to open /dev/zero */ |
| 1176 | fd = devzero = open("/dev/zero", O_RDWR); |
| 1177 | if (devzero == -1) { |
| 1178 | Py_DECREF(m_obj); |
| 1179 | PyErr_SetFromErrno(mmap_module_error); |
| 1180 | return NULL; |
| 1181 | } |
| 1182 | #endif |
| 1183 | } else { |
| 1184 | m_obj->fd = dup(fd); |
| 1185 | if (m_obj->fd == -1) { |
| 1186 | Py_DECREF(m_obj); |
| 1187 | PyErr_SetFromErrno(mmap_module_error); |
| 1188 | return NULL; |
| 1189 | } |
Georg Brandl | 38387b8 | 2005-08-24 07:17:40 +0000 | [diff] [blame] | 1190 | } |
Travis E. Oliphant | 8feafab | 2007-10-23 02:40:56 +0000 | [diff] [blame] | 1191 | |
Tim Peters | ec0a5f0 | 2006-02-16 23:47:20 +0000 | [diff] [blame] | 1192 | m_obj->data = mmap(NULL, map_size, |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 1193 | prot, flags, |
Travis E. Oliphant | 8feafab | 2007-10-23 02:40:56 +0000 | [diff] [blame] | 1194 | fd, offset); |
Neal Norwitz | 0e6bc8c | 2006-02-05 05:45:43 +0000 | [diff] [blame] | 1195 | |
| 1196 | if (devzero != -1) { |
| 1197 | close(devzero); |
| 1198 | } |
| 1199 | |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 1200 | if (m_obj->data == (char *)-1) { |
Andrew M. Kuchling | 16581c8 | 2004-05-19 14:39:08 +0000 | [diff] [blame] | 1201 | m_obj->data = NULL; |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 1202 | Py_DECREF(m_obj); |
| 1203 | PyErr_SetFromErrno(mmap_module_error); |
| 1204 | return NULL; |
| 1205 | } |
Neal Norwitz | 8856fb7 | 2005-12-18 03:34:22 +0000 | [diff] [blame] | 1206 | m_obj->access = (access_mode)access; |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 1207 | return (PyObject *)m_obj; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 1208 | } |
| 1209 | #endif /* UNIX */ |
| 1210 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 1211 | #ifdef MS_WINDOWS |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 1212 | static PyObject * |
Georg Brandl | 845c403 | 2008-01-21 14:16:46 +0000 | [diff] [blame] | 1213 | new_mmap_object(PyTypeObject *type, PyObject *args, PyObject *kwdict) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 1214 | { |
Fredrik Lundh | 54cf3dc | 2000-07-08 22:05:01 +0000 | [diff] [blame] | 1215 | mmap_object *m_obj; |
Travis E. Oliphant | 8feafab | 2007-10-23 02:40:56 +0000 | [diff] [blame] | 1216 | PyObject *map_size_obj = NULL, *offset_obj = NULL; |
| 1217 | Py_ssize_t map_size, offset; |
| 1218 | DWORD off_hi; /* upper 32 bits of offset */ |
| 1219 | DWORD off_lo; /* lower 32 bits of offset */ |
| 1220 | DWORD size_hi; /* upper 32 bits of size */ |
| 1221 | DWORD size_lo; /* lower 32 bits of size */ |
Fredrik Lundh | 54cf3dc | 2000-07-08 22:05:01 +0000 | [diff] [blame] | 1222 | char *tagname = ""; |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 1223 | DWORD dwErr = 0; |
| 1224 | int fileno; |
Mark Hammond | 071864a | 2000-07-30 02:46:26 +0000 | [diff] [blame] | 1225 | HANDLE fh = 0; |
Neal Norwitz | 8856fb7 | 2005-12-18 03:34:22 +0000 | [diff] [blame] | 1226 | int access = (access_mode)ACCESS_DEFAULT; |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 1227 | DWORD flProtect, dwDesiredAccess; |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 1228 | static char *keywords[] = { "fileno", "length", |
Tim Peters | ec0a5f0 | 2006-02-16 23:47:20 +0000 | [diff] [blame] | 1229 | "tagname", |
Travis E. Oliphant | 8feafab | 2007-10-23 02:40:56 +0000 | [diff] [blame] | 1230 | "access", "offset", NULL }; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 1231 | |
Travis E. Oliphant | 8feafab | 2007-10-23 02:40:56 +0000 | [diff] [blame] | 1232 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iO|ziO", keywords, |
Tim Peters | ec0a5f0 | 2006-02-16 23:47:20 +0000 | [diff] [blame] | 1233 | &fileno, &map_size_obj, |
Travis E. Oliphant | 8feafab | 2007-10-23 02:40:56 +0000 | [diff] [blame] | 1234 | &tagname, &access, &offset_obj)) { |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 1235 | return NULL; |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 1236 | } |
| 1237 | |
Neal Norwitz | 8856fb7 | 2005-12-18 03:34:22 +0000 | [diff] [blame] | 1238 | switch((access_mode)access) { |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 1239 | case ACCESS_READ: |
| 1240 | flProtect = PAGE_READONLY; |
| 1241 | dwDesiredAccess = FILE_MAP_READ; |
| 1242 | break; |
| 1243 | case ACCESS_DEFAULT: case ACCESS_WRITE: |
| 1244 | flProtect = PAGE_READWRITE; |
| 1245 | dwDesiredAccess = FILE_MAP_WRITE; |
| 1246 | break; |
| 1247 | case ACCESS_COPY: |
| 1248 | flProtect = PAGE_WRITECOPY; |
| 1249 | dwDesiredAccess = FILE_MAP_COPY; |
| 1250 | break; |
| 1251 | default: |
Tim Peters | ec0a5f0 | 2006-02-16 23:47:20 +0000 | [diff] [blame] | 1252 | return PyErr_Format(PyExc_ValueError, |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 1253 | "mmap invalid access parameter."); |
| 1254 | } |
| 1255 | |
Travis E. Oliphant | 8feafab | 2007-10-23 02:40:56 +0000 | [diff] [blame] | 1256 | map_size = _GetMapSize(map_size_obj, "size"); |
Andrew M. Kuchling | 70d2742 | 2000-06-18 04:45:14 +0000 | [diff] [blame] | 1257 | if (map_size < 0) |
| 1258 | return NULL; |
Travis E. Oliphant | 8feafab | 2007-10-23 02:40:56 +0000 | [diff] [blame] | 1259 | offset = _GetMapSize(offset_obj, "offset"); |
| 1260 | if (offset < 0) |
| 1261 | return NULL; |
Tim Peters | ec0a5f0 | 2006-02-16 23:47:20 +0000 | [diff] [blame] | 1262 | |
Neal Norwitz | 0e6bc8c | 2006-02-05 05:45:43 +0000 | [diff] [blame] | 1263 | /* assume -1 and 0 both mean invalid filedescriptor |
| 1264 | to 'anonymously' map memory. |
| 1265 | XXX: fileno == 0 is a valid fd, but was accepted prior to 2.5. |
| 1266 | XXX: Should this code be added? |
| 1267 | if (fileno == 0) |
| 1268 | PyErr_Warn(PyExc_DeprecationWarning, |
| 1269 | "don't use 0 for anonymous memory"); |
| 1270 | */ |
| 1271 | if (fileno != -1 && fileno != 0) { |
Mark Hammond | 071864a | 2000-07-30 02:46:26 +0000 | [diff] [blame] | 1272 | fh = (HANDLE)_get_osfhandle(fileno); |
| 1273 | if (fh==(HANDLE)-1) { |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 1274 | PyErr_SetFromErrno(mmap_module_error); |
| 1275 | return NULL; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 1276 | } |
Fred Drake | 1ef4e2d | 2000-04-05 14:15:31 +0000 | [diff] [blame] | 1277 | /* Win9x appears to need us seeked to zero */ |
Guido van Rossum | 69c2b88 | 2003-04-09 19:31:02 +0000 | [diff] [blame] | 1278 | lseek(fileno, 0, SEEK_SET); |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 1279 | } |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 1280 | |
Georg Brandl | 845c403 | 2008-01-21 14:16:46 +0000 | [diff] [blame] | 1281 | m_obj = (mmap_object *)type->tp_alloc(type, 0); |
Tim Peters | 8f9cc29 | 2006-02-17 00:00:20 +0000 | [diff] [blame] | 1282 | if (m_obj == NULL) |
Mark Hammond | 2cbed00 | 2000-07-30 02:22:43 +0000 | [diff] [blame] | 1283 | return NULL; |
| 1284 | /* Set every field to an invalid marker, so we can safely |
| 1285 | destruct the object in the face of failure */ |
| 1286 | m_obj->data = NULL; |
Mark Hammond | 071864a | 2000-07-30 02:46:26 +0000 | [diff] [blame] | 1287 | m_obj->file_handle = INVALID_HANDLE_VALUE; |
Hirokazu Yamamoto | 264fc12 | 2009-03-05 14:21:12 +0000 | [diff] [blame] | 1288 | m_obj->map_handle = NULL; |
Mark Hammond | 2cbed00 | 2000-07-30 02:22:43 +0000 | [diff] [blame] | 1289 | m_obj->tagname = NULL; |
Travis E. Oliphant | 8feafab | 2007-10-23 02:40:56 +0000 | [diff] [blame] | 1290 | m_obj->offset = offset; |
Mark Hammond | 2cbed00 | 2000-07-30 02:22:43 +0000 | [diff] [blame] | 1291 | |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 1292 | if (fh) { |
Mark Hammond | 2cbed00 | 2000-07-30 02:22:43 +0000 | [diff] [blame] | 1293 | /* It is necessary to duplicate the handle, so the |
| 1294 | Python code can close it on us */ |
| 1295 | if (!DuplicateHandle( |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 1296 | GetCurrentProcess(), /* source process handle */ |
| 1297 | fh, /* handle to be duplicated */ |
| 1298 | GetCurrentProcess(), /* target proc handle */ |
| 1299 | (LPHANDLE)&m_obj->file_handle, /* result */ |
| 1300 | 0, /* access - ignored due to options value */ |
| 1301 | FALSE, /* inherited by child processes? */ |
| 1302 | DUPLICATE_SAME_ACCESS)) { /* options */ |
Mark Hammond | 2cbed00 | 2000-07-30 02:22:43 +0000 | [diff] [blame] | 1303 | dwErr = GetLastError(); |
| 1304 | Py_DECREF(m_obj); |
| 1305 | PyErr_SetFromWindowsErr(dwErr); |
| 1306 | return NULL; |
| 1307 | } |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 1308 | if (!map_size) { |
Martin v. Löwis | 1518607 | 2006-02-18 12:38:35 +0000 | [diff] [blame] | 1309 | DWORD low,high; |
| 1310 | low = GetFileSize(fh, &high); |
| 1311 | /* low might just happen to have the value INVALID_FILE_SIZE; |
| 1312 | so we need to check the last error also. */ |
| 1313 | if (low == INVALID_FILE_SIZE && |
| 1314 | (dwErr = GetLastError()) != NO_ERROR) { |
| 1315 | Py_DECREF(m_obj); |
| 1316 | return PyErr_SetFromWindowsErr(dwErr); |
| 1317 | } |
| 1318 | |
| 1319 | #if SIZEOF_SIZE_T > 4 |
| 1320 | m_obj->size = (((size_t)high)<<32) + low; |
| 1321 | #else |
| 1322 | if (high) |
| 1323 | /* File is too large to map completely */ |
| 1324 | m_obj->size = (size_t)-1; |
| 1325 | else |
| 1326 | m_obj->size = low; |
| 1327 | #endif |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 1328 | } else { |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 1329 | m_obj->size = map_size; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 1330 | } |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 1331 | } |
| 1332 | else { |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 1333 | m_obj->size = map_size; |
| 1334 | } |
| 1335 | |
| 1336 | /* set the initial position */ |
| 1337 | m_obj->pos = (size_t) 0; |
| 1338 | |
Mark Hammond | 2cbed00 | 2000-07-30 02:22:43 +0000 | [diff] [blame] | 1339 | /* set the tag name */ |
Tim Peters | 0d9f9dc | 2001-01-10 05:42:18 +0000 | [diff] [blame] | 1340 | if (tagname != NULL && *tagname != '\0') { |
Mark Hammond | 2cbed00 | 2000-07-30 02:22:43 +0000 | [diff] [blame] | 1341 | m_obj->tagname = PyMem_Malloc(strlen(tagname)+1); |
| 1342 | if (m_obj->tagname == NULL) { |
| 1343 | PyErr_NoMemory(); |
| 1344 | Py_DECREF(m_obj); |
| 1345 | return NULL; |
| 1346 | } |
| 1347 | strcpy(m_obj->tagname, tagname); |
| 1348 | } |
| 1349 | else |
| 1350 | m_obj->tagname = NULL; |
| 1351 | |
Neal Norwitz | 8856fb7 | 2005-12-18 03:34:22 +0000 | [diff] [blame] | 1352 | m_obj->access = (access_mode)access; |
Tim Peters | e564e7f | 2006-02-16 23:46:01 +0000 | [diff] [blame] | 1353 | /* DWORD is a 4-byte int. If we're on a box where size_t consumes |
Thomas Wouters | 2c98a7b | 2006-02-17 09:59:00 +0000 | [diff] [blame] | 1354 | * more than 4 bytes, we need to break it apart. Else (size_t |
Tim Peters | e564e7f | 2006-02-16 23:46:01 +0000 | [diff] [blame] | 1355 | * consumes 4 bytes), C doesn't define what happens if we shift |
| 1356 | * right by 32, so we need different code. |
| 1357 | */ |
| 1358 | #if SIZEOF_SIZE_T > 4 |
Travis E. Oliphant | 8feafab | 2007-10-23 02:40:56 +0000 | [diff] [blame] | 1359 | size_hi = (DWORD)((offset + m_obj->size) >> 32); |
| 1360 | size_lo = (DWORD)((offset + m_obj->size) & 0xFFFFFFFF); |
| 1361 | off_hi = (DWORD)(offset >> 32); |
| 1362 | off_lo = (DWORD)(offset & 0xFFFFFFFF); |
Tim Peters | e564e7f | 2006-02-16 23:46:01 +0000 | [diff] [blame] | 1363 | #else |
| 1364 | size_hi = 0; |
Travis E. Oliphant | 8feafab | 2007-10-23 02:40:56 +0000 | [diff] [blame] | 1365 | size_lo = (DWORD)(offset + m_obj->size); |
| 1366 | off_hi = 0; |
| 1367 | off_lo = (DWORD)offset; |
Tim Peters | e564e7f | 2006-02-16 23:46:01 +0000 | [diff] [blame] | 1368 | #endif |
Travis E. Oliphant | 8feafab | 2007-10-23 02:40:56 +0000 | [diff] [blame] | 1369 | /* For files, it would be sufficient to pass 0 as size. |
| 1370 | For anonymous maps, we have to pass the size explicitly. */ |
Tim Peters | 8f9cc29 | 2006-02-17 00:00:20 +0000 | [diff] [blame] | 1371 | m_obj->map_handle = CreateFileMapping(m_obj->file_handle, |
| 1372 | NULL, |
| 1373 | flProtect, |
| 1374 | size_hi, |
| 1375 | size_lo, |
| 1376 | m_obj->tagname); |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 1377 | if (m_obj->map_handle != NULL) { |
Tim Peters | 8f9cc29 | 2006-02-17 00:00:20 +0000 | [diff] [blame] | 1378 | m_obj->data = (char *) MapViewOfFile(m_obj->map_handle, |
| 1379 | dwDesiredAccess, |
Travis E. Oliphant | 8feafab | 2007-10-23 02:40:56 +0000 | [diff] [blame] | 1380 | off_hi, |
| 1381 | off_lo, |
Hirokazu Yamamoto | b0e10c7 | 2009-02-28 12:13:07 +0000 | [diff] [blame] | 1382 | m_obj->size); |
Tim Peters | 23721ee | 2006-02-16 23:50:16 +0000 | [diff] [blame] | 1383 | if (m_obj->data != NULL) |
| 1384 | return (PyObject *)m_obj; |
Hirokazu Yamamoto | 264fc12 | 2009-03-05 14:21:12 +0000 | [diff] [blame] | 1385 | else { |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 1386 | dwErr = GetLastError(); |
Hirokazu Yamamoto | 264fc12 | 2009-03-05 14:21:12 +0000 | [diff] [blame] | 1387 | CloseHandle(m_obj->map_handle); |
| 1388 | m_obj->map_handle = NULL; |
| 1389 | } |
Tim Peters | 23721ee | 2006-02-16 23:50:16 +0000 | [diff] [blame] | 1390 | } else |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 1391 | dwErr = GetLastError(); |
Mark Hammond | 2cbed00 | 2000-07-30 02:22:43 +0000 | [diff] [blame] | 1392 | Py_DECREF(m_obj); |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 1393 | PyErr_SetFromWindowsErr(dwErr); |
Tim Peters | 23721ee | 2006-02-16 23:50:16 +0000 | [diff] [blame] | 1394 | return NULL; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 1395 | } |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 1396 | #endif /* MS_WINDOWS */ |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 1397 | |
Neal Norwitz | e9ac0bb | 2006-08-13 18:11:27 +0000 | [diff] [blame] | 1398 | static void |
| 1399 | setint(PyObject *d, const char *name, long value) |
| 1400 | { |
| 1401 | PyObject *o = PyInt_FromLong(value); |
Neal Norwitz | 5ed4997 | 2006-08-13 18:41:15 +0000 | [diff] [blame] | 1402 | if (o && PyDict_SetItemString(d, name, o) == 0) { |
Neal Norwitz | e9ac0bb | 2006-08-13 18:11:27 +0000 | [diff] [blame] | 1403 | Py_DECREF(o); |
Neal Norwitz | 5ed4997 | 2006-08-13 18:41:15 +0000 | [diff] [blame] | 1404 | } |
Neal Norwitz | e9ac0bb | 2006-08-13 18:11:27 +0000 | [diff] [blame] | 1405 | } |
| 1406 | |
Mark Hammond | 62b1ab1 | 2002-07-23 06:31:15 +0000 | [diff] [blame] | 1407 | PyMODINIT_FUNC |
Georg Brandl | 845c403 | 2008-01-21 14:16:46 +0000 | [diff] [blame] | 1408 | initmmap(void) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 1409 | { |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 1410 | PyObject *dict, *module; |
Tim Peters | 2caf8df | 2001-01-14 05:05:51 +0000 | [diff] [blame] | 1411 | |
Georg Brandl | 845c403 | 2008-01-21 14:16:46 +0000 | [diff] [blame] | 1412 | if (PyType_Ready(&mmap_object_type) < 0) |
| 1413 | return; |
Tim Peters | 2caf8df | 2001-01-14 05:05:51 +0000 | [diff] [blame] | 1414 | |
Georg Brandl | 845c403 | 2008-01-21 14:16:46 +0000 | [diff] [blame] | 1415 | module = Py_InitModule("mmap", NULL); |
Neal Norwitz | 1ac754f | 2006-01-19 06:09:39 +0000 | [diff] [blame] | 1416 | if (module == NULL) |
| 1417 | return; |
Tim Peters | 8f9cc29 | 2006-02-17 00:00:20 +0000 | [diff] [blame] | 1418 | dict = PyModule_GetDict(module); |
Neal Norwitz | e9ac0bb | 2006-08-13 18:11:27 +0000 | [diff] [blame] | 1419 | if (!dict) |
| 1420 | return; |
Facundo Batista | e139688 | 2008-02-17 18:59:29 +0000 | [diff] [blame] | 1421 | mmap_module_error = PyErr_NewException("mmap.error", |
| 1422 | PyExc_EnvironmentError , NULL); |
| 1423 | if (mmap_module_error == NULL) |
| 1424 | return; |
Tim Peters | 8f9cc29 | 2006-02-17 00:00:20 +0000 | [diff] [blame] | 1425 | PyDict_SetItemString(dict, "error", mmap_module_error); |
Georg Brandl | 845c403 | 2008-01-21 14:16:46 +0000 | [diff] [blame] | 1426 | PyDict_SetItemString(dict, "mmap", (PyObject*) &mmap_object_type); |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 1427 | #ifdef PROT_EXEC |
Neal Norwitz | e9ac0bb | 2006-08-13 18:11:27 +0000 | [diff] [blame] | 1428 | setint(dict, "PROT_EXEC", PROT_EXEC); |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 1429 | #endif |
| 1430 | #ifdef PROT_READ |
Neal Norwitz | e9ac0bb | 2006-08-13 18:11:27 +0000 | [diff] [blame] | 1431 | setint(dict, "PROT_READ", PROT_READ); |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 1432 | #endif |
| 1433 | #ifdef PROT_WRITE |
Neal Norwitz | e9ac0bb | 2006-08-13 18:11:27 +0000 | [diff] [blame] | 1434 | setint(dict, "PROT_WRITE", PROT_WRITE); |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 1435 | #endif |
| 1436 | |
| 1437 | #ifdef MAP_SHARED |
Neal Norwitz | e9ac0bb | 2006-08-13 18:11:27 +0000 | [diff] [blame] | 1438 | setint(dict, "MAP_SHARED", MAP_SHARED); |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 1439 | #endif |
| 1440 | #ifdef MAP_PRIVATE |
Neal Norwitz | e9ac0bb | 2006-08-13 18:11:27 +0000 | [diff] [blame] | 1441 | setint(dict, "MAP_PRIVATE", MAP_PRIVATE); |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 1442 | #endif |
| 1443 | #ifdef MAP_DENYWRITE |
Neal Norwitz | e9ac0bb | 2006-08-13 18:11:27 +0000 | [diff] [blame] | 1444 | setint(dict, "MAP_DENYWRITE", MAP_DENYWRITE); |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 1445 | #endif |
| 1446 | #ifdef MAP_EXECUTABLE |
Neal Norwitz | e9ac0bb | 2006-08-13 18:11:27 +0000 | [diff] [blame] | 1447 | setint(dict, "MAP_EXECUTABLE", MAP_EXECUTABLE); |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 1448 | #endif |
Neal Norwitz | 0e6bc8c | 2006-02-05 05:45:43 +0000 | [diff] [blame] | 1449 | #ifdef MAP_ANONYMOUS |
Neal Norwitz | e9ac0bb | 2006-08-13 18:11:27 +0000 | [diff] [blame] | 1450 | setint(dict, "MAP_ANON", MAP_ANONYMOUS); |
| 1451 | setint(dict, "MAP_ANONYMOUS", MAP_ANONYMOUS); |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 1452 | #endif |
| 1453 | |
Neal Norwitz | e9ac0bb | 2006-08-13 18:11:27 +0000 | [diff] [blame] | 1454 | setint(dict, "PAGESIZE", (long)my_getpagesize()); |
Andrew M. Kuchling | 961fe17 | 2000-06-03 19:41:42 +0000 | [diff] [blame] | 1455 | |
Travis E. Oliphant | 8feafab | 2007-10-23 02:40:56 +0000 | [diff] [blame] | 1456 | setint(dict, "ALLOCATIONGRANULARITY", (long)my_getallocationgranularity()); |
| 1457 | |
Neal Norwitz | e9ac0bb | 2006-08-13 18:11:27 +0000 | [diff] [blame] | 1458 | setint(dict, "ACCESS_READ", ACCESS_READ); |
| 1459 | setint(dict, "ACCESS_WRITE", ACCESS_WRITE); |
| 1460 | setint(dict, "ACCESS_COPY", ACCESS_COPY); |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 1461 | } |