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 | |
| 6 | / mmapmodule.cpp -- map a view of a file into memory |
| 7 | / |
| 8 | / todo: need permission flags, perhaps a 'chsize' analog |
| 9 | / not all functions check range yet!!! |
| 10 | / |
| 11 | / |
Mark Hammond | 071864a | 2000-07-30 02:46:26 +0000 | [diff] [blame] | 12 | / This version of mmapmodule.c has been changed significantly |
| 13 | / from the original mmapfile.c on which it was based. |
| 14 | / The original version of mmapfile is maintained by Sam at |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 15 | / ftp://squirl.nightmare.com/pub/python/python-ext. |
| 16 | */ |
| 17 | |
Martin v. Löwis | cfe7e09 | 2006-02-17 06:59:14 +0000 | [diff] [blame] | 18 | #define PY_SSIZE_T_CLEAN |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 19 | #include <Python.h> |
| 20 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 21 | #ifndef MS_WINDOWS |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 22 | #define UNIX |
| 23 | #endif |
| 24 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 25 | #ifdef MS_WINDOWS |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 26 | #include <windows.h> |
Fred Drake | 145f96e | 2000-10-01 17:50:46 +0000 | [diff] [blame] | 27 | static int |
| 28 | my_getpagesize(void) |
| 29 | { |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 30 | SYSTEM_INFO si; |
| 31 | GetSystemInfo(&si); |
| 32 | return si.dwPageSize; |
Fred Drake | 145f96e | 2000-10-01 17:50:46 +0000 | [diff] [blame] | 33 | } |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 34 | #endif |
| 35 | |
| 36 | #ifdef UNIX |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 37 | #include <sys/mman.h> |
Andrew M. Kuchling | 7b9fb92 | 2000-06-17 22:41:22 +0000 | [diff] [blame] | 38 | #include <sys/stat.h> |
Guido van Rossum | 4b36e6b | 2000-09-25 13:16:15 +0000 | [diff] [blame] | 39 | |
Fred Drake | 145f96e | 2000-10-01 17:50:46 +0000 | [diff] [blame] | 40 | #if defined(HAVE_SYSCONF) && defined(_SC_PAGESIZE) |
| 41 | static int |
| 42 | my_getpagesize(void) |
| 43 | { |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 44 | return sysconf(_SC_PAGESIZE); |
Fred Drake | 145f96e | 2000-10-01 17:50:46 +0000 | [diff] [blame] | 45 | } |
| 46 | #else |
| 47 | #define my_getpagesize getpagesize |
| 48 | #endif |
| 49 | |
Guido van Rossum | 4b36e6b | 2000-09-25 13:16:15 +0000 | [diff] [blame] | 50 | #endif /* UNIX */ |
| 51 | |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 52 | #include <string.h> |
| 53 | #include <sys/types.h> |
| 54 | |
Neal Norwitz | 3eaf2b5 | 2006-02-16 08:08:54 +0000 | [diff] [blame] | 55 | /* Prefer MAP_ANONYMOUS since MAP_ANON is deprecated according to man page. */ |
Neal Norwitz | 0e6bc8c | 2006-02-05 05:45:43 +0000 | [diff] [blame] | 56 | #if !defined(MAP_ANONYMOUS) && defined(MAP_ANON) |
| 57 | # define MAP_ANONYMOUS MAP_ANON |
| 58 | #endif |
| 59 | |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 60 | static PyObject *mmap_module_error; |
| 61 | |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 62 | typedef enum |
| 63 | { |
| 64 | ACCESS_DEFAULT, |
| 65 | ACCESS_READ, |
| 66 | ACCESS_WRITE, |
| 67 | ACCESS_COPY |
| 68 | } access_mode; |
| 69 | |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 70 | typedef struct { |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 71 | PyObject_HEAD |
| 72 | char * data; |
| 73 | size_t size; |
| 74 | size_t pos; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 75 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 76 | #ifdef MS_WINDOWS |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 77 | HANDLE map_handle; |
Mark Hammond | 071864a | 2000-07-30 02:46:26 +0000 | [diff] [blame] | 78 | HANDLE file_handle; |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 79 | char * tagname; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 80 | #endif |
| 81 | |
| 82 | #ifdef UNIX |
Andrew M. Kuchling | 7b9fb92 | 2000-06-17 22:41:22 +0000 | [diff] [blame] | 83 | int fd; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 84 | #endif |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 85 | |
| 86 | access_mode access; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 87 | } mmap_object; |
| 88 | |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 89 | |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 90 | static void |
Fredrik Lundh | 54cf3dc | 2000-07-08 22:05:01 +0000 | [diff] [blame] | 91 | mmap_object_dealloc(mmap_object *m_obj) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 92 | { |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 93 | #ifdef MS_WINDOWS |
Mark Hammond | 2cbed00 | 2000-07-30 02:22:43 +0000 | [diff] [blame] | 94 | if (m_obj->data != NULL) |
| 95 | UnmapViewOfFile (m_obj->data); |
| 96 | if (m_obj->map_handle != INVALID_HANDLE_VALUE) |
| 97 | CloseHandle (m_obj->map_handle); |
Mark Hammond | 071864a | 2000-07-30 02:46:26 +0000 | [diff] [blame] | 98 | if (m_obj->file_handle != INVALID_HANDLE_VALUE) |
| 99 | CloseHandle (m_obj->file_handle); |
Mark Hammond | 2cbed00 | 2000-07-30 02:22:43 +0000 | [diff] [blame] | 100 | if (m_obj->tagname) |
| 101 | PyMem_Free(m_obj->tagname); |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 102 | #endif /* MS_WINDOWS */ |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 103 | |
| 104 | #ifdef UNIX |
Neal Norwitz | 6eac200 | 2005-11-02 05:36:37 +0000 | [diff] [blame] | 105 | if (m_obj->fd >= 0) |
| 106 | (void) close(m_obj->fd); |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 107 | if (m_obj->data!=NULL) { |
Andrew M. Kuchling | 9bc5f33 | 2000-06-18 04:25:08 +0000 | [diff] [blame] | 108 | msync(m_obj->data, m_obj->size, MS_SYNC); |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 109 | munmap(m_obj->data, m_obj->size); |
| 110 | } |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 111 | #endif /* UNIX */ |
| 112 | |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 113 | PyObject_Del(m_obj); |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | static PyObject * |
Fredrik Lundh | 54cf3dc | 2000-07-08 22:05:01 +0000 | [diff] [blame] | 117 | mmap_close_method(mmap_object *self, PyObject *args) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 118 | { |
Andrew M. Kuchling | 841b9fb | 2000-06-03 20:43:43 +0000 | [diff] [blame] | 119 | if (!PyArg_ParseTuple(args, ":close")) |
Andrew M. Kuchling | 961fe17 | 2000-06-03 19:41:42 +0000 | [diff] [blame] | 120 | return NULL; |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 121 | #ifdef MS_WINDOWS |
Mark Hammond | 071864a | 2000-07-30 02:46:26 +0000 | [diff] [blame] | 122 | /* For each resource we maintain, we need to check |
Tim Peters | ec0a5f0 | 2006-02-16 23:47:20 +0000 | [diff] [blame] | 123 | the value is valid, and if so, free the resource |
Mark Hammond | 071864a | 2000-07-30 02:46:26 +0000 | [diff] [blame] | 124 | and set the member value to an invalid value so |
| 125 | the dealloc does not attempt to resource clearing |
| 126 | again. |
| 127 | TODO - should we check for errors in the close operations??? |
| 128 | */ |
| 129 | if (self->data != NULL) { |
Tim Peters | 8f9cc29 | 2006-02-17 00:00:20 +0000 | [diff] [blame] | 130 | UnmapViewOfFile(self->data); |
Mark Hammond | 071864a | 2000-07-30 02:46:26 +0000 | [diff] [blame] | 131 | self->data = NULL; |
| 132 | } |
| 133 | if (self->map_handle != INVALID_HANDLE_VALUE) { |
Tim Peters | 8f9cc29 | 2006-02-17 00:00:20 +0000 | [diff] [blame] | 134 | CloseHandle(self->map_handle); |
Mark Hammond | 071864a | 2000-07-30 02:46:26 +0000 | [diff] [blame] | 135 | self->map_handle = INVALID_HANDLE_VALUE; |
| 136 | } |
| 137 | if (self->file_handle != INVALID_HANDLE_VALUE) { |
Tim Peters | 8f9cc29 | 2006-02-17 00:00:20 +0000 | [diff] [blame] | 138 | CloseHandle(self->file_handle); |
Mark Hammond | 071864a | 2000-07-30 02:46:26 +0000 | [diff] [blame] | 139 | self->file_handle = INVALID_HANDLE_VALUE; |
| 140 | } |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 141 | #endif /* MS_WINDOWS */ |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 142 | |
| 143 | #ifdef UNIX |
Neal Norwitz | 6eac200 | 2005-11-02 05:36:37 +0000 | [diff] [blame] | 144 | (void) close(self->fd); |
| 145 | self->fd = -1; |
Neal Norwitz | e604c02 | 2003-01-10 20:52:16 +0000 | [diff] [blame] | 146 | if (self->data != NULL) { |
| 147 | munmap(self->data, self->size); |
| 148 | self->data = NULL; |
| 149 | } |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 150 | #endif |
| 151 | |
Tim Peters | 8f9cc29 | 2006-02-17 00:00:20 +0000 | [diff] [blame] | 152 | Py_INCREF(Py_None); |
Tim Peters | 23721ee | 2006-02-16 23:50:16 +0000 | [diff] [blame] | 153 | return Py_None; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 154 | } |
| 155 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 156 | #ifdef MS_WINDOWS |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 157 | #define CHECK_VALID(err) \ |
| 158 | do { \ |
Guido van Rossum | 69c2b88 | 2003-04-09 19:31:02 +0000 | [diff] [blame] | 159 | if (self->map_handle == INVALID_HANDLE_VALUE) { \ |
Tim Peters | 8f9cc29 | 2006-02-17 00:00:20 +0000 | [diff] [blame] | 160 | PyErr_SetString(PyExc_ValueError, "mmap closed or invalid"); \ |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 161 | return err; \ |
| 162 | } \ |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 163 | } while (0) |
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 |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 167 | #define CHECK_VALID(err) \ |
| 168 | do { \ |
| 169 | if (self->data == NULL) { \ |
Tim Peters | 8f9cc29 | 2006-02-17 00:00:20 +0000 | [diff] [blame] | 170 | PyErr_SetString(PyExc_ValueError, "mmap closed or invalid"); \ |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 171 | return err; \ |
| 172 | } \ |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 173 | } while (0) |
| 174 | #endif /* UNIX */ |
| 175 | |
| 176 | static PyObject * |
Fredrik Lundh | 54cf3dc | 2000-07-08 22:05:01 +0000 | [diff] [blame] | 177 | mmap_read_byte_method(mmap_object *self, |
| 178 | PyObject *args) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 179 | { |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 180 | CHECK_VALID(NULL); |
Andrew M. Kuchling | 841b9fb | 2000-06-03 20:43:43 +0000 | [diff] [blame] | 181 | if (!PyArg_ParseTuple(args, ":read_byte")) |
Andrew M. Kuchling | 961fe17 | 2000-06-03 19:41:42 +0000 | [diff] [blame] | 182 | return NULL; |
Andrew M. Kuchling | 70d2742 | 2000-06-18 04:45:14 +0000 | [diff] [blame] | 183 | if (self->pos < self->size) { |
Tim Peters | d6283b8 | 2001-05-09 18:48:26 +0000 | [diff] [blame] | 184 | char value = self->data[self->pos]; |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 185 | self->pos += 1; |
Tim Peters | d6283b8 | 2001-05-09 18:48:26 +0000 | [diff] [blame] | 186 | return Py_BuildValue("c", value); |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 187 | } else { |
Tim Peters | 8f9cc29 | 2006-02-17 00:00:20 +0000 | [diff] [blame] | 188 | PyErr_SetString(PyExc_ValueError, "read byte out of range"); |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 189 | return NULL; |
| 190 | } |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | static PyObject * |
Fredrik Lundh | 54cf3dc | 2000-07-08 22:05:01 +0000 | [diff] [blame] | 194 | mmap_read_line_method(mmap_object *self, |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 195 | PyObject *args) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 196 | { |
Fredrik Lundh | 54cf3dc | 2000-07-08 22:05:01 +0000 | [diff] [blame] | 197 | char *start = self->data+self->pos; |
| 198 | char *eof = self->data+self->size; |
| 199 | char *eol; |
| 200 | PyObject *result; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 201 | |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 202 | CHECK_VALID(NULL); |
Andrew M. Kuchling | 841b9fb | 2000-06-03 20:43:43 +0000 | [diff] [blame] | 203 | if (!PyArg_ParseTuple(args, ":readline")) |
Andrew M. Kuchling | 961fe17 | 2000-06-03 19:41:42 +0000 | [diff] [blame] | 204 | return NULL; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 205 | |
Fred Drake | 56a87a0 | 2000-04-04 18:17:35 +0000 | [diff] [blame] | 206 | eol = memchr(start, '\n', self->size - self->pos); |
| 207 | if (!eol) |
| 208 | eol = eof; |
| 209 | else |
| 210 | ++eol; /* we're interested in the position after the |
| 211 | newline. */ |
Andrew M. Kuchling | 70d2742 | 2000-06-18 04:45:14 +0000 | [diff] [blame] | 212 | result = PyString_FromStringAndSize(start, (eol - start)); |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 213 | self->pos += (eol - start); |
Tim Peters | 23721ee | 2006-02-16 23:50:16 +0000 | [diff] [blame] | 214 | return result; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | static PyObject * |
Fredrik Lundh | 54cf3dc | 2000-07-08 22:05:01 +0000 | [diff] [blame] | 218 | mmap_read_method(mmap_object *self, |
| 219 | PyObject *args) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 220 | { |
Martin v. Löwis | ad0a462 | 2006-02-16 14:30:23 +0000 | [diff] [blame] | 221 | Py_ssize_t num_bytes; |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 222 | PyObject *result; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 223 | |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 224 | CHECK_VALID(NULL); |
Martin v. Löwis | ad0a462 | 2006-02-16 14:30:23 +0000 | [diff] [blame] | 225 | if (!PyArg_ParseTuple(args, "n:read", &num_bytes)) |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 226 | return(NULL); |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 227 | |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 228 | /* silently 'adjust' out-of-range requests */ |
| 229 | if ((self->pos + num_bytes) > self->size) { |
| 230 | num_bytes -= (self->pos+num_bytes) - self->size; |
| 231 | } |
| 232 | result = Py_BuildValue("s#", self->data+self->pos, num_bytes); |
Tim Peters | ec0a5f0 | 2006-02-16 23:47:20 +0000 | [diff] [blame] | 233 | self->pos += num_bytes; |
Tim Peters | 23721ee | 2006-02-16 23:50:16 +0000 | [diff] [blame] | 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_find_method(mmap_object *self, |
| 239 | PyObject *args) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 240 | { |
Martin v. Löwis | ad0a462 | 2006-02-16 14:30:23 +0000 | [diff] [blame] | 241 | Py_ssize_t start = self->pos; |
Fredrik Lundh | 54cf3dc | 2000-07-08 22:05:01 +0000 | [diff] [blame] | 242 | char *needle; |
Martin v. Löwis | cfe7e09 | 2006-02-17 06:59:14 +0000 | [diff] [blame] | 243 | Py_ssize_t len; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 244 | |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 245 | CHECK_VALID(NULL); |
Tim Peters | 8f9cc29 | 2006-02-17 00:00:20 +0000 | [diff] [blame] | 246 | if (!PyArg_ParseTuple(args, "s#|n:find", &needle, &len, &start)) { |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 247 | return NULL; |
| 248 | } else { |
Greg Stein | 834f4dd | 2001-05-14 09:32:26 +0000 | [diff] [blame] | 249 | char *p; |
| 250 | char *e = self->data + self->size; |
| 251 | |
| 252 | if (start < 0) |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 253 | start += self->size; |
Greg Stein | 834f4dd | 2001-05-14 09:32:26 +0000 | [diff] [blame] | 254 | if (start < 0) |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 255 | start = 0; |
Tim Peters | d401edd | 2001-05-14 23:19:12 +0000 | [diff] [blame] | 256 | else if ((size_t)start > self->size) |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 257 | start = self->size; |
Greg Stein | 834f4dd | 2001-05-14 09:32:26 +0000 | [diff] [blame] | 258 | |
Tim Peters | c9ffa06 | 2002-03-08 05:43:32 +0000 | [diff] [blame] | 259 | for (p = self->data + start; p + len <= e; ++p) { |
Martin v. Löwis | cfe7e09 | 2006-02-17 06:59:14 +0000 | [diff] [blame] | 260 | Py_ssize_t i; |
Tim Peters | c9ffa06 | 2002-03-08 05:43:32 +0000 | [diff] [blame] | 261 | for (i = 0; i < len && needle[i] == p[i]; ++i) |
| 262 | /* nothing */; |
| 263 | if (i == len) { |
Martin v. Löwis | cfe7e09 | 2006-02-17 06:59:14 +0000 | [diff] [blame] | 264 | return PyInt_FromSsize_t(p - self->data); |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 265 | } |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 266 | } |
Martin v. Löwis | cfe7e09 | 2006-02-17 06:59:14 +0000 | [diff] [blame] | 267 | return PyInt_FromLong(-1); |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 268 | } |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 269 | } |
| 270 | |
Tim Peters | ec0a5f0 | 2006-02-16 23:47:20 +0000 | [diff] [blame] | 271 | static int |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 272 | is_writeable(mmap_object *self) |
| 273 | { |
| 274 | if (self->access != ACCESS_READ) |
Tim Peters | ec0a5f0 | 2006-02-16 23:47:20 +0000 | [diff] [blame] | 275 | return 1; |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 276 | PyErr_Format(PyExc_TypeError, "mmap can't modify a readonly memory map."); |
| 277 | return 0; |
| 278 | } |
| 279 | |
Tim Peters | ec0a5f0 | 2006-02-16 23:47:20 +0000 | [diff] [blame] | 280 | static int |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 281 | is_resizeable(mmap_object *self) |
| 282 | { |
| 283 | if ((self->access == ACCESS_WRITE) || (self->access == ACCESS_DEFAULT)) |
Tim Peters | ec0a5f0 | 2006-02-16 23:47:20 +0000 | [diff] [blame] | 284 | return 1; |
| 285 | PyErr_Format(PyExc_TypeError, |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 286 | "mmap can't resize a readonly or copy-on-write memory map."); |
| 287 | return 0; |
| 288 | } |
| 289 | |
| 290 | |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 291 | static PyObject * |
Fredrik Lundh | 54cf3dc | 2000-07-08 22:05:01 +0000 | [diff] [blame] | 292 | mmap_write_method(mmap_object *self, |
| 293 | PyObject *args) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 294 | { |
Martin v. Löwis | cfe7e09 | 2006-02-17 06:59:14 +0000 | [diff] [blame] | 295 | Py_ssize_t length; |
Fredrik Lundh | 54cf3dc | 2000-07-08 22:05:01 +0000 | [diff] [blame] | 296 | char *data; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 297 | |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 298 | CHECK_VALID(NULL); |
Tim Peters | 8f9cc29 | 2006-02-17 00:00:20 +0000 | [diff] [blame] | 299 | if (!PyArg_ParseTuple(args, "s#:write", &data, &length)) |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 300 | return(NULL); |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 301 | |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 302 | if (!is_writeable(self)) |
| 303 | return NULL; |
| 304 | |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 305 | if ((self->pos + length) > self->size) { |
Tim Peters | 8f9cc29 | 2006-02-17 00:00:20 +0000 | [diff] [blame] | 306 | PyErr_SetString(PyExc_ValueError, "data out of range"); |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 307 | return NULL; |
| 308 | } |
Tim Peters | 8f9cc29 | 2006-02-17 00:00:20 +0000 | [diff] [blame] | 309 | memcpy(self->data+self->pos, data, length); |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 310 | self->pos = self->pos+length; |
Tim Peters | 8f9cc29 | 2006-02-17 00:00:20 +0000 | [diff] [blame] | 311 | Py_INCREF(Py_None); |
Tim Peters | 23721ee | 2006-02-16 23:50:16 +0000 | [diff] [blame] | 312 | return Py_None; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | static PyObject * |
Fredrik Lundh | 54cf3dc | 2000-07-08 22:05:01 +0000 | [diff] [blame] | 316 | mmap_write_byte_method(mmap_object *self, |
| 317 | PyObject *args) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 318 | { |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 319 | char value; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 320 | |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 321 | CHECK_VALID(NULL); |
Tim Peters | 8f9cc29 | 2006-02-17 00:00:20 +0000 | [diff] [blame] | 322 | if (!PyArg_ParseTuple(args, "c:write_byte", &value)) |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 323 | return(NULL); |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 324 | |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 325 | if (!is_writeable(self)) |
| 326 | return NULL; |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 327 | *(self->data+self->pos) = value; |
| 328 | self->pos += 1; |
Tim Peters | 8f9cc29 | 2006-02-17 00:00:20 +0000 | [diff] [blame] | 329 | Py_INCREF(Py_None); |
Tim Peters | 23721ee | 2006-02-16 23:50:16 +0000 | [diff] [blame] | 330 | return Py_None; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 331 | } |
Tim Peters | ec0a5f0 | 2006-02-16 23:47:20 +0000 | [diff] [blame] | 332 | |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 333 | static PyObject * |
Fredrik Lundh | 54cf3dc | 2000-07-08 22:05:01 +0000 | [diff] [blame] | 334 | mmap_size_method(mmap_object *self, |
| 335 | PyObject *args) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 336 | { |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 337 | CHECK_VALID(NULL); |
Andrew M. Kuchling | 841b9fb | 2000-06-03 20:43:43 +0000 | [diff] [blame] | 338 | if (!PyArg_ParseTuple(args, ":size")) |
Andrew M. Kuchling | 961fe17 | 2000-06-03 19:41:42 +0000 | [diff] [blame] | 339 | return NULL; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 340 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 341 | #ifdef MS_WINDOWS |
Mark Hammond | 071864a | 2000-07-30 02:46:26 +0000 | [diff] [blame] | 342 | if (self->file_handle != INVALID_HANDLE_VALUE) { |
Martin v. Löwis | 1518607 | 2006-02-18 12:38:35 +0000 | [diff] [blame] | 343 | DWORD low,high; |
| 344 | PY_LONG_LONG size; |
| 345 | low = GetFileSize(self->file_handle, &high); |
| 346 | if (low == INVALID_FILE_SIZE) { |
| 347 | /* It might be that the function appears to have failed, |
| 348 | when indeed its size equals INVALID_FILE_SIZE */ |
| 349 | DWORD error = GetLastError(); |
| 350 | if (error != NO_ERROR) |
| 351 | return PyErr_SetFromWindowsErr(error); |
| 352 | } |
| 353 | if (!high && low < LONG_MAX) |
| 354 | return PyInt_FromLong((long)low); |
| 355 | size = (((PY_LONG_LONG)high)<<32) + low; |
| 356 | return PyLong_FromLongLong(size); |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 357 | } else { |
Martin v. Löwis | 1518607 | 2006-02-18 12:38:35 +0000 | [diff] [blame] | 358 | return PyInt_FromSsize_t(self->size); |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 359 | } |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 360 | #endif /* MS_WINDOWS */ |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 361 | |
| 362 | #ifdef UNIX |
Andrew M. Kuchling | 7b9fb92 | 2000-06-17 22:41:22 +0000 | [diff] [blame] | 363 | { |
| 364 | struct stat buf; |
| 365 | if (-1 == fstat(self->fd, &buf)) { |
| 366 | PyErr_SetFromErrno(mmap_module_error); |
| 367 | return NULL; |
| 368 | } |
Martin v. Löwis | 1518607 | 2006-02-18 12:38:35 +0000 | [diff] [blame] | 369 | return PyInt_FromSsize_t(buf.st_size); |
Andrew M. Kuchling | 7b9fb92 | 2000-06-17 22:41:22 +0000 | [diff] [blame] | 370 | } |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 371 | #endif /* UNIX */ |
| 372 | } |
| 373 | |
| 374 | /* This assumes that you want the entire file mapped, |
| 375 | / and when recreating the map will make the new file |
| 376 | / have the new size |
| 377 | / |
| 378 | / Is this really necessary? This could easily be done |
| 379 | / from python by just closing and re-opening with the |
| 380 | / new size? |
| 381 | */ |
| 382 | |
| 383 | static PyObject * |
Fredrik Lundh | 54cf3dc | 2000-07-08 22:05:01 +0000 | [diff] [blame] | 384 | mmap_resize_method(mmap_object *self, |
| 385 | PyObject *args) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 386 | { |
Martin v. Löwis | cfe7e09 | 2006-02-17 06:59:14 +0000 | [diff] [blame] | 387 | Py_ssize_t new_size; |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 388 | CHECK_VALID(NULL); |
Martin v. Löwis | cfe7e09 | 2006-02-17 06:59:14 +0000 | [diff] [blame] | 389 | if (!PyArg_ParseTuple(args, "n:resize", &new_size) || |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 390 | !is_resizeable(self)) { |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 391 | return NULL; |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 392 | #ifdef MS_WINDOWS |
Tim Peters | ec0a5f0 | 2006-02-16 23:47:20 +0000 | [diff] [blame] | 393 | } else { |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 394 | DWORD dwErrCode = 0; |
Martin v. Löwis | 1518607 | 2006-02-18 12:38:35 +0000 | [diff] [blame] | 395 | DWORD newSizeLow, newSizeHigh; |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 396 | /* First, unmap the file view */ |
Tim Peters | 8f9cc29 | 2006-02-17 00:00:20 +0000 | [diff] [blame] | 397 | UnmapViewOfFile(self->data); |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 398 | /* Close the mapping object */ |
Tim Peters | 8f9cc29 | 2006-02-17 00:00:20 +0000 | [diff] [blame] | 399 | CloseHandle(self->map_handle); |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 400 | /* Move to the desired EOF position */ |
Martin v. Löwis | 1518607 | 2006-02-18 12:38:35 +0000 | [diff] [blame] | 401 | #if SIZEOF_SIZE_T > 4 |
| 402 | newSizeHigh = (DWORD)(new_size >> 32); |
| 403 | newSizeLow = (DWORD)(new_size & 0xFFFFFFFF); |
| 404 | #else |
| 405 | newSizeHigh = 0; |
Martin v. Löwis | 5bb8a15 | 2006-02-18 12:49:49 +0000 | [diff] [blame] | 406 | newSizeLow = (DWORD)new_size; |
Martin v. Löwis | 1518607 | 2006-02-18 12:38:35 +0000 | [diff] [blame] | 407 | #endif |
Tim Peters | 8f9cc29 | 2006-02-17 00:00:20 +0000 | [diff] [blame] | 408 | SetFilePointer(self->file_handle, |
Martin v. Löwis | 1518607 | 2006-02-18 12:38:35 +0000 | [diff] [blame] | 409 | newSizeLow, &newSizeHigh, FILE_BEGIN); |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 410 | /* Change the size of the file */ |
Tim Peters | 8f9cc29 | 2006-02-17 00:00:20 +0000 | [diff] [blame] | 411 | SetEndOfFile(self->file_handle); |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 412 | /* Create another mapping object and remap the file view */ |
Tim Peters | 8f9cc29 | 2006-02-17 00:00:20 +0000 | [diff] [blame] | 413 | self->map_handle = CreateFileMapping( |
Mark Hammond | 071864a | 2000-07-30 02:46:26 +0000 | [diff] [blame] | 414 | self->file_handle, |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 415 | NULL, |
| 416 | PAGE_READWRITE, |
Martin v. Löwis | 1518607 | 2006-02-18 12:38:35 +0000 | [diff] [blame] | 417 | newSizeHigh, |
| 418 | newSizeLow, |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 419 | self->tagname); |
| 420 | if (self->map_handle != NULL) { |
Tim Peters | 8f9cc29 | 2006-02-17 00:00:20 +0000 | [diff] [blame] | 421 | self->data = (char *) MapViewOfFile(self->map_handle, |
| 422 | FILE_MAP_WRITE, |
| 423 | 0, |
| 424 | 0, |
| 425 | 0); |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 426 | if (self->data != NULL) { |
| 427 | self->size = new_size; |
Tim Peters | 8f9cc29 | 2006-02-17 00:00:20 +0000 | [diff] [blame] | 428 | Py_INCREF(Py_None); |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 429 | return Py_None; |
| 430 | } else { |
| 431 | dwErrCode = GetLastError(); |
| 432 | } |
| 433 | } else { |
| 434 | dwErrCode = GetLastError(); |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 435 | } |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 436 | PyErr_SetFromWindowsErr(dwErrCode); |
Tim Peters | 23721ee | 2006-02-16 23:50:16 +0000 | [diff] [blame] | 437 | return NULL; |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 438 | #endif /* MS_WINDOWS */ |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 439 | |
| 440 | #ifdef UNIX |
Tim Peters | ec0a5f0 | 2006-02-16 23:47:20 +0000 | [diff] [blame] | 441 | #ifndef HAVE_MREMAP |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 442 | } else { |
| 443 | PyErr_SetString(PyExc_SystemError, |
| 444 | "mmap: resizing not available--no mremap()"); |
| 445 | return NULL; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 446 | #else |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 447 | } else { |
Armin Rigo | 335ffe8 | 2005-09-20 19:04:02 +0000 | [diff] [blame] | 448 | void *newmap; |
| 449 | |
Georg Brandl | 38387b8 | 2005-08-24 07:17:40 +0000 | [diff] [blame] | 450 | if (ftruncate(self->fd, new_size) == -1) { |
| 451 | PyErr_SetFromErrno(mmap_module_error); |
| 452 | return NULL; |
| 453 | } |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 454 | |
Andrew M. Kuchling | 6fef30e | 2000-06-18 14:51:21 +0000 | [diff] [blame] | 455 | #ifdef MREMAP_MAYMOVE |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 456 | newmap = mremap(self->data, self->size, new_size, MREMAP_MAYMOVE); |
Andrew M. Kuchling | 6fef30e | 2000-06-18 14:51:21 +0000 | [diff] [blame] | 457 | #else |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 458 | newmap = mremap(self->data, self->size, new_size, 0); |
Andrew M. Kuchling | 6fef30e | 2000-06-18 14:51:21 +0000 | [diff] [blame] | 459 | #endif |
Tim Peters | ec0a5f0 | 2006-02-16 23:47:20 +0000 | [diff] [blame] | 460 | if (newmap == (void *)-1) |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 461 | { |
| 462 | PyErr_SetFromErrno(mmap_module_error); |
| 463 | return NULL; |
| 464 | } |
| 465 | self->data = newmap; |
| 466 | self->size = new_size; |
| 467 | Py_INCREF(Py_None); |
| 468 | return Py_None; |
Andrew M. Kuchling | 6fef30e | 2000-06-18 14:51:21 +0000 | [diff] [blame] | 469 | #endif /* HAVE_MREMAP */ |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 470 | #endif /* UNIX */ |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 471 | } |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | static PyObject * |
Fredrik Lundh | 54cf3dc | 2000-07-08 22:05:01 +0000 | [diff] [blame] | 475 | mmap_tell_method(mmap_object *self, PyObject *args) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 476 | { |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 477 | CHECK_VALID(NULL); |
Andrew M. Kuchling | 841b9fb | 2000-06-03 20:43:43 +0000 | [diff] [blame] | 478 | if (!PyArg_ParseTuple(args, ":tell")) |
Andrew M. Kuchling | 961fe17 | 2000-06-03 19:41:42 +0000 | [diff] [blame] | 479 | return NULL; |
Tim Peters | 8f9cc29 | 2006-02-17 00:00:20 +0000 | [diff] [blame] | 480 | return Py_BuildValue("l", (long) self->pos); |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | static PyObject * |
Fredrik Lundh | 54cf3dc | 2000-07-08 22:05:01 +0000 | [diff] [blame] | 484 | mmap_flush_method(mmap_object *self, PyObject *args) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 485 | { |
Martin v. Löwis | ad0a462 | 2006-02-16 14:30:23 +0000 | [diff] [blame] | 486 | Py_ssize_t offset = 0; |
| 487 | Py_ssize_t size = self->size; |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 488 | CHECK_VALID(NULL); |
Tim Peters | f288295 | 2006-02-17 01:07:39 +0000 | [diff] [blame] | 489 | if (!PyArg_ParseTuple(args, "|nn:flush", &offset, &size)) |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 490 | return NULL; |
Tim Peters | f288295 | 2006-02-17 01:07:39 +0000 | [diff] [blame] | 491 | if ((size_t)(offset + size) > self->size) { |
Tim Peters | 8f9cc29 | 2006-02-17 00:00:20 +0000 | [diff] [blame] | 492 | PyErr_SetString(PyExc_ValueError, "flush values out of range"); |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 493 | return NULL; |
| 494 | } else { |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 495 | #ifdef MS_WINDOWS |
Tim Peters | 23721ee | 2006-02-16 23:50:16 +0000 | [diff] [blame] | 496 | return Py_BuildValue("l", (long) |
| 497 | FlushViewOfFile(self->data+offset, size)); |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 498 | #endif /* MS_WINDOWS */ |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 499 | #ifdef UNIX |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 500 | /* XXX semantics of return value? */ |
| 501 | /* XXX flags for msync? */ |
| 502 | if (-1 == msync(self->data + offset, size, |
Andrew M. Kuchling | 9bc5f33 | 2000-06-18 04:25:08 +0000 | [diff] [blame] | 503 | MS_SYNC)) |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 504 | { |
| 505 | PyErr_SetFromErrno(mmap_module_error); |
| 506 | return NULL; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 507 | } |
Tim Peters | 8f9cc29 | 2006-02-17 00:00:20 +0000 | [diff] [blame] | 508 | return Py_BuildValue("l", (long) 0); |
Tim Peters | ec0a5f0 | 2006-02-16 23:47:20 +0000 | [diff] [blame] | 509 | #endif /* UNIX */ |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 510 | } |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | static PyObject * |
Fredrik Lundh | 54cf3dc | 2000-07-08 22:05:01 +0000 | [diff] [blame] | 514 | mmap_seek_method(mmap_object *self, PyObject *args) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 515 | { |
Martin v. Löwis | cfe7e09 | 2006-02-17 06:59:14 +0000 | [diff] [blame] | 516 | Py_ssize_t dist; |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 517 | int how=0; |
| 518 | CHECK_VALID(NULL); |
Martin v. Löwis | cfe7e09 | 2006-02-17 06:59:14 +0000 | [diff] [blame] | 519 | if (!PyArg_ParseTuple(args, "n|i:seek", &dist, &how)) |
Tim Peters | 8f9cc29 | 2006-02-17 00:00:20 +0000 | [diff] [blame] | 520 | return NULL; |
| 521 | else { |
Andrew M. Kuchling | 70d2742 | 2000-06-18 04:45:14 +0000 | [diff] [blame] | 522 | size_t where; |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 523 | switch (how) { |
Andrew M. Kuchling | 70d2742 | 2000-06-18 04:45:14 +0000 | [diff] [blame] | 524 | case 0: /* relative to start */ |
| 525 | if (dist < 0) |
| 526 | goto onoutofrange; |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 527 | where = dist; |
| 528 | break; |
Andrew M. Kuchling | 70d2742 | 2000-06-18 04:45:14 +0000 | [diff] [blame] | 529 | case 1: /* relative to current position */ |
Martin v. Löwis | cfe7e09 | 2006-02-17 06:59:14 +0000 | [diff] [blame] | 530 | if ((Py_ssize_t)self->pos + dist < 0) |
Andrew M. Kuchling | 70d2742 | 2000-06-18 04:45:14 +0000 | [diff] [blame] | 531 | goto onoutofrange; |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 532 | where = self->pos + dist; |
| 533 | break; |
Andrew M. Kuchling | 70d2742 | 2000-06-18 04:45:14 +0000 | [diff] [blame] | 534 | case 2: /* relative to end */ |
Martin v. Löwis | cfe7e09 | 2006-02-17 06:59:14 +0000 | [diff] [blame] | 535 | if ((Py_ssize_t)self->size + dist < 0) |
Andrew M. Kuchling | 70d2742 | 2000-06-18 04:45:14 +0000 | [diff] [blame] | 536 | goto onoutofrange; |
| 537 | where = self->size + dist; |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 538 | break; |
| 539 | default: |
Tim Peters | 8f9cc29 | 2006-02-17 00:00:20 +0000 | [diff] [blame] | 540 | PyErr_SetString(PyExc_ValueError, "unknown seek type"); |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 541 | return NULL; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 542 | } |
Andrew M. Kuchling | 70d2742 | 2000-06-18 04:45:14 +0000 | [diff] [blame] | 543 | if (where > self->size) |
| 544 | goto onoutofrange; |
| 545 | self->pos = where; |
Tim Peters | 8f9cc29 | 2006-02-17 00:00:20 +0000 | [diff] [blame] | 546 | Py_INCREF(Py_None); |
Tim Peters | 23721ee | 2006-02-16 23:50:16 +0000 | [diff] [blame] | 547 | return Py_None; |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 548 | } |
Andrew M. Kuchling | 70d2742 | 2000-06-18 04:45:14 +0000 | [diff] [blame] | 549 | |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 550 | onoutofrange: |
Tim Peters | 8f9cc29 | 2006-02-17 00:00:20 +0000 | [diff] [blame] | 551 | PyErr_SetString(PyExc_ValueError, "seek out of range"); |
Andrew M. Kuchling | 70d2742 | 2000-06-18 04:45:14 +0000 | [diff] [blame] | 552 | return NULL; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 553 | } |
| 554 | |
| 555 | static PyObject * |
Fredrik Lundh | 54cf3dc | 2000-07-08 22:05:01 +0000 | [diff] [blame] | 556 | mmap_move_method(mmap_object *self, PyObject *args) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 557 | { |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 558 | unsigned long dest, src, count; |
| 559 | CHECK_VALID(NULL); |
Tim Peters | 8f9cc29 | 2006-02-17 00:00:20 +0000 | [diff] [blame] | 560 | if (!PyArg_ParseTuple(args, "kkk:move", &dest, &src, &count) || |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 561 | !is_writeable(self)) { |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 562 | return NULL; |
| 563 | } else { |
| 564 | /* bounds check the values */ |
| 565 | if (/* end of source after end of data?? */ |
| 566 | ((src+count) > self->size) |
| 567 | /* dest will fit? */ |
| 568 | || (dest+count > self->size)) { |
Tim Peters | 8f9cc29 | 2006-02-17 00:00:20 +0000 | [diff] [blame] | 569 | PyErr_SetString(PyExc_ValueError, |
| 570 | "source or destination out of range"); |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 571 | return NULL; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 572 | } else { |
Tim Peters | 8f9cc29 | 2006-02-17 00:00:20 +0000 | [diff] [blame] | 573 | memmove(self->data+dest, self->data+src, count); |
| 574 | Py_INCREF(Py_None); |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 575 | return Py_None; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 576 | } |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 577 | } |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 578 | } |
| 579 | |
| 580 | static struct PyMethodDef mmap_object_methods[] = { |
Martin v. Löwis | 43b936d | 2002-01-17 23:15:58 +0000 | [diff] [blame] | 581 | {"close", (PyCFunction) mmap_close_method, METH_VARARGS}, |
| 582 | {"find", (PyCFunction) mmap_find_method, METH_VARARGS}, |
| 583 | {"flush", (PyCFunction) mmap_flush_method, METH_VARARGS}, |
| 584 | {"move", (PyCFunction) mmap_move_method, METH_VARARGS}, |
| 585 | {"read", (PyCFunction) mmap_read_method, METH_VARARGS}, |
| 586 | {"read_byte", (PyCFunction) mmap_read_byte_method, METH_VARARGS}, |
| 587 | {"readline", (PyCFunction) mmap_read_line_method, METH_VARARGS}, |
| 588 | {"resize", (PyCFunction) mmap_resize_method, METH_VARARGS}, |
| 589 | {"seek", (PyCFunction) mmap_seek_method, METH_VARARGS}, |
| 590 | {"size", (PyCFunction) mmap_size_method, METH_VARARGS}, |
| 591 | {"tell", (PyCFunction) mmap_tell_method, METH_VARARGS}, |
| 592 | {"write", (PyCFunction) mmap_write_method, METH_VARARGS}, |
| 593 | {"write_byte", (PyCFunction) mmap_write_byte_method, METH_VARARGS}, |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 594 | {NULL, NULL} /* sentinel */ |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 595 | }; |
| 596 | |
| 597 | /* Functions for treating an mmap'ed file as a buffer */ |
| 598 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 599 | static Py_ssize_t |
| 600 | mmap_buffer_getreadbuf(mmap_object *self, Py_ssize_t index, const void **ptr) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 601 | { |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 602 | CHECK_VALID(-1); |
Tim Peters | 8f9cc29 | 2006-02-17 00:00:20 +0000 | [diff] [blame] | 603 | if (index != 0) { |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 604 | PyErr_SetString(PyExc_SystemError, |
| 605 | "Accessing non-existent mmap segment"); |
| 606 | return -1; |
| 607 | } |
| 608 | *ptr = self->data; |
| 609 | return self->size; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 610 | } |
| 611 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 612 | static Py_ssize_t |
| 613 | mmap_buffer_getwritebuf(mmap_object *self, Py_ssize_t index, const void **ptr) |
Tim Peters | ec0a5f0 | 2006-02-16 23:47:20 +0000 | [diff] [blame] | 614 | { |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 615 | CHECK_VALID(-1); |
Tim Peters | 8f9cc29 | 2006-02-17 00:00:20 +0000 | [diff] [blame] | 616 | if (index != 0) { |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 617 | PyErr_SetString(PyExc_SystemError, |
| 618 | "Accessing non-existent mmap segment"); |
| 619 | return -1; |
| 620 | } |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 621 | if (!is_writeable(self)) |
| 622 | return -1; |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 623 | *ptr = self->data; |
| 624 | return self->size; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 625 | } |
| 626 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 627 | static Py_ssize_t |
| 628 | mmap_buffer_getsegcount(mmap_object *self, Py_ssize_t *lenp) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 629 | { |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 630 | CHECK_VALID(-1); |
Tim Peters | ec0a5f0 | 2006-02-16 23:47:20 +0000 | [diff] [blame] | 631 | if (lenp) |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 632 | *lenp = self->size; |
| 633 | return 1; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 634 | } |
| 635 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 636 | static Py_ssize_t |
| 637 | mmap_buffer_getcharbuffer(mmap_object *self, Py_ssize_t index, const void **ptr) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 638 | { |
Tim Peters | 8f9cc29 | 2006-02-17 00:00:20 +0000 | [diff] [blame] | 639 | if (index != 0) { |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 640 | PyErr_SetString(PyExc_SystemError, |
| 641 | "accessing non-existent buffer segment"); |
| 642 | return -1; |
| 643 | } |
| 644 | *ptr = (const char *)self->data; |
| 645 | return self->size; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 646 | } |
| 647 | |
| 648 | static PyObject * |
Fredrik Lundh | 54cf3dc | 2000-07-08 22:05:01 +0000 | [diff] [blame] | 649 | mmap_object_getattr(mmap_object *self, char *name) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 650 | { |
Tim Peters | 8f9cc29 | 2006-02-17 00:00:20 +0000 | [diff] [blame] | 651 | return Py_FindMethod(mmap_object_methods, (PyObject *)self, name); |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 652 | } |
| 653 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 654 | static Py_ssize_t |
Fredrik Lundh | 54cf3dc | 2000-07-08 22:05:01 +0000 | [diff] [blame] | 655 | mmap_length(mmap_object *self) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 656 | { |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 657 | CHECK_VALID(-1); |
| 658 | return self->size; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 659 | } |
| 660 | |
| 661 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 662 | mmap_item(mmap_object *self, Py_ssize_t i) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 663 | { |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 664 | CHECK_VALID(NULL); |
Guido van Rossum | ce8e1dc | 2000-07-01 00:51:51 +0000 | [diff] [blame] | 665 | if (i < 0 || (size_t)i >= self->size) { |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 666 | PyErr_SetString(PyExc_IndexError, "mmap index out of range"); |
| 667 | return NULL; |
| 668 | } |
| 669 | return PyString_FromStringAndSize(self->data + i, 1); |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 670 | } |
| 671 | |
| 672 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 673 | mmap_slice(mmap_object *self, Py_ssize_t ilow, Py_ssize_t ihigh) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 674 | { |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 675 | CHECK_VALID(NULL); |
| 676 | if (ilow < 0) |
| 677 | ilow = 0; |
Guido van Rossum | ce8e1dc | 2000-07-01 00:51:51 +0000 | [diff] [blame] | 678 | else if ((size_t)ilow > self->size) |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 679 | ilow = self->size; |
| 680 | if (ihigh < 0) |
| 681 | ihigh = 0; |
| 682 | if (ihigh < ilow) |
| 683 | ihigh = ilow; |
Guido van Rossum | ce8e1dc | 2000-07-01 00:51:51 +0000 | [diff] [blame] | 684 | else if ((size_t)ihigh > self->size) |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 685 | ihigh = self->size; |
Tim Peters | ec0a5f0 | 2006-02-16 23:47:20 +0000 | [diff] [blame] | 686 | |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 687 | return PyString_FromStringAndSize(self->data + ilow, ihigh-ilow); |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 688 | } |
| 689 | |
| 690 | static PyObject * |
Fredrik Lundh | 54cf3dc | 2000-07-08 22:05:01 +0000 | [diff] [blame] | 691 | mmap_concat(mmap_object *self, PyObject *bb) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 692 | { |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 693 | CHECK_VALID(NULL); |
| 694 | PyErr_SetString(PyExc_SystemError, |
| 695 | "mmaps don't support concatenation"); |
| 696 | return NULL; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 697 | } |
| 698 | |
| 699 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 700 | mmap_repeat(mmap_object *self, Py_ssize_t n) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 701 | { |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 702 | CHECK_VALID(NULL); |
| 703 | PyErr_SetString(PyExc_SystemError, |
| 704 | "mmaps don't support repeat operation"); |
| 705 | return NULL; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 706 | } |
| 707 | |
| 708 | static int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 709 | mmap_ass_slice(mmap_object *self, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 710 | { |
Guido van Rossum | 36d4f8b | 2000-04-10 21:34:37 +0000 | [diff] [blame] | 711 | const char *buf; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 712 | |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 713 | CHECK_VALID(-1); |
| 714 | if (ilow < 0) |
| 715 | ilow = 0; |
Guido van Rossum | ce8e1dc | 2000-07-01 00:51:51 +0000 | [diff] [blame] | 716 | else if ((size_t)ilow > self->size) |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 717 | ilow = self->size; |
| 718 | if (ihigh < 0) |
| 719 | ihigh = 0; |
| 720 | if (ihigh < ilow) |
| 721 | ihigh = ilow; |
Guido van Rossum | ce8e1dc | 2000-07-01 00:51:51 +0000 | [diff] [blame] | 722 | else if ((size_t)ihigh > self->size) |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 723 | ihigh = self->size; |
Tim Peters | ec0a5f0 | 2006-02-16 23:47:20 +0000 | [diff] [blame] | 724 | |
Thomas Wouters | 1baac72 | 2001-07-16 15:47:36 +0000 | [diff] [blame] | 725 | if (v == NULL) { |
| 726 | PyErr_SetString(PyExc_TypeError, |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 727 | "mmap object doesn't support slice deletion"); |
Thomas Wouters | 1baac72 | 2001-07-16 15:47:36 +0000 | [diff] [blame] | 728 | return -1; |
| 729 | } |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 730 | if (! (PyString_Check(v)) ) { |
Tim Peters | ec0a5f0 | 2006-02-16 23:47:20 +0000 | [diff] [blame] | 731 | PyErr_SetString(PyExc_IndexError, |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 732 | "mmap slice assignment must be a string"); |
| 733 | return -1; |
| 734 | } |
Tim Peters | 8f9cc29 | 2006-02-17 00:00:20 +0000 | [diff] [blame] | 735 | if (PyString_Size(v) != (ihigh - ilow)) { |
Tim Peters | ec0a5f0 | 2006-02-16 23:47:20 +0000 | [diff] [blame] | 736 | PyErr_SetString(PyExc_IndexError, |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 737 | "mmap slice assignment is wrong size"); |
| 738 | return -1; |
| 739 | } |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 740 | if (!is_writeable(self)) |
| 741 | return -1; |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 742 | buf = PyString_AsString(v); |
| 743 | memcpy(self->data + ilow, buf, ihigh-ilow); |
| 744 | return 0; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 745 | } |
| 746 | |
| 747 | static int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 748 | 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] | 749 | { |
Guido van Rossum | 36d4f8b | 2000-04-10 21:34:37 +0000 | [diff] [blame] | 750 | const char *buf; |
Tim Peters | ec0a5f0 | 2006-02-16 23:47:20 +0000 | [diff] [blame] | 751 | |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 752 | CHECK_VALID(-1); |
Guido van Rossum | ce8e1dc | 2000-07-01 00:51:51 +0000 | [diff] [blame] | 753 | if (i < 0 || (size_t)i >= self->size) { |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 754 | PyErr_SetString(PyExc_IndexError, "mmap index out of range"); |
| 755 | return -1; |
| 756 | } |
Thomas Wouters | 1baac72 | 2001-07-16 15:47:36 +0000 | [diff] [blame] | 757 | if (v == NULL) { |
| 758 | PyErr_SetString(PyExc_TypeError, |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 759 | "mmap object doesn't support item deletion"); |
Thomas Wouters | 1baac72 | 2001-07-16 15:47:36 +0000 | [diff] [blame] | 760 | return -1; |
| 761 | } |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 762 | if (! (PyString_Check(v) && PyString_Size(v)==1) ) { |
Tim Peters | ec0a5f0 | 2006-02-16 23:47:20 +0000 | [diff] [blame] | 763 | PyErr_SetString(PyExc_IndexError, |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 764 | "mmap assignment must be single-character string"); |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 765 | return -1; |
| 766 | } |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 767 | if (!is_writeable(self)) |
| 768 | return -1; |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 769 | buf = PyString_AsString(v); |
| 770 | self->data[i] = buf[0]; |
| 771 | return 0; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 772 | } |
| 773 | |
| 774 | static PySequenceMethods mmap_as_sequence = { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 775 | (lenfunc)mmap_length, /*sq_length*/ |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 776 | (binaryfunc)mmap_concat, /*sq_concat*/ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 777 | (ssizeargfunc)mmap_repeat, /*sq_repeat*/ |
| 778 | (ssizeargfunc)mmap_item, /*sq_item*/ |
| 779 | (ssizessizeargfunc)mmap_slice, /*sq_slice*/ |
| 780 | (ssizeobjargproc)mmap_ass_item, /*sq_ass_item*/ |
| 781 | (ssizessizeobjargproc)mmap_ass_slice, /*sq_ass_slice*/ |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 782 | }; |
| 783 | |
| 784 | static PyBufferProcs mmap_as_buffer = { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 785 | (readbufferproc)mmap_buffer_getreadbuf, |
| 786 | (writebufferproc)mmap_buffer_getwritebuf, |
| 787 | (segcountproc)mmap_buffer_getsegcount, |
| 788 | (charbufferproc)mmap_buffer_getcharbuffer, |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 789 | }; |
| 790 | |
| 791 | static PyTypeObject mmap_object_type = { |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 792 | PyObject_HEAD_INIT(0) /* patched in module init */ |
| 793 | 0, /* ob_size */ |
Guido van Rossum | 1464839 | 2001-12-08 18:02:58 +0000 | [diff] [blame] | 794 | "mmap.mmap", /* tp_name */ |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 795 | sizeof(mmap_object), /* tp_size */ |
| 796 | 0, /* tp_itemsize */ |
| 797 | /* methods */ |
| 798 | (destructor) mmap_object_dealloc, /* tp_dealloc */ |
| 799 | 0, /* tp_print */ |
| 800 | (getattrfunc) mmap_object_getattr, /* tp_getattr */ |
| 801 | 0, /* tp_setattr */ |
| 802 | 0, /* tp_compare */ |
| 803 | 0, /* tp_repr */ |
| 804 | 0, /* tp_as_number */ |
| 805 | &mmap_as_sequence, /*tp_as_sequence*/ |
| 806 | 0, /*tp_as_mapping*/ |
| 807 | 0, /*tp_hash*/ |
| 808 | 0, /*tp_call*/ |
| 809 | 0, /*tp_str*/ |
| 810 | 0, /*tp_getattro*/ |
| 811 | 0, /*tp_setattro*/ |
| 812 | &mmap_as_buffer, /*tp_as_buffer*/ |
| 813 | Py_TPFLAGS_HAVE_GETCHARBUFFER, /*tp_flags*/ |
| 814 | 0, /*tp_doc*/ |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 815 | }; |
| 816 | |
Andrew M. Kuchling | 70d2742 | 2000-06-18 04:45:14 +0000 | [diff] [blame] | 817 | |
| 818 | /* extract the map size from the given PyObject |
| 819 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 820 | Returns -1 on error, with an appropriate Python exception raised. On |
Andrew M. Kuchling | 70d2742 | 2000-06-18 04:45:14 +0000 | [diff] [blame] | 821 | success, the map size is returned. */ |
Martin v. Löwis | cfe7e09 | 2006-02-17 06:59:14 +0000 | [diff] [blame] | 822 | static Py_ssize_t |
Fredrik Lundh | 54cf3dc | 2000-07-08 22:05:01 +0000 | [diff] [blame] | 823 | _GetMapSize(PyObject *o) |
Andrew M. Kuchling | 70d2742 | 2000-06-18 04:45:14 +0000 | [diff] [blame] | 824 | { |
| 825 | if (PyInt_Check(o)) { |
| 826 | long i = PyInt_AsLong(o); |
| 827 | if (PyErr_Occurred()) |
| 828 | return -1; |
| 829 | if (i < 0) |
| 830 | goto onnegoverflow; |
Martin v. Löwis | cfe7e09 | 2006-02-17 06:59:14 +0000 | [diff] [blame] | 831 | return i; |
Andrew M. Kuchling | 70d2742 | 2000-06-18 04:45:14 +0000 | [diff] [blame] | 832 | } |
| 833 | else if (PyLong_Check(o)) { |
Martin v. Löwis | cfe7e09 | 2006-02-17 06:59:14 +0000 | [diff] [blame] | 834 | Py_ssize_t i = PyInt_AsSsize_t(o); |
Andrew M. Kuchling | 70d2742 | 2000-06-18 04:45:14 +0000 | [diff] [blame] | 835 | if (PyErr_Occurred()) { |
| 836 | /* yes negative overflow is mistaken for positive overflow |
| 837 | but not worth the trouble to check sign of 'i' */ |
| 838 | if (PyErr_ExceptionMatches(PyExc_OverflowError)) |
| 839 | goto onposoverflow; |
| 840 | else |
| 841 | return -1; |
| 842 | } |
| 843 | if (i < 0) |
| 844 | goto onnegoverflow; |
Martin v. Löwis | cfe7e09 | 2006-02-17 06:59:14 +0000 | [diff] [blame] | 845 | return i; |
Andrew M. Kuchling | 70d2742 | 2000-06-18 04:45:14 +0000 | [diff] [blame] | 846 | } |
| 847 | else { |
| 848 | PyErr_SetString(PyExc_TypeError, |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 849 | "map size must be an integral value"); |
Andrew M. Kuchling | 70d2742 | 2000-06-18 04:45:14 +0000 | [diff] [blame] | 850 | return -1; |
| 851 | } |
| 852 | |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 853 | onnegoverflow: |
Andrew M. Kuchling | 70d2742 | 2000-06-18 04:45:14 +0000 | [diff] [blame] | 854 | PyErr_SetString(PyExc_OverflowError, |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 855 | "memory mapped size must be positive"); |
Andrew M. Kuchling | 70d2742 | 2000-06-18 04:45:14 +0000 | [diff] [blame] | 856 | return -1; |
| 857 | |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 858 | onposoverflow: |
Andrew M. Kuchling | 70d2742 | 2000-06-18 04:45:14 +0000 | [diff] [blame] | 859 | PyErr_SetString(PyExc_OverflowError, |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 860 | "memory mapped size is too large (limited by C int)"); |
Andrew M. Kuchling | 70d2742 | 2000-06-18 04:45:14 +0000 | [diff] [blame] | 861 | return -1; |
| 862 | } |
| 863 | |
Tim Peters | ec0a5f0 | 2006-02-16 23:47:20 +0000 | [diff] [blame] | 864 | #ifdef UNIX |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 865 | static PyObject * |
Fredrik Lundh | 54cf3dc | 2000-07-08 22:05:01 +0000 | [diff] [blame] | 866 | new_mmap_object(PyObject *self, PyObject *args, PyObject *kwdict) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 867 | { |
Neal Norwitz | b567392 | 2002-09-05 21:48:07 +0000 | [diff] [blame] | 868 | #ifdef HAVE_FSTAT |
| 869 | struct stat st; |
| 870 | #endif |
Fredrik Lundh | 54cf3dc | 2000-07-08 22:05:01 +0000 | [diff] [blame] | 871 | mmap_object *m_obj; |
Andrew M. Kuchling | 70d2742 | 2000-06-18 04:45:14 +0000 | [diff] [blame] | 872 | PyObject *map_size_obj = NULL; |
Martin v. Löwis | cfe7e09 | 2006-02-17 06:59:14 +0000 | [diff] [blame] | 873 | Py_ssize_t map_size; |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 874 | int fd, flags = MAP_SHARED, prot = PROT_WRITE | PROT_READ; |
Neal Norwitz | 0e6bc8c | 2006-02-05 05:45:43 +0000 | [diff] [blame] | 875 | int devzero = -1; |
Neal Norwitz | 8856fb7 | 2005-12-18 03:34:22 +0000 | [diff] [blame] | 876 | int access = (int)ACCESS_DEFAULT; |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame^] | 877 | static char *keywords[] = {"fileno", "length", |
Tim Peters | ec0a5f0 | 2006-02-16 23:47:20 +0000 | [diff] [blame] | 878 | "flags", "prot", |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 879 | "access", NULL}; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 880 | |
Tim Peters | ec0a5f0 | 2006-02-16 23:47:20 +0000 | [diff] [blame] | 881 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iO|iii", keywords, |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 882 | &fd, &map_size_obj, &flags, &prot, |
| 883 | &access)) |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 884 | return NULL; |
Andrew M. Kuchling | 70d2742 | 2000-06-18 04:45:14 +0000 | [diff] [blame] | 885 | map_size = _GetMapSize(map_size_obj); |
| 886 | if (map_size < 0) |
| 887 | return NULL; |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 888 | |
Tim Peters | ec0a5f0 | 2006-02-16 23:47:20 +0000 | [diff] [blame] | 889 | if ((access != (int)ACCESS_DEFAULT) && |
Neal Norwitz | d1cfc8a | 2006-02-05 03:36:57 +0000 | [diff] [blame] | 890 | ((flags != MAP_SHARED) || (prot != (PROT_WRITE | PROT_READ)))) |
Tim Peters | ec0a5f0 | 2006-02-16 23:47:20 +0000 | [diff] [blame] | 891 | return PyErr_Format(PyExc_ValueError, |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 892 | "mmap can't specify both access and flags, prot."); |
Neal Norwitz | d1cfc8a | 2006-02-05 03:36:57 +0000 | [diff] [blame] | 893 | switch ((access_mode)access) { |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 894 | case ACCESS_READ: |
| 895 | flags = MAP_SHARED; |
| 896 | prot = PROT_READ; |
| 897 | break; |
| 898 | case ACCESS_WRITE: |
| 899 | flags = MAP_SHARED; |
| 900 | prot = PROT_READ | PROT_WRITE; |
| 901 | break; |
| 902 | case ACCESS_COPY: |
| 903 | flags = MAP_PRIVATE; |
| 904 | prot = PROT_READ | PROT_WRITE; |
| 905 | break; |
Tim Peters | ec0a5f0 | 2006-02-16 23:47:20 +0000 | [diff] [blame] | 906 | case ACCESS_DEFAULT: |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 907 | /* use the specified or default values of flags and prot */ |
| 908 | break; |
| 909 | default: |
Tim Peters | ec0a5f0 | 2006-02-16 23:47:20 +0000 | [diff] [blame] | 910 | return PyErr_Format(PyExc_ValueError, |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 911 | "mmap invalid access parameter."); |
| 912 | } |
Neal Norwitz | b567392 | 2002-09-05 21:48:07 +0000 | [diff] [blame] | 913 | |
| 914 | #ifdef HAVE_FSTAT |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 915 | # ifdef __VMS |
| 916 | /* on OpenVMS we must ensure that all bytes are written to the file */ |
| 917 | fsync(fd); |
| 918 | # endif |
Martin v. Löwis | 7fe60c0 | 2005-03-03 11:22:44 +0000 | [diff] [blame] | 919 | if (fstat(fd, &st) == 0 && S_ISREG(st.st_mode)) { |
| 920 | if (map_size == 0) { |
Martin v. Löwis | cfe7e09 | 2006-02-17 06:59:14 +0000 | [diff] [blame] | 921 | map_size = st.st_size; |
Martin v. Löwis | 7fe60c0 | 2005-03-03 11:22:44 +0000 | [diff] [blame] | 922 | } else if ((size_t)map_size > st.st_size) { |
Tim Peters | ec0a5f0 | 2006-02-16 23:47:20 +0000 | [diff] [blame] | 923 | PyErr_SetString(PyExc_ValueError, |
Martin v. Löwis | 7fe60c0 | 2005-03-03 11:22:44 +0000 | [diff] [blame] | 924 | "mmap length is greater than file size"); |
| 925 | return NULL; |
| 926 | } |
Neal Norwitz | b567392 | 2002-09-05 21:48:07 +0000 | [diff] [blame] | 927 | } |
| 928 | #endif |
Tim Peters | 8f9cc29 | 2006-02-17 00:00:20 +0000 | [diff] [blame] | 929 | m_obj = PyObject_New(mmap_object, &mmap_object_type); |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 930 | if (m_obj == NULL) {return NULL;} |
Neal Norwitz | 3b4fff8 | 2006-01-11 08:54:45 +0000 | [diff] [blame] | 931 | m_obj->data = NULL; |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 932 | m_obj->size = (size_t) map_size; |
| 933 | m_obj->pos = (size_t) 0; |
Neal Norwitz | 0e6bc8c | 2006-02-05 05:45:43 +0000 | [diff] [blame] | 934 | if (fd == -1) { |
| 935 | m_obj->fd = -1; |
| 936 | /* Assume the caller wants to map anonymous memory. |
| 937 | This is the same behaviour as Windows. mmap.mmap(-1, size) |
| 938 | on both Windows and Unix map anonymous memory. |
| 939 | */ |
| 940 | #ifdef MAP_ANONYMOUS |
| 941 | /* BSD way to map anonymous memory */ |
| 942 | flags |= MAP_ANONYMOUS; |
| 943 | #else |
| 944 | /* SVR4 method to map anonymous memory is to open /dev/zero */ |
| 945 | fd = devzero = open("/dev/zero", O_RDWR); |
| 946 | if (devzero == -1) { |
| 947 | Py_DECREF(m_obj); |
| 948 | PyErr_SetFromErrno(mmap_module_error); |
| 949 | return NULL; |
| 950 | } |
| 951 | #endif |
| 952 | } else { |
| 953 | m_obj->fd = dup(fd); |
| 954 | if (m_obj->fd == -1) { |
| 955 | Py_DECREF(m_obj); |
| 956 | PyErr_SetFromErrno(mmap_module_error); |
| 957 | return NULL; |
| 958 | } |
Georg Brandl | 38387b8 | 2005-08-24 07:17:40 +0000 | [diff] [blame] | 959 | } |
Neal Norwitz | 0e6bc8c | 2006-02-05 05:45:43 +0000 | [diff] [blame] | 960 | |
Tim Peters | ec0a5f0 | 2006-02-16 23:47:20 +0000 | [diff] [blame] | 961 | m_obj->data = mmap(NULL, map_size, |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 962 | prot, flags, |
| 963 | fd, 0); |
Neal Norwitz | 0e6bc8c | 2006-02-05 05:45:43 +0000 | [diff] [blame] | 964 | |
| 965 | if (devzero != -1) { |
| 966 | close(devzero); |
| 967 | } |
| 968 | |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 969 | if (m_obj->data == (char *)-1) { |
Andrew M. Kuchling | 16581c8 | 2004-05-19 14:39:08 +0000 | [diff] [blame] | 970 | m_obj->data = NULL; |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 971 | Py_DECREF(m_obj); |
| 972 | PyErr_SetFromErrno(mmap_module_error); |
| 973 | return NULL; |
| 974 | } |
Neal Norwitz | 8856fb7 | 2005-12-18 03:34:22 +0000 | [diff] [blame] | 975 | m_obj->access = (access_mode)access; |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 976 | return (PyObject *)m_obj; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 977 | } |
| 978 | #endif /* UNIX */ |
| 979 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 980 | #ifdef MS_WINDOWS |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 981 | static PyObject * |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 982 | new_mmap_object(PyObject *self, PyObject *args, PyObject *kwdict) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 983 | { |
Fredrik Lundh | 54cf3dc | 2000-07-08 22:05:01 +0000 | [diff] [blame] | 984 | mmap_object *m_obj; |
Andrew M. Kuchling | 70d2742 | 2000-06-18 04:45:14 +0000 | [diff] [blame] | 985 | PyObject *map_size_obj = NULL; |
Martin v. Löwis | cfe7e09 | 2006-02-17 06:59:14 +0000 | [diff] [blame] | 986 | Py_ssize_t map_size; |
Tim Peters | e564e7f | 2006-02-16 23:46:01 +0000 | [diff] [blame] | 987 | DWORD size_hi; /* upper 32 bits of m_obj->size */ |
| 988 | DWORD size_lo; /* lower 32 bits of m_obj->size */ |
Fredrik Lundh | 54cf3dc | 2000-07-08 22:05:01 +0000 | [diff] [blame] | 989 | char *tagname = ""; |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 990 | DWORD dwErr = 0; |
| 991 | int fileno; |
Mark Hammond | 071864a | 2000-07-30 02:46:26 +0000 | [diff] [blame] | 992 | HANDLE fh = 0; |
Neal Norwitz | 8856fb7 | 2005-12-18 03:34:22 +0000 | [diff] [blame] | 993 | int access = (access_mode)ACCESS_DEFAULT; |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 994 | DWORD flProtect, dwDesiredAccess; |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame^] | 995 | static char *keywords[] = { "fileno", "length", |
Tim Peters | ec0a5f0 | 2006-02-16 23:47:20 +0000 | [diff] [blame] | 996 | "tagname", |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 997 | "access", NULL }; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 998 | |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 999 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iO|zi", keywords, |
Tim Peters | ec0a5f0 | 2006-02-16 23:47:20 +0000 | [diff] [blame] | 1000 | &fileno, &map_size_obj, |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 1001 | &tagname, &access)) { |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 1002 | return NULL; |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 1003 | } |
| 1004 | |
Neal Norwitz | 8856fb7 | 2005-12-18 03:34:22 +0000 | [diff] [blame] | 1005 | switch((access_mode)access) { |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 1006 | case ACCESS_READ: |
| 1007 | flProtect = PAGE_READONLY; |
| 1008 | dwDesiredAccess = FILE_MAP_READ; |
| 1009 | break; |
| 1010 | case ACCESS_DEFAULT: case ACCESS_WRITE: |
| 1011 | flProtect = PAGE_READWRITE; |
| 1012 | dwDesiredAccess = FILE_MAP_WRITE; |
| 1013 | break; |
| 1014 | case ACCESS_COPY: |
| 1015 | flProtect = PAGE_WRITECOPY; |
| 1016 | dwDesiredAccess = FILE_MAP_COPY; |
| 1017 | break; |
| 1018 | default: |
Tim Peters | ec0a5f0 | 2006-02-16 23:47:20 +0000 | [diff] [blame] | 1019 | return PyErr_Format(PyExc_ValueError, |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 1020 | "mmap invalid access parameter."); |
| 1021 | } |
| 1022 | |
Andrew M. Kuchling | 70d2742 | 2000-06-18 04:45:14 +0000 | [diff] [blame] | 1023 | map_size = _GetMapSize(map_size_obj); |
| 1024 | if (map_size < 0) |
| 1025 | return NULL; |
Tim Peters | ec0a5f0 | 2006-02-16 23:47:20 +0000 | [diff] [blame] | 1026 | |
Neal Norwitz | 0e6bc8c | 2006-02-05 05:45:43 +0000 | [diff] [blame] | 1027 | /* assume -1 and 0 both mean invalid filedescriptor |
| 1028 | to 'anonymously' map memory. |
| 1029 | XXX: fileno == 0 is a valid fd, but was accepted prior to 2.5. |
| 1030 | XXX: Should this code be added? |
| 1031 | if (fileno == 0) |
| 1032 | PyErr_Warn(PyExc_DeprecationWarning, |
| 1033 | "don't use 0 for anonymous memory"); |
| 1034 | */ |
| 1035 | if (fileno != -1 && fileno != 0) { |
Mark Hammond | 071864a | 2000-07-30 02:46:26 +0000 | [diff] [blame] | 1036 | fh = (HANDLE)_get_osfhandle(fileno); |
| 1037 | if (fh==(HANDLE)-1) { |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 1038 | PyErr_SetFromErrno(mmap_module_error); |
| 1039 | return NULL; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 1040 | } |
Fred Drake | 1ef4e2d | 2000-04-05 14:15:31 +0000 | [diff] [blame] | 1041 | /* Win9x appears to need us seeked to zero */ |
Guido van Rossum | 69c2b88 | 2003-04-09 19:31:02 +0000 | [diff] [blame] | 1042 | lseek(fileno, 0, SEEK_SET); |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 1043 | } |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 1044 | |
Tim Peters | 8f9cc29 | 2006-02-17 00:00:20 +0000 | [diff] [blame] | 1045 | m_obj = PyObject_New(mmap_object, &mmap_object_type); |
| 1046 | if (m_obj == NULL) |
Mark Hammond | 2cbed00 | 2000-07-30 02:22:43 +0000 | [diff] [blame] | 1047 | return NULL; |
| 1048 | /* Set every field to an invalid marker, so we can safely |
| 1049 | destruct the object in the face of failure */ |
| 1050 | m_obj->data = NULL; |
Mark Hammond | 071864a | 2000-07-30 02:46:26 +0000 | [diff] [blame] | 1051 | m_obj->file_handle = INVALID_HANDLE_VALUE; |
Mark Hammond | 2cbed00 | 2000-07-30 02:22:43 +0000 | [diff] [blame] | 1052 | m_obj->map_handle = INVALID_HANDLE_VALUE; |
| 1053 | m_obj->tagname = NULL; |
| 1054 | |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 1055 | if (fh) { |
Mark Hammond | 2cbed00 | 2000-07-30 02:22:43 +0000 | [diff] [blame] | 1056 | /* It is necessary to duplicate the handle, so the |
| 1057 | Python code can close it on us */ |
| 1058 | if (!DuplicateHandle( |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 1059 | GetCurrentProcess(), /* source process handle */ |
| 1060 | fh, /* handle to be duplicated */ |
| 1061 | GetCurrentProcess(), /* target proc handle */ |
| 1062 | (LPHANDLE)&m_obj->file_handle, /* result */ |
| 1063 | 0, /* access - ignored due to options value */ |
| 1064 | FALSE, /* inherited by child processes? */ |
| 1065 | DUPLICATE_SAME_ACCESS)) { /* options */ |
Mark Hammond | 2cbed00 | 2000-07-30 02:22:43 +0000 | [diff] [blame] | 1066 | dwErr = GetLastError(); |
| 1067 | Py_DECREF(m_obj); |
| 1068 | PyErr_SetFromWindowsErr(dwErr); |
| 1069 | return NULL; |
| 1070 | } |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 1071 | if (!map_size) { |
Martin v. Löwis | 1518607 | 2006-02-18 12:38:35 +0000 | [diff] [blame] | 1072 | DWORD low,high; |
| 1073 | low = GetFileSize(fh, &high); |
| 1074 | /* low might just happen to have the value INVALID_FILE_SIZE; |
| 1075 | so we need to check the last error also. */ |
| 1076 | if (low == INVALID_FILE_SIZE && |
| 1077 | (dwErr = GetLastError()) != NO_ERROR) { |
| 1078 | Py_DECREF(m_obj); |
| 1079 | return PyErr_SetFromWindowsErr(dwErr); |
| 1080 | } |
| 1081 | |
| 1082 | #if SIZEOF_SIZE_T > 4 |
| 1083 | m_obj->size = (((size_t)high)<<32) + low; |
| 1084 | #else |
| 1085 | if (high) |
| 1086 | /* File is too large to map completely */ |
| 1087 | m_obj->size = (size_t)-1; |
| 1088 | else |
| 1089 | m_obj->size = low; |
| 1090 | #endif |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 1091 | } else { |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 1092 | m_obj->size = map_size; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 1093 | } |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 1094 | } |
| 1095 | else { |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 1096 | m_obj->size = map_size; |
| 1097 | } |
| 1098 | |
| 1099 | /* set the initial position */ |
| 1100 | m_obj->pos = (size_t) 0; |
| 1101 | |
Mark Hammond | 2cbed00 | 2000-07-30 02:22:43 +0000 | [diff] [blame] | 1102 | /* set the tag name */ |
Tim Peters | 0d9f9dc | 2001-01-10 05:42:18 +0000 | [diff] [blame] | 1103 | if (tagname != NULL && *tagname != '\0') { |
Mark Hammond | 2cbed00 | 2000-07-30 02:22:43 +0000 | [diff] [blame] | 1104 | m_obj->tagname = PyMem_Malloc(strlen(tagname)+1); |
| 1105 | if (m_obj->tagname == NULL) { |
| 1106 | PyErr_NoMemory(); |
| 1107 | Py_DECREF(m_obj); |
| 1108 | return NULL; |
| 1109 | } |
| 1110 | strcpy(m_obj->tagname, tagname); |
| 1111 | } |
| 1112 | else |
| 1113 | m_obj->tagname = NULL; |
| 1114 | |
Neal Norwitz | 8856fb7 | 2005-12-18 03:34:22 +0000 | [diff] [blame] | 1115 | m_obj->access = (access_mode)access; |
Tim Peters | e564e7f | 2006-02-16 23:46:01 +0000 | [diff] [blame] | 1116 | /* 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] | 1117 | * 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] | 1118 | * consumes 4 bytes), C doesn't define what happens if we shift |
| 1119 | * right by 32, so we need different code. |
| 1120 | */ |
| 1121 | #if SIZEOF_SIZE_T > 4 |
| 1122 | size_hi = (DWORD)(m_obj->size >> 32); |
| 1123 | size_lo = (DWORD)(m_obj->size & 0xFFFFFFFF); |
| 1124 | #else |
| 1125 | size_hi = 0; |
| 1126 | size_lo = (DWORD)m_obj->size; |
| 1127 | #endif |
Tim Peters | 8f9cc29 | 2006-02-17 00:00:20 +0000 | [diff] [blame] | 1128 | m_obj->map_handle = CreateFileMapping(m_obj->file_handle, |
| 1129 | NULL, |
| 1130 | flProtect, |
| 1131 | size_hi, |
| 1132 | size_lo, |
| 1133 | m_obj->tagname); |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 1134 | if (m_obj->map_handle != NULL) { |
Tim Peters | 8f9cc29 | 2006-02-17 00:00:20 +0000 | [diff] [blame] | 1135 | m_obj->data = (char *) MapViewOfFile(m_obj->map_handle, |
| 1136 | dwDesiredAccess, |
| 1137 | 0, |
| 1138 | 0, |
| 1139 | 0); |
Tim Peters | 23721ee | 2006-02-16 23:50:16 +0000 | [diff] [blame] | 1140 | if (m_obj->data != NULL) |
| 1141 | return (PyObject *)m_obj; |
| 1142 | else |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 1143 | dwErr = GetLastError(); |
Tim Peters | 23721ee | 2006-02-16 23:50:16 +0000 | [diff] [blame] | 1144 | } else |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 1145 | dwErr = GetLastError(); |
Mark Hammond | 2cbed00 | 2000-07-30 02:22:43 +0000 | [diff] [blame] | 1146 | Py_DECREF(m_obj); |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 1147 | PyErr_SetFromWindowsErr(dwErr); |
Tim Peters | 23721ee | 2006-02-16 23:50:16 +0000 | [diff] [blame] | 1148 | return NULL; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 1149 | } |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 1150 | #endif /* MS_WINDOWS */ |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 1151 | |
| 1152 | /* List of functions exported by this module */ |
| 1153 | static struct PyMethodDef mmap_functions[] = { |
Tim Peters | ec0a5f0 | 2006-02-16 23:47:20 +0000 | [diff] [blame] | 1154 | {"mmap", (PyCFunction) new_mmap_object, |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 1155 | METH_VARARGS|METH_KEYWORDS}, |
| 1156 | {NULL, NULL} /* Sentinel */ |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 1157 | }; |
| 1158 | |
Mark Hammond | 62b1ab1 | 2002-07-23 06:31:15 +0000 | [diff] [blame] | 1159 | PyMODINIT_FUNC |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 1160 | initmmap(void) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 1161 | { |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 1162 | PyObject *dict, *module; |
Tim Peters | 2caf8df | 2001-01-14 05:05:51 +0000 | [diff] [blame] | 1163 | |
| 1164 | /* Patch the object type */ |
| 1165 | mmap_object_type.ob_type = &PyType_Type; |
| 1166 | |
Tim Peters | 8f9cc29 | 2006-02-17 00:00:20 +0000 | [diff] [blame] | 1167 | module = Py_InitModule("mmap", mmap_functions); |
Neal Norwitz | 1ac754f | 2006-01-19 06:09:39 +0000 | [diff] [blame] | 1168 | if (module == NULL) |
| 1169 | return; |
Tim Peters | 8f9cc29 | 2006-02-17 00:00:20 +0000 | [diff] [blame] | 1170 | dict = PyModule_GetDict(module); |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 1171 | mmap_module_error = PyExc_EnvironmentError; |
| 1172 | Py_INCREF(mmap_module_error); |
Tim Peters | 8f9cc29 | 2006-02-17 00:00:20 +0000 | [diff] [blame] | 1173 | PyDict_SetItemString(dict, "error", mmap_module_error); |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 1174 | #ifdef PROT_EXEC |
Tim Peters | 8f9cc29 | 2006-02-17 00:00:20 +0000 | [diff] [blame] | 1175 | PyDict_SetItemString(dict, "PROT_EXEC", PyInt_FromLong(PROT_EXEC) ); |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 1176 | #endif |
| 1177 | #ifdef PROT_READ |
Tim Peters | 8f9cc29 | 2006-02-17 00:00:20 +0000 | [diff] [blame] | 1178 | PyDict_SetItemString(dict, "PROT_READ", PyInt_FromLong(PROT_READ) ); |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 1179 | #endif |
| 1180 | #ifdef PROT_WRITE |
Tim Peters | 8f9cc29 | 2006-02-17 00:00:20 +0000 | [diff] [blame] | 1181 | PyDict_SetItemString(dict, "PROT_WRITE", PyInt_FromLong(PROT_WRITE) ); |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 1182 | #endif |
| 1183 | |
| 1184 | #ifdef MAP_SHARED |
Tim Peters | 8f9cc29 | 2006-02-17 00:00:20 +0000 | [diff] [blame] | 1185 | PyDict_SetItemString(dict, "MAP_SHARED", PyInt_FromLong(MAP_SHARED) ); |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 1186 | #endif |
| 1187 | #ifdef MAP_PRIVATE |
Tim Peters | 8f9cc29 | 2006-02-17 00:00:20 +0000 | [diff] [blame] | 1188 | PyDict_SetItemString(dict, "MAP_PRIVATE", |
| 1189 | PyInt_FromLong(MAP_PRIVATE) ); |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 1190 | #endif |
| 1191 | #ifdef MAP_DENYWRITE |
Tim Peters | 8f9cc29 | 2006-02-17 00:00:20 +0000 | [diff] [blame] | 1192 | PyDict_SetItemString(dict, "MAP_DENYWRITE", |
| 1193 | PyInt_FromLong(MAP_DENYWRITE) ); |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 1194 | #endif |
| 1195 | #ifdef MAP_EXECUTABLE |
Tim Peters | 8f9cc29 | 2006-02-17 00:00:20 +0000 | [diff] [blame] | 1196 | PyDict_SetItemString(dict, "MAP_EXECUTABLE", |
| 1197 | PyInt_FromLong(MAP_EXECUTABLE) ); |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 1198 | #endif |
Neal Norwitz | 0e6bc8c | 2006-02-05 05:45:43 +0000 | [diff] [blame] | 1199 | #ifdef MAP_ANONYMOUS |
Tim Peters | 8f9cc29 | 2006-02-17 00:00:20 +0000 | [diff] [blame] | 1200 | PyDict_SetItemString(dict, "MAP_ANON", PyInt_FromLong(MAP_ANONYMOUS) ); |
| 1201 | PyDict_SetItemString(dict, "MAP_ANONYMOUS", |
| 1202 | PyInt_FromLong(MAP_ANONYMOUS) ); |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 1203 | #endif |
| 1204 | |
Tim Peters | 8f9cc29 | 2006-02-17 00:00:20 +0000 | [diff] [blame] | 1205 | PyDict_SetItemString(dict, "PAGESIZE", |
| 1206 | PyInt_FromLong((long)my_getpagesize())); |
Andrew M. Kuchling | 961fe17 | 2000-06-03 19:41:42 +0000 | [diff] [blame] | 1207 | |
Tim Peters | 8f9cc29 | 2006-02-17 00:00:20 +0000 | [diff] [blame] | 1208 | PyDict_SetItemString(dict, "ACCESS_READ", |
| 1209 | PyInt_FromLong(ACCESS_READ)); |
| 1210 | PyDict_SetItemString(dict, "ACCESS_WRITE", |
| 1211 | PyInt_FromLong(ACCESS_WRITE)); |
| 1212 | PyDict_SetItemString(dict, "ACCESS_COPY", |
| 1213 | PyInt_FromLong(ACCESS_COPY)); |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 1214 | } |