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