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