This is the mail archive of the libc-hacker@sourceware.cygnus.com mailing list for the glibc project.

Note that libc-hacker is a closed list. You may look at the archives of this list, but subscription and posting are not open.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Minor malloc cleanup


Hi,

Here is a patch to bring malloc in line with the next stand-alone
`ptmalloc' release, which I plan to do tomorrow.  internal_function
can be used for any static function that is not assigned to an
externally visible pointer, right ?  Or even _any_ static function ?

Regards,
Wolfram.

1999-05-16  Wolfram Gloger  <wmglo@dent.med.uni-muenchen.de>

	* malloc/malloc.c: Cleanup to bring in line with released
	stand-alone version `ptmalloc'.  Update some comments.
	(internal_function): Move fallback definition so that the source
	compiles outside of libc, and use it in more places.
	(malloc_atfork): Fix when malloc_check is in use.

--- malloc.c.orig	Wed May 12 18:41:38 1999
+++ malloc.c	Sun May 16 23:21:43 1999
@@ -182,10 +182,11 @@
   MALLOC_HOOKS             (default: NOT defined)
      Define to enable support run-time replacement of the allocation
      functions through user-defined `hooks'.
-  REALLOC_ZERO_BYTES_FREES (default: NOT defined)
+  REALLOC_ZERO_BYTES_FREES (default: defined)
      Define this if you think that realloc(p, 0) should be equivalent
-     to free(p). Otherwise, since malloc returns a unique pointer for
-     malloc(0), so does realloc(p, 0).
+     to free(p).  (The C standard requires this behaviour, therefore
+     it is the default.)  Otherwise, since malloc returns a unique
+     pointer for malloc(0), so does realloc(p, 0).
   HAVE_MEMCPY               (default: defined)
      Define if you are not otherwise using ANSI STD C, but still
      have memcpy and memset in your C library and want to use them.
@@ -366,10 +367,10 @@
 #endif
 
 /*
-  REALLOC_ZERO_BYTES_FREES should be set if a call to
-  realloc with zero bytes should be the same as a call to free.
-  Some people think it should. Otherwise, since this malloc
-  returns a unique pointer for malloc(0), so does realloc(p, 0).
+  REALLOC_ZERO_BYTES_FREES should be set if a call to realloc with
+  zero bytes should be the same as a call to free.  The C standard
+  requires this. Otherwise, since this malloc returns a unique pointer
+  for malloc(0), so does realloc(p, 0).
 */
 
 
@@ -528,6 +529,9 @@
 #if !defined(MAP_ANONYMOUS) && defined(MAP_ANON)
 #define MAP_ANONYMOUS MAP_ANON
 #endif
+#if !defined(MAP_FAILED)
+#define MAP_FAILED ((char*)-1)
+#endif
 
 #ifndef MAP_NORESERVE
 # ifdef MAP_AUTORESRV
@@ -818,6 +822,15 @@
    computed. */
 
 
+
+/* On some platforms we can compile internal, not exported functions better.
+   Let the environment provide a macro and define it to be empty if it
+   is not available.  */
+#ifndef internal_function
+# define internal_function
+#endif
+
+
 /*
 
   Special defines for the Linux/GNU C library.
@@ -1230,13 +1243,6 @@
 
 #endif
 
-/* On some platforms we can compile internal, not exported functions better.
-   Let the environment provide a macro and define it to be empty if it
-   is not available.  */
-#ifndef internal_function
-# define internal_function
-#endif
-
 
 
 /* sizes, alignments */
@@ -1613,7 +1619,7 @@
   (void)mutex_init(&list_lock);
 }
 
-#endif
+#endif /* !defined NO_THREADS */
 
 /* Initialization routine. */
 #if defined(_LIBC)
@@ -1852,10 +1858,12 @@
   return p;
 }
 
+static void
+internal_function
 #if __STD_C
-static void munmap_chunk(mchunkptr p)
+munmap_chunk(mchunkptr p)
 #else
-static void munmap_chunk(p) mchunkptr p;
+munmap_chunk(p) mchunkptr p;
 #endif
 {
   INTERNAL_SIZE_T size = chunksize(p);
@@ -1877,10 +1885,12 @@
 
 #if HAVE_MREMAP
 
+static mchunkptr
+internal_function
 #if __STD_C
-static mchunkptr mremap_chunk(mchunkptr p, size_t new_size)
+mremap_chunk(mchunkptr p, size_t new_size)
 #else
-static mchunkptr mremap_chunk(p, new_size) mchunkptr p; size_t new_size;
+mremap_chunk(p, new_size) mchunkptr p; size_t new_size;
 #endif
 {
   size_t page_mask = malloc_getpagesize - 1;
@@ -1899,7 +1909,7 @@
   cp = (char *)mremap((char *)p - offset, size + offset, new_size,
                       MREMAP_MAYMOVE);
 
-  if (cp == (char *)-1) return 0;
+  if (cp == MAP_FAILED) return 0;
 
   p = (mchunkptr)(cp + offset);
 
@@ -3034,7 +3044,7 @@
   if(next->size < MINSIZE &&
      (unsigned long)sz > trim_threshold &&
      ar_ptr != &main_arena) {                /* fencepost */
-    heap_info* heap = heap_for_ptr(top(ar_ptr));
+    heap_info *heap = heap_for_ptr(top(ar_ptr));
 
     if(top(ar_ptr) == chunk_at_offset(heap, sizeof(*heap)) &&
        heap->prev == heap_for_ptr(p))
@@ -4212,6 +4222,7 @@
    into a user pointer with requested size sz. */
 
 static Void_t*
+internal_function
 #if __STD_C
 chunk2mem_check(mchunkptr p, size_t sz)
 #else
@@ -4293,6 +4304,7 @@
    necessary. */
 
 static int
+internal_function
 #if __STD_C
 top_check(void)
 #else
@@ -4525,11 +4537,18 @@
 #endif
 {
   Void_t *vptr = NULL;
+  mchunkptr victim;
 
   tsd_getspecific(arena_key, vptr);
   if(!vptr) {
-    mchunkptr victim = chunk_alloc(&main_arena, request2size(sz));
-    return victim ? chunk2mem(victim) : 0;
+    if(save_malloc_hook != malloc_check) {
+      victim = chunk_alloc(&main_arena, request2size(sz));
+      return victim ? chunk2mem(victim) : 0;
+    } else {
+      if(top_check() < 0) return 0;
+      victim = chunk_alloc(&main_arena, request2size(sz+1));
+      return victim ? chunk2mem_check(victim, sz) : 0;
+    }
   } else {
     /* Suspend the thread until the `atfork' handlers have completed.
        By that time, the hooks will have been reset as well, so that
@@ -4554,7 +4573,7 @@
   if (mem == 0)                              /* free(0) has no effect */
     return;
 
-  p = mem2chunk(mem);
+  p = mem2chunk(mem);         /* do not bother to replicate free_check here */
 
 #if HAVE_MMAP
   if (chunk_is_mmapped(p))                       /* release mmapped memory. */
@@ -4573,7 +4592,7 @@
     (void)mutex_unlock(&ar_ptr->mutex);
 }
 
-#endif
+#endif /* !defined NO_THREADS */
 
 #endif /* defined _LIBC || defined MALLOC_HOOKS */
 

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]