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