This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Distinguish C- and perly- literals - PERLY_TILDE
[perl5.git] / malloc.c
index 72cf2cd..01e84bf 100644 (file)
--- a/malloc.c
+++ b/malloc.c
@@ -596,7 +596,7 @@ static const u_short buck_size[MAX_BUCKET_BY_TABLE + 1] =
 #  define MAX_PACKED_POW2 6
 #  define MAX_PACKED (MAX_PACKED_POW2 * BUCKETS_PER_POW2 + BUCKET_POW2_SHIFT)
 #  define MAX_POW2_ALGO ((1<<(MAX_PACKED_POW2 + 1)) - M_OVERHEAD)
-#  define TWOK_MASK ((1<<LOG_OF_MIN_ARENA) - 1)
+#  define TWOK_MASK nBIT_MASK(LOG_OF_MIN_ARENA)
 #  define TWOK_MASKED(x) (PTR2UV(x) & ~TWOK_MASK)
 #  define TWOK_SHIFT(x) (PTR2UV(x) & TWOK_MASK)
 #  define OV_INDEXp(block) (INT2PTR(u_char*,TWOK_MASKED(block)))
@@ -618,7 +618,7 @@ static const u_short buck_size[MAX_BUCKET_BY_TABLE + 1] =
 #ifdef IGNORE_SMALL_BAD_FREE
 #define FIRST_BUCKET_WITH_CHECK (6 * BUCKETS_PER_POW2) /* 64 */
 #  define N_BLKS(bucket) ( (bucket) < FIRST_BUCKET_WITH_CHECK          \
-                        ? ((1<<LOG_OF_MIN_ARENA) - 1)/BUCKET_SIZE_NO_SURPLUS(bucket) \
+                        ? nBIT_MASK(LOG_OF_MIN_ARENA)/BUCKET_SIZE_NO_SURPLUS(bucket) \
                         : n_blks[bucket] )
 #else
 #  define N_BLKS(bucket) n_blks[bucket]
@@ -1064,7 +1064,6 @@ emergency_sbrk(MEM_SIZE size)
 static void
 botch(const char *diag, const char *s, const char *file, int line)
 {
-    dVAR;
     dTHX;
     if (!(PERL_MAYBE_ALIVE && PERL_GET_THX))
        goto do_write;
@@ -1223,12 +1222,23 @@ S_adjust_size_and_find_bucket(size_t *nbytes_p)
        return bucket;
 }
 
