-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathpython2.5_no_free_list.patch
More file actions
912 lines (871 loc) · 25.6 KB
/
python2.5_no_free_list.patch
File metadata and controls
912 lines (871 loc) · 25.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
diff -r fb57d3630ed9 Include/objimpl.h
--- a/Include/objimpl.h Mon Mar 11 11:46:38 2013 +0100
+++ b/Include/objimpl.h Mon Mar 11 11:56:17 2013 +0100
@@ -100,36 +100,21 @@ PyAPI_FUNC(void) PyObject_Free(void *);
/* Macros */
-#ifdef WITH_PYMALLOC
-#ifdef PYMALLOC_DEBUG /* WITH_PYMALLOC && PYMALLOC_DEBUG */
+#if defined(WITH_PYMALLOC) && defined(PYMALLOC_DEBUG)
PyAPI_FUNC(void *) _PyObject_DebugMalloc(size_t nbytes);
PyAPI_FUNC(void *) _PyObject_DebugRealloc(void *p, size_t nbytes);
PyAPI_FUNC(void) _PyObject_DebugFree(void *p);
PyAPI_FUNC(void) _PyObject_DebugDumpAddress(const void *p);
PyAPI_FUNC(void) _PyObject_DebugCheckAddress(const void *p);
PyAPI_FUNC(void) _PyObject_DebugMallocStats(void);
-#define PyObject_MALLOC _PyObject_DebugMalloc
-#define PyObject_Malloc _PyObject_DebugMalloc
-#define PyObject_REALLOC _PyObject_DebugRealloc
-#define PyObject_Realloc _PyObject_DebugRealloc
-#define PyObject_FREE _PyObject_DebugFree
-#define PyObject_Free _PyObject_DebugFree
-
-#else /* WITH_PYMALLOC && ! PYMALLOC_DEBUG */
-#define PyObject_MALLOC PyObject_Malloc
-#define PyObject_REALLOC PyObject_Realloc
-#define PyObject_FREE PyObject_Free
#endif
-#else /* ! WITH_PYMALLOC */
-#define PyObject_MALLOC PyMem_MALLOC
-#define PyObject_REALLOC PyMem_REALLOC
-#define PyObject_FREE PyMem_FREE
-
-#endif /* WITH_PYMALLOC */
-
-#define PyObject_Del PyObject_Free
-#define PyObject_DEL PyObject_FREE
+/* Macros */
+#define PyObject_MALLOC PyObject_Malloc
+#define PyObject_REALLOC PyObject_Realloc
+#define PyObject_FREE PyObject_Free
+#define PyObject_Del PyObject_Free
+#define PyObject_DEL PyObject_Free
/* for source compatibility with 2.2 */
#define _PyObject_Del PyObject_Free
diff -r fb57d3630ed9 Include/pymem.h
--- a/Include/pymem.h Mon Mar 11 11:46:38 2013 +0100
+++ b/Include/pymem.h Mon Mar 11 11:56:17 2013 +0100
@@ -47,6 +47,24 @@ extern "C" {
performed on failure (no exception is set, no warning is printed, etc).
*/
+#define PY_ALLOC_SYSTEM_API 's' /* the system API (malloc, realloc, free) */
+#define PY_ALLOC_MEM_API 'm' /* the PyMem_Malloc() API */
+#define PY_ALLOC_OBJECT_API 'o' /* The PyObject_Malloc() API */
+
+PyAPI_FUNC(int) Py_GetAllocators(
+ char api,
+ void* (**malloc_p) (size_t),
+ void* (**realloc_p) (void*, size_t),
+ void (**free_p) (void*)
+ );
+
+PyAPI_FUNC(int) Py_SetAllocators(
+ char api,
+ void* (*malloc) (size_t),
+ void* (*realloc) (void*, size_t),
+ void (*free) (void*)
+ );
+
PyAPI_FUNC(void *) PyMem_Malloc(size_t);
PyAPI_FUNC(void *) PyMem_Realloc(void *, size_t);
PyAPI_FUNC(void) PyMem_Free(void *);
@@ -55,23 +73,14 @@ PyAPI_FUNC(void) PyMem_Free(void *);
no longer supported. They used to call PyErr_NoMemory() on failure. */
/* Macros. */
-#ifdef PYMALLOC_DEBUG
-/* Redirect all memory operations to Python's debugging allocator. */
-#define PyMem_MALLOC PyObject_MALLOC
-#define PyMem_REALLOC PyObject_REALLOC
-#define PyMem_FREE PyObject_FREE
-
-#else /* ! PYMALLOC_DEBUG */
/* PyMem_MALLOC(0) means malloc(1). Some systems would return NULL
for malloc(0), which would be treated as an error. Some platforms
would return a pointer with no memory behind it, which would break
pymalloc. To solve these problems, allocate an extra byte. */
-#define PyMem_MALLOC(n) malloc((n) ? (n) : 1)
-#define PyMem_REALLOC(p, n) realloc((p), (n) ? (n) : 1)
-#define PyMem_FREE free
-
-#endif /* PYMALLOC_DEBUG */
+#define PyMem_MALLOC(n) PyMem_Malloc(n)
+#define PyMem_REALLOC(p, n) PyMem_Realloc(p, n)
+#define PyMem_FREE(p) PyMem_Free(p)
/*
* Type-oriented memory interface
diff -r fb57d3630ed9 Include/sysmodule.h
--- a/Include/sysmodule.h Mon Mar 11 11:46:38 2013 +0100
+++ b/Include/sysmodule.h Mon Mar 11 11:56:17 2013 +0100
@@ -24,6 +24,8 @@ PyAPI_DATA(int) _PySys_CheckInterval;
PyAPI_FUNC(void) PySys_ResetWarnOptions(void);
PyAPI_FUNC(void) PySys_AddWarnOption(char *);
+PyAPI_DATA(int) _PySys_UseFreeList;
+
#ifdef __cplusplus
}
#endif
diff -r fb57d3630ed9 Objects/classobject.c
--- a/Objects/classobject.c Mon Mar 11 11:46:38 2013 +0100
+++ b/Objects/classobject.c Mon Mar 11 11:56:17 2013 +0100
@@ -2181,7 +2181,10 @@ PyMethod_New(PyObject *func, PyObject *s
PyErr_BadInternalCall();
return NULL;
}
- im = free_list;
+ if (_PySys_UseFreeList)
+ im = free_list;
+ else
+ im = NULL;
if (im != NULL) {
free_list = (PyMethodObject *)(im->im_self);
PyObject_INIT(im, &PyMethod_Type);
@@ -2310,8 +2313,13 @@ instancemethod_dealloc(register PyMethod
Py_DECREF(im->im_func);
Py_XDECREF(im->im_self);
Py_XDECREF(im->im_class);
- im->im_self = (PyObject *)free_list;
- free_list = im;
+ if (_PySys_UseFreeList) {
+ im->im_self = (PyObject *)free_list;
+ free_list = im;
+ }
+ else {
+ PyObject_GC_Del(im);
+ }
}
static int
diff -r fb57d3630ed9 Objects/floatobject.c
--- a/Objects/floatobject.c Mon Mar 11 11:46:38 2013 +0100
+++ b/Objects/floatobject.c Mon Mar 11 11:56:17 2013 +0100
@@ -50,14 +50,21 @@ PyObject *
PyFloat_FromDouble(double fval)
{
register PyFloatObject *op;
- if (free_list == NULL) {
- if ((free_list = fill_free_list()) == NULL)
+ if (_PySys_UseFreeList) {
+ if (free_list == NULL) {
+ if ((free_list = fill_free_list()) == NULL)
+ return NULL;
+ }
+ /* Inline PyObject_New */
+ op = free_list;
+ free_list = (PyFloatObject *)op->ob_type;
+ PyObject_INIT(op, &PyFloat_Type);
+ }
+ else {
+ op = (PyFloatObject *)PyObject_New(PyFloatObject, &PyFloat_Type);
+ if (op == NULL)
return NULL;
}
- /* Inline PyObject_New */
- op = free_list;
- free_list = (PyFloatObject *)op->ob_type;
- PyObject_INIT(op, &PyFloat_Type);
op->ob_fval = fval;
return (PyObject *) op;
}
@@ -175,8 +182,13 @@ static void
float_dealloc(PyFloatObject *op)
{
if (PyFloat_CheckExact(op)) {
- op->ob_type = (struct _typeobject *)free_list;
- free_list = op;
+ if (_PySys_UseFreeList) {
+ op->ob_type = (struct _typeobject *)free_list;
+ free_list = op;
+ }
+ else {
+ PyObject_Del(op);
+ }
}
else
op->ob_type->tp_free((PyObject *)op);
diff -r fb57d3630ed9 Objects/frameobject.c
--- a/Objects/frameobject.c Mon Mar 11 11:46:38 2013 +0100
+++ b/Objects/frameobject.c Mon Mar 11 11:56:17 2013 +0100
@@ -433,7 +433,7 @@ frame_dealloc(PyFrameObject *f)
co = f->f_code;
if (co->co_zombieframe == NULL)
co->co_zombieframe = f;
- else if (numfree < MAXFREELIST) {
+ else if (_PySys_UseFreeList && numfree < MAXFREELIST) {
++numfree;
f->f_back = free_list;
free_list = f;
@@ -610,7 +610,11 @@ PyFrame_New(PyThreadState *tstate, PyCod
nfrees = PyTuple_GET_SIZE(code->co_freevars);
extras = code->co_stacksize + code->co_nlocals + ncells +
nfrees;
- if (free_list == NULL) {
+ if (_PySys_UseFreeList)
+ f = free_list;
+ else
+ f = NULL;
+ if (f == NULL) {
f = PyObject_GC_NewVar(PyFrameObject, &PyFrame_Type,
extras);
if (f == NULL) {
diff -r fb57d3630ed9 Objects/intobject.c
--- a/Objects/intobject.c Mon Mar 11 11:46:38 2013 +0100
+++ b/Objects/intobject.c Mon Mar 11 11:56:17 2013 +0100
@@ -96,14 +96,21 @@ PyInt_FromLong(long ival)
return (PyObject *) v;
}
#endif
- if (free_list == NULL) {
- if ((free_list = fill_free_list()) == NULL)
+ if (_PySys_UseFreeList) {
+ if (free_list == NULL) {
+ if ((free_list = fill_free_list()) == NULL)
+ return NULL;
+ }
+ /* Inline PyObject_New */
+ v = free_list;
+ free_list = (PyIntObject *)v->ob_type;
+ PyObject_INIT(v, &PyInt_Type);
+ }
+ else {
+ v = (PyIntObject *)PyObject_New(PyIntObject, &PyInt_Type);
+ if (v == NULL)
return NULL;
}
- /* Inline PyObject_New */
- v = free_list;
- free_list = (PyIntObject *)v->ob_type;
- PyObject_INIT(v, &PyInt_Type);
v->ob_ival = ival;
return (PyObject *) v;
}
@@ -127,7 +134,7 @@ PyInt_FromSsize_t(Py_ssize_t ival)
static void
int_dealloc(PyIntObject *v)
{
- if (PyInt_CheckExact(v)) {
+ if (_PySys_UseFreeList && PyInt_CheckExact(v)) {
v->ob_type = (struct _typeobject *)free_list;
free_list = v;
}
@@ -138,8 +145,13 @@ int_dealloc(PyIntObject *v)
static void
int_free(PyIntObject *v)
{
- v->ob_type = (struct _typeobject *)free_list;
- free_list = v;
+ if (_PySys_UseFreeList) {
+ v->ob_type = (struct _typeobject *)free_list;
+ free_list = v;
+ }
+ else {
+ PyObject_Del(v);
+ }
}
long
@@ -1166,12 +1178,19 @@ int
int ival;
#if NSMALLNEGINTS + NSMALLPOSINTS > 0
for (ival = -NSMALLNEGINTS; ival < NSMALLPOSINTS; ival++) {
- if (!free_list && (free_list = fill_free_list()) == NULL)
- return 0;
- /* PyObject_New is inlined */
- v = free_list;
- free_list = (PyIntObject *)v->ob_type;
- PyObject_INIT(v, &PyInt_Type);
+ if (_PySys_UseFreeList) {
+ if (!free_list && (free_list = fill_free_list()) == NULL)
+ return 0;
+ /* PyObject_New is inlined */
+ v = free_list;
+ free_list = (PyIntObject *)v->ob_type;
+ PyObject_INIT(v, &PyInt_Type);
+ }
+ else {
+ v = (PyIntObject *)PyObject_New(PyIntObject, &PyInt_Type);
+ if (v == NULL)
+ return 0;
+ }
v->ob_ival = ival;
small_ints[ival + NSMALLNEGINTS] = v;
}
diff -r fb57d3630ed9 Objects/listobject.c
--- a/Objects/listobject.c Mon Mar 11 11:46:38 2013 +0100
+++ b/Objects/listobject.c Mon Mar 11 11:56:17 2013 +0100
@@ -105,7 +105,7 @@ PyList_New(Py_ssize_t size)
* which can cause compiler to optimise out */
if (size > PY_SIZE_MAX / sizeof(PyObject *))
return PyErr_NoMemory();
- if (num_free_lists) {
+ if (_PySys_UseFreeList && num_free_lists) {
num_free_lists--;
op = free_lists[num_free_lists];
_Py_NewReference((PyObject *)op);
@@ -275,7 +275,7 @@ list_dealloc(PyListObject *op)
}
PyMem_FREE(op->ob_item);
}
- if (num_free_lists < MAXFREELISTS && PyList_CheckExact(op))
+ if (_PySys_UseFreeList && num_free_lists < MAXFREELISTS && PyList_CheckExact(op))
free_lists[num_free_lists++] = op;
else
op->ob_type->tp_free((PyObject *)op);
diff -r fb57d3630ed9 Objects/methodobject.c
--- a/Objects/methodobject.c Mon Mar 11 11:46:38 2013 +0100
+++ b/Objects/methodobject.c Mon Mar 11 11:56:17 2013 +0100
@@ -10,7 +10,10 @@ PyObject *
PyCFunction_NewEx(PyMethodDef *ml, PyObject *self, PyObject *module)
{
PyCFunctionObject *op;
- op = free_list;
+ if (_PySys_UseFreeList)
+ op = free_list;
+ else
+ op = NULL;
if (op != NULL) {
free_list = (PyCFunctionObject *)(op->m_self);
PyObject_INIT(op, &PyCFunction_Type);
@@ -125,8 +128,13 @@ meth_dealloc(PyCFunctionObject *m)
_PyObject_GC_UNTRACK(m);
Py_XDECREF(m->m_self);
Py_XDECREF(m->m_module);
- m->m_self = (PyObject *)free_list;
- free_list = m;
+ if (_PySys_UseFreeList) {
+ m->m_self = (PyObject *)free_list;
+ free_list = m;
+ }
+ else {
+ PyObject_GC_Del(m);
+ }
}
static PyObject *
diff -r fb57d3630ed9 Objects/object.c
--- a/Objects/object.c Mon Mar 11 11:46:38 2013 +0100
+++ b/Objects/object.c Mon Mar 11 11:56:17 2013 +0100
@@ -2002,27 +2002,6 @@ PyTypeObject *_Py_cobject_hack = &PyCObj
Py_ssize_t (*_Py_abstract_hack)(PyObject *) = PyObject_Size;
-/* Python's malloc wrappers (see pymem.h) */
-
-void *
-PyMem_Malloc(size_t nbytes)
-{
- return PyMem_MALLOC(nbytes);
-}
-
-void *
-PyMem_Realloc(void *p, size_t nbytes)
-{
- return PyMem_REALLOC(p, nbytes);
-}
-
-void
-PyMem_Free(void *p)
-{
- PyMem_FREE(p);
-}
-
-
/* These methods are used to control infinite recursion in repr, str, print,
etc. Container objects that may recursively contain themselves,
e.g. builtin dictionaries and lists, should used Py_ReprEnter() and
diff -r fb57d3630ed9 Objects/obmalloc.c
--- a/Objects/obmalloc.c Mon Mar 11 11:46:38 2013 +0100
+++ b/Objects/obmalloc.c Mon Mar 11 11:56:17 2013 +0100
@@ -1,5 +1,187 @@
#include "Python.h"
+/* Python's malloc wrappers (see pymem.h) */
+
+static struct {
+ int init;
+ int use_pymalloc;
+
+ void* (*system_malloc) (size_t);
+ void* (*system_realloc) (void*, size_t);
+ void (*system_free) (void*);
+
+ void* (*mem_malloc) (size_t);
+ void* (*mem_realloc) (void*, size_t);
+ void (*mem_free) (void*);
+
+ void* (*object_malloc) (size_t);
+ void* (*object_realloc) (void*, size_t);
+ void (*object_free) (void*);
+} _PyMem_Allocators = {0, 1, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL};
+
+/* Forward declaration */
+static void* _PyObject_Malloc(size_t nbytes);
+static void _PyObject_Free(void *p);
+static void* _PyObject_Realloc(void *p, size_t nbytes);
+
+#ifndef PYMALLOC_DEBUG
+static void *
+_PyMem_Malloc(size_t nbytes)
+{
+ return _PyMem_Allocators.system_malloc(nbytes);
+}
+
+static void *
+_PyMem_Realloc(void *p, size_t nbytes)
+{
+ return _PyMem_Allocators.system_realloc(p, nbytes);
+}
+
+static void
+_PyMem_Free(void *p)
+{
+ _PyMem_Allocators.system_free(p);
+}
+#endif
+
+static void
+_PyMem_InitSystemAllocators(void)
+{
+ _PyMem_Allocators.init = 1;
+ _PyMem_Allocators.use_pymalloc = 1;
+
+ _PyMem_Allocators.system_malloc = malloc;
+ _PyMem_Allocators.system_realloc = realloc;
+ _PyMem_Allocators.system_free = free;
+
+#if defined(PYMALLOC_DEBUG)
+ _PyMem_Allocators.mem_malloc = _PyObject_DebugMalloc;
+ _PyMem_Allocators.mem_realloc = _PyObject_DebugRealloc;
+ _PyMem_Allocators.mem_free = _PyObject_DebugFree;
+#else
+ _PyMem_Allocators.mem_malloc = _PyMem_Malloc;
+ _PyMem_Allocators.mem_realloc = _PyMem_Realloc;
+ _PyMem_Allocators.mem_free = _PyMem_Free;
+#endif
+
+#ifndef WITH_PYMALLOC
+ /* pymalloc not enabled: Redirect the entry points to malloc */
+ _PyMem_Allocators.object_malloc = PyMem_Malloc;
+ _PyMem_Allocators.object_free = PyMem_Free;
+ _PyMem_Allocators.object_realloc = PyMem_Realloc;
+#elif defined(PYMALLOC_DEBUG) /* WITH_PYMALLOC && PYMALLOC_DEBUG */
+ _PyMem_Allocators.object_malloc = _PyObject_DebugMalloc;
+ _PyMem_Allocators.object_free = _PyObject_DebugFree;
+ _PyMem_Allocators.object_realloc = _PyObject_DebugRealloc;
+#else
+ _PyMem_Allocators.object_malloc = _PyObject_Malloc;
+ _PyMem_Allocators.object_free = _PyObject_Free;
+ _PyMem_Allocators.object_realloc = _PyObject_Realloc;
+#endif
+}
+
+#define _PyMem_InitAllocators() \
+ do { \
+ if (!_PyMem_Allocators.init) \
+ _PyMem_InitSystemAllocators(); \
+ } while (0)
+
+int Py_GetAllocators(
+ char api,
+ void* (**malloc_p) (size_t),
+ void* (**realloc_p) (void*, size_t),
+ void (**free_p) (void*)
+ )
+{
+ _PyMem_InitAllocators();
+
+ if (api == PY_ALLOC_SYSTEM_API) {
+ *malloc_p = _PyMem_Allocators.system_malloc;
+ *realloc_p = _PyMem_Allocators.system_realloc;
+ *free_p = _PyMem_Allocators.system_free;
+ }
+ else if (api == PY_ALLOC_MEM_API) {
+ *malloc_p = _PyMem_Allocators.mem_malloc;
+ *realloc_p = _PyMem_Allocators.mem_realloc;
+ *free_p = _PyMem_Allocators.mem_free;
+ }
+ else if (api == PY_ALLOC_OBJECT_API) {
+ *malloc_p = _PyMem_Allocators.object_malloc;
+ *realloc_p = _PyMem_Allocators.object_realloc;
+ *free_p = _PyMem_Allocators.object_free;
+ }
+ else {
+ *malloc_p = NULL;
+ *realloc_p = NULL;
+ *free_p = NULL;
+ return -1;
+ }
+ return 0;
+}
+
+int Py_SetAllocators(
+ char api,
+ void* (*user_malloc) (size_t),
+ void* (*user_realloc) (void*, size_t),
+ void (*user_free) (void*)
+ )
+{
+ _PyMem_InitAllocators();
+
+ if (api == PY_ALLOC_SYSTEM_API) {
+ _PyMem_Allocators.system_malloc = user_malloc;
+ _PyMem_Allocators.system_realloc = user_realloc;
+ _PyMem_Allocators.system_free = user_free;
+ }
+ else if (api == PY_ALLOC_MEM_API) {
+ _PyMem_Allocators.mem_malloc = user_malloc;
+ _PyMem_Allocators.mem_realloc = user_realloc;
+ _PyMem_Allocators.mem_free = user_free;
+ }
+ else if (api == PY_ALLOC_OBJECT_API) {
+ _PyMem_Allocators.object_malloc = user_malloc;
+ _PyMem_Allocators.object_realloc = user_realloc;
+ _PyMem_Allocators.object_free = user_free;
+ }
+ else {
+ return -1;
+ }
+ return 0;
+}
+
+void *
+PyMem_Malloc(size_t nbytes)
+{
+ _PyMem_InitAllocators();
+
+ if (nbytes > (size_t)PY_SSIZE_T_MAX)
+ return NULL;
+ if (nbytes == 0)
+ nbytes = 1;
+
+ return _PyMem_Allocators.mem_malloc(nbytes);
+}
+
+void *
+PyMem_Realloc(void *p, size_t nbytes)
+{
+ _PyMem_InitAllocators();
+
+ if (nbytes > (size_t)PY_SSIZE_T_MAX)
+ return NULL;
+ if (nbytes == 0)
+ nbytes = 1;
+
+ return _PyMem_Allocators.mem_realloc(p, nbytes);
+}
+
+void
+PyMem_Free(void *p)
+{
+ _PyMem_InitAllocators();
+ _PyMem_Allocators.mem_free(p);
+}
+
#ifdef WITH_PYMALLOC
/* An object allocator for Python.
@@ -529,7 +711,7 @@ new_arena(void)
if (numarenas > PY_SIZE_MAX / sizeof(*arenas))
return NULL; /* overflow */
nbytes = numarenas * sizeof(*arenas);
- arenaobj = (struct arena_object *)realloc(arenas, nbytes);
+ arenaobj = (struct arena_object *)_PyMem_Allocators.system_realloc(arenas, nbytes);
if (arenaobj == NULL)
return NULL;
arenas = arenaobj;
@@ -560,7 +742,7 @@ new_arena(void)
arenaobj = unused_arena_objects;
unused_arena_objects = arenaobj->nextarena;
assert(arenaobj->address == 0);
- arenaobj->address = (uptr)malloc(ARENA_SIZE);
+ arenaobj->address = (uptr)_PyMem_Allocators.system_malloc(ARENA_SIZE);
if (arenaobj->address == 0) {
/* The allocation failed: return NULL after putting the
* arenaobj back.
@@ -717,15 +899,17 @@ int Py_ADDRESS_IN_RANGE(void *P, poolp p
* Unless the optimizer reorders everything, being too smart...
*/
-#undef PyObject_Malloc
-void *
-PyObject_Malloc(size_t nbytes)
+static void *
+_PyObject_Malloc(size_t nbytes)
{
block *bp;
poolp pool;
poolp next;
uint size;
+ if (!_PyMem_Allocators.use_pymalloc)
+ return _PyMem_Allocators.system_malloc(nbytes);
+
/*
* This implicitly redirects malloc(0).
*/
@@ -899,14 +1083,13 @@ redirect:
*/
if (nbytes == 0)
nbytes = 1;
- return (void *)malloc(nbytes);
+ return (void *)_PyMem_Allocators.system_malloc(nbytes);
}
/* free */
-#undef PyObject_Free
-void
-PyObject_Free(void *p)
+static void
+_PyObject_Free(void *p)
{
poolp pool;
block *lastfree;
@@ -1005,7 +1188,7 @@ PyObject_Free(void *p)
unused_arena_objects = ao;
/* Free the entire arena. */
- free((void *)ao->address);
+ _PyMem_Allocators.system_free((void *)ao->address);
ao->address = 0; /* mark unassociated */
--narenas_currently_allocated;
@@ -1111,7 +1294,7 @@ PyObject_Free(void *p)
}
/* We didn't allocate this address. */
- free(p);
+ _PyMem_Allocators.system_free(p);
}
/* realloc. If p is NULL, this acts like malloc(nbytes). Else if nbytes==0,
@@ -1119,16 +1302,15 @@ PyObject_Free(void *p)
* return a non-NULL result.
*/
-#undef PyObject_Realloc
-void *
-PyObject_Realloc(void *p, size_t nbytes)
+static void *
+_PyObject_Realloc(void *p, size_t nbytes)
{
void *bp;
poolp pool;
size_t size;
if (p == NULL)
- return PyObject_Malloc(nbytes);
+ return _PyObject_Malloc(nbytes);
pool = POOL_ADDR(p);
if (Py_ADDRESS_IN_RANGE(p, pool)) {
@@ -1150,10 +1332,10 @@ PyObject_Realloc(void *p, size_t nbytes)
}
size = nbytes;
}
- bp = PyObject_Malloc(nbytes);
+ bp = _PyObject_Malloc(nbytes);
if (bp != NULL) {
memcpy(bp, p, size);
- PyObject_Free(p);
+ _PyObject_Free(p);
}
return bp;
}
@@ -1168,41 +1350,40 @@ PyObject_Realloc(void *p, size_t nbytes)
* at p. Instead we punt: let C continue to manage this block.
*/
if (nbytes)
- return realloc(p, nbytes);
+ return _PyMem_Allocators.system_realloc(p, nbytes);
/* C doesn't define the result of realloc(p, 0) (it may or may not
* return NULL then), but Python's docs promise that nbytes==0 never
* returns NULL. We don't pass 0 to realloc(), to avoid that endcase
* to begin with. Even then, we can't be sure that realloc() won't
* return NULL.
*/
- bp = realloc(p, 1);
+ bp = _PyMem_Allocators.system_realloc(p, 1);
return bp ? bp : p;
}
-#else /* ! WITH_PYMALLOC */
-
-/*==========================================================================*/
-/* pymalloc not enabled: Redirect the entry points to malloc. These will
- * only be used by extensions that are compiled with pymalloc enabled. */
+#endif /* WITH_PYMALLOC */
void *
-PyObject_Malloc(size_t n)
+PyObject_Malloc(size_t nbytes)
{
- return PyMem_MALLOC(n);
-}
-
-void *
-PyObject_Realloc(void *p, size_t n)
-{
- return PyMem_REALLOC(p, n);
+ _PyMem_InitAllocators();
+ return _PyMem_Allocators.object_malloc(nbytes);
}
void
PyObject_Free(void *p)
{
- PyMem_FREE(p);
+ _PyMem_InitAllocators();
+ _PyMem_Allocators.object_free(p);
}
-#endif /* WITH_PYMALLOC */
+
+void *
+PyObject_Realloc(void *p, size_t nbytes)
+{
+ _PyMem_InitAllocators();
+ return _PyMem_Allocators.object_realloc(p, nbytes);
+}
+
#ifdef PYMALLOC_DEBUG
/*==========================================================================*/
@@ -1324,7 +1505,7 @@ void *
/* overflow: can't represent total as a size_t */
return NULL;
- p = (uchar *)PyObject_Malloc(total);
+ p = (uchar *)_PyObject_Malloc(total);
if (p == NULL)
return NULL;
@@ -1358,7 +1539,7 @@ void
nbytes = read_size_t(q);
if (nbytes > 0)
memset(q, DEADBYTE, nbytes);
- PyObject_Free(q);
+ _PyObject_Free(q);
}
void *
@@ -1387,7 +1568,7 @@ void *
}
/* Resize and add decorations. */
- q = (uchar *)PyObject_Realloc(q - 2*SST, total);
+ q = (uchar *)_PyObject_Realloc(q - 2*SST, total);
if (q == NULL)
return NULL;
diff -r fb57d3630ed9 Objects/setobject.c
--- a/Objects/setobject.c Mon Mar 11 11:46:38 2013 +0100
+++ b/Objects/setobject.c Mon Mar 11 11:56:17 2013 +0100
@@ -558,7 +558,7 @@ set_dealloc(PySetObject *so)
}
if (so->table != so->smalltable)
PyMem_DEL(so->table);
- if (num_free_sets < MAXFREESETS && PyAnySet_CheckExact(so))
+ if (_PySys_UseFreeList && num_free_sets < MAXFREESETS && PyAnySet_CheckExact(so))
free_sets[num_free_sets++] = so;
else
so->ob_type->tp_free(so);
@@ -983,7 +983,8 @@ make_new_set(PyTypeObject *type, PyObjec
}
/* create PySetObject structure */
- if (num_free_sets &&
+ if (_PySys_UseFreeList &&
+ num_free_sets &&
(type == &PySet_Type || type == &PyFrozenSet_Type)) {
so = free_sets[--num_free_sets];
assert (so != NULL && PyAnySet_CheckExact(so));
diff -r fb57d3630ed9 Objects/tupleobject.c
--- a/Objects/tupleobject.c Mon Mar 11 11:46:38 2013 +0100
+++ b/Objects/tupleobject.c Mon Mar 11 11:56:17 2013 +0100
@@ -33,7 +33,7 @@ PyTuple_New(register Py_ssize_t size)
return NULL;
}
#if MAXSAVESIZE > 0
- if (size == 0 && free_tuples[0]) {
+ if (_PySys_UseFreeList && size == 0 && free_tuples[0]) {
op = free_tuples[0];
Py_INCREF(op);
#ifdef COUNT_ALLOCS
@@ -41,7 +41,7 @@ PyTuple_New(register Py_ssize_t size)
#endif
return (PyObject *) op;
}
- if (size < MAXSAVESIZE && (op = free_tuples[size]) != NULL) {
+ if (_PySys_UseFreeList && size < MAXSAVESIZE && (op = free_tuples[size]) != NULL) {
free_tuples[size] = (PyTupleObject *) op->ob_item[0];
num_free_tuples[size]--;
#ifdef COUNT_ALLOCS
@@ -72,7 +72,7 @@ PyTuple_New(register Py_ssize_t size)
for (i=0; i < size; i++)
op->ob_item[i] = NULL;
#if MAXSAVESIZE > 0
- if (size == 0) {
+ if (_PySys_UseFreeList && size == 0) {
free_tuples[0] = op;
++num_free_tuples[0];
Py_INCREF(op); /* extra INCREF so that this is never freed */
@@ -168,7 +168,8 @@ tupledealloc(register PyTupleObject *op)
while (--i >= 0)
Py_XDECREF(op->ob_item[i]);
#if MAXSAVESIZE > 0
- if (len < MAXSAVESIZE &&
+ if (_PySys_UseFreeList &&
+ len < MAXSAVESIZE &&
num_free_tuples[len] < MAXSAVEDTUPLES &&
op->ob_type == &PyTuple_Type)
{
diff -r fb57d3630ed9 Objects/unicodeobject.c
--- a/Objects/unicodeobject.c Mon Mar 11 11:46:38 2013 +0100
+++ b/Objects/unicodeobject.c Mon Mar 11 11:56:17 2013 +0100
@@ -240,7 +240,7 @@ PyUnicodeObject *_PyUnicode_New(Py_ssize
}
/* Unicode freelist & memory allocation */
- if (unicode_freelist) {
+ if (_PySys_UseFreeList && unicode_freelist) {
unicode = unicode_freelist;
unicode_freelist = *(PyUnicodeObject **)unicode;
unicode_freelist_size--;
@@ -292,7 +292,8 @@ PyUnicodeObject *_PyUnicode_New(Py_ssize
static
void unicode_dealloc(register PyUnicodeObject *unicode)
{
- if (PyUnicode_CheckExact(unicode) &&
+ if (_PySys_UseFreeList &&
+ PyUnicode_CheckExact(unicode) &&
unicode_freelist_size < MAX_UNICODE_FREELIST_SIZE) {
/* Keep-Alive optimization */
if (unicode->length >= KEEPALIVE_SIZE_LIMIT) {
diff -r fb57d3630ed9 Python/pythonrun.c
--- a/Python/pythonrun.c Mon Mar 11 11:46:38 2013 +0100
+++ b/Python/pythonrun.c Mon Mar 11 11:56:17 2013 +0100
@@ -148,6 +148,36 @@ add_flag(int flag, const char *envs)
return flag;
}
+static void
+inittracemalloc(void)
+{
+ PyObject *mod = NULL, *enable = NULL, *res = NULL;
+
+ mod = PyImport_ImportModule("_tracemalloc");
+ if (mod == NULL)
+ goto error;
+
+ enable = PyObject_GetAttrString(mod, "enable");
+ if (enable == NULL)
+ goto error;
+
+ res = PyObject_CallFunction(enable, NULL);
+ if (res == NULL)
+ goto error;
+
+ fprintf(stderr, "tracemalloc enabled\n");
+ goto done;
+
+error:
+ fprintf(stderr, "failed to enable tracemalloc:\n");
+ PyErr_Print();
+
+done:
+ Py_XDECREF(mod);
+ Py_XDECREF(enable);
+ Py_XDECREF(res);
+}
+
void
Py_InitializeEx(int install_sigs)
{
@@ -238,6 +268,9 @@ Py_InitializeEx(int install_sigs)
if (install_sigs)
initsigs(); /* Signal handling stuff, including initintr() */
+ if ((p = Py_GETENV("PYTRACEMALLOC")) && *p != '\0')
+ inittracemalloc();
+
initmain(); /* Module __main__ */
if (!Py_NoSiteFlag)
initsite(); /* Module site */
diff -r fb57d3630ed9 Python/sysmodule.c
--- a/Python/sysmodule.c Mon Mar 11 11:46:38 2013 +0100
+++ b/Python/sysmodule.c Mon Mar 11 11:56:17 2013 +0100
@@ -45,6 +45,8 @@ extern const char *PyWin_DLLVersionStrin
#include <langinfo.h>
#endif
+int _PySys_UseFreeList = 0;
+
PyObject *
PySys_GetObject(char *name)
{