Gentoo Websites Logo
Go to: Gentoo Home Documentation Forums Lists Bugs Planet Store Wiki Get Gentoo!
View | Details | Raw Unified | Return to bug 329499 | Differences between
and this patch

Collapse All | Expand All

(-)setup.py (-2 / +1 lines)
Lines 1636-1643 Link Here
1636
                   '_ctypes/callbacks.c',
1636
                   '_ctypes/callbacks.c',
1637
                   '_ctypes/callproc.c',
1637
                   '_ctypes/callproc.c',
1638
                   '_ctypes/stgdict.c',
1638
                   '_ctypes/stgdict.c',
1639
                   '_ctypes/cfield.c',
1639
                   '_ctypes/cfield.c']
1640
                   '_ctypes/malloc_closure.c']
1641
        depends = ['_ctypes/ctypes.h']
1640
        depends = ['_ctypes/ctypes.h']
1642
1641
1643
        if sys.platform == 'darwin':
1642
        if sys.platform == 'darwin':
(-)Modules/_ctypes/malloc_closure.c (-110 lines)
Lines 1-110 Link Here
1
#include <Python.h>
2
#include <ffi.h>
3
#ifdef MS_WIN32
4
#include <windows.h>
5
#else
6
#include <sys/mman.h>
7
#include <unistd.h>
8
# if !defined(MAP_ANONYMOUS) && defined(MAP_ANON)
9
#  define MAP_ANONYMOUS MAP_ANON
10
# endif
11
#endif
12
#include "ctypes.h"
13
14
/* BLOCKSIZE can be adjusted.  Larger blocksize will take a larger memory
15
   overhead, but allocate less blocks from the system.  It may be that some
16
   systems have a limit of how many mmap'd blocks can be open.
17
*/
18
19
#define BLOCKSIZE _pagesize
20
21
/* #define MALLOC_CLOSURE_DEBUG */ /* enable for some debugging output */
22
23
/******************************************************************/
24
25
typedef union _tagITEM {
26
    ffi_closure closure;
27
    union _tagITEM *next;
28
} ITEM;
29
30
static ITEM *free_list;
31
static int _pagesize;
32
33
static void more_core(void)
34
{
35
    ITEM *item;
36
    int count, i;
37
38
/* determine the pagesize */
39
#ifdef MS_WIN32
40
    if (!_pagesize) {
41
        SYSTEM_INFO systeminfo;
42
        GetSystemInfo(&systeminfo);
43
        _pagesize = systeminfo.dwPageSize;
44
    }
45
#else
46
    if (!_pagesize) {
47
#ifdef _SC_PAGESIZE
48
        _pagesize = sysconf(_SC_PAGESIZE);
49
#else
50
        _pagesize = getpagesize();
51
#endif
52
    }
53
#endif
54
55
    /* calculate the number of nodes to allocate */
56
    count = BLOCKSIZE / sizeof(ITEM);
57
58
    /* allocate a memory block */
59
#ifdef MS_WIN32
60
    item = (ITEM *)VirtualAlloc(NULL,
61
                                           count * sizeof(ITEM),
62
                                           MEM_COMMIT,
63
                                           PAGE_EXECUTE_READWRITE);
64
    if (item == NULL)
65
        return;
66
#else
67
    item = (ITEM *)mmap(NULL,
68
                        count * sizeof(ITEM),
69
                        PROT_READ | PROT_WRITE | PROT_EXEC,
70
                        MAP_PRIVATE | MAP_ANONYMOUS,
71
                        -1,
72
                        0);
73
    if (item == (void *)MAP_FAILED)
74
        return;
75
#endif
76
77
#ifdef MALLOC_CLOSURE_DEBUG
78
    printf("block at %p allocated (%d bytes), %d ITEMs\n",
79
           item, count * sizeof(ITEM), count);
80
#endif
81
    /* put them into the free list */
82
    for (i = 0; i < count; ++i) {
83
        item->next = free_list;
84
        free_list = item;
85
        ++item;
86
    }
87
}
88
89
/******************************************************************/
90
91
/* put the item back into the free list */
92
void _ctypes_free_closure(void *p)
93
{
94
    ITEM *item = (ITEM *)p;
95
    item->next = free_list;
96
    free_list = item;
97
}
98
99
/* return one item from the free list, allocating more if needed */
100
void *_ctypes_alloc_closure(void)
101
{
102
    ITEM *item;
103
    if (!free_list)
104
        more_core();
105
    if (!free_list)
106
        return NULL;
107
    item = free_list;
108
    free_list = item->next;
109
    return item;
110
}
(-)Modules/_ctypes/_ctypes.c (-1 / +1 lines)
Lines 3367-3373 Link Here
3367
    self->callable = callable;
