ae2451
Make it more likely for the system allocator to release free()d memory arenas on glibc-based systems. 
ae2451
Patch by Charles-François Natali.
ae2451
https://bugs.python.org/issue20494
ae2451
ae2451
diff --git a/Objects/obmalloc.c b/Objects/obmalloc.c
ae2451
--- a/Objects/obmalloc.c
ae2451
+++ b/Objects/obmalloc.c
ae2451
@@ -2,6 +2,13 @@
ae2451
 
ae2451
 #ifdef WITH_PYMALLOC
ae2451
 
ae2451
+#ifdef HAVE_MMAP
ae2451
+ #include <sys/mman.h>
ae2451
+ #ifdef MAP_ANONYMOUS
ae2451
+  #define ARENAS_USE_MMAP
ae2451
+ #endif
ae2451
+#endif
ae2451
+
ae2451
 #ifdef WITH_VALGRIND
ae2451
 #include <valgrind/valgrind.h>
ae2451
 
ae2451
@@ -75,7 +82,8 @@ static int running_on_valgrind = -1;
ae2451
  * Allocation strategy abstract:
ae2451
  *
ae2451
  * For small requests, the allocator sub-allocates <Big> blocks of memory.
ae2451
- * Requests greater than 256 bytes are routed to the system's allocator.
ae2451
+ * Requests greater than SMALL_REQUEST_THRESHOLD bytes are routed to the
ae2451
+ * system's allocator. 
ae2451
  *
ae2451
  * Small requests are grouped in size classes spaced 8 bytes apart, due
ae2451
  * to the required valid alignment of the returned address. Requests of
ae2451
@@ -107,10 +115,11 @@ static int running_on_valgrind = -1;
ae2451
  *       57-64                   64                       7
ae2451
  *       65-72                   72                       8
ae2451
  *        ...                   ...                     ...
ae2451
- *      241-248                 248                      30
ae2451
- *      249-256                 256                      31
ae2451
+ *      497-504                 504                      62
ae2451
+ *      505-512                 512                      63 
ae2451
  *
ae2451
- *      0, 257 and up: routed to the underlying allocator.
ae2451
+ *      0, SMALL_REQUEST_THRESHOLD + 1 and up: routed to the underlying
ae2451
+ *      allocator.
ae2451
  */
ae2451
 
ae2451
 /*==========================================================================*/
ae2451
@@ -143,10 +152,13 @@ static int running_on_valgrind = -1;
ae2451
  *      1) ALIGNMENT <= SMALL_REQUEST_THRESHOLD <= 256
ae2451
  *      2) SMALL_REQUEST_THRESHOLD is evenly divisible by ALIGNMENT
ae2451
  *
ae2451
+ * Note: a size threshold of 512 guarantees that newly created dictionaries
ae2451
+ * will be allocated from preallocated memory pools on 64-bit.
ae2451
+ *
ae2451
  * Although not required, for better performance and space efficiency,
ae2451
  * it is recommended that SMALL_REQUEST_THRESHOLD is set to a power of 2.
ae2451
  */
ae2451
-#define SMALL_REQUEST_THRESHOLD 256
ae2451
+#define SMALL_REQUEST_THRESHOLD 512 
ae2451
 #define NB_SMALL_SIZE_CLASSES   (SMALL_REQUEST_THRESHOLD / ALIGNMENT)
ae2451
 
ae2451
 /*
ae2451
@@ -174,15 +186,15 @@ static int running_on_valgrind = -1;
ae2451
 /*
ae2451
  * The allocator sub-allocates <Big> blocks of memory (called arenas) aligned
ae2451
  * on a page boundary. This is a reserved virtual address space for the
ae2451
- * current process (obtained through a malloc call). In no way this means
ae2451
- * that the memory arenas will be used entirely. A malloc(<Big>) is usually
ae2451
- * an address range reservation for <Big> bytes, unless all pages within this
ae2451
- * space are referenced subsequently. So malloc'ing big blocks and not using
ae2451
- * them does not mean "wasting memory". It's an addressable range wastage...
ae2451
+ * current process (obtained through a malloc()/mmap() call). In no way this
ae2451
+ * means that the memory arenas will be used entirely. A malloc(<Big>) is
ae2451
+ * usually an address range reservation for <Big> bytes, unless all pages within
ae2451
+ * this space are referenced subsequently. So malloc'ing big blocks and not
ae2451
+ * using them does not mean "wasting memory". It's an addressable range
ae2451
+ * wastage... 
ae2451
  *
ae2451
- * Therefore, allocating arenas with malloc is not optimal, because there is
ae2451
- * some address space wastage, but this is the most portable way to request
ae2451
- * memory from the system across various platforms.
ae2451
+ * Arenas are allocated with mmap() on systems supporting anonymous memory
ae2451
+ * mappings to reduce heap fragmentation.
ae2451
  */
