py-cvs-rel2_1 (Rev 1.2) merge
[python/dscho.git] / Modules / mmapmodule.c
blob63ef72a7f36d8c83d418eb60db1a93b144e00108
1 /*
2 / Author: Sam Rushing <rushing@nightmare.com>
3 / Hacked for Unix by A.M. Kuchling <amk1@bigfoot.com>
4 / $Id$
6 / mmapmodule.cpp -- map a view of a file into memory
8 / todo: need permission flags, perhaps a 'chsize' analog
9 / not all functions check range yet!!!
12 / Note: This module currently only deals with 32-bit file
13 / sizes.
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
18 / ftp://squirl.nightmare.com/pub/python/python-ext.
21 #include <Python.h>
23 #ifndef MS_WIN32
24 #define UNIX
25 #endif
27 #ifdef MS_WIN32
28 #include <windows.h>
29 static int
30 my_getpagesize(void)
32 SYSTEM_INFO si;
33 GetSystemInfo(&si);
34 return si.dwPageSize;
36 #endif
38 #ifdef UNIX
39 #include <unistd.h>
40 #include <sys/mman.h>
41 #include <sys/stat.h>
43 #ifndef MS_SYNC
44 /* This is missing e.g. on SunOS 4.1.4 */
45 #define MS_SYNC 0
46 #endif
48 #if defined(HAVE_SYSCONF) && defined(_SC_PAGESIZE)
49 static int
50 my_getpagesize(void)
52 return sysconf(_SC_PAGESIZE);
54 #else
55 #define my_getpagesize getpagesize
56 #endif
58 #endif /* UNIX */
60 #include <string.h>
61 #include <sys/types.h>
63 static PyObject *mmap_module_error;
65 typedef struct {
66 PyObject_HEAD
67 char * data;
68 size_t size;
69 size_t pos;
71 #ifdef MS_WIN32
72 HANDLE map_handle;
73 HANDLE file_handle;
74 char * tagname;
75 #endif
77 #ifdef UNIX
78 int fd;
79 #endif
80 } mmap_object;
82 static void
83 mmap_object_dealloc(mmap_object *m_obj)
85 #ifdef MS_WIN32
86 if (m_obj->data != NULL)
87 UnmapViewOfFile (m_obj->data);
88 if (m_obj->map_handle != INVALID_HANDLE_VALUE)
89 CloseHandle (m_obj->map_handle);
90 if (m_obj->file_handle != INVALID_HANDLE_VALUE)
91 CloseHandle (m_obj->file_handle);
92 if (m_obj->tagname)
93 PyMem_Free(m_obj->tagname);
94 #endif /* MS_WIN32 */
96 #ifdef UNIX
97 if (m_obj->data!=NULL) {
98 msync(m_obj->data, m_obj->size, MS_SYNC);
99 munmap(m_obj->data, m_obj->size);
101 #endif /* UNIX */
103 PyObject_Del(m_obj);
106 static PyObject *
107 mmap_close_method(mmap_object *self, PyObject *args)
109 if (!PyArg_ParseTuple(args, ":close"))
110 return NULL;
111 #ifdef MS_WIN32
112 /* For each resource we maintain, we need to check
113 the value is valid, and if so, free the resource
114 and set the member value to an invalid value so
115 the dealloc does not attempt to resource clearing
116 again.
117 TODO - should we check for errors in the close operations???
119 if (self->data != NULL) {
120 UnmapViewOfFile (self->data);
121 self->data = NULL;
123 if (self->map_handle != INVALID_HANDLE_VALUE) {
124 CloseHandle (self->map_handle);
125 self->map_handle = INVALID_HANDLE_VALUE;
127 if (self->file_handle != INVALID_HANDLE_VALUE) {
128 CloseHandle (self->file_handle);
129 self->file_handle = INVALID_HANDLE_VALUE;
131 #endif /* MS_WIN32 */
133 #ifdef UNIX
134 munmap(self->data, self->size);
135 self->data = NULL;
136 #endif
138 Py_INCREF (Py_None);
139 return (Py_None);
142 #ifdef MS_WIN32
143 #define CHECK_VALID(err) \
144 do { \
145 if (!self->map_handle) { \
146 PyErr_SetString (PyExc_ValueError, "mmap closed or invalid"); \
147 return err; \
149 } while (0)
150 #endif /* MS_WIN32 */
152 #ifdef UNIX
153 #define CHECK_VALID(err) \
154 do { \
155 if (self->data == NULL) { \
156 PyErr_SetString (PyExc_ValueError, "mmap closed or invalid"); \
157 return err; \
159 } while (0)
160 #endif /* UNIX */
162 static PyObject *
163 mmap_read_byte_method(mmap_object *self,
164 PyObject *args)
166 CHECK_VALID(NULL);
167 if (!PyArg_ParseTuple(args, ":read_byte"))
168 return NULL;
169 if (self->pos < self->size) {
170 char value = self->data[self->pos];
171 self->pos += 1;
172 return Py_BuildValue("c", value);
173 } else {
174 PyErr_SetString (PyExc_ValueError, "read byte out of range");
175 return NULL;
179 static PyObject *
180 mmap_read_line_method(mmap_object *self,
181 PyObject *args)
183 char *start = self->data+self->pos;
184 char *eof = self->data+self->size;
185 char *eol;
186 PyObject *result;
188 CHECK_VALID(NULL);
189 if (!PyArg_ParseTuple(args, ":readline"))
190 return NULL;
192 eol = memchr(start, '\n', self->size - self->pos);
193 if (!eol)
194 eol = eof;
195 else
196 ++eol; /* we're interested in the position after the
197 newline. */
198 result = PyString_FromStringAndSize(start, (eol - start));
199 self->pos += (eol - start);
200 return (result);
203 static PyObject *
204 mmap_read_method(mmap_object *self,
205 PyObject *args)
207 long num_bytes;
208 PyObject *result;
210 CHECK_VALID(NULL);
211 if (!PyArg_ParseTuple(args, "l:read", &num_bytes))
212 return(NULL);
214 /* silently 'adjust' out-of-range requests */
215 if ((self->pos + num_bytes) > self->size) {
216 num_bytes -= (self->pos+num_bytes) - self->size;
218 result = Py_BuildValue("s#", self->data+self->pos, num_bytes);
219 self->pos += num_bytes;
220 return (result);
223 static PyObject *
224 mmap_find_method(mmap_object *self,
225 PyObject *args)
227 long start = self->pos;
228 char *needle;
229 int len;
231 CHECK_VALID(NULL);
232 if (!PyArg_ParseTuple (args, "s#|l:find", &needle, &len, &start)) {
233 return NULL;
234 } else {
235 char *p;
236 char *e = self->data + self->size;
238 if (start < 0)
239 start += self->size;
240 if (start < 0)
241 start = 0;
242 else if ((size_t)start > self->size)
243 start = self->size;
244 p = self->data + start;
246 while (p < e) {
247 char *s = p;
248 char *n = needle;
249 while ((s<e) && (*n) && !(*s-*n)) {
250 s++, n++;
252 if (!*n) {
253 return Py_BuildValue (
254 "l",
255 (long) (p - self->data));
257 p++;
259 return Py_BuildValue ("l", (long) -1);
263 static PyObject *
264 mmap_write_method(mmap_object *self,
265 PyObject *args)
267 long length;
268 char *data;
270 CHECK_VALID(NULL);
271 if (!PyArg_ParseTuple (args, "s#:write", &data, &length))
272 return(NULL);
274 if ((self->pos + length) > self->size) {
275 PyErr_SetString (PyExc_ValueError, "data out of range");
276 return NULL;
278 memcpy (self->data+self->pos, data, length);
279 self->pos = self->pos+length;
280 Py_INCREF (Py_None);
281 return (Py_None);
284 static PyObject *
285 mmap_write_byte_method(mmap_object *self,
286 PyObject *args)
288 char value;
290 CHECK_VALID(NULL);
291 if (!PyArg_ParseTuple (args, "c:write_byte", &value))
292 return(NULL);
294 *(self->data+self->pos) = value;
295 self->pos += 1;
296 Py_INCREF (Py_None);
297 return (Py_None);
300 static PyObject *
301 mmap_size_method(mmap_object *self,
302 PyObject *args)
304 CHECK_VALID(NULL);
305 if (!PyArg_ParseTuple(args, ":size"))
306 return NULL;
308 #ifdef MS_WIN32
309 if (self->file_handle != INVALID_HANDLE_VALUE) {
310 return (Py_BuildValue (
311 "l", (long)
312 GetFileSize (self->file_handle, NULL)));
313 } else {
314 return (Py_BuildValue ("l", (long) self->size) );
316 #endif /* MS_WIN32 */
318 #ifdef UNIX
320 struct stat buf;
321 if (-1 == fstat(self->fd, &buf)) {
322 PyErr_SetFromErrno(mmap_module_error);
323 return NULL;
325 return (Py_BuildValue ("l", (long) buf.st_size) );
327 #endif /* UNIX */
330 /* This assumes that you want the entire file mapped,
331 / and when recreating the map will make the new file
332 / have the new size
334 / Is this really necessary? This could easily be done
335 / from python by just closing and re-opening with the
336 / new size?
339 static PyObject *
340 mmap_resize_method(mmap_object *self,
341 PyObject *args)
343 unsigned long new_size;
344 CHECK_VALID(NULL);
345 if (!PyArg_ParseTuple (args, "l:resize", &new_size)) {
346 return NULL;
347 #ifdef MS_WIN32
348 } else {
349 DWORD dwErrCode = 0;
350 /* First, unmap the file view */
351 UnmapViewOfFile (self->data);
352 /* Close the mapping object */
353 CloseHandle (self->map_handle);
354 /* Move to the desired EOF position */
355 SetFilePointer (self->file_handle,
356 new_size, NULL, FILE_BEGIN);
357 /* Change the size of the file */
358 SetEndOfFile (self->file_handle);
359 /* Create another mapping object and remap the file view */
360 self->map_handle = CreateFileMapping (
361 self->file_handle,
362 NULL,
363 PAGE_READWRITE,
365 new_size,
366 self->tagname);
367 if (self->map_handle != NULL) {
368 self->data = (char *) MapViewOfFile (self->map_handle,
369 FILE_MAP_WRITE,
373 if (self->data != NULL) {
374 self->size = new_size;
375 Py_INCREF (Py_None);
376 return Py_None;
377 } else {
378 dwErrCode = GetLastError();
380 } else {
381 dwErrCode = GetLastError();
383 PyErr_SetFromWindowsErr(dwErrCode);
384 return (NULL);
385 #endif /* MS_WIN32 */
387 #ifdef UNIX
388 #ifndef HAVE_MREMAP
389 } else {
390 PyErr_SetString(PyExc_SystemError,
391 "mmap: resizing not available--no mremap()");
392 return NULL;
393 #else
394 } else {
395 void *newmap;
397 #ifdef MREMAP_MAYMOVE
398 newmap = mremap(self->data, self->size, new_size, MREMAP_MAYMOVE);
399 #else
400 newmap = mremap(self->data, self->size, new_size, 0);
401 #endif
402 if (newmap == (void *)-1)
404 PyErr_SetFromErrno(mmap_module_error);
405 return NULL;
407 self->data = newmap;
408 self->size = new_size;
409 Py_INCREF(Py_None);
410 return Py_None;
411 #endif /* HAVE_MREMAP */
412 #endif /* UNIX */
416 static PyObject *
417 mmap_tell_method(mmap_object *self, PyObject *args)
419 CHECK_VALID(NULL);
420 if (!PyArg_ParseTuple(args, ":tell"))
421 return NULL;
422 return (Py_BuildValue ("l", (long) self->pos) );
425 static PyObject *
426 mmap_flush_method(mmap_object *self, PyObject *args)
428 size_t offset = 0;
429 size_t size = self->size;
430 CHECK_VALID(NULL);
431 if (!PyArg_ParseTuple (args, "|ll:flush", &offset, &size)) {
432 return NULL;
433 } else if ((offset + size) > self->size) {
434 PyErr_SetString (PyExc_ValueError,
435 "flush values out of range");
436 return NULL;
437 } else {
438 #ifdef MS_WIN32
439 return (Py_BuildValue("l", (long)
440 FlushViewOfFile(self->data+offset, size)));
441 #endif /* MS_WIN32 */
442 #ifdef UNIX
443 /* XXX semantics of return value? */
444 /* XXX flags for msync? */
445 if (-1 == msync(self->data + offset, size,
446 MS_SYNC))
448 PyErr_SetFromErrno(mmap_module_error);
449 return NULL;
451 return Py_BuildValue ("l", (long) 0);
452 #endif /* UNIX */
456 static PyObject *
457 mmap_seek_method(mmap_object *self, PyObject *args)
459 int dist;
460 int how=0;
461 CHECK_VALID(NULL);
462 if (!PyArg_ParseTuple (args, "i|i:seek", &dist, &how)) {
463 return(NULL);
464 } else {
465 size_t where;
466 switch (how) {
467 case 0: /* relative to start */
468 if (dist < 0)
469 goto onoutofrange;
470 where = dist;
471 break;
472 case 1: /* relative to current position */
473 if ((int)self->pos + dist < 0)
474 goto onoutofrange;
475 where = self->pos + dist;
476 break;
477 case 2: /* relative to end */
478 if ((int)self->size + dist < 0)
479 goto onoutofrange;
480 where = self->size + dist;
481 break;
482 default:
483 PyErr_SetString (PyExc_ValueError,
484 "unknown seek type");
485 return NULL;
487 if (where > self->size)
488 goto onoutofrange;
489 self->pos = where;
490 Py_INCREF (Py_None);
491 return (Py_None);
494 onoutofrange:
495 PyErr_SetString (PyExc_ValueError, "seek out of range");
496 return NULL;
499 static PyObject *
500 mmap_move_method(mmap_object *self, PyObject *args)
502 unsigned long dest, src, count;
503 CHECK_VALID(NULL);
504 if (!PyArg_ParseTuple (args, "iii:move", &dest, &src, &count)) {
505 return NULL;
506 } else {
507 /* bounds check the values */
508 if (/* end of source after end of data?? */
509 ((src+count) > self->size)
510 /* dest will fit? */
511 || (dest+count > self->size)) {
512 PyErr_SetString (PyExc_ValueError,
513 "source or destination out of range");
514 return NULL;
515 } else {
516 memmove (self->data+dest, self->data+src, count);
517 Py_INCREF (Py_None);
518 return Py_None;
523 static struct PyMethodDef mmap_object_methods[] = {
524 {"close", (PyCFunction) mmap_close_method, 1},
525 {"find", (PyCFunction) mmap_find_method, 1},
526 {"flush", (PyCFunction) mmap_flush_method, 1},
527 {"move", (PyCFunction) mmap_move_method, 1},
528 {"read", (PyCFunction) mmap_read_method, 1},
529 {"read_byte", (PyCFunction) mmap_read_byte_method, 1},
530 {"readline", (PyCFunction) mmap_read_line_method, 1},
531 {"resize", (PyCFunction) mmap_resize_method, 1},
532 {"seek", (PyCFunction) mmap_seek_method, 1},
533 {"size", (PyCFunction) mmap_size_method, 1},
534 {"tell", (PyCFunction) mmap_tell_method, 1},
535 {"write", (PyCFunction) mmap_write_method, 1},
536 {"write_byte", (PyCFunction) mmap_write_byte_method, 1},
537 {NULL, NULL} /* sentinel */
540 /* Functions for treating an mmap'ed file as a buffer */
542 static int
543 mmap_buffer_getreadbuf(mmap_object *self, int index, const void **ptr)
545 CHECK_VALID(-1);
546 if ( index != 0 ) {
547 PyErr_SetString(PyExc_SystemError,
548 "Accessing non-existent mmap segment");
549 return -1;
551 *ptr = self->data;
552 return self->size;
555 static int
556 mmap_buffer_getwritebuf(mmap_object *self, int index, const void **ptr)
558 CHECK_VALID(-1);
559 if ( index != 0 ) {
560 PyErr_SetString(PyExc_SystemError,
561 "Accessing non-existent mmap segment");
562 return -1;
564 *ptr = self->data;
565 return self->size;
568 static int
569 mmap_buffer_getsegcount(mmap_object *self, int *lenp)
571 CHECK_VALID(-1);
572 if (lenp)
573 *lenp = self->size;
574 return 1;
577 static int
578 mmap_buffer_getcharbuffer(mmap_object *self, int index, const void **ptr)
580 if ( index != 0 ) {
581 PyErr_SetString(PyExc_SystemError,
582 "accessing non-existent buffer segment");
583 return -1;
585 *ptr = (const char *)self->data;
586 return self->size;
589 static PyObject *
590 mmap_object_getattr(mmap_object *self, char *name)
592 return Py_FindMethod (mmap_object_methods, (PyObject *)self, name);
595 static int
596 mmap_length(mmap_object *self)
598 CHECK_VALID(-1);
599 return self->size;
602 static PyObject *
603 mmap_item(mmap_object *self, int i)
605 CHECK_VALID(NULL);
606 if (i < 0 || (size_t)i >= self->size) {
607 PyErr_SetString(PyExc_IndexError, "mmap index out of range");
608 return NULL;
610 return PyString_FromStringAndSize(self->data + i, 1);
613 static PyObject *
614 mmap_slice(mmap_object *self, int ilow, int ihigh)
616 CHECK_VALID(NULL);
617 if (ilow < 0)
618 ilow = 0;
619 else if ((size_t)ilow > self->size)
620 ilow = self->size;
621 if (ihigh < 0)
622 ihigh = 0;
623 if (ihigh < ilow)
624 ihigh = ilow;
625 else if ((size_t)ihigh > self->size)
626 ihigh = self->size;
628 return PyString_FromStringAndSize(self->data + ilow, ihigh-ilow);
631 static PyObject *
632 mmap_concat(mmap_object *self, PyObject *bb)
634 CHECK_VALID(NULL);
635 PyErr_SetString(PyExc_SystemError,
636 "mmaps don't support concatenation");
637 return NULL;
640 static PyObject *
641 mmap_repeat(mmap_object *self, int n)
643 CHECK_VALID(NULL);
644 PyErr_SetString(PyExc_SystemError,
645 "mmaps don't support repeat operation");
646 return NULL;
649 static int
650 mmap_ass_slice(mmap_object *self, int ilow, int ihigh, PyObject *v)
652 const char *buf;
654 CHECK_VALID(-1);
655 if (ilow < 0)
656 ilow = 0;
657 else if ((size_t)ilow > self->size)
658 ilow = self->size;
659 if (ihigh < 0)
660 ihigh = 0;
661 if (ihigh < ilow)
662 ihigh = ilow;
663 else if ((size_t)ihigh > self->size)
664 ihigh = self->size;
666 if (! (PyString_Check(v)) ) {
667 PyErr_SetString(PyExc_IndexError,
668 "mmap slice assignment must be a string");
669 return -1;
671 if ( PyString_Size(v) != (ihigh - ilow) ) {
672 PyErr_SetString(PyExc_IndexError,
673 "mmap slice assignment is wrong size");
674 return -1;
676 buf = PyString_AsString(v);
677 memcpy(self->data + ilow, buf, ihigh-ilow);
678 return 0;
681 static int
682 mmap_ass_item(mmap_object *self, int i, PyObject *v)
684 const char *buf;
686 CHECK_VALID(-1);
687 if (i < 0 || (size_t)i >= self->size) {
688 PyErr_SetString(PyExc_IndexError, "mmap index out of range");
689 return -1;
691 if (! (PyString_Check(v) && PyString_Size(v)==1) ) {
692 PyErr_SetString(PyExc_IndexError,
693 "mmap assignment must be single-character string");
694 return -1;
696 buf = PyString_AsString(v);
697 self->data[i] = buf[0];
698 return 0;
701 static PySequenceMethods mmap_as_sequence = {
702 (inquiry)mmap_length, /*sq_length*/
703 (binaryfunc)mmap_concat, /*sq_concat*/
704 (intargfunc)mmap_repeat, /*sq_repeat*/
705 (intargfunc)mmap_item, /*sq_item*/
706 (intintargfunc)mmap_slice, /*sq_slice*/
707 (intobjargproc)mmap_ass_item, /*sq_ass_item*/
708 (intintobjargproc)mmap_ass_slice, /*sq_ass_slice*/
711 static PyBufferProcs mmap_as_buffer = {
712 (getreadbufferproc)mmap_buffer_getreadbuf,
713 (getwritebufferproc)mmap_buffer_getwritebuf,
714 (getsegcountproc)mmap_buffer_getsegcount,
715 (getcharbufferproc)mmap_buffer_getcharbuffer,
718 static PyTypeObject mmap_object_type = {
719 PyObject_HEAD_INIT(0) /* patched in module init */
720 0, /* ob_size */
721 "mmap", /* tp_name */
722 sizeof(mmap_object), /* tp_size */
723 0, /* tp_itemsize */
724 /* methods */
725 (destructor) mmap_object_dealloc, /* tp_dealloc */
726 0, /* tp_print */
727 (getattrfunc) mmap_object_getattr, /* tp_getattr */
728 0, /* tp_setattr */
729 0, /* tp_compare */
730 0, /* tp_repr */
731 0, /* tp_as_number */
732 &mmap_as_sequence, /*tp_as_sequence*/
733 0, /*tp_as_mapping*/
734 0, /*tp_hash*/
735 0, /*tp_call*/
736 0, /*tp_str*/
737 0, /*tp_getattro*/
738 0, /*tp_setattro*/
739 &mmap_as_buffer, /*tp_as_buffer*/
740 Py_TPFLAGS_HAVE_GETCHARBUFFER, /*tp_flags*/
741 0, /*tp_doc*/
745 /* extract the map size from the given PyObject
747 The map size is restricted to [0, INT_MAX] because this is the current
748 Python limitation on object sizes. Although the mmap object *could* handle
749 a larger map size, there is no point because all the useful operations
750 (len(), slicing(), sequence indexing) are limited by a C int.
752 Returns -1 on error, with an appropriate Python exception raised. On
753 success, the map size is returned. */
754 static int
755 _GetMapSize(PyObject *o)
757 if (PyInt_Check(o)) {
758 long i = PyInt_AsLong(o);
759 if (PyErr_Occurred())
760 return -1;
761 if (i < 0)
762 goto onnegoverflow;
763 if (i > INT_MAX)
764 goto onposoverflow;
765 return (int)i;
767 else if (PyLong_Check(o)) {
768 long i = PyLong_AsLong(o);
769 if (PyErr_Occurred()) {
770 /* yes negative overflow is mistaken for positive overflow
771 but not worth the trouble to check sign of 'i' */
772 if (PyErr_ExceptionMatches(PyExc_OverflowError))
773 goto onposoverflow;
774 else
775 return -1;
777 if (i < 0)
778 goto onnegoverflow;
779 if (i > INT_MAX)
780 goto onposoverflow;
781 return (int)i;
783 else {
784 PyErr_SetString(PyExc_TypeError,
785 "map size must be an integral value");
786 return -1;
789 onnegoverflow:
790 PyErr_SetString(PyExc_OverflowError,
791 "memory mapped size must be positive");
792 return -1;
794 onposoverflow:
795 PyErr_SetString(PyExc_OverflowError,
796 "memory mapped size is too large (limited by C int)");
797 return -1;
800 #ifdef UNIX
801 static PyObject *
802 new_mmap_object(PyObject *self, PyObject *args, PyObject *kwdict)
804 mmap_object *m_obj;
805 PyObject *map_size_obj = NULL;
806 int map_size;
807 int fd, flags = MAP_SHARED, prot = PROT_WRITE | PROT_READ;
808 char *keywords[] = {"file", "size", "flags", "prot", NULL};
810 if (!PyArg_ParseTupleAndKeywords(args, kwdict,
811 "iO|ii", keywords,
812 &fd, &map_size_obj, &flags, &prot)
814 return NULL;
815 map_size = _GetMapSize(map_size_obj);
816 if (map_size < 0)
817 return NULL;
819 m_obj = PyObject_New (mmap_object, &mmap_object_type);
820 if (m_obj == NULL) {return NULL;}
821 m_obj->size = (size_t) map_size;
822 m_obj->pos = (size_t) 0;
823 m_obj->fd = fd;
824 m_obj->data = mmap(NULL, map_size,
825 prot, flags,
826 fd, 0);
827 if (m_obj->data == (char *)-1)
829 Py_DECREF(m_obj);
830 PyErr_SetFromErrno(mmap_module_error);
831 return NULL;
833 return (PyObject *)m_obj;
835 #endif /* UNIX */
837 #ifdef MS_WIN32
838 static PyObject *
839 new_mmap_object(PyObject *self, PyObject *args)
841 mmap_object *m_obj;
842 PyObject *map_size_obj = NULL;
843 int map_size;
844 char *tagname = "";
846 DWORD dwErr = 0;
847 int fileno;
848 HANDLE fh = 0;
850 if (!PyArg_ParseTuple(args,
851 "iO|z",
852 &fileno,
853 &map_size_obj,
854 &tagname)
856 return NULL;
858 map_size = _GetMapSize(map_size_obj);
859 if (map_size < 0)
860 return NULL;
862 /* if an actual filename has been specified */
863 if (fileno != 0) {
864 fh = (HANDLE)_get_osfhandle(fileno);
865 if (fh==(HANDLE)-1) {
866 PyErr_SetFromErrno(mmap_module_error);
867 return NULL;
869 /* Win9x appears to need us seeked to zero */
870 fseek(&_iob[fileno], 0, SEEK_SET);
873 m_obj = PyObject_New (mmap_object, &mmap_object_type);
874 if (m_obj==NULL)
875 return NULL;
876 /* Set every field to an invalid marker, so we can safely
877 destruct the object in the face of failure */
878 m_obj->data = NULL;
879 m_obj->file_handle = INVALID_HANDLE_VALUE;
880 m_obj->map_handle = INVALID_HANDLE_VALUE;
881 m_obj->tagname = NULL;
883 if (fh) {
884 /* It is necessary to duplicate the handle, so the
885 Python code can close it on us */
886 if (!DuplicateHandle(
887 GetCurrentProcess(), /* source process handle */
888 fh, /* handle to be duplicated */
889 GetCurrentProcess(), /* target proc handle */
890 (LPHANDLE)&m_obj->file_handle, /* result */
891 0, /* access - ignored due to options value */
892 FALSE, /* inherited by child processes? */
893 DUPLICATE_SAME_ACCESS)) { /* options */
894 dwErr = GetLastError();
895 Py_DECREF(m_obj);
896 PyErr_SetFromWindowsErr(dwErr);
897 return NULL;
899 if (!map_size) {
900 m_obj->size = GetFileSize (fh, NULL);
901 } else {
902 m_obj->size = map_size;
905 else {
906 m_obj->size = map_size;
909 /* set the initial position */
910 m_obj->pos = (size_t) 0;
912 /* set the tag name */
913 if (tagname != NULL && *tagname != '\0') {
914 m_obj->tagname = PyMem_Malloc(strlen(tagname)+1);
915 if (m_obj->tagname == NULL) {
916 PyErr_NoMemory();
917 Py_DECREF(m_obj);
918 return NULL;
920 strcpy(m_obj->tagname, tagname);
922 else
923 m_obj->tagname = NULL;
925 m_obj->map_handle = CreateFileMapping (m_obj->file_handle,
926 NULL,
927 PAGE_READWRITE,
929 m_obj->size,
930 m_obj->tagname);
931 if (m_obj->map_handle != NULL) {
932 m_obj->data = (char *) MapViewOfFile (m_obj->map_handle,
933 FILE_MAP_WRITE,
937 if (m_obj->data != NULL) {
938 return ((PyObject *) m_obj);
939 } else {
940 dwErr = GetLastError();
942 } else {
943 dwErr = GetLastError();
945 Py_DECREF(m_obj);
946 PyErr_SetFromWindowsErr(dwErr);
947 return (NULL);
949 #endif /* MS_WIN32 */
951 /* List of functions exported by this module */
952 static struct PyMethodDef mmap_functions[] = {
953 {"mmap", (PyCFunction) new_mmap_object,
954 METH_VARARGS|METH_KEYWORDS},
955 {NULL, NULL} /* Sentinel */
958 DL_EXPORT(void)
959 initmmap(void)
961 PyObject *dict, *module;
963 /* Patch the object type */
964 mmap_object_type.ob_type = &PyType_Type;
966 module = Py_InitModule ("mmap", mmap_functions);
967 dict = PyModule_GetDict (module);
968 mmap_module_error = PyExc_EnvironmentError;
969 Py_INCREF(mmap_module_error);
970 PyDict_SetItemString (dict, "error", mmap_module_error);
971 #ifdef PROT_EXEC
972 PyDict_SetItemString (dict, "PROT_EXEC", PyInt_FromLong(PROT_EXEC) );
973 #endif
974 #ifdef PROT_READ
975 PyDict_SetItemString (dict, "PROT_READ", PyInt_FromLong(PROT_READ) );
976 #endif
977 #ifdef PROT_WRITE
978 PyDict_SetItemString (dict, "PROT_WRITE", PyInt_FromLong(PROT_WRITE) );
979 #endif
981 #ifdef MAP_SHARED
982 PyDict_SetItemString (dict, "MAP_SHARED", PyInt_FromLong(MAP_SHARED) );
983 #endif
984 #ifdef MAP_PRIVATE
985 PyDict_SetItemString (dict, "MAP_PRIVATE",
986 PyInt_FromLong(MAP_PRIVATE) );
987 #endif
988 #ifdef MAP_DENYWRITE
989 PyDict_SetItemString (dict, "MAP_DENYWRITE",
990 PyInt_FromLong(MAP_DENYWRITE) );
991 #endif
992 #ifdef MAP_EXECUTABLE
993 PyDict_SetItemString (dict, "MAP_EXECUTABLE",
994 PyInt_FromLong(MAP_EXECUTABLE) );
995 #endif
996 #ifdef MAP_ANON
997 PyDict_SetItemString (dict, "MAP_ANON", PyInt_FromLong(MAP_ANON) );
998 PyDict_SetItemString (dict, "MAP_ANONYMOUS",
999 PyInt_FromLong(MAP_ANON) );
1000 #endif
1002 PyDict_SetItemString (dict, "PAGESIZE",
1003 PyInt_FromLong( (long)my_getpagesize() ) );