Mercurial > cpython-lru_cache-weakref
annotate Objects/obmalloc.c @ 39042:c02fa187e0f4 legacy-trunk
Part of SF patch #1484695. This removes dead code. The chksum was
already verified in .frombuf() on the lines above. If there was
a problem an exception is raised, so there was no way this condition
could have been true.
| author | Neal Norwitz <nnorwitz@gmail.com> |
|---|---|
| date | Mon, 10 Jul 2006 00:23:17 +0000 |
| parents | 82072a8f5267 |
| children | 321c075f95cf |
| rev | line source |
|---|---|
|
22469
8d0061493c57
Build obmalloc.c directly instead of #include'ing from object.c.
Tim Peters <tim.peters@gmail.com>
parents:
22465
diff
changeset
|
1 #include "Python.h" |
|
8d0061493c57
Build obmalloc.c directly instead of #include'ing from object.c.
Tim Peters <tim.peters@gmail.com>
parents:
22465
diff
changeset
|
2 |
|
8d0061493c57
Build obmalloc.c directly instead of #include'ing from object.c.
Tim Peters <tim.peters@gmail.com>
parents:
22465
diff
changeset
|
3 #ifdef WITH_PYMALLOC |
|
8d0061493c57
Build obmalloc.c directly instead of #include'ing from object.c.
Tim Peters <tim.peters@gmail.com>
parents:
22465
diff
changeset
|
4 |
|
16782
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
5 /* An object allocator for Python. |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
6 |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
7 Here is an introduction to the layers of the Python memory architecture, |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
8 showing where the object allocator is actually used (layer +2), It is |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
9 called for every object allocation and deallocation (PyObject_New/Del), |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
10 unless the object-specific allocators implement a proprietary allocation |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
11 scheme (ex.: ints use a simple free list). This is also the place where |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
12 the cyclic garbage collector operates selectively on container objects. |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
13 |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
14 |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
15 Object-specific allocators |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
16 _____ ______ ______ ________ |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
17 [ int ] [ dict ] [ list ] ... [ string ] Python core | |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
18 +3 | <----- Object-specific memory -----> | <-- Non-object memory --> | |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
19 _______________________________ | | |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
20 [ Python's object allocator ] | | |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
21 +2 | ####### Object memory ####### | <------ Internal buffers ------> | |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
22 ______________________________________________________________ | |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
23 [ Python's raw memory allocator (PyMem_ API) ] | |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
24 +1 | <----- Python memory (under PyMem manager's control) ------> | | |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
25 __________________________________________________________________ |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
26 [ Underlying general-purpose allocator (ex: C library malloc) ] |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
27 0 | <------ Virtual memory allocated for the python process -------> | |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
28 |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
29 ========================================================================= |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
30 _______________________________________________________________________ |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
31 [ OS-specific Virtual Memory Manager (VMM) ] |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
32 -1 | <--- Kernel dynamic storage allocation & management (page-based) ---> | |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
33 __________________________________ __________________________________ |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
34 [ ] [ ] |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
35 -2 | <-- Physical memory: ROM/RAM --> | | <-- Secondary storage (swap) --> | |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
36 |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
37 */ |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
38 /*==========================================================================*/ |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
39 |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
40 /* A fast, special-purpose memory allocator for small blocks, to be used |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
41 on top of a general-purpose malloc -- heavily based on previous art. */ |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
42 |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
43 /* Vladimir Marangozov -- August 2000 */ |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
44 |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
45 /* |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
46 * "Memory management is where the rubber meets the road -- if we do the wrong |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
47 * thing at any level, the results will not be good. And if we don't make the |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
48 * levels work well together, we are in serious trouble." (1) |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
49 * |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
50 * (1) Paul R. Wilson, Mark S. Johnstone, Michael Neely, and David Boles, |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
51 * "Dynamic Storage Allocation: A Survey and Critical Review", |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
52 * in Proc. 1995 Int'l. Workshop on Memory Management, September 1995. |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
53 */ |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
54 |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
55 /* #undef WITH_MEMORY_LIMITS */ /* disable mem limit checks */ |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
56 |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
57 /*==========================================================================*/ |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
58 |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
59 /* |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
60 * Allocation strategy abstract: |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
61 * |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
62 * For small requests, the allocator sub-allocates <Big> blocks of memory. |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
63 * Requests greater than 256 bytes are routed to the system's allocator. |
|
22470
0e4758ccfe2b
Just whitespace fiddling.
Tim Peters <tim.peters@gmail.com>
parents:
22469
diff
changeset
|
64 * |
|
16782
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
65 * Small requests are grouped in size classes spaced 8 bytes apart, due |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
66 * to the required valid alignment of the returned address. Requests of |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
67 * a particular size are serviced from memory pools of 4K (one VMM page). |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
68 * Pools are fragmented on demand and contain free lists of blocks of one |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
69 * particular size class. In other words, there is a fixed-size allocator |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
70 * for each size class. Free pools are shared by the different allocators |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
71 * thus minimizing the space reserved for a particular size class. |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
72 * |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
73 * This allocation strategy is a variant of what is known as "simple |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
74 * segregated storage based on array of free lists". The main drawback of |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
75 * simple segregated storage is that we might end up with lot of reserved |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
76 * memory for the different free lists, which degenerate in time. To avoid |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
77 * this, we partition each free list in pools and we share dynamically the |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
78 * reserved space between all free lists. This technique is quite efficient |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
79 * for memory intensive programs which allocate mainly small-sized blocks. |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
80 * |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
81 * For small requests we have the following table: |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
82 * |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
83 * Request in bytes Size of allocated block Size class idx |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
84 * ---------------------------------------------------------------- |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
85 * 1-8 8 0 |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
86 * 9-16 16 1 |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
87 * 17-24 24 2 |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
88 * 25-32 32 3 |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
89 * 33-40 40 4 |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
90 * 41-48 48 5 |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
91 * 49-56 56 6 |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
92 * 57-64 64 7 |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
93 * 65-72 72 8 |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
94 * ... ... ... |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
95 * 241-248 248 30 |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
96 * 249-256 256 31 |
|
22470
0e4758ccfe2b
Just whitespace fiddling.
Tim Peters <tim.peters@gmail.com>
parents:
22469
diff
changeset
|
97 * |
|
16782
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
98 * 0, 257 and up: routed to the underlying allocator. |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
99 */ |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
100 |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
101 /*==========================================================================*/ |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
102 |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
103 /* |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
104 * -- Main tunable settings section -- |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
105 */ |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
106 |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
107 /* |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
108 * Alignment of addresses returned to the user. 8-bytes alignment works |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
109 * on most current architectures (with 32-bit or 64-bit address busses). |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
110 * The alignment value is also used for grouping small requests in size |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
111 * classes spaced ALIGNMENT bytes apart. |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
112 * |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
113 * You shouldn't change this unless you know what you are doing. |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
114 */ |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
115 #define ALIGNMENT 8 /* must be 2^N */ |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
116 #define ALIGNMENT_SHIFT 3 |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
117 #define ALIGNMENT_MASK (ALIGNMENT - 1) |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
118 |
|
22836
f1f57bf09092
Widespread, but mostly in _PyMalloc_Malloc: optimize away all expensive
Tim Peters <tim.peters@gmail.com>
parents:
22795
diff
changeset
|
119 /* Return the number of bytes in size class I, as a uint. */ |
|
f1f57bf09092
Widespread, but mostly in _PyMalloc_Malloc: optimize away all expensive
Tim Peters <tim.peters@gmail.com>
parents:
22795
diff
changeset
|
120 #define INDEX2SIZE(I) (((uint)(I) + 1) << ALIGNMENT_SHIFT) |
|
f1f57bf09092
Widespread, but mostly in _PyMalloc_Malloc: optimize away all expensive
Tim Peters <tim.peters@gmail.com>
parents:
22795
diff
changeset
|
121 |
|
16782
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
122 /* |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
123 * Max size threshold below which malloc requests are considered to be |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
124 * small enough in order to use preallocated memory pools. You can tune |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
125 * this value according to your application behaviour and memory needs. |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
126 * |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
127 * The following invariants must hold: |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
128 * 1) ALIGNMENT <= SMALL_REQUEST_THRESHOLD <= 256 |
| 22684 | 129 * 2) SMALL_REQUEST_THRESHOLD is evenly divisible by ALIGNMENT |
|
16782
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
130 * |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
131 * Although not required, for better performance and space efficiency, |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
132 * it is recommended that SMALL_REQUEST_THRESHOLD is set to a power of 2. |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
133 */ |
| 22684 | 134 #define SMALL_REQUEST_THRESHOLD 256 |
|
16782
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
135 #define NB_SMALL_SIZE_CLASSES (SMALL_REQUEST_THRESHOLD / ALIGNMENT) |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
136 |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
137 /* |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
138 * The system's VMM page size can be obtained on most unices with a |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
139 * getpagesize() call or deduced from various header files. To make |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
140 * things simpler, we assume that it is 4K, which is OK for most systems. |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
141 * It is probably better if this is the native page size, but it doesn't |
| 35222 | 142 * have to be. In theory, if SYSTEM_PAGE_SIZE is larger than the native page |
| 143 * size, then `POOL_ADDR(p)->arenaindex' could rarely cause a segmentation | |
| 144 * violation fault. 4K is apparently OK for all the platforms that python | |
|
25928
75756c8558bd
Patch #627105: Document that SYSTEM_PAGE_SIZE really should not be
Martin v. Löwis <martin@v.loewis.de>
parents:
25233
diff
changeset
|
145 * currently targets. |
|
16782
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
146 */ |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
147 #define SYSTEM_PAGE_SIZE (4 * 1024) |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
148 #define SYSTEM_PAGE_SIZE_MASK (SYSTEM_PAGE_SIZE - 1) |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
149 |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
150 /* |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
151 * Maximum amount of memory managed by the allocator for small requests. |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
152 */ |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
153 #ifdef WITH_MEMORY_LIMITS |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
154 #ifndef SMALL_MEMORY_LIMIT |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
155 #define SMALL_MEMORY_LIMIT (64 * 1024 * 1024) /* 64 MB -- more? */ |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
156 #endif |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
157 #endif |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
158 |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
159 /* |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
160 * The allocator sub-allocates <Big> blocks of memory (called arenas) aligned |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
161 * on a page boundary. This is a reserved virtual address space for the |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
162 * current process (obtained through a malloc call). In no way this means |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
163 * that the memory arenas will be used entirely. A malloc(<Big>) is usually |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
164 * an address range reservation for <Big> bytes, unless all pages within this |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
165 * space are referenced subsequently. So malloc'ing big blocks and not using |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
166 * them does not mean "wasting memory". It's an addressable range wastage... |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
167 * |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
168 * Therefore, allocating arenas with malloc is not optimal, because there is |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
169 * some address space wastage, but this is the most portable way to request |
| 22684 | 170 * memory from the system across various platforms. |
|
16782
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
171 */ |
|
22686
f8bd930644aa
Now that we're no longer linking arenas together, there's no need to
Tim Peters <tim.peters@gmail.com>
parents:
22685
diff
changeset
|
172 #define ARENA_SIZE (256 << 10) /* 256KB */ |
|
16782
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
173 |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
174 #ifdef WITH_MEMORY_LIMITS |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
175 #define MAX_ARENAS (SMALL_MEMORY_LIMIT / ARENA_SIZE) |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
176 #endif |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
177 |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
178 /* |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
179 * Size of the pools used for small blocks. Should be a power of 2, |
|
22692
2c02cf278f02
It's once again thought safe to call the pymalloc free/realloc with an
Tim Peters <tim.peters@gmail.com>
parents:
22691
diff
changeset
|
180 * between 1K and SYSTEM_PAGE_SIZE, that is: 1k, 2k, 4k. |
|
16782
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
181 */ |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
182 #define POOL_SIZE SYSTEM_PAGE_SIZE /* must be 2^N */ |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
183 #define POOL_SIZE_MASK SYSTEM_PAGE_SIZE_MASK |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
184 |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
185 /* |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
186 * -- End of tunable settings section -- |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
187 */ |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
188 |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
189 /*==========================================================================*/ |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
190 |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
191 /* |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
192 * Locking |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
193 * |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
194 * To reduce lock contention, it would probably be better to refine the |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
195 * crude function locking with per size class locking. I'm not positive |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
196 * however, whether it's worth switching to such locking policy because |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
197 * of the performance penalty it might introduce. |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
198 * |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
199 * The following macros describe the simplest (should also be the fastest) |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
200 * lock object on a particular platform and the init/fini/lock/unlock |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
201 * operations on it. The locks defined here are not expected to be recursive |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
202 * because it is assumed that they will always be called in the order: |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
203 * INIT, [LOCK, UNLOCK]*, FINI. |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
204 */ |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
205 |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
206 /* |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
207 * Python's threads are serialized, so object malloc locking is disabled. |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
208 */ |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
209 #define SIMPLELOCK_DECL(lock) /* simple lock declaration */ |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
210 #define SIMPLELOCK_INIT(lock) /* allocate (if needed) and initialize */ |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
211 #define SIMPLELOCK_FINI(lock) /* free/destroy an existing lock */ |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
212 #define SIMPLELOCK_LOCK(lock) /* acquire released lock */ |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
213 #define SIMPLELOCK_UNLOCK(lock) /* release acquired lock */ |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
214 |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
215 /* |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
216 * Basic types |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
217 * I don't care if these are defined in <sys/types.h> or elsewhere. Axiom. |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
218 */ |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
219 #undef uchar |
|
37008
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
220 #define uchar unsigned char /* assuming == 8 bits */ |
|
16782
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
221 |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
222 #undef uint |
|
37008
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
223 #define uint unsigned int /* assuming >= 16 bits */ |
|
16782
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
224 |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
225 #undef ulong |
|
37008
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
226 #define ulong unsigned long /* assuming >= 32 bits */ |
|
16782
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
227 |
| 22684 | 228 #undef uptr |
|
37008
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
229 #define uptr Py_uintptr_t |
| 22684 | 230 |
|
16782
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
231 /* When you say memory, my mind reasons in terms of (pointers to) blocks */ |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
232 typedef uchar block; |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
233 |
|
22836
f1f57bf09092
Widespread, but mostly in _PyMalloc_Malloc: optimize away all expensive
Tim Peters <tim.peters@gmail.com>
parents:
22795
diff
changeset
|
234 /* Pool for small blocks. */ |
|
16782
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
235 struct pool_header { |
|
17030
3ec500403f01
Identifiers matching _[A-Z_]\w* are reserved for C implementations.
Tim Peters <tim.peters@gmail.com>
parents:
16782
diff
changeset
|
236 union { block *_padding; |
|
16782
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
237 uint count; } ref; /* number of allocated blocks */ |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
238 block *freeblock; /* pool's free list head */ |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
239 struct pool_header *nextpool; /* next pool of this size class */ |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
240 struct pool_header *prevpool; /* previous pool "" */ |
|
22690
00b2972ab4a4
Changed the #-of-arenas counters to uints -- no need to be insane about
Tim Peters <tim.peters@gmail.com>
parents:
22687
diff
changeset
|
241 uint arenaindex; /* index into arenas of base adr */ |
|
16782
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
242 uint szidx; /* block size class index */ |
|
22836
f1f57bf09092
Widespread, but mostly in _PyMalloc_Malloc: optimize away all expensive
Tim Peters <tim.peters@gmail.com>
parents:
22795
diff
changeset
|
243 uint nextoffset; /* bytes to virgin block */ |
|
f1f57bf09092
Widespread, but mostly in _PyMalloc_Malloc: optimize away all expensive
Tim Peters <tim.peters@gmail.com>
parents:
22795
diff
changeset
|
244 uint maxnextoffset; /* largest valid nextoffset */ |
|
16782
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
245 }; |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
246 |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
247 typedef struct pool_header *poolp; |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
248 |
|
37008
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
249 /* Record keeping for arenas. */ |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
250 struct arena_object { |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
251 /* The address of the arena, as returned by malloc. Note that 0 |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
252 * will never be returned by a successful malloc, and is used |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
253 * here to mark an arena_object that doesn't correspond to an |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
254 * allocated arena. |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
255 */ |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
256 uptr address; |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
257 |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
258 /* Pool-aligned pointer to the next pool to be carved off. */ |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
259 block* pool_address; |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
260 |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
261 /* The number of available pools in the arena: free pools + never- |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
262 * allocated pools. |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
263 */ |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
264 uint nfreepools; |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
265 |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
266 /* The total number of pools in the arena, whether or not available. */ |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
267 uint ntotalpools; |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
268 |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
269 /* Singly-linked list of available pools. */ |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
270 struct pool_header* freepools; |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
271 |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
272 /* Whenever this arena_object is not associated with an allocated |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
273 * arena, the nextarena member is used to link all unassociated |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
274 * arena_objects in the singly-linked `unused_arena_objects` list. |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
275 * The prevarena member is unused in this case. |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
276 * |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
277 * When this arena_object is associated with an allocated arena |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
278 * with at least one available pool, both members are used in the |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
279 * doubly-linked `usable_arenas` list, which is maintained in |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
280 * increasing order of `nfreepools` values. |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
281 * |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
282 * Else this arena_object is associated with an allocated arena |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
283 * all of whose pools are in use. `nextarena` and `prevarena` |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
284 * are both meaningless in this case. |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
285 */ |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
286 struct arena_object* nextarena; |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
287 struct arena_object* prevarena; |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
288 }; |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
289 |
|
16782
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
290 #undef ROUNDUP |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
291 #define ROUNDUP(x) (((x) + ALIGNMENT_MASK) & ~ALIGNMENT_MASK) |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
292 #define POOL_OVERHEAD ROUNDUP(sizeof(struct pool_header)) |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
293 |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
294 #define DUMMY_SIZE_IDX 0xffff /* size class of newly cached pools */ |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
295 |
| 22684 | 296 /* Round pointer P down to the closest pool-aligned address <= P, as a poolp */ |
|
22836
f1f57bf09092
Widespread, but mostly in _PyMalloc_Malloc: optimize away all expensive
Tim Peters <tim.peters@gmail.com>
parents:
22795
diff
changeset
|
297 #define POOL_ADDR(P) ((poolp)((uptr)(P) & ~(uptr)POOL_SIZE_MASK)) |
|
f1f57bf09092
Widespread, but mostly in _PyMalloc_Malloc: optimize away all expensive
Tim Peters <tim.peters@gmail.com>
parents:
22795
diff
changeset
|
298 |
|
22837
ef2fbce71736
_PyMalloc_DebugDumpStats(): vastly improved the output, and it now
Tim Peters <tim.peters@gmail.com>
parents:
22836
diff
changeset
|
299 /* Return total number of blocks in pool of size index I, as a uint. */ |
|
ef2fbce71736
_PyMalloc_DebugDumpStats(): vastly improved the output, and it now
Tim Peters <tim.peters@gmail.com>
parents:
22836
diff
changeset
|
300 #define NUMBLOCKS(I) ((uint)(POOL_SIZE - POOL_OVERHEAD) / INDEX2SIZE(I)) |
| 22684 | 301 |
|
16782
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
302 /*==========================================================================*/ |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
303 |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
304 /* |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
305 * This malloc lock |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
306 */ |
|
24294
0a08ed05bd42
Remove extraneous semicolon.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
24177
diff
changeset
|
307 SIMPLELOCK_DECL(_malloc_lock) |
|
17030
3ec500403f01
Identifiers matching _[A-Z_]\w* are reserved for C implementations.
Tim Peters <tim.peters@gmail.com>
parents:
16782
diff
changeset
|
308 #define LOCK() SIMPLELOCK_LOCK(_malloc_lock) |
|
3ec500403f01
Identifiers matching _[A-Z_]\w* are reserved for C implementations.
Tim Peters <tim.peters@gmail.com>
parents:
16782
diff
changeset
|
309 #define UNLOCK() SIMPLELOCK_UNLOCK(_malloc_lock) |
|
3ec500403f01
Identifiers matching _[A-Z_]\w* are reserved for C implementations.
Tim Peters <tim.peters@gmail.com>
parents:
16782
diff
changeset
|
310 #define LOCK_INIT() SIMPLELOCK_INIT(_malloc_lock) |
|
3ec500403f01
Identifiers matching _[A-Z_]\w* are reserved for C implementations.
Tim Peters <tim.peters@gmail.com>
parents:
16782
diff
changeset
|
311 #define LOCK_FINI() SIMPLELOCK_FINI(_malloc_lock) |
|
16782
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
312 |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
313 /* |
|
22697
6bb4386b1111
Added a long-overdue comment block giving an overview of pool operations
Tim Peters <tim.peters@gmail.com>
parents:
22692
diff
changeset
|
314 * Pool table -- headed, circular, doubly-linked lists of partially used pools. |
|
6bb4386b1111
Added a long-overdue comment block giving an overview of pool operations
Tim Peters <tim.peters@gmail.com>
parents:
22692
diff
changeset
|
315 |
|
6bb4386b1111
Added a long-overdue comment block giving an overview of pool operations
Tim Peters <tim.peters@gmail.com>
parents:
22692
diff
changeset
|
316 This is involved. For an index i, usedpools[i+i] is the header for a list of |
|
6bb4386b1111
Added a long-overdue comment block giving an overview of pool operations
Tim Peters <tim.peters@gmail.com>
parents:
22692
diff
changeset
|
317 all partially used pools holding small blocks with "size class idx" i. So |
|
6bb4386b1111
Added a long-overdue comment block giving an overview of pool operations
Tim Peters <tim.peters@gmail.com>
parents:
22692
diff
changeset
|
318 usedpools[0] corresponds to blocks of size 8, usedpools[2] to blocks of size |
|
6bb4386b1111
Added a long-overdue comment block giving an overview of pool operations
Tim Peters <tim.peters@gmail.com>
parents:
22692
diff
changeset
|
319 16, and so on: index 2*i <-> blocks of size (i+1)<<ALIGNMENT_SHIFT. |
|
6bb4386b1111
Added a long-overdue comment block giving an overview of pool operations
Tim Peters <tim.peters@gmail.com>
parents:
22692
diff
changeset
|
320 |
|
37008
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
321 Pools are carved off an arena's highwater mark (an arena_object's pool_address |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
322 member) as needed. Once carved off, a pool is in one of three states forever |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
323 after: |
|
22697
6bb4386b1111
Added a long-overdue comment block giving an overview of pool operations
Tim Peters <tim.peters@gmail.com>
parents:
22692
diff
changeset
|
324 |
|
22741
771b2a8d445d
Restructured my pool-management overview in terms of the three
Tim Peters <tim.peters@gmail.com>
parents:
22724
diff
changeset
|
325 used == partially used, neither empty nor full |
|
771b2a8d445d
Restructured my pool-management overview in terms of the three
Tim Peters <tim.peters@gmail.com>
parents:
22724
diff
changeset
|
326 At least one block in the pool is currently allocated, and at least one |
|
771b2a8d445d
Restructured my pool-management overview in terms of the three
Tim Peters <tim.peters@gmail.com>
parents:
22724
diff
changeset
|
327 block in the pool is not currently allocated (note this implies a pool |
|
771b2a8d445d
Restructured my pool-management overview in terms of the three
Tim Peters <tim.peters@gmail.com>
parents:
22724
diff
changeset
|
328 has room for at least two blocks). |
|
771b2a8d445d
Restructured my pool-management overview in terms of the three
Tim Peters <tim.peters@gmail.com>
parents:
22724
diff
changeset
|
329 This is a pool's initial state, as a pool is created only when malloc |
|
771b2a8d445d
Restructured my pool-management overview in terms of the three
Tim Peters <tim.peters@gmail.com>
parents:
22724
diff
changeset
|
330 needs space. |
|
771b2a8d445d
Restructured my pool-management overview in terms of the three
Tim Peters <tim.peters@gmail.com>
parents:
22724
diff
changeset
|
331 The pool holds blocks of a fixed size, and is in the circular list headed |
|
771b2a8d445d
Restructured my pool-management overview in terms of the three
Tim Peters <tim.peters@gmail.com>
parents:
22724
diff
changeset
|
332 at usedpools[i] (see above). It's linked to the other used pools of the |
|
771b2a8d445d
Restructured my pool-management overview in terms of the three
Tim Peters <tim.peters@gmail.com>
parents:
22724
diff
changeset
|
333 same size class via the pool_header's nextpool and prevpool members. |
|
771b2a8d445d
Restructured my pool-management overview in terms of the three
Tim Peters <tim.peters@gmail.com>
parents:
22724
diff
changeset
|
334 If all but one block is currently allocated, a malloc can cause a |
|
771b2a8d445d
Restructured my pool-management overview in terms of the three
Tim Peters <tim.peters@gmail.com>
parents:
22724
diff
changeset
|
335 transition to the full state. If all but one block is not currently |
|
771b2a8d445d
Restructured my pool-management overview in terms of the three
Tim Peters <tim.peters@gmail.com>
parents:
22724
diff
changeset
|
336 allocated, a free can cause a transition to the empty state. |
|
771b2a8d445d
Restructured my pool-management overview in terms of the three
Tim Peters <tim.peters@gmail.com>
parents:
22724
diff
changeset
|
337 |
|
771b2a8d445d
Restructured my pool-management overview in terms of the three
Tim Peters <tim.peters@gmail.com>
parents:
22724
diff
changeset
|
338 full == all the pool's blocks are currently allocated |
|
771b2a8d445d
Restructured my pool-management overview in terms of the three
Tim Peters <tim.peters@gmail.com>
parents:
22724
diff
changeset
|
339 On transition to full, a pool is unlinked from its usedpools[] list. |
|
771b2a8d445d
Restructured my pool-management overview in terms of the three
Tim Peters <tim.peters@gmail.com>
parents:
22724
diff
changeset
|
340 It's not linked to from anything then anymore, and its nextpool and |
|
771b2a8d445d
Restructured my pool-management overview in terms of the three
Tim Peters <tim.peters@gmail.com>
parents:
22724
diff
changeset
|
341 prevpool members are meaningless until it transitions back to used. |
|
771b2a8d445d
Restructured my pool-management overview in terms of the three
Tim Peters <tim.peters@gmail.com>
parents:
22724
diff
changeset
|
342 A free of a block in a full pool puts the pool back in the used state. |
|
771b2a8d445d
Restructured my pool-management overview in terms of the three
Tim Peters <tim.peters@gmail.com>
parents:
22724
diff
changeset
|
343 Then it's linked in at the front of the appropriate usedpools[] list, so |
|
771b2a8d445d
Restructured my pool-management overview in terms of the three
Tim Peters <tim.peters@gmail.com>
parents:
22724
diff
changeset
|
344 that the next allocation for its size class will reuse the freed block. |
|
22697
6bb4386b1111
Added a long-overdue comment block giving an overview of pool operations
Tim Peters <tim.peters@gmail.com>
parents:
22692
diff
changeset
|
345 |
|
22741
771b2a8d445d
Restructured my pool-management overview in terms of the three
Tim Peters <tim.peters@gmail.com>
parents:
22724
diff
changeset
|
346 empty == all the pool's blocks are currently available for allocation |
|
771b2a8d445d
Restructured my pool-management overview in terms of the three
Tim Peters <tim.peters@gmail.com>
parents:
22724
diff
changeset
|
347 On transition to empty, a pool is unlinked from its usedpools[] list, |
|
37008
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
348 and linked to the front of its arena_object's singly-linked freepools list, |
|
22741
771b2a8d445d
Restructured my pool-management overview in terms of the three
Tim Peters <tim.peters@gmail.com>
parents:
22724
diff
changeset
|
349 via its nextpool member. The prevpool member has no meaning in this case. |
|
771b2a8d445d
Restructured my pool-management overview in terms of the three
Tim Peters <tim.peters@gmail.com>
parents:
22724
diff
changeset
|
350 Empty pools have no inherent size class: the next time a malloc finds |
|
771b2a8d445d
Restructured my pool-management overview in terms of the three
Tim Peters <tim.peters@gmail.com>
parents:
22724
diff
changeset
|
351 an empty list in usedpools[], it takes the first pool off of freepools. |
|
771b2a8d445d
Restructured my pool-management overview in terms of the three
Tim Peters <tim.peters@gmail.com>
parents:
22724
diff
changeset
|
352 If the size class needed happens to be the same as the size class the pool |
|
22836
f1f57bf09092
Widespread, but mostly in _PyMalloc_Malloc: optimize away all expensive
Tim Peters <tim.peters@gmail.com>
parents:
22795
diff
changeset
|
353 last had, some pool initialization can be skipped. |
|
22741
771b2a8d445d
Restructured my pool-management overview in terms of the three
Tim Peters <tim.peters@gmail.com>
parents:
22724
diff
changeset
|
354 |
|
771b2a8d445d
Restructured my pool-management overview in terms of the three
Tim Peters <tim.peters@gmail.com>
parents:
22724
diff
changeset
|
355 |
|
771b2a8d445d
Restructured my pool-management overview in terms of the three
Tim Peters <tim.peters@gmail.com>
parents:
22724
diff
changeset
|
356 Block Management |
|
771b2a8d445d
Restructured my pool-management overview in terms of the three
Tim Peters <tim.peters@gmail.com>
parents:
22724
diff
changeset
|
357 |
|
771b2a8d445d
Restructured my pool-management overview in terms of the three
Tim Peters <tim.peters@gmail.com>
parents:
22724
diff
changeset
|
358 Blocks within pools are again carved out as needed. pool->freeblock points to |
|
771b2a8d445d
Restructured my pool-management overview in terms of the three
Tim Peters <tim.peters@gmail.com>
parents:
22724
diff
changeset
|
359 the start of a singly-linked list of free blocks within the pool. When a |
|
771b2a8d445d
Restructured my pool-management overview in terms of the three
Tim Peters <tim.peters@gmail.com>
parents:
22724
diff
changeset
|
360 block is freed, it's inserted at the front of its pool's freeblock list. Note |
|
771b2a8d445d
Restructured my pool-management overview in terms of the three
Tim Peters <tim.peters@gmail.com>
parents:
22724
diff
changeset
|
361 that the available blocks in a pool are *not* linked all together when a pool |
|
22836
f1f57bf09092
Widespread, but mostly in _PyMalloc_Malloc: optimize away all expensive
Tim Peters <tim.peters@gmail.com>
parents:
22795
diff
changeset
|
362 is initialized. Instead only "the first two" (lowest addresses) blocks are |
|
f1f57bf09092
Widespread, but mostly in _PyMalloc_Malloc: optimize away all expensive
Tim Peters <tim.peters@gmail.com>
parents:
22795
diff
changeset
|
363 set up, returning the first such block, and setting pool->freeblock to a |
|
f1f57bf09092
Widespread, but mostly in _PyMalloc_Malloc: optimize away all expensive
Tim Peters <tim.peters@gmail.com>
parents:
22795
diff
changeset
|
364 one-block list holding the second such block. This is consistent with that |
|
f1f57bf09092
Widespread, but mostly in _PyMalloc_Malloc: optimize away all expensive
Tim Peters <tim.peters@gmail.com>
parents:
22795
diff
changeset
|
365 pymalloc strives at all levels (arena, pool, and block) never to touch a piece |
|
f1f57bf09092
Widespread, but mostly in _PyMalloc_Malloc: optimize away all expensive
Tim Peters <tim.peters@gmail.com>
parents:
22795
diff
changeset
|
366 of memory until it's actually needed. |
|
f1f57bf09092
Widespread, but mostly in _PyMalloc_Malloc: optimize away all expensive
Tim Peters <tim.peters@gmail.com>
parents:
22795
diff
changeset
|
367 |
|
f1f57bf09092
Widespread, but mostly in _PyMalloc_Malloc: optimize away all expensive
Tim Peters <tim.peters@gmail.com>
parents:
22795
diff
changeset
|
368 So long as a pool is in the used state, we're certain there *is* a block |
|
22948
6cb52829da1a
SF bug 542181: Realloc behavior
Tim Peters <tim.peters@gmail.com>
parents:
22860
diff
changeset
|
369 available for allocating, and pool->freeblock is not NULL. If pool->freeblock |
|
6cb52829da1a
SF bug 542181: Realloc behavior
Tim Peters <tim.peters@gmail.com>
parents:
22860
diff
changeset
|
370 points to the end of the free list before we've carved the entire pool into |
|
6cb52829da1a
SF bug 542181: Realloc behavior
Tim Peters <tim.peters@gmail.com>
parents:
22860
diff
changeset
|
371 blocks, that means we simply haven't yet gotten to one of the higher-address |
|
6cb52829da1a
SF bug 542181: Realloc behavior
Tim Peters <tim.peters@gmail.com>
parents:
22860
diff
changeset
|
372 blocks. The offset from the pool_header to the start of "the next" virgin |
|
6cb52829da1a
SF bug 542181: Realloc behavior
Tim Peters <tim.peters@gmail.com>
parents:
22860
diff
changeset
|
373 block is stored in the pool_header nextoffset member, and the largest value |
|
6cb52829da1a
SF bug 542181: Realloc behavior
Tim Peters <tim.peters@gmail.com>
parents:
22860
diff
changeset
|
374 of nextoffset that makes sense is stored in the maxnextoffset member when a |
|
6cb52829da1a
SF bug 542181: Realloc behavior
Tim Peters <tim.peters@gmail.com>
parents:
22860
diff
changeset
|
375 pool is initialized. All the blocks in a pool have been passed out at least |
|
6cb52829da1a
SF bug 542181: Realloc behavior
Tim Peters <tim.peters@gmail.com>
parents:
22860
diff
changeset
|
376 once when and only when nextoffset > maxnextoffset. |
|
22741
771b2a8d445d
Restructured my pool-management overview in terms of the three
Tim Peters <tim.peters@gmail.com>
parents:
22724
diff
changeset
|
377 |
|
22697
6bb4386b1111
Added a long-overdue comment block giving an overview of pool operations
Tim Peters <tim.peters@gmail.com>
parents:
22692
diff
changeset
|
378 |
|
6bb4386b1111
Added a long-overdue comment block giving an overview of pool operations
Tim Peters <tim.peters@gmail.com>
parents:
22692
diff
changeset
|
379 Major obscurity: While the usedpools vector is declared to have poolp |
|
6bb4386b1111
Added a long-overdue comment block giving an overview of pool operations
Tim Peters <tim.peters@gmail.com>
parents:
22692
diff
changeset
|
380 entries, it doesn't really. It really contains two pointers per (conceptual) |
|
6bb4386b1111
Added a long-overdue comment block giving an overview of pool operations
Tim Peters <tim.peters@gmail.com>
parents:
22692
diff
changeset
|
381 poolp entry, the nextpool and prevpool members of a pool_header. The |
|
6bb4386b1111
Added a long-overdue comment block giving an overview of pool operations
Tim Peters <tim.peters@gmail.com>
parents:
22692
diff
changeset
|
382 excruciating initialization code below fools C so that |
|
6bb4386b1111
Added a long-overdue comment block giving an overview of pool operations
Tim Peters <tim.peters@gmail.com>
parents:
22692
diff
changeset
|
383 |
|
6bb4386b1111
Added a long-overdue comment block giving an overview of pool operations
Tim Peters <tim.peters@gmail.com>
parents:
22692
diff
changeset
|
384 usedpool[i+i] |
|
6bb4386b1111
Added a long-overdue comment block giving an overview of pool operations
Tim Peters <tim.peters@gmail.com>
parents:
22692
diff
changeset
|
385 |
|
6bb4386b1111
Added a long-overdue comment block giving an overview of pool operations
Tim Peters <tim.peters@gmail.com>
parents:
22692
diff
changeset
|
386 "acts like" a genuine poolp, but only so long as you only reference its |
|
6bb4386b1111
Added a long-overdue comment block giving an overview of pool operations
Tim Peters <tim.peters@gmail.com>
parents:
22692
diff
changeset
|
387 nextpool and prevpool members. The "- 2*sizeof(block *)" gibberish is |
|
6bb4386b1111
Added a long-overdue comment block giving an overview of pool operations
Tim Peters <tim.peters@gmail.com>
parents:
22692
diff
changeset
|
388 compensating for that a pool_header's nextpool and prevpool members |
|
6bb4386b1111
Added a long-overdue comment block giving an overview of pool operations
Tim Peters <tim.peters@gmail.com>
parents:
22692
diff
changeset
|
389 immediately follow a pool_header's first two members: |
|
6bb4386b1111
Added a long-overdue comment block giving an overview of pool operations
Tim Peters <tim.peters@gmail.com>
parents:
22692
diff
changeset
|
390 |
|
6bb4386b1111
Added a long-overdue comment block giving an overview of pool operations
Tim Peters <tim.peters@gmail.com>
parents:
22692
diff
changeset
|
391 union { block *_padding; |
|
6bb4386b1111
Added a long-overdue comment block giving an overview of pool operations
Tim Peters <tim.peters@gmail.com>
parents:
22692
diff
changeset
|
392 uint count; } ref; |
|
6bb4386b1111
Added a long-overdue comment block giving an overview of pool operations
Tim Peters <tim.peters@gmail.com>
parents:
22692
diff
changeset
|
393 block *freeblock; |
|
6bb4386b1111
Added a long-overdue comment block giving an overview of pool operations
Tim Peters <tim.peters@gmail.com>
parents:
22692
diff
changeset
|
394 |
|
6bb4386b1111
Added a long-overdue comment block giving an overview of pool operations
Tim Peters <tim.peters@gmail.com>
parents:
22692
diff
changeset
|
395 each of which consume sizeof(block *) bytes. So what usedpools[i+i] really |
|
6bb4386b1111
Added a long-overdue comment block giving an overview of pool operations
Tim Peters <tim.peters@gmail.com>
parents:
22692
diff
changeset
|
396 contains is a fudged-up pointer p such that *if* C believes it's a poolp |
|
6bb4386b1111
Added a long-overdue comment block giving an overview of pool operations
Tim Peters <tim.peters@gmail.com>
parents:
22692
diff
changeset
|
397 pointer, then p->nextpool and p->prevpool are both p (meaning that the headed |
|
6bb4386b1111
Added a long-overdue comment block giving an overview of pool operations
Tim Peters <tim.peters@gmail.com>
parents:
22692
diff
changeset
|
398 circular list is empty). |
|
6bb4386b1111
Added a long-overdue comment block giving an overview of pool operations
Tim Peters <tim.peters@gmail.com>
parents:
22692
diff
changeset
|
399 |
|
6bb4386b1111
Added a long-overdue comment block giving an overview of pool operations
Tim Peters <tim.peters@gmail.com>
parents:
22692
diff
changeset
|
400 It's unclear why the usedpools setup is so convoluted. It could be to |
|
6bb4386b1111
Added a long-overdue comment block giving an overview of pool operations
Tim Peters <tim.peters@gmail.com>
parents:
22692
diff
changeset
|
401 minimize the amount of cache required to hold this heavily-referenced table |
|
6bb4386b1111
Added a long-overdue comment block giving an overview of pool operations
Tim Peters <tim.peters@gmail.com>
parents:
22692
diff
changeset
|
402 (which only *needs* the two interpool pointer members of a pool_header). OTOH, |
|
6bb4386b1111
Added a long-overdue comment block giving an overview of pool operations
Tim Peters <tim.peters@gmail.com>
parents:
22692
diff
changeset
|
403 referencing code has to remember to "double the index" and doing so isn't |
|
6bb4386b1111
Added a long-overdue comment block giving an overview of pool operations
Tim Peters <tim.peters@gmail.com>
parents:
22692
diff
changeset
|
404 free, usedpools[0] isn't a strictly legal pointer, and we're crucially relying |
|
6bb4386b1111
Added a long-overdue comment block giving an overview of pool operations
Tim Peters <tim.peters@gmail.com>
parents:
22692
diff
changeset
|
405 on that C doesn't insert any padding anywhere in a pool_header at or before |
|
6bb4386b1111
Added a long-overdue comment block giving an overview of pool operations
Tim Peters <tim.peters@gmail.com>
parents:
22692
diff
changeset
|
406 the prevpool member. |
|
6bb4386b1111
Added a long-overdue comment block giving an overview of pool operations
Tim Peters <tim.peters@gmail.com>
parents:
22692
diff
changeset
|
407 **************************************************************************** */ |
|
6bb4386b1111
Added a long-overdue comment block giving an overview of pool operations
Tim Peters <tim.peters@gmail.com>
parents:
22692
diff
changeset
|
408 |
|
16782
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
409 #define PTA(x) ((poolp )((uchar *)&(usedpools[2*(x)]) - 2*sizeof(block *))) |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
410 #define PT(x) PTA(x), PTA(x) |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
411 |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
412 static poolp usedpools[2 * ((NB_SMALL_SIZE_CLASSES + 7) / 8) * 8] = { |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
413 PT(0), PT(1), PT(2), PT(3), PT(4), PT(5), PT(6), PT(7) |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
414 #if NB_SMALL_SIZE_CLASSES > 8 |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
415 , PT(8), PT(9), PT(10), PT(11), PT(12), PT(13), PT(14), PT(15) |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
416 #if NB_SMALL_SIZE_CLASSES > 16 |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
417 , PT(16), PT(17), PT(18), PT(19), PT(20), PT(21), PT(22), PT(23) |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
418 #if NB_SMALL_SIZE_CLASSES > 24 |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
419 , PT(24), PT(25), PT(26), PT(27), PT(28), PT(29), PT(30), PT(31) |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
420 #if NB_SMALL_SIZE_CLASSES > 32 |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
421 , PT(32), PT(33), PT(34), PT(35), PT(36), PT(37), PT(38), PT(39) |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
422 #if NB_SMALL_SIZE_CLASSES > 40 |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
423 , PT(40), PT(41), PT(42), PT(43), PT(44), PT(45), PT(46), PT(47) |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
424 #if NB_SMALL_SIZE_CLASSES > 48 |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
425 , PT(48), PT(49), PT(50), PT(51), PT(52), PT(53), PT(54), PT(55) |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
426 #if NB_SMALL_SIZE_CLASSES > 56 |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
427 , PT(56), PT(57), PT(58), PT(59), PT(60), PT(61), PT(62), PT(63) |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
428 #endif /* NB_SMALL_SIZE_CLASSES > 56 */ |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
429 #endif /* NB_SMALL_SIZE_CLASSES > 48 */ |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
430 #endif /* NB_SMALL_SIZE_CLASSES > 40 */ |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
431 #endif /* NB_SMALL_SIZE_CLASSES > 32 */ |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
432 #endif /* NB_SMALL_SIZE_CLASSES > 24 */ |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
433 #endif /* NB_SMALL_SIZE_CLASSES > 16 */ |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
434 #endif /* NB_SMALL_SIZE_CLASSES > 8 */ |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
435 }; |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
436 |
|
37008
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
437 /*========================================================================== |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
438 Arena management. |
|
16782
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
439 |
|
37008
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
440 `arenas` is a vector of arena_objects. It contains maxarenas entries, some of |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
441 which may not be currently used (== they're arena_objects that aren't |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
442 currently associated with an allocated arena). Note that arenas proper are |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
443 separately malloc'ed. |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
444 |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
445 Prior to Python 2.5, arenas were never free()'ed. Starting with Python 2.5, |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
446 we do try to free() arenas, and use some mild heuristic strategies to increase |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
447 the likelihood that arenas eventually can be freed. |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
448 |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
449 unused_arena_objects |
| 22684 | 450 |
|
37008
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
451 This is a singly-linked list of the arena_objects that are currently not |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
452 being used (no arena is associated with them). Objects are taken off the |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
453 head of the list in new_arena(), and are pushed on the head of the list in |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
454 PyObject_Free() when the arena is empty. Key invariant: an arena_object |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
455 is on this list if and only if its .address member is 0. |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
456 |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
457 usable_arenas |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
458 |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
459 This is a doubly-linked list of the arena_objects associated with arenas |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
460 that have pools available. These pools are either waiting to be reused, |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
461 or have not been used before. The list is sorted to have the most- |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
462 allocated arenas first (ascending order based on the nfreepools member). |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
463 This means that the next allocation will come from a heavily used arena, |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
464 which gives the nearly empty arenas a chance to be returned to the system. |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
465 In my unscientific tests this dramatically improved the number of arenas |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
466 that could be freed. |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
467 |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
468 Note that an arena_object associated with an arena all of whose pools are |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
469 currently in use isn't on either list. |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
470 */ |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
471 |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
472 /* Array of objects used to track chunks of memory (arenas). */ |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
473 static struct arena_object* arenas = NULL; |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
474 /* Number of slots currently allocated in the `arenas` vector. */ |
|
22690
00b2972ab4a4
Changed the #-of-arenas counters to uints -- no need to be insane about
Tim Peters <tim.peters@gmail.com>
parents:
22687
diff
changeset
|
475 static uint maxarenas = 0; |
| 22684 | 476 |
|
37008
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
477 /* The head of the singly-linked, NULL-terminated list of available |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
478 * arena_objects. |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
479 */ |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
480 static struct arena_object* unused_arena_objects = NULL; |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
481 |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
482 /* The head of the doubly-linked, NULL-terminated at each end, list of |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
483 * arena_objects associated with arenas that have pools available. |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
484 */ |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
485 static struct arena_object* usable_arenas = NULL; |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
486 |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
487 /* How many arena_objects do we initially allocate? |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
488 * 16 = can allocate 16 arenas = 16 * ARENA_SIZE = 4MB before growing the |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
489 * `arenas` vector. |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
490 */ |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
491 #define INITIAL_ARENA_OBJECTS 16 |
| 22684 | 492 |
|
37008
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
493 /* Number of arenas allocated that haven't been free()'d. */ |
|
38575
5722f112d5da
In a PYMALLOC_DEBUG build obmalloc adds extra debugging info
Tim Peters <tim.peters@gmail.com>
parents:
37598
diff
changeset
|
494 static size_t narenas_currently_allocated = 0; |
| 22684 | 495 |
|
37008
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
496 #ifdef PYMALLOC_DEBUG |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
497 /* Total number of times malloc() called to allocate an arena. */ |
|
38575
5722f112d5da
In a PYMALLOC_DEBUG build obmalloc adds extra debugging info
Tim Peters <tim.peters@gmail.com>
parents:
37598
diff
changeset
|
498 static size_t ntimes_arena_allocated = 0; |
|
37008
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
499 /* High water mark (max value ever seen) for narenas_currently_allocated. */ |
|
38575
5722f112d5da
In a PYMALLOC_DEBUG build obmalloc adds extra debugging info
Tim Peters <tim.peters@gmail.com>
parents:
37598
diff
changeset
|
500 static size_t narenas_highwater = 0; |
|
37008
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
501 #endif |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
502 |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
503 /* Allocate a new arena. If we run out of memory, return NULL. Else |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
504 * allocate a new arena, and return the address of an arena_object |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
505 * describing the new arena. It's expected that the caller will set |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
506 * `usable_arenas` to the return value. |
|
16782
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
507 */ |
|
37008
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
508 static struct arena_object* |
| 22684 | 509 new_arena(void) |
| 510 { | |
|
37008
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
511 struct arena_object* arenaobj; |
|
22686
f8bd930644aa
Now that we're no longer linking arenas together, there's no need to
Tim Peters <tim.peters@gmail.com>
parents:
22685
diff
changeset
|
512 uint excess; /* number of bytes above pool alignment */ |
| 22684 | 513 |
|
23020
e2848618bd19
_PyObject_DebugDumpStats: renamed to _PyObject_DebugMallocStats.
Tim Peters <tim.peters@gmail.com>
parents:
23009
diff
changeset
|
514 #ifdef PYMALLOC_DEBUG |
|
e2848618bd19
_PyObject_DebugDumpStats: renamed to _PyObject_DebugMallocStats.
Tim Peters <tim.peters@gmail.com>
parents:
23009
diff
changeset
|
515 if (Py_GETENV("PYTHONMALLOCSTATS")) |
|
e2848618bd19
_PyObject_DebugDumpStats: renamed to _PyObject_DebugMallocStats.
Tim Peters <tim.peters@gmail.com>
parents:
23009
diff
changeset
|
516 _PyObject_DebugMallocStats(); |
|
e2848618bd19
_PyObject_DebugDumpStats: renamed to _PyObject_DebugMallocStats.
Tim Peters <tim.peters@gmail.com>
parents:
23009
diff
changeset
|
517 #endif |
|
37008
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
518 if (unused_arena_objects == NULL) { |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
519 uint i; |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
520 uint numarenas; |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
521 size_t nbytes; |
|
23020
e2848618bd19
_PyObject_DebugDumpStats: renamed to _PyObject_DebugMallocStats.
Tim Peters <tim.peters@gmail.com>
parents:
23009
diff
changeset
|
522 |
|
37008
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
523 /* Double the number of arena objects on each allocation. |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
524 * Note that it's possible for `numarenas` to overflow. |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
525 */ |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
526 numarenas = maxarenas ? maxarenas << 1 : INITIAL_ARENA_OBJECTS; |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
527 if (numarenas <= maxarenas) |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
528 return NULL; /* overflow */ |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
529 nbytes = numarenas * sizeof(*arenas); |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
530 if (nbytes / sizeof(*arenas) != numarenas) |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
531 return NULL; /* overflow */ |
| 37598 | 532 arenaobj = (struct arena_object *)realloc(arenas, nbytes); |
|
37008
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
533 if (arenaobj == NULL) |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
534 return NULL; |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
535 arenas = arenaobj; |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
536 |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
537 /* We might need to fix pointers that were copied. However, |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
538 * new_arena only gets called when all the pages in the |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
539 * previous arenas are full. Thus, there are *no* pointers |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
540 * into the old array. Thus, we don't have to worry about |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
541 * invalid pointers. Just to be sure, some asserts: |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
542 */ |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
543 assert(usable_arenas == NULL); |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
544 assert(unused_arena_objects == NULL); |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
545 |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
546 /* Put the new arenas on the unused_arena_objects list. */ |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
547 for (i = maxarenas; i < numarenas; ++i) { |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
548 arenas[i].address = 0; /* mark as unassociated */ |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
549 arenas[i].nextarena = i < numarenas - 1 ? |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
550 &arenas[i+1] : NULL; |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
551 } |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
552 |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
553 /* Update globals. */ |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
554 unused_arena_objects = &arenas[maxarenas]; |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
555 maxarenas = numarenas; |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
556 } |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
557 |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
558 /* Take the next available arena object off the head of the list. */ |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
559 assert(unused_arena_objects != NULL); |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
560 arenaobj = unused_arena_objects; |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
561 unused_arena_objects = arenaobj->nextarena; |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
562 assert(arenaobj->address == 0); |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
563 arenaobj->address = (uptr)malloc(ARENA_SIZE); |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
564 if (arenaobj->address == 0) { |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
565 /* The allocation failed: return NULL after putting the |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
566 * arenaobj back. |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
567 */ |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
568 arenaobj->nextarena = unused_arena_objects; |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
569 unused_arena_objects = arenaobj; |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
570 return NULL; |
|
22686
f8bd930644aa
Now that we're no longer linking arenas together, there's no need to
Tim Peters <tim.peters@gmail.com>
parents:
22685
diff
changeset
|
571 } |
|
16782
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
572 |
|
37008
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
573 ++narenas_currently_allocated; |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
574 #ifdef PYMALLOC_DEBUG |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
575 ++ntimes_arena_allocated; |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
576 if (narenas_currently_allocated > narenas_highwater) |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
577 narenas_highwater = narenas_currently_allocated; |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
578 #endif |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
579 arenaobj->freepools = NULL; |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
580 /* pool_address <- first pool-aligned address in the arena |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
581 nfreepools <- number of whole pools that fit after alignment */ |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
582 arenaobj->pool_address = (block*)arenaobj->address; |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
583 arenaobj->nfreepools = ARENA_SIZE / POOL_SIZE; |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
584 assert(POOL_SIZE * arenaobj->nfreepools == ARENA_SIZE); |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
585 excess = (uint)(arenaobj->address & POOL_SIZE_MASK); |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
586 if (excess != 0) { |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
587 --arenaobj->nfreepools; |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
588 arenaobj->pool_address += POOL_SIZE - excess; |
| 22684 | 589 } |
|
37008
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
590 arenaobj->ntotalpools = arenaobj->nfreepools; |
| 22684 | 591 |
|
37008
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
592 return arenaobj; |
| 22684 | 593 } |
| 594 | |
|
37008
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
595 /* |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
596 Py_ADDRESS_IN_RANGE(P, POOL) |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
597 |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
598 Return true if and only if P is an address that was allocated by pymalloc. |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
599 POOL must be the pool address associated with P, i.e., POOL = POOL_ADDR(P) |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
600 (the caller is asked to compute this because the macro expands POOL more than |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
601 once, and for efficiency it's best for the caller to assign POOL_ADDR(P) to a |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
602 variable and pass the latter to the macro; because Py_ADDRESS_IN_RANGE is |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
603 called on every alloc/realloc/free, micro-efficiency is important here). |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
604 |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
605 Tricky: Let B be the arena base address associated with the pool, B = |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
606 arenas[(POOL)->arenaindex].address. Then P belongs to the arena if and only if |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
607 |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
608 B <= P < B + ARENA_SIZE |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
609 |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
610 Subtracting B throughout, this is true iff |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
611 |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
612 0 <= P-B < ARENA_SIZE |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
613 |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
614 By using unsigned arithmetic, the "0 <=" half of the test can be skipped. |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
615 |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
616 Obscure: A PyMem "free memory" function can call the pymalloc free or realloc |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
617 before the first arena has been allocated. `arenas` is still NULL in that |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
618 case. We're relying on that maxarenas is also 0 in that case, so that |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
619 (POOL)->arenaindex < maxarenas must be false, saving us from trying to index |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
620 into a NULL arenas. |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
621 |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
622 Details: given P and POOL, the arena_object corresponding to P is AO = |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
623 arenas[(POOL)->arenaindex]. Suppose obmalloc controls P. Then (barring wild |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
624 stores, etc), POOL is the correct address of P's pool, AO.address is the |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
625 correct base address of the pool's arena, and P must be within ARENA_SIZE of |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
626 AO.address. In addition, AO.address is not 0 (no arena can start at address 0 |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
627 (NULL)). Therefore Py_ADDRESS_IN_RANGE correctly reports that obmalloc |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
628 controls P. |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
629 |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
630 Now suppose obmalloc does not control P (e.g., P was obtained via a direct |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
631 call to the system malloc() or realloc()). (POOL)->arenaindex may be anything |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
632 in this case -- it may even be uninitialized trash. If the trash arenaindex |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
633 is >= maxarenas, the macro correctly concludes at once that obmalloc doesn't |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
634 control P. |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
635 |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
636 Else arenaindex is < maxarena, and AO is read up. If AO corresponds to an |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
637 allocated arena, obmalloc controls all the memory in slice AO.address : |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
638 AO.address+ARENA_SIZE. By case assumption, P is not controlled by obmalloc, |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
639 so P doesn't lie in that slice, so the macro correctly reports that P is not |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
640 controlled by obmalloc. |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
641 |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
642 Finally, if P is not controlled by obmalloc and AO corresponds to an unused |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
643 arena_object (one not currently associated with an allocated arena), |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
644 AO.address is 0, and the second test in the macro reduces to: |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
645 |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
646 P < ARENA_SIZE |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
647 |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
648 If P >= ARENA_SIZE (extremely likely), the macro again correctly concludes |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
649 that P is not controlled by obmalloc. However, if P < ARENA_SIZE, this part |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
650 of the test still passes, and the third clause (AO.address != 0) is necessary |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
651 to get the correct result: AO.address is 0 in this case, so the macro |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
652 correctly reports that P is not controlled by obmalloc (despite that P lies in |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
653 slice AO.address : AO.address + ARENA_SIZE). |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
654 |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
655 Note: The third (AO.address != 0) clause was added in Python 2.5. Before |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
656 2.5, arenas were never free()'ed, and an arenaindex < maxarena always |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
657 corresponded to a currently-allocated arena, so the "P is not controlled by |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
658 obmalloc, AO corresponds to an unused arena_object, and P < ARENA_SIZE" case |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
659 was impossible. |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
660 |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
661 Note that the logic is excruciating, and reading up possibly uninitialized |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
662 memory when P is not controlled by obmalloc (to get at (POOL)->arenaindex) |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
663 creates problems for some memory debuggers. The overwhelming advantage is |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
664 that this test determines whether an arbitrary address is controlled by |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
665 obmalloc in a small constant time, independent of the number of arenas |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
666 obmalloc controls. Since this test is needed at every entry point, it's |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
667 extremely desirable that it be this fast. |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
668 */ |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
669 #define Py_ADDRESS_IN_RANGE(P, POOL) \ |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
670 ((POOL)->arenaindex < maxarenas && \ |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
671 (uptr)(P) - arenas[(POOL)->arenaindex].address < (uptr)ARENA_SIZE && \ |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
672 arenas[(POOL)->arenaindex].address != 0) |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
673 |
|
32278
c2bc248d3e9c
SF bug 881641, make it easier to use valgrind
Neal Norwitz <nnorwitz@gmail.com>
parents:
29515
diff
changeset
|
674 |
|
c2bc248d3e9c
SF bug 881641, make it easier to use valgrind
Neal Norwitz <nnorwitz@gmail.com>
parents:
29515
diff
changeset
|
675 /* This is only useful when running memory debuggers such as |
|
c2bc248d3e9c
SF bug 881641, make it easier to use valgrind
Neal Norwitz <nnorwitz@gmail.com>
parents:
29515
diff
changeset
|
676 * Purify or Valgrind. Uncomment to use. |
|
c2bc248d3e9c
SF bug 881641, make it easier to use valgrind
Neal Norwitz <nnorwitz@gmail.com>
parents:
29515
diff
changeset
|
677 * |
|
36514
914f5b0da54e
Oops, this is supposed to be disabled by default.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36501
diff
changeset
|
678 #define Py_USING_MEMORY_DEBUGGER |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35864
diff
changeset
|
679 */ |
|
32278
c2bc248d3e9c
SF bug 881641, make it easier to use valgrind
Neal Norwitz <nnorwitz@gmail.com>
parents:
29515
diff
changeset
|
680 |
|
c2bc248d3e9c
SF bug 881641, make it easier to use valgrind
Neal Norwitz <nnorwitz@gmail.com>
parents:
29515
diff
changeset
|
681 #ifdef Py_USING_MEMORY_DEBUGGER |
|
c2bc248d3e9c
SF bug 881641, make it easier to use valgrind
Neal Norwitz <nnorwitz@gmail.com>
parents:
29515
diff
changeset
|
682 |
|
c2bc248d3e9c
SF bug 881641, make it easier to use valgrind
Neal Norwitz <nnorwitz@gmail.com>
parents:
29515
diff
changeset
|
683 /* Py_ADDRESS_IN_RANGE may access uninitialized memory by design |
|
c2bc248d3e9c
SF bug 881641, make it easier to use valgrind
Neal Norwitz <nnorwitz@gmail.com>
parents:
29515
diff
changeset
|
684 * This leads to thousands of spurious warnings when using |
|
c2bc248d3e9c
SF bug 881641, make it easier to use valgrind
Neal Norwitz <nnorwitz@gmail.com>
parents:
29515
diff
changeset
|
685 * Purify or Valgrind. By making a function, we can easily |
|
c2bc248d3e9c
SF bug 881641, make it easier to use valgrind
Neal Norwitz <nnorwitz@gmail.com>
parents:
29515
diff
changeset
|
686 * suppress the uninitialized memory reads in this one function. |
|
c2bc248d3e9c
SF bug 881641, make it easier to use valgrind
Neal Norwitz <nnorwitz@gmail.com>
parents:
29515
diff
changeset
|
687 * So we won't ignore real errors elsewhere. |
|
c2bc248d3e9c
SF bug 881641, make it easier to use valgrind
Neal Norwitz <nnorwitz@gmail.com>
parents:
29515
diff
changeset
|
688 * |
|
c2bc248d3e9c
SF bug 881641, make it easier to use valgrind
Neal Norwitz <nnorwitz@gmail.com>
parents:
29515
diff
changeset
|
689 * Disable the macro and use a function. |
|
c2bc248d3e9c
SF bug 881641, make it easier to use valgrind
Neal Norwitz <nnorwitz@gmail.com>
parents:
29515
diff
changeset
|
690 */ |
|
c2bc248d3e9c
SF bug 881641, make it easier to use valgrind
Neal Norwitz <nnorwitz@gmail.com>
parents:
29515
diff
changeset
|
691 |
|
c2bc248d3e9c
SF bug 881641, make it easier to use valgrind
Neal Norwitz <nnorwitz@gmail.com>
parents:
29515
diff
changeset
|
692 #undef Py_ADDRESS_IN_RANGE |
|
c2bc248d3e9c
SF bug 881641, make it easier to use valgrind
Neal Norwitz <nnorwitz@gmail.com>
parents:
29515
diff
changeset
|
693 |
|
35864
dbc93ee53e2f
Do a better job of not inlining Py_ADDRESS_IN_RANGE() for newer gcc's.
Neal Norwitz <nnorwitz@gmail.com>
parents:
35222
diff
changeset
|
694 #if defined(__GNUC__) && (__GNUC__ == 3) && (__GNUC_MINOR__ >= 1) |
|
dbc93ee53e2f
Do a better job of not inlining Py_ADDRESS_IN_RANGE() for newer gcc's.
Neal Norwitz <nnorwitz@gmail.com>
parents:
35222
diff
changeset
|
695 #define Py_NO_INLINE __attribute__((__noinline__)) |
|
dbc93ee53e2f
Do a better job of not inlining Py_ADDRESS_IN_RANGE() for newer gcc's.
Neal Norwitz <nnorwitz@gmail.com>
parents:
35222
diff
changeset
|
696 #else |
|
dbc93ee53e2f
Do a better job of not inlining Py_ADDRESS_IN_RANGE() for newer gcc's.
Neal Norwitz <nnorwitz@gmail.com>
parents:
35222
diff
changeset
|
697 #define Py_NO_INLINE |
|
dbc93ee53e2f
Do a better job of not inlining Py_ADDRESS_IN_RANGE() for newer gcc's.
Neal Norwitz <nnorwitz@gmail.com>
parents:
35222
diff
changeset
|
698 #endif |
|
dbc93ee53e2f
Do a better job of not inlining Py_ADDRESS_IN_RANGE() for newer gcc's.
Neal Norwitz <nnorwitz@gmail.com>
parents:
35222
diff
changeset
|
699 |
|
dbc93ee53e2f
Do a better job of not inlining Py_ADDRESS_IN_RANGE() for newer gcc's.
Neal Norwitz <nnorwitz@gmail.com>
parents:
35222
diff
changeset
|
700 /* Don't make static, to try to ensure this isn't inlined. */ |
|
dbc93ee53e2f
Do a better job of not inlining Py_ADDRESS_IN_RANGE() for newer gcc's.
Neal Norwitz <nnorwitz@gmail.com>
parents:
35222
diff
changeset
|
701 int Py_ADDRESS_IN_RANGE(void *P, poolp pool) Py_NO_INLINE; |
|
dbc93ee53e2f
Do a better job of not inlining Py_ADDRESS_IN_RANGE() for newer gcc's.
Neal Norwitz <nnorwitz@gmail.com>
parents:
35222
diff
changeset
|
702 #undef Py_NO_INLINE |
|
32278
c2bc248d3e9c
SF bug 881641, make it easier to use valgrind
Neal Norwitz <nnorwitz@gmail.com>
parents:
29515
diff
changeset
|
703 #endif |
|
22741
771b2a8d445d
Restructured my pool-management overview in terms of the three
Tim Peters <tim.peters@gmail.com>
parents:
22724
diff
changeset
|
704 |
|
16782
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
705 /*==========================================================================*/ |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
706 |
|
22794
f663ffbe9e5c
_PyMalloc_{Malloc, Realloc}: Strive to meet the doc's promises about
Tim Peters <tim.peters@gmail.com>
parents:
22743
diff
changeset
|
707 /* malloc. Note that nbytes==0 tries to return a non-NULL pointer, distinct |
|
f663ffbe9e5c
_PyMalloc_{Malloc, Realloc}: Strive to meet the doc's promises about
Tim Peters <tim.peters@gmail.com>
parents:
22743
diff
changeset
|
708 * from all other currently live pointers. This may not be possible. |
|
f663ffbe9e5c
_PyMalloc_{Malloc, Realloc}: Strive to meet the doc's promises about
Tim Peters <tim.peters@gmail.com>
parents:
22743
diff
changeset
|
709 */ |
|
16782
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
710 |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
711 /* |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
712 * The basic blocks are ordered by decreasing execution frequency, |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
713 * which minimizes the number of jumps in the most common cases, |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
714 * improves branching prediction and instruction scheduling (small |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
715 * block allocations typically result in a couple of instructions). |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
716 * Unless the optimizer reorders everything, being too smart... |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
717 */ |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
718 |
|
22983
1c26a3106653
Move PyObject_Malloc and PyObject_Free here from object.c. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
22948
diff
changeset
|
719 #undef PyObject_Malloc |
|
16782
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
720 void * |
|
22983
1c26a3106653
Move PyObject_Malloc and PyObject_Free here from object.c. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
22948
diff
changeset
|
721 PyObject_Malloc(size_t nbytes) |
|
16782
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
722 { |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
723 block *bp; |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
724 poolp pool; |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
725 poolp next; |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
726 uint size; |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
727 |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
728 /* |
|
22794
f663ffbe9e5c
_PyMalloc_{Malloc, Realloc}: Strive to meet the doc's promises about
Tim Peters <tim.peters@gmail.com>
parents:
22743
diff
changeset
|
729 * This implicitly redirects malloc(0). |
|
16782
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
730 */ |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
731 if ((nbytes - 1) < SMALL_REQUEST_THRESHOLD) { |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
732 LOCK(); |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
733 /* |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
734 * Most frequent paths first |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
735 */ |
|
37008
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
736 size = (uint)(nbytes - 1) >> ALIGNMENT_SHIFT; |
|
16782
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
737 pool = usedpools[size + size]; |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
738 if (pool != pool->nextpool) { |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
739 /* |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
740 * There is a used pool for this size class. |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
741 * Pick up the head block of its free list. |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
742 */ |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
743 ++pool->ref.count; |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
744 bp = pool->freeblock; |
|
22948
6cb52829da1a
SF bug 542181: Realloc behavior
Tim Peters <tim.peters@gmail.com>
parents:
22860
diff
changeset
|
745 assert(bp != NULL); |
|
16782
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
746 if ((pool->freeblock = *(block **)bp) != NULL) { |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
747 UNLOCK(); |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
748 return (void *)bp; |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
749 } |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
750 /* |
|
37008
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
751 * Reached the end of the free list, try to extend it. |
|
16782
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
752 */ |
|
22836
f1f57bf09092
Widespread, but mostly in _PyMalloc_Malloc: optimize away all expensive
Tim Peters <tim.peters@gmail.com>
parents:
22795
diff
changeset
|
753 if (pool->nextoffset <= pool->maxnextoffset) { |
|
37008
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
754 /* There is room for another block. */ |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
755 pool->freeblock = (block*)pool + |
|
22836
f1f57bf09092
Widespread, but mostly in _PyMalloc_Malloc: optimize away all expensive
Tim Peters <tim.peters@gmail.com>
parents:
22795
diff
changeset
|
756 pool->nextoffset; |
|
f1f57bf09092
Widespread, but mostly in _PyMalloc_Malloc: optimize away all expensive
Tim Peters <tim.peters@gmail.com>
parents:
22795
diff
changeset
|
757 pool->nextoffset += INDEX2SIZE(size); |
|
16782
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
758 *(block **)(pool->freeblock) = NULL; |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
759 UNLOCK(); |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
760 return (void *)bp; |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
761 } |
|
37008
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
762 /* Pool is full, unlink from used pools. */ |
|
16782
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
763 next = pool->nextpool; |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
764 pool = pool->prevpool; |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
765 next->prevpool = pool; |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
766 pool->nextpool = next; |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
767 UNLOCK(); |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
768 return (void *)bp; |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
769 } |
|
37008
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
770 |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
771 /* There isn't a pool of the right size class immediately |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
772 * available: use a free pool. |
|
16782
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
773 */ |
|
37008
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
774 if (usable_arenas == NULL) { |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
775 /* No arena has a free pool: allocate a new arena. */ |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
776 #ifdef WITH_MEMORY_LIMITS |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
777 if (narenas_currently_allocated >= MAX_ARENAS) { |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
778 UNLOCK(); |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
779 goto redirect; |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
780 } |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
781 #endif |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
782 usable_arenas = new_arena(); |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
783 if (usable_arenas == NULL) { |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
784 UNLOCK(); |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
785 goto redirect; |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
786 } |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
787 usable_arenas->nextarena = |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
788 usable_arenas->prevarena = NULL; |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
789 } |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
790 assert(usable_arenas->address != 0); |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
791 |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
792 /* Try to get a cached free pool. */ |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
793 pool = usable_arenas->freepools; |
|
16782
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
794 if (pool != NULL) { |
|
37008
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
795 /* Unlink from cached pools. */ |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
796 usable_arenas->freepools = pool->nextpool; |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
797 |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
798 /* This arena already had the smallest nfreepools |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
799 * value, so decreasing nfreepools doesn't change |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
800 * that, and we don't need to rearrange the |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
801 * usable_arenas list. However, if the arena has |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
802 * become wholly allocated, we need to remove its |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
803 * arena_object from usable_arenas. |
|
16782
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
804 */ |
|
37008
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
805 --usable_arenas->nfreepools; |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
806 if (usable_arenas->nfreepools == 0) { |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
807 /* Wholly allocated: remove. */ |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
808 assert(usable_arenas->freepools == NULL); |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
809 assert(usable_arenas->nextarena == NULL || |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
810 usable_arenas->nextarena->prevarena == |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
811 usable_arenas); |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
812 |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
813 usable_arenas = usable_arenas->nextarena; |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
814 if (usable_arenas != NULL) { |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
815 usable_arenas->prevarena = NULL; |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
816 assert(usable_arenas->address != 0); |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
817 } |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
818 } |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
819 else { |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
820 /* nfreepools > 0: it must be that freepools |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
821 * isn't NULL, or that we haven't yet carved |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
822 * off all the arena's pools for the first |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
823 * time. |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
824 */ |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
825 assert(usable_arenas->freepools != NULL || |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
826 usable_arenas->pool_address <= |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
827 (block*)usable_arenas->address + |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
828 ARENA_SIZE - POOL_SIZE); |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
829 } |
|
16782
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
830 init_pool: |
|
37008
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
831 /* Frontlink to used pools. */ |
|
16782
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
832 next = usedpools[size + size]; /* == prev */ |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
833 pool->nextpool = next; |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
834 pool->prevpool = next; |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
835 next->nextpool = pool; |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
836 next->prevpool = pool; |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
837 pool->ref.count = 1; |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
838 if (pool->szidx == size) { |
|
37008
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
839 /* Luckily, this pool last contained blocks |
|
16782
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
840 * of the same size class, so its header |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
841 * and free list are already initialized. |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
842 */ |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
843 bp = pool->freeblock; |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
844 pool->freeblock = *(block **)bp; |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
845 UNLOCK(); |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
846 return (void *)bp; |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
847 } |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
848 /* |
|
22836
f1f57bf09092
Widespread, but mostly in _PyMalloc_Malloc: optimize away all expensive
Tim Peters <tim.peters@gmail.com>
parents:
22795
diff
changeset
|
849 * Initialize the pool header, set up the free list to |
|
f1f57bf09092
Widespread, but mostly in _PyMalloc_Malloc: optimize away all expensive
Tim Peters <tim.peters@gmail.com>
parents:
22795
diff
changeset
|
850 * contain just the second block, and return the first |
|
f1f57bf09092
Widespread, but mostly in _PyMalloc_Malloc: optimize away all expensive
Tim Peters <tim.peters@gmail.com>
parents:
22795
diff
changeset
|
851 * block. |
|
16782
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
852 */ |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
853 pool->szidx = size; |
|
22836
f1f57bf09092
Widespread, but mostly in _PyMalloc_Malloc: optimize away all expensive
Tim Peters <tim.peters@gmail.com>
parents:
22795
diff
changeset
|
854 size = INDEX2SIZE(size); |
|
16782
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
855 bp = (block *)pool + POOL_OVERHEAD; |
|
22836
f1f57bf09092
Widespread, but mostly in _PyMalloc_Malloc: optimize away all expensive
Tim Peters <tim.peters@gmail.com>
parents:
22795
diff
changeset
|
856 pool->nextoffset = POOL_OVERHEAD + (size << 1); |
|
f1f57bf09092
Widespread, but mostly in _PyMalloc_Malloc: optimize away all expensive
Tim Peters <tim.peters@gmail.com>
parents:
22795
diff
changeset
|
857 pool->maxnextoffset = POOL_SIZE - size; |
|
16782
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
858 pool->freeblock = bp + size; |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
859 *(block **)(pool->freeblock) = NULL; |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
860 UNLOCK(); |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
861 return (void *)bp; |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
862 } |
|
37008
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
863 |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
864 /* Carve off a new pool. */ |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
865 assert(usable_arenas->nfreepools > 0); |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
866 assert(usable_arenas->freepools == NULL); |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
867 pool = (poolp)usable_arenas->pool_address; |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
868 assert((block*)pool <= (block*)usable_arenas->address + |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
869 ARENA_SIZE - POOL_SIZE); |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
870 pool->arenaindex = usable_arenas - arenas; |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
871 assert(&arenas[pool->arenaindex] == usable_arenas); |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
872 pool->szidx = DUMMY_SIZE_IDX; |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
873 usable_arenas->pool_address += POOL_SIZE; |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
874 --usable_arenas->nfreepools; |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
875 |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
876 if (usable_arenas->nfreepools == 0) { |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
877 assert(usable_arenas->nextarena == NULL || |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
878 usable_arenas->nextarena->prevarena == |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
879 usable_arenas); |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
880 /* Unlink the arena: it is completely allocated. */ |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
881 usable_arenas = usable_arenas->nextarena; |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
882 if (usable_arenas != NULL) { |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
883 usable_arenas->prevarena = NULL; |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
884 assert(usable_arenas->address != 0); |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
885 } |
|
16782
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
886 } |
|
37008
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
887 |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
888 goto init_pool; |
|
16782
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
889 } |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
890 |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
891 /* The small block allocator ends here. */ |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
892 |
| 22684 | 893 redirect: |
|
37008
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
894 /* Redirect the original request to the underlying (libc) allocator. |
|
16782
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
895 * We jump here on bigger requests, on error in the code above (as a |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
896 * last chance to serve the request) or when the max memory limit |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
897 * has been reached. |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
898 */ |
|
23158
4ab21e354403
PyObject_Malloc: make a tiny bit faster for platforms where malloc(0)
Tim Peters <tim.peters@gmail.com>
parents:
23157
diff
changeset
|
899 if (nbytes == 0) |
|
4ab21e354403
PyObject_Malloc: make a tiny bit faster for platforms where malloc(0)
Tim Peters <tim.peters@gmail.com>
parents:
23157
diff
changeset
|
900 nbytes = 1; |
|
4ab21e354403
PyObject_Malloc: make a tiny bit faster for platforms where malloc(0)
Tim Peters <tim.peters@gmail.com>
parents:
23157
diff
changeset
|
901 return (void *)malloc(nbytes); |
|
16782
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
902 } |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
903 |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
904 /* free */ |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
905 |
|
22983
1c26a3106653
Move PyObject_Malloc and PyObject_Free here from object.c. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
22948
diff
changeset
|
906 #undef PyObject_Free |
|
16782
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
907 void |
|
22983
1c26a3106653
Move PyObject_Malloc and PyObject_Free here from object.c. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
22948
diff
changeset
|
908 PyObject_Free(void *p) |
|
16782
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
909 { |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
910 poolp pool; |
|
22698
a4a1e6f55531
_PyMalloc_Free(): As was already done for _PyMalloc_Malloc, rearranged
Tim Peters <tim.peters@gmail.com>
parents:
22697
diff
changeset
|
911 block *lastfree; |
|
16782
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
912 poolp next, prev; |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
913 uint size; |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
914 |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
915 if (p == NULL) /* free(NULL) has no effect */ |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
916 return; |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
917 |
| 22684 | 918 pool = POOL_ADDR(p); |
|
32278
c2bc248d3e9c
SF bug 881641, make it easier to use valgrind
Neal Norwitz <nnorwitz@gmail.com>
parents:
29515
diff
changeset
|
919 if (Py_ADDRESS_IN_RANGE(p, pool)) { |
| 22684 | 920 /* We allocated this address. */ |
|
22697
6bb4386b1111
Added a long-overdue comment block giving an overview of pool operations
Tim Peters <tim.peters@gmail.com>
parents:
22692
diff
changeset
|
921 LOCK(); |
|
37008
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
922 /* Link p to the start of the pool's freeblock list. Since |
|
22698
a4a1e6f55531
_PyMalloc_Free(): As was already done for _PyMalloc_Malloc, rearranged
Tim Peters <tim.peters@gmail.com>
parents:
22697
diff
changeset
|
923 * the pool had at least the p block outstanding, the pool |
|
a4a1e6f55531
_PyMalloc_Free(): As was already done for _PyMalloc_Malloc, rearranged
Tim Peters <tim.peters@gmail.com>
parents:
22697
diff
changeset
|
924 * wasn't empty (so it's already in a usedpools[] list, or |
|
a4a1e6f55531
_PyMalloc_Free(): As was already done for _PyMalloc_Malloc, rearranged
Tim Peters <tim.peters@gmail.com>
parents:
22697
diff
changeset
|
925 * was full and is in no list -- it's not in the freeblocks |
|
a4a1e6f55531
_PyMalloc_Free(): As was already done for _PyMalloc_Malloc, rearranged
Tim Peters <tim.peters@gmail.com>
parents:
22697
diff
changeset
|
926 * list in any case). |
| 22684 | 927 */ |
|
22701
cf1a05c86595
Add one more assert that indirectly interlocking conditions are consistent
Tim Peters <tim.peters@gmail.com>
parents:
22700
diff
changeset
|
928 assert(pool->ref.count > 0); /* else it was empty */ |
|
22698
a4a1e6f55531
_PyMalloc_Free(): As was already done for _PyMalloc_Malloc, rearranged
Tim Peters <tim.peters@gmail.com>
parents:
22697
diff
changeset
|
929 *(block **)p = lastfree = pool->freeblock; |
|
a4a1e6f55531
_PyMalloc_Free(): As was already done for _PyMalloc_Malloc, rearranged
Tim Peters <tim.peters@gmail.com>
parents:
22697
diff
changeset
|
930 pool->freeblock = (block *)p; |
|
a4a1e6f55531
_PyMalloc_Free(): As was already done for _PyMalloc_Malloc, rearranged
Tim Peters <tim.peters@gmail.com>
parents:
22697
diff
changeset
|
931 if (lastfree) { |
|
37008
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
932 struct arena_object* ao; |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
933 uint nf; /* ao->nfreepools */ |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
934 |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
935 /* freeblock wasn't NULL, so the pool wasn't full, |
|
22698
a4a1e6f55531
_PyMalloc_Free(): As was already done for _PyMalloc_Malloc, rearranged
Tim Peters <tim.peters@gmail.com>
parents:
22697
diff
changeset
|
936 * and the pool is in a usedpools[] list. |
| 22684 | 937 */ |
|
22698
a4a1e6f55531
_PyMalloc_Free(): As was already done for _PyMalloc_Malloc, rearranged
Tim Peters <tim.peters@gmail.com>
parents:
22697
diff
changeset
|
938 if (--pool->ref.count != 0) { |
|
a4a1e6f55531
_PyMalloc_Free(): As was already done for _PyMalloc_Malloc, rearranged
Tim Peters <tim.peters@gmail.com>
parents:
22697
diff
changeset
|
939 /* pool isn't empty: leave it in usedpools */ |
|
a4a1e6f55531
_PyMalloc_Free(): As was already done for _PyMalloc_Malloc, rearranged
Tim Peters <tim.peters@gmail.com>
parents:
22697
diff
changeset
|
940 UNLOCK(); |
|
a4a1e6f55531
_PyMalloc_Free(): As was already done for _PyMalloc_Malloc, rearranged
Tim Peters <tim.peters@gmail.com>
parents:
22697
diff
changeset
|
941 return; |
|
a4a1e6f55531
_PyMalloc_Free(): As was already done for _PyMalloc_Malloc, rearranged
Tim Peters <tim.peters@gmail.com>
parents:
22697
diff
changeset
|
942 } |
|
37008
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
943 /* Pool is now empty: unlink from usedpools, and |
|
22699
35f58c297899
Fixed a typo in a new comment.
Tim Peters <tim.peters@gmail.com>
parents:
22698
diff
changeset
|
944 * link to the front of freepools. This ensures that |
|
22698
a4a1e6f55531
_PyMalloc_Free(): As was already done for _PyMalloc_Malloc, rearranged
Tim Peters <tim.peters@gmail.com>
parents:
22697
diff
changeset
|
945 * previously freed pools will be allocated later |
|
a4a1e6f55531
_PyMalloc_Free(): As was already done for _PyMalloc_Malloc, rearranged
Tim Peters <tim.peters@gmail.com>
parents:
22697
diff
changeset
|
946 * (being not referenced, they are perhaps paged out). |
|
a4a1e6f55531
_PyMalloc_Free(): As was already done for _PyMalloc_Malloc, rearranged
Tim Peters <tim.peters@gmail.com>
parents:
22697
diff
changeset
|
947 */ |
|
a4a1e6f55531
_PyMalloc_Free(): As was already done for _PyMalloc_Malloc, rearranged
Tim Peters <tim.peters@gmail.com>
parents:
22697
diff
changeset
|
948 next = pool->nextpool; |
|
a4a1e6f55531
_PyMalloc_Free(): As was already done for _PyMalloc_Malloc, rearranged
Tim Peters <tim.peters@gmail.com>
parents:
22697
diff
changeset
|
949 prev = pool->prevpool; |
|
a4a1e6f55531
_PyMalloc_Free(): As was already done for _PyMalloc_Malloc, rearranged
Tim Peters <tim.peters@gmail.com>
parents:
22697
diff
changeset
|
950 next->prevpool = prev; |
|
a4a1e6f55531
_PyMalloc_Free(): As was already done for _PyMalloc_Malloc, rearranged
Tim Peters <tim.peters@gmail.com>
parents:
22697
diff
changeset
|
951 prev->nextpool = next; |
|
37008
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
952 |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
953 /* Link the pool to freepools. This is a singly-linked |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
954 * list, and pool->prevpool isn't used there. |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
955 */ |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
956 ao = &arenas[pool->arenaindex]; |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
957 pool->nextpool = ao->freepools; |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
958 ao->freepools = pool; |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
959 nf = ++ao->nfreepools; |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
960 |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
961 /* All the rest is arena management. We just freed |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
962 * a pool, and there are 4 cases for arena mgmt: |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
963 * 1. If all the pools are free, return the arena to |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
964 * the system free(). |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
965 * 2. If this is the only free pool in the arena, |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
966 * add the arena back to the `usable_arenas` list. |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
967 * 3. If the "next" arena has a smaller count of free |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
968 * pools, we have to "slide this arena right" to |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
969 * restore that usable_arenas is sorted in order of |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
970 * nfreepools. |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
971 * 4. Else there's nothing more to do. |
|
22698
a4a1e6f55531
_PyMalloc_Free(): As was already done for _PyMalloc_Malloc, rearranged
Tim Peters <tim.peters@gmail.com>
parents:
22697
diff
changeset
|
972 */ |
|
37008
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
973 if (nf == ao->ntotalpools) { |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
974 /* Case 1. First unlink ao from usable_arenas. |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
975 */ |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
976 assert(ao->prevarena == NULL || |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
977 ao->prevarena->address != 0); |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
978 assert(ao ->nextarena == NULL || |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
979 ao->nextarena->address != 0); |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
980 |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
981 /* Fix the pointer in the prevarena, or the |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
982 * usable_arenas pointer. |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
983 */ |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
984 if (ao->prevarena == NULL) { |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
985 usable_arenas = ao->nextarena; |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
986 assert(usable_arenas == NULL || |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
987 usable_arenas->address != 0); |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
988 } |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
989 else { |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
990 assert(ao->prevarena->nextarena == ao); |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
991 ao->prevarena->nextarena = |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
992 ao->nextarena; |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
993 } |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
994 /* Fix the pointer in the nextarena. */ |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
995 if (ao->nextarena != NULL) { |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
996 assert(ao->nextarena->prevarena == ao); |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
997 ao->nextarena->prevarena = |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
998 ao->prevarena; |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
999 } |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
1000 /* Record that this arena_object slot is |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
1001 * available to be reused. |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
1002 */ |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
1003 ao->nextarena = unused_arena_objects; |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
1004 unused_arena_objects = ao; |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
1005 |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
1006 /* Free the entire arena. */ |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
1007 free((void *)ao->address); |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
1008 ao->address = 0; /* mark unassociated */ |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
1009 --narenas_currently_allocated; |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
1010 |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
1011 UNLOCK(); |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
1012 return; |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
1013 } |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
1014 if (nf == 1) { |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
1015 /* Case 2. Put ao at the head of |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
1016 * usable_arenas. Note that because |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
1017 * ao->nfreepools was 0 before, ao isn't |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
1018 * currently on the usable_arenas list. |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
1019 */ |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
1020 ao->nextarena = usable_arenas; |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
1021 ao->prevarena = NULL; |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
1022 if (usable_arenas) |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
1023 usable_arenas->prevarena = ao; |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
1024 usable_arenas = ao; |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
1025 assert(usable_arenas->address != 0); |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
1026 |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
1027 UNLOCK(); |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
1028 return; |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
1029 } |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
1030 /* If this arena is now out of order, we need to keep |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
1031 * the list sorted. The list is kept sorted so that |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
1032 * the "most full" arenas are used first, which allows |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
1033 * the nearly empty arenas to be completely freed. In |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
1034 * a few un-scientific tests, it seems like this |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
1035 * approach allowed a lot more memory to be freed. |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
1036 */ |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
1037 if (ao->nextarena == NULL || |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
1038 nf <= ao->nextarena->nfreepools) { |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
1039 /* Case 4. Nothing to do. */ |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
1040 UNLOCK(); |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
1041 return; |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
1042 } |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
1043 /* Case 3: We have to move the arena towards the end |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
1044 * of the list, because it has more free pools than |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
1045 * the arena to its right. |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
1046 * First unlink ao from usable_arenas. |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
1047 */ |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
1048 if (ao->prevarena != NULL) { |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
1049 /* ao isn't at the head of the list */ |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
1050 assert(ao->prevarena->nextarena == ao); |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
1051 ao->prevarena->nextarena = ao->nextarena; |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
1052 } |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
1053 else { |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
1054 /* ao is at the head of the list */ |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
1055 assert(usable_arenas == ao); |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
1056 usable_arenas = ao->nextarena; |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
1057 } |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
1058 ao->nextarena->prevarena = ao->prevarena; |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
1059 |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
1060 /* Locate the new insertion point by iterating over |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
1061 * the list, using our nextarena pointer. |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
1062 */ |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
1063 while (ao->nextarena != NULL && |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
1064 nf > ao->nextarena->nfreepools) { |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
1065 ao->prevarena = ao->nextarena; |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
1066 ao->nextarena = ao->nextarena->nextarena; |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
1067 } |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
1068 |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
1069 /* Insert ao at this point. */ |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
1070 assert(ao->nextarena == NULL || |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
1071 ao->prevarena == ao->nextarena->prevarena); |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
1072 assert(ao->prevarena->nextarena == ao->nextarena); |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
1073 |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
1074 ao->prevarena->nextarena = ao; |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
1075 if (ao->nextarena != NULL) |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
1076 ao->nextarena->prevarena = ao; |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
1077 |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
1078 /* Verify that the swaps worked. */ |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
1079 assert(ao->nextarena == NULL || |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
1080 nf <= ao->nextarena->nfreepools); |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
1081 assert(ao->prevarena == NULL || |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
1082 nf > ao->prevarena->nfreepools); |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
1083 assert(ao->nextarena == NULL || |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
1084 ao->nextarena->prevarena == ao); |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
1085 assert((usable_arenas == ao && |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
1086 ao->prevarena == NULL) || |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
1087 ao->prevarena->nextarena == ao); |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
1088 |
| 22684 | 1089 UNLOCK(); |
| 1090 return; | |
| 1091 } | |
|
37008
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
1092 /* Pool was full, so doesn't currently live in any list: |
|
22698
a4a1e6f55531
_PyMalloc_Free(): As was already done for _PyMalloc_Malloc, rearranged
Tim Peters <tim.peters@gmail.com>
parents:
22697
diff
changeset
|
1093 * link it to the front of the appropriate usedpools[] list. |
|
a4a1e6f55531
_PyMalloc_Free(): As was already done for _PyMalloc_Malloc, rearranged
Tim Peters <tim.peters@gmail.com>
parents:
22697
diff
changeset
|
1094 * This mimics LRU pool usage for new allocations and |
|
a4a1e6f55531
_PyMalloc_Free(): As was already done for _PyMalloc_Malloc, rearranged
Tim Peters <tim.peters@gmail.com>
parents:
22697
diff
changeset
|
1095 * targets optimal filling when several pools contain |
|
a4a1e6f55531
_PyMalloc_Free(): As was already done for _PyMalloc_Malloc, rearranged
Tim Peters <tim.peters@gmail.com>
parents:
22697
diff
changeset
|
1096 * blocks of the same size class. |
| 22684 | 1097 */ |
|
22698
a4a1e6f55531
_PyMalloc_Free(): As was already done for _PyMalloc_Malloc, rearranged
Tim Peters <tim.peters@gmail.com>
parents:
22697
diff
changeset
|
1098 --pool->ref.count; |
|
a4a1e6f55531
_PyMalloc_Free(): As was already done for _PyMalloc_Malloc, rearranged
Tim Peters <tim.peters@gmail.com>
parents:
22697
diff
changeset
|
1099 assert(pool->ref.count > 0); /* else the pool is empty */ |
|
a4a1e6f55531
_PyMalloc_Free(): As was already done for _PyMalloc_Malloc, rearranged
Tim Peters <tim.peters@gmail.com>
parents:
22697
diff
changeset
|
1100 size = pool->szidx; |
|
a4a1e6f55531
_PyMalloc_Free(): As was already done for _PyMalloc_Malloc, rearranged
Tim Peters <tim.peters@gmail.com>
parents:
22697
diff
changeset
|
1101 next = usedpools[size + size]; |
|
a4a1e6f55531
_PyMalloc_Free(): As was already done for _PyMalloc_Malloc, rearranged
Tim Peters <tim.peters@gmail.com>
parents:
22697
diff
changeset
|
1102 prev = next->prevpool; |
|
a4a1e6f55531
_PyMalloc_Free(): As was already done for _PyMalloc_Malloc, rearranged
Tim Peters <tim.peters@gmail.com>
parents:
22697
diff
changeset
|
1103 /* insert pool before next: prev <-> pool <-> next */ |
|
a4a1e6f55531
_PyMalloc_Free(): As was already done for _PyMalloc_Malloc, rearranged
Tim Peters <tim.peters@gmail.com>
parents:
22697
diff
changeset
|
1104 pool->nextpool = next; |
|
a4a1e6f55531
_PyMalloc_Free(): As was already done for _PyMalloc_Malloc, rearranged
Tim Peters <tim.peters@gmail.com>
parents:
22697
diff
changeset
|
1105 pool->prevpool = prev; |
|
a4a1e6f55531
_PyMalloc_Free(): As was already done for _PyMalloc_Malloc, rearranged
Tim Peters <tim.peters@gmail.com>
parents:
22697
diff
changeset
|
1106 next->prevpool = pool; |
|
a4a1e6f55531
_PyMalloc_Free(): As was already done for _PyMalloc_Malloc, rearranged
Tim Peters <tim.peters@gmail.com>
parents:
22697
diff
changeset
|
1107 prev->nextpool = pool; |
| 22684 | 1108 UNLOCK(); |
|
16782
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
1109 return; |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
1110 } |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
1111 |
|
22698
a4a1e6f55531
_PyMalloc_Free(): As was already done for _PyMalloc_Malloc, rearranged
Tim Peters <tim.peters@gmail.com>
parents:
22697
diff
changeset
|
1112 /* We didn't allocate this address. */ |
|
22794
f663ffbe9e5c
_PyMalloc_{Malloc, Realloc}: Strive to meet the doc's promises about
Tim Peters <tim.peters@gmail.com>
parents:
22743
diff
changeset
|
1113 free(p); |
|
16782
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
1114 } |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
1115 |
|
22794
f663ffbe9e5c
_PyMalloc_{Malloc, Realloc}: Strive to meet the doc's promises about
Tim Peters <tim.peters@gmail.com>
parents:
22743
diff
changeset
|
1116 /* realloc. If p is NULL, this acts like malloc(nbytes). Else if nbytes==0, |
|
f663ffbe9e5c
_PyMalloc_{Malloc, Realloc}: Strive to meet the doc's promises about
Tim Peters <tim.peters@gmail.com>
parents:
22743
diff
changeset
|
1117 * then as the Python docs promise, we do not treat this like free(p), and |
|
f663ffbe9e5c
_PyMalloc_{Malloc, Realloc}: Strive to meet the doc's promises about
Tim Peters <tim.peters@gmail.com>
parents:
22743
diff
changeset
|
1118 * return a non-NULL result. |
|
f663ffbe9e5c
_PyMalloc_{Malloc, Realloc}: Strive to meet the doc's promises about
Tim Peters <tim.peters@gmail.com>
parents:
22743
diff
changeset
|
1119 */ |
|
16782
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
1120 |
|
22983
1c26a3106653
Move PyObject_Malloc and PyObject_Free here from object.c. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
22948
diff
changeset
|
1121 #undef PyObject_Realloc |
|
16782
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
1122 void * |
|
22983
1c26a3106653
Move PyObject_Malloc and PyObject_Free here from object.c. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
22948
diff
changeset
|
1123 PyObject_Realloc(void *p, size_t nbytes) |
|
16782
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
1124 { |
|
22794
f663ffbe9e5c
_PyMalloc_{Malloc, Realloc}: Strive to meet the doc's promises about
Tim Peters <tim.peters@gmail.com>
parents:
22743
diff
changeset
|
1125 void *bp; |
|
16782
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
1126 poolp pool; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35864
diff
changeset
|
1127 size_t size; |
|
16782
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
1128 |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
1129 if (p == NULL) |
|
22983
1c26a3106653
Move PyObject_Malloc and PyObject_Free here from object.c. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
22948
diff
changeset
|
1130 return PyObject_Malloc(nbytes); |
|
16782
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
1131 |
| 22684 | 1132 pool = POOL_ADDR(p); |
|
32278
c2bc248d3e9c
SF bug 881641, make it easier to use valgrind
Neal Norwitz <nnorwitz@gmail.com>
parents:
29515
diff
changeset
|
1133 if (Py_ADDRESS_IN_RANGE(p, pool)) { |
|
16782
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
1134 /* We're in charge of this block */ |
|
22836
f1f57bf09092
Widespread, but mostly in _PyMalloc_Malloc: optimize away all expensive
Tim Peters <tim.peters@gmail.com>
parents:
22795
diff
changeset
|
1135 size = INDEX2SIZE(pool->szidx); |
|
23363
36840a989722
PyObject_Realloc(): If a small block is shrinking, bite the expense of
Tim Peters <tim.peters@gmail.com>
parents:
23298
diff
changeset
|
1136 if (nbytes <= size) { |
|
36840a989722
PyObject_Realloc(): If a small block is shrinking, bite the expense of
Tim Peters <tim.peters@gmail.com>
parents:
23298
diff
changeset
|
1137 /* The block is staying the same or shrinking. If |
|
36840a989722
PyObject_Realloc(): If a small block is shrinking, bite the expense of
Tim Peters <tim.peters@gmail.com>
parents:
23298
diff
changeset
|
1138 * it's shrinking, there's a tradeoff: it costs |
|
36840a989722
PyObject_Realloc(): If a small block is shrinking, bite the expense of
Tim Peters <tim.peters@gmail.com>
parents:
23298
diff
changeset
|
1139 * cycles to copy the block to a smaller size class, |
|
36840a989722
PyObject_Realloc(): If a small block is shrinking, bite the expense of
Tim Peters <tim.peters@gmail.com>
parents:
23298
diff
changeset
|
1140 * but it wastes memory not to copy it. The |
|
36840a989722
PyObject_Realloc(): If a small block is shrinking, bite the expense of
Tim Peters <tim.peters@gmail.com>
parents:
23298
diff
changeset
|
1141 * compromise here is to copy on shrink only if at |
|
36840a989722
PyObject_Realloc(): If a small block is shrinking, bite the expense of
Tim Peters <tim.peters@gmail.com>
parents:
23298
diff
changeset
|
1142 * least 25% of size can be shaved off. |
|
36840a989722
PyObject_Realloc(): If a small block is shrinking, bite the expense of
Tim Peters <tim.peters@gmail.com>
parents:
23298
diff
changeset
|
1143 */ |
|
36840a989722
PyObject_Realloc(): If a small block is shrinking, bite the expense of
Tim Peters <tim.peters@gmail.com>
parents:
23298
diff
changeset
|
1144 if (4 * nbytes > 3 * size) { |
|
36840a989722
PyObject_Realloc(): If a small block is shrinking, bite the expense of
Tim Peters <tim.peters@gmail.com>
parents:
23298
diff
changeset
|
1145 /* It's the same, |
|
36840a989722
PyObject_Realloc(): If a small block is shrinking, bite the expense of
Tim Peters <tim.peters@gmail.com>
parents:
23298
diff
changeset
|
1146 * or shrinking and new/old > 3/4. |
|
36840a989722
PyObject_Realloc(): If a small block is shrinking, bite the expense of
Tim Peters <tim.peters@gmail.com>
parents:
23298
diff
changeset
|
1147 */ |
|
36840a989722
PyObject_Realloc(): If a small block is shrinking, bite the expense of
Tim Peters <tim.peters@gmail.com>
parents:
23298
diff
changeset
|
1148 return p; |
|
36840a989722
PyObject_Realloc(): If a small block is shrinking, bite the expense of
Tim Peters <tim.peters@gmail.com>
parents:
23298
diff
changeset
|
1149 } |
|
36840a989722
PyObject_Realloc(): If a small block is shrinking, bite the expense of
Tim Peters <tim.peters@gmail.com>
parents:
23298
diff
changeset
|
1150 size = nbytes; |
|
36840a989722
PyObject_Realloc(): If a small block is shrinking, bite the expense of
Tim Peters <tim.peters@gmail.com>
parents:
23298
diff
changeset
|
1151 } |
|
22983
1c26a3106653
Move PyObject_Malloc and PyObject_Free here from object.c. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
22948
diff
changeset
|
1152 bp = PyObject_Malloc(nbytes); |
|
22794
f663ffbe9e5c
_PyMalloc_{Malloc, Realloc}: Strive to meet the doc's promises about
Tim Peters <tim.peters@gmail.com>
parents:
22743
diff
changeset
|
1153 if (bp != NULL) { |
|
f663ffbe9e5c
_PyMalloc_{Malloc, Realloc}: Strive to meet the doc's promises about
Tim Peters <tim.peters@gmail.com>
parents:
22743
diff
changeset
|
1154 memcpy(bp, p, size); |
|
22983
1c26a3106653
Move PyObject_Malloc and PyObject_Free here from object.c. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
22948
diff
changeset
|
1155 PyObject_Free(p); |
|
16782
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
1156 } |
|
22794
f663ffbe9e5c
_PyMalloc_{Malloc, Realloc}: Strive to meet the doc's promises about
Tim Peters <tim.peters@gmail.com>
parents:
22743
diff
changeset
|
1157 return bp; |
|
f663ffbe9e5c
_PyMalloc_{Malloc, Realloc}: Strive to meet the doc's promises about
Tim Peters <tim.peters@gmail.com>
parents:
22743
diff
changeset
|
1158 } |
| 35222 | 1159 /* We're not managing this block. If nbytes <= |
| 1160 * SMALL_REQUEST_THRESHOLD, it's tempting to try to take over this | |
| 1161 * block. However, if we do, we need to copy the valid data from | |
| 1162 * the C-managed block to one of our blocks, and there's no portable | |
| 1163 * way to know how much of the memory space starting at p is valid. | |
| 1164 * As bug 1185883 pointed out the hard way, it's possible that the | |
| 1165 * C-managed block is "at the end" of allocated VM space, so that | |
| 1166 * a memory fault can occur if we try to copy nbytes bytes starting | |
| 1167 * at p. Instead we punt: let C continue to manage this block. | |
| 1168 */ | |
| 1169 if (nbytes) | |
| 1170 return realloc(p, nbytes); | |
| 1171 /* C doesn't define the result of realloc(p, 0) (it may or may not | |
| 1172 * return NULL then), but Python's docs promise that nbytes==0 never | |
| 1173 * returns NULL. We don't pass 0 to realloc(), to avoid that endcase | |
| 1174 * to begin with. Even then, we can't be sure that realloc() won't | |
| 1175 * return NULL. | |
| 1176 */ | |
| 1177 bp = realloc(p, 1); | |
| 1178 return bp ? bp : p; | |
|
16782
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
1179 } |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
1180 |
|
22479
fb45791150d1
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
Tim Peters <tim.peters@gmail.com>
parents:
22470
diff
changeset
|
1181 #else /* ! WITH_PYMALLOC */ |
|
16782
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
1182 |
|
22479
fb45791150d1
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
Tim Peters <tim.peters@gmail.com>
parents:
22470
diff
changeset
|
1183 /*==========================================================================*/ |
|
22983
1c26a3106653
Move PyObject_Malloc and PyObject_Free here from object.c. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
22948
diff
changeset
|
1184 /* pymalloc not enabled: Redirect the entry points to malloc. These will |
|
1c26a3106653
Move PyObject_Malloc and PyObject_Free here from object.c. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
22948
diff
changeset
|
1185 * only be used by extensions that are compiled with pymalloc enabled. */ |
|
22484
ffcb2fd2947e
Minor code cleanup -- no semantic changes.
Tim Peters <tim.peters@gmail.com>
parents:
22479
diff
changeset
|
1186 |
|
22470
0e4758ccfe2b
Just whitespace fiddling.
Tim Peters <tim.peters@gmail.com>
parents:
22469
diff
changeset
|
1187 void * |
|
22983
1c26a3106653
Move PyObject_Malloc and PyObject_Free here from object.c. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
22948
diff
changeset
|
1188 PyObject_Malloc(size_t n) |
|
22469
8d0061493c57
Build obmalloc.c directly instead of #include'ing from object.c.
Tim Peters <tim.peters@gmail.com>
parents:
22465
diff
changeset
|
1189 { |
|
8d0061493c57
Build obmalloc.c directly instead of #include'ing from object.c.
Tim Peters <tim.peters@gmail.com>
parents:
22465
diff
changeset
|
1190 return PyMem_MALLOC(n); |
|
8d0061493c57
Build obmalloc.c directly instead of #include'ing from object.c.
Tim Peters <tim.peters@gmail.com>
parents:
22465
diff
changeset
|
1191 } |
|
8d0061493c57
Build obmalloc.c directly instead of #include'ing from object.c.
Tim Peters <tim.peters@gmail.com>
parents:
22465
diff
changeset
|
1192 |
|
22470
0e4758ccfe2b
Just whitespace fiddling.
Tim Peters <tim.peters@gmail.com>
parents:
22469
diff
changeset
|
1193 void * |
|
22983
1c26a3106653
Move PyObject_Malloc and PyObject_Free here from object.c. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
22948
diff
changeset
|
1194 PyObject_Realloc(void *p, size_t n) |
|
22469
8d0061493c57
Build obmalloc.c directly instead of #include'ing from object.c.
Tim Peters <tim.peters@gmail.com>
parents:
22465
diff
changeset
|
1195 { |
|
8d0061493c57
Build obmalloc.c directly instead of #include'ing from object.c.
Tim Peters <tim.peters@gmail.com>
parents:
22465
diff
changeset
|
1196 return PyMem_REALLOC(p, n); |
|
8d0061493c57
Build obmalloc.c directly instead of #include'ing from object.c.
Tim Peters <tim.peters@gmail.com>
parents:
22465
diff
changeset
|
1197 } |
|
8d0061493c57
Build obmalloc.c directly instead of #include'ing from object.c.
Tim Peters <tim.peters@gmail.com>
parents:
22465
diff
changeset
|
1198 |
|
8d0061493c57
Build obmalloc.c directly instead of #include'ing from object.c.
Tim Peters <tim.peters@gmail.com>
parents:
22465
diff
changeset
|
1199 void |
|
22983
1c26a3106653
Move PyObject_Malloc and PyObject_Free here from object.c. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
22948
diff
changeset
|
1200 PyObject_Free(void *p) |
|
22469
8d0061493c57
Build obmalloc.c directly instead of #include'ing from object.c.
Tim Peters <tim.peters@gmail.com>
parents:
22465
diff
changeset
|
1201 { |
|
8d0061493c57
Build obmalloc.c directly instead of #include'ing from object.c.
Tim Peters <tim.peters@gmail.com>
parents:
22465
diff
changeset
|
1202 PyMem_FREE(p); |
|
8d0061493c57
Build obmalloc.c directly instead of #include'ing from object.c.
Tim Peters <tim.peters@gmail.com>
parents:
22465
diff
changeset
|
1203 } |
|
8d0061493c57
Build obmalloc.c directly instead of #include'ing from object.c.
Tim Peters <tim.peters@gmail.com>
parents:
22465
diff
changeset
|
1204 #endif /* WITH_PYMALLOC */ |
|
8d0061493c57
Build obmalloc.c directly instead of #include'ing from object.c.
Tim Peters <tim.peters@gmail.com>
parents:
22465
diff
changeset
|
1205 |
|
22479
fb45791150d1
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
Tim Peters <tim.peters@gmail.com>
parents:
22470
diff
changeset
|
1206 #ifdef PYMALLOC_DEBUG |
|
fb45791150d1
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
Tim Peters <tim.peters@gmail.com>
parents:
22470
diff
changeset
|
1207 /*==========================================================================*/ |
|
22484
ffcb2fd2947e
Minor code cleanup -- no semantic changes.
Tim Peters <tim.peters@gmail.com>
parents:
22479
diff
changeset
|
1208 /* A x-platform debugging allocator. This doesn't manage memory directly, |
|
ffcb2fd2947e
Minor code cleanup -- no semantic changes.
Tim Peters <tim.peters@gmail.com>
parents:
22479
diff
changeset
|
1209 * it wraps a real allocator, adding extra debugging info to the memory blocks. |
|
ffcb2fd2947e
Minor code cleanup -- no semantic changes.
Tim Peters <tim.peters@gmail.com>
parents:
22479
diff
changeset
|
1210 */ |
|
22479
fb45791150d1
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
Tim Peters <tim.peters@gmail.com>
parents:
22470
diff
changeset
|
1211 |
|
22986
298a7c51d2c0
PYMALLOC_{CLEAN, DEAD, FORBIDDEN}BYTE symbols: remove the PYMALLOC_
Tim Peters <tim.peters@gmail.com>
parents:
22983
diff
changeset
|
1212 /* Special bytes broadcast into debug memory blocks at appropriate times. |
|
298a7c51d2c0
PYMALLOC_{CLEAN, DEAD, FORBIDDEN}BYTE symbols: remove the PYMALLOC_
Tim Peters <tim.peters@gmail.com>
parents:
22983
diff
changeset
|
1213 * Strings of these are unlikely to be valid addresses, floats, ints or |
|
298a7c51d2c0
PYMALLOC_{CLEAN, DEAD, FORBIDDEN}BYTE symbols: remove the PYMALLOC_
Tim Peters <tim.peters@gmail.com>
parents:
22983
diff
changeset
|
1214 * 7-bit ASCII. |
|
298a7c51d2c0
PYMALLOC_{CLEAN, DEAD, FORBIDDEN}BYTE symbols: remove the PYMALLOC_
Tim Peters <tim.peters@gmail.com>
parents:
22983
diff
changeset
|
1215 */ |
|
298a7c51d2c0
PYMALLOC_{CLEAN, DEAD, FORBIDDEN}BYTE symbols: remove the PYMALLOC_
Tim Peters <tim.peters@gmail.com>
parents:
22983
diff
changeset
|
1216 #undef CLEANBYTE |
|
298a7c51d2c0
PYMALLOC_{CLEAN, DEAD, FORBIDDEN}BYTE symbols: remove the PYMALLOC_
Tim Peters <tim.peters@gmail.com>
parents:
22983
diff
changeset
|
1217 #undef DEADBYTE |
|
298a7c51d2c0
PYMALLOC_{CLEAN, DEAD, FORBIDDEN}BYTE symbols: remove the PYMALLOC_
Tim Peters <tim.peters@gmail.com>
parents:
22983
diff
changeset
|
1218 #undef FORBIDDENBYTE |
|
298a7c51d2c0
PYMALLOC_{CLEAN, DEAD, FORBIDDEN}BYTE symbols: remove the PYMALLOC_
Tim Peters <tim.peters@gmail.com>
parents:
22983
diff
changeset
|
1219 #define CLEANBYTE 0xCB /* clean (newly allocated) memory */ |
|
24177
a038abd08e4a
Documented PYMALLOC_DEBUG. This completes primary coverage of all the
Tim Peters <tim.peters@gmail.com>
parents:
23363
diff
changeset
|
1220 #define DEADBYTE 0xDB /* dead (newly freed) memory */ |
|
22986
298a7c51d2c0
PYMALLOC_{CLEAN, DEAD, FORBIDDEN}BYTE symbols: remove the PYMALLOC_
Tim Peters <tim.peters@gmail.com>
parents:
22983
diff
changeset
|
1221 #define FORBIDDENBYTE 0xFB /* untouchable bytes at each end of a block */ |
|
22479
fb45791150d1
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
Tim Peters <tim.peters@gmail.com>
parents:
22470
diff
changeset
|
1222 |
|
38575
5722f112d5da
In a PYMALLOC_DEBUG build obmalloc adds extra debugging info
Tim Peters <tim.peters@gmail.com>
parents:
37598
diff
changeset
|
1223 static size_t serialno = 0; /* incremented on each debug {m,re}alloc */ |
|
22479
fb45791150d1
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
Tim Peters <tim.peters@gmail.com>
parents:
22470
diff
changeset
|
1224 |
|
22487
f02cdb2bc17a
_PyMalloc_DebugRealloc(): simplify decl of "fresh".
Tim Peters <tim.peters@gmail.com>
parents:
22484
diff
changeset
|
1225 /* serialno is always incremented via calling this routine. The point is |
|
38575
5722f112d5da
In a PYMALLOC_DEBUG build obmalloc adds extra debugging info
Tim Peters <tim.peters@gmail.com>
parents:
37598
diff
changeset
|
1226 * to supply a single place to set a breakpoint. |
|
5722f112d5da
In a PYMALLOC_DEBUG build obmalloc adds extra debugging info
Tim Peters <tim.peters@gmail.com>
parents:
37598
diff
changeset
|
1227 */ |
|
22487
f02cdb2bc17a
_PyMalloc_DebugRealloc(): simplify decl of "fresh".
Tim Peters <tim.peters@gmail.com>
parents:
22484
diff
changeset
|
1228 static void |
|
22631
7870fac049f7
Add missing "void" to function.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
22614
diff
changeset
|
1229 bumpserialno(void) |
|
22487
f02cdb2bc17a
_PyMalloc_DebugRealloc(): simplify decl of "fresh".
Tim Peters <tim.peters@gmail.com>
parents:
22484
diff
changeset
|
1230 { |
|
f02cdb2bc17a
_PyMalloc_DebugRealloc(): simplify decl of "fresh".
Tim Peters <tim.peters@gmail.com>
parents:
22484
diff
changeset
|
1231 ++serialno; |
|
f02cdb2bc17a
_PyMalloc_DebugRealloc(): simplify decl of "fresh".
Tim Peters <tim.peters@gmail.com>
parents:
22484
diff
changeset
|
1232 } |
|
f02cdb2bc17a
_PyMalloc_DebugRealloc(): simplify decl of "fresh".
Tim Peters <tim.peters@gmail.com>
parents:
22484
diff
changeset
|
1233 |
|
38575
5722f112d5da
In a PYMALLOC_DEBUG build obmalloc adds extra debugging info
Tim Peters <tim.peters@gmail.com>
parents:
37598
diff
changeset
|
1234 #define SST SIZEOF_SIZE_T |
|
22487
f02cdb2bc17a
_PyMalloc_DebugRealloc(): simplify decl of "fresh".
Tim Peters <tim.peters@gmail.com>
parents:
22484
diff
changeset
|
1235 |
|
38575
5722f112d5da
In a PYMALLOC_DEBUG build obmalloc adds extra debugging info
Tim Peters <tim.peters@gmail.com>
parents:
37598
diff
changeset
|
1236 /* Read sizeof(size_t) bytes at p as a big-endian size_t. */ |
|
5722f112d5da
In a PYMALLOC_DEBUG build obmalloc adds extra debugging info
Tim Peters <tim.peters@gmail.com>
parents:
37598
diff
changeset
|
1237 static size_t |
|
5722f112d5da
In a PYMALLOC_DEBUG build obmalloc adds extra debugging info
Tim Peters <tim.peters@gmail.com>
parents:
37598
diff
changeset
|
1238 read_size_t(const void *p) |
|
22479
fb45791150d1
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
Tim Peters <tim.peters@gmail.com>
parents:
22470
diff
changeset
|
1239 { |
|
22484
ffcb2fd2947e
Minor code cleanup -- no semantic changes.
Tim Peters <tim.peters@gmail.com>
parents:
22479
diff
changeset
|
1240 const uchar *q = (const uchar *)p; |
|
38575
5722f112d5da
In a PYMALLOC_DEBUG build obmalloc adds extra debugging info
Tim Peters <tim.peters@gmail.com>
parents:
37598
diff
changeset
|
1241 size_t result = *q++; |
|
5722f112d5da
In a PYMALLOC_DEBUG build obmalloc adds extra debugging info
Tim Peters <tim.peters@gmail.com>
parents:
37598
diff
changeset
|
1242 int i; |
|
5722f112d5da
In a PYMALLOC_DEBUG build obmalloc adds extra debugging info
Tim Peters <tim.peters@gmail.com>
parents:
37598
diff
changeset
|
1243 |
|
5722f112d5da
In a PYMALLOC_DEBUG build obmalloc adds extra debugging info
Tim Peters <tim.peters@gmail.com>
parents:
37598
diff
changeset
|
1244 for (i = SST; --i > 0; ++q) |
|
5722f112d5da
In a PYMALLOC_DEBUG build obmalloc adds extra debugging info
Tim Peters <tim.peters@gmail.com>
parents:
37598
diff
changeset
|
1245 result = (result << 8) | *q; |
|
5722f112d5da
In a PYMALLOC_DEBUG build obmalloc adds extra debugging info
Tim Peters <tim.peters@gmail.com>
parents:
37598
diff
changeset
|
1246 return result; |
|
22479
fb45791150d1
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
Tim Peters <tim.peters@gmail.com>
parents:
22470
diff
changeset
|
1247 } |
|
fb45791150d1
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
Tim Peters <tim.peters@gmail.com>
parents:
22470
diff
changeset
|
1248 |
|
38575
5722f112d5da
In a PYMALLOC_DEBUG build obmalloc adds extra debugging info
Tim Peters <tim.peters@gmail.com>
parents:
37598
diff
changeset
|
1249 /* Write n as a big-endian size_t, MSB at address p, LSB at |
|
5722f112d5da
In a PYMALLOC_DEBUG build obmalloc adds extra debugging info
Tim Peters <tim.peters@gmail.com>
parents:
37598
diff
changeset
|
1250 * p + sizeof(size_t) - 1. |
|
5722f112d5da
In a PYMALLOC_DEBUG build obmalloc adds extra debugging info
Tim Peters <tim.peters@gmail.com>
parents:
37598
diff
changeset
|
1251 */ |
|
22479
fb45791150d1
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
Tim Peters <tim.peters@gmail.com>
parents:
22470
diff
changeset
|
1252 static void |
|
38575
5722f112d5da
In a PYMALLOC_DEBUG build obmalloc adds extra debugging info
Tim Peters <tim.peters@gmail.com>
parents:
37598
diff
changeset
|
1253 write_size_t(void *p, size_t n) |
|
22479
fb45791150d1
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
Tim Peters <tim.peters@gmail.com>
parents:
22470
diff
changeset
|
1254 { |
|
38575
5722f112d5da
In a PYMALLOC_DEBUG build obmalloc adds extra debugging info
Tim Peters <tim.peters@gmail.com>
parents:
37598
diff
changeset
|
1255 uchar *q = (uchar *)p + SST - 1; |
|
5722f112d5da
In a PYMALLOC_DEBUG build obmalloc adds extra debugging info
Tim Peters <tim.peters@gmail.com>
parents:
37598
diff
changeset
|
1256 int i; |
|
5722f112d5da
In a PYMALLOC_DEBUG build obmalloc adds extra debugging info
Tim Peters <tim.peters@gmail.com>
parents:
37598
diff
changeset
|
1257 |
|
5722f112d5da
In a PYMALLOC_DEBUG build obmalloc adds extra debugging info
Tim Peters <tim.peters@gmail.com>
parents:
37598
diff
changeset
|
1258 for (i = SST; --i >= 0; --q) { |
|
5722f112d5da
In a PYMALLOC_DEBUG build obmalloc adds extra debugging info
Tim Peters <tim.peters@gmail.com>
parents:
37598
diff
changeset
|
1259 *q = (uchar)(n & 0xff); |
|
5722f112d5da
In a PYMALLOC_DEBUG build obmalloc adds extra debugging info
Tim Peters <tim.peters@gmail.com>
parents:
37598
diff
changeset
|
1260 n >>= 8; |
|
5722f112d5da
In a PYMALLOC_DEBUG build obmalloc adds extra debugging info
Tim Peters <tim.peters@gmail.com>
parents:
37598
diff
changeset
|
1261 } |
|
22479
fb45791150d1
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
Tim Peters <tim.peters@gmail.com>
parents:
22470
diff
changeset
|
1262 } |
|
fb45791150d1
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
Tim Peters <tim.peters@gmail.com>
parents:
22470
diff
changeset
|
1263 |
|
23159
fca379c30906
_PyObject_DebugMallocStats(): Added some potentially expensive internal
Tim Peters <tim.peters@gmail.com>
parents:
23158
diff
changeset
|
1264 #ifdef Py_DEBUG |
|
fca379c30906
_PyObject_DebugMallocStats(): Added some potentially expensive internal
Tim Peters <tim.peters@gmail.com>
parents:
23158
diff
changeset
|
1265 /* Is target in the list? The list is traversed via the nextpool pointers. |
|
fca379c30906
_PyObject_DebugMallocStats(): Added some potentially expensive internal
Tim Peters <tim.peters@gmail.com>
parents:
23158
diff
changeset
|
1266 * The list may be NULL-terminated, or circular. Return 1 if target is in |
|
fca379c30906
_PyObject_DebugMallocStats(): Added some potentially expensive internal
Tim Peters <tim.peters@gmail.com>
parents:
23158
diff
changeset
|
1267 * list, else 0. |
|
fca379c30906
_PyObject_DebugMallocStats(): Added some potentially expensive internal
Tim Peters <tim.peters@gmail.com>
parents:
23158
diff
changeset
|
1268 */ |
|
fca379c30906
_PyObject_DebugMallocStats(): Added some potentially expensive internal
Tim Peters <tim.peters@gmail.com>
parents:
23158
diff
changeset
|
1269 static int |
|
fca379c30906
_PyObject_DebugMallocStats(): Added some potentially expensive internal
Tim Peters <tim.peters@gmail.com>
parents:
23158
diff
changeset
|
1270 pool_is_in_list(const poolp target, poolp list) |
|
fca379c30906
_PyObject_DebugMallocStats(): Added some potentially expensive internal
Tim Peters <tim.peters@gmail.com>
parents:
23158
diff
changeset
|
1271 { |
|
fca379c30906
_PyObject_DebugMallocStats(): Added some potentially expensive internal
Tim Peters <tim.peters@gmail.com>
parents:
23158
diff
changeset
|
1272 poolp origlist = list; |
|
fca379c30906
_PyObject_DebugMallocStats(): Added some potentially expensive internal
Tim Peters <tim.peters@gmail.com>
parents:
23158
diff
changeset
|
1273 assert(target != NULL); |
|
fca379c30906
_PyObject_DebugMallocStats(): Added some potentially expensive internal
Tim Peters <tim.peters@gmail.com>
parents:
23158
diff
changeset
|
1274 if (list == NULL) |
|
fca379c30906
_PyObject_DebugMallocStats(): Added some potentially expensive internal
Tim Peters <tim.peters@gmail.com>
parents:
23158
diff
changeset
|
1275 return 0; |
|
fca379c30906
_PyObject_DebugMallocStats(): Added some potentially expensive internal
Tim Peters <tim.peters@gmail.com>
parents:
23158
diff
changeset
|
1276 do { |
|
fca379c30906
_PyObject_DebugMallocStats(): Added some potentially expensive internal
Tim Peters <tim.peters@gmail.com>
parents:
23158
diff
changeset
|
1277 if (target == list) |
|
fca379c30906
_PyObject_DebugMallocStats(): Added some potentially expensive internal
Tim Peters <tim.peters@gmail.com>
parents:
23158
diff
changeset
|
1278 return 1; |
|
fca379c30906
_PyObject_DebugMallocStats(): Added some potentially expensive internal
Tim Peters <tim.peters@gmail.com>
parents:
23158
diff
changeset
|
1279 list = list->nextpool; |
|
fca379c30906
_PyObject_DebugMallocStats(): Added some potentially expensive internal
Tim Peters <tim.peters@gmail.com>
parents:
23158
diff
changeset
|
1280 } while (list != NULL && list != origlist); |
|
fca379c30906
_PyObject_DebugMallocStats(): Added some potentially expensive internal
Tim Peters <tim.peters@gmail.com>
parents:
23158
diff
changeset
|
1281 return 0; |
|
fca379c30906
_PyObject_DebugMallocStats(): Added some potentially expensive internal
Tim Peters <tim.peters@gmail.com>
parents:
23158
diff
changeset
|
1282 } |
|
fca379c30906
_PyObject_DebugMallocStats(): Added some potentially expensive internal
Tim Peters <tim.peters@gmail.com>
parents:
23158
diff
changeset
|
1283 |
|
fca379c30906
_PyObject_DebugMallocStats(): Added some potentially expensive internal
Tim Peters <tim.peters@gmail.com>
parents:
23158
diff
changeset
|
1284 #else |
|
fca379c30906
_PyObject_DebugMallocStats(): Added some potentially expensive internal
Tim Peters <tim.peters@gmail.com>
parents:
23158
diff
changeset
|
1285 #define pool_is_in_list(X, Y) 1 |
|
fca379c30906
_PyObject_DebugMallocStats(): Added some potentially expensive internal
Tim Peters <tim.peters@gmail.com>
parents:
23158
diff
changeset
|
1286 |
|
fca379c30906
_PyObject_DebugMallocStats(): Added some potentially expensive internal
Tim Peters <tim.peters@gmail.com>
parents:
23158
diff
changeset
|
1287 #endif /* Py_DEBUG */ |
|
fca379c30906
_PyObject_DebugMallocStats(): Added some potentially expensive internal
Tim Peters <tim.peters@gmail.com>
parents:
23158
diff
changeset
|
1288 |
|
38575
5722f112d5da
In a PYMALLOC_DEBUG build obmalloc adds extra debugging info
Tim Peters <tim.peters@gmail.com>
parents:
37598
diff
changeset
|
1289 /* Let S = sizeof(size_t). The debug malloc asks for 4*S extra bytes and |
|
5722f112d5da
In a PYMALLOC_DEBUG build obmalloc adds extra debugging info
Tim Peters <tim.peters@gmail.com>
parents:
37598
diff
changeset
|
1290 fills them with useful stuff, here calling the underlying malloc's result p: |
|
22479
fb45791150d1
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
Tim Peters <tim.peters@gmail.com>
parents:
22470
diff
changeset
|
1291 |
|
38575
5722f112d5da
In a PYMALLOC_DEBUG build obmalloc adds extra debugging info
Tim Peters <tim.peters@gmail.com>
parents:
37598
diff
changeset
|
1292 p[0: S] |
|
5722f112d5da
In a PYMALLOC_DEBUG build obmalloc adds extra debugging info
Tim Peters <tim.peters@gmail.com>
parents:
37598
diff
changeset
|
1293 Number of bytes originally asked for. This is a size_t, big-endian (easier |
|
5722f112d5da
In a PYMALLOC_DEBUG build obmalloc adds extra debugging info
Tim Peters <tim.peters@gmail.com>
parents:
37598
diff
changeset
|
1294 to read in a memory dump). |
|
5722f112d5da
In a PYMALLOC_DEBUG build obmalloc adds extra debugging info
Tim Peters <tim.peters@gmail.com>
parents:
37598
diff
changeset
|
1295 p[S: 2*S] |
|
22986
298a7c51d2c0
PYMALLOC_{CLEAN, DEAD, FORBIDDEN}BYTE symbols: remove the PYMALLOC_
Tim Peters <tim.peters@gmail.com>
parents:
22983
diff
changeset
|
1296 Copies of FORBIDDENBYTE. Used to catch under- writes and reads. |
|
38575
5722f112d5da
In a PYMALLOC_DEBUG build obmalloc adds extra debugging info
Tim Peters <tim.peters@gmail.com>
parents:
37598
diff
changeset
|
1297 p[2*S: 2*S+n] |
|
22986
298a7c51d2c0
PYMALLOC_{CLEAN, DEAD, FORBIDDEN}BYTE symbols: remove the PYMALLOC_
Tim Peters <tim.peters@gmail.com>
parents:
22983
diff
changeset
|
1298 The requested memory, filled with copies of CLEANBYTE. |
|
22479
fb45791150d1
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
Tim Peters <tim.peters@gmail.com>
parents:
22470
diff
changeset
|
1299 Used to catch reference to uninitialized memory. |
|
38575
5722f112d5da
In a PYMALLOC_DEBUG build obmalloc adds extra debugging info
Tim Peters <tim.peters@gmail.com>
parents:
37598
diff
changeset
|
1300 &p[2*S] is returned. Note that this is 8-byte aligned if pymalloc |
|
22479
fb45791150d1
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
Tim Peters <tim.peters@gmail.com>
parents:
22470
diff
changeset
|
1301 handled the request itself. |
|
38575
5722f112d5da
In a PYMALLOC_DEBUG build obmalloc adds extra debugging info
Tim Peters <tim.peters@gmail.com>
parents:
37598
diff
changeset
|
1302 p[2*S+n: 2*S+n+S] |
|
22986
298a7c51d2c0
PYMALLOC_{CLEAN, DEAD, FORBIDDEN}BYTE symbols: remove the PYMALLOC_
Tim Peters <tim.peters@gmail.com>
parents:
22983
diff
changeset
|
1303 Copies of FORBIDDENBYTE. Used to catch over- writes and reads. |
|
38575
5722f112d5da
In a PYMALLOC_DEBUG build obmalloc adds extra debugging info
Tim Peters <tim.peters@gmail.com>
parents:
37598
diff
changeset
|
1304 p[2*S+n+S: 2*S+n+2*S] |
|
22983
1c26a3106653
Move PyObject_Malloc and PyObject_Free here from object.c. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
22948
diff
changeset
|
1305 A serial number, incremented by 1 on each call to _PyObject_DebugMalloc |
|
1c26a3106653
Move PyObject_Malloc and PyObject_Free here from object.c. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
22948
diff
changeset
|
1306 and _PyObject_DebugRealloc. |
|
38575
5722f112d5da
In a PYMALLOC_DEBUG build obmalloc adds extra debugging info
Tim Peters <tim.peters@gmail.com>
parents:
37598
diff
changeset
|
1307 This is a big-endian size_t. |
|
22479
fb45791150d1
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
Tim Peters <tim.peters@gmail.com>
parents:
22470
diff
changeset
|
1308 If "bad memory" is detected later, the serial number gives an |
|
fb45791150d1
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
Tim Peters <tim.peters@gmail.com>
parents:
22470
diff
changeset
|
1309 excellent way to set a breakpoint on the next run, to capture the |
|
fb45791150d1
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
Tim Peters <tim.peters@gmail.com>
parents:
22470
diff
changeset
|
1310 instant at which this block was passed out. |
|
fb45791150d1
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
Tim Peters <tim.peters@gmail.com>
parents:
22470
diff
changeset
|
1311 */ |
|
fb45791150d1
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
Tim Peters <tim.peters@gmail.com>
parents:
22470
diff
changeset
|
1312 |
|
fb45791150d1
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
Tim Peters <tim.peters@gmail.com>
parents:
22470
diff
changeset
|
1313 void * |
|
22983
1c26a3106653
Move PyObject_Malloc and PyObject_Free here from object.c. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
22948
diff
changeset
|
1314 _PyObject_DebugMalloc(size_t nbytes) |
|
22479
fb45791150d1
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
Tim Peters <tim.peters@gmail.com>
parents:
22470
diff
changeset
|
1315 { |
|
fb45791150d1
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
Tim Peters <tim.peters@gmail.com>
parents:
22470
diff
changeset
|
1316 uchar *p; /* base address of malloc'ed block */ |
|
38575
5722f112d5da
In a PYMALLOC_DEBUG build obmalloc adds extra debugging info
Tim Peters <tim.peters@gmail.com>
parents:
37598
diff
changeset
|
1317 uchar *tail; /* p + 2*SST + nbytes == pointer to tail pad bytes */ |
|
5722f112d5da
In a PYMALLOC_DEBUG build obmalloc adds extra debugging info
Tim Peters <tim.peters@gmail.com>
parents:
37598
diff
changeset
|
1318 size_t total; /* nbytes + 4*SST */ |
|
22479
fb45791150d1
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
Tim Peters <tim.peters@gmail.com>
parents:
22470
diff
changeset
|
1319 |
|
22487
f02cdb2bc17a
_PyMalloc_DebugRealloc(): simplify decl of "fresh".
Tim Peters <tim.peters@gmail.com>
parents:
22484
diff
changeset
|
1320 bumpserialno(); |
|
38575
5722f112d5da
In a PYMALLOC_DEBUG build obmalloc adds extra debugging info
Tim Peters <tim.peters@gmail.com>
parents:
37598
diff
changeset
|
1321 total = nbytes + 4*SST; |
|
5722f112d5da
In a PYMALLOC_DEBUG build obmalloc adds extra debugging info
Tim Peters <tim.peters@gmail.com>
parents:
37598
diff
changeset
|
1322 if (total < nbytes) |
|
5722f112d5da
In a PYMALLOC_DEBUG build obmalloc adds extra debugging info
Tim Peters <tim.peters@gmail.com>
parents:
37598
diff
changeset
|
1323 /* overflow: can't represent total as a size_t */ |
|
22479
fb45791150d1
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
Tim Peters <tim.peters@gmail.com>
parents:
22470
diff
changeset
|
1324 return NULL; |
|
fb45791150d1
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
Tim Peters <tim.peters@gmail.com>
parents:
22470
diff
changeset
|
1325 |
|
23009
8be6238748c4
Small anal correctness tweaks:
Tim Peters <tim.peters@gmail.com>
parents:
22988
diff
changeset
|
1326 p = (uchar *)PyObject_Malloc(total); |
|
22479
fb45791150d1
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
Tim Peters <tim.peters@gmail.com>
parents:
22470
diff
changeset
|
1327 if (p == NULL) |
|
fb45791150d1
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
Tim Peters <tim.peters@gmail.com>
parents:
22470
diff
changeset
|
1328 return NULL; |
|
fb45791150d1
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
Tim Peters <tim.peters@gmail.com>
parents:
22470
diff
changeset
|
1329 |
|
38575
5722f112d5da
In a PYMALLOC_DEBUG build obmalloc adds extra debugging info
Tim Peters <tim.peters@gmail.com>
parents:
37598
diff
changeset
|
1330 write_size_t(p, nbytes); |
|
5722f112d5da
In a PYMALLOC_DEBUG build obmalloc adds extra debugging info
Tim Peters <tim.peters@gmail.com>
parents:
37598
diff
changeset
|
1331 memset(p + SST, FORBIDDENBYTE, SST); |
|
22479
fb45791150d1
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
Tim Peters <tim.peters@gmail.com>
parents:
22470
diff
changeset
|
1332 |
|
fb45791150d1
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
Tim Peters <tim.peters@gmail.com>
parents:
22470
diff
changeset
|
1333 if (nbytes > 0) |
|
38575
5722f112d5da
In a PYMALLOC_DEBUG build obmalloc adds extra debugging info
Tim Peters <tim.peters@gmail.com>
parents:
37598
diff
changeset
|
1334 memset(p + 2*SST, CLEANBYTE, nbytes); |
|
22479
fb45791150d1
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
Tim Peters <tim.peters@gmail.com>
parents:
22470
diff
changeset
|
1335 |
|
38575
5722f112d5da
In a PYMALLOC_DEBUG build obmalloc adds extra debugging info
Tim Peters <tim.peters@gmail.com>
parents:
37598
diff
changeset
|
1336 tail = p + 2*SST + nbytes; |
|
5722f112d5da
In a PYMALLOC_DEBUG build obmalloc adds extra debugging info
Tim Peters <tim.peters@gmail.com>
parents:
37598
diff
changeset
|
1337 memset(tail, FORBIDDENBYTE, SST); |
|
5722f112d5da
In a PYMALLOC_DEBUG build obmalloc adds extra debugging info
Tim Peters <tim.peters@gmail.com>
parents:
37598
diff
changeset
|
1338 write_size_t(tail + SST, serialno); |
|
22479
fb45791150d1
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
Tim Peters <tim.peters@gmail.com>
parents:
22470
diff
changeset
|
1339 |
|
38576
82072a8f5267
_PyObject_DebugMalloc(): The return value should add
Tim Peters <tim.peters@gmail.com>
parents:
38575
diff
changeset
|
1340 return p + 2*SST; |
|
22479
fb45791150d1
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
Tim Peters <tim.peters@gmail.com>
parents:
22470
diff
changeset
|
1341 } |
|
fb45791150d1
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
Tim Peters <tim.peters@gmail.com>
parents:
22470
diff
changeset
|
1342 |
|
38575
5722f112d5da
In a PYMALLOC_DEBUG build obmalloc adds extra debugging info
Tim Peters <tim.peters@gmail.com>
parents:
37598
diff
changeset
|
1343 /* The debug free first checks the 2*SST bytes on each end for sanity (in |
|
22986
298a7c51d2c0
PYMALLOC_{CLEAN, DEAD, FORBIDDEN}BYTE symbols: remove the PYMALLOC_
Tim Peters <tim.peters@gmail.com>
parents:
22983
diff
changeset
|
1344 particular, that the FORBIDDENBYTEs are still intact). |
|
298a7c51d2c0
PYMALLOC_{CLEAN, DEAD, FORBIDDEN}BYTE symbols: remove the PYMALLOC_
Tim Peters <tim.peters@gmail.com>
parents:
22983
diff
changeset
|
1345 Then fills the original bytes with DEADBYTE. |
|
22479
fb45791150d1
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
Tim Peters <tim.peters@gmail.com>
parents:
22470
diff
changeset
|
1346 Then calls the underlying free. |
|
fb45791150d1
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
Tim Peters <tim.peters@gmail.com>
parents:
22470
diff
changeset
|
1347 */ |
|
fb45791150d1
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
Tim Peters <tim.peters@gmail.com>
parents:
22470
diff
changeset
|
1348 void |
|
22983
1c26a3106653
Move PyObject_Malloc and PyObject_Free here from object.c. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
22948
diff
changeset
|
1349 _PyObject_DebugFree(void *p) |
|
22479
fb45791150d1
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
Tim Peters <tim.peters@gmail.com>
parents:
22470
diff
changeset
|
1350 { |
|
38575
5722f112d5da
In a PYMALLOC_DEBUG build obmalloc adds extra debugging info
Tim Peters <tim.peters@gmail.com>
parents:
37598
diff
changeset
|
1351 uchar *q = (uchar *)p - 2*SST; /* address returned from malloc */ |
|
22479
fb45791150d1
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
Tim Peters <tim.peters@gmail.com>
parents:
22470
diff
changeset
|
1352 size_t nbytes; |
|
fb45791150d1
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
Tim Peters <tim.peters@gmail.com>
parents:
22470
diff
changeset
|
1353 |
|
fb45791150d1
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
Tim Peters <tim.peters@gmail.com>
parents:
22470
diff
changeset
|
1354 if (p == NULL) |
|
fb45791150d1
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
Tim Peters <tim.peters@gmail.com>
parents:
22470
diff
changeset
|
1355 return; |
|
22983
1c26a3106653
Move PyObject_Malloc and PyObject_Free here from object.c. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
22948
diff
changeset
|
1356 _PyObject_DebugCheckAddress(p); |
|
38575
5722f112d5da
In a PYMALLOC_DEBUG build obmalloc adds extra debugging info
Tim Peters <tim.peters@gmail.com>
parents:
37598
diff
changeset
|
1357 nbytes = read_size_t(q); |
|
22479
fb45791150d1
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
Tim Peters <tim.peters@gmail.com>
parents:
22470
diff
changeset
|
1358 if (nbytes > 0) |
|
22986
298a7c51d2c0
PYMALLOC_{CLEAN, DEAD, FORBIDDEN}BYTE symbols: remove the PYMALLOC_
Tim Peters <tim.peters@gmail.com>
parents:
22983
diff
changeset
|
1359 memset(q, DEADBYTE, nbytes); |
|
38575
5722f112d5da
In a PYMALLOC_DEBUG build obmalloc adds extra debugging info
Tim Peters <tim.peters@gmail.com>
parents:
37598
diff
changeset
|
1360 PyObject_Free(q); |
|
22479
fb45791150d1
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
Tim Peters <tim.peters@gmail.com>
parents:
22470
diff
changeset
|
1361 } |
|
fb45791150d1
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
Tim Peters <tim.peters@gmail.com>
parents:
22470
diff
changeset
|
1362 |
|
fb45791150d1
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
Tim Peters <tim.peters@gmail.com>
parents:
22470
diff
changeset
|
1363 void * |
|
22983
1c26a3106653
Move PyObject_Malloc and PyObject_Free here from object.c. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
22948
diff
changeset
|
1364 _PyObject_DebugRealloc(void *p, size_t nbytes) |
|
22479
fb45791150d1
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
Tim Peters <tim.peters@gmail.com>
parents:
22470
diff
changeset
|
1365 { |
|
fb45791150d1
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
Tim Peters <tim.peters@gmail.com>
parents:
22470
diff
changeset
|
1366 uchar *q = (uchar *)p; |
|
22988
be54465a39c4
_PyObject_DebugRealloc(): rewritten to let the underlying realloc do
Tim Peters <tim.peters@gmail.com>
parents:
22987
diff
changeset
|
1367 uchar *tail; |
|
38575
5722f112d5da
In a PYMALLOC_DEBUG build obmalloc adds extra debugging info
Tim Peters <tim.peters@gmail.com>
parents:
37598
diff
changeset
|
1368 size_t total; /* nbytes + 4*SST */ |
|
22479
fb45791150d1
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
Tim Peters <tim.peters@gmail.com>
parents:
22470
diff
changeset
|
1369 size_t original_nbytes; |
|
38575
5722f112d5da
In a PYMALLOC_DEBUG build obmalloc adds extra debugging info
Tim Peters <tim.peters@gmail.com>
parents:
37598
diff
changeset
|
1370 int i; |
|
22479
fb45791150d1
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
Tim Peters <tim.peters@gmail.com>
parents:
22470
diff
changeset
|
1371 |
|
22614
f294fdd18b5b
PYMALLOC_DEBUG routines: The "check API family" gimmick was going nowhere
Tim Peters <tim.peters@gmail.com>
parents:
22487
diff
changeset
|
1372 if (p == NULL) |
|
22983
1c26a3106653
Move PyObject_Malloc and PyObject_Free here from object.c. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
22948
diff
changeset
|
1373 return _PyObject_DebugMalloc(nbytes); |
|
22479
fb45791150d1
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
Tim Peters <tim.peters@gmail.com>
parents:
22470
diff
changeset
|
1374 |
|
22983
1c26a3106653
Move PyObject_Malloc and PyObject_Free here from object.c. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
22948
diff
changeset
|
1375 _PyObject_DebugCheckAddress(p); |
|
22988
be54465a39c4
_PyObject_DebugRealloc(): rewritten to let the underlying realloc do
Tim Peters <tim.peters@gmail.com>
parents:
22987
diff
changeset
|
1376 bumpserialno(); |
|
38575
5722f112d5da
In a PYMALLOC_DEBUG build obmalloc adds extra debugging info
Tim Peters <tim.peters@gmail.com>
parents:
37598
diff
changeset
|
1377 original_nbytes = read_size_t(q - 2*SST); |
|
5722f112d5da
In a PYMALLOC_DEBUG build obmalloc adds extra debugging info
Tim Peters <tim.peters@gmail.com>
parents:
37598
diff
changeset
|
1378 total = nbytes + 4*SST; |
|
5722f112d5da
In a PYMALLOC_DEBUG build obmalloc adds extra debugging info
Tim Peters <tim.peters@gmail.com>
parents:
37598
diff
changeset
|
1379 if (total < nbytes) |
|
5722f112d5da
In a PYMALLOC_DEBUG build obmalloc adds extra debugging info
Tim Peters <tim.peters@gmail.com>
parents:
37598
diff
changeset
|
1380 /* overflow: can't represent total as a size_t */ |
|
22988
be54465a39c4
_PyObject_DebugRealloc(): rewritten to let the underlying realloc do
Tim Peters <tim.peters@gmail.com>
parents:
22987
diff
changeset
|
1381 return NULL; |
|
22479
fb45791150d1
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
Tim Peters <tim.peters@gmail.com>
parents:
22470
diff
changeset
|
1382 |
|
fb45791150d1
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
Tim Peters <tim.peters@gmail.com>
parents:
22470
diff
changeset
|
1383 if (nbytes < original_nbytes) { |
|
22988
be54465a39c4
_PyObject_DebugRealloc(): rewritten to let the underlying realloc do
Tim Peters <tim.peters@gmail.com>
parents:
22987
diff
changeset
|
1384 /* shrinking: mark old extra memory dead */ |
|
be54465a39c4
_PyObject_DebugRealloc(): rewritten to let the underlying realloc do
Tim Peters <tim.peters@gmail.com>
parents:
22987
diff
changeset
|
1385 memset(q + nbytes, DEADBYTE, original_nbytes - nbytes); |
|
22479
fb45791150d1
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
Tim Peters <tim.peters@gmail.com>
parents:
22470
diff
changeset
|
1386 } |
|
fb45791150d1
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
Tim Peters <tim.peters@gmail.com>
parents:
22470
diff
changeset
|
1387 |
|
22988
be54465a39c4
_PyObject_DebugRealloc(): rewritten to let the underlying realloc do
Tim Peters <tim.peters@gmail.com>
parents:
22987
diff
changeset
|
1388 /* Resize and add decorations. */ |
|
38575
5722f112d5da
In a PYMALLOC_DEBUG build obmalloc adds extra debugging info
Tim Peters <tim.peters@gmail.com>
parents:
37598
diff
changeset
|
1389 q = (uchar *)PyObject_Realloc(q - 2*SST, total); |
|
22988
be54465a39c4
_PyObject_DebugRealloc(): rewritten to let the underlying realloc do
Tim Peters <tim.peters@gmail.com>
parents:
22987
diff
changeset
|
1390 if (q == NULL) |
|
be54465a39c4
_PyObject_DebugRealloc(): rewritten to let the underlying realloc do
Tim Peters <tim.peters@gmail.com>
parents:
22987
diff
changeset
|
1391 return NULL; |
|
be54465a39c4
_PyObject_DebugRealloc(): rewritten to let the underlying realloc do
Tim Peters <tim.peters@gmail.com>
parents:
22987
diff
changeset
|
1392 |
|
38575
5722f112d5da
In a PYMALLOC_DEBUG build obmalloc adds extra debugging info
Tim Peters <tim.peters@gmail.com>
parents:
37598
diff
changeset
|
1393 write_size_t(q, nbytes); |
|
5722f112d5da
In a PYMALLOC_DEBUG build obmalloc adds extra debugging info
Tim Peters <tim.peters@gmail.com>
parents:
37598
diff
changeset
|
1394 for (i = 0; i < SST; ++i) |
|
5722f112d5da
In a PYMALLOC_DEBUG build obmalloc adds extra debugging info
Tim Peters <tim.peters@gmail.com>
parents:
37598
diff
changeset
|
1395 assert(q[SST + i] == FORBIDDENBYTE); |
|
5722f112d5da
In a PYMALLOC_DEBUG build obmalloc adds extra debugging info
Tim Peters <tim.peters@gmail.com>
parents:
37598
diff
changeset
|
1396 q += 2*SST; |
|
22988
be54465a39c4
_PyObject_DebugRealloc(): rewritten to let the underlying realloc do
Tim Peters <tim.peters@gmail.com>
parents:
22987
diff
changeset
|
1397 tail = q + nbytes; |
|
38575
5722f112d5da
In a PYMALLOC_DEBUG build obmalloc adds extra debugging info
Tim Peters <tim.peters@gmail.com>
parents:
37598
diff
changeset
|
1398 memset(tail, FORBIDDENBYTE, SST); |
|
5722f112d5da
In a PYMALLOC_DEBUG build obmalloc adds extra debugging info
Tim Peters <tim.peters@gmail.com>
parents:
37598
diff
changeset
|
1399 write_size_t(tail + SST, serialno); |
|
22988
be54465a39c4
_PyObject_DebugRealloc(): rewritten to let the underlying realloc do
Tim Peters <tim.peters@gmail.com>
parents:
22987
diff
changeset
|
1400 |
|
be54465a39c4
_PyObject_DebugRealloc(): rewritten to let the underlying realloc do
Tim Peters <tim.peters@gmail.com>
parents:
22987
diff
changeset
|
1401 if (nbytes > original_nbytes) { |
|
be54465a39c4
_PyObject_DebugRealloc(): rewritten to let the underlying realloc do
Tim Peters <tim.peters@gmail.com>
parents:
22987
diff
changeset
|
1402 /* growing: mark new extra memory clean */ |
|
be54465a39c4
_PyObject_DebugRealloc(): rewritten to let the underlying realloc do
Tim Peters <tim.peters@gmail.com>
parents:
22987
diff
changeset
|
1403 memset(q + original_nbytes, CLEANBYTE, |
|
be54465a39c4
_PyObject_DebugRealloc(): rewritten to let the underlying realloc do
Tim Peters <tim.peters@gmail.com>
parents:
22987
diff
changeset
|
1404 nbytes - original_nbytes); |
|
22948
6cb52829da1a
SF bug 542181: Realloc behavior
Tim Peters <tim.peters@gmail.com>
parents:
22860
diff
changeset
|
1405 } |
|
22988
be54465a39c4
_PyObject_DebugRealloc(): rewritten to let the underlying realloc do
Tim Peters <tim.peters@gmail.com>
parents:
22987
diff
changeset
|
1406 |
|
be54465a39c4
_PyObject_DebugRealloc(): rewritten to let the underlying realloc do
Tim Peters <tim.peters@gmail.com>
parents:
22987
diff
changeset
|
1407 return q; |
|
22479
fb45791150d1
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
Tim Peters <tim.peters@gmail.com>
parents:
22470
diff
changeset
|
1408 } |
|
fb45791150d1
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
Tim Peters <tim.peters@gmail.com>
parents:
22470
diff
changeset
|
1409 |
|
22724
9baa8c9a1e84
New PYMALLOC_DEBUG function void _PyMalloc_DebugDumpStats(void).
Tim Peters <tim.peters@gmail.com>
parents:
22701
diff
changeset
|
1410 /* Check the forbidden bytes on both ends of the memory allocated for p. |
|
22983
1c26a3106653
Move PyObject_Malloc and PyObject_Free here from object.c. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
22948
diff
changeset
|
1411 * If anything is wrong, print info to stderr via _PyObject_DebugDumpAddress, |
|
22724
9baa8c9a1e84
New PYMALLOC_DEBUG function void _PyMalloc_DebugDumpStats(void).
Tim Peters <tim.peters@gmail.com>
parents:
22701
diff
changeset
|
1412 * and call Py_FatalError to kill the program. |
|
9baa8c9a1e84
New PYMALLOC_DEBUG function void _PyMalloc_DebugDumpStats(void).
Tim Peters <tim.peters@gmail.com>
parents:
22701
diff
changeset
|
1413 */ |
|
9baa8c9a1e84
New PYMALLOC_DEBUG function void _PyMalloc_DebugDumpStats(void).
Tim Peters <tim.peters@gmail.com>
parents:
22701
diff
changeset
|
1414 void |
|
22983
1c26a3106653
Move PyObject_Malloc and PyObject_Free here from object.c. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
22948
diff
changeset
|
1415 _PyObject_DebugCheckAddress(const void *p) |
|
22479
fb45791150d1
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
Tim Peters <tim.peters@gmail.com>
parents:
22470
diff
changeset
|
1416 { |
|
fb45791150d1
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
Tim Peters <tim.peters@gmail.com>
parents:
22470
diff
changeset
|
1417 const uchar *q = (const uchar *)p; |
|
22614
f294fdd18b5b
PYMALLOC_DEBUG routines: The "check API family" gimmick was going nowhere
Tim Peters <tim.peters@gmail.com>
parents:
22487
diff
changeset
|
1418 char *msg; |
|
38575
5722f112d5da
In a PYMALLOC_DEBUG build obmalloc adds extra debugging info
Tim Peters <tim.peters@gmail.com>
parents:
37598
diff
changeset
|
1419 size_t nbytes; |
|
23298
5a6be8fa8de2
_PyObject_DebugCheckAddress(): If the leading pad bytes are corrupt,
Tim Peters <tim.peters@gmail.com>
parents:
23159
diff
changeset
|
1420 const uchar *tail; |
|
22614
f294fdd18b5b
PYMALLOC_DEBUG routines: The "check API family" gimmick was going nowhere
Tim Peters <tim.peters@gmail.com>
parents:
22487
diff
changeset
|
1421 int i; |
|
22479
fb45791150d1
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
Tim Peters <tim.peters@gmail.com>
parents:
22470
diff
changeset
|
1422 |
|
22614
f294fdd18b5b
PYMALLOC_DEBUG routines: The "check API family" gimmick was going nowhere
Tim Peters <tim.peters@gmail.com>
parents:
22487
diff
changeset
|
1423 if (p == NULL) { |
|
22479
fb45791150d1
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
Tim Peters <tim.peters@gmail.com>
parents:
22470
diff
changeset
|
1424 msg = "didn't expect a NULL pointer"; |
|
22614
f294fdd18b5b
PYMALLOC_DEBUG routines: The "check API family" gimmick was going nowhere
Tim Peters <tim.peters@gmail.com>
parents:
22487
diff
changeset
|
1425 goto error; |
|
f294fdd18b5b
PYMALLOC_DEBUG routines: The "check API family" gimmick was going nowhere
Tim Peters <tim.peters@gmail.com>
parents:
22487
diff
changeset
|
1426 } |
|
22479
fb45791150d1
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
Tim Peters <tim.peters@gmail.com>
parents:
22470
diff
changeset
|
1427 |
|
23298
5a6be8fa8de2
_PyObject_DebugCheckAddress(): If the leading pad bytes are corrupt,
Tim Peters <tim.peters@gmail.com>
parents:
23159
diff
changeset
|
1428 /* Check the stuff at the start of p first: if there's underwrite |
|
5a6be8fa8de2
_PyObject_DebugCheckAddress(): If the leading pad bytes are corrupt,
Tim Peters <tim.peters@gmail.com>
parents:
23159
diff
changeset
|
1429 * corruption, the number-of-bytes field may be nuts, and checking |
|
5a6be8fa8de2
_PyObject_DebugCheckAddress(): If the leading pad bytes are corrupt,
Tim Peters <tim.peters@gmail.com>
parents:
23159
diff
changeset
|
1430 * the tail could lead to a segfault then. |
|
5a6be8fa8de2
_PyObject_DebugCheckAddress(): If the leading pad bytes are corrupt,
Tim Peters <tim.peters@gmail.com>
parents:
23159
diff
changeset
|
1431 */ |
|
38575
5722f112d5da
In a PYMALLOC_DEBUG build obmalloc adds extra debugging info
Tim Peters <tim.peters@gmail.com>
parents:
37598
diff
changeset
|
1432 for (i = SST; i >= 1; --i) { |
|
22986
298a7c51d2c0
PYMALLOC_{CLEAN, DEAD, FORBIDDEN}BYTE symbols: remove the PYMALLOC_
Tim Peters <tim.peters@gmail.com>
parents:
22983
diff
changeset
|
1433 if (*(q-i) != FORBIDDENBYTE) { |
|
22614
f294fdd18b5b
PYMALLOC_DEBUG routines: The "check API family" gimmick was going nowhere
Tim Peters <tim.peters@gmail.com>
parents:
22487
diff
changeset
|
1434 msg = "bad leading pad byte"; |
|
f294fdd18b5b
PYMALLOC_DEBUG routines: The "check API family" gimmick was going nowhere
Tim Peters <tim.peters@gmail.com>
parents:
22487
diff
changeset
|
1435 goto error; |
|
f294fdd18b5b
PYMALLOC_DEBUG routines: The "check API family" gimmick was going nowhere
Tim Peters <tim.peters@gmail.com>
parents:
22487
diff
changeset
|
1436 } |
|
f294fdd18b5b
PYMALLOC_DEBUG routines: The "check API family" gimmick was going nowhere
Tim Peters <tim.peters@gmail.com>
parents:
22487
diff
changeset
|
1437 } |
|
22479
fb45791150d1
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
Tim Peters <tim.peters@gmail.com>
parents:
22470
diff
changeset
|
1438 |
|
38575
5722f112d5da
In a PYMALLOC_DEBUG build obmalloc adds extra debugging info
Tim Peters <tim.peters@gmail.com>
parents:
37598
diff
changeset
|
1439 nbytes = read_size_t(q - 2*SST); |
|
23298
5a6be8fa8de2
_PyObject_DebugCheckAddress(): If the leading pad bytes are corrupt,
Tim Peters <tim.peters@gmail.com>
parents:
23159
diff
changeset
|
1440 tail = q + nbytes; |
|
38575
5722f112d5da
In a PYMALLOC_DEBUG build obmalloc adds extra debugging info
Tim Peters <tim.peters@gmail.com>
parents:
37598
diff
changeset
|
1441 for (i = 0; i < SST; ++i) { |
|
23298
5a6be8fa8de2
_PyObject_DebugCheckAddress(): If the leading pad bytes are corrupt,
Tim Peters <tim.peters@gmail.com>
parents:
23159
diff
changeset
|
1442 if (tail[i] != FORBIDDENBYTE) { |
|
5a6be8fa8de2
_PyObject_DebugCheckAddress(): If the leading pad bytes are corrupt,
Tim Peters <tim.peters@gmail.com>
parents:
23159
diff
changeset
|
1443 msg = "bad trailing pad byte"; |
|
5a6be8fa8de2
_PyObject_DebugCheckAddress(): If the leading pad bytes are corrupt,
Tim Peters <tim.peters@gmail.com>
parents:
23159
diff
changeset
|
1444 goto error; |
|
22479
fb45791150d1
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
Tim Peters <tim.peters@gmail.com>
parents:
22470
diff
changeset
|
1445 } |
|
fb45791150d1
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
Tim Peters <tim.peters@gmail.com>
parents:
22470
diff
changeset
|
1446 } |
|
fb45791150d1
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
Tim Peters <tim.peters@gmail.com>
parents:
22470
diff
changeset
|
1447 |
|
22614
f294fdd18b5b
PYMALLOC_DEBUG routines: The "check API family" gimmick was going nowhere
Tim Peters <tim.peters@gmail.com>
parents:
22487
diff
changeset
|
1448 return; |
|
f294fdd18b5b
PYMALLOC_DEBUG routines: The "check API family" gimmick was going nowhere
Tim Peters <tim.peters@gmail.com>
parents:
22487
diff
changeset
|
1449 |
|
f294fdd18b5b
PYMALLOC_DEBUG routines: The "check API family" gimmick was going nowhere
Tim Peters <tim.peters@gmail.com>
parents:
22487
diff
changeset
|
1450 error: |
|
22983
1c26a3106653
Move PyObject_Malloc and PyObject_Free here from object.c. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
22948
diff
changeset
|
1451 _PyObject_DebugDumpAddress(p); |
|
22614
f294fdd18b5b
PYMALLOC_DEBUG routines: The "check API family" gimmick was going nowhere
Tim Peters <tim.peters@gmail.com>
parents:
22487
diff
changeset
|
1452 Py_FatalError(msg); |
|
22479
fb45791150d1
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
Tim Peters <tim.peters@gmail.com>
parents:
22470
diff
changeset
|
1453 } |
|
fb45791150d1
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
Tim Peters <tim.peters@gmail.com>
parents:
22470
diff
changeset
|
1454 |
|
22724
9baa8c9a1e84
New PYMALLOC_DEBUG function void _PyMalloc_DebugDumpStats(void).
Tim Peters <tim.peters@gmail.com>
parents:
22701
diff
changeset
|
1455 /* Display info to stderr about the memory block at p. */ |
|
22479
fb45791150d1
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
Tim Peters <tim.peters@gmail.com>
parents:
22470
diff
changeset
|
1456 void |
|
22983
1c26a3106653
Move PyObject_Malloc and PyObject_Free here from object.c. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
22948
diff
changeset
|
1457 _PyObject_DebugDumpAddress(const void *p) |
|
22479
fb45791150d1
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
Tim Peters <tim.peters@gmail.com>
parents:
22470
diff
changeset
|
1458 { |
|
fb45791150d1
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
Tim Peters <tim.peters@gmail.com>
parents:
22470
diff
changeset
|
1459 const uchar *q = (const uchar *)p; |
|
fb45791150d1
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
Tim Peters <tim.peters@gmail.com>
parents:
22470
diff
changeset
|
1460 const uchar *tail; |
|
38575
5722f112d5da
In a PYMALLOC_DEBUG build obmalloc adds extra debugging info
Tim Peters <tim.peters@gmail.com>
parents:
37598
diff
changeset
|
1461 size_t nbytes, serial; |
|
22614
f294fdd18b5b
PYMALLOC_DEBUG routines: The "check API family" gimmick was going nowhere
Tim Peters <tim.peters@gmail.com>
parents:
22487
diff
changeset
|
1462 int i; |
|
38575
5722f112d5da
In a PYMALLOC_DEBUG build obmalloc adds extra debugging info
Tim Peters <tim.peters@gmail.com>
parents:
37598
diff
changeset
|
1463 int ok; |
|
22479
fb45791150d1
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
Tim Peters <tim.peters@gmail.com>
parents:
22470
diff
changeset
|
1464 |
|
fb45791150d1
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
Tim Peters <tim.peters@gmail.com>
parents:
22470
diff
changeset
|
1465 fprintf(stderr, "Debug memory block at address p=%p:\n", p); |
|
fb45791150d1
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
Tim Peters <tim.peters@gmail.com>
parents:
22470
diff
changeset
|
1466 if (p == NULL) |
|
fb45791150d1
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
Tim Peters <tim.peters@gmail.com>
parents:
22470
diff
changeset
|
1467 return; |
|
fb45791150d1
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
Tim Peters <tim.peters@gmail.com>
parents:
22470
diff
changeset
|
1468 |
|
38575
5722f112d5da
In a PYMALLOC_DEBUG build obmalloc adds extra debugging info
Tim Peters <tim.peters@gmail.com>
parents:
37598
diff
changeset
|
1469 nbytes = read_size_t(q - 2*SST); |
|
5722f112d5da
In a PYMALLOC_DEBUG build obmalloc adds extra debugging info
Tim Peters <tim.peters@gmail.com>
parents:
37598
diff
changeset
|
1470 fprintf(stderr, " %" PY_FORMAT_SIZE_T "u bytes originally " |
|
5722f112d5da
In a PYMALLOC_DEBUG build obmalloc adds extra debugging info
Tim Peters <tim.peters@gmail.com>
parents:
37598
diff
changeset
|
1471 "requested\n", nbytes); |
|
22479
fb45791150d1
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
Tim Peters <tim.peters@gmail.com>
parents:
22470
diff
changeset
|
1472 |
|
23298
5a6be8fa8de2
_PyObject_DebugCheckAddress(): If the leading pad bytes are corrupt,
Tim Peters <tim.peters@gmail.com>
parents:
23159
diff
changeset
|
1473 /* In case this is nuts, check the leading pad bytes first. */ |
|
38575
5722f112d5da
In a PYMALLOC_DEBUG build obmalloc adds extra debugging info
Tim Peters <tim.peters@gmail.com>
parents:
37598
diff
changeset
|
1474 fprintf(stderr, " The %d pad bytes at p-%d are ", SST, SST); |
|
5722f112d5da
In a PYMALLOC_DEBUG build obmalloc adds extra debugging info
Tim Peters <tim.peters@gmail.com>
parents:
37598
diff
changeset
|
1475 ok = 1; |
|
5722f112d5da
In a PYMALLOC_DEBUG build obmalloc adds extra debugging info
Tim Peters <tim.peters@gmail.com>
parents:
37598
diff
changeset
|
1476 for (i = 1; i <= SST; ++i) { |
|
5722f112d5da
In a PYMALLOC_DEBUG build obmalloc adds extra debugging info
Tim Peters <tim.peters@gmail.com>
parents:
37598
diff
changeset
|
1477 if (*(q-i) != FORBIDDENBYTE) { |
|
5722f112d5da
In a PYMALLOC_DEBUG build obmalloc adds extra debugging info
Tim Peters <tim.peters@gmail.com>
parents:
37598
diff
changeset
|
1478 ok = 0; |
|
5722f112d5da
In a PYMALLOC_DEBUG build obmalloc adds extra debugging info
Tim Peters <tim.peters@gmail.com>
parents:
37598
diff
changeset
|
1479 break; |
|
5722f112d5da
In a PYMALLOC_DEBUG build obmalloc adds extra debugging info
Tim Peters <tim.peters@gmail.com>
parents:
37598
diff
changeset
|
1480 } |
|
5722f112d5da
In a PYMALLOC_DEBUG build obmalloc adds extra debugging info
Tim Peters <tim.peters@gmail.com>
parents:
37598
diff
changeset
|
1481 } |
|
5722f112d5da
In a PYMALLOC_DEBUG build obmalloc adds extra debugging info
Tim Peters <tim.peters@gmail.com>
parents:
37598
diff
changeset
|
1482 if (ok) |
|
23298
5a6be8fa8de2
_PyObject_DebugCheckAddress(): If the leading pad bytes are corrupt,
Tim Peters <tim.peters@gmail.com>
parents:
23159
diff
changeset
|
1483 fputs("FORBIDDENBYTE, as expected.\n", stderr); |
|
22479
fb45791150d1
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
Tim Peters <tim.peters@gmail.com>
parents:
22470
diff
changeset
|
1484 else { |
|
22986
298a7c51d2c0
PYMALLOC_{CLEAN, DEAD, FORBIDDEN}BYTE symbols: remove the PYMALLOC_
Tim Peters <tim.peters@gmail.com>
parents:
22983
diff
changeset
|
1485 fprintf(stderr, "not all FORBIDDENBYTE (0x%02x):\n", |
|
298a7c51d2c0
PYMALLOC_{CLEAN, DEAD, FORBIDDEN}BYTE symbols: remove the PYMALLOC_
Tim Peters <tim.peters@gmail.com>
parents:
22983
diff
changeset
|
1486 FORBIDDENBYTE); |
|
38575
5722f112d5da
In a PYMALLOC_DEBUG build obmalloc adds extra debugging info
Tim Peters <tim.peters@gmail.com>
parents:
37598
diff
changeset
|
1487 for (i = SST; i >= 1; --i) { |
|
22479
fb45791150d1
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
Tim Peters <tim.peters@gmail.com>
parents:
22470
diff
changeset
|
1488 const uchar byte = *(q-i); |
|
fb45791150d1
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
Tim Peters <tim.peters@gmail.com>
parents:
22470
diff
changeset
|
1489 fprintf(stderr, " at p-%d: 0x%02x", i, byte); |
|
22986
298a7c51d2c0
PYMALLOC_{CLEAN, DEAD, FORBIDDEN}BYTE symbols: remove the PYMALLOC_
Tim Peters <tim.peters@gmail.com>
parents:
22983
diff
changeset
|
1490 if (byte != FORBIDDENBYTE) |
|
22479
fb45791150d1
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
Tim Peters <tim.peters@gmail.com>
parents:
22470
diff
changeset
|
1491 fputs(" *** OUCH", stderr); |
|
fb45791150d1
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
Tim Peters <tim.peters@gmail.com>
parents:
22470
diff
changeset
|
1492 fputc('\n', stderr); |
|
fb45791150d1
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
Tim Peters <tim.peters@gmail.com>
parents:
22470
diff
changeset
|
1493 } |
|
23298
5a6be8fa8de2
_PyObject_DebugCheckAddress(): If the leading pad bytes are corrupt,
Tim Peters <tim.peters@gmail.com>
parents:
23159
diff
changeset
|
1494 |
|
5a6be8fa8de2
_PyObject_DebugCheckAddress(): If the leading pad bytes are corrupt,
Tim Peters <tim.peters@gmail.com>
parents:
23159
diff
changeset
|
1495 fputs(" Because memory is corrupted at the start, the " |
|
5a6be8fa8de2
_PyObject_DebugCheckAddress(): If the leading pad bytes are corrupt,
Tim Peters <tim.peters@gmail.com>
parents:
23159
diff
changeset
|
1496 "count of bytes requested\n" |
|
5a6be8fa8de2
_PyObject_DebugCheckAddress(): If the leading pad bytes are corrupt,
Tim Peters <tim.peters@gmail.com>
parents:
23159
diff
changeset
|
1497 " may be bogus, and checking the trailing pad " |
|
5a6be8fa8de2
_PyObject_DebugCheckAddress(): If the leading pad bytes are corrupt,
Tim Peters <tim.peters@gmail.com>
parents:
23159
diff
changeset
|
1498 "bytes may segfault.\n", stderr); |
|
22479
fb45791150d1
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
Tim Peters <tim.peters@gmail.com>
parents:
22470
diff
changeset
|
1499 } |
|
fb45791150d1
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
Tim Peters <tim.peters@gmail.com>
parents:
22470
diff
changeset
|
1500 |
|
fb45791150d1
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
Tim Peters <tim.peters@gmail.com>
parents:
22470
diff
changeset
|
1501 tail = q + nbytes; |
|
38575
5722f112d5da
In a PYMALLOC_DEBUG build obmalloc adds extra debugging info
Tim Peters <tim.peters@gmail.com>
parents:
37598
diff
changeset
|
1502 fprintf(stderr, " The %d pad bytes at tail=%p are ", SST, tail); |
|
5722f112d5da
In a PYMALLOC_DEBUG build obmalloc adds extra debugging info
Tim Peters <tim.peters@gmail.com>
parents:
37598
diff
changeset
|
1503 ok = 1; |
|
5722f112d5da
In a PYMALLOC_DEBUG build obmalloc adds extra debugging info
Tim Peters <tim.peters@gmail.com>
parents:
37598
diff
changeset
|
1504 for (i = 0; i < SST; ++i) { |
|
5722f112d5da
In a PYMALLOC_DEBUG build obmalloc adds extra debugging info
Tim Peters <tim.peters@gmail.com>
parents:
37598
diff
changeset
|
1505 if (tail[i] != FORBIDDENBYTE) { |
|
5722f112d5da
In a PYMALLOC_DEBUG build obmalloc adds extra debugging info
Tim Peters <tim.peters@gmail.com>
parents:
37598
diff
changeset
|
1506 ok = 0; |
|
5722f112d5da
In a PYMALLOC_DEBUG build obmalloc adds extra debugging info
Tim Peters <tim.peters@gmail.com>
parents:
37598
diff
changeset
|
1507 break; |
|
5722f112d5da
In a PYMALLOC_DEBUG build obmalloc adds extra debugging info
Tim Peters <tim.peters@gmail.com>
parents:
37598
diff
changeset
|
1508 } |
|
5722f112d5da
In a PYMALLOC_DEBUG build obmalloc adds extra debugging info
Tim Peters <tim.peters@gmail.com>
parents:
37598
diff
changeset
|
1509 } |
|
5722f112d5da
In a PYMALLOC_DEBUG build obmalloc adds extra debugging info
Tim Peters <tim.peters@gmail.com>
parents:
37598
diff
changeset
|
1510 if (ok) |
|
23298
5a6be8fa8de2
_PyObject_DebugCheckAddress(): If the leading pad bytes are corrupt,
Tim Peters <tim.peters@gmail.com>
parents:
23159
diff
changeset
|
1511 fputs("FORBIDDENBYTE, as expected.\n", stderr); |
|
22479
fb45791150d1
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
Tim Peters <tim.peters@gmail.com>
parents:
22470
diff
changeset
|
1512 else { |
|
22986
298a7c51d2c0
PYMALLOC_{CLEAN, DEAD, FORBIDDEN}BYTE symbols: remove the PYMALLOC_
Tim Peters <tim.peters@gmail.com>
parents:
22983
diff
changeset
|
1513 fprintf(stderr, "not all FORBIDDENBYTE (0x%02x):\n", |
|
298a7c51d2c0
PYMALLOC_{CLEAN, DEAD, FORBIDDEN}BYTE symbols: remove the PYMALLOC_
Tim Peters <tim.peters@gmail.com>
parents:
22983
diff
changeset
|
1514 FORBIDDENBYTE); |
|
38575
5722f112d5da
In a PYMALLOC_DEBUG build obmalloc adds extra debugging info
Tim Peters <tim.peters@gmail.com>
parents:
37598
diff
changeset
|
1515 for (i = 0; i < SST; ++i) { |
|
22479
fb45791150d1
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
Tim Peters <tim.peters@gmail.com>
parents:
22470
diff
changeset
|
1516 const uchar byte = tail[i]; |
|
fb45791150d1
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
Tim Peters <tim.peters@gmail.com>
parents:
22470
diff
changeset
|
1517 fprintf(stderr, " at tail+%d: 0x%02x", |
|
fb45791150d1
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
Tim Peters <tim.peters@gmail.com>
parents:
22470
diff
changeset
|
1518 i, byte); |
|
22986
298a7c51d2c0
PYMALLOC_{CLEAN, DEAD, FORBIDDEN}BYTE symbols: remove the PYMALLOC_
Tim Peters <tim.peters@gmail.com>
parents:
22983
diff
changeset
|
1519 if (byte != FORBIDDENBYTE) |
|
22479
fb45791150d1
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
Tim Peters <tim.peters@gmail.com>
parents:
22470
diff
changeset
|
1520 fputs(" *** OUCH", stderr); |
|
fb45791150d1
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
Tim Peters <tim.peters@gmail.com>
parents:
22470
diff
changeset
|
1521 fputc('\n', stderr); |
|
fb45791150d1
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
Tim Peters <tim.peters@gmail.com>
parents:
22470
diff
changeset
|
1522 } |
|
fb45791150d1
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
Tim Peters <tim.peters@gmail.com>
parents:
22470
diff
changeset
|
1523 } |
|
fb45791150d1
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
Tim Peters <tim.peters@gmail.com>
parents:
22470
diff
changeset
|
1524 |
|
38575
5722f112d5da
In a PYMALLOC_DEBUG build obmalloc adds extra debugging info
Tim Peters <tim.peters@gmail.com>
parents:
37598
diff
changeset
|
1525 serial = read_size_t(tail + SST); |
|
5722f112d5da
In a PYMALLOC_DEBUG build obmalloc adds extra debugging info
Tim Peters <tim.peters@gmail.com>
parents:
37598
diff
changeset
|
1526 fprintf(stderr, " The block was made by call #%" PY_FORMAT_SIZE_T |
|
5722f112d5da
In a PYMALLOC_DEBUG build obmalloc adds extra debugging info
Tim Peters <tim.peters@gmail.com>
parents:
37598
diff
changeset
|
1527 "u to debug malloc/realloc.\n", serial); |
|
22479
fb45791150d1
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
Tim Peters <tim.peters@gmail.com>
parents:
22470
diff
changeset
|
1528 |
|
fb45791150d1
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
Tim Peters <tim.peters@gmail.com>
parents:
22470
diff
changeset
|
1529 if (nbytes > 0) { |
|
38575
5722f112d5da
In a PYMALLOC_DEBUG build obmalloc adds extra debugging info
Tim Peters <tim.peters@gmail.com>
parents:
37598
diff
changeset
|
1530 i = 0; |
|
23298
5a6be8fa8de2
_PyObject_DebugCheckAddress(): If the leading pad bytes are corrupt,
Tim Peters <tim.peters@gmail.com>
parents:
23159
diff
changeset
|
1531 fputs(" Data at p:", stderr); |
|
22479
fb45791150d1
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
Tim Peters <tim.peters@gmail.com>
parents:
22470
diff
changeset
|
1532 /* print up to 8 bytes at the start */ |
|
fb45791150d1
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
Tim Peters <tim.peters@gmail.com>
parents:
22470
diff
changeset
|
1533 while (q < tail && i < 8) { |
|
fb45791150d1
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
Tim Peters <tim.peters@gmail.com>
parents:
22470
diff
changeset
|
1534 fprintf(stderr, " %02x", *q); |
|
fb45791150d1
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
Tim Peters <tim.peters@gmail.com>
parents:
22470
diff
changeset
|
1535 ++i; |
|
fb45791150d1
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
Tim Peters <tim.peters@gmail.com>
parents:
22470
diff
changeset
|
1536 ++q; |
|
fb45791150d1
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
Tim Peters <tim.peters@gmail.com>
parents:
22470
diff
changeset
|
1537 } |
|
fb45791150d1
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
Tim Peters <tim.peters@gmail.com>
parents:
22470
diff
changeset
|
1538 /* and up to 8 at the end */ |
|
fb45791150d1
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
Tim Peters <tim.peters@gmail.com>
parents:
22470
diff
changeset
|
1539 if (q < tail) { |
|
fb45791150d1
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
Tim Peters <tim.peters@gmail.com>
parents:
22470
diff
changeset
|
1540 if (tail - q > 8) { |
|
22484
ffcb2fd2947e
Minor code cleanup -- no semantic changes.
Tim Peters <tim.peters@gmail.com>
parents:
22479
diff
changeset
|
1541 fputs(" ...", stderr); |
|
22479
fb45791150d1
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
Tim Peters <tim.peters@gmail.com>
parents:
22470
diff
changeset
|
1542 q = tail - 8; |
|
fb45791150d1
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
Tim Peters <tim.peters@gmail.com>
parents:
22470
diff
changeset
|
1543 } |
|
fb45791150d1
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
Tim Peters <tim.peters@gmail.com>
parents:
22470
diff
changeset
|
1544 while (q < tail) { |
|
fb45791150d1
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
Tim Peters <tim.peters@gmail.com>
parents:
22470
diff
changeset
|
1545 fprintf(stderr, " %02x", *q); |
|
fb45791150d1
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
Tim Peters <tim.peters@gmail.com>
parents:
22470
diff
changeset
|
1546 ++q; |
|
fb45791150d1
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
Tim Peters <tim.peters@gmail.com>
parents:
22470
diff
changeset
|
1547 } |
|
fb45791150d1
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
Tim Peters <tim.peters@gmail.com>
parents:
22470
diff
changeset
|
1548 } |
|
22484
ffcb2fd2947e
Minor code cleanup -- no semantic changes.
Tim Peters <tim.peters@gmail.com>
parents:
22479
diff
changeset
|
1549 fputc('\n', stderr); |
|
22479
fb45791150d1
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
Tim Peters <tim.peters@gmail.com>
parents:
22470
diff
changeset
|
1550 } |
|
fb45791150d1
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
Tim Peters <tim.peters@gmail.com>
parents:
22470
diff
changeset
|
1551 } |
|
fb45791150d1
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
Tim Peters <tim.peters@gmail.com>
parents:
22470
diff
changeset
|
1552 |
|
38575
5722f112d5da
In a PYMALLOC_DEBUG build obmalloc adds extra debugging info
Tim Peters <tim.peters@gmail.com>
parents:
37598
diff
changeset
|
1553 static size_t |
|
5722f112d5da
In a PYMALLOC_DEBUG build obmalloc adds extra debugging info
Tim Peters <tim.peters@gmail.com>
parents:
37598
diff
changeset
|
1554 printone(const char* msg, size_t value) |
|
22837
ef2fbce71736
_PyMalloc_DebugDumpStats(): vastly improved the output, and it now
Tim Peters <tim.peters@gmail.com>
parents:
22836
diff
changeset
|
1555 { |
|
22860
04367cff8689
Minor improvements to the stats output dump, including adding commas to
Tim Peters <tim.peters@gmail.com>
parents:
22838
diff
changeset
|
1556 int i, k; |
|
04367cff8689
Minor improvements to the stats output dump, including adding commas to
Tim Peters <tim.peters@gmail.com>
parents:
22838
diff
changeset
|
1557 char buf[100]; |
|
38575
5722f112d5da
In a PYMALLOC_DEBUG build obmalloc adds extra debugging info
Tim Peters <tim.peters@gmail.com>
parents:
37598
diff
changeset
|
1558 size_t origvalue = value; |
|
22837
ef2fbce71736
_PyMalloc_DebugDumpStats(): vastly improved the output, and it now
Tim Peters <tim.peters@gmail.com>
parents:
22836
diff
changeset
|
1559 |
|
ef2fbce71736
_PyMalloc_DebugDumpStats(): vastly improved the output, and it now
Tim Peters <tim.peters@gmail.com>
parents:
22836
diff
changeset
|
1560 fputs(msg, stderr); |
|
22860
04367cff8689
Minor improvements to the stats output dump, including adding commas to
Tim Peters <tim.peters@gmail.com>
parents:
22838
diff
changeset
|
1561 for (i = (int)strlen(msg); i < 35; ++i) |
|
22837
ef2fbce71736
_PyMalloc_DebugDumpStats(): vastly improved the output, and it now
Tim Peters <tim.peters@gmail.com>
parents:
22836
diff
changeset
|
1562 fputc(' ', stderr); |
|
22860
04367cff8689
Minor improvements to the stats output dump, including adding commas to
Tim Peters <tim.peters@gmail.com>
parents:
22838
diff
changeset
|
1563 fputc('=', stderr); |
|
04367cff8689
Minor improvements to the stats output dump, including adding commas to
Tim Peters <tim.peters@gmail.com>
parents:
22838
diff
changeset
|
1564 |
|
04367cff8689
Minor improvements to the stats output dump, including adding commas to
Tim Peters <tim.peters@gmail.com>
parents:
22838
diff
changeset
|
1565 /* Write the value with commas. */ |
|
04367cff8689
Minor improvements to the stats output dump, including adding commas to
Tim Peters <tim.peters@gmail.com>
parents:
22838
diff
changeset
|
1566 i = 22; |
|
04367cff8689
Minor improvements to the stats output dump, including adding commas to
Tim Peters <tim.peters@gmail.com>
parents:
22838
diff
changeset
|
1567 buf[i--] = '\0'; |
|
04367cff8689
Minor improvements to the stats output dump, including adding commas to
Tim Peters <tim.peters@gmail.com>
parents:
22838
diff
changeset
|
1568 buf[i--] = '\n'; |
|
04367cff8689
Minor improvements to the stats output dump, including adding commas to
Tim Peters <tim.peters@gmail.com>
parents:
22838
diff
changeset
|
1569 k = 3; |
|
04367cff8689
Minor improvements to the stats output dump, including adding commas to
Tim Peters <tim.peters@gmail.com>
parents:
22838
diff
changeset
|
1570 do { |
|
38575
5722f112d5da
In a PYMALLOC_DEBUG build obmalloc adds extra debugging info
Tim Peters <tim.peters@gmail.com>
parents:
37598
diff
changeset
|
1571 size_t nextvalue = value / 10; |
|
5722f112d5da
In a PYMALLOC_DEBUG build obmalloc adds extra debugging info
Tim Peters <tim.peters@gmail.com>
parents:
37598
diff
changeset
|
1572 uint digit = (uint)(value - nextvalue * 10); |
|
22860
04367cff8689
Minor improvements to the stats output dump, including adding commas to
Tim Peters <tim.peters@gmail.com>
parents:
22838
diff
changeset
|
1573 value = nextvalue; |
|
04367cff8689
Minor improvements to the stats output dump, including adding commas to
Tim Peters <tim.peters@gmail.com>
parents:
22838
diff
changeset
|
1574 buf[i--] = (char)(digit + '0'); |
|
04367cff8689
Minor improvements to the stats output dump, including adding commas to
Tim Peters <tim.peters@gmail.com>
parents:
22838
diff
changeset
|
1575 --k; |
|
04367cff8689
Minor improvements to the stats output dump, including adding commas to
Tim Peters <tim.peters@gmail.com>
parents:
22838
diff
changeset
|
1576 if (k == 0 && value && i >= 0) { |
|
04367cff8689
Minor improvements to the stats output dump, including adding commas to
Tim Peters <tim.peters@gmail.com>
parents:
22838
diff
changeset
|
1577 k = 3; |
|
04367cff8689
Minor improvements to the stats output dump, including adding commas to
Tim Peters <tim.peters@gmail.com>
parents:
22838
diff
changeset
|
1578 buf[i--] = ','; |
|
04367cff8689
Minor improvements to the stats output dump, including adding commas to
Tim Peters <tim.peters@gmail.com>
parents:
22838
diff
changeset
|
1579 } |
|
04367cff8689
Minor improvements to the stats output dump, including adding commas to
Tim Peters <tim.peters@gmail.com>
parents:
22838
diff
changeset
|
1580 } while (value && i >= 0); |
|
04367cff8689
Minor improvements to the stats output dump, including adding commas to
Tim Peters <tim.peters@gmail.com>
parents:
22838
diff
changeset
|
1581 |
|
04367cff8689
Minor improvements to the stats output dump, including adding commas to
Tim Peters <tim.peters@gmail.com>
parents:
22838
diff
changeset
|
1582 while (i >= 0) |
|
04367cff8689
Minor improvements to the stats output dump, including adding commas to
Tim Peters <tim.peters@gmail.com>
parents:
22838
diff
changeset
|
1583 buf[i--] = ' '; |
|
04367cff8689
Minor improvements to the stats output dump, including adding commas to
Tim Peters <tim.peters@gmail.com>
parents:
22838
diff
changeset
|
1584 fputs(buf, stderr); |
|
04367cff8689
Minor improvements to the stats output dump, including adding commas to
Tim Peters <tim.peters@gmail.com>
parents:
22838
diff
changeset
|
1585 |
|
04367cff8689
Minor improvements to the stats output dump, including adding commas to
Tim Peters <tim.peters@gmail.com>
parents:
22838
diff
changeset
|
1586 return origvalue; |
|
22837
ef2fbce71736
_PyMalloc_DebugDumpStats(): vastly improved the output, and it now
Tim Peters <tim.peters@gmail.com>
parents:
22836
diff
changeset
|
1587 } |
|
ef2fbce71736
_PyMalloc_DebugDumpStats(): vastly improved the output, and it now
Tim Peters <tim.peters@gmail.com>
parents:
22836
diff
changeset
|
1588 |
|
23159
fca379c30906
_PyObject_DebugMallocStats(): Added some potentially expensive internal
Tim Peters <tim.peters@gmail.com>
parents:
23158
diff
changeset
|
1589 /* Print summary info to stderr about the state of pymalloc's structures. |
|
fca379c30906
_PyObject_DebugMallocStats(): Added some potentially expensive internal
Tim Peters <tim.peters@gmail.com>
parents:
23158
diff
changeset
|
1590 * In Py_DEBUG mode, also perform some expensive internal consistency |
|
fca379c30906
_PyObject_DebugMallocStats(): Added some potentially expensive internal
Tim Peters <tim.peters@gmail.com>
parents:
23158
diff
changeset
|
1591 * checks. |
|
fca379c30906
_PyObject_DebugMallocStats(): Added some potentially expensive internal
Tim Peters <tim.peters@gmail.com>
parents:
23158
diff
changeset
|
1592 */ |
|
22724
9baa8c9a1e84
New PYMALLOC_DEBUG function void _PyMalloc_DebugDumpStats(void).
Tim Peters <tim.peters@gmail.com>
parents:
22701
diff
changeset
|
1593 void |
|
23020
e2848618bd19
_PyObject_DebugDumpStats: renamed to _PyObject_DebugMallocStats.
Tim Peters <tim.peters@gmail.com>
parents:
23009
diff
changeset
|
1594 _PyObject_DebugMallocStats(void) |
|
22724
9baa8c9a1e84
New PYMALLOC_DEBUG function void _PyMalloc_DebugDumpStats(void).
Tim Peters <tim.peters@gmail.com>
parents:
22701
diff
changeset
|
1595 { |
|
9baa8c9a1e84
New PYMALLOC_DEBUG function void _PyMalloc_DebugDumpStats(void).
Tim Peters <tim.peters@gmail.com>
parents:
22701
diff
changeset
|
1596 uint i; |
|
9baa8c9a1e84
New PYMALLOC_DEBUG function void _PyMalloc_DebugDumpStats(void).
Tim Peters <tim.peters@gmail.com>
parents:
22701
diff
changeset
|
1597 const uint numclasses = SMALL_REQUEST_THRESHOLD >> ALIGNMENT_SHIFT; |
|
22837
ef2fbce71736
_PyMalloc_DebugDumpStats(): vastly improved the output, and it now
Tim Peters <tim.peters@gmail.com>
parents:
22836
diff
changeset
|
1598 /* # of pools, allocated blocks, and free blocks per class index */ |
|
38575
5722f112d5da
In a PYMALLOC_DEBUG build obmalloc adds extra debugging info
Tim Peters <tim.peters@gmail.com>
parents:
37598
diff
changeset
|
1599 size_t numpools[SMALL_REQUEST_THRESHOLD >> ALIGNMENT_SHIFT]; |
|
5722f112d5da
In a PYMALLOC_DEBUG build obmalloc adds extra debugging info
Tim Peters <tim.peters@gmail.com>
parents:
37598
diff
changeset
|
1600 size_t numblocks[SMALL_REQUEST_THRESHOLD >> ALIGNMENT_SHIFT]; |
|
5722f112d5da
In a PYMALLOC_DEBUG build obmalloc adds extra debugging info
Tim Peters <tim.peters@gmail.com>
parents:
37598
diff
changeset
|
1601 size_t numfreeblocks[SMALL_REQUEST_THRESHOLD >> ALIGNMENT_SHIFT]; |
|
22837
ef2fbce71736
_PyMalloc_DebugDumpStats(): vastly improved the output, and it now
Tim Peters <tim.peters@gmail.com>
parents:
22836
diff
changeset
|
1602 /* total # of allocated bytes in used and full pools */ |
|
38575
5722f112d5da
In a PYMALLOC_DEBUG build obmalloc adds extra debugging info
Tim Peters <tim.peters@gmail.com>
parents:
37598
diff
changeset
|
1603 size_t allocated_bytes = 0; |
|
22837
ef2fbce71736
_PyMalloc_DebugDumpStats(): vastly improved the output, and it now
Tim Peters <tim.peters@gmail.com>
parents:
22836
diff
changeset
|
1604 /* total # of available bytes in used pools */ |
|
38575
5722f112d5da
In a PYMALLOC_DEBUG build obmalloc adds extra debugging info
Tim Peters <tim.peters@gmail.com>
parents:
37598
diff
changeset
|
1605 size_t available_bytes = 0; |
|
22837
ef2fbce71736
_PyMalloc_DebugDumpStats(): vastly improved the output, and it now
Tim Peters <tim.peters@gmail.com>
parents:
22836
diff
changeset
|
1606 /* # of free pools + pools not yet carved out of current arena */ |
|
ef2fbce71736
_PyMalloc_DebugDumpStats(): vastly improved the output, and it now
Tim Peters <tim.peters@gmail.com>
parents:
22836
diff
changeset
|
1607 uint numfreepools = 0; |
|
ef2fbce71736
_PyMalloc_DebugDumpStats(): vastly improved the output, and it now
Tim Peters <tim.peters@gmail.com>
parents:
22836
diff
changeset
|
1608 /* # of bytes for arena alignment padding */ |
|
38575
5722f112d5da
In a PYMALLOC_DEBUG build obmalloc adds extra debugging info
Tim Peters <tim.peters@gmail.com>
parents:
37598
diff
changeset
|
1609 size_t arena_alignment = 0; |
|
22837
ef2fbce71736
_PyMalloc_DebugDumpStats(): vastly improved the output, and it now
Tim Peters <tim.peters@gmail.com>
parents:
22836
diff
changeset
|
1610 /* # of bytes in used and full pools used for pool_headers */ |
|
38575
5722f112d5da
In a PYMALLOC_DEBUG build obmalloc adds extra debugging info
Tim Peters <tim.peters@gmail.com>
parents:
37598
diff
changeset
|
1611 size_t pool_header_bytes = 0; |
|
22837
ef2fbce71736
_PyMalloc_DebugDumpStats(): vastly improved the output, and it now
Tim Peters <tim.peters@gmail.com>
parents:
22836
diff
changeset
|
1612 /* # of bytes in used and full pools wasted due to quantization, |
|
ef2fbce71736
_PyMalloc_DebugDumpStats(): vastly improved the output, and it now
Tim Peters <tim.peters@gmail.com>
parents:
22836
diff
changeset
|
1613 * i.e. the necessarily leftover space at the ends of used and |
|
ef2fbce71736
_PyMalloc_DebugDumpStats(): vastly improved the output, and it now
Tim Peters <tim.peters@gmail.com>
parents:
22836
diff
changeset
|
1614 * full pools. |
|
ef2fbce71736
_PyMalloc_DebugDumpStats(): vastly improved the output, and it now
Tim Peters <tim.peters@gmail.com>
parents:
22836
diff
changeset
|
1615 */ |
|
38575
5722f112d5da
In a PYMALLOC_DEBUG build obmalloc adds extra debugging info
Tim Peters <tim.peters@gmail.com>
parents:
37598
diff
changeset
|
1616 size_t quantization = 0; |
|
37008
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
1617 /* # of arenas actually allocated. */ |
|
38575
5722f112d5da
In a PYMALLOC_DEBUG build obmalloc adds extra debugging info
Tim Peters <tim.peters@gmail.com>
parents:
37598
diff
changeset
|
1618 size_t narenas = 0; |
|
22837
ef2fbce71736
_PyMalloc_DebugDumpStats(): vastly improved the output, and it now
Tim Peters <tim.peters@gmail.com>
parents:
22836
diff
changeset
|
1619 /* running total -- should equal narenas * ARENA_SIZE */ |
|
38575
5722f112d5da
In a PYMALLOC_DEBUG build obmalloc adds extra debugging info
Tim Peters <tim.peters@gmail.com>
parents:
37598
diff
changeset
|
1620 size_t total; |
|
22837
ef2fbce71736
_PyMalloc_DebugDumpStats(): vastly improved the output, and it now
Tim Peters <tim.peters@gmail.com>
parents:
22836
diff
changeset
|
1621 char buf[128]; |
|
22724
9baa8c9a1e84
New PYMALLOC_DEBUG function void _PyMalloc_DebugDumpStats(void).
Tim Peters <tim.peters@gmail.com>
parents:
22701
diff
changeset
|
1622 |
|
9baa8c9a1e84
New PYMALLOC_DEBUG function void _PyMalloc_DebugDumpStats(void).
Tim Peters <tim.peters@gmail.com>
parents:
22701
diff
changeset
|
1623 fprintf(stderr, "Small block threshold = %d, in %u size classes.\n", |
|
9baa8c9a1e84
New PYMALLOC_DEBUG function void _PyMalloc_DebugDumpStats(void).
Tim Peters <tim.peters@gmail.com>
parents:
22701
diff
changeset
|
1624 SMALL_REQUEST_THRESHOLD, numclasses); |
|
9baa8c9a1e84
New PYMALLOC_DEBUG function void _PyMalloc_DebugDumpStats(void).
Tim Peters <tim.peters@gmail.com>
parents:
22701
diff
changeset
|
1625 |
|
9baa8c9a1e84
New PYMALLOC_DEBUG function void _PyMalloc_DebugDumpStats(void).
Tim Peters <tim.peters@gmail.com>
parents:
22701
diff
changeset
|
1626 for (i = 0; i < numclasses; ++i) |
|
9baa8c9a1e84
New PYMALLOC_DEBUG function void _PyMalloc_DebugDumpStats(void).
Tim Peters <tim.peters@gmail.com>
parents:
22701
diff
changeset
|
1627 numpools[i] = numblocks[i] = numfreeblocks[i] = 0; |
|
9baa8c9a1e84
New PYMALLOC_DEBUG function void _PyMalloc_DebugDumpStats(void).
Tim Peters <tim.peters@gmail.com>
parents:
22701
diff
changeset
|
1628 |
|
22743
7361ec88188a
Fixed errors in two comments.
Tim Peters <tim.peters@gmail.com>
parents:
22741
diff
changeset
|
1629 /* Because full pools aren't linked to from anything, it's easiest |
|
7361ec88188a
Fixed errors in two comments.
Tim Peters <tim.peters@gmail.com>
parents:
22741
diff
changeset
|
1630 * to march over all the arenas. If we're lucky, most of the memory |
|
7361ec88188a
Fixed errors in two comments.
Tim Peters <tim.peters@gmail.com>
parents:
22741
diff
changeset
|
1631 * will be living in full pools -- would be a shame to miss them. |
|
22724
9baa8c9a1e84
New PYMALLOC_DEBUG function void _PyMalloc_DebugDumpStats(void).
Tim Peters <tim.peters@gmail.com>
parents:
22701
diff
changeset
|
1632 */ |
|
37008
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
1633 for (i = 0; i < maxarenas; ++i) { |
|
22724
9baa8c9a1e84
New PYMALLOC_DEBUG function void _PyMalloc_DebugDumpStats(void).
Tim Peters <tim.peters@gmail.com>
parents:
22701
diff
changeset
|
1634 uint poolsinarena; |
|
9baa8c9a1e84
New PYMALLOC_DEBUG function void _PyMalloc_DebugDumpStats(void).
Tim Peters <tim.peters@gmail.com>
parents:
22701
diff
changeset
|
1635 uint j; |
|
37008
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
1636 uptr base = arenas[i].address; |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
1637 |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
1638 /* Skip arenas which are not allocated. */ |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
1639 if (arenas[i].address == (uptr)NULL) |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
1640 continue; |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
1641 narenas += 1; |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
1642 |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
1643 poolsinarena = arenas[i].ntotalpools; |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
1644 numfreepools += arenas[i].nfreepools; |
|
22724
9baa8c9a1e84
New PYMALLOC_DEBUG function void _PyMalloc_DebugDumpStats(void).
Tim Peters <tim.peters@gmail.com>
parents:
22701
diff
changeset
|
1645 |
|
9baa8c9a1e84
New PYMALLOC_DEBUG function void _PyMalloc_DebugDumpStats(void).
Tim Peters <tim.peters@gmail.com>
parents:
22701
diff
changeset
|
1646 /* round up to pool alignment */ |
|
9baa8c9a1e84
New PYMALLOC_DEBUG function void _PyMalloc_DebugDumpStats(void).
Tim Peters <tim.peters@gmail.com>
parents:
22701
diff
changeset
|
1647 if (base & (uptr)POOL_SIZE_MASK) { |
|
22837
ef2fbce71736
_PyMalloc_DebugDumpStats(): vastly improved the output, and it now
Tim Peters <tim.peters@gmail.com>
parents:
22836
diff
changeset
|
1648 arena_alignment += POOL_SIZE; |
|
22724
9baa8c9a1e84
New PYMALLOC_DEBUG function void _PyMalloc_DebugDumpStats(void).
Tim Peters <tim.peters@gmail.com>
parents:
22701
diff
changeset
|
1649 base &= ~(uptr)POOL_SIZE_MASK; |
|
9baa8c9a1e84
New PYMALLOC_DEBUG function void _PyMalloc_DebugDumpStats(void).
Tim Peters <tim.peters@gmail.com>
parents:
22701
diff
changeset
|
1650 base += POOL_SIZE; |
|
9baa8c9a1e84
New PYMALLOC_DEBUG function void _PyMalloc_DebugDumpStats(void).
Tim Peters <tim.peters@gmail.com>
parents:
22701
diff
changeset
|
1651 } |
|
9baa8c9a1e84
New PYMALLOC_DEBUG function void _PyMalloc_DebugDumpStats(void).
Tim Peters <tim.peters@gmail.com>
parents:
22701
diff
changeset
|
1652 |
|
9baa8c9a1e84
New PYMALLOC_DEBUG function void _PyMalloc_DebugDumpStats(void).
Tim Peters <tim.peters@gmail.com>
parents:
22701
diff
changeset
|
1653 /* visit every pool in the arena */ |
|
37008
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
1654 assert(base <= (uptr) arenas[i].pool_address); |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
1655 for (j = 0; |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
1656 base < (uptr) arenas[i].pool_address; |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
1657 ++j, base += POOL_SIZE) { |
|
22724
9baa8c9a1e84
New PYMALLOC_DEBUG function void _PyMalloc_DebugDumpStats(void).
Tim Peters <tim.peters@gmail.com>
parents:
22701
diff
changeset
|
1658 poolp p = (poolp)base; |
|
23159
fca379c30906
_PyObject_DebugMallocStats(): Added some potentially expensive internal
Tim Peters <tim.peters@gmail.com>
parents:
23158
diff
changeset
|
1659 const uint sz = p->szidx; |
|
fca379c30906
_PyObject_DebugMallocStats(): Added some potentially expensive internal
Tim Peters <tim.peters@gmail.com>
parents:
23158
diff
changeset
|
1660 uint freeblocks; |
|
fca379c30906
_PyObject_DebugMallocStats(): Added some potentially expensive internal
Tim Peters <tim.peters@gmail.com>
parents:
23158
diff
changeset
|
1661 |
|
22724
9baa8c9a1e84
New PYMALLOC_DEBUG function void _PyMalloc_DebugDumpStats(void).
Tim Peters <tim.peters@gmail.com>
parents:
22701
diff
changeset
|
1662 if (p->ref.count == 0) { |
|
9baa8c9a1e84
New PYMALLOC_DEBUG function void _PyMalloc_DebugDumpStats(void).
Tim Peters <tim.peters@gmail.com>
parents:
22701
diff
changeset
|
1663 /* currently unused */ |
|
37008
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
1664 assert(pool_is_in_list(p, arenas[i].freepools)); |
|
22724
9baa8c9a1e84
New PYMALLOC_DEBUG function void _PyMalloc_DebugDumpStats(void).
Tim Peters <tim.peters@gmail.com>
parents:
22701
diff
changeset
|
1665 continue; |
|
9baa8c9a1e84
New PYMALLOC_DEBUG function void _PyMalloc_DebugDumpStats(void).
Tim Peters <tim.peters@gmail.com>
parents:
22701
diff
changeset
|
1666 } |
|
23159
fca379c30906
_PyObject_DebugMallocStats(): Added some potentially expensive internal
Tim Peters <tim.peters@gmail.com>
parents:
23158
diff
changeset
|
1667 ++numpools[sz]; |
|
fca379c30906
_PyObject_DebugMallocStats(): Added some potentially expensive internal
Tim Peters <tim.peters@gmail.com>
parents:
23158
diff
changeset
|
1668 numblocks[sz] += p->ref.count; |
|
fca379c30906
_PyObject_DebugMallocStats(): Added some potentially expensive internal
Tim Peters <tim.peters@gmail.com>
parents:
23158
diff
changeset
|
1669 freeblocks = NUMBLOCKS(sz) - p->ref.count; |
|
fca379c30906
_PyObject_DebugMallocStats(): Added some potentially expensive internal
Tim Peters <tim.peters@gmail.com>
parents:
23158
diff
changeset
|
1670 numfreeblocks[sz] += freeblocks; |
|
fca379c30906
_PyObject_DebugMallocStats(): Added some potentially expensive internal
Tim Peters <tim.peters@gmail.com>
parents:
23158
diff
changeset
|
1671 #ifdef Py_DEBUG |
|
fca379c30906
_PyObject_DebugMallocStats(): Added some potentially expensive internal
Tim Peters <tim.peters@gmail.com>
parents:
23158
diff
changeset
|
1672 if (freeblocks > 0) |
|
fca379c30906
_PyObject_DebugMallocStats(): Added some potentially expensive internal
Tim Peters <tim.peters@gmail.com>
parents:
23158
diff
changeset
|
1673 assert(pool_is_in_list(p, usedpools[sz + sz])); |
|
fca379c30906
_PyObject_DebugMallocStats(): Added some potentially expensive internal
Tim Peters <tim.peters@gmail.com>
parents:
23158
diff
changeset
|
1674 #endif |
|
22724
9baa8c9a1e84
New PYMALLOC_DEBUG function void _PyMalloc_DebugDumpStats(void).
Tim Peters <tim.peters@gmail.com>
parents:
22701
diff
changeset
|
1675 } |
|
9baa8c9a1e84
New PYMALLOC_DEBUG function void _PyMalloc_DebugDumpStats(void).
Tim Peters <tim.peters@gmail.com>
parents:
22701
diff
changeset
|
1676 } |
|
37008
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
1677 assert(narenas == narenas_currently_allocated); |
|
22724
9baa8c9a1e84
New PYMALLOC_DEBUG function void _PyMalloc_DebugDumpStats(void).
Tim Peters <tim.peters@gmail.com>
parents:
22701
diff
changeset
|
1678 |
|
9baa8c9a1e84
New PYMALLOC_DEBUG function void _PyMalloc_DebugDumpStats(void).
Tim Peters <tim.peters@gmail.com>
parents:
22701
diff
changeset
|
1679 fputc('\n', stderr); |
|
22860
04367cff8689
Minor improvements to the stats output dump, including adding commas to
Tim Peters <tim.peters@gmail.com>
parents:
22838
diff
changeset
|
1680 fputs("class size num pools blocks in use avail blocks\n" |
|
04367cff8689
Minor improvements to the stats output dump, including adding commas to
Tim Peters <tim.peters@gmail.com>
parents:
22838
diff
changeset
|
1681 "----- ---- --------- ------------- ------------\n", |
|
22724
9baa8c9a1e84
New PYMALLOC_DEBUG function void _PyMalloc_DebugDumpStats(void).
Tim Peters <tim.peters@gmail.com>
parents:
22701
diff
changeset
|
1682 stderr); |
|
9baa8c9a1e84
New PYMALLOC_DEBUG function void _PyMalloc_DebugDumpStats(void).
Tim Peters <tim.peters@gmail.com>
parents:
22701
diff
changeset
|
1683 |
|
9baa8c9a1e84
New PYMALLOC_DEBUG function void _PyMalloc_DebugDumpStats(void).
Tim Peters <tim.peters@gmail.com>
parents:
22701
diff
changeset
|
1684 for (i = 0; i < numclasses; ++i) { |
|
38575
5722f112d5da
In a PYMALLOC_DEBUG build obmalloc adds extra debugging info
Tim Peters <tim.peters@gmail.com>
parents:
37598
diff
changeset
|
1685 size_t p = numpools[i]; |
|
5722f112d5da
In a PYMALLOC_DEBUG build obmalloc adds extra debugging info
Tim Peters <tim.peters@gmail.com>
parents:
37598
diff
changeset
|
1686 size_t b = numblocks[i]; |
|
5722f112d5da
In a PYMALLOC_DEBUG build obmalloc adds extra debugging info
Tim Peters <tim.peters@gmail.com>
parents:
37598
diff
changeset
|
1687 size_t f = numfreeblocks[i]; |
|
22836
f1f57bf09092
Widespread, but mostly in _PyMalloc_Malloc: optimize away all expensive
Tim Peters <tim.peters@gmail.com>
parents:
22795
diff
changeset
|
1688 uint size = INDEX2SIZE(i); |
|
22724
9baa8c9a1e84
New PYMALLOC_DEBUG function void _PyMalloc_DebugDumpStats(void).
Tim Peters <tim.peters@gmail.com>
parents:
22701
diff
changeset
|
1689 if (p == 0) { |
|
9baa8c9a1e84
New PYMALLOC_DEBUG function void _PyMalloc_DebugDumpStats(void).
Tim Peters <tim.peters@gmail.com>
parents:
22701
diff
changeset
|
1690 assert(b == 0 && f == 0); |
|
9baa8c9a1e84
New PYMALLOC_DEBUG function void _PyMalloc_DebugDumpStats(void).
Tim Peters <tim.peters@gmail.com>
parents:
22701
diff
changeset
|
1691 continue; |
|
9baa8c9a1e84
New PYMALLOC_DEBUG function void _PyMalloc_DebugDumpStats(void).
Tim Peters <tim.peters@gmail.com>
parents:
22701
diff
changeset
|
1692 } |
|
38575
5722f112d5da
In a PYMALLOC_DEBUG build obmalloc adds extra debugging info
Tim Peters <tim.peters@gmail.com>
parents:
37598
diff
changeset
|
1693 fprintf(stderr, "%5u %6u " |
|
5722f112d5da
In a PYMALLOC_DEBUG build obmalloc adds extra debugging info
Tim Peters <tim.peters@gmail.com>
parents:
37598
diff
changeset
|
1694 "%11" PY_FORMAT_SIZE_T "u " |
|
5722f112d5da
In a PYMALLOC_DEBUG build obmalloc adds extra debugging info
Tim Peters <tim.peters@gmail.com>
parents:
37598
diff
changeset
|
1695 "%15" PY_FORMAT_SIZE_T "u " |
|
5722f112d5da
In a PYMALLOC_DEBUG build obmalloc adds extra debugging info
Tim Peters <tim.peters@gmail.com>
parents:
37598
diff
changeset
|
1696 "%13" PY_FORMAT_SIZE_T "u\n", |
|
22724
9baa8c9a1e84
New PYMALLOC_DEBUG function void _PyMalloc_DebugDumpStats(void).
Tim Peters <tim.peters@gmail.com>
parents:
22701
diff
changeset
|
1697 i, size, p, b, f); |
|
22837
ef2fbce71736
_PyMalloc_DebugDumpStats(): vastly improved the output, and it now
Tim Peters <tim.peters@gmail.com>
parents:
22836
diff
changeset
|
1698 allocated_bytes += b * size; |
|
ef2fbce71736
_PyMalloc_DebugDumpStats(): vastly improved the output, and it now
Tim Peters <tim.peters@gmail.com>
parents:
22836
diff
changeset
|
1699 available_bytes += f * size; |
|
ef2fbce71736
_PyMalloc_DebugDumpStats(): vastly improved the output, and it now
Tim Peters <tim.peters@gmail.com>
parents:
22836
diff
changeset
|
1700 pool_header_bytes += p * POOL_OVERHEAD; |
|
ef2fbce71736
_PyMalloc_DebugDumpStats(): vastly improved the output, and it now
Tim Peters <tim.peters@gmail.com>
parents:
22836
diff
changeset
|
1701 quantization += p * ((POOL_SIZE - POOL_OVERHEAD) % size); |
|
22724
9baa8c9a1e84
New PYMALLOC_DEBUG function void _PyMalloc_DebugDumpStats(void).
Tim Peters <tim.peters@gmail.com>
parents:
22701
diff
changeset
|
1702 } |
|
9baa8c9a1e84
New PYMALLOC_DEBUG function void _PyMalloc_DebugDumpStats(void).
Tim Peters <tim.peters@gmail.com>
parents:
22701
diff
changeset
|
1703 fputc('\n', stderr); |
|
23020
e2848618bd19
_PyObject_DebugDumpStats: renamed to _PyObject_DebugMallocStats.
Tim Peters <tim.peters@gmail.com>
parents:
23009
diff
changeset
|
1704 (void)printone("# times object malloc called", serialno); |
|
22837
ef2fbce71736
_PyMalloc_DebugDumpStats(): vastly improved the output, and it now
Tim Peters <tim.peters@gmail.com>
parents:
22836
diff
changeset
|
1705 |
|
37008
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
1706 (void)printone("# arenas allocated total", ntimes_arena_allocated); |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
1707 (void)printone("# arenas reclaimed", ntimes_arena_allocated - narenas); |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
1708 (void)printone("# arenas highwater mark", narenas_highwater); |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
1709 (void)printone("# arenas allocated current", narenas); |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
1710 |
|
22837
ef2fbce71736
_PyMalloc_DebugDumpStats(): vastly improved the output, and it now
Tim Peters <tim.peters@gmail.com>
parents:
22836
diff
changeset
|
1711 PyOS_snprintf(buf, sizeof(buf), |
|
38575
5722f112d5da
In a PYMALLOC_DEBUG build obmalloc adds extra debugging info
Tim Peters <tim.peters@gmail.com>
parents:
37598
diff
changeset
|
1712 "%" PY_FORMAT_SIZE_T "u arenas * %d bytes/arena", |
|
5722f112d5da
In a PYMALLOC_DEBUG build obmalloc adds extra debugging info
Tim Peters <tim.peters@gmail.com>
parents:
37598
diff
changeset
|
1713 narenas, ARENA_SIZE); |
|
37008
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
1714 (void)printone(buf, narenas * ARENA_SIZE); |
|
22837
ef2fbce71736
_PyMalloc_DebugDumpStats(): vastly improved the output, and it now
Tim Peters <tim.peters@gmail.com>
parents:
22836
diff
changeset
|
1715 |
|
ef2fbce71736
_PyMalloc_DebugDumpStats(): vastly improved the output, and it now
Tim Peters <tim.peters@gmail.com>
parents:
22836
diff
changeset
|
1716 fputc('\n', stderr); |
|
ef2fbce71736
_PyMalloc_DebugDumpStats(): vastly improved the output, and it now
Tim Peters <tim.peters@gmail.com>
parents:
22836
diff
changeset
|
1717 |
|
22860
04367cff8689
Minor improvements to the stats output dump, including adding commas to
Tim Peters <tim.peters@gmail.com>
parents:
22838
diff
changeset
|
1718 total = printone("# bytes in allocated blocks", allocated_bytes); |
|
23020
e2848618bd19
_PyObject_DebugDumpStats: renamed to _PyObject_DebugMallocStats.
Tim Peters <tim.peters@gmail.com>
parents:
23009
diff
changeset
|
1719 total += printone("# bytes in available blocks", available_bytes); |
|
22860
04367cff8689
Minor improvements to the stats output dump, including adding commas to
Tim Peters <tim.peters@gmail.com>
parents:
22838
diff
changeset
|
1720 |
|
22837
ef2fbce71736
_PyMalloc_DebugDumpStats(): vastly improved the output, and it now
Tim Peters <tim.peters@gmail.com>
parents:
22836
diff
changeset
|
1721 PyOS_snprintf(buf, sizeof(buf), |
|
ef2fbce71736
_PyMalloc_DebugDumpStats(): vastly improved the output, and it now
Tim Peters <tim.peters@gmail.com>
parents:
22836
diff
changeset
|
1722 "%u unused pools * %d bytes", numfreepools, POOL_SIZE); |
|
38575
5722f112d5da
In a PYMALLOC_DEBUG build obmalloc adds extra debugging info
Tim Peters <tim.peters@gmail.com>
parents:
37598
diff
changeset
|
1723 total += printone(buf, (size_t)numfreepools * POOL_SIZE); |
|
22837
ef2fbce71736
_PyMalloc_DebugDumpStats(): vastly improved the output, and it now
Tim Peters <tim.peters@gmail.com>
parents:
22836
diff
changeset
|
1724 |
|
ef2fbce71736
_PyMalloc_DebugDumpStats(): vastly improved the output, and it now
Tim Peters <tim.peters@gmail.com>
parents:
22836
diff
changeset
|
1725 total += printone("# bytes lost to pool headers", pool_header_bytes); |
|
ef2fbce71736
_PyMalloc_DebugDumpStats(): vastly improved the output, and it now
Tim Peters <tim.peters@gmail.com>
parents:
22836
diff
changeset
|
1726 total += printone("# bytes lost to quantization", quantization); |
|
ef2fbce71736
_PyMalloc_DebugDumpStats(): vastly improved the output, and it now
Tim Peters <tim.peters@gmail.com>
parents:
22836
diff
changeset
|
1727 total += printone("# bytes lost to arena alignment", arena_alignment); |
|
ef2fbce71736
_PyMalloc_DebugDumpStats(): vastly improved the output, and it now
Tim Peters <tim.peters@gmail.com>
parents:
22836
diff
changeset
|
1728 (void)printone("Total", total); |
|
22724
9baa8c9a1e84
New PYMALLOC_DEBUG function void _PyMalloc_DebugDumpStats(void).
Tim Peters <tim.peters@gmail.com>
parents:
22701
diff
changeset
|
1729 } |
|
9baa8c9a1e84
New PYMALLOC_DEBUG function void _PyMalloc_DebugDumpStats(void).
Tim Peters <tim.peters@gmail.com>
parents:
22701
diff
changeset
|
1730 |
|
22479
fb45791150d1
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
Tim Peters <tim.peters@gmail.com>
parents:
22470
diff
changeset
|
1731 #endif /* PYMALLOC_DEBUG */ |
|
32278
c2bc248d3e9c
SF bug 881641, make it easier to use valgrind
Neal Norwitz <nnorwitz@gmail.com>
parents:
29515
diff
changeset
|
1732 |
|
c2bc248d3e9c
SF bug 881641, make it easier to use valgrind
Neal Norwitz <nnorwitz@gmail.com>
parents:
29515
diff
changeset
|
1733 #ifdef Py_USING_MEMORY_DEBUGGER |
|
37008
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
1734 /* Make this function last so gcc won't inline it since the definition is |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
1735 * after the reference. |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
1736 */ |
|
32278
c2bc248d3e9c
SF bug 881641, make it easier to use valgrind
Neal Norwitz <nnorwitz@gmail.com>
parents:
29515
diff
changeset
|
1737 int |
|
c2bc248d3e9c
SF bug 881641, make it easier to use valgrind
Neal Norwitz <nnorwitz@gmail.com>
parents:
29515
diff
changeset
|
1738 Py_ADDRESS_IN_RANGE(void *P, poolp pool) |
|
c2bc248d3e9c
SF bug 881641, make it easier to use valgrind
Neal Norwitz <nnorwitz@gmail.com>
parents:
29515
diff
changeset
|
1739 { |
|
37008
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
1740 return pool->arenaindex < maxarenas && |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
1741 (uptr)P - arenas[pool->arenaindex].address < (uptr)ARENA_SIZE && |
|
685849bd905c
Merge the tim-obmalloc branch to the trunk.
Tim Peters <tim.peters@gmail.com>
parents:
36514
diff
changeset
|
1742 arenas[pool->arenaindex].address != 0; |
|
32278
c2bc248d3e9c
SF bug 881641, make it easier to use valgrind
Neal Norwitz <nnorwitz@gmail.com>
parents:
29515
diff
changeset
|
1743 } |
|
c2bc248d3e9c
SF bug 881641, make it easier to use valgrind
Neal Norwitz <nnorwitz@gmail.com>
parents:
29515
diff
changeset
|
1744 #endif |
