1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
|
diff -ru pyobjc-1946/pyobjc-core/Modules/objc/closure_pool.m pyobjc-1946+iPhone/pyobjc-core/Modules/objc/closure_pool.m
--- pyobjc-1946/pyobjc-core/Modules/objc/closure_pool.m 2007-11-25 10:33:57.000000000 +0000
+++ pyobjc-1946+iPhone/pyobjc-core/Modules/objc/closure_pool.m 2008-07-24 00:32:15.000000000 +0000
@@ -1,62 +1,22 @@
-/*
- * A simple allocator for closure. This assumes that most closures are kept
- * alive forever and we therefore don't have to return storage to the OS.
- */
#include "pyobjc.h"
-
#include <sys/mman.h>
+#include <mach/vm_param.h>
-typedef struct freelist {
- struct freelist* next;
-} freelist;
-
-static freelist* closure_freelist = NULL;
-
-
-static freelist* allocate_block(void)
-{
-
- /* Allocate ffi_closure in groups of 10 VM pages */
-#define BLOCKSIZE ((PAGE_SIZE*10)/sizeof(ffi_closure*))
-
- freelist* newblock = mmap(NULL, BLOCKSIZE * sizeof(ffi_closure),
- PROT_READ|PROT_WRITE|PROT_EXEC,
- MAP_PRIVATE|MAP_ANON, -1, 0);
- size_t i;
-
- if (newblock == (void*)-1) {
- PyErr_NoMemory();
- return NULL;
- }
- for (i = 0; i < BLOCKSIZE-1; i++) {
- ((freelist*)(((ffi_closure*)newblock)+i))->next =
- (freelist*)(((ffi_closure*)newblock)+(i+1));
- }
-
- ((freelist*)(((ffi_closure*)newblock)+(BLOCKSIZE-1)))->next = NULL;
- return newblock;
-}
-
-
+ffi_closure *PyObjC_malloc_closure(void) {
+ ffi_closure *closure = (ffi_closure *) mmap(
+ NULL, sizeof(ffi_closure),
+ PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE,
+ -1, 0
+ );
+
+ if (closure == MAP_FAILED) {
+ PyErr_NoMemory();
+ return NULL;
+ }
-ffi_closure*
-PyObjC_malloc_closure(void)
-{
- if (closure_freelist == NULL) {
- closure_freelist = allocate_block();
- if (closure_freelist == NULL) {
- return NULL;
- }
- }
- ffi_closure* result = (ffi_closure*)closure_freelist;
- closure_freelist = closure_freelist->next;
- return result;
+ return closure;
}
-int
-PyObjC_free_closure(ffi_closure* cl)
-{
- ((freelist*)cl)->next = closure_freelist;
- closure_freelist = (freelist*)cl;
- return 0;
+int PyObjC_free_closure(ffi_closure *closure) {
+ return munmap(closure, sizeof(ffi_closure));
}
diff -ru pyobjc-1946/pyobjc-core/Modules/objc/libffi_support.m pyobjc-1946+iPhone/pyobjc-core/Modules/objc/libffi_support.m
--- pyobjc-1946/pyobjc-core/Modules/objc/libffi_support.m 2008-07-24 00:37:30.000000000 +0000
+++ pyobjc-1946+iPhone/pyobjc-core/Modules/objc/libffi_support.m 2008-07-24 00:37:09.000000000 +0000
@@ -17,6 +17,7 @@
#import <Foundation/NSHost.h>
#import <CoreFoundation/CoreFoundation.h>
+#include <sys/mman.h>
#ifdef __ppc64__
extern bool ffi64_stret_needs_ptr(const ffi_type* inType,
@@ -3724,11 +3725,20 @@
rv = ffi_prep_closure(cl, cif, func, userdata);
if (rv != FFI_OK) {
PyObjCFFI_FreeCIF(cif);
+ PyObjC_free_closure(cl);
PyErr_Format(PyExc_RuntimeError,
"Cannot create FFI closure: %d", rv);
return NULL;
}
+ if (mprotect(cl, sizeof(*cl), PROT_READ | PROT_EXEC) == -1) {
+ PyObjCFFI_FreeCIF(cif);
+ PyObjC_free_closure(cl);
+ PyErr_Format(PyExc_RuntimeError,
+ "Cannot create FFI closure: %d", errno);
+ return NULL;
+ }
+
return (IMP)cl;
}
diff -ru pyobjc-1946/pyobjc-core/Modules/objc/opaque-pointer.m pyobjc-1946+iPhone/pyobjc-core/Modules/objc/opaque-pointer.m
--- pyobjc-1946/pyobjc-core/Modules/objc/opaque-pointer.m 2007-11-23 13:29:54.000000000 +0000
+++ pyobjc-1946+iPhone/pyobjc-core/Modules/objc/opaque-pointer.m 2008-07-24 00:35:43.000000000 +0000
@@ -2,6 +2,7 @@
* Generic support for opaque pointer types, such as NSZone*
*/
#include "pyobjc.h"
+#include <sys/mman.h>
typedef struct {
PyObject_HEAD
@@ -247,6 +248,13 @@
"Cannot create FFI closure: %d", rv);
goto error_cleanup;
}
+
+ if (mprotect(cl, sizeof(*cl), PROT_READ | PROT_EXEC) == -1) {
+ PyErr_Format(PyExc_RuntimeError,
+ "Cannot create FFI closure: %d", errno);
+ goto error_cleanup;
+ }
+
to_c = (PyObjCPointerWrapper_FromPythonFunc)cl;
cl = NULL;
@@ -261,6 +271,13 @@
"Cannot create FFI closure: %d", rv);
goto error_cleanup;
}
+
+ if (mprotect(cl, sizeof(*cl), PROT_READ | PROT_EXEC) == -1) {
+ PyErr_Format(PyExc_RuntimeError,
+ "Cannot create FFI closure: %d", errno);
+ goto error_cleanup;
+ }
+
from_c = (PyObjCPointerWrapper_ToPythonFunc)cl;
cl = NULL;
diff -ru pyobjc-1946/pyobjc-core/Modules/objc/struct-wrapper.m pyobjc-1946+iPhone/pyobjc-core/Modules/objc/struct-wrapper.m
--- pyobjc-1946/pyobjc-core/Modules/objc/struct-wrapper.m 2007-11-23 13:29:54.000000000 +0000
+++ pyobjc-1946+iPhone/pyobjc-core/Modules/objc/struct-wrapper.m 2008-07-24 00:35:46.000000000 +0000
@@ -12,6 +12,7 @@
* is only usefull for PyObjC.
*/
#include "pyobjc.h"
+#include <sys/mman.h>
/*
* First some helpers: easy access to the actual fields
@@ -603,6 +604,13 @@
return NULL;
}
+ if (mprotect(cl, sizeof(*cl), PROT_READ | PROT_EXEC) == -1) {
+ PyObjC_free_closure(cl);
+ PyErr_Format(PyExc_RuntimeError,
+ "Cannot create FFI closure: %d", errno);
+ return NULL;
+ }
+
return (initproc)cl;
}
|