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