From: Gurusamy Sarathy Date: Mon, 8 Feb 1999 10:34:55 +0000 (+0000) Subject: make EMBEDMYMALLOC the default and provide PERL_POLLUTE_MALLOC to let X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=86058a2d0cb92466b4e8a316b21562a79c7559b9;p=p5sagit%2Fp5-mst-13.2.git make EMBEDMYMALLOC the default and provide PERL_POLLUTE_MALLOC to let them ask for insanity (untested) p4raw-id: //depot/perl@2832 --- diff --git a/INSTALL b/INSTALL index de89e2d..98e7c62 100644 --- a/INSTALL +++ b/INSTALL @@ -747,6 +747,11 @@ To build without perl's malloc, you can use the Configure command 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 @@ -767,6 +772,18 @@ in Perl 5.004. 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 @@ -1011,11 +1028,6 @@ The latter is especially useful if you see something like this 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 diff --git a/Todo-5.005 b/Todo-5.005 index 58959fa..a8831b1 100644 --- a/Todo-5.005 +++ b/Todo-5.005 @@ -17,7 +17,6 @@ Compiler 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 diff --git a/ext/SDBM_File/sdbm/sdbm.h b/ext/SDBM_File/sdbm/sdbm.h index 84d5f75..4921de7 100644 --- a/ext/SDBM_File/sdbm/sdbm.h +++ b/ext/SDBM_File/sdbm/sdbm.h @@ -168,27 +168,19 @@ extern long sdbm_hash proto((char *, int)); /* This comes after 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 diff --git a/hints/machten.sh b/hints/machten.sh index f283873..7672837 100644 --- a/hints/machten.sh +++ b/hints/machten.sh @@ -64,13 +64,6 @@ usemymalloc=${usemymalloc:-y} # 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. diff --git a/hints/next_3.sh b/hints/next_3.sh index 99adf50..1a174b8 100644 --- a/hints/next_3.sh +++ b/hints/next_3.sh @@ -32,8 +32,8 @@ # 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 @@ -42,7 +42,7 @@ # # 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. diff --git a/hints/next_4.sh b/hints/next_4.sh index d1d0398..8bc623a 100644 --- a/hints/next_4.sh +++ b/hints/next_4.sh @@ -18,7 +18,7 @@ libc='/NextLibrary/Frameworks/System.framework/System' 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' diff --git a/hints/qnx.sh b/hints/qnx.sh index b53a33d..06d9010 100644 --- a/hints/qnx.sh +++ b/hints/qnx.sh @@ -98,13 +98,9 @@ libc='/usr/lib/clib3r.lib' # 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: diff --git a/iperlsys.h b/iperlsys.h index cfdc39f..97f30e3 100644 --- a/iperlsys.h +++ b/iperlsys.h @@ -610,9 +610,15 @@ public: #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 */ diff --git a/malloc.c b/malloc.c index e8fe41e..fd3b05b 100644 --- a/malloc.c +++ b/malloc.c @@ -827,7 +827,7 @@ botch(char *diag, char *s) #endif Malloc_t -malloc(register size_t nbytes) +Perl_malloc(register size_t nbytes) { register union overhead *p; register int bucket; @@ -1330,7 +1330,7 @@ morecore(register int bucket) } Free_t -free(void *mp) +Perl_mfree(void *mp) { register MEM_SIZE size; register union overhead *ovp; @@ -1412,7 +1412,7 @@ free(void *mp) #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; @@ -1431,7 +1431,7 @@ realloc(void *mp, size_t nbytes) BARK_64K_LIMIT("Reallocation",nbytes,size); if (!cp) - return malloc(nbytes); + return Perl_malloc(nbytes); MALLOC_LOCK; ovp = (union overhead *)((caddr_t)cp @@ -1568,12 +1568,12 @@ realloc(void *mp, size_t nbytes) "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 in order to get these definitions. +=item C + +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 constitute Perl's memory allocation API. +See L for further information about that. + =item C and C Issues The C global is now thread local, so a C declaration is needed diff --git a/pod/perlguts.pod b/pod/perlguts.pod index 15ca4b7..551e84c 100644 --- a/pod/perlguts.pod +++ b/pod/perlguts.pod @@ -1236,7 +1236,12 @@ consult L. =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. diff --git a/sv.c b/sv.c index 2dea64f..be8870f 100644 --- a/sv.c +++ b/sv.c @@ -603,7 +603,7 @@ more_xpv(void) #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)