Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(164998)

Delta Between Two Patch Sets: Modules/mmapmodule.c

Issue 23001: Accept mutable bytes-like objects
Left Patch Set: Created 5 years ago
Right Patch Set: Created 4 years, 8 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
LEFTRIGHT
1 /* 1 /*
2 / Author: Sam Rushing <rushing@nightmare.com> 2 / Author: Sam Rushing <rushing@nightmare.com>
3 / Hacked for Unix by AMK 3 / Hacked for Unix by AMK
4 / $Id$ 4 / $Id$
5 5
6 / Modified to support mmap with offset - to map a 'window' of a file 6 / Modified to support mmap with offset - to map a 'window' of a file
7 / Author: Yotam Medini yotamm@mellanox.co.il 7 / Author: Yotam Medini yotamm@mellanox.co.il
8 / 8 /
9 / mmapmodule.cpp -- map a view of a file into memory 9 / mmapmodule.cpp -- map a view of a file into memory
10 / 10 /
(...skipping 447 matching lines...) Expand 10 before | Expand all | Expand 10 after
458 return PyLong_FromLong((long)low); 458 return PyLong_FromLong((long)low);
459 size = (((PY_LONG_LONG)high)<<32) + low; 459 size = (((PY_LONG_LONG)high)<<32) + low;
460 return PyLong_FromLongLong(size); 460 return PyLong_FromLongLong(size);
461 } else { 461 } else {
462 return PyLong_FromSsize_t(self->size); 462 return PyLong_FromSsize_t(self->size);
463 } 463 }
464 #endif /* MS_WINDOWS */ 464 #endif /* MS_WINDOWS */
465 465
466 #ifdef UNIX 466 #ifdef UNIX
467 { 467 {
468 struct stat buf; 468 struct _Py_stat_struct buf;
469 if (-1 == fstat(self->fd, &buf)) { 469 if (-1 == _Py_fstat(self->fd, &buf)) {
470 PyErr_SetFromErrno(PyExc_OSError); 470 PyErr_SetFromErrno(PyExc_OSError);
471 return NULL; 471 return NULL;
472 } 472 }
473 #ifdef HAVE_LARGEFILE_SUPPORT 473 #ifdef HAVE_LARGEFILE_SUPPORT
474 return PyLong_FromLongLong(buf.st_size); 474 return PyLong_FromLongLong(buf.st_size);
475 #else 475 #else
476 return PyLong_FromLong(buf.st_size); 476 return PyLong_FromLong(buf.st_size);
477 #endif 477 #endif
478 } 478 }
479 #endif /* UNIX */ 479 #endif /* UNIX */
(...skipping 626 matching lines...) Expand 10 before | Expand all | Expand 10 after
1106 #ifdef HAVE_LARGEFILE_SUPPORT 1106 #ifdef HAVE_LARGEFILE_SUPPORT
1107 #define _Py_PARSE_OFF_T "L" 1107 #define _Py_PARSE_OFF_T "L"
1108 #else 1108 #else
1109 #define _Py_PARSE_OFF_T "l" 1109 #define _Py_PARSE_OFF_T "l"
1110 #endif 1110 #endif
1111 1111
1112 static PyObject * 1112 static PyObject *
1113 new_mmap_object(PyTypeObject *type, PyObject *args, PyObject *kwdict) 1113 new_mmap_object(PyTypeObject *type, PyObject *args, PyObject *kwdict)
1114 { 1114 {
1115 #ifdef HAVE_FSTAT 1115 #ifdef HAVE_FSTAT
1116 struct stat st; 1116 struct _Py_stat_struct st;
1117 #endif 1117 #endif
1118 mmap_object *m_obj; 1118 mmap_object *m_obj;
1119 PyObject *map_size_obj = NULL; 1119 PyObject *map_size_obj = NULL;
1120 Py_ssize_t map_size; 1120 Py_ssize_t map_size;
1121 off_t offset = 0; 1121 off_t offset = 0;
1122 int fd, flags = MAP_SHARED, prot = PROT_WRITE | PROT_READ; 1122 int fd, flags = MAP_SHARED, prot = PROT_WRITE | PROT_READ;
1123 int devzero = -1; 1123 int devzero = -1;
1124 int access = (int)ACCESS_DEFAULT; 1124 int access = (int)ACCESS_DEFAULT;
1125 static char *keywords[] = {"fileno", "length", 1125 static char *keywords[] = {"fileno", "length",
1126 "flags", "prot", 1126 "flags", "prot",
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
1173 "mmap invalid access parameter."); 1173 "mmap invalid access parameter.");
1174 } 1174 }
1175 1175
1176 #ifdef __APPLE__ 1176 #ifdef __APPLE__
1177 /* Issue #11277: fsync(2) is not enough on OS X - a special, OS X specific 1177 /* Issue #11277: fsync(2) is not enough on OS X - a special, OS X specific
1178 fcntl(2) is necessary to force DISKSYNC and get around mmap(2) bug */ 1178 fcntl(2) is necessary to force DISKSYNC and get around mmap(2) bug */
1179 if (fd != -1) 1179 if (fd != -1)
1180 (void)fcntl(fd, F_FULLFSYNC); 1180 (void)fcntl(fd, F_FULLFSYNC);
1181 #endif 1181 #endif
1182 #ifdef HAVE_FSTAT 1182 #ifdef HAVE_FSTAT
1183 if (fd != -1 && fstat(fd, &st) == 0 && S_ISREG(st.st_mode)) { 1183 if (fd != -1 && _Py_fstat(fd, &st) == 0 && S_ISREG(st.st_mode)) {
1184 if (map_size == 0) { 1184 if (map_size == 0) {
1185 if (st.st_size == 0) { 1185 if (st.st_size == 0) {
1186 PyErr_SetString(PyExc_ValueError, 1186 PyErr_SetString(PyExc_ValueError,
1187 "cannot mmap an empty file"); 1187 "cannot mmap an empty file");
1188 return NULL; 1188 return NULL;
1189 } 1189 }
1190 if (offset >= st.st_size) { 1190 if (offset >= st.st_size) {
1191 PyErr_SetString(PyExc_ValueError, 1191 PyErr_SetString(PyExc_ValueError,
1192 "mmap offset is greater than file size"); 1192 "mmap offset is greater than file size");
1193 return NULL; 1193 return NULL;
1194 } 1194 }
1195 if (st.st_size - offset > PY_SSIZE_T_MAX) { 1195 if (st.st_size - offset > PY_SSIZE_T_MAX) {
1196 PyErr_SetString(PyExc_ValueError, 1196 PyErr_SetString(PyExc_ValueError,
1197 "mmap length is too large"); 1197 "mmap length is too large");
1198 return NULL; 1198 return NULL;
1199 } 1199 }
1200 map_size = (Py_ssize_t) (st.st_size - offset); 1200 map_size = (Py_ssize_t) (st.st_size - offset);
1201 } else if (offset + (size_t)map_size > st.st_size) { 1201 } else if (offset + map_size > st.st_size) {
1202 PyErr_SetString(PyExc_ValueError, 1202 PyErr_SetString(PyExc_ValueError,
1203 "mmap length is greater than file size"); 1203 "mmap length is greater than file size");
1204 return NULL; 1204 return NULL;
1205 } 1205 }
1206 } 1206 }
1207 #endif 1207 #endif
1208 m_obj = (mmap_object *)type->tp_alloc(type, 0); 1208 m_obj = (mmap_object *)type->tp_alloc(type, 0);
1209 if (m_obj == NULL) {return NULL;} 1209 if (m_obj == NULL) {return NULL;}
1210 m_obj->data = NULL; 1210 m_obj->data = NULL;
1211 m_obj->size = (size_t) map_size; 1211 m_obj->size = (size_t) map_size;
1212 m_obj->pos = (size_t) 0; 1212 m_obj->pos = (size_t) 0;
1213 m_obj->weakreflist = NULL; 1213 m_obj->weakreflist = NULL;
1214 m_obj->exports = 0; 1214 m_obj->exports = 0;
1215 m_obj->offset = offset; 1215 m_obj->offset = offset;
1216 if (fd == -1) { 1216 if (fd == -1) {
1217 m_obj->fd = -1; 1217 m_obj->fd = -1;
1218 /* Assume the caller wants to map anonymous memory. 1218 /* Assume the caller wants to map anonymous memory.
1219 This is the same behaviour as Windows. mmap.mmap(-1, size) 1219 This is the same behaviour as Windows. mmap.mmap(-1, size)
1220 on both Windows and Unix map anonymous memory. 1220 on both Windows and Unix map anonymous memory.
1221 */ 1221 */
1222 #ifdef MAP_ANONYMOUS 1222 #ifdef MAP_ANONYMOUS
1223 /* BSD way to map anonymous memory */ 1223 /* BSD way to map anonymous memory */
1224 flags |= MAP_ANONYMOUS; 1224 flags |= MAP_ANONYMOUS;
1225 #else 1225 #else
1226 /* SVR4 method to map anonymous memory is to open /dev/zero */ 1226 /* SVR4 method to map anonymous memory is to open /dev/zero */
1227 fd = devzero = _Py_open("/dev/zero", O_RDWR); 1227 fd = devzero = _Py_open("/dev/zero", O_RDWR);
1228 if (devzero == -1) { 1228 if (devzero == -1) {
1229 Py_DECREF(m_obj); 1229 Py_DECREF(m_obj);
1230 PyErr_SetFromErrno(PyExc_OSError);
1231 return NULL; 1230 return NULL;
1232 } 1231 }
1233 #endif 1232 #endif
1234 } 1233 }
1235 else { 1234 else {
1236 m_obj->fd = _Py_dup(fd); 1235 m_obj->fd = _Py_dup(fd);
1237 if (m_obj->fd == -1) { 1236 if (m_obj->fd == -1) {
1238 Py_DECREF(m_obj); 1237 Py_DECREF(m_obj);
1239 return NULL; 1238 return NULL;
1240 } 1239 }
(...skipping 290 matching lines...) Expand 10 before | Expand all | Expand 10 after
1531 1530
1532 setint(dict, "PAGESIZE", (long)my_getpagesize()); 1531 setint(dict, "PAGESIZE", (long)my_getpagesize());
1533 1532
1534 setint(dict, "ALLOCATIONGRANULARITY", (long)my_getallocationgranularity()); 1533 setint(dict, "ALLOCATIONGRANULARITY", (long)my_getallocationgranularity());
1535 1534
1536 setint(dict, "ACCESS_READ", ACCESS_READ); 1535 setint(dict, "ACCESS_READ", ACCESS_READ);
1537 setint(dict, "ACCESS_WRITE", ACCESS_WRITE); 1536 setint(dict, "ACCESS_WRITE", ACCESS_WRITE);
1538 setint(dict, "ACCESS_COPY", ACCESS_COPY); 1537 setint(dict, "ACCESS_COPY", ACCESS_COPY);
1539 return module; 1538 return module;
1540 } 1539 }
LEFTRIGHT

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+