blob: 9714ddff16728ee3680a9e1cff7fca35927e8ccb [file] [log] [blame]
Guido van Rossum09fdf072000-03-31 01:17:07 +00001/*
2 / Author: Sam Rushing <rushing@nightmare.com>
Andrew M. Kuchling10f9c072001-11-05 21:25:42 +00003 / Hacked for Unix by AMK
Guido van Rossum09fdf072000-03-31 01:17:07 +00004 / $Id$
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +00005
Guido van Rossum8ce8a782007-11-01 19:42:39 +00006 / Modified to support mmap with offset - to map a 'window' of a file
7 / Author: Yotam Medini yotamm@mellanox.co.il
8 /
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +00009 / mmapmodule.cpp -- map a view of a file into memory
10 /
11 / todo: need permission flags, perhaps a 'chsize' analog
12 / not all functions check range yet!!!
13 /
14 /
Mark Hammond071864a2000-07-30 02:46:26 +000015 / 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. Kuchling1ed7d2d2000-03-30 21:14:30 +000018 / ftp://squirl.nightmare.com/pub/python/python-ext.
19*/
20
Martin v. Löwiscfe7e092006-02-17 06:59:14 +000021#define PY_SSIZE_T_CLEAN
Guido van Rossum09fdf072000-03-31 01:17:07 +000022#include <Python.h>
23
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000024#ifndef MS_WINDOWS
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +000025#define UNIX
26#endif
27
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000028#ifdef MS_WINDOWS
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +000029#include <windows.h>
Fred Drake145f96e2000-10-01 17:50:46 +000030static int
31my_getpagesize(void)
32{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000033 SYSTEM_INFO si;
34 GetSystemInfo(&si);
35 return si.dwPageSize;
Fred Drake145f96e2000-10-01 17:50:46 +000036}
Guido van Rossum8ce8a782007-11-01 19:42:39 +000037
38static int
39my_getallocationgranularity (void)
40{
41
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000042 SYSTEM_INFO si;
43 GetSystemInfo(&si);
44 return si.dwAllocationGranularity;
Guido van Rossum8ce8a782007-11-01 19:42:39 +000045}
46
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +000047#endif
48
49#ifdef UNIX
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +000050#include <sys/mman.h>
Andrew M. Kuchling7b9fb922000-06-17 22:41:22 +000051#include <sys/stat.h>
Guido van Rossum4b36e6b2000-09-25 13:16:15 +000052
Fred Drake145f96e2000-10-01 17:50:46 +000053#if defined(HAVE_SYSCONF) && defined(_SC_PAGESIZE)
54static int
55my_getpagesize(void)
56{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000057 return sysconf(_SC_PAGESIZE);
Fred Drake145f96e2000-10-01 17:50:46 +000058}
Guido van Rossum8ce8a782007-11-01 19:42:39 +000059
60#define my_getallocationgranularity my_getpagesize
Fred Drake145f96e2000-10-01 17:50:46 +000061#else
62#define my_getpagesize getpagesize
63#endif
64
Guido van Rossum4b36e6b2000-09-25 13:16:15 +000065#endif /* UNIX */
66
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +000067#include <string.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000068
69#ifdef HAVE_SYS_TYPES_H
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +000070#include <sys/types.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000071#endif /* HAVE_SYS_TYPES_H */
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +000072
Neal Norwitz3eaf2b52006-02-16 08:08:54 +000073/* Prefer MAP_ANONYMOUS since MAP_ANON is deprecated according to man page. */
Neal Norwitz0e6bc8c2006-02-05 05:45:43 +000074#if !defined(MAP_ANONYMOUS) && defined(MAP_ANON)
75# define MAP_ANONYMOUS MAP_ANON
76#endif
77
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +000078static PyObject *mmap_module_error;
79
Tim Peters5ebfd362001-11-13 23:11:19 +000080typedef enum
81{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000082 ACCESS_DEFAULT,
83 ACCESS_READ,
84 ACCESS_WRITE,
85 ACCESS_COPY
Tim Peters5ebfd362001-11-13 23:11:19 +000086} access_mode;
87
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +000088typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000089 PyObject_HEAD
90 char * data;
91 size_t size;
92 size_t pos; /* relative to offset */
93 size_t offset;
94 int exports;
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +000095
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000096#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000097 HANDLE map_handle;
98 HANDLE file_handle;
99 char * tagname;
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000100#endif
101
102#ifdef UNIX
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000103 int fd;
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000104#endif
Tim Peters5ebfd362001-11-13 23:11:19 +0000105
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000106 access_mode access;
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000107} mmap_object;
108
Tim Peters5ebfd362001-11-13 23:11:19 +0000109
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000110static void
Fredrik Lundh54cf3dc2000-07-08 22:05:01 +0000111mmap_object_dealloc(mmap_object *m_obj)
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000112{
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000113#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000114 if (m_obj->data != NULL)
115 UnmapViewOfFile (m_obj->data);
116 if (m_obj->map_handle != NULL)
117 CloseHandle (m_obj->map_handle);
118 if (m_obj->file_handle != INVALID_HANDLE_VALUE)
119 CloseHandle (m_obj->file_handle);
120 if (m_obj->tagname)
121 PyMem_Free(m_obj->tagname);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000122#endif /* MS_WINDOWS */
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000123
124#ifdef UNIX
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000125 if (m_obj->fd >= 0)
126 (void) close(m_obj->fd);
127 if (m_obj->data!=NULL) {
128 msync(m_obj->data, m_obj->size, MS_SYNC);
129 munmap(m_obj->data, m_obj->size);
130 }
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000131#endif /* UNIX */
132
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000133 Py_TYPE(m_obj)->tp_free((PyObject*)m_obj);
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000134}
135
136static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000137mmap_close_method(mmap_object *self, PyObject *unused)
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000138{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000139 if (self->exports > 0) {
140 PyErr_SetString(PyExc_BufferError, "cannot close "\
141 "exported pointers exist");
142 return NULL;
143 }
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000144#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000145 /* For each resource we maintain, we need to check
146 the value is valid, and if so, free the resource
147 and set the member value to an invalid value so
148 the dealloc does not attempt to resource clearing
149 again.
150 TODO - should we check for errors in the close operations???
151 */
152 if (self->data != NULL) {
153 UnmapViewOfFile(self->data);
154 self->data = NULL;
155 }
156 if (self->map_handle != NULL) {
157 CloseHandle(self->map_handle);
158 self->map_handle = NULL;
159 }
160 if (self->file_handle != INVALID_HANDLE_VALUE) {
161 CloseHandle(self->file_handle);
162 self->file_handle = INVALID_HANDLE_VALUE;
163 }
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000164#endif /* MS_WINDOWS */
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000165
166#ifdef UNIX
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000167 if (0 <= self->fd)
168 (void) close(self->fd);
169 self->fd = -1;
170 if (self->data != NULL) {
171 munmap(self->data, self->size);
172 self->data = NULL;
173 }
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000174#endif
175
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000176 Py_INCREF(Py_None);
177 return Py_None;
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000178}
179
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000180#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000181#define CHECK_VALID(err) \
182do { \
183 if (self->map_handle == NULL) { \
184 PyErr_SetString(PyExc_ValueError, "mmap closed or invalid"); \
185 return err; \
186 } \
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000187} while (0)
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000188#endif /* MS_WINDOWS */
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000189
190#ifdef UNIX
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000191#define CHECK_VALID(err) \
192do { \
193 if (self->data == NULL) { \
194 PyErr_SetString(PyExc_ValueError, "mmap closed or invalid"); \
195 return err; \
196 } \
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000197} while (0)
198#endif /* UNIX */
199
200static PyObject *
Fredrik Lundh54cf3dc2000-07-08 22:05:01 +0000201mmap_read_byte_method(mmap_object *self,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000202 PyObject *unused)
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000203{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000204 CHECK_VALID(NULL);
205 if (self->pos < self->size) {
206 char value = self->data[self->pos];
207 self->pos += 1;
208 return Py_BuildValue("b", value);
209 } else {
210 PyErr_SetString(PyExc_ValueError, "read byte out of range");
211 return NULL;
212 }
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000213}
214
215static PyObject *
Fredrik Lundh54cf3dc2000-07-08 22:05:01 +0000216mmap_read_line_method(mmap_object *self,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000217 PyObject *unused)
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000218{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000219 char *start = self->data+self->pos;
220 char *eof = self->data+self->size;
221 char *eol;
222 PyObject *result;
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000223
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000224 CHECK_VALID(NULL);
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000225
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000226 eol = memchr(start, '\n', self->size - self->pos);
227 if (!eol)
228 eol = eof;
229 else
230 ++eol; /* we're interested in the position after the
231 newline. */
232 result = PyBytes_FromStringAndSize(start, (eol - start));
233 self->pos += (eol - start);
234 return result;
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000235}
236
237static PyObject *
Fredrik Lundh54cf3dc2000-07-08 22:05:01 +0000238mmap_read_method(mmap_object *self,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000239 PyObject *args)
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000240{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000241 Py_ssize_t num_bytes, n;
242 PyObject *result;
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000243
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000244 CHECK_VALID(NULL);
245 if (!PyArg_ParseTuple(args, "n:read", &num_bytes))
246 return(NULL);
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000247
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000248 /* silently 'adjust' out-of-range requests */
249 assert(self->size >= self->pos);
250 n = self->size - self->pos;
251 /* The difference can overflow, only if self->size is greater than
252 * PY_SSIZE_T_MAX. But then the operation cannot possibly succeed,
253 * because the mapped area and the returned string each need more
254 * than half of the addressable memory. So we clip the size, and let
255 * the code below raise MemoryError.
256 */
257 if (n < 0)
258 n = PY_SSIZE_T_MAX;
259 if (num_bytes < 0 || num_bytes > n) {
260 num_bytes = n;
261 }
262 result = PyBytes_FromStringAndSize(self->data+self->pos, num_bytes);
263 self->pos += num_bytes;
264 return result;
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000265}
266
267static PyObject *
Georg Brandlfceab5a2008-01-19 20:08:23 +0000268mmap_gfind(mmap_object *self,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000269 PyObject *args,
270 int reverse)
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000271{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000272 Py_ssize_t start = self->pos;
273 Py_ssize_t end = self->size;
274 const char *needle;
275 Py_ssize_t len;
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000276
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000277 CHECK_VALID(NULL);
278 if (!PyArg_ParseTuple(args, reverse ? "y#|nn:rfind" : "y#|nn:find",
279 &needle, &len, &start, &end)) {
280 return NULL;
281 } else {
282 const char *p, *start_p, *end_p;
283 int sign = reverse ? -1 : 1;
Greg Stein834f4dd2001-05-14 09:32:26 +0000284
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000285 if (start < 0)
286 start += self->size;
287 if (start < 0)
288 start = 0;
289 else if ((size_t)start > self->size)
290 start = self->size;
Greg Stein834f4dd2001-05-14 09:32:26 +0000291
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000292 if (end < 0)
293 end += self->size;
294 if (end < 0)
295 end = 0;
296 else if ((size_t)end > self->size)
297 end = self->size;
Georg Brandlfceab5a2008-01-19 20:08:23 +0000298
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000299 start_p = self->data + start;
300 end_p = self->data + end;
Georg Brandlfceab5a2008-01-19 20:08:23 +0000301
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000302 for (p = (reverse ? end_p - len : start_p);
303 (p >= start_p) && (p + len <= end_p); p += sign) {
304 Py_ssize_t i;
305 for (i = 0; i < len && needle[i] == p[i]; ++i)
306 /* nothing */;
307 if (i == len) {
308 return PyLong_FromSsize_t(p - self->data);
309 }
310 }
311 return PyLong_FromLong(-1);
312 }
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000313}
314
Georg Brandlfceab5a2008-01-19 20:08:23 +0000315static PyObject *
316mmap_find_method(mmap_object *self,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000317 PyObject *args)
Georg Brandlfceab5a2008-01-19 20:08:23 +0000318{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000319 return mmap_gfind(self, args, 0);
Georg Brandlfceab5a2008-01-19 20:08:23 +0000320}
321
322static PyObject *
323mmap_rfind_method(mmap_object *self,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000324 PyObject *args)
Georg Brandlfceab5a2008-01-19 20:08:23 +0000325{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000326 return mmap_gfind(self, args, 1);
Georg Brandlfceab5a2008-01-19 20:08:23 +0000327}
328
Tim Petersec0a5f02006-02-16 23:47:20 +0000329static int
Sean Reifscheider54cf12b2007-09-17 17:55:36 +0000330is_writable(mmap_object *self)
Tim Peters5ebfd362001-11-13 23:11:19 +0000331{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000332 if (self->access != ACCESS_READ)
333 return 1;
334 PyErr_Format(PyExc_TypeError, "mmap can't modify a readonly memory map.");
335 return 0;
Tim Peters5ebfd362001-11-13 23:11:19 +0000336}
337
Tim Petersec0a5f02006-02-16 23:47:20 +0000338static int
Tim Peters5ebfd362001-11-13 23:11:19 +0000339is_resizeable(mmap_object *self)
340{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000341 if (self->exports > 0) {
342 PyErr_SetString(PyExc_BufferError,
343 "mmap can't resize with extant buffers exported.");
344 return 0;
345 }
346 if ((self->access == ACCESS_WRITE) || (self->access == ACCESS_DEFAULT))
347 return 1;
348 PyErr_Format(PyExc_TypeError,
349 "mmap can't resize a readonly or copy-on-write memory map.");
350 return 0;
Tim Peters5ebfd362001-11-13 23:11:19 +0000351}
352
353
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000354static PyObject *
Fredrik Lundh54cf3dc2000-07-08 22:05:01 +0000355mmap_write_method(mmap_object *self,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000356 PyObject *args)
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000357{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000358 Py_ssize_t length;
359 char *data;
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000360
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000361 CHECK_VALID(NULL);
362 if (!PyArg_ParseTuple(args, "y#:write", &data, &length))
363 return(NULL);
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000364
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000365 if (!is_writable(self))
366 return NULL;
Tim Peters5ebfd362001-11-13 23:11:19 +0000367
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000368 if ((self->pos + length) > self->size) {
369 PyErr_SetString(PyExc_ValueError, "data out of range");
370 return NULL;
371 }
372 memcpy(self->data+self->pos, data, length);
373 self->pos = self->pos+length;
374 Py_INCREF(Py_None);
375 return Py_None;
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000376}
377
378static PyObject *
Fredrik Lundh54cf3dc2000-07-08 22:05:01 +0000379mmap_write_byte_method(mmap_object *self,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000380 PyObject *args)
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000381{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000382 char value;
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000383
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000384 CHECK_VALID(NULL);
385 if (!PyArg_ParseTuple(args, "b:write_byte", &value))
386 return(NULL);
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000387
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000388 if (!is_writable(self))
389 return NULL;
Hirokazu Yamamoto39c6dea2009-02-28 10:56:50 +0000390
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000391 if (self->pos < self->size) {
392 *(self->data+self->pos) = value;
393 self->pos += 1;
394 Py_INCREF(Py_None);
395 return Py_None;
396 }
397 else {
398 PyErr_SetString(PyExc_ValueError, "write byte out of range");
399 return NULL;
400 }
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000401}
Tim Petersec0a5f02006-02-16 23:47:20 +0000402
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000403static PyObject *
Fredrik Lundh54cf3dc2000-07-08 22:05:01 +0000404mmap_size_method(mmap_object *self,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000405 PyObject *unused)
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000406{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000407 CHECK_VALID(NULL);
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000408
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000409#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000410 if (self->file_handle != INVALID_HANDLE_VALUE) {
411 DWORD low,high;
412 PY_LONG_LONG size;
413 low = GetFileSize(self->file_handle, &high);
414 if (low == INVALID_FILE_SIZE) {
415 /* It might be that the function appears to have failed,
416 when indeed its size equals INVALID_FILE_SIZE */
417 DWORD error = GetLastError();
418 if (error != NO_ERROR)
419 return PyErr_SetFromWindowsErr(error);
420 }
421 if (!high && low < LONG_MAX)
422 return PyLong_FromLong((long)low);
423 size = (((PY_LONG_LONG)high)<<32) + low;
424 return PyLong_FromLongLong(size);
425 } else {
426 return PyLong_FromSsize_t(self->size);
427 }
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000428#endif /* MS_WINDOWS */
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000429
430#ifdef UNIX
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000431 {
432 struct stat buf;
433 if (-1 == fstat(self->fd, &buf)) {
434 PyErr_SetFromErrno(mmap_module_error);
435 return NULL;
436 }
437 return PyLong_FromSsize_t(buf.st_size);
438 }
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000439#endif /* UNIX */
440}
441
442/* This assumes that you want the entire file mapped,
443 / and when recreating the map will make the new file
444 / have the new size
445 /
446 / Is this really necessary? This could easily be done
447 / from python by just closing and re-opening with the
448 / new size?
449 */
450
451static PyObject *
Fredrik Lundh54cf3dc2000-07-08 22:05:01 +0000452mmap_resize_method(mmap_object *self,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000453 PyObject *args)
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000454{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000455 Py_ssize_t new_size;
456 CHECK_VALID(NULL);
457 if (!PyArg_ParseTuple(args, "n:resize", &new_size) ||
458 !is_resizeable(self)) {
459 return NULL;
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000460#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000461 } else {
462 DWORD dwErrCode = 0;
463 DWORD off_hi, off_lo, newSizeLow, newSizeHigh;
464 /* First, unmap the file view */
465 UnmapViewOfFile(self->data);
466 self->data = NULL;
467 /* Close the mapping object */
468 CloseHandle(self->map_handle);
469 self->map_handle = NULL;
470 /* Move to the desired EOF position */
Martin v. Löwis15186072006-02-18 12:38:35 +0000471#if SIZEOF_SIZE_T > 4
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000472 newSizeHigh = (DWORD)((self->offset + new_size) >> 32);
473 newSizeLow = (DWORD)((self->offset + new_size) & 0xFFFFFFFF);
474 off_hi = (DWORD)(self->offset >> 32);
475 off_lo = (DWORD)(self->offset & 0xFFFFFFFF);
Martin v. Löwis15186072006-02-18 12:38:35 +0000476#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000477 newSizeHigh = 0;
478 newSizeLow = (DWORD)(self->offset + new_size);
479 off_hi = 0;
480 off_lo = (DWORD)self->offset;
Martin v. Löwis15186072006-02-18 12:38:35 +0000481#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000482 SetFilePointer(self->file_handle,
483 newSizeLow, &newSizeHigh, FILE_BEGIN);
484 /* Change the size of the file */
485 SetEndOfFile(self->file_handle);
486 /* Create another mapping object and remap the file view */
487 self->map_handle = CreateFileMapping(
488 self->file_handle,
489 NULL,
490 PAGE_READWRITE,
491 0,
492 0,
493 self->tagname);
494 if (self->map_handle != NULL) {
495 self->data = (char *) MapViewOfFile(self->map_handle,
496 FILE_MAP_WRITE,
497 off_hi,
498 off_lo,
499 new_size);
500 if (self->data != NULL) {
501 self->size = new_size;
502 Py_INCREF(Py_None);
503 return Py_None;
504 } else {
505 dwErrCode = GetLastError();
506 CloseHandle(self->map_handle);
507 self->map_handle = NULL;
508 }
509 } else {
510 dwErrCode = GetLastError();
511 }
512 PyErr_SetFromWindowsErr(dwErrCode);
513 return NULL;
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000514#endif /* MS_WINDOWS */
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000515
516#ifdef UNIX
Tim Petersec0a5f02006-02-16 23:47:20 +0000517#ifndef HAVE_MREMAP
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000518 } else {
519 PyErr_SetString(PyExc_SystemError,
520 "mmap: resizing not available--no mremap()");
521 return NULL;
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000522#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000523 } else {
524 void *newmap;
Armin Rigo335ffe82005-09-20 19:04:02 +0000525
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000526 if (ftruncate(self->fd, self->offset + new_size) == -1) {
527 PyErr_SetFromErrno(mmap_module_error);
528 return NULL;
529 }
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000530
Andrew M. Kuchling6fef30e2000-06-18 14:51:21 +0000531#ifdef MREMAP_MAYMOVE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000532 newmap = mremap(self->data, self->size, new_size, MREMAP_MAYMOVE);
Andrew M. Kuchling6fef30e2000-06-18 14:51:21 +0000533#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000534 #if defined(__NetBSD__)
535 newmap = mremap(self->data, self->size, self->data, new_size, 0);
536 #else
537 newmap = mremap(self->data, self->size, new_size, 0);
538 #endif /* __NetBSD__ */
Andrew M. Kuchling6fef30e2000-06-18 14:51:21 +0000539#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000540 if (newmap == (void *)-1)
541 {
542 PyErr_SetFromErrno(mmap_module_error);
543 return NULL;
544 }
545 self->data = newmap;
546 self->size = new_size;
547 Py_INCREF(Py_None);
548 return Py_None;
Andrew M. Kuchling6fef30e2000-06-18 14:51:21 +0000549#endif /* HAVE_MREMAP */
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000550#endif /* UNIX */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000551 }
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000552}
553
554static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000555mmap_tell_method(mmap_object *self, PyObject *unused)
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000556{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000557 CHECK_VALID(NULL);
558 return PyLong_FromSize_t(self->pos);
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000559}
560
561static PyObject *
Fredrik Lundh54cf3dc2000-07-08 22:05:01 +0000562mmap_flush_method(mmap_object *self, PyObject *args)
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000563{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000564 Py_ssize_t offset = 0;
565 Py_ssize_t size = self->size;
566 CHECK_VALID(NULL);
567 if (!PyArg_ParseTuple(args, "|nn:flush", &offset, &size))
568 return NULL;
569 if ((size_t)(offset + size) > self->size) {
570 PyErr_SetString(PyExc_ValueError, "flush values out of range");
571 return NULL;
572 }
Christian Heimesaf98da12008-01-27 15:18:18 +0000573#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000574 return PyLong_FromLong((long) FlushViewOfFile(self->data+offset, size));
Christian Heimesaf98da12008-01-27 15:18:18 +0000575#elif defined(UNIX)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000576 /* XXX semantics of return value? */
577 /* XXX flags for msync? */
578 if (-1 == msync(self->data + offset, size, MS_SYNC)) {
579 PyErr_SetFromErrno(mmap_module_error);
580 return NULL;
581 }
582 return PyLong_FromLong(0);
Christian Heimesaf98da12008-01-27 15:18:18 +0000583#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000584 PyErr_SetString(PyExc_ValueError, "flush not supported on this system");
585 return NULL;
Christian Heimesaf98da12008-01-27 15:18:18 +0000586#endif
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000587}
588
589static PyObject *
Fredrik Lundh54cf3dc2000-07-08 22:05:01 +0000590mmap_seek_method(mmap_object *self, PyObject *args)
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000591{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000592 Py_ssize_t dist;
593 int how=0;
594 CHECK_VALID(NULL);
595 if (!PyArg_ParseTuple(args, "n|i:seek", &dist, &how))
596 return NULL;
597 else {
598 size_t where;
599 switch (how) {
600 case 0: /* relative to start */
601 if (dist < 0)
602 goto onoutofrange;
603 where = dist;
604 break;
605 case 1: /* relative to current position */
606 if ((Py_ssize_t)self->pos + dist < 0)
607 goto onoutofrange;
608 where = self->pos + dist;
609 break;
610 case 2: /* relative to end */
611 if ((Py_ssize_t)self->size + dist < 0)
612 goto onoutofrange;
613 where = self->size + dist;
614 break;
615 default:
616 PyErr_SetString(PyExc_ValueError, "unknown seek type");
617 return NULL;
618 }
619 if (where > self->size)
620 goto onoutofrange;
621 self->pos = where;
622 Py_INCREF(Py_None);
623 return Py_None;
624 }
Andrew M. Kuchling70d27422000-06-18 04:45:14 +0000625
Tim Peters5ebfd362001-11-13 23:11:19 +0000626 onoutofrange:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000627 PyErr_SetString(PyExc_ValueError, "seek out of range");
628 return NULL;
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000629}
630
631static PyObject *
Fredrik Lundh54cf3dc2000-07-08 22:05:01 +0000632mmap_move_method(mmap_object *self, PyObject *args)
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000633{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000634 unsigned long dest, src, cnt;
635 CHECK_VALID(NULL);
636 if (!PyArg_ParseTuple(args, "kkk:move", &dest, &src, &cnt) ||
637 !is_writable(self)) {
638 return NULL;
639 } else {
640 /* bounds check the values */
641 if (cnt < 0 || (cnt + dest) < cnt || (cnt + src) < cnt ||
642 src < 0 || src > self->size || (src + cnt) > self->size ||
643 dest < 0 || dest > self->size || (dest + cnt) > self->size) {
644 PyErr_SetString(PyExc_ValueError,
645 "source, destination, or count out of range");
646 return NULL;
647 }
648 memmove(self->data+dest, self->data+src, cnt);
649 Py_INCREF(Py_None);
650 return Py_None;
651 }
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000652}
653
654static struct PyMethodDef mmap_object_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000655 {"close", (PyCFunction) mmap_close_method, METH_NOARGS},
656 {"find", (PyCFunction) mmap_find_method, METH_VARARGS},
657 {"rfind", (PyCFunction) mmap_rfind_method, METH_VARARGS},
658 {"flush", (PyCFunction) mmap_flush_method, METH_VARARGS},
659 {"move", (PyCFunction) mmap_move_method, METH_VARARGS},
660 {"read", (PyCFunction) mmap_read_method, METH_VARARGS},
661 {"read_byte", (PyCFunction) mmap_read_byte_method, METH_NOARGS},
662 {"readline", (PyCFunction) mmap_read_line_method, METH_NOARGS},
663 {"resize", (PyCFunction) mmap_resize_method, METH_VARARGS},
664 {"seek", (PyCFunction) mmap_seek_method, METH_VARARGS},
665 {"size", (PyCFunction) mmap_size_method, METH_NOARGS},
666 {"tell", (PyCFunction) mmap_tell_method, METH_NOARGS},
667 {"write", (PyCFunction) mmap_write_method, METH_VARARGS},
668 {"write_byte", (PyCFunction) mmap_write_byte_method, METH_VARARGS},
669 {NULL, NULL} /* sentinel */
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000670};
671
672/* Functions for treating an mmap'ed file as a buffer */
673
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000674static int
Guido van Rossum98297ee2007-11-06 21:34:58 +0000675mmap_buffer_getbuf(mmap_object *self, Py_buffer *view, int flags)
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000676{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000677 CHECK_VALID(-1);
678 if (PyBuffer_FillInfo(view, (PyObject*)self, self->data, self->size,
679 (self->access == ACCESS_READ), flags) < 0)
680 return -1;
681 self->exports++;
682 return 0;
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000683}
684
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000685static void
Travis E. Oliphant8ae62b62007-09-23 02:00:13 +0000686mmap_buffer_releasebuf(mmap_object *self, Py_buffer *view)
Tim Petersec0a5f02006-02-16 23:47:20 +0000687{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000688 self->exports--;
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000689}
690
Martin v. Löwis18e16552006-02-15 17:27:45 +0000691static Py_ssize_t
Fredrik Lundh54cf3dc2000-07-08 22:05:01 +0000692mmap_length(mmap_object *self)
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000693{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000694 CHECK_VALID(-1);
695 return self->size;
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000696}
697
698static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000699mmap_item(mmap_object *self, Py_ssize_t i)
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000700{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000701 CHECK_VALID(NULL);
702 if (i < 0 || (size_t)i >= self->size) {
703 PyErr_SetString(PyExc_IndexError, "mmap index out of range");
704 return NULL;
705 }
706 return PyBytes_FromStringAndSize(self->data + i, 1);
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000707}
708
709static PyObject *
Thomas Woutersed03b412007-08-28 21:37:11 +0000710mmap_subscript(mmap_object *self, PyObject *item)
711{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000712 CHECK_VALID(NULL);
713 if (PyIndex_Check(item)) {
714 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
715 if (i == -1 && PyErr_Occurred())
716 return NULL;
717 if (i < 0)
718 i += self->size;
719 if (i < 0 || (size_t)i >= self->size) {
720 PyErr_SetString(PyExc_IndexError,
721 "mmap index out of range");
722 return NULL;
723 }
724 return PyLong_FromLong(Py_CHARMASK(self->data[i]));
725 }
726 else if (PySlice_Check(item)) {
727 Py_ssize_t start, stop, step, slicelen;
Thomas Woutersed03b412007-08-28 21:37:11 +0000728
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000729 if (PySlice_GetIndicesEx((PySliceObject *)item, self->size,
730 &start, &stop, &step, &slicelen) < 0) {
731 return NULL;
732 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000733
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000734 if (slicelen <= 0)
735 return PyBytes_FromStringAndSize("", 0);
736 else if (step == 1)
737 return PyBytes_FromStringAndSize(self->data + start,
738 slicelen);
739 else {
740 char *result_buf = (char *)PyMem_Malloc(slicelen);
741 Py_ssize_t cur, i;
742 PyObject *result;
Thomas Woutersed03b412007-08-28 21:37:11 +0000743
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000744 if (result_buf == NULL)
745 return PyErr_NoMemory();
746 for (cur = start, i = 0; i < slicelen;
747 cur += step, i++) {
748 result_buf[i] = self->data[cur];
749 }
750 result = PyBytes_FromStringAndSize(result_buf,
751 slicelen);
752 PyMem_Free(result_buf);
753 return result;
754 }
755 }
756 else {
757 PyErr_SetString(PyExc_TypeError,
758 "mmap indices must be integers");
759 return NULL;
760 }
Thomas Woutersed03b412007-08-28 21:37:11 +0000761}
762
763static PyObject *
Fredrik Lundh54cf3dc2000-07-08 22:05:01 +0000764mmap_concat(mmap_object *self, PyObject *bb)
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000765{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000766 CHECK_VALID(NULL);
767 PyErr_SetString(PyExc_SystemError,
768 "mmaps don't support concatenation");
769 return NULL;
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000770}
771
772static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000773mmap_repeat(mmap_object *self, Py_ssize_t n)
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000774{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000775 CHECK_VALID(NULL);
776 PyErr_SetString(PyExc_SystemError,
777 "mmaps don't support repeat operation");
778 return NULL;
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000779}
780
781static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000782mmap_ass_item(mmap_object *self, Py_ssize_t i, PyObject *v)
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000783{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000784 const char *buf;
Tim Petersec0a5f02006-02-16 23:47:20 +0000785
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000786 CHECK_VALID(-1);
787 if (i < 0 || (size_t)i >= self->size) {
788 PyErr_SetString(PyExc_IndexError, "mmap index out of range");
789 return -1;
790 }
791 if (v == NULL) {
792 PyErr_SetString(PyExc_TypeError,
793 "mmap object doesn't support item deletion");
794 return -1;
795 }
796 if (! (PyBytes_Check(v) && PyBytes_Size(v)==1) ) {
797 PyErr_SetString(PyExc_IndexError,
798 "mmap assignment must be length-1 bytes()");
799 return -1;
800 }
801 if (!is_writable(self))
802 return -1;
803 buf = PyBytes_AsString(v);
804 self->data[i] = buf[0];
805 return 0;
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000806}
807
Thomas Woutersed03b412007-08-28 21:37:11 +0000808static int
809mmap_ass_subscript(mmap_object *self, PyObject *item, PyObject *value)
810{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000811 CHECK_VALID(-1);
Thomas Woutersed03b412007-08-28 21:37:11 +0000812
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000813 if (!is_writable(self))
814 return -1;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000815
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000816 if (PyIndex_Check(item)) {
817 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
818 Py_ssize_t v;
Thomas Woutersed03b412007-08-28 21:37:11 +0000819
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000820 if (i == -1 && PyErr_Occurred())
821 return -1;
822 if (i < 0)
823 i += self->size;
824 if (i < 0 || (size_t)i >= self->size) {
825 PyErr_SetString(PyExc_IndexError,
826 "mmap index out of range");
827 return -1;
828 }
829 if (value == NULL) {
830 PyErr_SetString(PyExc_TypeError,
831 "mmap doesn't support item deletion");
832 return -1;
833 }
834 if (!PyIndex_Check(value)) {
835 PyErr_SetString(PyExc_TypeError,
836 "mmap item value must be an int");
837 return -1;
838 }
839 v = PyNumber_AsSsize_t(value, PyExc_TypeError);
840 if (v == -1 && PyErr_Occurred())
841 return -1;
842 if (v < 0 || v > 255) {
843 PyErr_SetString(PyExc_ValueError,
844 "mmap item value must be "
845 "in range(0, 256)");
846 return -1;
847 }
848 self->data[i] = v;
849 return 0;
850 }
851 else if (PySlice_Check(item)) {
852 Py_ssize_t start, stop, step, slicelen;
853 Py_buffer vbuf;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000854
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000855 if (PySlice_GetIndicesEx((PySliceObject *)item,
856 self->size, &start, &stop,
857 &step, &slicelen) < 0) {
858 return -1;
859 }
860 if (value == NULL) {
861 PyErr_SetString(PyExc_TypeError,
862 "mmap object doesn't support slice deletion");
863 return -1;
864 }
865 if (PyObject_GetBuffer(value, &vbuf, PyBUF_SIMPLE) < 0)
866 return -1;
867 if (vbuf.len != slicelen) {
868 PyErr_SetString(PyExc_IndexError,
869 "mmap slice assignment is wrong size");
870 PyBuffer_Release(&vbuf);
871 return -1;
872 }
Thomas Woutersed03b412007-08-28 21:37:11 +0000873
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000874 if (slicelen == 0) {
875 }
876 else if (step == 1) {
877 memcpy(self->data + start, vbuf.buf, slicelen);
878 }
879 else {
880 Py_ssize_t cur, i;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000881
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000882 for (cur = start, i = 0;
883 i < slicelen;
884 cur += step, i++)
885 {
886 self->data[cur] = ((char *)vbuf.buf)[i];
887 }
888 }
889 PyBuffer_Release(&vbuf);
890 return 0;
891 }
892 else {
893 PyErr_SetString(PyExc_TypeError,
894 "mmap indices must be integer");
895 return -1;
896 }
Thomas Woutersed03b412007-08-28 21:37:11 +0000897}
898
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000899static PySequenceMethods mmap_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000900 (lenfunc)mmap_length, /*sq_length*/
901 (binaryfunc)mmap_concat, /*sq_concat*/
902 (ssizeargfunc)mmap_repeat, /*sq_repeat*/
903 (ssizeargfunc)mmap_item, /*sq_item*/
904 0, /*sq_slice*/
905 (ssizeobjargproc)mmap_ass_item, /*sq_ass_item*/
906 0, /*sq_ass_slice*/
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000907};
908
Thomas Woutersed03b412007-08-28 21:37:11 +0000909static PyMappingMethods mmap_as_mapping = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000910 (lenfunc)mmap_length,
911 (binaryfunc)mmap_subscript,
912 (objobjargproc)mmap_ass_subscript,
Thomas Woutersed03b412007-08-28 21:37:11 +0000913};
914
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000915static PyBufferProcs mmap_as_buffer = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000916 (getbufferproc)mmap_buffer_getbuf,
917 (releasebufferproc)mmap_buffer_releasebuf,
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000918};
919
Georg Brandl86def6c2008-01-21 20:36:10 +0000920static PyObject *
921new_mmap_object(PyTypeObject *type, PyObject *args, PyObject *kwdict);
922
Christian Heimese1c98112008-01-21 11:20:28 +0000923PyDoc_STRVAR(mmap_doc,
924"Windows: mmap(fileno, length[, tagname[, access[, offset]]])\n\
925\n\
926Maps length bytes from the file specified by the file handle fileno,\n\
927and returns a mmap object. If length is larger than the current size\n\
928of the file, the file is extended to contain length bytes. If length\n\
929is 0, the maximum length of the map is the current size of the file,\n\
930except that if the file is empty Windows raises an exception (you cannot\n\
931create an empty mapping on Windows).\n\
932\n\
933Unix: mmap(fileno, length[, flags[, prot[, access[, offset]]]])\n\
934\n\
935Maps length bytes from the file specified by the file descriptor fileno,\n\
936and returns a mmap object. If length is 0, the maximum length of the map\n\
937will be the current size of the file when mmap is called.\n\
938flags specifies the nature of the mapping. MAP_PRIVATE creates a\n\
939private copy-on-write mapping, so changes to the contents of the mmap\n\
Benjamin Petersonc4fe6f32008-08-19 18:57:56 +0000940object will be private to this process, and MAP_SHARED creates a mapping\n\
Christian Heimese1c98112008-01-21 11:20:28 +0000941that's shared with all other processes mapping the same areas of the file.\n\
942The default value is MAP_SHARED.\n\
943\n\
944To map anonymous memory, pass -1 as the fileno (both versions).");
945
946
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000947static PyTypeObject mmap_object_type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000948 PyVarObject_HEAD_INIT(NULL, 0)
949 "mmap.mmap", /* tp_name */
950 sizeof(mmap_object), /* tp_size */
951 0, /* tp_itemsize */
952 /* methods */
953 (destructor) mmap_object_dealloc, /* tp_dealloc */
954 0, /* tp_print */
955 0, /* tp_getattr */
956 0, /* tp_setattr */
957 0, /* tp_reserved */
958 0, /* tp_repr */
959 0, /* tp_as_number */
960 &mmap_as_sequence, /*tp_as_sequence*/
961 &mmap_as_mapping, /*tp_as_mapping*/
962 0, /*tp_hash*/
963 0, /*tp_call*/
964 0, /*tp_str*/
965 PyObject_GenericGetAttr, /*tp_getattro*/
966 0, /*tp_setattro*/
967 &mmap_as_buffer, /*tp_as_buffer*/
968 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
969 mmap_doc, /*tp_doc*/
970 0, /* tp_traverse */
971 0, /* tp_clear */
972 0, /* tp_richcompare */
973 0, /* tp_weaklistoffset */
974 0, /* tp_iter */
975 0, /* tp_iternext */
976 mmap_object_methods, /* tp_methods */
977 0, /* tp_members */
978 0, /* tp_getset */
979 0, /* tp_base */
980 0, /* tp_dict */
981 0, /* tp_descr_get */
982 0, /* tp_descr_set */
983 0, /* tp_dictoffset */
984 0, /* tp_init */
985 PyType_GenericAlloc, /* tp_alloc */
986 new_mmap_object, /* tp_new */
987 PyObject_Del, /* tp_free */
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000988};
989
Andrew M. Kuchling70d27422000-06-18 04:45:14 +0000990
991/* extract the map size from the given PyObject
992
Thomas Wouters7e474022000-07-16 12:04:32 +0000993 Returns -1 on error, with an appropriate Python exception raised. On
Andrew M. Kuchling70d27422000-06-18 04:45:14 +0000994 success, the map size is returned. */
Martin v. Löwiscfe7e092006-02-17 06:59:14 +0000995static Py_ssize_t
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000996_GetMapSize(PyObject *o, const char* param)
Andrew M. Kuchling70d27422000-06-18 04:45:14 +0000997{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000998 if (o == NULL)
999 return 0;
1000 if (PyIndex_Check(o)) {
1001 Py_ssize_t i = PyNumber_AsSsize_t(o, PyExc_OverflowError);
1002 if (i==-1 && PyErr_Occurred())
1003 return -1;
1004 if (i < 0) {
1005 PyErr_Format(PyExc_OverflowError,
1006 "memory mapped %s must be positive",
1007 param);
1008 return -1;
1009 }
1010 return i;
1011 }
Andrew M. Kuchling70d27422000-06-18 04:45:14 +00001012
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001013 PyErr_SetString(PyExc_TypeError, "map size must be an integral value");
1014 return -1;
Andrew M. Kuchling70d27422000-06-18 04:45:14 +00001015}
1016
Tim Petersec0a5f02006-02-16 23:47:20 +00001017#ifdef UNIX
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +00001018static PyObject *
Georg Brandl86def6c2008-01-21 20:36:10 +00001019new_mmap_object(PyTypeObject *type, PyObject *args, PyObject *kwdict)
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +00001020{
Neal Norwitzb5673922002-09-05 21:48:07 +00001021#ifdef HAVE_FSTAT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001022 struct stat st;
Neal Norwitzb5673922002-09-05 21:48:07 +00001023#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001024 mmap_object *m_obj;
1025 PyObject *map_size_obj = NULL, *offset_obj = NULL;
1026 Py_ssize_t map_size, offset;
1027 int fd, flags = MAP_SHARED, prot = PROT_WRITE | PROT_READ;
1028 int devzero = -1;
1029 int access = (int)ACCESS_DEFAULT;
1030 static char *keywords[] = {"fileno", "length",
1031 "flags", "prot",
1032 "access", "offset", NULL};
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +00001033
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001034 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iO|iiiO", keywords,
1035 &fd, &map_size_obj, &flags, &prot,
1036 &access, &offset_obj))
1037 return NULL;
1038 map_size = _GetMapSize(map_size_obj, "size");
1039 if (map_size < 0)
1040 return NULL;
1041 offset = _GetMapSize(offset_obj, "offset");
1042 if (offset < 0)
1043 return NULL;
Tim Peters5ebfd362001-11-13 23:11:19 +00001044
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001045 if ((access != (int)ACCESS_DEFAULT) &&
1046 ((flags != MAP_SHARED) || (prot != (PROT_WRITE | PROT_READ))))
1047 return PyErr_Format(PyExc_ValueError,
1048 "mmap can't specify both access and flags, prot.");
1049 switch ((access_mode)access) {
1050 case ACCESS_READ:
1051 flags = MAP_SHARED;
1052 prot = PROT_READ;
1053 break;
1054 case ACCESS_WRITE:
1055 flags = MAP_SHARED;
1056 prot = PROT_READ | PROT_WRITE;
1057 break;
1058 case ACCESS_COPY:
1059 flags = MAP_PRIVATE;
1060 prot = PROT_READ | PROT_WRITE;
1061 break;
1062 case ACCESS_DEFAULT:
1063 /* use the specified or default values of flags and prot */
1064 break;
1065 default:
1066 return PyErr_Format(PyExc_ValueError,
1067 "mmap invalid access parameter.");
1068 }
Neal Norwitzb5673922002-09-05 21:48:07 +00001069
Christian Heimesa156e092008-02-16 07:38:31 +00001070 if (prot == PROT_READ) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001071 access = ACCESS_READ;
Christian Heimesa156e092008-02-16 07:38:31 +00001072 }
1073
Neal Norwitzb5673922002-09-05 21:48:07 +00001074#ifdef HAVE_FSTAT
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +00001075# ifdef __VMS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001076 /* on OpenVMS we must ensure that all bytes are written to the file */
1077 if (fd != -1) {
1078 fsync(fd);
1079 }
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +00001080# endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001081 if (fd != -1 && fstat(fd, &st) == 0 && S_ISREG(st.st_mode)) {
1082 if (map_size == 0) {
1083 map_size = st.st_size;
1084 } else if ((size_t)offset + (size_t)map_size > st.st_size) {
1085 PyErr_SetString(PyExc_ValueError,
1086 "mmap length is greater than file size");
1087 return NULL;
1088 }
1089 }
Neal Norwitzb5673922002-09-05 21:48:07 +00001090#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001091 m_obj = (mmap_object *)type->tp_alloc(type, 0);
1092 if (m_obj == NULL) {return NULL;}
1093 m_obj->data = NULL;
1094 m_obj->size = (size_t) map_size;
1095 m_obj->pos = (size_t) 0;
1096 m_obj->exports = 0;
1097 m_obj->offset = offset;
1098 if (fd == -1) {
1099 m_obj->fd = -1;
1100 /* Assume the caller wants to map anonymous memory.
1101 This is the same behaviour as Windows. mmap.mmap(-1, size)
1102 on both Windows and Unix map anonymous memory.
1103 */
Neal Norwitz0e6bc8c2006-02-05 05:45:43 +00001104#ifdef MAP_ANONYMOUS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001105 /* BSD way to map anonymous memory */
1106 flags |= MAP_ANONYMOUS;
Neal Norwitz0e6bc8c2006-02-05 05:45:43 +00001107#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001108 /* SVR4 method to map anonymous memory is to open /dev/zero */
1109 fd = devzero = open("/dev/zero", O_RDWR);
1110 if (devzero == -1) {
1111 Py_DECREF(m_obj);
1112 PyErr_SetFromErrno(mmap_module_error);
1113 return NULL;
1114 }
Neal Norwitz0e6bc8c2006-02-05 05:45:43 +00001115#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001116 } else {
1117 m_obj->fd = dup(fd);
1118 if (m_obj->fd == -1) {
1119 Py_DECREF(m_obj);
1120 PyErr_SetFromErrno(mmap_module_error);
1121 return NULL;
1122 }
1123 }
Neal Norwitz0e6bc8c2006-02-05 05:45:43 +00001124
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001125 m_obj->data = mmap(NULL, map_size,
1126 prot, flags,
1127 fd, offset);
Neal Norwitz0e6bc8c2006-02-05 05:45:43 +00001128
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001129 if (devzero != -1) {
1130 close(devzero);
1131 }
1132
1133 if (m_obj->data == (char *)-1) {
1134 m_obj->data = NULL;
1135 Py_DECREF(m_obj);
1136 PyErr_SetFromErrno(mmap_module_error);
1137 return NULL;
1138 }
1139 m_obj->access = (access_mode)access;
1140 return (PyObject *)m_obj;
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +00001141}
1142#endif /* UNIX */
1143
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001144#ifdef MS_WINDOWS
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +00001145static PyObject *
Georg Brandl86def6c2008-01-21 20:36:10 +00001146new_mmap_object(PyTypeObject *type, PyObject *args, PyObject *kwdict)
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +00001147{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001148 mmap_object *m_obj;
1149 PyObject *map_size_obj = NULL, *offset_obj = NULL;
1150 Py_ssize_t map_size, offset;
1151 DWORD off_hi; /* upper 32 bits of offset */
1152 DWORD off_lo; /* lower 32 bits of offset */
1153 DWORD size_hi; /* upper 32 bits of size */
1154 DWORD size_lo; /* lower 32 bits of size */
1155 char *tagname = "";
1156 DWORD dwErr = 0;
1157 int fileno;
1158 HANDLE fh = 0;
1159 int access = (access_mode)ACCESS_DEFAULT;
1160 DWORD flProtect, dwDesiredAccess;
1161 static char *keywords[] = { "fileno", "length",
1162 "tagname",
1163 "access", "offset", NULL };
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +00001164
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001165 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iO|ziO", keywords,
1166 &fileno, &map_size_obj,
1167 &tagname, &access, &offset_obj)) {
1168 return NULL;
1169 }
Tim Peters5ebfd362001-11-13 23:11:19 +00001170
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001171 switch((access_mode)access) {
1172 case ACCESS_READ:
1173 flProtect = PAGE_READONLY;
1174 dwDesiredAccess = FILE_MAP_READ;
1175 break;
1176 case ACCESS_DEFAULT: case ACCESS_WRITE:
1177 flProtect = PAGE_READWRITE;
1178 dwDesiredAccess = FILE_MAP_WRITE;
1179 break;
1180 case ACCESS_COPY:
1181 flProtect = PAGE_WRITECOPY;
1182 dwDesiredAccess = FILE_MAP_COPY;
1183 break;
1184 default:
1185 return PyErr_Format(PyExc_ValueError,
1186 "mmap invalid access parameter.");
1187 }
Tim Peters5ebfd362001-11-13 23:11:19 +00001188
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001189 map_size = _GetMapSize(map_size_obj, "size");
1190 if (map_size < 0)
1191 return NULL;
1192 offset = _GetMapSize(offset_obj, "offset");
1193 if (offset < 0)
1194 return NULL;
Tim Petersec0a5f02006-02-16 23:47:20 +00001195
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001196 /* assume -1 and 0 both mean invalid filedescriptor
1197 to 'anonymously' map memory.
1198 XXX: fileno == 0 is a valid fd, but was accepted prior to 2.5.
1199 XXX: Should this code be added?
1200 if (fileno == 0)
1201 PyErr_WarnEx(PyExc_DeprecationWarning,
1202 "don't use 0 for anonymous memory",
1203 1);
1204 */
1205 if (fileno != -1 && fileno != 0) {
1206 fh = (HANDLE)_get_osfhandle(fileno);
1207 if (fh==(HANDLE)-1) {
1208 PyErr_SetFromErrno(mmap_module_error);
1209 return NULL;
1210 }
1211 /* Win9x appears to need us seeked to zero */
1212 lseek(fileno, 0, SEEK_SET);
1213 }
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +00001214
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001215 m_obj = (mmap_object *)type->tp_alloc(type, 0);
1216 if (m_obj == NULL)
1217 return NULL;
1218 /* Set every field to an invalid marker, so we can safely
1219 destruct the object in the face of failure */
1220 m_obj->data = NULL;
1221 m_obj->file_handle = INVALID_HANDLE_VALUE;
1222 m_obj->map_handle = NULL;
1223 m_obj->tagname = NULL;
1224 m_obj->offset = offset;
Mark Hammond2cbed002000-07-30 02:22:43 +00001225
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001226 if (fh) {
1227 /* It is necessary to duplicate the handle, so the
1228 Python code can close it on us */
1229 if (!DuplicateHandle(
1230 GetCurrentProcess(), /* source process handle */
1231 fh, /* handle to be duplicated */
1232 GetCurrentProcess(), /* target proc handle */
1233 (LPHANDLE)&m_obj->file_handle, /* result */
1234 0, /* access - ignored due to options value */
1235 FALSE, /* inherited by child processes? */
1236 DUPLICATE_SAME_ACCESS)) { /* options */
1237 dwErr = GetLastError();
1238 Py_DECREF(m_obj);
1239 PyErr_SetFromWindowsErr(dwErr);
1240 return NULL;
1241 }
1242 if (!map_size) {
1243 DWORD low,high;
1244 low = GetFileSize(fh, &high);
1245 /* low might just happen to have the value INVALID_FILE_SIZE;
1246 so we need to check the last error also. */
1247 if (low == INVALID_FILE_SIZE &&
1248 (dwErr = GetLastError()) != NO_ERROR) {
1249 Py_DECREF(m_obj);
1250 return PyErr_SetFromWindowsErr(dwErr);
1251 }
Guido van Rossum98297ee2007-11-06 21:34:58 +00001252
Martin v. Löwis15186072006-02-18 12:38:35 +00001253#if SIZEOF_SIZE_T > 4
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001254 m_obj->size = (((size_t)high)<<32) + low;
Martin v. Löwis15186072006-02-18 12:38:35 +00001255#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001256 if (high)
1257 /* File is too large to map completely */
1258 m_obj->size = (size_t)-1;
1259 else
1260 m_obj->size = low;
Martin v. Löwis15186072006-02-18 12:38:35 +00001261#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001262 } else {
1263 m_obj->size = map_size;
1264 }
1265 }
1266 else {
1267 m_obj->size = map_size;
1268 }
Guido van Rossum09fdf072000-03-31 01:17:07 +00001269
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001270 /* set the initial position */
1271 m_obj->pos = (size_t) 0;
Guido van Rossum09fdf072000-03-31 01:17:07 +00001272
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001273 m_obj->exports = 0;
1274 /* set the tag name */
1275 if (tagname != NULL && *tagname != '\0') {
1276 m_obj->tagname = PyMem_Malloc(strlen(tagname)+1);
1277 if (m_obj->tagname == NULL) {
1278 PyErr_NoMemory();
1279 Py_DECREF(m_obj);
1280 return NULL;
1281 }
1282 strcpy(m_obj->tagname, tagname);
1283 }
1284 else
1285 m_obj->tagname = NULL;
Mark Hammond2cbed002000-07-30 02:22:43 +00001286
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001287 m_obj->access = (access_mode)access;
1288 /* DWORD is a 4-byte int. If we're on a box where size_t consumes
1289 * more than 4 bytes, we need to break it apart. Else (size_t
1290 * consumes 4 bytes), C doesn't define what happens if we shift
1291 * right by 32, so we need different code.
1292 */
Tim Peterse564e7f2006-02-16 23:46:01 +00001293#if SIZEOF_SIZE_T > 4
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001294 size_hi = (DWORD)((offset + m_obj->size) >> 32);
1295 size_lo = (DWORD)((offset + m_obj->size) & 0xFFFFFFFF);
1296 off_hi = (DWORD)(offset >> 32);
1297 off_lo = (DWORD)(offset & 0xFFFFFFFF);
Tim Peterse564e7f2006-02-16 23:46:01 +00001298#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001299 size_hi = 0;
1300 size_lo = (DWORD)(offset + m_obj->size);
1301 off_hi = 0;
1302 off_lo = (DWORD)offset;
Tim Peterse564e7f2006-02-16 23:46:01 +00001303#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001304 /* For files, it would be sufficient to pass 0 as size.
1305 For anonymous maps, we have to pass the size explicitly. */
1306 m_obj->map_handle = CreateFileMapping(m_obj->file_handle,
1307 NULL,
1308 flProtect,
1309 size_hi,
1310 size_lo,
1311 m_obj->tagname);
1312 if (m_obj->map_handle != NULL) {
1313 m_obj->data = (char *) MapViewOfFile(m_obj->map_handle,
1314 dwDesiredAccess,
1315 off_hi,
1316 off_lo,
1317 m_obj->size);
1318 if (m_obj->data != NULL)
1319 return (PyObject *)m_obj;
1320 else {
1321 dwErr = GetLastError();
1322 CloseHandle(m_obj->map_handle);
1323 m_obj->map_handle = NULL;
1324 }
1325 } else
1326 dwErr = GetLastError();
1327 Py_DECREF(m_obj);
1328 PyErr_SetFromWindowsErr(dwErr);
1329 return NULL;
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +00001330}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001331#endif /* MS_WINDOWS */
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +00001332
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001333static void
1334setint(PyObject *d, const char *name, long value)
1335{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001336 PyObject *o = PyLong_FromLong(value);
1337 if (o && PyDict_SetItemString(d, name, o) == 0) {
1338 Py_DECREF(o);
1339 }
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001340}
1341
Martin v. Löwis1a214512008-06-11 05:26:20 +00001342
1343static struct PyModuleDef mmapmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001344 PyModuleDef_HEAD_INIT,
1345 "mmap",
1346 NULL,
1347 -1,
1348 NULL,
1349 NULL,
1350 NULL,
1351 NULL,
1352 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001353};
1354
Mark Hammond62b1ab12002-07-23 06:31:15 +00001355PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00001356PyInit_mmap(void)
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +00001357{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001358 PyObject *dict, *module;
Tim Peters2caf8df2001-01-14 05:05:51 +00001359
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001360 if (PyType_Ready(&mmap_object_type) < 0)
1361 return NULL;
Tim Peters2caf8df2001-01-14 05:05:51 +00001362
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001363 module = PyModule_Create(&mmapmodule);
1364 if (module == NULL)
1365 return NULL;
1366 dict = PyModule_GetDict(module);
1367 if (!dict)
1368 return NULL;
1369 mmap_module_error = PyErr_NewException("mmap.error",
1370 PyExc_EnvironmentError , NULL);
1371 if (mmap_module_error == NULL)
1372 return NULL;
1373 PyDict_SetItemString(dict, "error", mmap_module_error);
1374 PyDict_SetItemString(dict, "mmap", (PyObject*) &mmap_object_type);
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +00001375#ifdef PROT_EXEC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001376 setint(dict, "PROT_EXEC", PROT_EXEC);
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +00001377#endif
1378#ifdef PROT_READ
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001379 setint(dict, "PROT_READ", PROT_READ);
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +00001380#endif
1381#ifdef PROT_WRITE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001382 setint(dict, "PROT_WRITE", PROT_WRITE);
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +00001383#endif
1384
1385#ifdef MAP_SHARED
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001386 setint(dict, "MAP_SHARED", MAP_SHARED);
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +00001387#endif
1388#ifdef MAP_PRIVATE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001389 setint(dict, "MAP_PRIVATE", MAP_PRIVATE);
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +00001390#endif
1391#ifdef MAP_DENYWRITE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001392 setint(dict, "MAP_DENYWRITE", MAP_DENYWRITE);
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +00001393#endif
1394#ifdef MAP_EXECUTABLE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001395 setint(dict, "MAP_EXECUTABLE", MAP_EXECUTABLE);
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +00001396#endif
Neal Norwitz0e6bc8c2006-02-05 05:45:43 +00001397#ifdef MAP_ANONYMOUS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001398 setint(dict, "MAP_ANON", MAP_ANONYMOUS);
1399 setint(dict, "MAP_ANONYMOUS", MAP_ANONYMOUS);
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +00001400#endif
1401
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001402 setint(dict, "PAGESIZE", (long)my_getpagesize());
Andrew M. Kuchling961fe172000-06-03 19:41:42 +00001403
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001404 setint(dict, "ALLOCATIONGRANULARITY", (long)my_getallocationgranularity());
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001405
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001406 setint(dict, "ACCESS_READ", ACCESS_READ);
1407 setint(dict, "ACCESS_WRITE", ACCESS_WRITE);
1408 setint(dict, "ACCESS_COPY", ACCESS_COPY);
1409 return module;
Tim Peters5ebfd362001-11-13 23:11:19 +00001410}