summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNicholas Clark <nick@ccl4.org>2007-03-20 23:20:46 +0000
committerNicholas Clark <nick@ccl4.org>2007-03-20 23:20:46 +0000
commite1a95402a0ef053bf8dd15ba3824743513f0725e (patch)
treee24c3d7f39399bfd622523e87c08d2da852b5a71
parentf481a1585ea33cf1fd9d19cebebe19137cc6977a (diff)
downloadperl-e1a95402a0ef053bf8dd15ba3824743513f0725e.tar.gz
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
-rw-r--r--util.c25
1 files changed, 17 insertions, 8 deletions
diff --git a/util.c b/util.c
index 5a95d68247..b6acab49b9 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);
}