Author: Armin Rigo <[email protected]>
Branch: ffi_closure_alloc
Changeset: r2376:4406e3b72133
Date: 2015-11-10 15:03 +0100
http://bitbucket.org/cffi/cffi/changeset/4406e3b72133/

Log:    try

diff --git a/c/_cffi_backend.c b/c/_cffi_backend.c
--- a/c/_cffi_backend.c
+++ b/c/_cffi_backend.c
@@ -58,8 +58,6 @@
 # endif
 #endif
 
-#include "malloc_closure.h"
-
 #if PY_MAJOR_VERSION >= 3
 # define STR_OR_BYTES "bytes"
 # define PyText_Type PyUnicode_Type
@@ -1614,7 +1612,7 @@
         ffi_closure *closure = (ffi_closure *)cd->c_data;
         PyObject *args = (PyObject *)(closure->user_data);
         Py_XDECREF(args);
-        cffi_closure_free(closure);
+        ffi_closure_free(closure);
     }
     else if (cd->c_type->ct_flags & CT_IS_UNSIZED_CHAR_A) {  /* from_buffer */
         Py_buffer *view = ((CDataObject_owngc_frombuf *)cd)->bufferview;
@@ -5091,6 +5089,7 @@
     PyObject *py_rawerr, *infotuple = NULL;
     cif_description_t *cif_descr;
     ffi_closure *closure;
+    void *closure_ex;
     Py_ssize_t size;
 
     if (!PyArg_ParseTuple(args, "O!O|OO:callback", &CTypeDescr_Type, &ct, &ob,
@@ -5135,7 +5134,7 @@
     if (infotuple == NULL)
         return NULL;
 
-    closure = cffi_closure_alloc();
+    closure = ffi_closure_alloc(sizeof(ffi_closure), &closure_ex);
 
     cd = PyObject_GC_New(CDataObject, &CDataOwningGC_Type);
     if (cd == NULL)
@@ -5153,8 +5152,8 @@
                      "return type or with '...'", ct->ct_name);
         goto error;
     }
-    if (ffi_prep_closure(closure, &cif_descr->cif,
-                         invoke_callback, infotuple) != FFI_OK) {
+    if (ffi_prep_closure_loc(closure, &cif_descr->cif, invoke_callback,
+                             infotuple, closure_ex) != FFI_OK) {
         PyErr_SetString(PyExc_SystemError,
                         "libffi failed to build this callback");
         goto error;
@@ -5168,7 +5167,7 @@
  error:
     closure->user_data = NULL;
     if (cd == NULL)
-        cffi_closure_free(closure);
+        ffi_closure_free(closure);
     else
         Py_DECREF(cd);
     Py_XDECREF(infotuple);
diff --git a/c/malloc_closure.h b/c/libffi_msvc/malloc_closure.c
rename from c/malloc_closure.h
rename to c/libffi_msvc/malloc_closure.c
--- a/c/malloc_closure.h
+++ b/c/libffi_msvc/malloc_closure.c
@@ -3,63 +3,8 @@
  * and has received some edits.
  */
 
+#include <windows.h>
 #include <ffi.h>
-#ifdef MS_WIN32
-#include <windows.h>
-#else
-#include <sys/mman.h>
-#include <unistd.h>
-# if !defined(MAP_ANONYMOUS) && defined(MAP_ANON)
-#  define MAP_ANONYMOUS MAP_ANON
-# endif
-#endif
-
-/* On PaX enable kernels that have MPROTECT enable we can't use PROT_EXEC.
-
-   This is, apparently, an undocumented change to ffi_prep_closure():
-   depending on the Linux kernel we're running on, we must give it a
-   mmap that is either PROT_READ|PROT_WRITE|PROT_EXEC or only
-   PROT_READ|PROT_WRITE.  In the latter case, just trying to obtain a
-   mmap with PROT_READ|PROT_WRITE|PROT_EXEC would kill our process(!),
-   but in that situation libffi is fine with only PROT_READ|PROT_WRITE.
-   There is nothing in the libffi API to know that, though, so we have
-   to guess by parsing /proc/self/status.  "Meh."
- */
-#ifdef __linux__
-#include <stdlib.h>
-
-static int emutramp_enabled = -1;
-
-static int
-emutramp_enabled_check (void)
-{
-    char *buf = NULL;
-    size_t len = 0;
-    FILE *f;
-    int ret;
-    f = fopen ("/proc/self/status", "r");
-    if (f == NULL)
-        return 0;
-    ret = 0;
-
-    while (getline (&buf, &len, f) != -1)
-        if (!strncmp (buf, "PaX:", 4))
-            {
-                char emutramp;
-                if (sscanf (buf, "%*s %*c%c", &emutramp) == 1)
-                    ret = (emutramp == 'E');
-                break;
-            }
-    free (buf);
-    fclose (f);
-    return ret;
-}
-
-#define is_emutramp_enabled() (emutramp_enabled >= 0 ? emutramp_enabled \
-        : (emutramp_enabled = emutramp_enabled_check ()))
-#else
-#define is_emutramp_enabled() 0
-#endif
 
 
 /* 'allocate_num_pages' is dynamically adjusted starting from one
@@ -90,24 +35,14 @@
     union mmaped_block *item;
     Py_ssize_t count, i;
 
-/* determine the pagesize */
-#ifdef MS_WIN32
+    /* determine the pagesize */
     if (!_pagesize) {
         SYSTEM_INFO systeminfo;
         GetSystemInfo(&systeminfo);
         _pagesize = systeminfo.dwPageSize;
+        if (_pagesize <= 0)
+            _pagesize = 4096;
     }
-#else
-    if (!_pagesize) {
-#ifdef _SC_PAGESIZE
-        _pagesize = sysconf(_SC_PAGESIZE);
-#else
-        _pagesize = getpagesize();
-#endif
-    }
-#endif
-    if (_pagesize <= 0)
-        _pagesize = 4096;
 
     /* bump 'allocate_num_pages' */
     allocate_num_pages = 1 + (
@@ -117,28 +52,12 @@
     count = (allocate_num_pages * _pagesize) / sizeof(union mmaped_block);
 
     /* allocate a memory block */
-#ifdef MS_WIN32
     item = (union mmaped_block *)VirtualAlloc(NULL,
                                            count * sizeof(union mmaped_block),
                                            MEM_COMMIT,
                                            PAGE_EXECUTE_READWRITE);
     if (item == NULL)
         return;
-#else
-    {
-    int prot = PROT_READ | PROT_WRITE | PROT_EXEC;
-    if (is_emutramp_enabled ())
-        prot &= ~PROT_EXEC;
-    item = (union mmaped_block *)mmap(NULL,
-                        allocate_num_pages * _pagesize,
-                        prot,
-                        MAP_PRIVATE | MAP_ANONYMOUS,
-                        -1,
-                        0);
-    if (item == (void *)MAP_FAILED)
-        return;
-    }
-#endif
 
 #ifdef MALLOC_CLOSURE_DEBUG
     printf("block at %p allocated (%ld bytes), %ld mmaped_blocks\n",
@@ -155,7 +74,7 @@
 /******************************************************************/
 
 /* put the item back into the free list */
-static void cffi_closure_free(ffi_closure *p)
+void ffi_closure_free(void *p)
 {
     union mmaped_block *item = (union mmaped_block *)p;
     item->next = free_list;
@@ -163,14 +82,18 @@
 }
 
 /* return one item from the free list, allocating more if needed */
-static ffi_closure *cffi_closure_alloc(void)
+void *ffi_closure_alloc (size_t size, void **code)
 {
     union mmaped_block *item;
+    void *result;
+    assert(size == sizeof(ffi_closure));
     if (!free_list)
         more_core();
     if (!free_list)
         return NULL;
     item = free_list;
     free_list = item->next;
-    return &item->closure;
+    result = &item->closure;
+    *code = result;
+    return result;
 }
diff --git a/c/misc_win32.h b/c/misc_win32.h
--- a/c/misc_win32.h
+++ b/c/misc_win32.h
@@ -234,8 +234,3 @@
     sprintf(buf, "error 0x%x", (unsigned int)dw);
     return buf;
 }
-
-/************************************************************/
-/* obscure */
-
-#define ffi_prep_closure(a,b,c,d)  ffi_prep_closure_loc(a,b,c,d,a)
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to