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