*/
/*
- Here are some notes on configuring Perl's malloc.
+ Here are some notes on configuring Perl's malloc. (For non-perl
+ usage see below.)
There are two macros which serve as bulk disablers of advanced
features of this malloc: NO_FANCY_MALLOC, PLAIN_MALLOC (undef by
*/
+/*
+ If used outside of Perl environment, it may be useful to redefine
+ the following macros (listed below with defaults):
+
+ # Type of address returned by allocation functions
+ Malloc_t void *
+
+ # Type of size argument for allocation functions
+ MEM_SIZE unsigned long
+
+ # Maximal value in LONG
+ LONG_MAX 0x7FFFFFFF
+
+ # Unsigned integer type big enough to keep a pointer
+ UV unsigned long
+
+ # Type of pointer with 1-byte granularity
+ caddr_t char *
+
+ # Type returned by free()
+ Free_t void
+
+ # Fatal error reporting function
+ croak(format, arg) warn(idem) + exit(1)
+
+ # Error reporting function
+ warn(format, arg) fprintf(stderr, idem)
+
+ # Locking/unlocking for MT operation
+ MALLOC_LOCK MUTEX_LOCK(PL_malloc_mutex)
+ MALLOC_UNLOCK MUTEX_UNLOCK(PL_malloc_mutex)
+
+ # Locking/unlocking mutex for MT operation
+ MUTEX_LOCK(l) void
+ MUTEX_UNLOCK(l) void
+ */
+
#ifndef NO_FANCY_MALLOC
# ifndef SMALL_BUCKET_VIA_TABLE
# define SMALL_BUCKET_VIA_TABLE
* implementation, the available sizes are 2^n-4 (or 2^n-12) bytes long.
* If PACK_MALLOC is defined, small blocks are 2^n bytes long.
* This is designed for use in a program that uses vast quantities of memory,
- * but bombs when it runs out.
+ * but bombs when it runs out.
+ *
+ * Modifications Copyright Ilya Zakharevich 1996-98.
+ *
+ * Still very quick, but much more thrifty. (Std config is 10% slower
+ * than it was, and takes 67% of old heap size for typical usage.)
+ *
+ * Allocations of small blocks are now table-driven to many different
+ * buckets. Sizes of really big buckets are increased to accomodata
+ * common size=power-of-2 blocks. Running-out-of-memory is made into
+ * an exception. Deeply configurable and thread-safe.
+ *
*/
#ifdef PERL_CORE
# define PerlIO_stderr() stderr
# endif
# ifndef croak /* make depend */
-# define croak(mess, arg) warn((mess), (arg)); exit(1);
+# define croak(mess, arg) (warn((mess), (arg)), exit(1))
# endif
# ifndef warn
-# define warn(mess, arg) fprintf(stderr, (mess), (arg));
+# define warn(mess, arg) fprintf(stderr, (mess), (arg))
# endif
# ifdef DEBUG_m
# undef DEBUG_m
# define MUTEX_UNLOCK(l)
#endif
+#ifndef MALLOC_LOCK
+# define MALLOC_LOCK MUTEX_LOCK(PL_malloc_mutex)
+#endif
+
+#ifndef MALLOC_UNLOCK
+# define MALLOC_UNLOCK MUTEX_UNLOCK(PL_malloc_mutex)
+#endif
+
#ifdef DEBUGGING
# undef DEBUG_m
# define DEBUG_m(a) if (PL_debug & 128) a
if (size >= BIG_SIZE) {
/* Give the possibility to recover: */
- MUTEX_UNLOCK(&PL_malloc_mutex);
+ MALLOC_UNLOCK;
croak("Out of memory during \"large\" request for %i bytes", size);
}
SvCUR(sv) = SvLEN(sv) = 0;
}
do_croak:
- MUTEX_UNLOCK(&PL_malloc_mutex);
+ MALLOC_UNLOCK;
croak("Out of memory during request for %i bytes", size);
}
croak("%s", "panic: malloc");
#endif
- MUTEX_LOCK(&PL_malloc_mutex);
+ MALLOC_LOCK;
/*
* Convert amount of memory requested into
* closest block size stored in hash buckets
if (nextf[bucket] == NULL)
morecore(bucket);
if ((p = nextf[bucket]) == NULL) {
- MUTEX_UNLOCK(&PL_malloc_mutex);
+ MALLOC_UNLOCK;
#ifdef PERL_CORE
if (!PL_nomemok) {
PerlIO_puts(PerlIO_stderr(),"Out of memory!\n");
*((u_int *)((caddr_t)p + nbytes - RSLOP)) = RMAGIC;
}
#endif
- MUTEX_UNLOCK(&PL_malloc_mutex);
+ MALLOC_UNLOCK;
return ((Malloc_t)(p + CHUNK_SHIFT));
}
"failed to fix bad sbrk()\n"));
#ifdef PACK_MALLOC
if (slack) {
- MUTEX_UNLOCK(&PL_malloc_mutex);
+ MALLOC_UNLOCK;
croak("%s", "panic: Off-page sbrk");
}
#endif
if (nextf[bucket])
return;
if (bucket == sizeof(MEM_SIZE)*8*BUCKETS_PER_POW2) {
- MUTEX_UNLOCK(&PL_malloc_mutex);
+ MALLOC_UNLOCK;
croak("%s", "Out of memory during ridiculously large request");
}
if (bucket > max_bucket)
#endif
return; /* sanity */
}
- MUTEX_LOCK(&PL_malloc_mutex);
+ MALLOC_LOCK;
#ifdef RCHECK
ASSERT(ovp->ov_rmagic == RMAGIC, "chunk's head overwrite");
if (OV_INDEX(ovp) <= MAX_SHORT_BUCKET) {
size = OV_INDEX(ovp);
ovp->ov_next = nextf[size];
nextf[size] = ovp;
- MUTEX_UNLOCK(&PL_malloc_mutex);
+ MALLOC_UNLOCK;
}
/*
if (!cp)
return malloc(nbytes);
- MUTEX_LOCK(&PL_malloc_mutex);
+ MALLOC_LOCK;
ovp = (union overhead *)((caddr_t)cp
- sizeof (union overhead) * CHUNK_SHIFT);
bucket = OV_INDEX(ovp);
}
#endif
res = cp;
- MUTEX_UNLOCK(&PL_malloc_mutex);
+ MALLOC_UNLOCK;
DEBUG_m(PerlIO_printf(Perl_debug_log,
"0x%lx: (%05lu) realloc %ld bytes inplace\n",
(unsigned long)res,(unsigned long)(PL_an++),
goto hard_way;
} else {
hard_way:
- MUTEX_UNLOCK(&PL_malloc_mutex);
+ MALLOC_UNLOCK;
DEBUG_m(PerlIO_printf(Perl_debug_log,
"0x%lx: (%05lu) realloc %ld bytes the hard way\n",
(unsigned long)cp,(unsigned long)(PL_an++),