blob: 77aa64309dbd04a325190e2dd5f869057c8bf4d3 [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{
Tim Peters5ebfd362001-11-13 23:11:19 +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
42 SYSTEM_INFO si;
43 GetSystemInfo(&si);
44 return si.dwAllocationGranularity;
45}
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{
Tim Peters5ebfd362001-11-13 23:11:19 +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{
82 ACCESS_DEFAULT,
83 ACCESS_READ,
84 ACCESS_WRITE,
85 ACCESS_COPY
86} access_mode;
87
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +000088typedef struct {
Guido van Rossum09fdf072000-03-31 01:17:07 +000089 PyObject_HEAD
90 char * data;
91 size_t size;
Guido van Rossum8ce8a782007-11-01 19:42:39 +000092 size_t pos; /* relative to offset */
93 size_t offset;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +000094 int exports;
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +000095
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000096#ifdef MS_WINDOWS
Guido van Rossum09fdf072000-03-31 01:17:07 +000097 HANDLE map_handle;
Mark Hammond071864a2000-07-30 02:46:26 +000098 HANDLE file_handle;
Guido van Rossum09fdf072000-03-31 01:17:07 +000099 char * tagname;
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000100#endif
101
102#ifdef UNIX
Andrew M. Kuchling7b9fb922000-06-17 22:41:22 +0000103 int fd;
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000104#endif
Tim Peters5ebfd362001-11-13 23:11:19 +0000105
106 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
Mark Hammond2cbed002000-07-30 02:22:43 +0000114 if (m_obj->data != NULL)
115 UnmapViewOfFile (m_obj->data);
116 if (m_obj->map_handle != INVALID_HANDLE_VALUE)
117 CloseHandle (m_obj->map_handle);
Mark Hammond071864a2000-07-30 02:46:26 +0000118 if (m_obj->file_handle != INVALID_HANDLE_VALUE)
119 CloseHandle (m_obj->file_handle);
Mark Hammond2cbed002000-07-30 02:22:43 +0000120 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
Neal Norwitz6eac2002005-11-02 05:36:37 +0000125 if (m_obj->fd >= 0)
126 (void) close(m_obj->fd);
Guido van Rossum09fdf072000-03-31 01:17:07 +0000127 if (m_obj->data!=NULL) {
Andrew M. Kuchling9bc5f332000-06-18 04:25:08 +0000128 msync(m_obj->data, m_obj->size, MS_SYNC);
Guido van Rossum09fdf072000-03-31 01:17:07 +0000129 munmap(m_obj->data, m_obj->size);
130 }
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000131#endif /* UNIX */
132
Guido van Rossumb18618d2000-05-03 23:44:39 +0000133 PyObject_Del(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{
Travis E. Oliphantb99f7622007-08-18 11:21:56 +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
Mark Hammond071864a2000-07-30 02:46:26 +0000145 /* For each resource we maintain, we need to check
Tim Petersec0a5f02006-02-16 23:47:20 +0000146 the value is valid, and if so, free the resource
Mark Hammond071864a2000-07-30 02:46:26 +0000147 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) {
Tim Peters8f9cc292006-02-17 00:00:20 +0000153 UnmapViewOfFile(self->data);
Mark Hammond071864a2000-07-30 02:46:26 +0000154 self->data = NULL;
155 }
156 if (self->map_handle != INVALID_HANDLE_VALUE) {
Tim Peters8f9cc292006-02-17 00:00:20 +0000157 CloseHandle(self->map_handle);
Mark Hammond071864a2000-07-30 02:46:26 +0000158 self->map_handle = INVALID_HANDLE_VALUE;
159 }
160 if (self->file_handle != INVALID_HANDLE_VALUE) {
Tim Peters8f9cc292006-02-17 00:00:20 +0000161 CloseHandle(self->file_handle);
Mark Hammond071864a2000-07-30 02:46:26 +0000162 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
Neal Norwitz6eac2002005-11-02 05:36:37 +0000167 (void) close(self->fd);
168 self->fd = -1;
Neal Norwitze604c022003-01-10 20:52:16 +0000169 if (self->data != NULL) {
170 munmap(self->data, self->size);
171 self->data = NULL;
172 }
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000173#endif
174
Tim Peters8f9cc292006-02-17 00:00:20 +0000175 Py_INCREF(Py_None);
Tim Peters23721ee2006-02-16 23:50:16 +0000176 return Py_None;
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000177}
178
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000179#ifdef MS_WINDOWS
Guido van Rossum09fdf072000-03-31 01:17:07 +0000180#define CHECK_VALID(err) \
181do { \
Guido van Rossum69c2b882003-04-09 19:31:02 +0000182 if (self->map_handle == INVALID_HANDLE_VALUE) { \
Tim Peters8f9cc292006-02-17 00:00:20 +0000183 PyErr_SetString(PyExc_ValueError, "mmap closed or invalid"); \
Guido van Rossum09fdf072000-03-31 01:17:07 +0000184 return err; \
185 } \
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000186} while (0)
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000187#endif /* MS_WINDOWS */
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000188
189#ifdef UNIX
Guido van Rossum09fdf072000-03-31 01:17:07 +0000190#define CHECK_VALID(err) \
191do { \
192 if (self->data == NULL) { \
Tim Peters8f9cc292006-02-17 00:00:20 +0000193 PyErr_SetString(PyExc_ValueError, "mmap closed or invalid"); \
Guido van Rossum09fdf072000-03-31 01:17:07 +0000194 return err; \
195 } \
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000196} while (0)
197#endif /* UNIX */
198
199static PyObject *
Fredrik Lundh54cf3dc2000-07-08 22:05:01 +0000200mmap_read_byte_method(mmap_object *self,
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000201 PyObject *unused)
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000202{
Guido van Rossum09fdf072000-03-31 01:17:07 +0000203 CHECK_VALID(NULL);
Andrew M. Kuchling70d27422000-06-18 04:45:14 +0000204 if (self->pos < self->size) {
Tim Petersd6283b82001-05-09 18:48:26 +0000205 char value = self->data[self->pos];
Guido van Rossum09fdf072000-03-31 01:17:07 +0000206 self->pos += 1;
Tim Petersd6283b82001-05-09 18:48:26 +0000207 return Py_BuildValue("c", value);
Guido van Rossum09fdf072000-03-31 01:17:07 +0000208 } else {
Tim Peters8f9cc292006-02-17 00:00:20 +0000209 PyErr_SetString(PyExc_ValueError, "read byte out of range");
Guido van Rossum09fdf072000-03-31 01:17:07 +0000210 return NULL;
211 }
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000212}
213
214static PyObject *
Fredrik Lundh54cf3dc2000-07-08 22:05:01 +0000215mmap_read_line_method(mmap_object *self,
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000216 PyObject *unused)
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000217{
Fredrik Lundh54cf3dc2000-07-08 22:05:01 +0000218 char *start = self->data+self->pos;
219 char *eof = self->data+self->size;
220 char *eol;
221 PyObject *result;
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000222
Guido van Rossum09fdf072000-03-31 01:17:07 +0000223 CHECK_VALID(NULL);
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000224
Fred Drake56a87a02000-04-04 18:17:35 +0000225 eol = memchr(start, '\n', self->size - self->pos);
226 if (!eol)
227 eol = eof;
228 else
229 ++eol; /* we're interested in the position after the
230 newline. */
Guido van Rossumb358a2c2007-07-16 19:29:02 +0000231 result = PyBytes_FromStringAndSize(start, (eol - start));
Guido van Rossum09fdf072000-03-31 01:17:07 +0000232 self->pos += (eol - start);
Tim Peters23721ee2006-02-16 23:50:16 +0000233 return result;
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000234}
235
236static PyObject *
Fredrik Lundh54cf3dc2000-07-08 22:05:01 +0000237mmap_read_method(mmap_object *self,
238 PyObject *args)
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000239{
Martin v. Löwisad0a4622006-02-16 14:30:23 +0000240 Py_ssize_t num_bytes;
Guido van Rossum09fdf072000-03-31 01:17:07 +0000241 PyObject *result;
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000242
Guido van Rossum09fdf072000-03-31 01:17:07 +0000243 CHECK_VALID(NULL);
Martin v. Löwisad0a4622006-02-16 14:30:23 +0000244 if (!PyArg_ParseTuple(args, "n:read", &num_bytes))
Guido van Rossum09fdf072000-03-31 01:17:07 +0000245 return(NULL);
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000246
Guido van Rossum09fdf072000-03-31 01:17:07 +0000247 /* silently 'adjust' out-of-range requests */
248 if ((self->pos + num_bytes) > self->size) {
249 num_bytes -= (self->pos+num_bytes) - self->size;
250 }
Guido van Rossume5aeaad2007-07-26 18:28:23 +0000251 result = PyBytes_FromStringAndSize(self->data+self->pos, num_bytes);
Tim Petersec0a5f02006-02-16 23:47:20 +0000252 self->pos += num_bytes;
Tim Peters23721ee2006-02-16 23:50:16 +0000253 return result;
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000254}
255
256static PyObject *
Fredrik Lundh54cf3dc2000-07-08 22:05:01 +0000257mmap_find_method(mmap_object *self,
258 PyObject *args)
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000259{
Martin v. Löwisad0a4622006-02-16 14:30:23 +0000260 Py_ssize_t start = self->pos;
Fredrik Lundh54cf3dc2000-07-08 22:05:01 +0000261 char *needle;
Martin v. Löwiscfe7e092006-02-17 06:59:14 +0000262 Py_ssize_t len;
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000263
Guido van Rossum09fdf072000-03-31 01:17:07 +0000264 CHECK_VALID(NULL);
Tim Peters8f9cc292006-02-17 00:00:20 +0000265 if (!PyArg_ParseTuple(args, "s#|n:find", &needle, &len, &start)) {
Guido van Rossum09fdf072000-03-31 01:17:07 +0000266 return NULL;
267 } else {
Greg Stein834f4dd2001-05-14 09:32:26 +0000268 char *p;
269 char *e = self->data + self->size;
270
271 if (start < 0)
Tim Peters5ebfd362001-11-13 23:11:19 +0000272 start += self->size;
Greg Stein834f4dd2001-05-14 09:32:26 +0000273 if (start < 0)
Tim Peters5ebfd362001-11-13 23:11:19 +0000274 start = 0;
Tim Petersd401edd2001-05-14 23:19:12 +0000275 else if ((size_t)start > self->size)
Tim Peters5ebfd362001-11-13 23:11:19 +0000276 start = self->size;
Greg Stein834f4dd2001-05-14 09:32:26 +0000277
Tim Petersc9ffa062002-03-08 05:43:32 +0000278 for (p = self->data + start; p + len <= e; ++p) {
Martin v. Löwiscfe7e092006-02-17 06:59:14 +0000279 Py_ssize_t i;
Tim Petersc9ffa062002-03-08 05:43:32 +0000280 for (i = 0; i < len && needle[i] == p[i]; ++i)
281 /* nothing */;
282 if (i == len) {
Christian Heimes217cfd12007-12-02 14:31:20 +0000283 return PyLong_FromSsize_t(p - self->data);
Guido van Rossum09fdf072000-03-31 01:17:07 +0000284 }
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000285 }
Christian Heimes217cfd12007-12-02 14:31:20 +0000286 return PyLong_FromLong(-1);
Guido van Rossum09fdf072000-03-31 01:17:07 +0000287 }
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000288}
289
Tim Petersec0a5f02006-02-16 23:47:20 +0000290static int
Sean Reifscheider54cf12b2007-09-17 17:55:36 +0000291is_writable(mmap_object *self)
Tim Peters5ebfd362001-11-13 23:11:19 +0000292{
293 if (self->access != ACCESS_READ)
Tim Petersec0a5f02006-02-16 23:47:20 +0000294 return 1;
Tim Peters5ebfd362001-11-13 23:11:19 +0000295 PyErr_Format(PyExc_TypeError, "mmap can't modify a readonly memory map.");
296 return 0;
297}
298
Tim Petersec0a5f02006-02-16 23:47:20 +0000299static int
Tim Peters5ebfd362001-11-13 23:11:19 +0000300is_resizeable(mmap_object *self)
301{
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000302 if (self->exports > 0) {
303 PyErr_SetString(PyExc_BufferError,
304 "mmap can't resize with extant buffers exported.");
305 return 0;
306 }
Tim Peters5ebfd362001-11-13 23:11:19 +0000307 if ((self->access == ACCESS_WRITE) || (self->access == ACCESS_DEFAULT))
Guido van Rossum98297ee2007-11-06 21:34:58 +0000308 return 1;
Tim Petersec0a5f02006-02-16 23:47:20 +0000309 PyErr_Format(PyExc_TypeError,
Tim Peters5ebfd362001-11-13 23:11:19 +0000310 "mmap can't resize a readonly or copy-on-write memory map.");
311 return 0;
312}
313
314
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000315static PyObject *
Fredrik Lundh54cf3dc2000-07-08 22:05:01 +0000316mmap_write_method(mmap_object *self,
317 PyObject *args)
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000318{
Martin v. Löwiscfe7e092006-02-17 06:59:14 +0000319 Py_ssize_t length;
Fredrik Lundh54cf3dc2000-07-08 22:05:01 +0000320 char *data;
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000321
Guido van Rossum09fdf072000-03-31 01:17:07 +0000322 CHECK_VALID(NULL);
Tim Peters8f9cc292006-02-17 00:00:20 +0000323 if (!PyArg_ParseTuple(args, "s#:write", &data, &length))
Guido van Rossum09fdf072000-03-31 01:17:07 +0000324 return(NULL);
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000325
Sean Reifscheider54cf12b2007-09-17 17:55:36 +0000326 if (!is_writable(self))
Tim Peters5ebfd362001-11-13 23:11:19 +0000327 return NULL;
328
Guido van Rossum09fdf072000-03-31 01:17:07 +0000329 if ((self->pos + length) > self->size) {
Tim Peters8f9cc292006-02-17 00:00:20 +0000330 PyErr_SetString(PyExc_ValueError, "data out of range");
Guido van Rossum09fdf072000-03-31 01:17:07 +0000331 return NULL;
332 }
Tim Peters8f9cc292006-02-17 00:00:20 +0000333 memcpy(self->data+self->pos, data, length);
Guido van Rossum09fdf072000-03-31 01:17:07 +0000334 self->pos = self->pos+length;
Tim Peters8f9cc292006-02-17 00:00:20 +0000335 Py_INCREF(Py_None);
Tim Peters23721ee2006-02-16 23:50:16 +0000336 return Py_None;
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000337}
338
339static PyObject *
Fredrik Lundh54cf3dc2000-07-08 22:05:01 +0000340mmap_write_byte_method(mmap_object *self,
341 PyObject *args)
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000342{
Guido van Rossum09fdf072000-03-31 01:17:07 +0000343 char value;
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000344
Guido van Rossum09fdf072000-03-31 01:17:07 +0000345 CHECK_VALID(NULL);
Tim Peters8f9cc292006-02-17 00:00:20 +0000346 if (!PyArg_ParseTuple(args, "c:write_byte", &value))
Guido van Rossum09fdf072000-03-31 01:17:07 +0000347 return(NULL);
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000348
Sean Reifscheider54cf12b2007-09-17 17:55:36 +0000349 if (!is_writable(self))
Tim Peters5ebfd362001-11-13 23:11:19 +0000350 return NULL;
Guido van Rossum09fdf072000-03-31 01:17:07 +0000351 *(self->data+self->pos) = value;
352 self->pos += 1;
Tim Peters8f9cc292006-02-17 00:00:20 +0000353 Py_INCREF(Py_None);
Tim Peters23721ee2006-02-16 23:50:16 +0000354 return Py_None;
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000355}
Tim Petersec0a5f02006-02-16 23:47:20 +0000356
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000357static PyObject *
Fredrik Lundh54cf3dc2000-07-08 22:05:01 +0000358mmap_size_method(mmap_object *self,
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000359 PyObject *unused)
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000360{
Guido van Rossum09fdf072000-03-31 01:17:07 +0000361 CHECK_VALID(NULL);
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000362
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000363#ifdef MS_WINDOWS
Mark Hammond071864a2000-07-30 02:46:26 +0000364 if (self->file_handle != INVALID_HANDLE_VALUE) {
Martin v. Löwis15186072006-02-18 12:38:35 +0000365 DWORD low,high;
366 PY_LONG_LONG size;
367 low = GetFileSize(self->file_handle, &high);
368 if (low == INVALID_FILE_SIZE) {
369 /* It might be that the function appears to have failed,
370 when indeed its size equals INVALID_FILE_SIZE */
371 DWORD error = GetLastError();
372 if (error != NO_ERROR)
373 return PyErr_SetFromWindowsErr(error);
374 }
375 if (!high && low < LONG_MAX)
Christian Heimes217cfd12007-12-02 14:31:20 +0000376 return PyLong_FromLong((long)low);
Martin v. Löwis15186072006-02-18 12:38:35 +0000377 size = (((PY_LONG_LONG)high)<<32) + low;
378 return PyLong_FromLongLong(size);
Guido van Rossum09fdf072000-03-31 01:17:07 +0000379 } else {
Christian Heimes217cfd12007-12-02 14:31:20 +0000380 return PyLong_FromSsize_t(self->size);
Guido van Rossum09fdf072000-03-31 01:17:07 +0000381 }
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000382#endif /* MS_WINDOWS */
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000383
384#ifdef UNIX
Andrew M. Kuchling7b9fb922000-06-17 22:41:22 +0000385 {
386 struct stat buf;
387 if (-1 == fstat(self->fd, &buf)) {
388 PyErr_SetFromErrno(mmap_module_error);
389 return NULL;
390 }
Christian Heimes217cfd12007-12-02 14:31:20 +0000391 return PyLong_FromSsize_t(buf.st_size);
Andrew M. Kuchling7b9fb922000-06-17 22:41:22 +0000392 }
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000393#endif /* UNIX */
394}
395
396/* This assumes that you want the entire file mapped,
397 / and when recreating the map will make the new file
398 / have the new size
399 /
400 / Is this really necessary? This could easily be done
401 / from python by just closing and re-opening with the
402 / new size?
403 */
404
405static PyObject *
Fredrik Lundh54cf3dc2000-07-08 22:05:01 +0000406mmap_resize_method(mmap_object *self,
407 PyObject *args)
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000408{
Martin v. Löwiscfe7e092006-02-17 06:59:14 +0000409 Py_ssize_t new_size;
Guido van Rossum09fdf072000-03-31 01:17:07 +0000410 CHECK_VALID(NULL);
Martin v. Löwiscfe7e092006-02-17 06:59:14 +0000411 if (!PyArg_ParseTuple(args, "n:resize", &new_size) ||
Tim Peters5ebfd362001-11-13 23:11:19 +0000412 !is_resizeable(self)) {
Guido van Rossum09fdf072000-03-31 01:17:07 +0000413 return NULL;
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000414#ifdef MS_WINDOWS
Tim Petersec0a5f02006-02-16 23:47:20 +0000415 } else {
Guido van Rossum09fdf072000-03-31 01:17:07 +0000416 DWORD dwErrCode = 0;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000417 DWORD off_hi, off_lo, newSizeLow, newSizeHigh;
Guido van Rossum09fdf072000-03-31 01:17:07 +0000418 /* First, unmap the file view */
Tim Peters8f9cc292006-02-17 00:00:20 +0000419 UnmapViewOfFile(self->data);
Guido van Rossum09fdf072000-03-31 01:17:07 +0000420 /* Close the mapping object */
Tim Peters8f9cc292006-02-17 00:00:20 +0000421 CloseHandle(self->map_handle);
Guido van Rossum09fdf072000-03-31 01:17:07 +0000422 /* Move to the desired EOF position */
Martin v. Löwis15186072006-02-18 12:38:35 +0000423#if SIZEOF_SIZE_T > 4
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000424 newSizeHigh = (DWORD)((self->offset + new_size) >> 32);
425 newSizeLow = (DWORD)((self->offset + new_size) & 0xFFFFFFFF);
426 off_hi = (DWORD)(self->offset >> 32);
427 off_lo = (DWORD)(self->offset & 0xFFFFFFFF);
Martin v. Löwis15186072006-02-18 12:38:35 +0000428#else
429 newSizeHigh = 0;
Martin v. Löwis5bb8a152006-02-18 12:49:49 +0000430 newSizeLow = (DWORD)new_size;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000431 off_hi = 0;
432 off_lo = (DWORD)self->offset;
Martin v. Löwis15186072006-02-18 12:38:35 +0000433#endif
Tim Peters8f9cc292006-02-17 00:00:20 +0000434 SetFilePointer(self->file_handle,
Martin v. Löwis15186072006-02-18 12:38:35 +0000435 newSizeLow, &newSizeHigh, FILE_BEGIN);
Guido van Rossum09fdf072000-03-31 01:17:07 +0000436 /* Change the size of the file */
Tim Peters8f9cc292006-02-17 00:00:20 +0000437 SetEndOfFile(self->file_handle);
Guido van Rossum09fdf072000-03-31 01:17:07 +0000438 /* Create another mapping object and remap the file view */
Tim Peters8f9cc292006-02-17 00:00:20 +0000439 self->map_handle = CreateFileMapping(
Mark Hammond071864a2000-07-30 02:46:26 +0000440 self->file_handle,
Guido van Rossum09fdf072000-03-31 01:17:07 +0000441 NULL,
442 PAGE_READWRITE,
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000443 0,
444 0,
Guido van Rossum09fdf072000-03-31 01:17:07 +0000445 self->tagname);
446 if (self->map_handle != NULL) {
Tim Peters8f9cc292006-02-17 00:00:20 +0000447 self->data = (char *) MapViewOfFile(self->map_handle,
448 FILE_MAP_WRITE,
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000449 off_hi,
450 off_lo,
451 new_size);
Guido van Rossum09fdf072000-03-31 01:17:07 +0000452 if (self->data != NULL) {
453 self->size = new_size;
Tim Peters8f9cc292006-02-17 00:00:20 +0000454 Py_INCREF(Py_None);
Guido van Rossum09fdf072000-03-31 01:17:07 +0000455 return Py_None;
456 } else {
457 dwErrCode = GetLastError();
458 }
459 } else {
460 dwErrCode = GetLastError();
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000461 }
Guido van Rossum09fdf072000-03-31 01:17:07 +0000462 PyErr_SetFromWindowsErr(dwErrCode);
Tim Peters23721ee2006-02-16 23:50:16 +0000463 return NULL;
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000464#endif /* MS_WINDOWS */
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000465
466#ifdef UNIX
Tim Petersec0a5f02006-02-16 23:47:20 +0000467#ifndef HAVE_MREMAP
Tim Peters5ebfd362001-11-13 23:11:19 +0000468 } else {
469 PyErr_SetString(PyExc_SystemError,
470 "mmap: resizing not available--no mremap()");
471 return NULL;
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000472#else
Tim Peters5ebfd362001-11-13 23:11:19 +0000473 } else {
Armin Rigo335ffe82005-09-20 19:04:02 +0000474 void *newmap;
475
Georg Brandl38387b82005-08-24 07:17:40 +0000476 if (ftruncate(self->fd, new_size) == -1) {
477 PyErr_SetFromErrno(mmap_module_error);
478 return NULL;
479 }
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000480
Andrew M. Kuchling6fef30e2000-06-18 14:51:21 +0000481#ifdef MREMAP_MAYMOVE
Tim Peters5ebfd362001-11-13 23:11:19 +0000482 newmap = mremap(self->data, self->size, new_size, MREMAP_MAYMOVE);
Andrew M. Kuchling6fef30e2000-06-18 14:51:21 +0000483#else
Tim Peters5ebfd362001-11-13 23:11:19 +0000484 newmap = mremap(self->data, self->size, new_size, 0);
Andrew M. Kuchling6fef30e2000-06-18 14:51:21 +0000485#endif
Tim Petersec0a5f02006-02-16 23:47:20 +0000486 if (newmap == (void *)-1)
Tim Peters5ebfd362001-11-13 23:11:19 +0000487 {
488 PyErr_SetFromErrno(mmap_module_error);
489 return NULL;
490 }
491 self->data = newmap;
492 self->size = new_size;
493 Py_INCREF(Py_None);
494 return Py_None;
Andrew M. Kuchling6fef30e2000-06-18 14:51:21 +0000495#endif /* HAVE_MREMAP */
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000496#endif /* UNIX */
Tim Peters5ebfd362001-11-13 23:11:19 +0000497 }
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000498}
499
500static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000501mmap_tell_method(mmap_object *self, PyObject *unused)
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000502{
Guido van Rossum09fdf072000-03-31 01:17:07 +0000503 CHECK_VALID(NULL);
Christian Heimes217cfd12007-12-02 14:31:20 +0000504 return PyLong_FromSize_t(self->pos);
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000505}
506
507static PyObject *
Fredrik Lundh54cf3dc2000-07-08 22:05:01 +0000508mmap_flush_method(mmap_object *self, PyObject *args)
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000509{
Martin v. Löwisad0a4622006-02-16 14:30:23 +0000510 Py_ssize_t offset = 0;
511 Py_ssize_t size = self->size;
Guido van Rossum09fdf072000-03-31 01:17:07 +0000512 CHECK_VALID(NULL);
Tim Petersf2882952006-02-17 01:07:39 +0000513 if (!PyArg_ParseTuple(args, "|nn:flush", &offset, &size))
Guido van Rossum09fdf072000-03-31 01:17:07 +0000514 return NULL;
Tim Petersf2882952006-02-17 01:07:39 +0000515 if ((size_t)(offset + size) > self->size) {
Tim Peters8f9cc292006-02-17 00:00:20 +0000516 PyErr_SetString(PyExc_ValueError, "flush values out of range");
Guido van Rossum09fdf072000-03-31 01:17:07 +0000517 return NULL;
518 } else {
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000519#ifdef MS_WINDOWS
Christian Heimes217cfd12007-12-02 14:31:20 +0000520 return PyLong_FromLong((long)
Tim Peters23721ee2006-02-16 23:50:16 +0000521 FlushViewOfFile(self->data+offset, size));
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000522#endif /* MS_WINDOWS */
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000523#ifdef UNIX
Guido van Rossum09fdf072000-03-31 01:17:07 +0000524 /* XXX semantics of return value? */
525 /* XXX flags for msync? */
526 if (-1 == msync(self->data + offset, size,
Andrew M. Kuchling9bc5f332000-06-18 04:25:08 +0000527 MS_SYNC))
Guido van Rossum09fdf072000-03-31 01:17:07 +0000528 {
529 PyErr_SetFromErrno(mmap_module_error);
530 return NULL;
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000531 }
Christian Heimes217cfd12007-12-02 14:31:20 +0000532 return PyLong_FromLong(0);
Tim Petersec0a5f02006-02-16 23:47:20 +0000533#endif /* UNIX */
Guido van Rossum09fdf072000-03-31 01:17:07 +0000534 }
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000535}
536
537static PyObject *
Fredrik Lundh54cf3dc2000-07-08 22:05:01 +0000538mmap_seek_method(mmap_object *self, PyObject *args)
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000539{
Martin v. Löwiscfe7e092006-02-17 06:59:14 +0000540 Py_ssize_t dist;
Guido van Rossum09fdf072000-03-31 01:17:07 +0000541 int how=0;
542 CHECK_VALID(NULL);
Martin v. Löwiscfe7e092006-02-17 06:59:14 +0000543 if (!PyArg_ParseTuple(args, "n|i:seek", &dist, &how))
Tim Peters8f9cc292006-02-17 00:00:20 +0000544 return NULL;
545 else {
Andrew M. Kuchling70d27422000-06-18 04:45:14 +0000546 size_t where;
Guido van Rossum09fdf072000-03-31 01:17:07 +0000547 switch (how) {
Andrew M. Kuchling70d27422000-06-18 04:45:14 +0000548 case 0: /* relative to start */
549 if (dist < 0)
550 goto onoutofrange;
Guido van Rossum09fdf072000-03-31 01:17:07 +0000551 where = dist;
552 break;
Andrew M. Kuchling70d27422000-06-18 04:45:14 +0000553 case 1: /* relative to current position */
Martin v. Löwiscfe7e092006-02-17 06:59:14 +0000554 if ((Py_ssize_t)self->pos + dist < 0)
Andrew M. Kuchling70d27422000-06-18 04:45:14 +0000555 goto onoutofrange;
Guido van Rossum09fdf072000-03-31 01:17:07 +0000556 where = self->pos + dist;
557 break;
Andrew M. Kuchling70d27422000-06-18 04:45:14 +0000558 case 2: /* relative to end */
Martin v. Löwiscfe7e092006-02-17 06:59:14 +0000559 if ((Py_ssize_t)self->size + dist < 0)
Andrew M. Kuchling70d27422000-06-18 04:45:14 +0000560 goto onoutofrange;
561 where = self->size + dist;
Guido van Rossum09fdf072000-03-31 01:17:07 +0000562 break;
563 default:
Tim Peters8f9cc292006-02-17 00:00:20 +0000564 PyErr_SetString(PyExc_ValueError, "unknown seek type");
Guido van Rossum09fdf072000-03-31 01:17:07 +0000565 return NULL;
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000566 }
Andrew M. Kuchling70d27422000-06-18 04:45:14 +0000567 if (where > self->size)
568 goto onoutofrange;
569 self->pos = where;
Tim Peters8f9cc292006-02-17 00:00:20 +0000570 Py_INCREF(Py_None);
Tim Peters23721ee2006-02-16 23:50:16 +0000571 return Py_None;
Guido van Rossum09fdf072000-03-31 01:17:07 +0000572 }
Andrew M. Kuchling70d27422000-06-18 04:45:14 +0000573
Tim Peters5ebfd362001-11-13 23:11:19 +0000574 onoutofrange:
Tim Peters8f9cc292006-02-17 00:00:20 +0000575 PyErr_SetString(PyExc_ValueError, "seek out of range");
Andrew M. Kuchling70d27422000-06-18 04:45:14 +0000576 return NULL;
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000577}
578
579static PyObject *
Fredrik Lundh54cf3dc2000-07-08 22:05:01 +0000580mmap_move_method(mmap_object *self, PyObject *args)
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000581{
Guido van Rossum09fdf072000-03-31 01:17:07 +0000582 unsigned long dest, src, count;
583 CHECK_VALID(NULL);
Tim Peters8f9cc292006-02-17 00:00:20 +0000584 if (!PyArg_ParseTuple(args, "kkk:move", &dest, &src, &count) ||
Sean Reifscheider54cf12b2007-09-17 17:55:36 +0000585 !is_writable(self)) {
Guido van Rossum09fdf072000-03-31 01:17:07 +0000586 return NULL;
587 } else {
588 /* bounds check the values */
589 if (/* end of source after end of data?? */
590 ((src+count) > self->size)
591 /* dest will fit? */
592 || (dest+count > self->size)) {
Tim Peters8f9cc292006-02-17 00:00:20 +0000593 PyErr_SetString(PyExc_ValueError,
594 "source or destination out of range");
Guido van Rossum09fdf072000-03-31 01:17:07 +0000595 return NULL;
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000596 } else {
Tim Peters8f9cc292006-02-17 00:00:20 +0000597 memmove(self->data+dest, self->data+src, count);
598 Py_INCREF(Py_None);
Guido van Rossum09fdf072000-03-31 01:17:07 +0000599 return Py_None;
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000600 }
Guido van Rossum09fdf072000-03-31 01:17:07 +0000601 }
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000602}
603
604static struct PyMethodDef mmap_object_methods[] = {
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000605 {"close", (PyCFunction) mmap_close_method, METH_NOARGS},
Martin v. Löwis43b936d2002-01-17 23:15:58 +0000606 {"find", (PyCFunction) mmap_find_method, METH_VARARGS},
607 {"flush", (PyCFunction) mmap_flush_method, METH_VARARGS},
608 {"move", (PyCFunction) mmap_move_method, METH_VARARGS},
609 {"read", (PyCFunction) mmap_read_method, METH_VARARGS},
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000610 {"read_byte", (PyCFunction) mmap_read_byte_method, METH_NOARGS},
611 {"readline", (PyCFunction) mmap_read_line_method, METH_NOARGS},
Martin v. Löwis43b936d2002-01-17 23:15:58 +0000612 {"resize", (PyCFunction) mmap_resize_method, METH_VARARGS},
613 {"seek", (PyCFunction) mmap_seek_method, METH_VARARGS},
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000614 {"size", (PyCFunction) mmap_size_method, METH_NOARGS},
615 {"tell", (PyCFunction) mmap_tell_method, METH_NOARGS},
Martin v. Löwis43b936d2002-01-17 23:15:58 +0000616 {"write", (PyCFunction) mmap_write_method, METH_VARARGS},
617 {"write_byte", (PyCFunction) mmap_write_byte_method, METH_VARARGS},
Guido van Rossum09fdf072000-03-31 01:17:07 +0000618 {NULL, NULL} /* sentinel */
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000619};
620
621/* Functions for treating an mmap'ed file as a buffer */
622
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000623static int
Guido van Rossum98297ee2007-11-06 21:34:58 +0000624mmap_buffer_getbuf(mmap_object *self, Py_buffer *view, int flags)
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000625{
Guido van Rossum09fdf072000-03-31 01:17:07 +0000626 CHECK_VALID(-1);
Guido van Rossum98297ee2007-11-06 21:34:58 +0000627 if (PyBuffer_FillInfo(view, self->data, self->size,
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000628 (self->access == ACCESS_READ), flags) < 0)
629 return -1;
630 self->exports++;
631 return 0;
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000632}
633
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000634static void
Travis E. Oliphant8ae62b62007-09-23 02:00:13 +0000635mmap_buffer_releasebuf(mmap_object *self, Py_buffer *view)
Tim Petersec0a5f02006-02-16 23:47:20 +0000636{
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000637 self->exports--;
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000638}
639
640static PyObject *
Fredrik Lundh54cf3dc2000-07-08 22:05:01 +0000641mmap_object_getattr(mmap_object *self, char *name)
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000642{
Tim Peters8f9cc292006-02-17 00:00:20 +0000643 return Py_FindMethod(mmap_object_methods, (PyObject *)self, name);
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000644}
645
Martin v. Löwis18e16552006-02-15 17:27:45 +0000646static Py_ssize_t
Fredrik Lundh54cf3dc2000-07-08 22:05:01 +0000647mmap_length(mmap_object *self)
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000648{
Guido van Rossum09fdf072000-03-31 01:17:07 +0000649 CHECK_VALID(-1);
650 return self->size;
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000651}
652
653static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000654mmap_item(mmap_object *self, Py_ssize_t i)
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000655{
Guido van Rossum09fdf072000-03-31 01:17:07 +0000656 CHECK_VALID(NULL);
Guido van Rossumce8e1dc2000-07-01 00:51:51 +0000657 if (i < 0 || (size_t)i >= self->size) {
Guido van Rossum09fdf072000-03-31 01:17:07 +0000658 PyErr_SetString(PyExc_IndexError, "mmap index out of range");
659 return NULL;
660 }
Guido van Rossumb358a2c2007-07-16 19:29:02 +0000661 return PyBytes_FromStringAndSize(self->data + i, 1);
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000662}
663
664static PyObject *
Thomas Woutersed03b412007-08-28 21:37:11 +0000665mmap_subscript(mmap_object *self, PyObject *item)
666{
667 CHECK_VALID(NULL);
668 if (PyIndex_Check(item)) {
669 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
670 if (i == -1 && PyErr_Occurred())
671 return NULL;
672 if (i < 0)
673 i += self->size;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000674 if (i < 0 || (size_t)i > self->size) {
Thomas Woutersed03b412007-08-28 21:37:11 +0000675 PyErr_SetString(PyExc_IndexError,
676 "mmap index out of range");
677 return NULL;
678 }
Christian Heimes217cfd12007-12-02 14:31:20 +0000679 return PyLong_FromLong(Py_CHARMASK(self->data[i]));
Thomas Woutersed03b412007-08-28 21:37:11 +0000680 }
681 else if (PySlice_Check(item)) {
682 Py_ssize_t start, stop, step, slicelen;
683
684 if (PySlice_GetIndicesEx((PySliceObject *)item, self->size,
685 &start, &stop, &step, &slicelen) < 0) {
686 return NULL;
687 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000688
Thomas Woutersed03b412007-08-28 21:37:11 +0000689 if (slicelen <= 0)
Guido van Rossum98297ee2007-11-06 21:34:58 +0000690 return PyString_FromStringAndSize("", 0);
Thomas Woutersed03b412007-08-28 21:37:11 +0000691 else if (step == 1)
Guido van Rossum98297ee2007-11-06 21:34:58 +0000692 return PyString_FromStringAndSize(self->data + start,
693 slicelen);
Thomas Woutersed03b412007-08-28 21:37:11 +0000694 else {
695 char *result_buf = (char *)PyMem_Malloc(slicelen);
696 Py_ssize_t cur, i;
697 PyObject *result;
698
699 if (result_buf == NULL)
700 return PyErr_NoMemory();
701 for (cur = start, i = 0; i < slicelen;
702 cur += step, i++) {
703 result_buf[i] = self->data[cur];
704 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000705 result = PyString_FromStringAndSize(result_buf,
706 slicelen);
Thomas Woutersed03b412007-08-28 21:37:11 +0000707 PyMem_Free(result_buf);
708 return result;
709 }
710 }
711 else {
712 PyErr_SetString(PyExc_TypeError,
713 "mmap indices must be integers");
714 return NULL;
715 }
716}
717
718static PyObject *
Fredrik Lundh54cf3dc2000-07-08 22:05:01 +0000719mmap_concat(mmap_object *self, PyObject *bb)
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000720{
Guido van Rossum09fdf072000-03-31 01:17:07 +0000721 CHECK_VALID(NULL);
722 PyErr_SetString(PyExc_SystemError,
723 "mmaps don't support concatenation");
724 return NULL;
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000725}
726
727static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000728mmap_repeat(mmap_object *self, Py_ssize_t n)
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000729{
Guido van Rossum09fdf072000-03-31 01:17:07 +0000730 CHECK_VALID(NULL);
731 PyErr_SetString(PyExc_SystemError,
732 "mmaps don't support repeat operation");
733 return NULL;
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000734}
735
736static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000737mmap_ass_item(mmap_object *self, Py_ssize_t i, PyObject *v)
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000738{
Guido van Rossum36d4f8b2000-04-10 21:34:37 +0000739 const char *buf;
Tim Petersec0a5f02006-02-16 23:47:20 +0000740
Guido van Rossum09fdf072000-03-31 01:17:07 +0000741 CHECK_VALID(-1);
Guido van Rossumce8e1dc2000-07-01 00:51:51 +0000742 if (i < 0 || (size_t)i >= self->size) {
Guido van Rossum09fdf072000-03-31 01:17:07 +0000743 PyErr_SetString(PyExc_IndexError, "mmap index out of range");
744 return -1;
745 }
Thomas Wouters1baac722001-07-16 15:47:36 +0000746 if (v == NULL) {
747 PyErr_SetString(PyExc_TypeError,
Tim Peters5ebfd362001-11-13 23:11:19 +0000748 "mmap object doesn't support item deletion");
Thomas Wouters1baac722001-07-16 15:47:36 +0000749 return -1;
750 }
Guido van Rossumb358a2c2007-07-16 19:29:02 +0000751 if (! (PyBytes_Check(v) && PyBytes_Size(v)==1) ) {
Tim Petersec0a5f02006-02-16 23:47:20 +0000752 PyErr_SetString(PyExc_IndexError,
Thomas Woutersed03b412007-08-28 21:37:11 +0000753 "mmap assignment must be length-1 bytes()");
Guido van Rossum09fdf072000-03-31 01:17:07 +0000754 return -1;
755 }
Sean Reifscheider54cf12b2007-09-17 17:55:36 +0000756 if (!is_writable(self))
Tim Peters5ebfd362001-11-13 23:11:19 +0000757 return -1;
Guido van Rossumb358a2c2007-07-16 19:29:02 +0000758 buf = PyBytes_AsString(v);
Guido van Rossum09fdf072000-03-31 01:17:07 +0000759 self->data[i] = buf[0];
760 return 0;
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000761}
762
Thomas Woutersed03b412007-08-28 21:37:11 +0000763static int
764mmap_ass_subscript(mmap_object *self, PyObject *item, PyObject *value)
765{
766 CHECK_VALID(-1);
767
Guido van Rossum98297ee2007-11-06 21:34:58 +0000768 if (!is_writable(self))
769 return -1;
770
Thomas Woutersed03b412007-08-28 21:37:11 +0000771 if (PyIndex_Check(item)) {
772 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
Guido van Rossum98297ee2007-11-06 21:34:58 +0000773 Py_ssize_t v;
Thomas Woutersed03b412007-08-28 21:37:11 +0000774
775 if (i == -1 && PyErr_Occurred())
776 return -1;
777 if (i < 0)
778 i += self->size;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000779 if (i < 0 || (size_t)i > self->size) {
Thomas Woutersed03b412007-08-28 21:37:11 +0000780 PyErr_SetString(PyExc_IndexError,
Guido van Rossum98297ee2007-11-06 21:34:58 +0000781 "mmap index out of range");
Thomas Woutersed03b412007-08-28 21:37:11 +0000782 return -1;
783 }
784 if (value == NULL) {
785 PyErr_SetString(PyExc_TypeError,
Guido van Rossum98297ee2007-11-06 21:34:58 +0000786 "mmap doesn't support item deletion");
Thomas Woutersed03b412007-08-28 21:37:11 +0000787 return -1;
788 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000789 if (!PyIndex_Check(value)) {
790 PyErr_SetString(PyExc_TypeError,
791 "mmap item value must be an int");
Thomas Woutersed03b412007-08-28 21:37:11 +0000792 return -1;
793 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000794 v = PyNumber_AsSsize_t(value, PyExc_TypeError);
795 if (v == -1 && PyErr_Occurred())
Thomas Woutersed03b412007-08-28 21:37:11 +0000796 return -1;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000797 if (v < 0 || v > 255) {
798 PyErr_SetString(PyExc_ValueError,
799 "mmap item value must be "
800 "in range(0, 256)");
801 return -1;
802 }
803 self->data[i] = v;
Thomas Woutersed03b412007-08-28 21:37:11 +0000804 return 0;
805 }
806 else if (PySlice_Check(item)) {
807 Py_ssize_t start, stop, step, slicelen;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000808 Py_buffer vbuf;
809
Thomas Woutersed03b412007-08-28 21:37:11 +0000810 if (PySlice_GetIndicesEx((PySliceObject *)item,
811 self->size, &start, &stop,
812 &step, &slicelen) < 0) {
813 return -1;
814 }
815 if (value == NULL) {
816 PyErr_SetString(PyExc_TypeError,
817 "mmap object doesn't support slice deletion");
818 return -1;
819 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000820 if (PyObject_GetBuffer(value, &vbuf, PyBUF_SIMPLE) < 0)
Thomas Woutersed03b412007-08-28 21:37:11 +0000821 return -1;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000822 if (vbuf.len != slicelen) {
Thomas Woutersed03b412007-08-28 21:37:11 +0000823 PyErr_SetString(PyExc_IndexError,
824 "mmap slice assignment is wrong size");
Guido van Rossum98297ee2007-11-06 21:34:58 +0000825 PyObject_ReleaseBuffer(value, &vbuf);
Thomas Woutersed03b412007-08-28 21:37:11 +0000826 return -1;
827 }
Thomas Woutersed03b412007-08-28 21:37:11 +0000828
Guido van Rossum98297ee2007-11-06 21:34:58 +0000829 if (slicelen == 0) {
830 }
Thomas Woutersed03b412007-08-28 21:37:11 +0000831 else if (step == 1) {
Guido van Rossum98297ee2007-11-06 21:34:58 +0000832 memcpy(self->data + start, vbuf.buf, slicelen);
Thomas Woutersed03b412007-08-28 21:37:11 +0000833 }
834 else {
835 Py_ssize_t cur, i;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000836
837 for (cur = start, i = 0;
838 i < slicelen;
839 cur += step, i++)
840 {
841 self->data[cur] = ((char *)vbuf.buf)[i];
Thomas Woutersed03b412007-08-28 21:37:11 +0000842 }
Thomas Woutersed03b412007-08-28 21:37:11 +0000843 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000844 PyObject_ReleaseBuffer(value, &vbuf);
845 return 0;
Thomas Woutersed03b412007-08-28 21:37:11 +0000846 }
847 else {
848 PyErr_SetString(PyExc_TypeError,
849 "mmap indices must be integer");
850 return -1;
851 }
852}
853
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000854static PySequenceMethods mmap_as_sequence = {
Martin v. Löwis18e16552006-02-15 17:27:45 +0000855 (lenfunc)mmap_length, /*sq_length*/
Guido van Rossum09fdf072000-03-31 01:17:07 +0000856 (binaryfunc)mmap_concat, /*sq_concat*/
Martin v. Löwis18e16552006-02-15 17:27:45 +0000857 (ssizeargfunc)mmap_repeat, /*sq_repeat*/
858 (ssizeargfunc)mmap_item, /*sq_item*/
Thomas Woutersd2cf20e2007-08-30 22:57:53 +0000859 0, /*sq_slice*/
Martin v. Löwis18e16552006-02-15 17:27:45 +0000860 (ssizeobjargproc)mmap_ass_item, /*sq_ass_item*/
Thomas Woutersd2cf20e2007-08-30 22:57:53 +0000861 0, /*sq_ass_slice*/
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000862};
863
Thomas Woutersed03b412007-08-28 21:37:11 +0000864static PyMappingMethods mmap_as_mapping = {
865 (lenfunc)mmap_length,
866 (binaryfunc)mmap_subscript,
867 (objobjargproc)mmap_ass_subscript,
868};
869
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000870static PyBufferProcs mmap_as_buffer = {
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000871 (getbufferproc)mmap_buffer_getbuf,
872 (releasebufferproc)mmap_buffer_releasebuf,
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000873};
874
875static PyTypeObject mmap_object_type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000876 PyVarObject_HEAD_INIT(0, 0) /* patched in module init */
Guido van Rossum14648392001-12-08 18:02:58 +0000877 "mmap.mmap", /* tp_name */
Guido van Rossum09fdf072000-03-31 01:17:07 +0000878 sizeof(mmap_object), /* tp_size */
879 0, /* tp_itemsize */
880 /* methods */
881 (destructor) mmap_object_dealloc, /* tp_dealloc */
882 0, /* tp_print */
883 (getattrfunc) mmap_object_getattr, /* tp_getattr */
884 0, /* tp_setattr */
885 0, /* tp_compare */
886 0, /* tp_repr */
887 0, /* tp_as_number */
888 &mmap_as_sequence, /*tp_as_sequence*/
Thomas Woutersed03b412007-08-28 21:37:11 +0000889 &mmap_as_mapping, /*tp_as_mapping*/
Guido van Rossum09fdf072000-03-31 01:17:07 +0000890 0, /*tp_hash*/
891 0, /*tp_call*/
892 0, /*tp_str*/
893 0, /*tp_getattro*/
894 0, /*tp_setattro*/
895 &mmap_as_buffer, /*tp_as_buffer*/
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +0000896 Py_TPFLAGS_DEFAULT, /*tp_flags*/
Guido van Rossum09fdf072000-03-31 01:17:07 +0000897 0, /*tp_doc*/
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000898};
899
Andrew M. Kuchling70d27422000-06-18 04:45:14 +0000900
901/* extract the map size from the given PyObject
902
Thomas Wouters7e474022000-07-16 12:04:32 +0000903 Returns -1 on error, with an appropriate Python exception raised. On
Andrew M. Kuchling70d27422000-06-18 04:45:14 +0000904 success, the map size is returned. */
Martin v. Löwiscfe7e092006-02-17 06:59:14 +0000905static Py_ssize_t
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000906_GetMapSize(PyObject *o, const char* param)
Andrew M. Kuchling70d27422000-06-18 04:45:14 +0000907{
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000908 if (o == NULL)
909 return 0;
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000910 if (PyIndex_Check(o)) {
911 Py_ssize_t i = PyNumber_AsSsize_t(o, PyExc_OverflowError);
Guido van Rossum98297ee2007-11-06 21:34:58 +0000912 if (i==-1 && PyErr_Occurred())
Andrew M. Kuchling70d27422000-06-18 04:45:14 +0000913 return -1;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000914 if (i < 0) {
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000915 PyErr_Format(PyExc_OverflowError,
916 "memory mapped %s must be positive",
917 param);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000918 return -1;
919 }
Martin v. Löwiscfe7e092006-02-17 06:59:14 +0000920 return i;
Andrew M. Kuchling70d27422000-06-18 04:45:14 +0000921 }
Andrew M. Kuchling70d27422000-06-18 04:45:14 +0000922
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000923 PyErr_SetString(PyExc_TypeError, "map size must be an integral value");
Andrew M. Kuchling70d27422000-06-18 04:45:14 +0000924 return -1;
925}
926
Tim Petersec0a5f02006-02-16 23:47:20 +0000927#ifdef UNIX
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000928static PyObject *
Fredrik Lundh54cf3dc2000-07-08 22:05:01 +0000929new_mmap_object(PyObject *self, PyObject *args, PyObject *kwdict)
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000930{
Neal Norwitzb5673922002-09-05 21:48:07 +0000931#ifdef HAVE_FSTAT
932 struct stat st;
933#endif
Fredrik Lundh54cf3dc2000-07-08 22:05:01 +0000934 mmap_object *m_obj;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000935 PyObject *map_size_obj = NULL, *offset_obj = NULL;
936 Py_ssize_t map_size, offset;
Guido van Rossum09fdf072000-03-31 01:17:07 +0000937 int fd, flags = MAP_SHARED, prot = PROT_WRITE | PROT_READ;
Neal Norwitz0e6bc8c2006-02-05 05:45:43 +0000938 int devzero = -1;
Neal Norwitz8856fb72005-12-18 03:34:22 +0000939 int access = (int)ACCESS_DEFAULT;
Martin v. Löwis02cbf4a2006-02-27 17:20:04 +0000940 static char *keywords[] = {"fileno", "length",
Tim Petersec0a5f02006-02-16 23:47:20 +0000941 "flags", "prot",
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000942 "access", "offset", NULL};
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +0000943
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000944 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iO|iiiO", keywords,
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000945 &fd, &map_size_obj, &flags, &prot,
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000946 &access, &offset_obj))
Guido van Rossum09fdf072000-03-31 01:17:07 +0000947 return NULL;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000948 map_size = _GetMapSize(map_size_obj, "size");
Andrew M. Kuchling70d27422000-06-18 04:45:14 +0000949 if (map_size < 0)
950 return NULL;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000951 offset = _GetMapSize(offset_obj, "offset");
952 if (offset < 0)
953 return NULL;
Tim Peters5ebfd362001-11-13 23:11:19 +0000954
Tim Petersec0a5f02006-02-16 23:47:20 +0000955 if ((access != (int)ACCESS_DEFAULT) &&
Neal Norwitzd1cfc8a2006-02-05 03:36:57 +0000956 ((flags != MAP_SHARED) || (prot != (PROT_WRITE | PROT_READ))))
Tim Petersec0a5f02006-02-16 23:47:20 +0000957 return PyErr_Format(PyExc_ValueError,
Tim Peters5ebfd362001-11-13 23:11:19 +0000958 "mmap can't specify both access and flags, prot.");
Neal Norwitzd1cfc8a2006-02-05 03:36:57 +0000959 switch ((access_mode)access) {
Tim Peters5ebfd362001-11-13 23:11:19 +0000960 case ACCESS_READ:
961 flags = MAP_SHARED;
962 prot = PROT_READ;
963 break;
964 case ACCESS_WRITE:
965 flags = MAP_SHARED;
966 prot = PROT_READ | PROT_WRITE;
967 break;
968 case ACCESS_COPY:
969 flags = MAP_PRIVATE;
970 prot = PROT_READ | PROT_WRITE;
971 break;
Tim Petersec0a5f02006-02-16 23:47:20 +0000972 case ACCESS_DEFAULT:
Tim Peters5ebfd362001-11-13 23:11:19 +0000973 /* use the specified or default values of flags and prot */
974 break;
975 default:
Tim Petersec0a5f02006-02-16 23:47:20 +0000976 return PyErr_Format(PyExc_ValueError,
Tim Peters5ebfd362001-11-13 23:11:19 +0000977 "mmap invalid access parameter.");
978 }
Neal Norwitzb5673922002-09-05 21:48:07 +0000979
980#ifdef HAVE_FSTAT
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +0000981# ifdef __VMS
982 /* on OpenVMS we must ensure that all bytes are written to the file */
983 fsync(fd);
984# endif
Martin v. Löwis7fe60c02005-03-03 11:22:44 +0000985 if (fstat(fd, &st) == 0 && S_ISREG(st.st_mode)) {
986 if (map_size == 0) {
Martin v. Löwiscfe7e092006-02-17 06:59:14 +0000987 map_size = st.st_size;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000988 } else if ((size_t)offset + (size_t)map_size > st.st_size) {
Tim Petersec0a5f02006-02-16 23:47:20 +0000989 PyErr_SetString(PyExc_ValueError,
Martin v. Löwis7fe60c02005-03-03 11:22:44 +0000990 "mmap length is greater than file size");
991 return NULL;
992 }
Neal Norwitzb5673922002-09-05 21:48:07 +0000993 }
994#endif
Tim Peters8f9cc292006-02-17 00:00:20 +0000995 m_obj = PyObject_New(mmap_object, &mmap_object_type);
Guido van Rossum09fdf072000-03-31 01:17:07 +0000996 if (m_obj == NULL) {return NULL;}
Neal Norwitz3b4fff82006-01-11 08:54:45 +0000997 m_obj->data = NULL;
Guido van Rossum09fdf072000-03-31 01:17:07 +0000998 m_obj->size = (size_t) map_size;
999 m_obj->pos = (size_t) 0;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00001000 m_obj->exports = 0;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001001 m_obj->offset = offset;
Neal Norwitz0e6bc8c2006-02-05 05:45:43 +00001002 if (fd == -1) {
1003 m_obj->fd = -1;
1004 /* Assume the caller wants to map anonymous memory.
1005 This is the same behaviour as Windows. mmap.mmap(-1, size)
1006 on both Windows and Unix map anonymous memory.
1007 */
1008#ifdef MAP_ANONYMOUS
1009 /* BSD way to map anonymous memory */
1010 flags |= MAP_ANONYMOUS;
1011#else
1012 /* SVR4 method to map anonymous memory is to open /dev/zero */
1013 fd = devzero = open("/dev/zero", O_RDWR);
1014 if (devzero == -1) {
1015 Py_DECREF(m_obj);
1016 PyErr_SetFromErrno(mmap_module_error);
1017 return NULL;
1018 }
1019#endif
1020 } else {
1021 m_obj->fd = dup(fd);
1022 if (m_obj->fd == -1) {
1023 Py_DECREF(m_obj);
1024 PyErr_SetFromErrno(mmap_module_error);
1025 return NULL;
1026 }
Georg Brandl38387b82005-08-24 07:17:40 +00001027 }
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001028
Tim Petersec0a5f02006-02-16 23:47:20 +00001029 m_obj->data = mmap(NULL, map_size,
Guido van Rossum09fdf072000-03-31 01:17:07 +00001030 prot, flags,
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001031 fd, offset);
Neal Norwitz0e6bc8c2006-02-05 05:45:43 +00001032
1033 if (devzero != -1) {
1034 close(devzero);
1035 }
1036
Tim Peters5ebfd362001-11-13 23:11:19 +00001037 if (m_obj->data == (char *)-1) {
Andrew M. Kuchling16581c82004-05-19 14:39:08 +00001038 m_obj->data = NULL;
Guido van Rossum09fdf072000-03-31 01:17:07 +00001039 Py_DECREF(m_obj);
1040 PyErr_SetFromErrno(mmap_module_error);
1041 return NULL;
1042 }
Neal Norwitz8856fb72005-12-18 03:34:22 +00001043 m_obj->access = (access_mode)access;
Guido van Rossum09fdf072000-03-31 01:17:07 +00001044 return (PyObject *)m_obj;
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +00001045}
1046#endif /* UNIX */
1047
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001048#ifdef MS_WINDOWS
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +00001049static PyObject *
Tim Peters5ebfd362001-11-13 23:11:19 +00001050new_mmap_object(PyObject *self, PyObject *args, PyObject *kwdict)
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +00001051{
Fredrik Lundh54cf3dc2000-07-08 22:05:01 +00001052 mmap_object *m_obj;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001053 PyObject *map_size_obj = NULL, *offset_obj = NULL;
1054 Py_ssize_t map_size, offset;
1055 DWORD off_hi; /* upper 32 bits of offset */
1056 DWORD off_lo; /* lower 32 bits of offset */
1057 DWORD size_hi; /* upper 32 bits of size */
1058 DWORD size_lo; /* lower 32 bits of size */
Fredrik Lundh54cf3dc2000-07-08 22:05:01 +00001059 char *tagname = "";
Guido van Rossum09fdf072000-03-31 01:17:07 +00001060 DWORD dwErr = 0;
1061 int fileno;
Mark Hammond071864a2000-07-30 02:46:26 +00001062 HANDLE fh = 0;
Neal Norwitz8856fb72005-12-18 03:34:22 +00001063 int access = (access_mode)ACCESS_DEFAULT;
Tim Peters5ebfd362001-11-13 23:11:19 +00001064 DWORD flProtect, dwDesiredAccess;
Martin v. Löwis02cbf4a2006-02-27 17:20:04 +00001065 static char *keywords[] = { "fileno", "length",
Tim Petersec0a5f02006-02-16 23:47:20 +00001066 "tagname",
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001067 "access", "offset", NULL };
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +00001068
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001069 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iO|ziO", keywords,
Tim Petersec0a5f02006-02-16 23:47:20 +00001070 &fileno, &map_size_obj,
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001071 &tagname, &access, &offset_obj)) {
Guido van Rossum09fdf072000-03-31 01:17:07 +00001072 return NULL;
Tim Peters5ebfd362001-11-13 23:11:19 +00001073 }
1074
Neal Norwitz8856fb72005-12-18 03:34:22 +00001075 switch((access_mode)access) {
Tim Peters5ebfd362001-11-13 23:11:19 +00001076 case ACCESS_READ:
1077 flProtect = PAGE_READONLY;
1078 dwDesiredAccess = FILE_MAP_READ;
1079 break;
1080 case ACCESS_DEFAULT: case ACCESS_WRITE:
1081 flProtect = PAGE_READWRITE;
1082 dwDesiredAccess = FILE_MAP_WRITE;
1083 break;
1084 case ACCESS_COPY:
1085 flProtect = PAGE_WRITECOPY;
1086 dwDesiredAccess = FILE_MAP_COPY;
1087 break;
1088 default:
Tim Petersec0a5f02006-02-16 23:47:20 +00001089 return PyErr_Format(PyExc_ValueError,
Tim Peters5ebfd362001-11-13 23:11:19 +00001090 "mmap invalid access parameter.");
1091 }
1092
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001093 map_size = _GetMapSize(map_size_obj, "size");
Andrew M. Kuchling70d27422000-06-18 04:45:14 +00001094 if (map_size < 0)
1095 return NULL;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001096 offset = _GetMapSize(offset_obj, "offset");
1097 if (offset < 0)
1098 return NULL;
Tim Petersec0a5f02006-02-16 23:47:20 +00001099
Neal Norwitz0e6bc8c2006-02-05 05:45:43 +00001100 /* assume -1 and 0 both mean invalid filedescriptor
1101 to 'anonymously' map memory.
1102 XXX: fileno == 0 is a valid fd, but was accepted prior to 2.5.
1103 XXX: Should this code be added?
1104 if (fileno == 0)
Skip Montanaro46fc3372007-08-12 11:44:53 +00001105 PyErr_WarnEx(PyExc_DeprecationWarning,
1106 "don't use 0 for anonymous memory",
1107 1);
Neal Norwitz0e6bc8c2006-02-05 05:45:43 +00001108 */
1109 if (fileno != -1 && fileno != 0) {
Mark Hammond071864a2000-07-30 02:46:26 +00001110 fh = (HANDLE)_get_osfhandle(fileno);
1111 if (fh==(HANDLE)-1) {
Tim Peters5ebfd362001-11-13 23:11:19 +00001112 PyErr_SetFromErrno(mmap_module_error);
1113 return NULL;
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +00001114 }
Fred Drake1ef4e2d2000-04-05 14:15:31 +00001115 /* Win9x appears to need us seeked to zero */
Guido van Rossum69c2b882003-04-09 19:31:02 +00001116 lseek(fileno, 0, SEEK_SET);
Guido van Rossum09fdf072000-03-31 01:17:07 +00001117 }
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +00001118
Tim Peters8f9cc292006-02-17 00:00:20 +00001119 m_obj = PyObject_New(mmap_object, &mmap_object_type);
1120 if (m_obj == NULL)
Mark Hammond2cbed002000-07-30 02:22:43 +00001121 return NULL;
1122 /* Set every field to an invalid marker, so we can safely
1123 destruct the object in the face of failure */
1124 m_obj->data = NULL;
Mark Hammond071864a2000-07-30 02:46:26 +00001125 m_obj->file_handle = INVALID_HANDLE_VALUE;
Mark Hammond2cbed002000-07-30 02:22:43 +00001126 m_obj->map_handle = INVALID_HANDLE_VALUE;
1127 m_obj->tagname = NULL;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001128 m_obj->offset = offset;
Mark Hammond2cbed002000-07-30 02:22:43 +00001129
Guido van Rossum09fdf072000-03-31 01:17:07 +00001130 if (fh) {
Mark Hammond2cbed002000-07-30 02:22:43 +00001131 /* It is necessary to duplicate the handle, so the
1132 Python code can close it on us */
1133 if (!DuplicateHandle(
Tim Peters5ebfd362001-11-13 23:11:19 +00001134 GetCurrentProcess(), /* source process handle */
1135 fh, /* handle to be duplicated */
1136 GetCurrentProcess(), /* target proc handle */
1137 (LPHANDLE)&m_obj->file_handle, /* result */
1138 0, /* access - ignored due to options value */
1139 FALSE, /* inherited by child processes? */
1140 DUPLICATE_SAME_ACCESS)) { /* options */
Mark Hammond2cbed002000-07-30 02:22:43 +00001141 dwErr = GetLastError();
1142 Py_DECREF(m_obj);
1143 PyErr_SetFromWindowsErr(dwErr);
1144 return NULL;
1145 }
Guido van Rossum09fdf072000-03-31 01:17:07 +00001146 if (!map_size) {
Martin v. Löwis15186072006-02-18 12:38:35 +00001147 DWORD low,high;
1148 low = GetFileSize(fh, &high);
1149 /* low might just happen to have the value INVALID_FILE_SIZE;
1150 so we need to check the last error also. */
1151 if (low == INVALID_FILE_SIZE &&
1152 (dwErr = GetLastError()) != NO_ERROR) {
1153 Py_DECREF(m_obj);
1154 return PyErr_SetFromWindowsErr(dwErr);
Guido van Rossum98297ee2007-11-06 21:34:58 +00001155 }
1156
Martin v. Löwis15186072006-02-18 12:38:35 +00001157#if SIZEOF_SIZE_T > 4
1158 m_obj->size = (((size_t)high)<<32) + low;
1159#else
1160 if (high)
1161 /* File is too large to map completely */
1162 m_obj->size = (size_t)-1;
1163 else
1164 m_obj->size = low;
1165#endif
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +00001166 } else {
Guido van Rossum09fdf072000-03-31 01:17:07 +00001167 m_obj->size = map_size;
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +00001168 }
Guido van Rossum09fdf072000-03-31 01:17:07 +00001169 }
1170 else {
Guido van Rossum09fdf072000-03-31 01:17:07 +00001171 m_obj->size = map_size;
1172 }
1173
1174 /* set the initial position */
1175 m_obj->pos = (size_t) 0;
1176
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00001177 m_obj->exports = 0;
Mark Hammond2cbed002000-07-30 02:22:43 +00001178 /* set the tag name */
Tim Peters0d9f9dc2001-01-10 05:42:18 +00001179 if (tagname != NULL && *tagname != '\0') {
Mark Hammond2cbed002000-07-30 02:22:43 +00001180 m_obj->tagname = PyMem_Malloc(strlen(tagname)+1);
1181 if (m_obj->tagname == NULL) {
1182 PyErr_NoMemory();
1183 Py_DECREF(m_obj);
1184 return NULL;
1185 }
1186 strcpy(m_obj->tagname, tagname);
1187 }
1188 else
1189 m_obj->tagname = NULL;
1190
Neal Norwitz8856fb72005-12-18 03:34:22 +00001191 m_obj->access = (access_mode)access;
Tim Peterse564e7f2006-02-16 23:46:01 +00001192 /* DWORD is a 4-byte int. If we're on a box where size_t consumes
Thomas Wouters2c98a7b2006-02-17 09:59:00 +00001193 * more than 4 bytes, we need to break it apart. Else (size_t
Tim Peterse564e7f2006-02-16 23:46:01 +00001194 * consumes 4 bytes), C doesn't define what happens if we shift
1195 * right by 32, so we need different code.
1196 */
1197#if SIZEOF_SIZE_T > 4
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001198 size_hi = (DWORD)((offset + m_obj->size) >> 32);
1199 size_lo = (DWORD)((offset + m_obj->size) & 0xFFFFFFFF);
1200 off_hi = (DWORD)(offset >> 32);
1201 off_lo = (DWORD)(offset & 0xFFFFFFFF);
Tim Peterse564e7f2006-02-16 23:46:01 +00001202#else
1203 size_hi = 0;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001204 size_lo = (DWORD)(offset + m_obj->size);
1205 off_hi = 0;
1206 off_lo = (DWORD)offset;
Tim Peterse564e7f2006-02-16 23:46:01 +00001207#endif
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001208 /* For files, it would be sufficient to pass 0 as size.
1209 For anonymous maps, we have to pass the size explicitly. */
Tim Peters8f9cc292006-02-17 00:00:20 +00001210 m_obj->map_handle = CreateFileMapping(m_obj->file_handle,
1211 NULL,
1212 flProtect,
1213 size_hi,
1214 size_lo,
1215 m_obj->tagname);
Guido van Rossum09fdf072000-03-31 01:17:07 +00001216 if (m_obj->map_handle != NULL) {
Tim Peters8f9cc292006-02-17 00:00:20 +00001217 m_obj->data = (char *) MapViewOfFile(m_obj->map_handle,
1218 dwDesiredAccess,
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001219 off_hi,
1220 off_lo,
Tim Peters8f9cc292006-02-17 00:00:20 +00001221 0);
Tim Peters23721ee2006-02-16 23:50:16 +00001222 if (m_obj->data != NULL)
1223 return (PyObject *)m_obj;
1224 else
Tim Peters5ebfd362001-11-13 23:11:19 +00001225 dwErr = GetLastError();
Tim Peters23721ee2006-02-16 23:50:16 +00001226 } else
Guido van Rossum09fdf072000-03-31 01:17:07 +00001227 dwErr = GetLastError();
Mark Hammond2cbed002000-07-30 02:22:43 +00001228 Py_DECREF(m_obj);
Guido van Rossum09fdf072000-03-31 01:17:07 +00001229 PyErr_SetFromWindowsErr(dwErr);
Tim Peters23721ee2006-02-16 23:50:16 +00001230 return NULL;
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +00001231}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001232#endif /* MS_WINDOWS */
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +00001233
1234/* List of functions exported by this module */
1235static struct PyMethodDef mmap_functions[] = {
Tim Petersec0a5f02006-02-16 23:47:20 +00001236 {"mmap", (PyCFunction) new_mmap_object,
Guido van Rossum09fdf072000-03-31 01:17:07 +00001237 METH_VARARGS|METH_KEYWORDS},
1238 {NULL, NULL} /* Sentinel */
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +00001239};
1240
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001241static void
1242setint(PyObject *d, const char *name, long value)
1243{
Christian Heimes217cfd12007-12-02 14:31:20 +00001244 PyObject *o = PyLong_FromLong(value);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001245 if (o && PyDict_SetItemString(d, name, o) == 0) {
1246 Py_DECREF(o);
1247 }
1248}
1249
Mark Hammond62b1ab12002-07-23 06:31:15 +00001250PyMODINIT_FUNC
Tim Peters5ebfd362001-11-13 23:11:19 +00001251 initmmap(void)
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +00001252{
Guido van Rossum09fdf072000-03-31 01:17:07 +00001253 PyObject *dict, *module;
Tim Peters2caf8df2001-01-14 05:05:51 +00001254
1255 /* Patch the object type */
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001256 Py_Type(&mmap_object_type) = &PyType_Type;
Tim Peters2caf8df2001-01-14 05:05:51 +00001257
Tim Peters8f9cc292006-02-17 00:00:20 +00001258 module = Py_InitModule("mmap", mmap_functions);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00001259 if (module == NULL)
1260 return;
Tim Peters8f9cc292006-02-17 00:00:20 +00001261 dict = PyModule_GetDict(module);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001262 if (!dict)
1263 return;
Guido van Rossum09fdf072000-03-31 01:17:07 +00001264 mmap_module_error = PyExc_EnvironmentError;
Tim Peters8f9cc292006-02-17 00:00:20 +00001265 PyDict_SetItemString(dict, "error", mmap_module_error);
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +00001266#ifdef PROT_EXEC
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001267 setint(dict, "PROT_EXEC", PROT_EXEC);
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +00001268#endif
1269#ifdef PROT_READ
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001270 setint(dict, "PROT_READ", PROT_READ);
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +00001271#endif
1272#ifdef PROT_WRITE
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001273 setint(dict, "PROT_WRITE", PROT_WRITE);
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +00001274#endif
1275
1276#ifdef MAP_SHARED
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001277 setint(dict, "MAP_SHARED", MAP_SHARED);
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +00001278#endif
1279#ifdef MAP_PRIVATE
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001280 setint(dict, "MAP_PRIVATE", MAP_PRIVATE);
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +00001281#endif
1282#ifdef MAP_DENYWRITE
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001283 setint(dict, "MAP_DENYWRITE", MAP_DENYWRITE);
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +00001284#endif
1285#ifdef MAP_EXECUTABLE
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001286 setint(dict, "MAP_EXECUTABLE", MAP_EXECUTABLE);
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +00001287#endif
Neal Norwitz0e6bc8c2006-02-05 05:45:43 +00001288#ifdef MAP_ANONYMOUS
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001289 setint(dict, "MAP_ANON", MAP_ANONYMOUS);
1290 setint(dict, "MAP_ANONYMOUS", MAP_ANONYMOUS);
Andrew M. Kuchling1ed7d2d2000-03-30 21:14:30 +00001291#endif
1292
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001293 setint(dict, "PAGESIZE", (long)my_getpagesize());
Andrew M. Kuchling961fe172000-06-03 19:41:42 +00001294
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001295 setint(dict, "ALLOCATIONGRANULARITY", (long)my_getallocationgranularity());
1296
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001297 setint(dict, "ACCESS_READ", ACCESS_READ);
1298 setint(dict, "ACCESS_WRITE", ACCESS_WRITE);
1299 setint(dict, "ACCESS_COPY", ACCESS_COPY);
Tim Peters5ebfd362001-11-13 23:11:19 +00001300}