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 1867-1874 Link Here
1867
                   '_ctypes/callbacks.c',
1867
                   '_ctypes/callbacks.c',
1868
                   '_ctypes/callproc.c',
1868
                   '_ctypes/callproc.c',
1869
                   '_ctypes/stgdict.c',
1869
                   '_ctypes/stgdict.c',
1870
                   '_ctypes/cfield.c',
1870
                   '_ctypes/cfield.c']
1871
                   '_ctypes/malloc_closure.c']
1872
        depends = ['_ctypes/ctypes.h']
1871
        depends = ['_ctypes/ctypes.h']
1873
1872
1874
        if sys.platform == 'darwin':
1873
        if sys.platform == 'darwin':
(-)Modules/_ctypes/malloc_closure.c (-114 lines)
Lines 1-114 Link Here
1
/*****************************************************************
2
  This file should be kept compatible with Python 2.3, see PEP 291.
3
 *****************************************************************/
4
5
#include <Python.h>
6
#include <ffi.h>
7
#ifdef MS_WIN32
8
#include <windows.h>
9
#else
10
#include <sys/mman.h>
11
#include <unistd.h>
12
# if !defined(MAP_ANONYMOUS) && defined(MAP_ANON)
13
#  define MAP_ANONYMOUS MAP_ANON
14
# endif
15
#endif
16
#include "ctypes.h"
17
18
/* BLOCKSIZE can be adjusted.  Larger blocksize will take a larger memory
19
   overhead, but allocate less blocks from the system.  It may be that some
20
   systems have a limit of how many mmap'd blocks can be open.
21
*/
22
23
#define BLOCKSIZE _pagesize
24
25
/* #define MALLOC_CLOSURE_DEBUG */ /* enable for some debugging output */
26
27
/******************************************************************/
28
29
typedef union _tagITEM {
30
    ffi_closure closure;
31
    union _tagITEM *next;
32
} ITEM;
33
34
static ITEM *free_list;
35
static int _pagesize;
36
37
static void more_core(void)
38
{
39
    ITEM *item;
40
    int count, i;
41
42
/* determine the pagesize */
43
#ifdef MS_WIN32
44
    if (!_pagesize) {
45
        SYSTEM_INFO systeminfo;
46
        GetSystemInfo(&systeminfo);
47
        _pagesize = systeminfo.dwPageSize;
48
    }
49
#else
50
    if (!_pagesize) {
51
#ifdef _SC_PAGESIZE
52
        _pagesize = sysconf(_SC_PAGESIZE);
53
#else
54
        _pagesize = getpagesize();
55
#endif
56
    }
57
#endif
58
59
    /* calculate the number of nodes to allocate */
60
    count = BLOCKSIZE / sizeof(ITEM);
61
62
    /* allocate a memory block */
63
#ifdef MS_WIN32
64
    item = (ITEM *)VirtualAlloc(NULL,
65
                                           count * sizeof(ITEM),
66
                                           MEM_COMMIT,
67
                                           PAGE_EXECUTE_READWRITE);
68
    if (item == NULL)
69
        return;
70
#else
71
    item = (ITEM *)mmap(NULL,
72
                        count * sizeof(ITEM),
73
                        PROT_READ | PROT_WRITE | PROT_EXEC,
74
                        MAP_PRIVATE | MAP_ANONYMOUS,
75
                        -1,
76
                        0);
77
    if (item == (void *)MAP_FAILED)
78
        return;
79
#endif
80
81
#ifdef MALLOC_CLOSURE_DEBUG
82
    printf("block at %p allocated (%d bytes), %d ITEMs\n",
83
           item, count * sizeof(ITEM), count);
84
#endif
85
    /* put them into the free list */
86
    for (i = 0; i < count; ++i) {
87
        item->next = free_list;
88
        free_list = item;
89
        ++item;
90
    }
91
}
92
93
/******************************************************************/
94
95
/* put the item back into the free list */
96
void _ctypes_free_closure(void *p)
97
{
98
    ITEM *item = (ITEM *)p;
99
    item->next = free_list;
100
    free_list = item;
101
}
102
103
/* return one item from the free list, allocating more if needed */
104
void *_ctypes_alloc_closure(void)
105
{
106
    ITEM *item;
107
    if (!free_list)
108
        more_core();
109
    if (!free_list)
110
        return NULL;
111
    item = free_list;
112
    free_list = item->next;
113
    return item;
114
}
(-)Modules/_ctypes/_ctypes.c (-1 / +1 lines)
Lines 3463-3469 Link Here
3463
    self->callable = callable;
