[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [patches] malloc & madvise



> I'm not an expert on the malloc internals, but one thing to consider when 
> changing internal datastructures like this is whether you keep 
> compatibility for malloc_set_state/malloc_get_state: whether a binary 

It should be easy to achieve full binary compatibility for set/get state
as well: It is sufficient to initialize the additional data structures
for holding free blocks in malloc_set_state, as done by the attached
patch. If I understood correctly, malloc_set_state is usually called at
the beginning of the program, so initializing empty lists is appropriate.

> reliably work with libc with this change, and vice versa.  If it won't, 

With the attached patch, it should work reliably: The saved format never
contains any information about the free blocks lists. If using new libc,
these lists are initialized in malloc_set_state.

> there are ABI issues and it may be necessary to go (by default) with a 
> dumber approach that doesn't need to keep extra state.

Are there any other caveats with this approach?

	Richard
diff -ur eglibc-2.10.1.orig/malloc/hooks.c eglibc-2.10.1.new/malloc/hooks.c
--- eglibc-2.10.1.orig/malloc/hooks.c	2009-04-30 23:37:18.000000000 +0200
+++ eglibc-2.10.1.new/malloc/hooks.c	2009-11-17 16:19:09.542367372 +0100
@@ -684,6 +684,29 @@
     narenas = ms->narenas;
 #endif
   }
+#ifdef USE_MADVISE
+  /*
+    We need to initialize data structures keeping track of free blocks. 
+    We initialize empty lists whenever it is necessary. This makes sense if
+    malloc_set_state is called at the beginning of the application and the
+    free blocks had no reason to be paged in.
+  */
+  main_arena.can_free = 0;
+  main_arena.free_blocks.size = 0;
+  main_arena.free_blocks.fd_time = main_arena.free_blocks.bk_time = 
+    &main_arena.free_blocks;
+  for (i = 1; i < NBINS; ++i) {
+    b = bin_at(&main_arena,i);
+    for (mchunkptr p = last(b); p != b; p = p->bk) {
+      if (CHUNK_IS_LARGE(p)) {
+        p->free_blocks.size = 0;
+        p->free_blocks.fd_chunk = p->free_blocks.bk_chunk = &p->free_blocks;
+      }
+    }
+  }
+  check_free_blocks(&main_arena);
+#endif
+
   check_malloc_state(&main_arena);
 
   (void)mutex_unlock(&main_arena.mutex);