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