or you can answer 'n' at the appropriate interactive Configure prompt.
+Note that Perl's malloc family of functions are called Perl_malloc(),
+Perl_realloc(), Perl_calloc() and Perl_mfree(). The names do not clash
+with the system versions of these functions. See -DPERL_POLLUTE_MALLOC
+below if you want to do that for some reason.
+
=head2 Malloc Performance Flags
If you are using Perl's malloc, you may add one or more of the following
Undefined by default. Defining it in addition to NO_FANCY_MALLOC returns
malloc to the version used in Perl version 5.000.
+=item -DPERL_POLLUTE_MALLOC
+
+Undefined by default. This is used to force Perl's malloc family of functions
+to have the same names as the system versions. This is normally only required
+when you have a need to replace the system versions of these functions.
+This may be sometimes required when you have libraries that like to free()
+data that may have been allocated by Perl_malloc() and vice versa.
+
+Note that enabling this option may sometimes lead to duplicate symbols from
+the linker for malloc et al. In such cases, the system probably does not
+allow its malloc functions to be fully replaced with custom versions.
+
=back
=head2 Building a debugging perl
at Perl startup.
-=item malloc duplicates
-
-If you get duplicates upon linking for malloc et al, add -DEMBEDMYMALLOC
-to your ccflags variable in config.sh.
-
=item varargs
If you get varargs problems with gcc, be sure that gcc is installed
Namespace cleanup
CPP-space: restrict what we export from headers
- stop malloc()/free() pollution unless asked
header-space: move into CORE/perl/
API-space: begin list of things that constitute public api
/* This comes after <stdlib.h> so we don't try to change the standard
* library prototypes; we'll use our own instead. */
-#if defined(MYMALLOC) && (defined(HIDEMYMALLOC) || defined(EMBEDMYMALLOC))
+#if defined(MYMALLOC)
-# ifdef HIDEMYMALLOC
-# define malloc Mymalloc
-# define calloc Mycalloc
-# define realloc Myremalloc
-# define free Myfree
-# endif
-# ifdef EMBEDMYMALLOC
-# define malloc Perl_malloc
-# define calloc Perl_calloc
-# define realloc Perl_realloc
-# define free Perl_free
-# endif
+# define malloc Perl_malloc
+# define calloc Perl_calloc
+# define realloc Perl_realloc
+# define free Perl_mfree
- Malloc_t malloc proto((MEM_SIZE nbytes));
- Malloc_t calloc proto((MEM_SIZE elements, MEM_SIZE size));
- Malloc_t realloc proto((Malloc_t where, MEM_SIZE nbytes));
- Free_t free proto((Malloc_t where));
+Malloc_t Perl_malloc proto((MEM_SIZE nbytes));
+Malloc_t Perl_calloc proto((MEM_SIZE elements, MEM_SIZE size));
+Malloc_t Perl_realloc proto((Malloc_t where, MEM_SIZE nbytes));
+Free_t Perl_mfree proto((Malloc_t where));
-#endif /* MYMALLOC && (HIDEMYMALLOC || EMBEDMYMALLOC) */
+#endif /* MYMALLOC */
#ifdef I_STRING
#include <string.h>
# Do not wrap the following long line
malloc_cflags='ccflags="$ccflags -DPLAIN_MALLOC -DNO_FANCY_MALLOC -DUSE_PERL_SBRK"'
-# Note that an empty malloc_cflags appears in config.sh if perl's
-# malloc() is not used. his is harmless.
-case "$usemymalloc" in
-n) unset malloc_cflags;;
-*) ccflags="$ccflags -DHIDEMYMALLOC"
-esac
-
# When MachTen does a fork(), it immediately copies the whole of
# the parent process' data space for the child. This can be
# expensive. Using vfork() where appropriate avoids this cost.
# than no perl at all.
#
# So, this hintsfile is using perl's malloc. If you want to turn
-# perl's malloc off, you need to remove '-DUSE_PERL_SBRK' and
-# '-DHIDEMYMALLOC' from the ccflags and set usemymalloc to 'n'.
+# perl's malloc off, you need to remove '-DUSE_PERL_SBRK'
+# from the ccflags and set usemymalloc to 'n'.
#
# 1997:
# From perl5.003_22 the malloc bug has no impact any more. We can run
#
# use the following two lines to enable USE_PERL_SBRK. Try this if you
# encounter intermittent core dumps:
-#ccflags='-DUSE_NEXT_CTYPE -DUSE_PERL_SBRK -DHIDEMYMALLOC'
+#ccflags='-DUSE_NEXT_CTYPE -DUSE_PERL_SBRK'
#usemymalloc='y'
# use the following two lines if you have perl5.003_22 or better and
# do not encounter intermittent core dumps.
ldflags='-dynamic -prebind'
lddlflags='-dynamic -bundle -undefined suppress'
-ccflags='-dynamic -fno-common -DUSE_NEXT_CTYPE -DUSE_PERL_SBRK -DHIDEMYMALLOC'
+ccflags='-dynamic -fno-common -DUSE_NEXT_CTYPE -DUSE_PERL_SBRK'
cccdlflags='none'
ld='cc'
#optimize='-g -O'
# constructs make a lot of noise, so I turn those warnings off.
# A few still remain...
#
-# HIDEMYMALLOC is necessary if using mymalloc since it is very
-# tricky (though not impossible) to totally replace the watcom
-# malloc/free set.
-#
# unix.h is required as a general rule for unixy applications.
#----------------------------------------------------------------
-ccflags='-DHIDEMYMALLOC -mf -w4 -Wc,-wcd=202 -Wc,-wcd=203 -Wc,-wcd=302 -Wc,-fi=unix.h'
+ccflags='-mf -w4 -Wc,-wcd=202 -Wc,-wcd=203 -Wc,-wcd=302 -Wc,-fi=unix.h'
#----------------------------------------------------------------
# ldflags:
#else /* PERL_OBJECT */
+#ifdef MYMALLOC
+#define PerlMem_malloc(size) Perl_malloc((size))
+#define PerlMem_realloc(buf, size) Perl_realloc((buf), (size))
+#define PerlMem_free(buf) Perl_mfree((buf))
+#else
#define PerlMem_malloc(size) malloc((size))
#define PerlMem_realloc(buf, size) realloc((buf), (size))
#define PerlMem_free(buf) free((buf))
+#endif
#endif /* PERL_OBJECT */
#endif
Malloc_t
-malloc(register size_t nbytes)
+Perl_malloc(register size_t nbytes)
{
register union overhead *p;
register int bucket;
}
Free_t
-free(void *mp)
+Perl_mfree(void *mp)
{
register MEM_SIZE size;
register union overhead *ovp;
#define reall_srchlen 4 /* 4 should be plenty, -1 =>'s whole list */
Malloc_t
-realloc(void *mp, size_t nbytes)
+Perl_realloc(void *mp, size_t nbytes)
{
register MEM_SIZE onb;
union overhead *ovp;
BARK_64K_LIMIT("Reallocation",nbytes,size);
if (!cp)
- return malloc(nbytes);
+ return Perl_malloc(nbytes);
MALLOC_LOCK;
ovp = (union overhead *)((caddr_t)cp
"0x%lx: (%05lu) realloc %ld bytes the hard way\n",
(unsigned long)cp,(unsigned long)(PL_an++),
(long)size));
- if ((res = (char*)malloc(nbytes)) == NULL)
+ if ((res = (char*)Perl_malloc(nbytes)) == NULL)
return (NULL);
if (cp != res) /* common optimization */
Copy(cp, res, (MEM_SIZE)(nbytes<onb?nbytes:onb), char);
if (was_alloced)
- free(cp);
+ Perl_mfree(cp);
}
return ((Malloc_t)res);
}
}
Malloc_t
-calloc(register size_t elements, register size_t size)
+Perl_calloc(register size_t elements, register size_t size)
{
long sz = elements * size;
- Malloc_t p = malloc(sz);
+ Malloc_t p = Perl_malloc(sz);
if (p) {
memset((void*)p, 0, sz);
# endif
# ifdef PERL_SBRK_VIA_MALLOC
-# if defined(HIDEMYMALLOC) || defined(EMBEDMYMALLOC)
-# undef malloc /* Expose names that */
-# undef calloc /* HIDEMYMALLOC hides */
-# undef realloc
-# undef free
-# else
-# include "Error: -DPERL_SBRK_VIA_MALLOC needs -D(HIDE|EMBED)MYMALLOC"
-# endif
/* it may seem schizophrenic to use perl's malloc and let it call system */
/* malloc, the reason for that is only the 3.2 version of the OS that had */
* library prototypes; we'll use our own in proto.h instead. */
#ifdef MYMALLOC
-
-# ifdef HIDEMYMALLOC
-# define malloc Mymalloc
-# define calloc Mycalloc
-# define realloc Myrealloc
-# define free Myfree
-Malloc_t Mymalloc _((MEM_SIZE nbytes));
-Malloc_t Mycalloc _((MEM_SIZE elements, MEM_SIZE size));
-Malloc_t Myrealloc _((Malloc_t where, MEM_SIZE nbytes));
-Free_t Myfree _((Malloc_t where));
-# endif
-# ifdef EMBEDMYMALLOC
-# define malloc Perl_malloc
-# define calloc Perl_calloc
-# define realloc Perl_realloc
-/* VMS' external symbols are case-insensitive, and there's already a */
-/* perl_free in perl.h */
-#ifdef VMS
-# define free Perl_myfree
-#else
-# define free Perl_free
-#endif
+# ifdef PERL_POLLUTE_MALLOC
+# define Perl_malloc malloc
+# define Perl_calloc calloc
+# define Perl_realloc realloc
+# define Perl_mfree free
+# else
+# define EMBEDMYMALLOC /* for compatibility */
+# endif
Malloc_t Perl_malloc _((MEM_SIZE nbytes));
Malloc_t Perl_calloc _((MEM_SIZE elements, MEM_SIZE size));
Malloc_t Perl_realloc _((Malloc_t where, MEM_SIZE nbytes));
-#ifdef VMS
-Free_t Perl_myfree _((Malloc_t where));
-#else
-Free_t Perl_free _((Malloc_t where));
-#endif
+/* 'mfree' rather than 'free', since there is already a 'perl_free'
+ * that causes clashes with case-insensitive linkers */
+Free_t Perl_mfree _((Malloc_t where));
# endif
# undef safemalloc
# undef safecalloc
# undef saferealloc
# undef safefree
-# define safemalloc malloc
-# define safecalloc calloc
-# define saferealloc realloc
-# define safefree free
+# define safemalloc Perl_malloc
+# define safecalloc Perl_calloc
+# define saferealloc Perl_realloc
+# define safefree Perl_mfree
#endif /* MYMALLOC */
preprocessor definitions are not available by default. You need to explicitly
compile perl with C<-DPERL_POLLUTE> in order to get these definitions.
+=item C<PERL_POLLUTE_MALLOC>
+
+Enabling the use of Perl's malloc in release 5.005 and earlier caused
+the namespace of system versions of the malloc family of functions to
+be usurped by the Perl versions of these functions, since they used the
+same names by default.
+
+Besides causing problems on platforms that do not allow these functions to
+be cleanly replaced, this also meant that the system versions could not
+be called in programs that used Perl's malloc. Previous versions of Perl
+have allowed this behavior to be suppressed with the HIDEMYMALLOC and
+EMBEDMYMALLOC preprocessor definitions.
+
+As of release 5.006, Perl's malloc family of functions have default names
+distinct from the system versions. You need to explicitly compile perl with
+C<-DPERL_POLLUTE_MALLOC> in order to get the older behavior. HIDEMYMALLOC
+and EMBEDMYMALLOC have no effect, since the behavior they enabled is now
+the default.
+
+Note that these functions do B<not> constitute Perl's memory allocation API.
+See L<perlguts/"Memory Allocation"> for further information about that.
+
=item C<PL_na> and C<dTHR> Issues
The C<PL_na> global is now thread local, so a C<dTHR> declaration is needed
=head2 Memory Allocation
-It is suggested that you use the version of malloc that is distributed
+All memory meant to be used with the Perl API functions should be manipulated
+using the macros described in this section. The macros provide the necessary
+transparency between differences in the actual malloc implementation that is
+used within perl.
+
+It is suggested that you enable the version of malloc that is distributed
with Perl. It keeps pools of various sizes of unallocated memory in
order to satisfy allocation requests more quickly. However, on some
platforms, it may cause spurious malloc or free errors.
#ifdef PURIFY
# define my_safemalloc(s) safemalloc(s)
-# define my_safefree(s) free(s)
+# define my_safefree(s) safefree(s)
#else
STATIC void*
my_safemalloc(MEM_SIZE size)