ae2451
 #define ARENA_SIZE              (256 << 10)     /* 256KB */
ae2451
 
ae2451
@@ -440,6 +452,9 @@ static poolp usedpools[2 * ((NB_SMALL_SI
ae2451
     , PT(48), PT(49), PT(50), PT(51), PT(52), PT(53), PT(54), PT(55)
ae2451
 #if NB_SMALL_SIZE_CLASSES > 56
ae2451
     , PT(56), PT(57), PT(58), PT(59), PT(60), PT(61), PT(62), PT(63)
ae2451
+#if NB_SMALL_SIZE_CLASSES > 64
ae2451
+#error "NB_SMALL_SIZE_CLASSES should be less than 64"
ae2451
+#endif /* NB_SMALL_SIZE_CLASSES > 64 */
ae2451
 #endif /* NB_SMALL_SIZE_CLASSES > 56 */
ae2451
 #endif /* NB_SMALL_SIZE_CLASSES > 48 */
ae2451
 #endif /* NB_SMALL_SIZE_CLASSES > 40 */
ae2451
@@ -577,7 +592,12 @@ new_arena(void)
ae2451
     arenaobj = unused_arena_objects;
ae2451
     unused_arena_objects = arenaobj->nextarena;
ae2451
     assert(arenaobj->address == 0);
ae2451
+#ifdef ARENAS_USE_MMAP
ae2451
+    arenaobj->address = (uptr)mmap(NULL, ARENA_SIZE, PROT_READ|PROT_WRITE,
ae2451
+                                   MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
ae2451
+#else
ae2451
     arenaobj->address = (uptr)malloc(ARENA_SIZE);
ae2451
+#endif    
ae2451
     if (arenaobj->address == 0) {
ae2451
         /* The allocation failed: return NULL after putting the
ae2451
          * arenaobj back.
ae2451
@@ -1054,7 +1074,11 @@ PyObject_Free(void *p)
ae2451
                 unused_arena_objects = ao;
ae2451
 
ae2451
                 /* Free the entire arena. */
ae2451
+#ifdef ARENAS_USE_MMAP
ae2451
+                munmap((void *)ao->address, ARENA_SIZE);
ae2451
+#else
ae2451
                 free((void *)ao->address);
ae2451
+#endif
ae2451
                 ao->address = 0;                        /* mark unassociated */
ae2451
                 --narenas_currently_allocated;
ae2451
 
ae2451
diff --git a/configure b/configure
ae2451
--- a/configure
ae2451
+++ b/configure
ae2451
@@ -10164,7 +10164,7 @@ for ac_func in alarm setitimer getitimer
ae2451
  clock confstr ctermid execv fchmod fchown fork fpathconf ftime ftruncate \
ae2451
  gai_strerror getgroups getlogin getloadavg getpeername getpgid getpid \
ae2451
  getpriority getresuid getresgid getpwent getspnam getspent getsid getwd \
ae2451
- initgroups kill killpg lchmod lchown lstat mkfifo mknod mktime \
ae2451
+ initgroups kill killpg lchmod lchown lstat mkfifo mknod mktime mmap \
ae2451
  mremap nice pathconf pause plock poll pthread_init \
ae2451
  putenv readlink realpath \
ae2451
  select sem_open sem_timedwait sem_getvalue sem_unlink setegid seteuid \
ae2451
diff --git a/configure.ac b/configure.ac
ae2451
--- a/configure.ac
ae2451
+++ b/configure.ac
ae2451
@@ -2905,7 +2905,7 @@ AC_CHECK_FUNCS(alarm setitimer getitimer
ae2451
  clock confstr ctermid execv fchmod fchown fork fpathconf ftime ftruncate \
ae2451
  gai_strerror getgroups getlogin getloadavg getpeername getpgid getpid \
ae2451
  getpriority getresuid getresgid getpwent getspnam getspent getsid getwd \
ae2451
- initgroups kill killpg lchmod lchown lstat mkfifo mknod mktime \
ae2451
+ initgroups kill killpg lchmod lchown lstat mkfifo mknod mktime mmap \
ae2451
  mremap nice pathconf pause plock poll pthread_init \
ae2451
  putenv readlink realpath \
ae2451
  select sem_open sem_timedwait sem_getvalue sem_unlink setegid seteuid \
ae2451
diff --git a/pyconfig.h.in b/pyconfig.h.in
ae2451
--- a/pyconfig.h.in
ae2451
+++ b/pyconfig.h.in
ae2451
@@ -475,6 +475,9 @@
ae2451
 /* Define to 1 if you have the `mktime' function. */
ae2451
 #undef HAVE_MKTIME
ae2451
 
ae2451
+/* Define to 1 if you have the `mmap' function. */
ae2451
+#undef HAVE_MMAP
ae2451
+
ae2451
 /* Define to 1 if you have the `mremap' function. */
ae2451
 #undef HAVE_MREMAP
ae2451