From: Nicholas Clark <nick@ccl4.org>
Date: Tue, 20 Mar 2007 23:20:46 +0000 (+0000)
Subject: In safecalloc, use the system calloc() for the non-DEBUGGING case, as
X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=e1a95402a0ef053bf8dd15ba3824743513f0725e;p=p5sagit%2Fp5-mst-13.2.git

In safecalloc, use the system calloc() for the non-DEBUGGING case, as
it may be able to memset() to 0 for free.

p4raw-id: //depot/perl@30652
---

diff --git a/util.c b/util.c
index 5a95d68..b6acab4 100644
--- a/util.c
+++ b/util.c
@@ -258,11 +258,16 @@ Perl_safesyscalloc(MEM_SIZE count, MEM_SIZE size)
 {
     dTHX;
     Malloc_t ptr;
+    const MEM_SIZE total_size = size * count
+#ifdef PERL_TRACK_MEMPOOL
+	+ sTHX
+#endif
+	;
 
 #ifdef HAS_64K_LIMIT
-    if (size * count > 0xffff) {
+    if (total_size > 0xffff) {
 	PerlIO_printf(Perl_error_log,
-		      "Allocation too large: %lx\n", size * count) FLUSH;
+		      "Allocation too large: %lx\n", total_size) FLUSH;
 	my_exit(1);
     }
 #endif /* HAS_64K_LIMIT */
@@ -270,20 +275,24 @@ Perl_safesyscalloc(MEM_SIZE count, MEM_SIZE size)
     if ((long)size < 0 || (long)count < 0)
 	Perl_croak_nocontext("panic: calloc");
 #endif
-    size *= count;
 #ifdef PERL_TRACK_MEMPOOL
-    size += sTHX;
+    /* Have to use malloc() because we've added some space for our tracking
+       header.  */
+    ptr = (Malloc_t)PerlMem_malloc(total_size);
+#else
+    /* Use calloc() because it might save a memset() if the memory is fresh
+       and clean from the OS.  */
+    ptr = (Malloc_t)PerlMem_calloc(count, size);
 #endif
-    ptr = (Malloc_t)PerlMem_malloc(size?size:1);	/* malloc(0) is NASTY on our system */
     PERL_ALLOC_CHECK(ptr);
-    DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%"UVxf": (%05ld) calloc %ld x %ld bytes\n",PTR2UV(ptr),(long)PL_an++,(long)count,(long)size));
+    DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%"UVxf": (%05ld) calloc %ld x %ld bytes\n",PTR2UV(ptr),(long)PL_an++,(long)count,(long)total_size));
     if (ptr != NULL) {
-	memset((void*)ptr, 0, size);
 #ifdef PERL_TRACK_MEMPOOL
 	{
 	    struct perl_memory_debug_header *const header
 		= (struct perl_memory_debug_header *)ptr;
 
+	    memset((void*)ptr, 0, total_size);
 	    header->interpreter = aTHX;
 	    /* Link us into the list.  */
 	    header->prev = &PL_memory_debug_header;
@@ -291,7 +300,7 @@ Perl_safesyscalloc(MEM_SIZE count, MEM_SIZE size)
 	    PL_memory_debug_header.next = header;
 	    header->next->prev = header;
 #  ifdef PERL_POISON
-	    header->size = size;
+	    header->size = total_size;
 #  endif
 	    ptr = (Malloc_t)((char*)ptr+sTHX);
 	}