Skip to content

Commit c90a23b

Browse files
committed
[GR-13552] Initial support for module mmap.
PullRequest: graalpython/390
2 parents 52d12f5 + 74bceee commit c90a23b

32 files changed

+2336
-330
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
2+
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3+
#
4+
# The Universal Permissive License (UPL), Version 1.0
5+
#
6+
# Subject to the condition set forth below, permission is hereby granted to any
7+
# person obtaining a copy of this software, associated documentation and/or
8+
# data (collectively the "Software"), free of charge and under any and all
9+
# copyright rights in the Software, and any and all patent rights owned or
10+
# freely licensable by each licensor hereunder covering either (i) the
11+
# unmodified Software as contributed to or provided by such licensor, or (ii)
12+
# the Larger Works (as defined below), to deal in both
13+
#
14+
# (a) the Software, and
15+
#
16+
# (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
17+
# one is included with the Software each a "Larger Work" to which the Software
18+
# is contributed by such licensors),
19+
#
20+
# without restriction, including without limitation the rights to copy, create
21+
# derivative works of, display, perform, and distribute the Software and make,
22+
# use, sell, offer for sale, import, export, have made, and have sold the
23+
# Software and the Larger Work(s), and to sublicense the foregoing rights on
24+
# either these or other terms.
25+
#
26+
# This license is subject to the following condition:
27+
#
28+
# The above copyright notice and either this complete permission notice or at a
29+
# minimum a reference to the UPL must be included in all copies or substantial
30+
# portions of the Software.
31+
#
32+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
33+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
34+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
35+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
36+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
37+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
38+
# SOFTWARE.
39+
40+
import mmap
41+
42+
# create 64 MB anonymous mmap
43+
size = 64*1024
44+
mm = mmap.mmap(-1, size)
45+
data = b"Hello World"
46+
ndata = len(data)
47+
niter = size // ndata
48+
49+
50+
def fill():
51+
# sequential access
52+
mm.seek(0)
53+
for i in range(niter):
54+
mm.write(data)
55+
56+
57+
def measure(num):
58+
for i in range(num):
59+
fill()
60+
print(mm[0:ndata])
61+
62+
63+
def __benchmark__(num=100):
64+
measure(num)
65+
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
2+
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3+
#
4+
# The Universal Permissive License (UPL), Version 1.0
5+
#
6+
# Subject to the condition set forth below, permission is hereby granted to any
7+
# person obtaining a copy of this software, associated documentation and/or
8+
# data (collectively the "Software"), free of charge and under any and all
9+
# copyright rights in the Software, and any and all patent rights owned or
10+
# freely licensable by each licensor hereunder covering either (i) the
11+
# unmodified Software as contributed to or provided by such licensor, or (ii)
12+
# the Larger Works (as defined below), to deal in both
13+
#
14+
# (a) the Software, and
15+
#
16+
# (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
17+
# one is included with the Software each a "Larger Work" to which the Software
18+
# is contributed by such licensors),
19+
#
20+
# without restriction, including without limitation the rights to copy, create
21+
# derivative works of, display, perform, and distribute the Software and make,
22+
# use, sell, offer for sale, import, export, have made, and have sold the
23+
# Software and the Larger Work(s), and to sublicense the foregoing rights on
24+
# either these or other terms.
25+
#
26+
# This license is subject to the following condition:
27+
#
28+
# The above copyright notice and either this complete permission notice or at a
29+
# minimum a reference to the UPL must be included in all copies or substantial
30+
# portions of the Software.
31+
#
32+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
33+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
34+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
35+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
36+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
37+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
38+
# SOFTWARE.
39+
40+
import mmap
41+
import tempfile
42+
43+
# create temporary file
44+
data = b"Hello World"
45+
ndata = len(data)
46+
47+
48+
def fill(mm, size):
49+
# sequential access
50+
mm.seek(0)
51+
for i in range(size // ndata):
52+
mm.write(data)
53+
54+
55+
def measure(num):
56+
tmp_path = tempfile.mkstemp()
57+
with open(tmp_path[1], "wb") as f:
58+
f.write(b'\x00' * (64 * 1024))
59+
60+
with open(tmp_path[1], "r+b") as f:
61+
mm = mmap.mmap(f.fileno(), 0)
62+
size = mm.size()
63+
for i in range(num):
64+
fill(mm, size)
65+
print(mm[0:ndata])
66+
67+
68+
def __benchmark__(num=100):
69+
measure(num)
70+
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/* Copyright (c) 2019, Oracle and/or its affiliates.
2+
* Copyright (C) 1996-2017 Python Software Foundation
3+
*
4+
* Licensed under the PYTHON SOFTWARE FOUNDATION LICENSE VERSION 2
5+
*/
6+
7+
#include "../src/capi.h"
8+
9+
typedef enum
10+
{
11+
ACCESS_DEFAULT,
12+
ACCESS_READ,
13+
ACCESS_WRITE,
14+
ACCESS_COPY
15+
} access_mode;
16+
17+
typedef struct {
18+
PyObject_HEAD
19+
char * data;
20+
Py_ssize_t size;
21+
Py_ssize_t pos; /* relative to offset */
22+
#ifdef MS_WINDOWS
23+
long long offset;
24+
#else
25+
off_t offset;
26+
#endif
27+
int exports;
28+
29+
#ifdef MS_WINDOWS
30+
HANDLE map_handle;
31+
HANDLE file_handle;
32+
char * tagname;
33+
#endif
34+
35+
#ifdef UNIX
36+
int fd;
37+
#endif
38+
39+
PyObject *weakreflist;
40+
access_mode access;
41+
} mmap_object;
42+
43+
44+
POLYGLOT_DECLARE_TYPE(mmap_object);
45+
static PyTypeObject mmap_object_type = PY_TRUFFLE_TYPE("mmap.mmap", NULL, Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, sizeof(mmap_object));
46+
47+
int mmap_getbuffer(PyObject *self, Py_buffer *view, int flags) {
48+
// TODO(fa) readonly flag
49+
return PyBuffer_FillInfo(view, (PyObject*)self, ((mmap_object *)self)->data, PyObject_Size((PyObject *)self), 0, flags);
50+
}
51+
52+
static PyObject* mmap_init_bufferprotocol(PyObject* self, PyObject* mmap_type) {
53+
assert(PyType_Check(mmap_type));
54+
initialize_type_structure(&mmap_object_type, mmap_type, polyglot_mmap_object_typeid());
55+
56+
polyglot_invoke(PY_TRUFFLE_CEXT, "PyTruffle_SetBufferProcs", native_to_java(mmap_type), (getbufferproc) mmap_getbuffer, (releasebufferproc) NULL);
57+
return Py_None;
58+
}
59+
60+
static struct PyMethodDef module_functions[] = {
61+
{"init_bufferprotocol", mmap_init_bufferprotocol, METH_O, NULL},
62+
{NULL, NULL} /* sentinel */
63+
};
64+
65+
static struct PyModuleDef mmapmodule = {
66+
PyModuleDef_HEAD_INIT,
67+
"_mmap",
68+
NULL,
69+
-1,
70+
module_functions,
71+
NULL,
72+
NULL,
73+
NULL,
74+
NULL
75+
};
76+
77+
PyMODINIT_FUNC
78+
PyInit__mmap(void)
79+
{
80+
PyObject *dict, *module;
81+
82+
module = PyModule_Create(&mmapmodule);
83+
if (module == NULL) {
84+
return NULL;
85+
}
86+
dict = PyModule_GetDict(module);
87+
if (!dict) {
88+
return NULL;
89+
}
90+
91+
return module;
92+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
2+
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3+
#
4+
# The Universal Permissive License (UPL), Version 1.0
5+
#
6+
# Subject to the condition set forth below, permission is hereby granted to any
7+
# person obtaining a copy of this software, associated documentation and/or
8+
# data (collectively the "Software"), free of charge and under any and all
9+
# copyright rights in the Software, and any and all patent rights owned or
10+
# freely licensable by each licensor hereunder covering either (i) the
11+
# unmodified Software as contributed to or provided by such licensor, or (ii)
12+
# the Larger Works (as defined below), to deal in both
13+
#
14+
# (a) the Software, and
15+
#
16+
# (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
17+
# one is included with the Software each a "Larger Work" to which the Software
18+
# is contributed by such licensors),
19+
#
20+
# without restriction, including without limitation the rights to copy, create
21+
# derivative works of, display, perform, and distribute the Software and make,
22+
# use, sell, offer for sale, import, export, have made, and have sold the
23+
# Software and the Larger Work(s), and to sublicense the foregoing rights on
24+
# either these or other terms.
25+
#
26+
# This license is subject to the following condition:
27+
#
28+
# The above copyright notice and either this complete permission notice or at a
29+
# minimum a reference to the UPL must be included in all copies or substantial
30+
# portions of the Software.
31+
#
32+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
33+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
34+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
35+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
36+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
37+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
38+
# SOFTWARE.
39+
40+
import sys
41+
import mmap
42+
import tempfile
43+
from . import CPyExtTestCase, CPyExtFunction, CPyExtFunctionOutVars, unhandled_error_compare, GRAALPYTHON
44+
__dir__ = __file__.rpartition("/")[0]
45+
46+
47+
def create_and_map_file():
48+
tmp = tempfile.mktemp(prefix="pymmap_")
49+
with open(tmp, "w") as f:
50+
f.write("hello, world")
51+
f = open(tmp, "r")
52+
return mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ)
53+
54+
55+
class TestPyMmap(CPyExtTestCase):
56+
def compile_module(self, name):
57+
type(self).mro()[1].__dict__["test_%s" % name].create_module(name)
58+
super(TestPyMmap, self).compile_module(name)
59+
60+
61+
test_buffer = CPyExtFunction(
62+
lambda args: b"hello, world",
63+
lambda: (
64+
(create_and_map_file(),),
65+
),
66+
code="""
67+
static PyObject* get_mmap_buf(PyObject* mmapObj) {
68+
Py_buffer buf;
69+
Py_ssize_t len, i;
70+
char* data = NULL;
71+
if (PyObject_GetBuffer(mmapObj, &buf, PyBUF_SIMPLE)) {
72+
return NULL;
73+
}
74+
len = buf.len;
75+
data = (char*) malloc(sizeof(char)*len);
76+
for (i=0; i < buf.len; i++) {
77+
data[i] = ((char *) buf.buf)[i];
78+
}
79+
return PyBytes_FromStringAndSize(data, len);
80+
}
81+
""",
82+
resultspec="O",
83+
argspec='O',
84+
arguments=["PyObject* mmapObj"],
85+
callfunction="get_mmap_buf",
86+
cmpfunc=unhandled_error_compare
87+
)

0 commit comments

Comments
 (0)