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); |
| 116 | if (m_obj->map_handle != INVALID_HANDLE_VALUE) |
| 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 | |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 133 | PyObject_Del(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 | } |
| 156 | if (self->map_handle != INVALID_HANDLE_VALUE) { |
Tim Peters | 8f9cc29 | 2006-02-17 00:00:20 +0000 | [diff] [blame] | 157 | CloseHandle(self->map_handle); |
Mark Hammond | 071864a | 2000-07-30 02:46:26 +0000 | [diff] [blame] | 158 | self->map_handle = INVALID_HANDLE_VALUE; |
| 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 { \ |
Guido van Rossum | 69c2b88 | 2003-04-09 19:31:02 +0000 | [diff] [blame] | 182 | if (self->map_handle == INVALID_HANDLE_VALUE) { \ |
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; |
Tim Peters | d6283b8 | 2001-05-09 18:48:26 +0000 | [diff] [blame] | 207 | return Py_BuildValue("c", 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. */ |
Guido van Rossum | b358a2c | 2007-07-16 19:29:02 +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 */ |
| 248 | if ((self->pos + num_bytes) > self->size) { |
| 249 | num_bytes -= (self->pos+num_bytes) - self->size; |
| 250 | } |
Guido van Rossum | e5aeaad | 2007-07-26 18:28:23 +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 * |
Fredrik Lundh | 54cf3dc | 2000-07-08 22:05:01 +0000 | [diff] [blame] | 257 | mmap_find_method(mmap_object *self, |
| 258 | PyObject *args) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 259 | { |
Martin v. Löwis | ad0a462 | 2006-02-16 14:30:23 +0000 | [diff] [blame] | 260 | Py_ssize_t start = self->pos; |
Fredrik Lundh | 54cf3dc | 2000-07-08 22:05:01 +0000 | [diff] [blame] | 261 | char *needle; |
Martin v. Löwis | cfe7e09 | 2006-02-17 06:59:14 +0000 | [diff] [blame] | 262 | Py_ssize_t len; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 263 | |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 264 | CHECK_VALID(NULL); |
Tim Peters | 8f9cc29 | 2006-02-17 00:00:20 +0000 | [diff] [blame] | 265 | if (!PyArg_ParseTuple(args, "s#|n:find", &needle, &len, &start)) { |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 266 | return NULL; |
| 267 | } else { |
Greg Stein | 834f4dd | 2001-05-14 09:32:26 +0000 | [diff] [blame] | 268 | char *p; |
| 269 | char *e = self->data + self->size; |
| 270 | |
| 271 | if (start < 0) |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 272 | start += self->size; |
Greg Stein | 834f4dd | 2001-05-14 09:32:26 +0000 | [diff] [blame] | 273 | if (start < 0) |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 274 | start = 0; |
Tim Peters | d401edd | 2001-05-14 23:19:12 +0000 | [diff] [blame] | 275 | else if ((size_t)start > self->size) |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 276 | start = self->size; |
Greg Stein | 834f4dd | 2001-05-14 09:32:26 +0000 | [diff] [blame] | 277 | |
Tim Peters | c9ffa06 | 2002-03-08 05:43:32 +0000 | [diff] [blame] | 278 | for (p = self->data + start; p + len <= e; ++p) { |
Martin v. Löwis | cfe7e09 | 2006-02-17 06:59:14 +0000 | [diff] [blame] | 279 | Py_ssize_t i; |
Tim Peters | c9ffa06 | 2002-03-08 05:43:32 +0000 | [diff] [blame] | 280 | for (i = 0; i < len && needle[i] == p[i]; ++i) |
| 281 | /* nothing */; |
| 282 | if (i == len) { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame^] | 283 | return PyLong_FromSsize_t(p - self->data); |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 284 | } |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 285 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame^] | 286 | return PyLong_FromLong(-1); |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 287 | } |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 288 | } |
| 289 | |
Tim Peters | ec0a5f0 | 2006-02-16 23:47:20 +0000 | [diff] [blame] | 290 | static int |
Sean Reifscheider | 54cf12b | 2007-09-17 17:55:36 +0000 | [diff] [blame] | 291 | is_writable(mmap_object *self) |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 292 | { |
| 293 | if (self->access != ACCESS_READ) |
Tim Peters | ec0a5f0 | 2006-02-16 23:47:20 +0000 | [diff] [blame] | 294 | return 1; |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 295 | PyErr_Format(PyExc_TypeError, "mmap can't modify a readonly memory map."); |
| 296 | return 0; |
| 297 | } |
| 298 | |
Tim Peters | ec0a5f0 | 2006-02-16 23:47:20 +0000 | [diff] [blame] | 299 | static int |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 300 | is_resizeable(mmap_object *self) |
| 301 | { |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 302 | if (self->exports > 0) { |
| 303 | PyErr_SetString(PyExc_BufferError, |
| 304 | "mmap can't resize with extant buffers exported."); |
| 305 | return 0; |
| 306 | } |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 307 | if ((self->access == ACCESS_WRITE) || (self->access == ACCESS_DEFAULT)) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 308 | return 1; |
Tim Peters | ec0a5f0 | 2006-02-16 23:47:20 +0000 | [diff] [blame] | 309 | PyErr_Format(PyExc_TypeError, |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 310 | "mmap can't resize a readonly or copy-on-write memory map."); |
| 311 | return 0; |
| 312 | } |
| 313 | |
| 314 | |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 315 | static PyObject * |
Fredrik Lundh | 54cf3dc | 2000-07-08 22:05:01 +0000 | [diff] [blame] | 316 | mmap_write_method(mmap_object *self, |
| 317 | PyObject *args) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 318 | { |
Martin v. Löwis | cfe7e09 | 2006-02-17 06:59:14 +0000 | [diff] [blame] | 319 | Py_ssize_t length; |
Fredrik Lundh | 54cf3dc | 2000-07-08 22:05:01 +0000 | [diff] [blame] | 320 | char *data; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 321 | |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 322 | CHECK_VALID(NULL); |
Tim Peters | 8f9cc29 | 2006-02-17 00:00:20 +0000 | [diff] [blame] | 323 | if (!PyArg_ParseTuple(args, "s#:write", &data, &length)) |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 324 | return(NULL); |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 325 | |
Sean Reifscheider | 54cf12b | 2007-09-17 17:55:36 +0000 | [diff] [blame] | 326 | if (!is_writable(self)) |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 327 | return NULL; |
| 328 | |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 329 | if ((self->pos + length) > self->size) { |
Tim Peters | 8f9cc29 | 2006-02-17 00:00:20 +0000 | [diff] [blame] | 330 | PyErr_SetString(PyExc_ValueError, "data out of range"); |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 331 | return NULL; |
| 332 | } |
Tim Peters | 8f9cc29 | 2006-02-17 00:00:20 +0000 | [diff] [blame] | 333 | memcpy(self->data+self->pos, data, length); |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 334 | self->pos = self->pos+length; |
Tim Peters | 8f9cc29 | 2006-02-17 00:00:20 +0000 | [diff] [blame] | 335 | Py_INCREF(Py_None); |
Tim Peters | 23721ee | 2006-02-16 23:50:16 +0000 | [diff] [blame] | 336 | return Py_None; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | static PyObject * |
Fredrik Lundh | 54cf3dc | 2000-07-08 22:05:01 +0000 | [diff] [blame] | 340 | mmap_write_byte_method(mmap_object *self, |
| 341 | PyObject *args) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 342 | { |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 343 | char value; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 344 | |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 345 | CHECK_VALID(NULL); |
Tim Peters | 8f9cc29 | 2006-02-17 00:00:20 +0000 | [diff] [blame] | 346 | if (!PyArg_ParseTuple(args, "c:write_byte", &value)) |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 347 | return(NULL); |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 348 | |
Sean Reifscheider | 54cf12b | 2007-09-17 17:55:36 +0000 | [diff] [blame] | 349 | if (!is_writable(self)) |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 350 | return NULL; |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 351 | *(self->data+self->pos) = value; |
| 352 | self->pos += 1; |
Tim Peters | 8f9cc29 | 2006-02-17 00:00:20 +0000 | [diff] [blame] | 353 | Py_INCREF(Py_None); |
Tim Peters | 23721ee | 2006-02-16 23:50:16 +0000 | [diff] [blame] | 354 | return Py_None; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 355 | } |
Tim Peters | ec0a5f0 | 2006-02-16 23:47:20 +0000 | [diff] [blame] | 356 | |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 357 | static PyObject * |
Fredrik Lundh | 54cf3dc | 2000-07-08 22:05:01 +0000 | [diff] [blame] | 358 | mmap_size_method(mmap_object *self, |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 359 | PyObject *unused) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 360 | { |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 361 | CHECK_VALID(NULL); |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 362 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 363 | #ifdef MS_WINDOWS |
Mark Hammond | 071864a | 2000-07-30 02:46:26 +0000 | [diff] [blame] | 364 | if (self->file_handle != INVALID_HANDLE_VALUE) { |
Martin v. Löwis | 1518607 | 2006-02-18 12:38:35 +0000 | [diff] [blame] | 365 | DWORD low,high; |
| 366 | PY_LONG_LONG size; |
| 367 | low = GetFileSize(self->file_handle, &high); |
| 368 | if (low == INVALID_FILE_SIZE) { |
| 369 | /* It might be that the function appears to have failed, |
| 370 | when indeed its size equals INVALID_FILE_SIZE */ |
| 371 | DWORD error = GetLastError(); |
| 372 | if (error != NO_ERROR) |
| 373 | return PyErr_SetFromWindowsErr(error); |
| 374 | } |
| 375 | if (!high && low < LONG_MAX) |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame^] | 376 | return PyLong_FromLong((long)low); |
Martin v. Löwis | 1518607 | 2006-02-18 12:38:35 +0000 | [diff] [blame] | 377 | size = (((PY_LONG_LONG)high)<<32) + low; |
| 378 | return PyLong_FromLongLong(size); |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 379 | } else { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame^] | 380 | return PyLong_FromSsize_t(self->size); |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 381 | } |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 382 | #endif /* MS_WINDOWS */ |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 383 | |
| 384 | #ifdef UNIX |
Andrew M. Kuchling | 7b9fb92 | 2000-06-17 22:41:22 +0000 | [diff] [blame] | 385 | { |
| 386 | struct stat buf; |
| 387 | if (-1 == fstat(self->fd, &buf)) { |
| 388 | PyErr_SetFromErrno(mmap_module_error); |
| 389 | return NULL; |
| 390 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame^] | 391 | return PyLong_FromSsize_t(buf.st_size); |
Andrew M. Kuchling | 7b9fb92 | 2000-06-17 22:41:22 +0000 | [diff] [blame] | 392 | } |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 393 | #endif /* UNIX */ |
| 394 | } |
| 395 | |
| 396 | /* This assumes that you want the entire file mapped, |
| 397 | / and when recreating the map will make the new file |
| 398 | / have the new size |
| 399 | / |
| 400 | / Is this really necessary? This could easily be done |
| 401 | / from python by just closing and re-opening with the |
| 402 | / new size? |
| 403 | */ |
| 404 | |
| 405 | static PyObject * |
Fredrik Lundh | 54cf3dc | 2000-07-08 22:05:01 +0000 | [diff] [blame] | 406 | mmap_resize_method(mmap_object *self, |
| 407 | PyObject *args) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 408 | { |
Martin v. Löwis | cfe7e09 | 2006-02-17 06:59:14 +0000 | [diff] [blame] | 409 | Py_ssize_t new_size; |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 410 | CHECK_VALID(NULL); |
Martin v. Löwis | cfe7e09 | 2006-02-17 06:59:14 +0000 | [diff] [blame] | 411 | if (!PyArg_ParseTuple(args, "n:resize", &new_size) || |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 412 | !is_resizeable(self)) { |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 413 | return NULL; |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 414 | #ifdef MS_WINDOWS |
Tim Peters | ec0a5f0 | 2006-02-16 23:47:20 +0000 | [diff] [blame] | 415 | } else { |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 416 | DWORD dwErrCode = 0; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 417 | DWORD off_hi, off_lo, newSizeLow, newSizeHigh; |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 418 | /* First, unmap the file view */ |
Tim Peters | 8f9cc29 | 2006-02-17 00:00:20 +0000 | [diff] [blame] | 419 | UnmapViewOfFile(self->data); |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 420 | /* Close the mapping object */ |
Tim Peters | 8f9cc29 | 2006-02-17 00:00:20 +0000 | [diff] [blame] | 421 | CloseHandle(self->map_handle); |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 422 | /* Move to the desired EOF position */ |
Martin v. Löwis | 1518607 | 2006-02-18 12:38:35 +0000 | [diff] [blame] | 423 | #if SIZEOF_SIZE_T > 4 |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 424 | newSizeHigh = (DWORD)((self->offset + new_size) >> 32); |
| 425 | newSizeLow = (DWORD)((self->offset + new_size) & 0xFFFFFFFF); |
| 426 | off_hi = (DWORD)(self->offset >> 32); |
| 427 | off_lo = (DWORD)(self->offset & 0xFFFFFFFF); |
Martin v. Löwis | 1518607 | 2006-02-18 12:38:35 +0000 | [diff] [blame] | 428 | #else |
| 429 | newSizeHigh = 0; |
Martin v. Löwis | 5bb8a15 | 2006-02-18 12:49:49 +0000 | [diff] [blame] | 430 | newSizeLow = (DWORD)new_size; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 431 | off_hi = 0; |
| 432 | off_lo = (DWORD)self->offset; |
Martin v. Löwis | 1518607 | 2006-02-18 12:38:35 +0000 | [diff] [blame] | 433 | #endif |
Tim Peters | 8f9cc29 | 2006-02-17 00:00:20 +0000 | [diff] [blame] | 434 | SetFilePointer(self->file_handle, |
Martin v. Löwis | 1518607 | 2006-02-18 12:38:35 +0000 | [diff] [blame] | 435 | newSizeLow, &newSizeHigh, FILE_BEGIN); |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 436 | /* Change the size of the file */ |
Tim Peters | 8f9cc29 | 2006-02-17 00:00:20 +0000 | [diff] [blame] | 437 | SetEndOfFile(self->file_handle); |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 438 | /* Create another mapping object and remap the file view */ |
Tim Peters | 8f9cc29 | 2006-02-17 00:00:20 +0000 | [diff] [blame] | 439 | self->map_handle = CreateFileMapping( |
Mark Hammond | 071864a | 2000-07-30 02:46:26 +0000 | [diff] [blame] | 440 | self->file_handle, |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 441 | NULL, |
| 442 | PAGE_READWRITE, |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 443 | 0, |
| 444 | 0, |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 445 | self->tagname); |
| 446 | if (self->map_handle != NULL) { |
Tim Peters | 8f9cc29 | 2006-02-17 00:00:20 +0000 | [diff] [blame] | 447 | self->data = (char *) MapViewOfFile(self->map_handle, |
| 448 | FILE_MAP_WRITE, |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 449 | off_hi, |
| 450 | off_lo, |
| 451 | new_size); |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 452 | if (self->data != NULL) { |
| 453 | self->size = new_size; |
Tim Peters | 8f9cc29 | 2006-02-17 00:00:20 +0000 | [diff] [blame] | 454 | Py_INCREF(Py_None); |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 455 | return Py_None; |
| 456 | } else { |
| 457 | dwErrCode = GetLastError(); |
| 458 | } |
| 459 | } else { |
| 460 | dwErrCode = GetLastError(); |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 461 | } |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 462 | PyErr_SetFromWindowsErr(dwErrCode); |
Tim Peters | 23721ee | 2006-02-16 23:50:16 +0000 | [diff] [blame] | 463 | return NULL; |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 464 | #endif /* MS_WINDOWS */ |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 465 | |
| 466 | #ifdef UNIX |
Tim Peters | ec0a5f0 | 2006-02-16 23:47:20 +0000 | [diff] [blame] | 467 | #ifndef HAVE_MREMAP |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 468 | } else { |
| 469 | PyErr_SetString(PyExc_SystemError, |
| 470 | "mmap: resizing not available--no mremap()"); |
| 471 | return NULL; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 472 | #else |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 473 | } else { |
Armin Rigo | 335ffe8 | 2005-09-20 19:04:02 +0000 | [diff] [blame] | 474 | void *newmap; |
| 475 | |
Georg Brandl | 38387b8 | 2005-08-24 07:17:40 +0000 | [diff] [blame] | 476 | if (ftruncate(self->fd, new_size) == -1) { |
| 477 | PyErr_SetFromErrno(mmap_module_error); |
| 478 | return NULL; |
| 479 | } |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 480 | |
Andrew M. Kuchling | 6fef30e | 2000-06-18 14:51:21 +0000 | [diff] [blame] | 481 | #ifdef MREMAP_MAYMOVE |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 482 | newmap = mremap(self->data, self->size, new_size, MREMAP_MAYMOVE); |
Andrew M. Kuchling | 6fef30e | 2000-06-18 14:51:21 +0000 | [diff] [blame] | 483 | #else |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 484 | newmap = mremap(self->data, self->size, new_size, 0); |
Andrew M. Kuchling | 6fef30e | 2000-06-18 14:51:21 +0000 | [diff] [blame] | 485 | #endif |
Tim Peters | ec0a5f0 | 2006-02-16 23:47:20 +0000 | [diff] [blame] | 486 | if (newmap == (void *)-1) |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 487 | { |
| 488 | PyErr_SetFromErrno(mmap_module_error); |
| 489 | return NULL; |
| 490 | } |
| 491 | self->data = newmap; |
| 492 | self->size = new_size; |
| 493 | Py_INCREF(Py_None); |
| 494 | return Py_None; |
Andrew M. Kuchling | 6fef30e | 2000-06-18 14:51:21 +0000 | [diff] [blame] | 495 | #endif /* HAVE_MREMAP */ |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 496 | #endif /* UNIX */ |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 497 | } |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | static PyObject * |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 501 | mmap_tell_method(mmap_object *self, PyObject *unused) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 502 | { |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 503 | CHECK_VALID(NULL); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame^] | 504 | return PyLong_FromSize_t(self->pos); |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 505 | } |
| 506 | |
| 507 | static PyObject * |
Fredrik Lundh | 54cf3dc | 2000-07-08 22:05:01 +0000 | [diff] [blame] | 508 | mmap_flush_method(mmap_object *self, PyObject *args) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 509 | { |
Martin v. Löwis | ad0a462 | 2006-02-16 14:30:23 +0000 | [diff] [blame] | 510 | Py_ssize_t offset = 0; |
| 511 | Py_ssize_t size = self->size; |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 512 | CHECK_VALID(NULL); |
Tim Peters | f288295 | 2006-02-17 01:07:39 +0000 | [diff] [blame] | 513 | if (!PyArg_ParseTuple(args, "|nn:flush", &offset, &size)) |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 514 | return NULL; |
Tim Peters | f288295 | 2006-02-17 01:07:39 +0000 | [diff] [blame] | 515 | if ((size_t)(offset + size) > self->size) { |
Tim Peters | 8f9cc29 | 2006-02-17 00:00:20 +0000 | [diff] [blame] | 516 | PyErr_SetString(PyExc_ValueError, "flush values out of range"); |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 517 | return NULL; |
| 518 | } else { |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 519 | #ifdef MS_WINDOWS |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame^] | 520 | return PyLong_FromLong((long) |
Tim Peters | 23721ee | 2006-02-16 23:50:16 +0000 | [diff] [blame] | 521 | FlushViewOfFile(self->data+offset, size)); |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 522 | #endif /* MS_WINDOWS */ |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 523 | #ifdef UNIX |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 524 | /* XXX semantics of return value? */ |
| 525 | /* XXX flags for msync? */ |
| 526 | if (-1 == msync(self->data + offset, size, |
Andrew M. Kuchling | 9bc5f33 | 2000-06-18 04:25:08 +0000 | [diff] [blame] | 527 | MS_SYNC)) |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 528 | { |
| 529 | PyErr_SetFromErrno(mmap_module_error); |
| 530 | return NULL; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 531 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame^] | 532 | return PyLong_FromLong(0); |
Tim Peters | ec0a5f0 | 2006-02-16 23:47:20 +0000 | [diff] [blame] | 533 | #endif /* UNIX */ |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 534 | } |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 535 | } |
| 536 | |
| 537 | static PyObject * |
Fredrik Lundh | 54cf3dc | 2000-07-08 22:05:01 +0000 | [diff] [blame] | 538 | mmap_seek_method(mmap_object *self, PyObject *args) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 539 | { |
Martin v. Löwis | cfe7e09 | 2006-02-17 06:59:14 +0000 | [diff] [blame] | 540 | Py_ssize_t dist; |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 541 | int how=0; |
| 542 | CHECK_VALID(NULL); |
Martin v. Löwis | cfe7e09 | 2006-02-17 06:59:14 +0000 | [diff] [blame] | 543 | if (!PyArg_ParseTuple(args, "n|i:seek", &dist, &how)) |
Tim Peters | 8f9cc29 | 2006-02-17 00:00:20 +0000 | [diff] [blame] | 544 | return NULL; |
| 545 | else { |
Andrew M. Kuchling | 70d2742 | 2000-06-18 04:45:14 +0000 | [diff] [blame] | 546 | size_t where; |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 547 | switch (how) { |
Andrew M. Kuchling | 70d2742 | 2000-06-18 04:45:14 +0000 | [diff] [blame] | 548 | case 0: /* relative to start */ |
| 549 | if (dist < 0) |
| 550 | goto onoutofrange; |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 551 | where = dist; |
| 552 | break; |
Andrew M. Kuchling | 70d2742 | 2000-06-18 04:45:14 +0000 | [diff] [blame] | 553 | case 1: /* relative to current position */ |
Martin v. Löwis | cfe7e09 | 2006-02-17 06:59:14 +0000 | [diff] [blame] | 554 | if ((Py_ssize_t)self->pos + dist < 0) |
Andrew M. Kuchling | 70d2742 | 2000-06-18 04:45:14 +0000 | [diff] [blame] | 555 | goto onoutofrange; |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 556 | where = self->pos + dist; |
| 557 | break; |
Andrew M. Kuchling | 70d2742 | 2000-06-18 04:45:14 +0000 | [diff] [blame] | 558 | case 2: /* relative to end */ |
Martin v. Löwis | cfe7e09 | 2006-02-17 06:59:14 +0000 | [diff] [blame] | 559 | if ((Py_ssize_t)self->size + dist < 0) |
Andrew M. Kuchling | 70d2742 | 2000-06-18 04:45:14 +0000 | [diff] [blame] | 560 | goto onoutofrange; |
| 561 | where = self->size + dist; |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 562 | break; |
| 563 | default: |
Tim Peters | 8f9cc29 | 2006-02-17 00:00:20 +0000 | [diff] [blame] | 564 | PyErr_SetString(PyExc_ValueError, "unknown seek type"); |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 565 | return NULL; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 566 | } |
Andrew M. Kuchling | 70d2742 | 2000-06-18 04:45:14 +0000 | [diff] [blame] | 567 | if (where > self->size) |
| 568 | goto onoutofrange; |
| 569 | self->pos = where; |
Tim Peters | 8f9cc29 | 2006-02-17 00:00:20 +0000 | [diff] [blame] | 570 | Py_INCREF(Py_None); |
Tim Peters | 23721ee | 2006-02-16 23:50:16 +0000 | [diff] [blame] | 571 | return Py_None; |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 572 | } |
Andrew M. Kuchling | 70d2742 | 2000-06-18 04:45:14 +0000 | [diff] [blame] | 573 | |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 574 | onoutofrange: |
Tim Peters | 8f9cc29 | 2006-02-17 00:00:20 +0000 | [diff] [blame] | 575 | PyErr_SetString(PyExc_ValueError, "seek out of range"); |
Andrew M. Kuchling | 70d2742 | 2000-06-18 04:45:14 +0000 | [diff] [blame] | 576 | return NULL; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 577 | } |
| 578 | |
| 579 | static PyObject * |
Fredrik Lundh | 54cf3dc | 2000-07-08 22:05:01 +0000 | [diff] [blame] | 580 | mmap_move_method(mmap_object *self, PyObject *args) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 581 | { |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 582 | unsigned long dest, src, count; |
| 583 | CHECK_VALID(NULL); |
Tim Peters | 8f9cc29 | 2006-02-17 00:00:20 +0000 | [diff] [blame] | 584 | if (!PyArg_ParseTuple(args, "kkk:move", &dest, &src, &count) || |
Sean Reifscheider | 54cf12b | 2007-09-17 17:55:36 +0000 | [diff] [blame] | 585 | !is_writable(self)) { |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 586 | return NULL; |
| 587 | } else { |
| 588 | /* bounds check the values */ |
| 589 | if (/* end of source after end of data?? */ |
| 590 | ((src+count) > self->size) |
| 591 | /* dest will fit? */ |
| 592 | || (dest+count > self->size)) { |
Tim Peters | 8f9cc29 | 2006-02-17 00:00:20 +0000 | [diff] [blame] | 593 | PyErr_SetString(PyExc_ValueError, |
| 594 | "source or destination out of range"); |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 595 | return NULL; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 596 | } else { |
Tim Peters | 8f9cc29 | 2006-02-17 00:00:20 +0000 | [diff] [blame] | 597 | memmove(self->data+dest, self->data+src, count); |
| 598 | Py_INCREF(Py_None); |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 599 | return Py_None; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 600 | } |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 601 | } |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 602 | } |
| 603 | |
| 604 | static struct PyMethodDef mmap_object_methods[] = { |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 605 | {"close", (PyCFunction) mmap_close_method, METH_NOARGS}, |
Martin v. Löwis | 43b936d | 2002-01-17 23:15:58 +0000 | [diff] [blame] | 606 | {"find", (PyCFunction) mmap_find_method, METH_VARARGS}, |
| 607 | {"flush", (PyCFunction) mmap_flush_method, METH_VARARGS}, |
| 608 | {"move", (PyCFunction) mmap_move_method, METH_VARARGS}, |
| 609 | {"read", (PyCFunction) mmap_read_method, METH_VARARGS}, |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 610 | {"read_byte", (PyCFunction) mmap_read_byte_method, METH_NOARGS}, |
| 611 | {"readline", (PyCFunction) mmap_read_line_method, METH_NOARGS}, |
Martin v. Löwis | 43b936d | 2002-01-17 23:15:58 +0000 | [diff] [blame] | 612 | {"resize", (PyCFunction) mmap_resize_method, METH_VARARGS}, |
| 613 | {"seek", (PyCFunction) mmap_seek_method, METH_VARARGS}, |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 614 | {"size", (PyCFunction) mmap_size_method, METH_NOARGS}, |
| 615 | {"tell", (PyCFunction) mmap_tell_method, METH_NOARGS}, |
Martin v. Löwis | 43b936d | 2002-01-17 23:15:58 +0000 | [diff] [blame] | 616 | {"write", (PyCFunction) mmap_write_method, METH_VARARGS}, |
| 617 | {"write_byte", (PyCFunction) mmap_write_byte_method, METH_VARARGS}, |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 618 | {NULL, NULL} /* sentinel */ |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 619 | }; |
| 620 | |
| 621 | /* Functions for treating an mmap'ed file as a buffer */ |
| 622 | |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 623 | static int |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 624 | mmap_buffer_getbuf(mmap_object *self, Py_buffer *view, int flags) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 625 | { |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 626 | CHECK_VALID(-1); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 627 | if (PyBuffer_FillInfo(view, self->data, self->size, |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 628 | (self->access == ACCESS_READ), flags) < 0) |
| 629 | return -1; |
| 630 | self->exports++; |
| 631 | return 0; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 632 | } |
| 633 | |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 634 | static void |
Travis E. Oliphant | 8ae62b6 | 2007-09-23 02:00:13 +0000 | [diff] [blame] | 635 | mmap_buffer_releasebuf(mmap_object *self, Py_buffer *view) |
Tim Peters | ec0a5f0 | 2006-02-16 23:47:20 +0000 | [diff] [blame] | 636 | { |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 637 | self->exports--; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 638 | } |
| 639 | |
| 640 | static PyObject * |
Fredrik Lundh | 54cf3dc | 2000-07-08 22:05:01 +0000 | [diff] [blame] | 641 | mmap_object_getattr(mmap_object *self, char *name) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 642 | { |
Tim Peters | 8f9cc29 | 2006-02-17 00:00:20 +0000 | [diff] [blame] | 643 | return Py_FindMethod(mmap_object_methods, (PyObject *)self, name); |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 644 | } |
| 645 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 646 | static Py_ssize_t |
Fredrik Lundh | 54cf3dc | 2000-07-08 22:05:01 +0000 | [diff] [blame] | 647 | mmap_length(mmap_object *self) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 648 | { |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 649 | CHECK_VALID(-1); |
| 650 | return self->size; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 651 | } |
| 652 | |
| 653 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 654 | mmap_item(mmap_object *self, Py_ssize_t i) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 655 | { |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 656 | CHECK_VALID(NULL); |
Guido van Rossum | ce8e1dc | 2000-07-01 00:51:51 +0000 | [diff] [blame] | 657 | if (i < 0 || (size_t)i >= self->size) { |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 658 | PyErr_SetString(PyExc_IndexError, "mmap index out of range"); |
| 659 | return NULL; |
| 660 | } |
Guido van Rossum | b358a2c | 2007-07-16 19:29:02 +0000 | [diff] [blame] | 661 | return PyBytes_FromStringAndSize(self->data + i, 1); |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 662 | } |
| 663 | |
| 664 | static PyObject * |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 665 | mmap_subscript(mmap_object *self, PyObject *item) |
| 666 | { |
| 667 | CHECK_VALID(NULL); |
| 668 | if (PyIndex_Check(item)) { |
| 669 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
| 670 | if (i == -1 && PyErr_Occurred()) |
| 671 | return NULL; |
| 672 | if (i < 0) |
| 673 | i += self->size; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 674 | if (i < 0 || (size_t)i > self->size) { |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 675 | PyErr_SetString(PyExc_IndexError, |
| 676 | "mmap index out of range"); |
| 677 | return NULL; |
| 678 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame^] | 679 | return PyLong_FromLong(Py_CHARMASK(self->data[i])); |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 680 | } |
| 681 | else if (PySlice_Check(item)) { |
| 682 | Py_ssize_t start, stop, step, slicelen; |
| 683 | |
| 684 | if (PySlice_GetIndicesEx((PySliceObject *)item, self->size, |
| 685 | &start, &stop, &step, &slicelen) < 0) { |
| 686 | return NULL; |
| 687 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 688 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 689 | if (slicelen <= 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 690 | return PyString_FromStringAndSize("", 0); |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 691 | else if (step == 1) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 692 | return PyString_FromStringAndSize(self->data + start, |
| 693 | slicelen); |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 694 | else { |
| 695 | char *result_buf = (char *)PyMem_Malloc(slicelen); |
| 696 | Py_ssize_t cur, i; |
| 697 | PyObject *result; |
| 698 | |
| 699 | if (result_buf == NULL) |
| 700 | return PyErr_NoMemory(); |
| 701 | for (cur = start, i = 0; i < slicelen; |
| 702 | cur += step, i++) { |
| 703 | result_buf[i] = self->data[cur]; |
| 704 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 705 | result = PyString_FromStringAndSize(result_buf, |
| 706 | slicelen); |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 707 | PyMem_Free(result_buf); |
| 708 | return result; |
| 709 | } |
| 710 | } |
| 711 | else { |
| 712 | PyErr_SetString(PyExc_TypeError, |
| 713 | "mmap indices must be integers"); |
| 714 | return NULL; |
| 715 | } |
| 716 | } |
| 717 | |
| 718 | static PyObject * |
Fredrik Lundh | 54cf3dc | 2000-07-08 22:05:01 +0000 | [diff] [blame] | 719 | mmap_concat(mmap_object *self, PyObject *bb) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 720 | { |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 721 | CHECK_VALID(NULL); |
| 722 | PyErr_SetString(PyExc_SystemError, |
| 723 | "mmaps don't support concatenation"); |
| 724 | return NULL; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 725 | } |
| 726 | |
| 727 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 728 | mmap_repeat(mmap_object *self, Py_ssize_t n) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 729 | { |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 730 | CHECK_VALID(NULL); |
| 731 | PyErr_SetString(PyExc_SystemError, |
| 732 | "mmaps don't support repeat operation"); |
| 733 | return NULL; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 734 | } |
| 735 | |
| 736 | static int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 737 | 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] | 738 | { |
Guido van Rossum | 36d4f8b | 2000-04-10 21:34:37 +0000 | [diff] [blame] | 739 | const char *buf; |
Tim Peters | ec0a5f0 | 2006-02-16 23:47:20 +0000 | [diff] [blame] | 740 | |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 741 | CHECK_VALID(-1); |
Guido van Rossum | ce8e1dc | 2000-07-01 00:51:51 +0000 | [diff] [blame] | 742 | if (i < 0 || (size_t)i >= self->size) { |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 743 | PyErr_SetString(PyExc_IndexError, "mmap index out of range"); |
| 744 | return -1; |
| 745 | } |
Thomas Wouters | 1baac72 | 2001-07-16 15:47:36 +0000 | [diff] [blame] | 746 | if (v == NULL) { |
| 747 | PyErr_SetString(PyExc_TypeError, |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 748 | "mmap object doesn't support item deletion"); |
Thomas Wouters | 1baac72 | 2001-07-16 15:47:36 +0000 | [diff] [blame] | 749 | return -1; |
| 750 | } |
Guido van Rossum | b358a2c | 2007-07-16 19:29:02 +0000 | [diff] [blame] | 751 | if (! (PyBytes_Check(v) && PyBytes_Size(v)==1) ) { |
Tim Peters | ec0a5f0 | 2006-02-16 23:47:20 +0000 | [diff] [blame] | 752 | PyErr_SetString(PyExc_IndexError, |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 753 | "mmap assignment must be length-1 bytes()"); |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 754 | return -1; |
| 755 | } |
Sean Reifscheider | 54cf12b | 2007-09-17 17:55:36 +0000 | [diff] [blame] | 756 | if (!is_writable(self)) |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 757 | return -1; |
Guido van Rossum | b358a2c | 2007-07-16 19:29:02 +0000 | [diff] [blame] | 758 | buf = PyBytes_AsString(v); |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 759 | self->data[i] = buf[0]; |
| 760 | return 0; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 761 | } |
| 762 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 763 | static int |
| 764 | mmap_ass_subscript(mmap_object *self, PyObject *item, PyObject *value) |
| 765 | { |
| 766 | CHECK_VALID(-1); |
| 767 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 768 | if (!is_writable(self)) |
| 769 | return -1; |
| 770 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 771 | if (PyIndex_Check(item)) { |
| 772 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 773 | Py_ssize_t v; |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 774 | |
| 775 | if (i == -1 && PyErr_Occurred()) |
| 776 | return -1; |
| 777 | if (i < 0) |
| 778 | i += self->size; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 779 | if (i < 0 || (size_t)i > self->size) { |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 780 | PyErr_SetString(PyExc_IndexError, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 781 | "mmap index out of range"); |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 782 | return -1; |
| 783 | } |
| 784 | if (value == NULL) { |
| 785 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 786 | "mmap doesn't support item deletion"); |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 787 | return -1; |
| 788 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 789 | if (!PyIndex_Check(value)) { |
| 790 | PyErr_SetString(PyExc_TypeError, |
| 791 | "mmap item value must be an int"); |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 792 | return -1; |
| 793 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 794 | v = PyNumber_AsSsize_t(value, PyExc_TypeError); |
| 795 | if (v == -1 && PyErr_Occurred()) |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 796 | return -1; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 797 | if (v < 0 || v > 255) { |
| 798 | PyErr_SetString(PyExc_ValueError, |
| 799 | "mmap item value must be " |
| 800 | "in range(0, 256)"); |
| 801 | return -1; |
| 802 | } |
| 803 | self->data[i] = v; |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 804 | return 0; |
| 805 | } |
| 806 | else if (PySlice_Check(item)) { |
| 807 | Py_ssize_t start, stop, step, slicelen; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 808 | Py_buffer vbuf; |
| 809 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 810 | if (PySlice_GetIndicesEx((PySliceObject *)item, |
| 811 | self->size, &start, &stop, |
| 812 | &step, &slicelen) < 0) { |
| 813 | return -1; |
| 814 | } |
| 815 | if (value == NULL) { |
| 816 | PyErr_SetString(PyExc_TypeError, |
| 817 | "mmap object doesn't support slice deletion"); |
| 818 | return -1; |
| 819 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 820 | if (PyObject_GetBuffer(value, &vbuf, PyBUF_SIMPLE) < 0) |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 821 | return -1; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 822 | if (vbuf.len != slicelen) { |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 823 | PyErr_SetString(PyExc_IndexError, |
| 824 | "mmap slice assignment is wrong size"); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 825 | PyObject_ReleaseBuffer(value, &vbuf); |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 826 | return -1; |
| 827 | } |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 828 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 829 | if (slicelen == 0) { |
| 830 | } |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 831 | else if (step == 1) { |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 832 | memcpy(self->data + start, vbuf.buf, slicelen); |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 833 | } |
| 834 | else { |
| 835 | Py_ssize_t cur, i; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 836 | |
| 837 | for (cur = start, i = 0; |
| 838 | i < slicelen; |
| 839 | cur += step, i++) |
| 840 | { |
| 841 | self->data[cur] = ((char *)vbuf.buf)[i]; |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 842 | } |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 843 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 844 | PyObject_ReleaseBuffer(value, &vbuf); |
| 845 | return 0; |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 846 | } |
| 847 | else { |
| 848 | PyErr_SetString(PyExc_TypeError, |
| 849 | "mmap indices must be integer"); |
| 850 | return -1; |
| 851 | } |
| 852 | } |
| 853 | |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 854 | static PySequenceMethods mmap_as_sequence = { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 855 | (lenfunc)mmap_length, /*sq_length*/ |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 856 | (binaryfunc)mmap_concat, /*sq_concat*/ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 857 | (ssizeargfunc)mmap_repeat, /*sq_repeat*/ |
| 858 | (ssizeargfunc)mmap_item, /*sq_item*/ |
Thomas Wouters | d2cf20e | 2007-08-30 22:57:53 +0000 | [diff] [blame] | 859 | 0, /*sq_slice*/ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 860 | (ssizeobjargproc)mmap_ass_item, /*sq_ass_item*/ |
Thomas Wouters | d2cf20e | 2007-08-30 22:57:53 +0000 | [diff] [blame] | 861 | 0, /*sq_ass_slice*/ |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 862 | }; |
| 863 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 864 | static PyMappingMethods mmap_as_mapping = { |
| 865 | (lenfunc)mmap_length, |
| 866 | (binaryfunc)mmap_subscript, |
| 867 | (objobjargproc)mmap_ass_subscript, |
| 868 | }; |
| 869 | |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 870 | static PyBufferProcs mmap_as_buffer = { |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 871 | (getbufferproc)mmap_buffer_getbuf, |
| 872 | (releasebufferproc)mmap_buffer_releasebuf, |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 873 | }; |
| 874 | |
| 875 | static PyTypeObject mmap_object_type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 876 | PyVarObject_HEAD_INIT(0, 0) /* patched in module init */ |
Guido van Rossum | 1464839 | 2001-12-08 18:02:58 +0000 | [diff] [blame] | 877 | "mmap.mmap", /* tp_name */ |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 878 | sizeof(mmap_object), /* tp_size */ |
| 879 | 0, /* tp_itemsize */ |
| 880 | /* methods */ |
| 881 | (destructor) mmap_object_dealloc, /* tp_dealloc */ |
| 882 | 0, /* tp_print */ |
| 883 | (getattrfunc) mmap_object_getattr, /* tp_getattr */ |
| 884 | 0, /* tp_setattr */ |
| 885 | 0, /* tp_compare */ |
| 886 | 0, /* tp_repr */ |
| 887 | 0, /* tp_as_number */ |
| 888 | &mmap_as_sequence, /*tp_as_sequence*/ |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 889 | &mmap_as_mapping, /*tp_as_mapping*/ |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 890 | 0, /*tp_hash*/ |
| 891 | 0, /*tp_call*/ |
| 892 | 0, /*tp_str*/ |
| 893 | 0, /*tp_getattro*/ |
| 894 | 0, /*tp_setattro*/ |
| 895 | &mmap_as_buffer, /*tp_as_buffer*/ |
Guido van Rossum | 3cf5b1e | 2006-07-27 21:53:35 +0000 | [diff] [blame] | 896 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 897 | 0, /*tp_doc*/ |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 898 | }; |
| 899 | |
Andrew M. Kuchling | 70d2742 | 2000-06-18 04:45:14 +0000 | [diff] [blame] | 900 | |
| 901 | /* extract the map size from the given PyObject |
| 902 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 903 | Returns -1 on error, with an appropriate Python exception raised. On |
Andrew M. Kuchling | 70d2742 | 2000-06-18 04:45:14 +0000 | [diff] [blame] | 904 | success, the map size is returned. */ |
Martin v. Löwis | cfe7e09 | 2006-02-17 06:59:14 +0000 | [diff] [blame] | 905 | static Py_ssize_t |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 906 | _GetMapSize(PyObject *o, const char* param) |
Andrew M. Kuchling | 70d2742 | 2000-06-18 04:45:14 +0000 | [diff] [blame] | 907 | { |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 908 | if (o == NULL) |
| 909 | return 0; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 910 | if (PyIndex_Check(o)) { |
| 911 | Py_ssize_t i = PyNumber_AsSsize_t(o, PyExc_OverflowError); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 912 | if (i==-1 && PyErr_Occurred()) |
Andrew M. Kuchling | 70d2742 | 2000-06-18 04:45:14 +0000 | [diff] [blame] | 913 | return -1; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 914 | if (i < 0) { |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 915 | PyErr_Format(PyExc_OverflowError, |
| 916 | "memory mapped %s must be positive", |
| 917 | param); |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 918 | return -1; |
| 919 | } |
Martin v. Löwis | cfe7e09 | 2006-02-17 06:59:14 +0000 | [diff] [blame] | 920 | return i; |
Andrew M. Kuchling | 70d2742 | 2000-06-18 04:45:14 +0000 | [diff] [blame] | 921 | } |
Andrew M. Kuchling | 70d2742 | 2000-06-18 04:45:14 +0000 | [diff] [blame] | 922 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 923 | PyErr_SetString(PyExc_TypeError, "map size must be an integral value"); |
Andrew M. Kuchling | 70d2742 | 2000-06-18 04:45:14 +0000 | [diff] [blame] | 924 | return -1; |
| 925 | } |
| 926 | |
Tim Peters | ec0a5f0 | 2006-02-16 23:47:20 +0000 | [diff] [blame] | 927 | #ifdef UNIX |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 928 | static PyObject * |
Fredrik Lundh | 54cf3dc | 2000-07-08 22:05:01 +0000 | [diff] [blame] | 929 | new_mmap_object(PyObject *self, PyObject *args, PyObject *kwdict) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 930 | { |
Neal Norwitz | b567392 | 2002-09-05 21:48:07 +0000 | [diff] [blame] | 931 | #ifdef HAVE_FSTAT |
| 932 | struct stat st; |
| 933 | #endif |
Fredrik Lundh | 54cf3dc | 2000-07-08 22:05:01 +0000 | [diff] [blame] | 934 | mmap_object *m_obj; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 935 | PyObject *map_size_obj = NULL, *offset_obj = NULL; |
| 936 | Py_ssize_t map_size, offset; |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 937 | int fd, flags = MAP_SHARED, prot = PROT_WRITE | PROT_READ; |
Neal Norwitz | 0e6bc8c | 2006-02-05 05:45:43 +0000 | [diff] [blame] | 938 | int devzero = -1; |
Neal Norwitz | 8856fb7 | 2005-12-18 03:34:22 +0000 | [diff] [blame] | 939 | int access = (int)ACCESS_DEFAULT; |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 940 | static char *keywords[] = {"fileno", "length", |
Tim Peters | ec0a5f0 | 2006-02-16 23:47:20 +0000 | [diff] [blame] | 941 | "flags", "prot", |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 942 | "access", "offset", NULL}; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 943 | |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 944 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iO|iiiO", keywords, |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 945 | &fd, &map_size_obj, &flags, &prot, |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 946 | &access, &offset_obj)) |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 947 | return NULL; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 948 | map_size = _GetMapSize(map_size_obj, "size"); |
Andrew M. Kuchling | 70d2742 | 2000-06-18 04:45:14 +0000 | [diff] [blame] | 949 | if (map_size < 0) |
| 950 | return NULL; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 951 | offset = _GetMapSize(offset_obj, "offset"); |
| 952 | if (offset < 0) |
| 953 | return NULL; |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 954 | |
Tim Peters | ec0a5f0 | 2006-02-16 23:47:20 +0000 | [diff] [blame] | 955 | if ((access != (int)ACCESS_DEFAULT) && |
Neal Norwitz | d1cfc8a | 2006-02-05 03:36:57 +0000 | [diff] [blame] | 956 | ((flags != MAP_SHARED) || (prot != (PROT_WRITE | PROT_READ)))) |
Tim Peters | ec0a5f0 | 2006-02-16 23:47:20 +0000 | [diff] [blame] | 957 | return PyErr_Format(PyExc_ValueError, |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 958 | "mmap can't specify both access and flags, prot."); |
Neal Norwitz | d1cfc8a | 2006-02-05 03:36:57 +0000 | [diff] [blame] | 959 | switch ((access_mode)access) { |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 960 | case ACCESS_READ: |
| 961 | flags = MAP_SHARED; |
| 962 | prot = PROT_READ; |
| 963 | break; |
| 964 | case ACCESS_WRITE: |
| 965 | flags = MAP_SHARED; |
| 966 | prot = PROT_READ | PROT_WRITE; |
| 967 | break; |
| 968 | case ACCESS_COPY: |
| 969 | flags = MAP_PRIVATE; |
| 970 | prot = PROT_READ | PROT_WRITE; |
| 971 | break; |
Tim Peters | ec0a5f0 | 2006-02-16 23:47:20 +0000 | [diff] [blame] | 972 | case ACCESS_DEFAULT: |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 973 | /* use the specified or default values of flags and prot */ |
| 974 | break; |
| 975 | default: |
Tim Peters | ec0a5f0 | 2006-02-16 23:47:20 +0000 | [diff] [blame] | 976 | return PyErr_Format(PyExc_ValueError, |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 977 | "mmap invalid access parameter."); |
| 978 | } |
Neal Norwitz | b567392 | 2002-09-05 21:48:07 +0000 | [diff] [blame] | 979 | |
| 980 | #ifdef HAVE_FSTAT |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 981 | # ifdef __VMS |
| 982 | /* on OpenVMS we must ensure that all bytes are written to the file */ |
| 983 | fsync(fd); |
| 984 | # endif |
Martin v. Löwis | 7fe60c0 | 2005-03-03 11:22:44 +0000 | [diff] [blame] | 985 | if (fstat(fd, &st) == 0 && S_ISREG(st.st_mode)) { |
| 986 | if (map_size == 0) { |
Martin v. Löwis | cfe7e09 | 2006-02-17 06:59:14 +0000 | [diff] [blame] | 987 | map_size = st.st_size; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 988 | } else if ((size_t)offset + (size_t)map_size > st.st_size) { |
Tim Peters | ec0a5f0 | 2006-02-16 23:47:20 +0000 | [diff] [blame] | 989 | PyErr_SetString(PyExc_ValueError, |
Martin v. Löwis | 7fe60c0 | 2005-03-03 11:22:44 +0000 | [diff] [blame] | 990 | "mmap length is greater than file size"); |
| 991 | return NULL; |
| 992 | } |
Neal Norwitz | b567392 | 2002-09-05 21:48:07 +0000 | [diff] [blame] | 993 | } |
| 994 | #endif |
Tim Peters | 8f9cc29 | 2006-02-17 00:00:20 +0000 | [diff] [blame] | 995 | m_obj = PyObject_New(mmap_object, &mmap_object_type); |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 996 | if (m_obj == NULL) {return NULL;} |
Neal Norwitz | 3b4fff8 | 2006-01-11 08:54:45 +0000 | [diff] [blame] | 997 | m_obj->data = NULL; |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 998 | m_obj->size = (size_t) map_size; |
| 999 | m_obj->pos = (size_t) 0; |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 1000 | m_obj->exports = 0; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 1001 | m_obj->offset = offset; |
Neal Norwitz | 0e6bc8c | 2006-02-05 05:45:43 +0000 | [diff] [blame] | 1002 | if (fd == -1) { |
| 1003 | m_obj->fd = -1; |
| 1004 | /* Assume the caller wants to map anonymous memory. |
| 1005 | This is the same behaviour as Windows. mmap.mmap(-1, size) |
| 1006 | on both Windows and Unix map anonymous memory. |
| 1007 | */ |
| 1008 | #ifdef MAP_ANONYMOUS |
| 1009 | /* BSD way to map anonymous memory */ |
| 1010 | flags |= MAP_ANONYMOUS; |
| 1011 | #else |
| 1012 | /* SVR4 method to map anonymous memory is to open /dev/zero */ |
| 1013 | fd = devzero = open("/dev/zero", O_RDWR); |
| 1014 | if (devzero == -1) { |
| 1015 | Py_DECREF(m_obj); |
| 1016 | PyErr_SetFromErrno(mmap_module_error); |
| 1017 | return NULL; |
| 1018 | } |
| 1019 | #endif |
| 1020 | } else { |
| 1021 | m_obj->fd = dup(fd); |
| 1022 | if (m_obj->fd == -1) { |
| 1023 | Py_DECREF(m_obj); |
| 1024 | PyErr_SetFromErrno(mmap_module_error); |
| 1025 | return NULL; |
| 1026 | } |
Georg Brandl | 38387b8 | 2005-08-24 07:17:40 +0000 | [diff] [blame] | 1027 | } |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 1028 | |
Tim Peters | ec0a5f0 | 2006-02-16 23:47:20 +0000 | [diff] [blame] | 1029 | m_obj->data = mmap(NULL, map_size, |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 1030 | prot, flags, |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 1031 | fd, offset); |
Neal Norwitz | 0e6bc8c | 2006-02-05 05:45:43 +0000 | [diff] [blame] | 1032 | |
| 1033 | if (devzero != -1) { |
| 1034 | close(devzero); |
| 1035 | } |
| 1036 | |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 1037 | if (m_obj->data == (char *)-1) { |
Andrew M. Kuchling | 16581c8 | 2004-05-19 14:39:08 +0000 | [diff] [blame] | 1038 | m_obj->data = NULL; |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 1039 | Py_DECREF(m_obj); |
| 1040 | PyErr_SetFromErrno(mmap_module_error); |
| 1041 | return NULL; |
| 1042 | } |
Neal Norwitz | 8856fb7 | 2005-12-18 03:34:22 +0000 | [diff] [blame] | 1043 | m_obj->access = (access_mode)access; |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 1044 | return (PyObject *)m_obj; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 1045 | } |
| 1046 | #endif /* UNIX */ |
| 1047 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 1048 | #ifdef MS_WINDOWS |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 1049 | static PyObject * |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 1050 | new_mmap_object(PyObject *self, PyObject *args, PyObject *kwdict) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 1051 | { |
Fredrik Lundh | 54cf3dc | 2000-07-08 22:05:01 +0000 | [diff] [blame] | 1052 | mmap_object *m_obj; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 1053 | PyObject *map_size_obj = NULL, *offset_obj = NULL; |
| 1054 | Py_ssize_t map_size, offset; |
| 1055 | DWORD off_hi; /* upper 32 bits of offset */ |
| 1056 | DWORD off_lo; /* lower 32 bits of offset */ |
| 1057 | DWORD size_hi; /* upper 32 bits of size */ |
| 1058 | DWORD size_lo; /* lower 32 bits of size */ |
Fredrik Lundh | 54cf3dc | 2000-07-08 22:05:01 +0000 | [diff] [blame] | 1059 | char *tagname = ""; |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 1060 | DWORD dwErr = 0; |
| 1061 | int fileno; |
Mark Hammond | 071864a | 2000-07-30 02:46:26 +0000 | [diff] [blame] | 1062 | HANDLE fh = 0; |
Neal Norwitz | 8856fb7 | 2005-12-18 03:34:22 +0000 | [diff] [blame] | 1063 | int access = (access_mode)ACCESS_DEFAULT; |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 1064 | DWORD flProtect, dwDesiredAccess; |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 1065 | static char *keywords[] = { "fileno", "length", |
Tim Peters | ec0a5f0 | 2006-02-16 23:47:20 +0000 | [diff] [blame] | 1066 | "tagname", |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 1067 | "access", "offset", NULL }; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 1068 | |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 1069 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iO|ziO", keywords, |
Tim Peters | ec0a5f0 | 2006-02-16 23:47:20 +0000 | [diff] [blame] | 1070 | &fileno, &map_size_obj, |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 1071 | &tagname, &access, &offset_obj)) { |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 1072 | return NULL; |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 1073 | } |
| 1074 | |
Neal Norwitz | 8856fb7 | 2005-12-18 03:34:22 +0000 | [diff] [blame] | 1075 | switch((access_mode)access) { |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 1076 | case ACCESS_READ: |
| 1077 | flProtect = PAGE_READONLY; |
| 1078 | dwDesiredAccess = FILE_MAP_READ; |
| 1079 | break; |
| 1080 | case ACCESS_DEFAULT: case ACCESS_WRITE: |
| 1081 | flProtect = PAGE_READWRITE; |
| 1082 | dwDesiredAccess = FILE_MAP_WRITE; |
| 1083 | break; |
| 1084 | case ACCESS_COPY: |
| 1085 | flProtect = PAGE_WRITECOPY; |
| 1086 | dwDesiredAccess = FILE_MAP_COPY; |
| 1087 | break; |
| 1088 | default: |
Tim Peters | ec0a5f0 | 2006-02-16 23:47:20 +0000 | [diff] [blame] | 1089 | return PyErr_Format(PyExc_ValueError, |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 1090 | "mmap invalid access parameter."); |
| 1091 | } |
| 1092 | |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 1093 | map_size = _GetMapSize(map_size_obj, "size"); |
Andrew M. Kuchling | 70d2742 | 2000-06-18 04:45:14 +0000 | [diff] [blame] | 1094 | if (map_size < 0) |
| 1095 | return NULL; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 1096 | offset = _GetMapSize(offset_obj, "offset"); |
| 1097 | if (offset < 0) |
| 1098 | return NULL; |
Tim Peters | ec0a5f0 | 2006-02-16 23:47:20 +0000 | [diff] [blame] | 1099 | |
Neal Norwitz | 0e6bc8c | 2006-02-05 05:45:43 +0000 | [diff] [blame] | 1100 | /* assume -1 and 0 both mean invalid filedescriptor |
| 1101 | to 'anonymously' map memory. |
| 1102 | XXX: fileno == 0 is a valid fd, but was accepted prior to 2.5. |
| 1103 | XXX: Should this code be added? |
| 1104 | if (fileno == 0) |
Skip Montanaro | 46fc337 | 2007-08-12 11:44:53 +0000 | [diff] [blame] | 1105 | PyErr_WarnEx(PyExc_DeprecationWarning, |
| 1106 | "don't use 0 for anonymous memory", |
| 1107 | 1); |
Neal Norwitz | 0e6bc8c | 2006-02-05 05:45:43 +0000 | [diff] [blame] | 1108 | */ |
| 1109 | if (fileno != -1 && fileno != 0) { |
Mark Hammond | 071864a | 2000-07-30 02:46:26 +0000 | [diff] [blame] | 1110 | fh = (HANDLE)_get_osfhandle(fileno); |
| 1111 | if (fh==(HANDLE)-1) { |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 1112 | PyErr_SetFromErrno(mmap_module_error); |
| 1113 | return NULL; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 1114 | } |
Fred Drake | 1ef4e2d | 2000-04-05 14:15:31 +0000 | [diff] [blame] | 1115 | /* Win9x appears to need us seeked to zero */ |
Guido van Rossum | 69c2b88 | 2003-04-09 19:31:02 +0000 | [diff] [blame] | 1116 | lseek(fileno, 0, SEEK_SET); |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 1117 | } |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 1118 | |
Tim Peters | 8f9cc29 | 2006-02-17 00:00:20 +0000 | [diff] [blame] | 1119 | m_obj = PyObject_New(mmap_object, &mmap_object_type); |
| 1120 | if (m_obj == NULL) |
Mark Hammond | 2cbed00 | 2000-07-30 02:22:43 +0000 | [diff] [blame] | 1121 | return NULL; |
| 1122 | /* Set every field to an invalid marker, so we can safely |
| 1123 | destruct the object in the face of failure */ |
| 1124 | m_obj->data = NULL; |
Mark Hammond | 071864a | 2000-07-30 02:46:26 +0000 | [diff] [blame] | 1125 | m_obj->file_handle = INVALID_HANDLE_VALUE; |
Mark Hammond | 2cbed00 | 2000-07-30 02:22:43 +0000 | [diff] [blame] | 1126 | m_obj->map_handle = INVALID_HANDLE_VALUE; |
| 1127 | m_obj->tagname = NULL; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 1128 | m_obj->offset = offset; |
Mark Hammond | 2cbed00 | 2000-07-30 02:22:43 +0000 | [diff] [blame] | 1129 | |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 1130 | if (fh) { |
Mark Hammond | 2cbed00 | 2000-07-30 02:22:43 +0000 | [diff] [blame] | 1131 | /* It is necessary to duplicate the handle, so the |
| 1132 | Python code can close it on us */ |
| 1133 | if (!DuplicateHandle( |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 1134 | GetCurrentProcess(), /* source process handle */ |
| 1135 | fh, /* handle to be duplicated */ |
| 1136 | GetCurrentProcess(), /* target proc handle */ |
| 1137 | (LPHANDLE)&m_obj->file_handle, /* result */ |
| 1138 | 0, /* access - ignored due to options value */ |
| 1139 | FALSE, /* inherited by child processes? */ |
| 1140 | DUPLICATE_SAME_ACCESS)) { /* options */ |
Mark Hammond | 2cbed00 | 2000-07-30 02:22:43 +0000 | [diff] [blame] | 1141 | dwErr = GetLastError(); |
| 1142 | Py_DECREF(m_obj); |
| 1143 | PyErr_SetFromWindowsErr(dwErr); |
| 1144 | return NULL; |
| 1145 | } |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 1146 | if (!map_size) { |
Martin v. Löwis | 1518607 | 2006-02-18 12:38:35 +0000 | [diff] [blame] | 1147 | DWORD low,high; |
| 1148 | low = GetFileSize(fh, &high); |
| 1149 | /* low might just happen to have the value INVALID_FILE_SIZE; |
| 1150 | so we need to check the last error also. */ |
| 1151 | if (low == INVALID_FILE_SIZE && |
| 1152 | (dwErr = GetLastError()) != NO_ERROR) { |
| 1153 | Py_DECREF(m_obj); |
| 1154 | return PyErr_SetFromWindowsErr(dwErr); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1155 | } |
| 1156 | |
Martin v. Löwis | 1518607 | 2006-02-18 12:38:35 +0000 | [diff] [blame] | 1157 | #if SIZEOF_SIZE_T > 4 |
| 1158 | m_obj->size = (((size_t)high)<<32) + low; |
| 1159 | #else |
| 1160 | if (high) |
| 1161 | /* File is too large to map completely */ |
| 1162 | m_obj->size = (size_t)-1; |
| 1163 | else |
| 1164 | m_obj->size = low; |
| 1165 | #endif |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 1166 | } else { |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 1167 | m_obj->size = map_size; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 1168 | } |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 1169 | } |
| 1170 | else { |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 1171 | m_obj->size = map_size; |
| 1172 | } |
| 1173 | |
| 1174 | /* set the initial position */ |
| 1175 | m_obj->pos = (size_t) 0; |
| 1176 | |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 1177 | m_obj->exports = 0; |
Mark Hammond | 2cbed00 | 2000-07-30 02:22:43 +0000 | [diff] [blame] | 1178 | /* set the tag name */ |
Tim Peters | 0d9f9dc | 2001-01-10 05:42:18 +0000 | [diff] [blame] | 1179 | if (tagname != NULL && *tagname != '\0') { |
Mark Hammond | 2cbed00 | 2000-07-30 02:22:43 +0000 | [diff] [blame] | 1180 | m_obj->tagname = PyMem_Malloc(strlen(tagname)+1); |
| 1181 | if (m_obj->tagname == NULL) { |
| 1182 | PyErr_NoMemory(); |
| 1183 | Py_DECREF(m_obj); |
| 1184 | return NULL; |
| 1185 | } |
| 1186 | strcpy(m_obj->tagname, tagname); |
| 1187 | } |
| 1188 | else |
| 1189 | m_obj->tagname = NULL; |
| 1190 | |
Neal Norwitz | 8856fb7 | 2005-12-18 03:34:22 +0000 | [diff] [blame] | 1191 | m_obj->access = (access_mode)access; |
Tim Peters | e564e7f | 2006-02-16 23:46:01 +0000 | [diff] [blame] | 1192 | /* 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] | 1193 | * 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] | 1194 | * consumes 4 bytes), C doesn't define what happens if we shift |
| 1195 | * right by 32, so we need different code. |
| 1196 | */ |
| 1197 | #if SIZEOF_SIZE_T > 4 |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 1198 | size_hi = (DWORD)((offset + m_obj->size) >> 32); |
| 1199 | size_lo = (DWORD)((offset + m_obj->size) & 0xFFFFFFFF); |
| 1200 | off_hi = (DWORD)(offset >> 32); |
| 1201 | off_lo = (DWORD)(offset & 0xFFFFFFFF); |
Tim Peters | e564e7f | 2006-02-16 23:46:01 +0000 | [diff] [blame] | 1202 | #else |
| 1203 | size_hi = 0; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 1204 | size_lo = (DWORD)(offset + m_obj->size); |
| 1205 | off_hi = 0; |
| 1206 | off_lo = (DWORD)offset; |
Tim Peters | e564e7f | 2006-02-16 23:46:01 +0000 | [diff] [blame] | 1207 | #endif |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 1208 | /* For files, it would be sufficient to pass 0 as size. |
| 1209 | For anonymous maps, we have to pass the size explicitly. */ |
Tim Peters | 8f9cc29 | 2006-02-17 00:00:20 +0000 | [diff] [blame] | 1210 | m_obj->map_handle = CreateFileMapping(m_obj->file_handle, |
| 1211 | NULL, |
| 1212 | flProtect, |
| 1213 | size_hi, |
| 1214 | size_lo, |
| 1215 | m_obj->tagname); |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 1216 | if (m_obj->map_handle != NULL) { |
Tim Peters | 8f9cc29 | 2006-02-17 00:00:20 +0000 | [diff] [blame] | 1217 | m_obj->data = (char *) MapViewOfFile(m_obj->map_handle, |
| 1218 | dwDesiredAccess, |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 1219 | off_hi, |
| 1220 | off_lo, |
Tim Peters | 8f9cc29 | 2006-02-17 00:00:20 +0000 | [diff] [blame] | 1221 | 0); |
Tim Peters | 23721ee | 2006-02-16 23:50:16 +0000 | [diff] [blame] | 1222 | if (m_obj->data != NULL) |
| 1223 | return (PyObject *)m_obj; |
| 1224 | else |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 1225 | dwErr = GetLastError(); |
Tim Peters | 23721ee | 2006-02-16 23:50:16 +0000 | [diff] [blame] | 1226 | } else |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 1227 | dwErr = GetLastError(); |
Mark Hammond | 2cbed00 | 2000-07-30 02:22:43 +0000 | [diff] [blame] | 1228 | Py_DECREF(m_obj); |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 1229 | PyErr_SetFromWindowsErr(dwErr); |
Tim Peters | 23721ee | 2006-02-16 23:50:16 +0000 | [diff] [blame] | 1230 | return NULL; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 1231 | } |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 1232 | #endif /* MS_WINDOWS */ |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 1233 | |
| 1234 | /* List of functions exported by this module */ |
| 1235 | static struct PyMethodDef mmap_functions[] = { |
Tim Peters | ec0a5f0 | 2006-02-16 23:47:20 +0000 | [diff] [blame] | 1236 | {"mmap", (PyCFunction) new_mmap_object, |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 1237 | METH_VARARGS|METH_KEYWORDS}, |
| 1238 | {NULL, NULL} /* Sentinel */ |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 1239 | }; |
| 1240 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 1241 | static void |
| 1242 | setint(PyObject *d, const char *name, long value) |
| 1243 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame^] | 1244 | PyObject *o = PyLong_FromLong(value); |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 1245 | if (o && PyDict_SetItemString(d, name, o) == 0) { |
| 1246 | Py_DECREF(o); |
| 1247 | } |
| 1248 | } |
| 1249 | |
Mark Hammond | 62b1ab1 | 2002-07-23 06:31:15 +0000 | [diff] [blame] | 1250 | PyMODINIT_FUNC |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 1251 | initmmap(void) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 1252 | { |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 1253 | PyObject *dict, *module; |
Tim Peters | 2caf8df | 2001-01-14 05:05:51 +0000 | [diff] [blame] | 1254 | |
| 1255 | /* Patch the object type */ |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 1256 | Py_Type(&mmap_object_type) = &PyType_Type; |
Tim Peters | 2caf8df | 2001-01-14 05:05:51 +0000 | [diff] [blame] | 1257 | |
Tim Peters | 8f9cc29 | 2006-02-17 00:00:20 +0000 | [diff] [blame] | 1258 | module = Py_InitModule("mmap", mmap_functions); |
Neal Norwitz | 1ac754f | 2006-01-19 06:09:39 +0000 | [diff] [blame] | 1259 | if (module == NULL) |
| 1260 | return; |
Tim Peters | 8f9cc29 | 2006-02-17 00:00:20 +0000 | [diff] [blame] | 1261 | dict = PyModule_GetDict(module); |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 1262 | if (!dict) |
| 1263 | return; |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 1264 | mmap_module_error = PyExc_EnvironmentError; |
Tim Peters | 8f9cc29 | 2006-02-17 00:00:20 +0000 | [diff] [blame] | 1265 | PyDict_SetItemString(dict, "error", mmap_module_error); |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 1266 | #ifdef PROT_EXEC |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 1267 | setint(dict, "PROT_EXEC", PROT_EXEC); |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 1268 | #endif |
| 1269 | #ifdef PROT_READ |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 1270 | setint(dict, "PROT_READ", PROT_READ); |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 1271 | #endif |
| 1272 | #ifdef PROT_WRITE |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 1273 | setint(dict, "PROT_WRITE", PROT_WRITE); |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 1274 | #endif |
| 1275 | |
| 1276 | #ifdef MAP_SHARED |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 1277 | setint(dict, "MAP_SHARED", MAP_SHARED); |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 1278 | #endif |
| 1279 | #ifdef MAP_PRIVATE |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 1280 | setint(dict, "MAP_PRIVATE", MAP_PRIVATE); |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 1281 | #endif |
| 1282 | #ifdef MAP_DENYWRITE |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 1283 | setint(dict, "MAP_DENYWRITE", MAP_DENYWRITE); |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 1284 | #endif |
| 1285 | #ifdef MAP_EXECUTABLE |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 1286 | setint(dict, "MAP_EXECUTABLE", MAP_EXECUTABLE); |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 1287 | #endif |
Neal Norwitz | 0e6bc8c | 2006-02-05 05:45:43 +0000 | [diff] [blame] | 1288 | #ifdef MAP_ANONYMOUS |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 1289 | setint(dict, "MAP_ANON", MAP_ANONYMOUS); |
| 1290 | setint(dict, "MAP_ANONYMOUS", MAP_ANONYMOUS); |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 1291 | #endif |
| 1292 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 1293 | setint(dict, "PAGESIZE", (long)my_getpagesize()); |
Andrew M. Kuchling | 961fe17 | 2000-06-03 19:41:42 +0000 | [diff] [blame] | 1294 | |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 1295 | setint(dict, "ALLOCATIONGRANULARITY", (long)my_getallocationgranularity()); |
| 1296 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 1297 | setint(dict, "ACCESS_READ", ACCESS_READ); |
| 1298 | setint(dict, "ACCESS_WRITE", ACCESS_WRITE); |
| 1299 | setint(dict, "ACCESS_COPY", ACCESS_COPY); |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 1300 | } |