2 / Author: Sam Rushing <rushing@nightmare.com>
3 / Hacked for Unix by AMK
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 / This version of mmapmodule.c has been changed significantly
13 / from the original mmapfile.c on which it was based.
14 / The original version of mmapfile is maintained by Sam at
15 / ftp://squirl.nightmare.com/pub/python/python-ext.
18 #define PY_SSIZE_T_CLEAN
40 #if defined(HAVE_SYSCONF) && defined(_SC_PAGESIZE)
44 return sysconf(_SC_PAGESIZE
);
47 #define my_getpagesize getpagesize
54 #ifdef HAVE_SYS_TYPES_H
55 #include <sys/types.h>
56 #endif /* HAVE_SYS_TYPES_H */
58 /* Prefer MAP_ANONYMOUS since MAP_ANON is deprecated according to man page. */
59 #if !defined(MAP_ANONYMOUS) && defined(MAP_ANON)
60 # define MAP_ANONYMOUS MAP_ANON
63 static PyObject
*mmap_module_error
;
94 mmap_object_dealloc(mmap_object
*m_obj
)
97 if (m_obj
->data
!= NULL
)
98 UnmapViewOfFile (m_obj
->data
);
99 if (m_obj
->map_handle
!= INVALID_HANDLE_VALUE
)
100 CloseHandle (m_obj
->map_handle
);
101 if (m_obj
->file_handle
!= INVALID_HANDLE_VALUE
)
102 CloseHandle (m_obj
->file_handle
);
104 PyMem_Free(m_obj
->tagname
);
105 #endif /* MS_WINDOWS */
109 (void) close(m_obj
->fd
);
110 if (m_obj
->data
!=NULL
) {
111 msync(m_obj
->data
, m_obj
->size
, MS_SYNC
);
112 munmap(m_obj
->data
, m_obj
->size
);
120 mmap_close_method(mmap_object
*self
, PyObject
*unused
)
123 /* For each resource we maintain, we need to check
124 the value is valid, and if so, free the resource
125 and set the member value to an invalid value so
126 the dealloc does not attempt to resource clearing
128 TODO - should we check for errors in the close operations???
130 if (self
->data
!= NULL
) {
131 UnmapViewOfFile(self
->data
);
134 if (self
->map_handle
!= INVALID_HANDLE_VALUE
) {
135 CloseHandle(self
->map_handle
);
136 self
->map_handle
= INVALID_HANDLE_VALUE
;
138 if (self
->file_handle
!= INVALID_HANDLE_VALUE
) {
139 CloseHandle(self
->file_handle
);
140 self
->file_handle
= INVALID_HANDLE_VALUE
;
142 #endif /* MS_WINDOWS */
145 (void) close(self
->fd
);
147 if (self
->data
!= NULL
) {
148 munmap(self
->data
, self
->size
);
158 #define CHECK_VALID(err) \
160 if (self->map_handle == INVALID_HANDLE_VALUE) { \
161 PyErr_SetString(PyExc_ValueError, "mmap closed or invalid"); \
165 #endif /* MS_WINDOWS */
168 #define CHECK_VALID(err) \
170 if (self->data == NULL) { \
171 PyErr_SetString(PyExc_ValueError, "mmap closed or invalid"); \
178 mmap_read_byte_method(mmap_object
*self
,
182 if (self
->pos
< self
->size
) {
183 char value
= self
->data
[self
->pos
];
185 return Py_BuildValue("c", value
);
187 PyErr_SetString(PyExc_ValueError
, "read byte out of range");
193 mmap_read_line_method(mmap_object
*self
,
196 char *start
= self
->data
+self
->pos
;
197 char *eof
= self
->data
+self
->size
;
203 eol
= memchr(start
, '\n', self
->size
- self
->pos
);
207 ++eol
; /* we're interested in the position after the
209 result
= PyString_FromStringAndSize(start
, (eol
- start
));
210 self
->pos
+= (eol
- start
);
215 mmap_read_method(mmap_object
*self
,
218 Py_ssize_t num_bytes
;
222 if (!PyArg_ParseTuple(args
, "n:read", &num_bytes
))
225 /* silently 'adjust' out-of-range requests */
226 if ((self
->pos
+ num_bytes
) > self
->size
) {
227 num_bytes
-= (self
->pos
+num_bytes
) - self
->size
;
229 result
= Py_BuildValue("s#", self
->data
+self
->pos
, num_bytes
);
230 self
->pos
+= num_bytes
;
235 mmap_find_method(mmap_object
*self
,
238 Py_ssize_t start
= self
->pos
;
243 if (!PyArg_ParseTuple(args
, "s#|n:find", &needle
, &len
, &start
)) {
247 char *e
= self
->data
+ self
->size
;
253 else if ((size_t)start
> self
->size
)
256 for (p
= self
->data
+ start
; p
+ len
<= e
; ++p
) {
258 for (i
= 0; i
< len
&& needle
[i
] == p
[i
]; ++i
)
261 return PyInt_FromSsize_t(p
- self
->data
);
264 return PyInt_FromLong(-1);
269 is_writeable(mmap_object
*self
)
271 if (self
->access
!= ACCESS_READ
)
273 PyErr_Format(PyExc_TypeError
, "mmap can't modify a readonly memory map.");
278 is_resizeable(mmap_object
*self
)
280 if ((self
->access
== ACCESS_WRITE
) || (self
->access
== ACCESS_DEFAULT
))
282 PyErr_Format(PyExc_TypeError
,
283 "mmap can't resize a readonly or copy-on-write memory map.");
289 mmap_write_method(mmap_object
*self
,
296 if (!PyArg_ParseTuple(args
, "s#:write", &data
, &length
))
299 if (!is_writeable(self
))
302 if ((self
->pos
+ length
) > self
->size
) {
303 PyErr_SetString(PyExc_ValueError
, "data out of range");
306 memcpy(self
->data
+self
->pos
, data
, length
);
307 self
->pos
= self
->pos
+length
;
313 mmap_write_byte_method(mmap_object
*self
,
319 if (!PyArg_ParseTuple(args
, "c:write_byte", &value
))
322 if (!is_writeable(self
))
324 *(self
->data
+self
->pos
) = value
;
331 mmap_size_method(mmap_object
*self
,
337 if (self
->file_handle
!= INVALID_HANDLE_VALUE
) {
340 low
= GetFileSize(self
->file_handle
, &high
);
341 if (low
== INVALID_FILE_SIZE
) {
342 /* It might be that the function appears to have failed,
343 when indeed its size equals INVALID_FILE_SIZE */
344 DWORD error
= GetLastError();
345 if (error
!= NO_ERROR
)
346 return PyErr_SetFromWindowsErr(error
);
348 if (!high
&& low
< LONG_MAX
)
349 return PyInt_FromLong((long)low
);
350 size
= (((PY_LONG_LONG
)high
)<<32) + low
;
351 return PyLong_FromLongLong(size
);
353 return PyInt_FromSsize_t(self
->size
);
355 #endif /* MS_WINDOWS */
360 if (-1 == fstat(self
->fd
, &buf
)) {
361 PyErr_SetFromErrno(mmap_module_error
);
364 return PyInt_FromSsize_t(buf
.st_size
);
369 /* This assumes that you want the entire file mapped,
370 / and when recreating the map will make the new file
373 / Is this really necessary? This could easily be done
374 / from python by just closing and re-opening with the
379 mmap_resize_method(mmap_object
*self
,
384 if (!PyArg_ParseTuple(args
, "n:resize", &new_size
) ||
385 !is_resizeable(self
)) {
390 DWORD newSizeLow
, newSizeHigh
;
391 /* First, unmap the file view */
392 UnmapViewOfFile(self
->data
);
393 /* Close the mapping object */
394 CloseHandle(self
->map_handle
);
395 /* Move to the desired EOF position */
396 #if SIZEOF_SIZE_T > 4
397 newSizeHigh
= (DWORD
)(new_size
>> 32);
398 newSizeLow
= (DWORD
)(new_size
& 0xFFFFFFFF);
401 newSizeLow
= (DWORD
)new_size
;
403 SetFilePointer(self
->file_handle
,
404 newSizeLow
, &newSizeHigh
, FILE_BEGIN
);
405 /* Change the size of the file */
406 SetEndOfFile(self
->file_handle
);
407 /* Create another mapping object and remap the file view */
408 self
->map_handle
= CreateFileMapping(
415 if (self
->map_handle
!= NULL
) {
416 self
->data
= (char *) MapViewOfFile(self
->map_handle
,
421 if (self
->data
!= NULL
) {
422 self
->size
= new_size
;
426 dwErrCode
= GetLastError();
429 dwErrCode
= GetLastError();
431 PyErr_SetFromWindowsErr(dwErrCode
);
433 #endif /* MS_WINDOWS */
438 PyErr_SetString(PyExc_SystemError
,
439 "mmap: resizing not available--no mremap()");
445 if (ftruncate(self
->fd
, new_size
) == -1) {
446 PyErr_SetFromErrno(mmap_module_error
);
450 #ifdef MREMAP_MAYMOVE
451 newmap
= mremap(self
->data
, self
->size
, new_size
, MREMAP_MAYMOVE
);
453 newmap
= mremap(self
->data
, self
->size
, new_size
, 0);
455 if (newmap
== (void *)-1)
457 PyErr_SetFromErrno(mmap_module_error
);
461 self
->size
= new_size
;
464 #endif /* HAVE_MREMAP */
470 mmap_tell_method(mmap_object
*self
, PyObject
*unused
)
473 return PyInt_FromSize_t(self
->pos
);
477 mmap_flush_method(mmap_object
*self
, PyObject
*args
)
479 Py_ssize_t offset
= 0;
480 Py_ssize_t size
= self
->size
;
482 if (!PyArg_ParseTuple(args
, "|nn:flush", &offset
, &size
))
484 if ((size_t)(offset
+ size
) > self
->size
) {
485 PyErr_SetString(PyExc_ValueError
, "flush values out of range");
489 return PyInt_FromLong((long)
490 FlushViewOfFile(self
->data
+offset
, size
));
491 #endif /* MS_WINDOWS */
493 /* XXX semantics of return value? */
494 /* XXX flags for msync? */
495 if (-1 == msync(self
->data
+ offset
, size
,
498 PyErr_SetFromErrno(mmap_module_error
);
501 return PyInt_FromLong(0);
507 mmap_seek_method(mmap_object
*self
, PyObject
*args
)
512 if (!PyArg_ParseTuple(args
, "n|i:seek", &dist
, &how
))
517 case 0: /* relative to start */
522 case 1: /* relative to current position */
523 if ((Py_ssize_t
)self
->pos
+ dist
< 0)
525 where
= self
->pos
+ dist
;
527 case 2: /* relative to end */
528 if ((Py_ssize_t
)self
->size
+ dist
< 0)
530 where
= self
->size
+ dist
;
533 PyErr_SetString(PyExc_ValueError
, "unknown seek type");
536 if (where
> self
->size
)
544 PyErr_SetString(PyExc_ValueError
, "seek out of range");
549 mmap_move_method(mmap_object
*self
, PyObject
*args
)
551 unsigned long dest
, src
, count
;
553 if (!PyArg_ParseTuple(args
, "kkk:move", &dest
, &src
, &count
) ||
554 !is_writeable(self
)) {
557 /* bounds check the values */
558 if (/* end of source after end of data?? */
559 ((src
+count
) > self
->size
)
561 || (dest
+count
> self
->size
)) {
562 PyErr_SetString(PyExc_ValueError
,
563 "source or destination out of range");
566 memmove(self
->data
+dest
, self
->data
+src
, count
);
573 static struct PyMethodDef mmap_object_methods
[] = {
574 {"close", (PyCFunction
) mmap_close_method
, METH_NOARGS
},
575 {"find", (PyCFunction
) mmap_find_method
, METH_VARARGS
},
576 {"flush", (PyCFunction
) mmap_flush_method
, METH_VARARGS
},
577 {"move", (PyCFunction
) mmap_move_method
, METH_VARARGS
},
578 {"read", (PyCFunction
) mmap_read_method
, METH_VARARGS
},
579 {"read_byte", (PyCFunction
) mmap_read_byte_method
, METH_NOARGS
},
580 {"readline", (PyCFunction
) mmap_read_line_method
, METH_NOARGS
},
581 {"resize", (PyCFunction
) mmap_resize_method
, METH_VARARGS
},
582 {"seek", (PyCFunction
) mmap_seek_method
, METH_VARARGS
},
583 {"size", (PyCFunction
) mmap_size_method
, METH_NOARGS
},
584 {"tell", (PyCFunction
) mmap_tell_method
, METH_NOARGS
},
585 {"write", (PyCFunction
) mmap_write_method
, METH_VARARGS
},
586 {"write_byte", (PyCFunction
) mmap_write_byte_method
, METH_VARARGS
},
587 {NULL
, NULL
} /* sentinel */
590 /* Functions for treating an mmap'ed file as a buffer */
593 mmap_buffer_getreadbuf(mmap_object
*self
, Py_ssize_t index
, const void **ptr
)
597 PyErr_SetString(PyExc_SystemError
,
598 "Accessing non-existent mmap segment");
606 mmap_buffer_getwritebuf(mmap_object
*self
, Py_ssize_t index
, const void **ptr
)
610 PyErr_SetString(PyExc_SystemError
,
611 "Accessing non-existent mmap segment");
614 if (!is_writeable(self
))
621 mmap_buffer_getsegcount(mmap_object
*self
, Py_ssize_t
*lenp
)
630 mmap_buffer_getcharbuffer(mmap_object
*self
, Py_ssize_t index
, const void **ptr
)
633 PyErr_SetString(PyExc_SystemError
,
634 "accessing non-existent buffer segment");
637 *ptr
= (const char *)self
->data
;
642 mmap_object_getattr(mmap_object
*self
, char *name
)
644 return Py_FindMethod(mmap_object_methods
, (PyObject
*)self
, name
);
648 mmap_length(mmap_object
*self
)
655 mmap_item(mmap_object
*self
, Py_ssize_t i
)
658 if (i
< 0 || (size_t)i
>= self
->size
) {
659 PyErr_SetString(PyExc_IndexError
, "mmap index out of range");
662 return PyString_FromStringAndSize(self
->data
+ i
, 1);
666 mmap_slice(mmap_object
*self
, Py_ssize_t ilow
, Py_ssize_t ihigh
)
671 else if ((size_t)ilow
> self
->size
)
677 else if ((size_t)ihigh
> self
->size
)
680 return PyString_FromStringAndSize(self
->data
+ ilow
, ihigh
-ilow
);
684 mmap_concat(mmap_object
*self
, PyObject
*bb
)
687 PyErr_SetString(PyExc_SystemError
,
688 "mmaps don't support concatenation");
693 mmap_repeat(mmap_object
*self
, Py_ssize_t n
)
696 PyErr_SetString(PyExc_SystemError
,
697 "mmaps don't support repeat operation");
702 mmap_ass_slice(mmap_object
*self
, Py_ssize_t ilow
, Py_ssize_t ihigh
, PyObject
*v
)
709 else if ((size_t)ilow
> self
->size
)
715 else if ((size_t)ihigh
> self
->size
)
719 PyErr_SetString(PyExc_TypeError
,
720 "mmap object doesn't support slice deletion");
723 if (! (PyString_Check(v
)) ) {
724 PyErr_SetString(PyExc_IndexError
,
725 "mmap slice assignment must be a string");
728 if (PyString_Size(v
) != (ihigh
- ilow
)) {
729 PyErr_SetString(PyExc_IndexError
,
730 "mmap slice assignment is wrong size");
733 if (!is_writeable(self
))
735 buf
= PyString_AsString(v
);
736 memcpy(self
->data
+ ilow
, buf
, ihigh
-ilow
);
741 mmap_ass_item(mmap_object
*self
, Py_ssize_t i
, PyObject
*v
)
746 if (i
< 0 || (size_t)i
>= self
->size
) {
747 PyErr_SetString(PyExc_IndexError
, "mmap index out of range");
751 PyErr_SetString(PyExc_TypeError
,
752 "mmap object doesn't support item deletion");
755 if (! (PyString_Check(v
) && PyString_Size(v
)==1) ) {
756 PyErr_SetString(PyExc_IndexError
,
757 "mmap assignment must be single-character string");
760 if (!is_writeable(self
))
762 buf
= PyString_AsString(v
);
763 self
->data
[i
] = buf
[0];
767 static PySequenceMethods mmap_as_sequence
= {
768 (lenfunc
)mmap_length
, /*sq_length*/
769 (binaryfunc
)mmap_concat
, /*sq_concat*/
770 (ssizeargfunc
)mmap_repeat
, /*sq_repeat*/
771 (ssizeargfunc
)mmap_item
, /*sq_item*/
772 (ssizessizeargfunc
)mmap_slice
, /*sq_slice*/
773 (ssizeobjargproc
)mmap_ass_item
, /*sq_ass_item*/
774 (ssizessizeobjargproc
)mmap_ass_slice
, /*sq_ass_slice*/
777 static PyBufferProcs mmap_as_buffer
= {
778 (readbufferproc
)mmap_buffer_getreadbuf
,
779 (writebufferproc
)mmap_buffer_getwritebuf
,
780 (segcountproc
)mmap_buffer_getsegcount
,
781 (charbufferproc
)mmap_buffer_getcharbuffer
,
784 static PyTypeObject mmap_object_type
= {
785 PyObject_HEAD_INIT(0) /* patched in module init */
787 "mmap.mmap", /* tp_name */
788 sizeof(mmap_object
), /* tp_size */
791 (destructor
) mmap_object_dealloc
, /* tp_dealloc */
793 (getattrfunc
) mmap_object_getattr
, /* tp_getattr */
797 0, /* tp_as_number */
798 &mmap_as_sequence
, /*tp_as_sequence*/
805 &mmap_as_buffer
, /*tp_as_buffer*/
806 Py_TPFLAGS_HAVE_GETCHARBUFFER
, /*tp_flags*/
811 /* extract the map size from the given PyObject
813 Returns -1 on error, with an appropriate Python exception raised. On
814 success, the map size is returned. */
816 _GetMapSize(PyObject
*o
)
818 if (PyIndex_Check(o
)) {
819 Py_ssize_t i
= PyNumber_AsSsize_t(o
, PyExc_OverflowError
);
820 if (i
==-1 && PyErr_Occurred())
823 PyErr_SetString(PyExc_OverflowError
,
824 "memory mapped size must be positive");
830 PyErr_SetString(PyExc_TypeError
, "map size must be an integral value");
836 new_mmap_object(PyObject
*self
, PyObject
*args
, PyObject
*kwdict
)
842 PyObject
*map_size_obj
= NULL
;
844 int fd
, flags
= MAP_SHARED
, prot
= PROT_WRITE
| PROT_READ
;
846 int access
= (int)ACCESS_DEFAULT
;
847 static char *keywords
[] = {"fileno", "length",
851 if (!PyArg_ParseTupleAndKeywords(args
, kwdict
, "iO|iii", keywords
,
852 &fd
, &map_size_obj
, &flags
, &prot
,
855 map_size
= _GetMapSize(map_size_obj
);
859 if ((access
!= (int)ACCESS_DEFAULT
) &&
860 ((flags
!= MAP_SHARED
) || (prot
!= (PROT_WRITE
| PROT_READ
))))
861 return PyErr_Format(PyExc_ValueError
,
862 "mmap can't specify both access and flags, prot.");
863 switch ((access_mode
)access
) {
870 prot
= PROT_READ
| PROT_WRITE
;
874 prot
= PROT_READ
| PROT_WRITE
;
877 /* use the specified or default values of flags and prot */
880 return PyErr_Format(PyExc_ValueError
,
881 "mmap invalid access parameter.");
886 /* on OpenVMS we must ensure that all bytes are written to the file */
889 if (fstat(fd
, &st
) == 0 && S_ISREG(st
.st_mode
)) {
891 map_size
= st
.st_size
;
892 } else if ((size_t)map_size
> st
.st_size
) {
893 PyErr_SetString(PyExc_ValueError
,
894 "mmap length is greater than file size");
899 m_obj
= PyObject_New(mmap_object
, &mmap_object_type
);
900 if (m_obj
== NULL
) {return NULL
;}
902 m_obj
->size
= (size_t) map_size
;
903 m_obj
->pos
= (size_t) 0;
906 /* Assume the caller wants to map anonymous memory.
907 This is the same behaviour as Windows. mmap.mmap(-1, size)
908 on both Windows and Unix map anonymous memory.
911 /* BSD way to map anonymous memory */
912 flags
|= MAP_ANONYMOUS
;
914 /* SVR4 method to map anonymous memory is to open /dev/zero */
915 fd
= devzero
= open("/dev/zero", O_RDWR
);
918 PyErr_SetFromErrno(mmap_module_error
);
924 if (m_obj
->fd
== -1) {
926 PyErr_SetFromErrno(mmap_module_error
);
931 m_obj
->data
= mmap(NULL
, map_size
,
939 if (m_obj
->data
== (char *)-1) {
942 PyErr_SetFromErrno(mmap_module_error
);
945 m_obj
->access
= (access_mode
)access
;
946 return (PyObject
*)m_obj
;
952 new_mmap_object(PyObject
*self
, PyObject
*args
, PyObject
*kwdict
)
955 PyObject
*map_size_obj
= NULL
;
957 DWORD size_hi
; /* upper 32 bits of m_obj->size */
958 DWORD size_lo
; /* lower 32 bits of m_obj->size */
963 int access
= (access_mode
)ACCESS_DEFAULT
;
964 DWORD flProtect
, dwDesiredAccess
;
965 static char *keywords
[] = { "fileno", "length",
969 if (!PyArg_ParseTupleAndKeywords(args
, kwdict
, "iO|zi", keywords
,
970 &fileno
, &map_size_obj
,
971 &tagname
, &access
)) {
975 switch((access_mode
)access
) {
977 flProtect
= PAGE_READONLY
;
978 dwDesiredAccess
= FILE_MAP_READ
;
980 case ACCESS_DEFAULT
: case ACCESS_WRITE
:
981 flProtect
= PAGE_READWRITE
;
982 dwDesiredAccess
= FILE_MAP_WRITE
;
985 flProtect
= PAGE_WRITECOPY
;
986 dwDesiredAccess
= FILE_MAP_COPY
;
989 return PyErr_Format(PyExc_ValueError
,
990 "mmap invalid access parameter.");
993 map_size
= _GetMapSize(map_size_obj
);
997 /* assume -1 and 0 both mean invalid filedescriptor
998 to 'anonymously' map memory.
999 XXX: fileno == 0 is a valid fd, but was accepted prior to 2.5.
1000 XXX: Should this code be added?
1002 PyErr_Warn(PyExc_DeprecationWarning,
1003 "don't use 0 for anonymous memory");
1005 if (fileno
!= -1 && fileno
!= 0) {
1006 fh
= (HANDLE
)_get_osfhandle(fileno
);
1007 if (fh
==(HANDLE
)-1) {
1008 PyErr_SetFromErrno(mmap_module_error
);
1011 /* Win9x appears to need us seeked to zero */
1012 lseek(fileno
, 0, SEEK_SET
);
1015 m_obj
= PyObject_New(mmap_object
, &mmap_object_type
);
1018 /* Set every field to an invalid marker, so we can safely
1019 destruct the object in the face of failure */
1021 m_obj
->file_handle
= INVALID_HANDLE_VALUE
;
1022 m_obj
->map_handle
= INVALID_HANDLE_VALUE
;
1023 m_obj
->tagname
= NULL
;
1026 /* It is necessary to duplicate the handle, so the
1027 Python code can close it on us */
1028 if (!DuplicateHandle(
1029 GetCurrentProcess(), /* source process handle */
1030 fh
, /* handle to be duplicated */
1031 GetCurrentProcess(), /* target proc handle */
1032 (LPHANDLE
)&m_obj
->file_handle
, /* result */
1033 0, /* access - ignored due to options value */
1034 FALSE
, /* inherited by child processes? */
1035 DUPLICATE_SAME_ACCESS
)) { /* options */
1036 dwErr
= GetLastError();
1038 PyErr_SetFromWindowsErr(dwErr
);
1043 low
= GetFileSize(fh
, &high
);
1044 /* low might just happen to have the value INVALID_FILE_SIZE;
1045 so we need to check the last error also. */
1046 if (low
== INVALID_FILE_SIZE
&&
1047 (dwErr
= GetLastError()) != NO_ERROR
) {
1049 return PyErr_SetFromWindowsErr(dwErr
);
1052 #if SIZEOF_SIZE_T > 4
1053 m_obj
->size
= (((size_t)high
)<<32) + low
;
1056 /* File is too large to map completely */
1057 m_obj
->size
= (size_t)-1;
1062 m_obj
->size
= map_size
;
1066 m_obj
->size
= map_size
;
1069 /* set the initial position */
1070 m_obj
->pos
= (size_t) 0;
1072 /* set the tag name */
1073 if (tagname
!= NULL
&& *tagname
!= '\0') {
1074 m_obj
->tagname
= PyMem_Malloc(strlen(tagname
)+1);
1075 if (m_obj
->tagname
== NULL
) {
1080 strcpy(m_obj
->tagname
, tagname
);
1083 m_obj
->tagname
= NULL
;
1085 m_obj
->access
= (access_mode
)access
;
1086 /* DWORD is a 4-byte int. If we're on a box where size_t consumes
1087 * more than 4 bytes, we need to break it apart. Else (size_t
1088 * consumes 4 bytes), C doesn't define what happens if we shift
1089 * right by 32, so we need different code.
1091 #if SIZEOF_SIZE_T > 4
1092 size_hi
= (DWORD
)(m_obj
->size
>> 32);
1093 size_lo
= (DWORD
)(m_obj
->size
& 0xFFFFFFFF);
1096 size_lo
= (DWORD
)m_obj
->size
;
1098 m_obj
->map_handle
= CreateFileMapping(m_obj
->file_handle
,
1104 if (m_obj
->map_handle
!= NULL
) {
1105 m_obj
->data
= (char *) MapViewOfFile(m_obj
->map_handle
,
1110 if (m_obj
->data
!= NULL
)
1111 return (PyObject
*)m_obj
;
1113 dwErr
= GetLastError();
1115 dwErr
= GetLastError();
1117 PyErr_SetFromWindowsErr(dwErr
);
1120 #endif /* MS_WINDOWS */
1122 /* List of functions exported by this module */
1123 static struct PyMethodDef mmap_functions
[] = {
1124 {"mmap", (PyCFunction
) new_mmap_object
,
1125 METH_VARARGS
|METH_KEYWORDS
},
1126 {NULL
, NULL
} /* Sentinel */
1130 setint(PyObject
*d
, const char *name
, long value
)
1132 PyObject
*o
= PyInt_FromLong(value
);
1133 if (o
&& PyDict_SetItemString(d
, name
, o
) == 0) {
1141 PyObject
*dict
, *module
;
1143 /* Patch the object type */
1144 mmap_object_type
.ob_type
= &PyType_Type
;
1146 module
= Py_InitModule("mmap", mmap_functions
);
1149 dict
= PyModule_GetDict(module
);
1152 mmap_module_error
= PyExc_EnvironmentError
;
1153 PyDict_SetItemString(dict
, "error", mmap_module_error
);
1155 setint(dict
, "PROT_EXEC", PROT_EXEC
);
1158 setint(dict
, "PROT_READ", PROT_READ
);
1161 setint(dict
, "PROT_WRITE", PROT_WRITE
);
1165 setint(dict
, "MAP_SHARED", MAP_SHARED
);
1168 setint(dict
, "MAP_PRIVATE", MAP_PRIVATE
);
1170 #ifdef MAP_DENYWRITE
1171 setint(dict
, "MAP_DENYWRITE", MAP_DENYWRITE
);
1173 #ifdef MAP_EXECUTABLE
1174 setint(dict
, "MAP_EXECUTABLE", MAP_EXECUTABLE
);
1176 #ifdef MAP_ANONYMOUS
1177 setint(dict
, "MAP_ANON", MAP_ANONYMOUS
);
1178 setint(dict
, "MAP_ANONYMOUS", MAP_ANONYMOUS
);
1181 setint(dict
, "PAGESIZE", (long)my_getpagesize());
1183 setint(dict
, "ACCESS_READ", ACCESS_READ
);
1184 setint(dict
, "ACCESS_WRITE", ACCESS_WRITE
);
1185 setint(dict
, "ACCESS_COPY", ACCESS_COPY
);