Debian Codegraph @

pypy/module/cpyext/src/intobject.c

raw symbols SCIP
1
2 /* Integer object implementation -- copied&adapted from CPython */
3
4 #include "Python.h"
5
6 /* Integers are quite normal objects, to make object handling uniform.
7 (Using odd pointers to represent integers would save much space
8 but require extra checks for this special case throughout the code.)
9 Since a typical Python program spends much of its time allocating
10 and deallocating integers, these operations should be very fast.
11 Therefore we use a dedicated allocation scheme with a much lower
12 overhead (in space and time) than straight malloc(): a simple
13 dedicated free list, filled when necessary with memory from malloc().
14
15 block_list is a singly-linked list of all PyIntBlocks ever allocated,
16 linked via their next members. PyIntBlocks are never returned to the
17 system before shutdown (PyInt_Fini).
18
19 free_list is a singly-linked list of available PyIntObjects, linked
20 via abuse of their ob_type members.
21 */
22
23 #define BLOCK_SIZE 1000 /* 1K less typical malloc overhead */
24 #define BHEAD_SIZE 8 /* Enough for a 64-bit pointer */
25 #define N_INTOBJECTS ((BLOCK_SIZE - BHEAD_SIZE) / sizeof(PyIntObject))
26
27 struct _intblock {
28 struct _intblock *next;
29 PyIntObject objects[N_INTOBJECTS];
30 };
31
32 typedef struct _intblock PyIntBlock;
33
34 static PyIntBlock *block_list = NULL;
35 static PyIntObject *free_list = NULL;
36
37 static PyIntObject *
38 fill_free_list(void)
39 {
40 PyIntObject *p, *q;
41 /* Python's object allocator isn't appropriate for large blocks. */
42 p = (PyIntObject *) PyMem_MALLOC(sizeof(PyIntBlock));
43 if (p == NULL)
44 return (PyIntObject *) PyErr_NoMemory();
45 ((PyIntBlock *)p)->next = block_list;
46 block_list = (PyIntBlock *)p;
47 /* Link the int objects together, from rear to front, then return
48 the address of the last int object in the block. */
49 p = &((PyIntBlock *)p)->objects[0];
50 q = p + N_INTOBJECTS;
51 while (--q > p)
52 Py_TYPE(q) = (struct _typeobject *)(q-1);
53 Py_TYPE(q) = NULL;
54 return p + N_INTOBJECTS - 1;
55 }
56
57 /* this is CPython's int_dealloc */
58 #ifdef CPYEXT_TESTS
59 #define _Py_int_dealloc _cpyexttest_int_dealloc
60 #ifdef __GNUC__
61 __attribute__((visibility("default")))
62 #else
63 __declspec(dllexport)
64 #endif
65 #else /* CPYEXT_TESTS */
66 #define _Py_int_dealloc _PyPy_int_dealloc
67 #endif /* CPYEXT_TESTS */
68 void
69 _Py_int_dealloc(PyObject *obj)
70 {
71 PyIntObject *v = (PyIntObject *)obj;
72 if (PyInt_CheckExact(v)) {
73 Py_TYPE(v) = (struct _typeobject *)free_list;
74 free_list = v;
75 }
76 else
77 Py_TYPE(v)->tp_free((PyObject *)v);
78 }