+/*
+These have the same interfaces as the C lib ones, so are considered documented
+
+=for apidoc malloc
+=for apidoc calloc
+=for apidoc realloc
+=cut
+*/
+
 Malloc_t
 Perl_malloc(size_t nbytes)
 {
-        dVAR;
        union overhead *p;
        int bucket;
+#if defined(DEBUGGING) || defined(RCHECK)
+       MEM_SIZE size = nbytes;
+#endif
 
         /* A structure that has more than PTRDIFF_MAX bytes is unfortunately
          * legal in C, but in such, if two elements are far enough apart, we
@@ -1236,16 +1246,13 @@ Perl_malloc(size_t nbytes)
          * malloc so that pointer subtraction in the same structure is always
          * well defined */
         if (nbytes > PTRDIFF_MAX) {
+            dTHX;
             MYMALLOC_WRITE2STDERR("Memory requests are limited to PTRDIFF_MAX"
                                   " bytes to prevent possible undefined"
                                   " behavior");
             return NULL;
         }
 
-#if defined(DEBUGGING) || defined(RCHECK)
-       MEM_SIZE size = nbytes;
-#endif
-
        BARK_64K_LIMIT("Allocation",nbytes,nbytes);
 #ifdef DEBUGGING
        if ((long)nbytes < 0)
@@ -1321,8 +1328,9 @@ Perl_malloc(size_t nbytes)
        MALLOC_UNLOCK;
 
        DEBUG_m(PerlIO_printf(Perl_debug_log,
-                             "0x% "UVxf ": (%05lu) malloc %ld bytes\n",
-                             PTR2UV((Malloc_t)(p + CHUNK_SHIFT)), (unsigned long)(PL_an++),
+                             "%p: (%05lu) malloc %ld bytes\n",
+                             (Malloc_t)(p + CHUNK_SHIFT),
+                              (unsigned long)(PL_an++),
                              (long)size));
 
        FILLCHECK_DEADBEEF((unsigned char*)(p + CHUNK_SHIFT),
@@ -1461,7 +1469,6 @@ get_from_bigger_buckets(int bucket, MEM_SIZE size)
 static union overhead *
 getpages(MEM_SIZE needed, int *nblksp, int bucket)
 {
-    dVAR;
     /* Need to do (possibly expensive) system call. Try to
        optimize it for rare calling. */
     MEM_SIZE require = needed - sbrked_remains;
@@ -1656,7 +1663,6 @@ getpages_adjacent(MEM_SIZE require)
 static void
 morecore(int bucket)
 {
-        dVAR;
        union overhead *ovp;
        int rnu;       /* 2^rnu bytes will be requested */
        int nblks;              /* become nblks blocks of the desired size */
@@ -1670,7 +1676,8 @@ morecore(int bucket)
            /* It's our first time.  Initialize ourselves */
            were_called = 1;    /* Avoid a loop */
            if (!MallocCfg[MallocCfg_skip_cfg_env]) {
-               char *s = getenv("PERL_MALLOC_OPT"), *t = s, *off;
+               char *s = getenv("PERL_MALLOC_OPT"), *t = s;
+                const char *off;
                const char *opts = PERL_MALLOC_OPT_CHARS;
                int changed = 0;
 
@@ -1679,7 +1686,7 @@ morecore(int bucket)
                    IV val = 0;
 
                    t += 2;
-                   while (*t <= '9' && *t >= '0')
+                   while (isDIGIT(*t))
                        val = 10*val + *t++ - '0';
                    if (!*t || *t == ';') {
                        if (MallocCfg[off - opts] != val)
@@ -1792,7 +1799,6 @@ morecore(int bucket)
 Free_t
 Perl_mfree(Malloc_t where)
 {
-        dVAR;
        MEM_SIZE size;
        union overhead *ovp;
        char *cp = (char*)where;
@@ -1888,7 +1894,6 @@ Perl_mfree(Malloc_t where)
 Malloc_t
 Perl_realloc(void *mp, size_t nbytes)
 {
-        dVAR;
        MEM_SIZE onb;
        union overhead *ovp;
        char *res;
@@ -2251,8 +2256,9 @@ Perl_dump_mstats(pTHX_ const char *s)
        for (i = MIN_EVEN_REPORT; i <= buffer.topbucket; i += BUCKETS_PER_POW2) {
                PerlIO_printf(Perl_error_log, 
                              ((i < 8*BUCKETS_PER_POW2 || i == 10*BUCKETS_PER_POW2)
-                              ? " %5"UVuf 
-                              : ((i < 12*BUCKETS_PER_POW2) ? " %3"UVuf : " %"UVuf)),
+                              ? " %5" UVuf
+                              : ((i < 12*BUCKETS_PER_POW2) ? " %3" UVuf
+                                                            : " %" UVuf)),
                              buffer.nfree[i]);
        }
 #ifdef BUCKETS_ROOT2
@@ -2270,8 +2276,8 @@ Perl_dump_mstats(pTHX_ const char *s)
        for (i = MIN_EVEN_REPORT; i <= buffer.topbucket; i += BUCKETS_PER_POW2) {
                PerlIO_printf(Perl_error_log, 
                              ((i < 8*BUCKETS_PER_POW2 || i == 10*BUCKETS_PER_POW2)
-                              ? " %5"IVdf
-                              : ((i < 12*BUCKETS_PER_POW2) ? " %3"IVdf : " %"IVdf)), 
+                              ? " %5" IVdf
+                              : ((i < 12*BUCKETS_PER_POW2) ? " %3" IVdf : " %" IVdf)),
                              buffer.ntotal[i] - buffer.nfree[i]);
        }
 #ifdef BUCKETS_ROOT2