Skip to content

Commit 2f67130

Browse files
committed
malloc.c: fix compilation error, clean up more
When configured with -DEBUGGING -Dusethreads -Dusemymalloc, malloc.c didn't compile anymore (see below for the full error message). This was probably broken by commits 8e3a36a and 13e5ba4. What they missed was that all uses of warn/croak in this file were supposed to have no context parameter (aTHX/pTHX) and the only reason croak2 was introduced was that C89 didn't have variable-argument macros, so croak() was hardcoded with one argument and croak2() with two arguments. Instead, we can simply replace all uses of croak/croak2 with Perl_croak_nocontext. On threaded builds, this is a function with no extra pTHX_ parameter; on non-threaded builds, this just turns into regular croak. Now we don't need to manually check for MULTIPLICITY ourselves. Back in the day (perl 5.005), malloc.c was supposed to be usable outside of perl, so it had checks for PERL_CORE being defined. These were removed in b9e5552, but some vestiges remained: PERL_CORE was still mentioned as a configuration option in a comment and Perl_mfree (but none of the other functions) tagged its warning messages with the configuration options in play ("PERL_CORE", "RMAGIC", or "RMAGIC, PERL_CORE", added in commit 52c6645). With the removal of non-PERL_CORE support, these don't make much sense anymore, so I removed them. This change in turn enables further improvements: Common code in the RCHECK and non-RCHECK branches can be extracted, making the "#ifdef RCHECK" conditional sections smaller and eliminating the "#else" sections entirely. ------------------------------------------------------------------------ In file included from malloc.c:240: malloc.c: In function ‘Perl_malloc’: perl.h:225:17: error: ‘my_perl’ undeclared (first use in this function) 225 | # define aTHX my_perl | ^~~~~~~ perl.h:230:25: note: in expansion of macro ‘aTHX’ 230 | # define aTHX_ aTHX, | ^~~~ embed.h:966:60: note: in expansion of macro ‘aTHX_’ 966 | # define croak(...) Perl_croak(aTHX_ __VA_ARGS__) | ^~~~~ malloc.c:1254:13: note: in expansion of macro ‘croak’ 1254 | croak("panic: malloc"); | ^~~~~ perl.h:225:17: note: each undeclared identifier is reported only once for each function it appears in 225 | # define aTHX my_perl | ^~~~~~~ perl.h:230:25: note: in expansion of macro ‘aTHX’ 230 | # define aTHX_ aTHX, | ^~~~ embed.h:966:60: note: in expansion of macro ‘aTHX_’ 966 | # define croak(...) Perl_croak(aTHX_ __VA_ARGS__) | ^~~~~ malloc.c:1254:13: note: in expansion of macro ‘croak’ 1254 | croak("panic: malloc"); | ^~~~~ malloc.c: In function ‘Perl_mfree’: perl.h:225:17: error: ‘my_perl’ undeclared (first use in this function) 225 | # define aTHX my_perl | ^~~~~~~ perl.h:230:25: note: in expansion of macro ‘aTHX’ 230 | # define aTHX_ aTHX, | ^~~~ embed.h:966:60: note: in expansion of macro ‘aTHX_’ 966 | # define croak(...) Perl_croak(aTHX_ __VA_ARGS__) | ^~~~~ malloc.c:1820:13: note: in expansion of macro ‘croak’ 1820 | croak("wrong alignment in free()"); | ^~~~~ malloc.c: In function ‘Perl_realloc’: perl.h:225:17: error: ‘my_perl’ undeclared (first use in this function) 225 | # define aTHX my_perl | ^~~~~~~ perl.h:230:25: note: in expansion of macro ‘aTHX’ 230 | # define aTHX_ aTHX, | ^~~~ embed.h:966:60: note: in expansion of macro ‘aTHX_’ 966 | # define croak(...) Perl_croak(aTHX_ __VA_ARGS__) | ^~~~~ malloc.c:1925:13: note: in expansion of macro ‘croak’ 1925 | croak("panic: realloc"); | ^~~~~ make: *** [makefile:265: malloc.o] Error 1
1 parent b6f991f commit 2f67130

File tree

2 files changed

+23
-42
lines changed

2 files changed

+23
-42
lines changed

malloc.c