3367
    self->callable = callable;
3368
3368
3369
    self->thunk = thunk;
3369
    self->thunk = thunk;
3370
    *(void **)self->b_ptr = (void *)thunk->pcl;
3370
    *(void **)self->b_ptr = (void *)thunk->pcl_exec;
3371
3371
3372
    Py_INCREF((PyObject *)thunk); /* for KeepRef */
3372
    Py_INCREF((PyObject *)thunk); /* for KeepRef */
3373
    if (-1 == KeepRef((CDataObject *)self, 0, (PyObject *)thunk)) {
3373
    if (-1 == KeepRef((CDataObject *)self, 0, (PyObject *)thunk)) {
(-)Modules/_ctypes/ctypes.h (-1 / +2 lines)
Lines 54-60 Link Here
54
54
55
typedef struct {
55
typedef struct {
56
    PyObject_VAR_HEAD
56
    PyObject_VAR_HEAD
57
    ffi_closure *pcl; /* the C callable */
57
    ffi_closure *pcl_write; /* the C callable, writeable */
58
    void *pcl_exec;         /* the C callable, executable */
58
    ffi_cif cif;
59
    ffi_cif cif;
59
    int flags;
60
    int flags;
60
    PyObject *converters;
61
    PyObject *converters;
(-)Modules/_ctypes/callbacks.c (-6 / +10 lines)
Lines 16-23 Link Here
16
    Py_XDECREF(self->converters);
16
    Py_XDECREF(self->converters);
17
    Py_XDECREF(self->callable);
17
    Py_XDECREF(self->callable);
18
    Py_XDECREF(self->restype);
18
    Py_XDECREF(self->restype);
19
    if (self->pcl)
19
    if (self->pcl_write)
20
        _ctypes_free_closure(self->pcl);
20
        ffi_closure_free(self->pcl_write);
21
    PyObject_GC_Del(self);
21
    PyObject_GC_Del(self);
22
}
22
}
23
23
Lines 370-376 Link Here
370
        return NULL;
370
        return NULL;
371
    }
371
    }
372
372
373
    p->pcl = NULL;
373
    p->pcl_exec = NULL;
374
    p->pcl_write = NULL;
374
    memset(&p->cif, 0, sizeof(p->cif));
375
    memset(&p->cif, 0, sizeof(p->cif));
375
    p->converters = NULL;
376
    p->converters = NULL;
376
    p->callable = NULL;
377
    p->callable = NULL;
Lines 400-407 Link Here
400
401
401
    assert(CThunk_CheckExact((PyObject *)p));
402
    assert(CThunk_CheckExact((PyObject *)p));
402
403
403
    p->pcl = _ctypes_alloc_closure();
404
    p->pcl_write = ffi_closure_alloc(sizeof(ffi_closure),
404
    if (p->pcl == NULL) {
405
                                     &p->pcl_exec);
406
    if (p->pcl_write == NULL) {
405
        PyErr_NoMemory();
407
        PyErr_NoMemory();
406
        goto error;
408
        goto error;
407
    }
409
    }
Lines 446-452 Link Here
446
                     "ffi_prep_cif failed with %d", result);
448
                     "ffi_prep_cif failed with %d", result);
447
        goto error;
449
        goto error;
448
    }
450
    }
449
    result = ffi_prep_closure(p->pcl, &p->cif, closure_fcn, p);
451
    result = ffi_prep_closure_loc(p->pcl_write, &p->cif, closure_fcn,
452
                                  p,
453
                                  p->pcl_exec);
450
    if (result != FFI_OK) {
454
    if (result != FFI_OK) {
451
        PyErr_Format(PyExc_RuntimeError,
455
        PyErr_Format(PyExc_RuntimeError,
452
                     "ffi_prep_closure failed with %d", result);
456
                     "ffi_prep_closure failed with %d", result);

Return to bug 329499