3463
    self->callable = callable;
3464
3464
3465
    self->thunk = thunk;
3465
    self->thunk = thunk;
3466
    *(void **)self->b_ptr = (void *)thunk->pcl;
3466
    *(void **)self->b_ptr = (void *)thunk->pcl_exec;
3467
3467
3468
    Py_INCREF((PyObject *)thunk); /* for KeepRef */
3468
    Py_INCREF((PyObject *)thunk); /* for KeepRef */
3469
    if (-1 == KeepRef((CDataObject *)self, 0, (PyObject *)thunk)) {
3469
    if (-1 == KeepRef((CDataObject *)self, 0, (PyObject *)thunk)) {
(-)Modules/_ctypes/ctypes.h (-1 / +2 lines)
Lines 95-101 Link Here
95
95
96
typedef struct {
96
typedef struct {
97
    PyObject_VAR_HEAD
97
    PyObject_VAR_HEAD
98
    ffi_closure *pcl; /* the C callable */
98
    ffi_closure *pcl_write; /* the C callable, writeable */
99
    void *pcl_exec;         /* the C callable, executable */
99
    ffi_cif cif;
100
    ffi_cif cif;
100
    int flags;
101
    int flags;
101
    PyObject *converters;
102
    PyObject *converters;
(-)Modules/_ctypes/callbacks.c (-6 / +10 lines)
Lines 21-28 Link Here
21
    Py_XDECREF(self->converters);
21
    Py_XDECREF(self->converters);
22
    Py_XDECREF(self->callable);
22
    Py_XDECREF(self->callable);
23
    Py_XDECREF(self->restype);
23
    Py_XDECREF(self->restype);
24
    if (self->pcl)
24
    if (self->pcl_write)
25
        _ctypes_free_closure(self->pcl);
25
        ffi_closure_free(self->pcl_write);
26
    PyObject_GC_Del(self);
26
    PyObject_GC_Del(self);
27
}
27
}
28
28
Lines 391-397 Link Here
391
        return NULL;
391
        return NULL;
392
    }
392
    }
393
393
394
    p->pcl = NULL;
394
    p->pcl_exec = NULL;
395
    p->pcl_write = NULL;
395
    memset(&p->cif, 0, sizeof(p->cif));
396
    memset(&p->cif, 0, sizeof(p->cif));
396
    p->converters = NULL;
397
    p->converters = NULL;
397
    p->callable = NULL;
398
    p->callable = NULL;
Lines 421-428 Link Here
421
422
422
    assert(CThunk_CheckExact(p));
423
    assert(CThunk_CheckExact(p));
423
424
424
    p->pcl = _ctypes_alloc_closure();
425
    p->pcl_write = ffi_closure_alloc(sizeof(ffi_closure),
425
    if (p->pcl == NULL) {
426
                                     &p->pcl_exec);
427
    if (p->pcl_write == NULL) {
426
        PyErr_NoMemory();
428
        PyErr_NoMemory();
427
        goto error;
429
        goto error;
428
    }
430
    }
Lines 467-473 Link Here
467
                     "ffi_prep_cif failed with %d", result);
469
                     "ffi_prep_cif failed with %d", result);
468
        goto error;
470
        goto error;
469
    }
471
    }
470
    result = ffi_prep_closure(p->pcl, &p->cif, closure_fcn, p);
472
    result = ffi_prep_closure_loc(p->pcl_write, &p->cif, closure_fcn,
473
                                  p,
474
                                  p->pcl_exec);
471
    if (result != FFI_OK) {
475
    if (result != FFI_OK) {
472
        PyErr_Format(PyExc_RuntimeError,
476
        PyErr_Format(PyExc_RuntimeError,
473
                     "ffi_prep_closure failed with %d", result);
477
                     "ffi_prep_closure failed with %d", result);

Return to bug 329499