+21-40
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,7 @@
2525
returns it to the state as of Perl 5.000.
2626
2727
Note that some of the settings below may be ignored in the code based
28-
on values of other macros. The PERL_CORE symbol is only defined when
29-
perl itself is being compiled (so malloc can make some assumptions
30-
about perl's facilities being available to it).
28+
on values of other macros.
3129
3230
Each config option has a short description, followed by its name,
3331
default value, and a comment about the default (if applicable). Some
@@ -238,11 +236,6 @@
238236
#include "EXTERN.h"
239237
#define PERL_IN_MALLOC_C
240238
#include "perl.h"
241-
#if defined(MULTIPLICITY)
242-
# define croak2 Perl_croak_nocontext
243-
#else
244-
# define croak2 croak
245-
#endif
246239
#ifdef USE_ITHREADS
247240
# define PERL_MAYBE_ALIVE PL_thr_key
248241
#else
@@ -931,7 +924,7 @@ static char *emergency_buffer_prepared;
931924
# endif
932925

933926
# ifndef emergency_sbrk_croak
934-
# define emergency_sbrk_croak croak2
927+
# define emergency_sbrk_croak Perl_croak_nocontext
935928
# endif
936929

937930
static char *
@@ -1251,7 +1244,7 @@ Perl_malloc(size_t nbytes)
12511244
BARK_64K_LIMIT("Allocation",nbytes,nbytes);
12521245
#ifdef DEBUGGING
12531246
if ((long)nbytes < 0)
1254-
croak("panic: malloc");
1247+
Perl_croak_nocontext("panic: malloc");
12551248
#endif
12561249

12571250
bucket = adjust_size_and_find_bucket(&nbytes);
@@ -1704,7 +1697,7 @@ morecore(int bucket)
17041697
#endif
17051698
if (bucket == sizeof(MEM_SIZE)*8*BUCKETS_PER_POW2) {
17061699
MALLOC_UNLOCK;
1707-
croak2("Out of memory during ridiculously large request");
1700+
Perl_croak_nocontext("Out of memory during ridiculously large request");
17081701
}
17091702
if (bucket > max_bucket)
17101703
max_bucket = bucket;
@@ -1817,7 +1810,7 @@ Perl_mfree(Malloc_t where)
18171810
return;
18181811
#ifdef DEBUGGING
18191812
if (PTR2UV(cp) & (MEM_ALIGNBYTES - 1))
1820-
croak("wrong alignment in free()");
1813+
Perl_croak_nocontext("wrong alignment in free()");
18211814
#endif
18221815
ovp = (union overhead *)((caddr_t)cp
18231816
- sizeof (union overhead) * CHUNK_SHIFT);
@@ -1839,26 +1832,19 @@ Perl_mfree(Malloc_t where)
18391832
}
18401833
if (!bad_free_warn)
18411834
return;
1842-
#ifdef RCHECK
18431835
{
18441836
dTHX;
18451837
if (!PERL_IS_ALIVE || !PL_curcop) {
1846-
if (ovp->ov_rmagic == RMAGIC - 1)
1847-
Perl_ck_warner_d(aTHX_ packWARN(WARN_MALLOC),
1848-
"Duplicate free() ignored (%s)", "RMAGIC, PERL_CORE");
1849-
else
1850-
Perl_ck_warner_d(aTHX_ packWARN(WARN_MALLOC),
1851-
"Bad free() ignored (%s)", "RMAGIC, PERL_CORE");
1838+
#ifdef RCHECK
1839+
if (ovp->ov_rmagic == RMAGIC - 1) {
1840+
Perl_ck_warner_d(aTHX_ packWARN(WARN_MALLOC), "Duplicate free() ignored");
1841+
return;
1842+
}
1843+
#endif
1844+
Perl_ck_warner_d(aTHX_ packWARN(WARN_MALLOC), "Bad free() ignored");
18521845
}
18531846
}
1854-
#else
1855-
{
1856-
dTHX;
1857-
if (!PERL_IS_ALIVE || !PL_curcop)
1858-
Perl_ck_warner_d(aTHX_ packWARN(WARN_MALLOC), "Bad free() ignored (%s)", "PERL_CORE");
1859-
}
1860-
#endif
1861-
return; /* sanity */
1847+
return; /* sanity */
18621848
}
18631849
#ifdef RCHECK
18641850
ASSERT(ovp->ov_rmagic == RMAGIC, "chunk's head overwrite");
@@ -1922,7 +1908,7 @@ Perl_realloc(void *mp, size_t nbytes)
19221908
MEM_SIZE size = nbytes;
19231909

19241910
if ((long)nbytes < 0)
1925-
croak("panic: realloc");
1911+
Perl_croak_nocontext("panic: realloc");
19261912
#endif
19271913

19281914
BARK_64K_LIMIT("Reallocation",nbytes,size);
@@ -1948,24 +1934,19 @@ Perl_realloc(void *mp, size_t nbytes)
19481934
}
19491935
if (!bad_free_warn)
19501936
return NULL;
1951-
#ifdef RCHECK
19521937
{
19531938
dTHX;
19541939
if (!PERL_IS_ALIVE || !PL_curcop) {
1955-
if (ovp->ov_rmagic == RMAGIC - 1)
1940+
#ifdef RCHECK
1941+
if (ovp->ov_rmagic == RMAGIC - 1) {
19561942
Perl_ck_warner_d(aTHX_ packWARN(WARN_MALLOC), "realloc() of freed memory ignored");
1957-
else
1958-
Perl_ck_warner_d(aTHX_ packWARN(WARN_MALLOC), "Bad realloc() ignored");
1959-
}
1960-
}
1961-
#else
1962-
{
1963-
dTHX;
1964-
if (!PERL_IS_ALIVE || !PL_curcop)
1943+
return NULL;
1944+
}
1945+
#endif
19651946
Perl_ck_warner_d(aTHX_ packWARN(WARN_MALLOC), "Bad realloc() ignored");
1947+
}
19661948
}
1967-
#endif
1968-
return NULL; /* sanity */
1949+
return NULL; /* sanity */
19691950
}
19701951

19711952
onb = BUCKET_SIZE_REAL(bucket);

pod/perldiag.pod

+2-2
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@ most likely an unexpected right brace '}'.
476476
symbol has no filehandle associated with it. Perhaps you didn't do an
477477
open(), or did it in another package.
478478

479-
=item Bad free() ignored (%s)
479+
=item Bad free() ignored
480480

481481
(S malloc) An internal routine called free() on something that had never
482482
been malloc()ed in the first place. Mandatory, but can be disabled by
@@ -2324,7 +2324,7 @@ See L<perlfunc/dump>.
23242324

23252325
(F) Your machine doesn't support dump/undump.
23262326

2327-
=item Duplicate free() ignored (%s)
2327+
=item Duplicate free() ignored
23282328

23292329
(S malloc) An internal routine called free() on something that had
23302330
already been freed.

0 commit comments

Comments
 (0)