src/stdlib/SDL_malloc.c
branchSDL-1.3
changeset 1662 782fd950bd46
parent 1465 8dfa9a6d69a5
child 1668 4da1ee79c9af
     1.1 --- a/src/stdlib/SDL_malloc.c	Sun May 21 17:27:13 2006 +0000
     1.2 +++ b/src/stdlib/SDL_malloc.c	Sun May 28 13:04:16 2006 +0000
     1.3 @@ -477,8 +477,8 @@
     1.4  #ifndef WIN32
     1.5  #ifdef _WIN32
     1.6  #define WIN32 1
     1.7 -#endif  /* _WIN32 */
     1.8 -#endif  /* WIN32 */
     1.9 +#endif /* _WIN32 */
    1.10 +#endif /* WIN32 */
    1.11  #ifdef WIN32
    1.12  #define WIN32_LEAN_AND_MEAN
    1.13  #include <windows.h>
    1.14 @@ -491,123 +491,123 @@
    1.15  #define LACKS_STRINGS_H
    1.16  #define LACKS_SYS_TYPES_H
    1.17  #define LACKS_ERRNO_H
    1.18 -#define LACKS_FCNTL_H 
    1.19 +#define LACKS_FCNTL_H
    1.20  #define MALLOC_FAILURE_ACTION
    1.21 -#define MMAP_CLEARS 0 /* WINCE and some others apparently don't clear */
    1.22 -#endif  /* WIN32 */
    1.23 +#define MMAP_CLEARS 0           /* WINCE and some others apparently don't clear */
    1.24 +#endif /* WIN32 */
    1.25  
    1.26  #if defined(DARWIN) || defined(_DARWIN)
    1.27  /* Mac OSX docs advise not to use sbrk; it seems better to use mmap */
    1.28  #ifndef HAVE_MORECORE
    1.29  #define HAVE_MORECORE 0
    1.30  #define HAVE_MMAP 1
    1.31 -#endif  /* HAVE_MORECORE */
    1.32 -#endif  /* DARWIN */
    1.33 +#endif /* HAVE_MORECORE */
    1.34 +#endif /* DARWIN */
    1.35  
    1.36  #ifndef LACKS_SYS_TYPES_H
    1.37 -#include <sys/types.h>  /* For size_t */
    1.38 -#endif  /* LACKS_SYS_TYPES_H */
    1.39 +#include <sys/types.h>          /* For size_t */
    1.40 +#endif /* LACKS_SYS_TYPES_H */
    1.41  
    1.42  /* The maximum possible size_t value has all bits set */
    1.43  #define MAX_SIZE_T           (~(size_t)0)
    1.44  
    1.45  #ifndef ONLY_MSPACES
    1.46  #define ONLY_MSPACES 0
    1.47 -#endif  /* ONLY_MSPACES */
    1.48 +#endif /* ONLY_MSPACES */
    1.49  #ifndef MSPACES
    1.50  #if ONLY_MSPACES
    1.51  #define MSPACES 1
    1.52 -#else   /* ONLY_MSPACES */
    1.53 +#else /* ONLY_MSPACES */
    1.54  #define MSPACES 0
    1.55 -#endif  /* ONLY_MSPACES */
    1.56 -#endif  /* MSPACES */
    1.57 +#endif /* ONLY_MSPACES */
    1.58 +#endif /* MSPACES */
    1.59  #ifndef MALLOC_ALIGNMENT
    1.60  #define MALLOC_ALIGNMENT ((size_t)8U)
    1.61 -#endif  /* MALLOC_ALIGNMENT */
    1.62 +#endif /* MALLOC_ALIGNMENT */
    1.63  #ifndef FOOTERS
    1.64  #define FOOTERS 0
    1.65 -#endif  /* FOOTERS */
    1.66 +#endif /* FOOTERS */
    1.67  #ifndef ABORT
    1.68  #define ABORT  abort()
    1.69 -#endif  /* ABORT */
    1.70 +#endif /* ABORT */
    1.71  #ifndef ABORT_ON_ASSERT_FAILURE
    1.72  #define ABORT_ON_ASSERT_FAILURE 1
    1.73 -#endif  /* ABORT_ON_ASSERT_FAILURE */
    1.74 +#endif /* ABORT_ON_ASSERT_FAILURE */
    1.75  #ifndef PROCEED_ON_ERROR
    1.76  #define PROCEED_ON_ERROR 0
    1.77 -#endif  /* PROCEED_ON_ERROR */
    1.78 +#endif /* PROCEED_ON_ERROR */
    1.79  #ifndef USE_LOCKS
    1.80  #define USE_LOCKS 0
    1.81 -#endif  /* USE_LOCKS */
    1.82 +#endif /* USE_LOCKS */
    1.83  #ifndef INSECURE
    1.84  #define INSECURE 0
    1.85 -#endif  /* INSECURE */
    1.86 +#endif /* INSECURE */
    1.87  #ifndef HAVE_MMAP
    1.88  #define HAVE_MMAP 1
    1.89 -#endif  /* HAVE_MMAP */
    1.90 +#endif /* HAVE_MMAP */
    1.91  #ifndef MMAP_CLEARS
    1.92  #define MMAP_CLEARS 1
    1.93 -#endif  /* MMAP_CLEARS */
    1.94 +#endif /* MMAP_CLEARS */
    1.95  #ifndef HAVE_MREMAP
    1.96  #ifdef linux
    1.97  #define HAVE_MREMAP 1
    1.98 -#else   /* linux */
    1.99 +#else /* linux */
   1.100  #define HAVE_MREMAP 0
   1.101 -#endif  /* linux */
   1.102 -#endif  /* HAVE_MREMAP */
   1.103 +#endif /* linux */
   1.104 +#endif /* HAVE_MREMAP */
   1.105  #ifndef MALLOC_FAILURE_ACTION
   1.106  #define MALLOC_FAILURE_ACTION  errno = ENOMEM;
   1.107 -#endif  /* MALLOC_FAILURE_ACTION */
   1.108 +#endif /* MALLOC_FAILURE_ACTION */
   1.109  #ifndef HAVE_MORECORE
   1.110  #if ONLY_MSPACES
   1.111  #define HAVE_MORECORE 0
   1.112 -#else   /* ONLY_MSPACES */
   1.113 +#else /* ONLY_MSPACES */
   1.114  #define HAVE_MORECORE 1
   1.115 -#endif  /* ONLY_MSPACES */
   1.116 -#endif  /* HAVE_MORECORE */
   1.117 +#endif /* ONLY_MSPACES */
   1.118 +#endif /* HAVE_MORECORE */
   1.119  #if !HAVE_MORECORE
   1.120  #define MORECORE_CONTIGUOUS 0
   1.121 -#else   /* !HAVE_MORECORE */
   1.122 +#else /* !HAVE_MORECORE */
   1.123  #ifndef MORECORE
   1.124  #define MORECORE sbrk
   1.125 -#endif  /* MORECORE */
   1.126 +#endif /* MORECORE */
   1.127  #ifndef MORECORE_CONTIGUOUS
   1.128  #define MORECORE_CONTIGUOUS 1
   1.129 -#endif  /* MORECORE_CONTIGUOUS */
   1.130 -#endif  /* HAVE_MORECORE */
   1.131 +#endif /* MORECORE_CONTIGUOUS */
   1.132 +#endif /* HAVE_MORECORE */
   1.133  #ifndef DEFAULT_GRANULARITY
   1.134  #if MORECORE_CONTIGUOUS
   1.135 -#define DEFAULT_GRANULARITY (0)  /* 0 means to compute in init_mparams */
   1.136 -#else   /* MORECORE_CONTIGUOUS */
   1.137 +#define DEFAULT_GRANULARITY (0) /* 0 means to compute in init_mparams */
   1.138 +#else /* MORECORE_CONTIGUOUS */
   1.139  #define DEFAULT_GRANULARITY ((size_t)64U * (size_t)1024U)
   1.140 -#endif  /* MORECORE_CONTIGUOUS */
   1.141 -#endif  /* DEFAULT_GRANULARITY */
   1.142 +#endif /* MORECORE_CONTIGUOUS */
   1.143 +#endif /* DEFAULT_GRANULARITY */
   1.144  #ifndef DEFAULT_TRIM_THRESHOLD
   1.145  #ifndef MORECORE_CANNOT_TRIM
   1.146  #define DEFAULT_TRIM_THRESHOLD ((size_t)2U * (size_t)1024U * (size_t)1024U)
   1.147 -#else   /* MORECORE_CANNOT_TRIM */
   1.148 +#else /* MORECORE_CANNOT_TRIM */
   1.149  #define DEFAULT_TRIM_THRESHOLD MAX_SIZE_T
   1.150 -#endif  /* MORECORE_CANNOT_TRIM */
   1.151 -#endif  /* DEFAULT_TRIM_THRESHOLD */
   1.152 +#endif /* MORECORE_CANNOT_TRIM */
   1.153 +#endif /* DEFAULT_TRIM_THRESHOLD */
   1.154  #ifndef DEFAULT_MMAP_THRESHOLD
   1.155  #if HAVE_MMAP
   1.156  #define DEFAULT_MMAP_THRESHOLD ((size_t)256U * (size_t)1024U)
   1.157 -#else   /* HAVE_MMAP */
   1.158 +#else /* HAVE_MMAP */
   1.159  #define DEFAULT_MMAP_THRESHOLD MAX_SIZE_T
   1.160 -#endif  /* HAVE_MMAP */
   1.161 -#endif  /* DEFAULT_MMAP_THRESHOLD */
   1.162 +#endif /* HAVE_MMAP */
   1.163 +#endif /* DEFAULT_MMAP_THRESHOLD */
   1.164  #ifndef USE_BUILTIN_FFS
   1.165  #define USE_BUILTIN_FFS 0
   1.166 -#endif  /* USE_BUILTIN_FFS */
   1.167 +#endif /* USE_BUILTIN_FFS */
   1.168  #ifndef USE_DEV_RANDOM
   1.169  #define USE_DEV_RANDOM 0
   1.170 -#endif  /* USE_DEV_RANDOM */
   1.171 +#endif /* USE_DEV_RANDOM */
   1.172  #ifndef NO_MALLINFO
   1.173  #define NO_MALLINFO 0
   1.174 -#endif  /* NO_MALLINFO */
   1.175 +#endif /* NO_MALLINFO */
   1.176  #ifndef MALLINFO_FIELD_TYPE
   1.177  #define MALLINFO_FIELD_TYPE size_t
   1.178 -#endif  /* MALLINFO_FIELD_TYPE */
   1.179 +#endif /* MALLINFO_FIELD_TYPE */
   1.180  
   1.181  #define memset	SDL_memset
   1.182  #define memcpy	SDL_memcpy
   1.183 @@ -658,25 +658,27 @@
   1.184  #include "/usr/include/malloc.h"
   1.185  #else /* HAVE_USR_INCLUDE_MALLOC_H */
   1.186  
   1.187 -struct mallinfo {
   1.188 -  MALLINFO_FIELD_TYPE arena;    /* non-mmapped space allocated from system */
   1.189 -  MALLINFO_FIELD_TYPE ordblks;  /* number of free chunks */
   1.190 -  MALLINFO_FIELD_TYPE smblks;   /* always 0 */
   1.191 -  MALLINFO_FIELD_TYPE hblks;    /* always 0 */
   1.192 -  MALLINFO_FIELD_TYPE hblkhd;   /* space in mmapped regions */
   1.193 -  MALLINFO_FIELD_TYPE usmblks;  /* maximum total allocated space */
   1.194 -  MALLINFO_FIELD_TYPE fsmblks;  /* always 0 */
   1.195 -  MALLINFO_FIELD_TYPE uordblks; /* total allocated space */
   1.196 -  MALLINFO_FIELD_TYPE fordblks; /* total free space */
   1.197 -  MALLINFO_FIELD_TYPE keepcost; /* releasable (via malloc_trim) space */
   1.198 +struct mallinfo
   1.199 +{
   1.200 +    MALLINFO_FIELD_TYPE arena;  /* non-mmapped space allocated from system */
   1.201 +    MALLINFO_FIELD_TYPE ordblks;        /* number of free chunks */
   1.202 +    MALLINFO_FIELD_TYPE smblks; /* always 0 */
   1.203 +    MALLINFO_FIELD_TYPE hblks;  /* always 0 */
   1.204 +    MALLINFO_FIELD_TYPE hblkhd; /* space in mmapped regions */
   1.205 +    MALLINFO_FIELD_TYPE usmblks;        /* maximum total allocated space */
   1.206 +    MALLINFO_FIELD_TYPE fsmblks;        /* always 0 */
   1.207 +    MALLINFO_FIELD_TYPE uordblks;       /* total allocated space */
   1.208 +    MALLINFO_FIELD_TYPE fordblks;       /* total free space */
   1.209 +    MALLINFO_FIELD_TYPE keepcost;       /* releasable (via malloc_trim) space */
   1.210  };
   1.211  
   1.212  #endif /* HAVE_USR_INCLUDE_MALLOC_H */
   1.213  #endif /* NO_MALLINFO */
   1.214  
   1.215  #ifdef __cplusplus
   1.216 -extern "C" {
   1.217 -#endif /* __cplusplus */
   1.218 +extern "C"
   1.219 +{
   1.220 +#endif                          /* __cplusplus */
   1.221  
   1.222  #if !ONLY_MSPACES
   1.223  
   1.224 @@ -699,7 +701,7 @@
   1.225  #define dlmalloc_max_footprint malloc_max_footprint
   1.226  #define dlindependent_calloc   independent_calloc
   1.227  #define dlindependent_comalloc independent_comalloc
   1.228 -#endif /* USE_DL_PREFIX */
   1.229 +#endif                          /* USE_DL_PREFIX */
   1.230  
   1.231  
   1.232  /*
   1.233 @@ -716,7 +718,7 @@
   1.234    maximum supported value of n differs across systems, but is in all
   1.235    cases less than the maximum representable value of a size_t.
   1.236  */
   1.237 -void* dlmalloc(size_t);
   1.238 +    void *dlmalloc (size_t);
   1.239  
   1.240  /*
   1.241    free(void* p)
   1.242 @@ -725,14 +727,14 @@
   1.243    It has no effect if p is null. If p was not malloced or already
   1.244    freed, free(p) will by default cause the current program to abort.
   1.245  */
   1.246 -void  dlfree(void*);
   1.247 +    void dlfree (void *);
   1.248  
   1.249  /*
   1.250    calloc(size_t n_elements, size_t element_size);
   1.251    Returns a pointer to n_elements * element_size bytes, with all locations
   1.252    set to zero.
   1.253  */
   1.254 -void* dlcalloc(size_t, size_t);
   1.255 +    void *dlcalloc (size_t, size_t);
   1.256  
   1.257  /*
   1.258    realloc(void* p, size_t n)
   1.259 @@ -757,7 +759,7 @@
   1.260    to be used as an argument to realloc is not supported.
   1.261  */
   1.262  
   1.263 -void* dlrealloc(void*, size_t);
   1.264 +    void *dlrealloc (void *, size_t);
   1.265  
   1.266  /*
   1.267    memalign(size_t alignment, size_t n);
   1.268 @@ -771,14 +773,14 @@
   1.269  
   1.270    Overreliance on memalign is a sure way to fragment space.
   1.271  */
   1.272 -void* dlmemalign(size_t, size_t);
   1.273 +    void *dlmemalign (size_t, size_t);
   1.274  
   1.275  /*
   1.276    valloc(size_t n);
   1.277    Equivalent to memalign(pagesize, n), where pagesize is the page
   1.278    size of the system. If the pagesize is unknown, 4096 is used.
   1.279  */
   1.280 -void* dlvalloc(size_t);
   1.281 +    void *dlvalloc (size_t);
   1.282  
   1.283  /*
   1.284    mallopt(int parameter_number, int parameter_value)
   1.285 @@ -798,7 +800,7 @@
   1.286    M_GRANULARITY        -2     page size   any power of 2 >= page size
   1.287    M_MMAP_THRESHOLD     -3      256*1024   any   (or 0 if no MMAP support)
   1.288  */
   1.289 -int dlmallopt(int, int);
   1.290 +    int dlmallopt (int, int);
   1.291  
   1.292  /*
   1.293    malloc_footprint();
   1.294 @@ -809,7 +811,7 @@
   1.295    Even if locks are otherwise defined, this function does not use them,
   1.296    so results might not be up to date.
   1.297  */
   1.298 -size_t dlmalloc_footprint(void);
   1.299 +    size_t dlmalloc_footprint (void);
   1.300  
   1.301  /*
   1.302    malloc_max_footprint();
   1.303 @@ -822,7 +824,7 @@
   1.304    otherwise defined, this function does not use them, so results might
   1.305    not be up to date.
   1.306  */
   1.307 -size_t dlmalloc_max_footprint(void);
   1.308 +    size_t dlmalloc_max_footprint (void);
   1.309  
   1.310  #if !NO_MALLINFO
   1.311  /*
   1.312 @@ -847,8 +849,8 @@
   1.313    be kept as longs, the reported values may wrap around zero and
   1.314    thus be inaccurate.
   1.315  */
   1.316 -struct mallinfo dlmallinfo(void);
   1.317 -#endif /* NO_MALLINFO */
   1.318 +    struct mallinfo dlmallinfo (void);
   1.319 +#endif                          /* NO_MALLINFO */
   1.320  
   1.321  /*
   1.322    independent_calloc(size_t n_elements, size_t element_size, void* chunks[]);
   1.323 @@ -902,7 +904,7 @@
   1.324      return first;
   1.325    }
   1.326  */
   1.327 -void** dlindependent_calloc(size_t, size_t, void**);
   1.328 +    void **dlindependent_calloc (size_t, size_t, void **);
   1.329  
   1.330  /*
   1.331    independent_comalloc(size_t n_elements, size_t sizes[], void* chunks[]);
   1.332 @@ -963,7 +965,7 @@
   1.333    since it cannot reuse existing noncontiguous small chunks that
   1.334    might be available for some of the elements.
   1.335  */
   1.336 -void** dlindependent_comalloc(size_t, size_t*, void**);
   1.337 +    void **dlindependent_comalloc (size_t, size_t *, void **);
   1.338  
   1.339  
   1.340  /*
   1.341 @@ -971,7 +973,7 @@
   1.342    Equivalent to valloc(minimum-page-that-holds(n)), that is,
   1.343    round up n to nearest pagesize.
   1.344   */
   1.345 -void*  dlpvalloc(size_t);
   1.346 +    void *dlpvalloc (size_t);
   1.347  
   1.348  /*
   1.349    malloc_trim(size_t pad);
   1.350 @@ -994,7 +996,7 @@
   1.351  
   1.352    Malloc_trim returns 1 if it actually released any memory, else 0.
   1.353  */
   1.354 -int  dlmalloc_trim(size_t);
   1.355 +    int dlmalloc_trim (size_t);
   1.356  
   1.357  /*
   1.358    malloc_usable_size(void* p);
   1.359 @@ -1010,7 +1012,7 @@
   1.360    p = malloc(n);
   1.361    assert(malloc_usable_size(p) >= 256);
   1.362  */
   1.363 -size_t dlmalloc_usable_size(void*);
   1.364 +    size_t dlmalloc_usable_size (void *);
   1.365  
   1.366  /*
   1.367    malloc_stats();
   1.368 @@ -1031,9 +1033,9 @@
   1.369    malloc_stats prints only the most commonly interesting statistics.
   1.370    More information can be obtained by calling mallinfo.
   1.371  */
   1.372 -void  dlmalloc_stats(void);
   1.373 -
   1.374 -#endif /* ONLY_MSPACES */
   1.375 +    void dlmalloc_stats (void);
   1.376 +
   1.377 +#endif                          /* ONLY_MSPACES */
   1.378  
   1.379  #if MSPACES
   1.380  
   1.381 @@ -1041,7 +1043,7 @@
   1.382    mspace is an opaque type representing an independent
   1.383    region of space that supports mspace_malloc, etc.
   1.384  */
   1.385 -typedef void* mspace;
   1.386 +    typedef void *mspace;
   1.387  
   1.388  /*
   1.389    create_mspace creates and returns a new independent space with the
   1.390 @@ -1054,7 +1056,7 @@
   1.391    compiling with a different DEFAULT_GRANULARITY or dynamically
   1.392    setting with mallopt(M_GRANULARITY, value).
   1.393  */
   1.394 -mspace create_mspace(size_t capacity, int locked);
   1.395 +    mspace create_mspace (size_t capacity, int locked);
   1.396  
   1.397  /*
   1.398    destroy_mspace destroys the given space, and attempts to return all
   1.399 @@ -1062,7 +1064,7 @@
   1.400    bytes freed. After destruction, the results of access to all memory
   1.401    used by the space become undefined.
   1.402  */
   1.403 -size_t destroy_mspace(mspace msp);
   1.404 +    size_t destroy_mspace (mspace msp);
   1.405  
   1.406  /*
   1.407    create_mspace_with_base uses the memory supplied as the initial base
   1.408 @@ -1073,13 +1075,13 @@
   1.409    Destroying this space will deallocate all additionally allocated
   1.410    space (if possible) but not the initial base.
   1.411  */
   1.412 -mspace create_mspace_with_base(void* base, size_t capacity, int locked);
   1.413 +    mspace create_mspace_with_base (void *base, size_t capacity, int locked);
   1.414  
   1.415  /*
   1.416    mspace_malloc behaves as malloc, but operates within
   1.417    the given space.
   1.418  */
   1.419 -void* mspace_malloc(mspace msp, size_t bytes);
   1.420 +    void *mspace_malloc (mspace msp, size_t bytes);
   1.421  
   1.422  /*
   1.423    mspace_free behaves as free, but operates within
   1.424 @@ -1089,7 +1091,7 @@
   1.425    free may be called instead of mspace_free because freed chunks from
   1.426    any space are handled by their originating spaces.
   1.427  */
   1.428 -void mspace_free(mspace msp, void* mem);
   1.429 +    void mspace_free (mspace msp, void *mem);
   1.430  
   1.431  /*
   1.432    mspace_realloc behaves as realloc, but operates within
   1.433 @@ -1100,45 +1102,45 @@
   1.434    realloced chunks from any space are handled by their originating
   1.435    spaces.
   1.436  */
   1.437 -void* mspace_realloc(mspace msp, void* mem, size_t newsize);
   1.438 +    void *mspace_realloc (mspace msp, void *mem, size_t newsize);
   1.439  
   1.440  /*
   1.441    mspace_calloc behaves as calloc, but operates within
   1.442    the given space.
   1.443  */
   1.444 -void* mspace_calloc(mspace msp, size_t n_elements, size_t elem_size);
   1.445 +    void *mspace_calloc (mspace msp, size_t n_elements, size_t elem_size);
   1.446  
   1.447  /*
   1.448    mspace_memalign behaves as memalign, but operates within
   1.449    the given space.
   1.450  */
   1.451 -void* mspace_memalign(mspace msp, size_t alignment, size_t bytes);
   1.452 +    void *mspace_memalign (mspace msp, size_t alignment, size_t bytes);
   1.453  
   1.454  /*
   1.455    mspace_independent_calloc behaves as independent_calloc, but
   1.456    operates within the given space.
   1.457  */
   1.458 -void** mspace_independent_calloc(mspace msp, size_t n_elements,
   1.459 -                                 size_t elem_size, void* chunks[]);
   1.460 +    void **mspace_independent_calloc (mspace msp, size_t n_elements,
   1.461 +                                      size_t elem_size, void *chunks[]);
   1.462  
   1.463  /*
   1.464    mspace_independent_comalloc behaves as independent_comalloc, but
   1.465    operates within the given space.
   1.466  */
   1.467 -void** mspace_independent_comalloc(mspace msp, size_t n_elements,
   1.468 -                                   size_t sizes[], void* chunks[]);
   1.469 +    void **mspace_independent_comalloc (mspace msp, size_t n_elements,
   1.470 +                                        size_t sizes[], void *chunks[]);
   1.471  
   1.472  /*
   1.473    mspace_footprint() returns the number of bytes obtained from the
   1.474    system for this space.
   1.475  */
   1.476 -size_t mspace_footprint(mspace msp);
   1.477 +    size_t mspace_footprint (mspace msp);
   1.478  
   1.479  /*
   1.480    mspace_max_footprint() returns the peak number of bytes obtained from the
   1.481    system for this space.
   1.482  */
   1.483 -size_t mspace_max_footprint(mspace msp);
   1.484 +    size_t mspace_max_footprint (mspace msp);
   1.485  
   1.486  
   1.487  #if !NO_MALLINFO
   1.488 @@ -1146,30 +1148,30 @@
   1.489    mspace_mallinfo behaves as mallinfo, but reports properties of
   1.490    the given space.
   1.491  */
   1.492 -struct mallinfo mspace_mallinfo(mspace msp);
   1.493 -#endif /* NO_MALLINFO */
   1.494 +    struct mallinfo mspace_mallinfo (mspace msp);
   1.495 +#endif                          /* NO_MALLINFO */
   1.496  
   1.497  /*
   1.498    mspace_malloc_stats behaves as malloc_stats, but reports
   1.499    properties of the given space.
   1.500  */
   1.501 -void mspace_malloc_stats(mspace msp);
   1.502 +    void mspace_malloc_stats (mspace msp);
   1.503  
   1.504  /*
   1.505    mspace_trim behaves as malloc_trim, but
   1.506    operates within the given space.
   1.507  */
   1.508 -int mspace_trim(mspace msp, size_t pad);
   1.509 +    int mspace_trim (mspace msp, size_t pad);
   1.510  
   1.511  /*
   1.512    An alias for mallopt.
   1.513  */
   1.514 -int mspace_mallopt(int, int);
   1.515 -
   1.516 -#endif /* MSPACES */
   1.517 +    int mspace_mallopt (int, int);
   1.518 +
   1.519 +#endif                          /* MSPACES */
   1.520  
   1.521  #ifdef __cplusplus
   1.522 -};  /* end of extern "C" */
   1.523 +};                              /* end of extern "C" */
   1.524  #endif /* __cplusplus */
   1.525  
   1.526  /*
   1.527 @@ -1185,21 +1187,21 @@
   1.528  /*------------------------------ internal #includes ---------------------- */
   1.529  
   1.530  #ifdef _MSC_VER
   1.531 -#pragma warning( disable : 4146 ) /* no "unsigned" warnings */
   1.532 +#pragma warning( disable : 4146 )       /* no "unsigned" warnings */
   1.533  #endif /* _MSC_VER */
   1.534  
   1.535  #ifndef LACKS_STDIO_H
   1.536 -#include <stdio.h>       /* for printing in malloc_stats */
   1.537 +#include <stdio.h>              /* for printing in malloc_stats */
   1.538  #endif
   1.539  
   1.540  #ifndef LACKS_ERRNO_H
   1.541 -#include <errno.h>       /* for MALLOC_FAILURE_ACTION */
   1.542 +#include <errno.h>              /* for MALLOC_FAILURE_ACTION */
   1.543  #endif /* LACKS_ERRNO_H */
   1.544  #if FOOTERS
   1.545 -#include <time.h>        /* for magic initialization */
   1.546 +#include <time.h>               /* for magic initialization */
   1.547  #endif /* FOOTERS */
   1.548  #ifndef LACKS_STDLIB_H
   1.549 -#include <stdlib.h>      /* for abort() */
   1.550 +#include <stdlib.h>             /* for abort() */
   1.551  #endif /* LACKS_STDLIB_H */
   1.552  #ifdef DEBUG
   1.553  #if ABORT_ON_ASSERT_FAILURE
   1.554 @@ -1207,20 +1209,20 @@
   1.555  #else /* ABORT_ON_ASSERT_FAILURE */
   1.556  #include <assert.h>
   1.557  #endif /* ABORT_ON_ASSERT_FAILURE */
   1.558 -#else  /* DEBUG */
   1.559 +#else /* DEBUG */
   1.560  #define assert(x)
   1.561  #endif /* DEBUG */
   1.562  #ifndef LACKS_STRING_H
   1.563 -#include <string.h>      /* for memset etc */
   1.564 -#endif  /* LACKS_STRING_H */
   1.565 +#include <string.h>             /* for memset etc */
   1.566 +#endif /* LACKS_STRING_H */
   1.567  #if USE_BUILTIN_FFS
   1.568  #ifndef LACKS_STRINGS_H
   1.569 -#include <strings.h>     /* for ffs */
   1.570 +#include <strings.h>            /* for ffs */
   1.571  #endif /* LACKS_STRINGS_H */
   1.572  #endif /* USE_BUILTIN_FFS */
   1.573  #if HAVE_MMAP
   1.574  #ifndef LACKS_SYS_MMAN_H
   1.575 -#include <sys/mman.h>    /* for mmap */
   1.576 +#include <sys/mman.h>           /* for mmap */
   1.577  #endif /* LACKS_SYS_MMAN_H */
   1.578  #ifndef LACKS_FCNTL_H
   1.579  #include <fcntl.h>
   1.580 @@ -1228,17 +1230,17 @@
   1.581  #endif /* HAVE_MMAP */
   1.582  #if HAVE_MORECORE
   1.583  #ifndef LACKS_UNISTD_H
   1.584 -#include <unistd.h>     /* for sbrk */
   1.585 +#include <unistd.h>             /* for sbrk */
   1.586  #else /* LACKS_UNISTD_H */
   1.587  #if !defined(__FreeBSD__) && !defined(__OpenBSD__) && !defined(__NetBSD__)
   1.588 -extern void*     sbrk(ptrdiff_t);
   1.589 +extern void *sbrk (ptrdiff_t);
   1.590  #endif /* FreeBSD etc */
   1.591  #endif /* LACKS_UNISTD_H */
   1.592  #endif /* HAVE_MMAP */
   1.593  
   1.594  #ifndef WIN32
   1.595  #ifndef malloc_getpagesize
   1.596 -#  ifdef _SC_PAGESIZE         /* some SVR4 systems omit an underscore */
   1.597 +#  ifdef _SC_PAGESIZE           /* some SVR4 systems omit an underscore */
   1.598  #    ifndef _SC_PAGE_SIZE
   1.599  #      define _SC_PAGE_SIZE _SC_PAGESIZE
   1.600  #    endif
   1.601 @@ -1247,10 +1249,10 @@
   1.602  #    define malloc_getpagesize sysconf(_SC_PAGE_SIZE)
   1.603  #  else
   1.604  #    if defined(BSD) || defined(DGUX) || defined(HAVE_GETPAGESIZE)
   1.605 -       extern size_t getpagesize();
   1.606 +extern size_t getpagesize ();
   1.607  #      define malloc_getpagesize getpagesize()
   1.608  #    else
   1.609 -#      ifdef WIN32 /* use supplied emulation of getpagesize */
   1.610 +#      ifdef WIN32              /* use supplied emulation of getpagesize */
   1.611  #        define malloc_getpagesize getpagesize()
   1.612  #      else
   1.613  #        ifndef LACKS_SYS_PARAM_H
   1.614 @@ -1321,7 +1323,7 @@
   1.615  
   1.616  /* MORECORE and MMAP must return MFAIL on failure */
   1.617  #define MFAIL                ((void*)(MAX_SIZE_T))
   1.618 -#define CMFAIL               ((char*)(MFAIL)) /* defined for convenience */
   1.619 +#define CMFAIL               ((char*)(MFAIL))   /* defined for convenience */
   1.620  
   1.621  #if !HAVE_MMAP
   1.622  #define IS_MMAPPED_BIT       (SIZE_T_ZERO)
   1.623 @@ -1349,7 +1351,7 @@
   1.624     is unlikely to be needed, but is supplied just in case.
   1.625  */
   1.626  #define MMAP_FLAGS           (MAP_PRIVATE)
   1.627 -static int dev_zero_fd = -1; /* Cached file descriptor for /dev/zero. */
   1.628 +static int dev_zero_fd = -1;    /* Cached file descriptor for /dev/zero. */
   1.629  #define CALL_MMAP(s) ((dev_zero_fd < 0) ? \
   1.630             (dev_zero_fd = open("/dev/zero", O_RDWR), \
   1.631              mmap(0, (s), MMAP_PROT, MMAP_FLAGS, dev_zero_fd, 0)) : \
   1.632 @@ -1360,34 +1362,42 @@
   1.633  #else /* WIN32 */
   1.634  
   1.635  /* Win32 MMAP via VirtualAlloc */
   1.636 -static void* win32mmap(size_t size) {
   1.637 -  void* ptr = VirtualAlloc(0, size, MEM_RESERVE|MEM_COMMIT, PAGE_READWRITE);
   1.638 -  return (ptr != 0)? ptr: MFAIL;
   1.639 +static void *
   1.640 +win32mmap (size_t size)
   1.641 +{
   1.642 +    void *ptr =
   1.643 +        VirtualAlloc (0, size, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
   1.644 +    return (ptr != 0) ? ptr : MFAIL;
   1.645  }
   1.646  
   1.647  /* For direct MMAP, use MEM_TOP_DOWN to minimize interference */
   1.648 -static void* win32direct_mmap(size_t size) {
   1.649 -  void* ptr = VirtualAlloc(0, size, MEM_RESERVE|MEM_COMMIT|MEM_TOP_DOWN,
   1.650 -                           PAGE_READWRITE);
   1.651 -  return (ptr != 0)? ptr: MFAIL;
   1.652 +static void *
   1.653 +win32direct_mmap (size_t size)
   1.654 +{
   1.655 +    void *ptr =
   1.656 +        VirtualAlloc (0, size, MEM_RESERVE | MEM_COMMIT | MEM_TOP_DOWN,
   1.657 +                      PAGE_READWRITE);
   1.658 +    return (ptr != 0) ? ptr : MFAIL;
   1.659  }
   1.660  
   1.661  /* This function supports releasing coalesed segments */
   1.662 -static int win32munmap(void* ptr, size_t size) {
   1.663 -  MEMORY_BASIC_INFORMATION minfo;
   1.664 -  char* cptr = ptr;
   1.665 -  while (size) {
   1.666 -    if (VirtualQuery(cptr, &minfo, sizeof(minfo)) == 0)
   1.667 -      return -1;
   1.668 -    if (minfo.BaseAddress != cptr || minfo.AllocationBase != cptr ||
   1.669 -        minfo.State != MEM_COMMIT || minfo.RegionSize > size)
   1.670 -      return -1;
   1.671 -    if (VirtualFree(cptr, 0, MEM_RELEASE) == 0)
   1.672 -      return -1;
   1.673 -    cptr += minfo.RegionSize;
   1.674 -    size -= minfo.RegionSize;
   1.675 -  }
   1.676 -  return 0;
   1.677 +static int
   1.678 +win32munmap (void *ptr, size_t size)
   1.679 +{
   1.680 +    MEMORY_BASIC_INFORMATION minfo;
   1.681 +    char *cptr = ptr;
   1.682 +    while (size) {
   1.683 +        if (VirtualQuery (cptr, &minfo, sizeof (minfo)) == 0)
   1.684 +            return -1;
   1.685 +        if (minfo.BaseAddress != cptr || minfo.AllocationBase != cptr ||
   1.686 +            minfo.State != MEM_COMMIT || minfo.RegionSize > size)
   1.687 +            return -1;
   1.688 +        if (VirtualFree (cptr, 0, MEM_RELEASE) == 0)
   1.689 +            return -1;
   1.690 +        cptr += minfo.RegionSize;
   1.691 +        size -= minfo.RegionSize;
   1.692 +    }
   1.693 +    return 0;
   1.694  }
   1.695  
   1.696  #define CALL_MMAP(s)         win32mmap(s)
   1.697 @@ -1398,13 +1408,13 @@
   1.698  
   1.699  #if HAVE_MMAP && HAVE_MREMAP
   1.700  #define CALL_MREMAP(addr, osz, nsz, mv) mremap((addr), (osz), (nsz), (mv))
   1.701 -#else  /* HAVE_MMAP && HAVE_MREMAP */
   1.702 +#else /* HAVE_MMAP && HAVE_MREMAP */
   1.703  #define CALL_MREMAP(addr, osz, nsz, mv) MFAIL
   1.704  #endif /* HAVE_MMAP && HAVE_MREMAP */
   1.705  
   1.706  #if HAVE_MORECORE
   1.707  #define CALL_MORECORE(S)     MORECORE(S)
   1.708 -#else  /* HAVE_MORECORE */
   1.709 +#else /* HAVE_MORECORE */
   1.710  #define CALL_MORECORE(S)     MFAIL
   1.711  #endif /* HAVE_MORECORE */
   1.712  
   1.713 @@ -1454,21 +1464,26 @@
   1.714  */
   1.715  
   1.716  #define MLOCK_T long
   1.717 -static int win32_acquire_lock (MLOCK_T *sl) {
   1.718 -  for (;;) {
   1.719 +static int
   1.720 +win32_acquire_lock (MLOCK_T * sl)
   1.721 +{
   1.722 +    for (;;) {
   1.723  #ifdef InterlockedCompareExchangePointer
   1.724 -    if (!InterlockedCompareExchange(sl, 1, 0))
   1.725 -      return 0;
   1.726 -#else  /* Use older void* version */
   1.727 -    if (!InterlockedCompareExchange((void**)sl, (void*)1, (void*)0))
   1.728 -      return 0;
   1.729 +        if (!InterlockedCompareExchange (sl, 1, 0))
   1.730 +            return 0;
   1.731 +#else /* Use older void* version */
   1.732 +        if (!InterlockedCompareExchange
   1.733 +            ((void **) sl, (void *) 1, (void *) 0))
   1.734 +            return 0;
   1.735  #endif /* InterlockedCompareExchangePointer */
   1.736 -    Sleep (0);
   1.737 -  }
   1.738 +        Sleep (0);
   1.739 +    }
   1.740  }
   1.741  
   1.742 -static void win32_release_lock (MLOCK_T *sl) {
   1.743 -  InterlockedExchange (sl, 0);
   1.744 +static void
   1.745 +win32_release_lock (MLOCK_T * sl)
   1.746 +{
   1.747 +    InterlockedExchange (sl, 0);
   1.748  }
   1.749  
   1.750  #define INITIAL_LOCK(l)      *(l)=0
   1.751 @@ -1481,7 +1496,7 @@
   1.752  #endif /* WIN32 */
   1.753  
   1.754  #define USE_LOCK_BIT               (2U)
   1.755 -#else  /* USE_LOCKS */
   1.756 +#else /* USE_LOCKS */
   1.757  #define USE_LOCK_BIT               (0U)
   1.758  #define INITIAL_LOCK(l)
   1.759  #endif /* USE_LOCKS */
   1.760 @@ -1497,7 +1512,7 @@
   1.761  #if USE_LOCKS
   1.762  #define ACQUIRE_MAGIC_INIT_LOCK()  ACQUIRE_LOCK(&magic_init_mutex);
   1.763  #define RELEASE_MAGIC_INIT_LOCK()  RELEASE_LOCK(&magic_init_mutex);
   1.764 -#else  /* USE_LOCKS */
   1.765 +#else /* USE_LOCKS */
   1.766  #define ACQUIRE_MAGIC_INIT_LOCK()
   1.767  #define RELEASE_MAGIC_INIT_LOCK()
   1.768  #endif /* USE_LOCKS */
   1.769 @@ -1640,19 +1655,20 @@
   1.770  
   1.771  */
   1.772  
   1.773 -struct malloc_chunk {
   1.774 -  size_t               prev_foot;  /* Size of previous chunk (if free).  */
   1.775 -  size_t               head;       /* Size and inuse bits. */
   1.776 -  struct malloc_chunk* fd;         /* double links -- used only if free. */
   1.777 -  struct malloc_chunk* bk;
   1.778 +struct malloc_chunk
   1.779 +{
   1.780 +    size_t prev_foot;           /* Size of previous chunk (if free).  */
   1.781 +    size_t head;                /* Size and inuse bits. */
   1.782 +    struct malloc_chunk *fd;    /* double links -- used only if free. */
   1.783 +    struct malloc_chunk *bk;
   1.784  };
   1.785  
   1.786 -typedef struct malloc_chunk  mchunk;
   1.787 -typedef struct malloc_chunk* mchunkptr;
   1.788 -typedef struct malloc_chunk* sbinptr;  /* The type of bins of chunks */
   1.789 -typedef size_t bindex_t;               /* Described below */
   1.790 -typedef unsigned int binmap_t;         /* Described below */
   1.791 -typedef unsigned int flag_t;           /* The type of various bit flag sets */
   1.792 +typedef struct malloc_chunk mchunk;
   1.793 +typedef struct malloc_chunk *mchunkptr;
   1.794 +typedef struct malloc_chunk *sbinptr;   /* The type of bins of chunks */
   1.795 +typedef size_t bindex_t;        /* Described below */
   1.796 +typedef unsigned int binmap_t;  /* Described below */
   1.797 +typedef unsigned int flag_t;    /* The type of various bit flag sets */
   1.798  
   1.799  /* ------------------- Chunks sizes and alignments ----------------------- */
   1.800  
   1.801 @@ -1845,21 +1861,22 @@
   1.802    is of course much better.
   1.803  */
   1.804  
   1.805 -struct malloc_tree_chunk {
   1.806 -  /* The first four fields must be compatible with malloc_chunk */
   1.807 -  size_t                    prev_foot;
   1.808 -  size_t                    head;
   1.809 -  struct malloc_tree_chunk* fd;
   1.810 -  struct malloc_tree_chunk* bk;
   1.811 -
   1.812 -  struct malloc_tree_chunk* child[2];
   1.813 -  struct malloc_tree_chunk* parent;
   1.814 -  bindex_t                  index;
   1.815 +struct malloc_tree_chunk
   1.816 +{
   1.817 +    /* The first four fields must be compatible with malloc_chunk */
   1.818 +    size_t prev_foot;
   1.819 +    size_t head;
   1.820 +    struct malloc_tree_chunk *fd;
   1.821 +    struct malloc_tree_chunk *bk;
   1.822 +
   1.823 +    struct malloc_tree_chunk *child[2];
   1.824 +    struct malloc_tree_chunk *parent;
   1.825 +    bindex_t index;
   1.826  };
   1.827  
   1.828 -typedef struct malloc_tree_chunk  tchunk;
   1.829 -typedef struct malloc_tree_chunk* tchunkptr;
   1.830 -typedef struct malloc_tree_chunk* tbinptr; /* The type of bins of trees */
   1.831 +typedef struct malloc_tree_chunk tchunk;
   1.832 +typedef struct malloc_tree_chunk *tchunkptr;
   1.833 +typedef struct malloc_tree_chunk *tbinptr;      /* The type of bins of trees */
   1.834  
   1.835  /* A little helper macro for trees */
   1.836  #define leftmost_child(t) ((t)->child[0] != 0? (t)->child[0] : (t)->child[1])
   1.837 @@ -1921,18 +1938,19 @@
   1.838      and deallocated/trimmed using MORECORE with negative arguments.
   1.839  */
   1.840  
   1.841 -struct malloc_segment {
   1.842 -  char*        base;             /* base address */
   1.843 -  size_t       size;             /* allocated size */
   1.844 -  struct malloc_segment* next;   /* ptr to next segment */
   1.845 -  flag_t       sflags;           /* mmap and extern flag */
   1.846 +struct malloc_segment
   1.847 +{
   1.848 +    char *base;                 /* base address */
   1.849 +    size_t size;                /* allocated size */
   1.850 +    struct malloc_segment *next;        /* ptr to next segment */
   1.851 +    flag_t sflags;              /* mmap and extern flag */
   1.852  };
   1.853  
   1.854  #define is_mmapped_segment(S)  ((S)->sflags & IS_MMAPPED_BIT)
   1.855  #define is_extern_segment(S)   ((S)->sflags & EXTERN_BIT)
   1.856  
   1.857 -typedef struct malloc_segment  msegment;
   1.858 -typedef struct malloc_segment* msegmentptr;
   1.859 +typedef struct malloc_segment msegment;
   1.860 +typedef struct malloc_segment *msegmentptr;
   1.861  
   1.862  /* ---------------------------- malloc_state ----------------------------- */
   1.863  
   1.864 @@ -2019,28 +2037,29 @@
   1.865  #define MAX_SMALL_SIZE    (MIN_LARGE_SIZE - SIZE_T_ONE)
   1.866  #define MAX_SMALL_REQUEST (MAX_SMALL_SIZE - CHUNK_ALIGN_MASK - CHUNK_OVERHEAD)
   1.867  
   1.868 -struct malloc_state {
   1.869 -  binmap_t   smallmap;
   1.870 -  binmap_t   treemap;
   1.871 -  size_t     dvsize;
   1.872 -  size_t     topsize;
   1.873 -  char*      least_addr;
   1.874 -  mchunkptr  dv;
   1.875 -  mchunkptr  top;
   1.876 -  size_t     trim_check;
   1.877 -  size_t     magic;
   1.878 -  mchunkptr  smallbins[(NSMALLBINS+1)*2];
   1.879 -  tbinptr    treebins[NTREEBINS];
   1.880 -  size_t     footprint;
   1.881 -  size_t     max_footprint;
   1.882 -  flag_t     mflags;
   1.883 +struct malloc_state
   1.884 +{
   1.885 +    binmap_t smallmap;
   1.886 +    binmap_t treemap;
   1.887 +    size_t dvsize;
   1.888 +    size_t topsize;
   1.889 +    char *least_addr;
   1.890 +    mchunkptr dv;
   1.891 +    mchunkptr top;
   1.892 +    size_t trim_check;
   1.893 +    size_t magic;
   1.894 +    mchunkptr smallbins[(NSMALLBINS + 1) * 2];
   1.895 +    tbinptr treebins[NTREEBINS];
   1.896 +    size_t footprint;
   1.897 +    size_t max_footprint;
   1.898 +    flag_t mflags;
   1.899  #if USE_LOCKS
   1.900 -  MLOCK_T    mutex;     /* locate lock among fields that rarely change */
   1.901 -#endif /* USE_LOCKS */
   1.902 -  msegment   seg;
   1.903 +    MLOCK_T mutex;              /* locate lock among fields that rarely change */
   1.904 +#endif                          /* USE_LOCKS */
   1.905 +    msegment seg;
   1.906  };
   1.907  
   1.908 -typedef struct malloc_state*    mstate;
   1.909 +typedef struct malloc_state *mstate;
   1.910  
   1.911  /* ------------- Global malloc_state and malloc_params ------------------- */
   1.912  
   1.913 @@ -2050,13 +2069,14 @@
   1.914    initialized in init_mparams.
   1.915  */
   1.916  
   1.917 -struct malloc_params {
   1.918 -  size_t magic;
   1.919 -  size_t page_size;
   1.920 -  size_t granularity;
   1.921 -  size_t mmap_threshold;
   1.922 -  size_t trim_threshold;
   1.923 -  flag_t default_mflags;
   1.924 +struct malloc_params
   1.925 +{
   1.926 +    size_t magic;
   1.927 +    size_t page_size;
   1.928 +    size_t granularity;
   1.929 +    size_t mmap_threshold;
   1.930 +    size_t trim_threshold;
   1.931 +    flag_t default_mflags;
   1.932  };
   1.933  
   1.934  static struct malloc_params mparams;
   1.935 @@ -2105,30 +2125,34 @@
   1.936    ((char*)(A) >= S->base && (char*)(A) < S->base + S->size)
   1.937  
   1.938  /* Return segment holding given address */
   1.939 -static msegmentptr segment_holding(mstate m, char* addr) {
   1.940 -  msegmentptr sp = &m->seg;
   1.941 -  for (;;) {
   1.942 -    if (addr >= sp->base && addr < sp->base + sp->size)
   1.943 -      return sp;
   1.944 -    if ((sp = sp->next) == 0)
   1.945 -      return 0;
   1.946 -  }
   1.947 +static msegmentptr
   1.948 +segment_holding (mstate m, char *addr)
   1.949 +{
   1.950 +    msegmentptr sp = &m->seg;
   1.951 +    for (;;) {
   1.952 +        if (addr >= sp->base && addr < sp->base + sp->size)
   1.953 +            return sp;
   1.954 +        if ((sp = sp->next) == 0)
   1.955 +            return 0;
   1.956 +    }
   1.957  }
   1.958  
   1.959  /* Return true if segment contains a segment link */
   1.960 -static int has_segment_link(mstate m, msegmentptr ss) {
   1.961 -  msegmentptr sp = &m->seg;
   1.962 -  for (;;) {
   1.963 -    if ((char*)sp >= ss->base && (char*)sp < ss->base + ss->size)
   1.964 -      return 1;
   1.965 -    if ((sp = sp->next) == 0)
   1.966 -      return 0;
   1.967 -  }
   1.968 +static int
   1.969 +has_segment_link (mstate m, msegmentptr ss)
   1.970 +{
   1.971 +    msegmentptr sp = &m->seg;
   1.972 +    for (;;) {
   1.973 +        if ((char *) sp >= ss->base && (char *) sp < ss->base + ss->size)
   1.974 +            return 1;
   1.975 +        if ((sp = sp->next) == 0)
   1.976 +            return 0;
   1.977 +    }
   1.978  }
   1.979  
   1.980  #ifndef MORECORE_CANNOT_TRIM
   1.981  #define should_trim(M,s)  ((s) > (M)->trim_check)
   1.982 -#else  /* MORECORE_CANNOT_TRIM */
   1.983 +#else /* MORECORE_CANNOT_TRIM */
   1.984  #define should_trim(M,s)  (0)
   1.985  #endif /* MORECORE_CANNOT_TRIM */
   1.986  
   1.987 @@ -2160,11 +2184,11 @@
   1.988  
   1.989  #ifndef PREACTION
   1.990  #define PREACTION(M) (0)
   1.991 -#endif  /* PREACTION */
   1.992 +#endif /* PREACTION */
   1.993  
   1.994  #ifndef POSTACTION
   1.995  #define POSTACTION(M)
   1.996 -#endif  /* POSTACTION */
   1.997 +#endif /* POSTACTION */
   1.998  
   1.999  #endif /* USE_LOCKS */
  1.1000  
  1.1001 @@ -2182,7 +2206,7 @@
  1.1002  int malloc_corruption_error_count;
  1.1003  
  1.1004  /* default corruption action */
  1.1005 -static void reset_on_error(mstate m);
  1.1006 +static void reset_on_error (mstate m);
  1.1007  
  1.1008  #define CORRUPTION_ERROR_ACTION(m)  reset_on_error(m)
  1.1009  #define USAGE_ERROR_ACTION(m, p)
  1.1010 @@ -2218,18 +2242,18 @@
  1.1011  #define check_mmapped_chunk(M,P)    do_check_mmapped_chunk(M,P)
  1.1012  #define check_malloc_state(M)       do_check_malloc_state(M)
  1.1013  
  1.1014 -static void   do_check_any_chunk(mstate m, mchunkptr p);
  1.1015 -static void   do_check_top_chunk(mstate m, mchunkptr p);
  1.1016 -static void   do_check_mmapped_chunk(mstate m, mchunkptr p);
  1.1017 -static void   do_check_inuse_chunk(mstate m, mchunkptr p);
  1.1018 -static void   do_check_free_chunk(mstate m, mchunkptr p);
  1.1019 -static void   do_check_malloced_chunk(mstate m, void* mem, size_t s);
  1.1020 -static void   do_check_tree(mstate m, tchunkptr t);
  1.1021 -static void   do_check_treebin(mstate m, bindex_t i);
  1.1022 -static void   do_check_smallbin(mstate m, bindex_t i);
  1.1023 -static void   do_check_malloc_state(mstate m);
  1.1024 -static int    bin_find(mstate m, mchunkptr x);
  1.1025 -static size_t traverse_and_check(mstate m);
  1.1026 +static void do_check_any_chunk (mstate m, mchunkptr p);
  1.1027 +static void do_check_top_chunk (mstate m, mchunkptr p);
  1.1028 +static void do_check_mmapped_chunk (mstate m, mchunkptr p);
  1.1029 +static void do_check_inuse_chunk (mstate m, mchunkptr p);
  1.1030 +static void do_check_free_chunk (mstate m, mchunkptr p);
  1.1031 +static void do_check_malloced_chunk (mstate m, void *mem, size_t s);
  1.1032 +static void do_check_tree (mstate m, tchunkptr t);
  1.1033 +static void do_check_treebin (mstate m, bindex_t i);
  1.1034 +static void do_check_smallbin (mstate m, bindex_t i);
  1.1035 +static void do_check_malloc_state (mstate m);
  1.1036 +static int bin_find (mstate m, mchunkptr x);
  1.1037 +static size_t traverse_and_check (mstate m);
  1.1038  #endif /* DEBUG */
  1.1039  
  1.1040  /* ---------------------------- Indexing Bins ---------------------------- */
  1.1041 @@ -2394,7 +2418,7 @@
  1.1042  #if (FOOTERS && !INSECURE)
  1.1043  /* Check if (alleged) mstate m has expected magic field */
  1.1044  #define ok_magic(M)      ((M)->magic == mparams.magic)
  1.1045 -#else  /* (FOOTERS && !INSECURE) */
  1.1046 +#else /* (FOOTERS && !INSECURE) */
  1.1047  #define ok_magic(M)      (1)
  1.1048  #endif /* (FOOTERS && !INSECURE) */
  1.1049  
  1.1050 @@ -2459,446 +2483,478 @@
  1.1051  /* ---------------------------- setting mparams -------------------------- */
  1.1052  
  1.1053  /* Initialize mparams */
  1.1054 -static int init_mparams(void) {
  1.1055 -  if (mparams.page_size == 0) {
  1.1056 -    size_t s;
  1.1057 -
  1.1058 -    mparams.mmap_threshold = DEFAULT_MMAP_THRESHOLD;
  1.1059 -    mparams.trim_threshold = DEFAULT_TRIM_THRESHOLD;
  1.1060 +static int
  1.1061 +init_mparams (void)
  1.1062 +{
  1.1063 +    if (mparams.page_size == 0) {
  1.1064 +        size_t s;
  1.1065 +
  1.1066 +        mparams.mmap_threshold = DEFAULT_MMAP_THRESHOLD;
  1.1067 +        mparams.trim_threshold = DEFAULT_TRIM_THRESHOLD;
  1.1068  #if MORECORE_CONTIGUOUS
  1.1069 -    mparams.default_mflags = USE_LOCK_BIT|USE_MMAP_BIT;
  1.1070 -#else  /* MORECORE_CONTIGUOUS */
  1.1071 -    mparams.default_mflags = USE_LOCK_BIT|USE_MMAP_BIT|USE_NONCONTIGUOUS_BIT;
  1.1072 +        mparams.default_mflags = USE_LOCK_BIT | USE_MMAP_BIT;
  1.1073 +#else /* MORECORE_CONTIGUOUS */
  1.1074 +        mparams.default_mflags =
  1.1075 +            USE_LOCK_BIT | USE_MMAP_BIT | USE_NONCONTIGUOUS_BIT;
  1.1076  #endif /* MORECORE_CONTIGUOUS */
  1.1077  
  1.1078  #if (FOOTERS && !INSECURE)
  1.1079 -    {
  1.1080 +        {
  1.1081  #if USE_DEV_RANDOM
  1.1082 -      int fd;
  1.1083 -      unsigned char buf[sizeof(size_t)];
  1.1084 -      /* Try to use /dev/urandom, else fall back on using time */
  1.1085 -      if ((fd = open("/dev/urandom", O_RDONLY)) >= 0 &&
  1.1086 -          read(fd, buf, sizeof(buf)) == sizeof(buf)) {
  1.1087 -        s = *((size_t *) buf);
  1.1088 -        close(fd);
  1.1089 -      }
  1.1090 -      else
  1.1091 +            int fd;
  1.1092 +            unsigned char buf[sizeof (size_t)];
  1.1093 +            /* Try to use /dev/urandom, else fall back on using time */
  1.1094 +            if ((fd = open ("/dev/urandom", O_RDONLY)) >= 0 &&
  1.1095 +                read (fd, buf, sizeof (buf)) == sizeof (buf)) {
  1.1096 +                s = *((size_t *) buf);
  1.1097 +                close (fd);
  1.1098 +            } else
  1.1099  #endif /* USE_DEV_RANDOM */
  1.1100 -        s = (size_t)(time(0) ^ (size_t)0x55555555U);
  1.1101 -
  1.1102 -      s |= (size_t)8U;    /* ensure nonzero */
  1.1103 -      s &= ~(size_t)7U;   /* improve chances of fault for bad values */
  1.1104 -
  1.1105 -    }
  1.1106 +                s = (size_t) (time (0) ^ (size_t) 0x55555555U);
  1.1107 +
  1.1108 +            s |= (size_t) 8U;   /* ensure nonzero */
  1.1109 +            s &= ~(size_t) 7U;  /* improve chances of fault for bad values */
  1.1110 +
  1.1111 +        }
  1.1112  #else /* (FOOTERS && !INSECURE) */
  1.1113 -    s = (size_t)0x58585858U;
  1.1114 +        s = (size_t) 0x58585858U;
  1.1115  #endif /* (FOOTERS && !INSECURE) */
  1.1116 -    ACQUIRE_MAGIC_INIT_LOCK();
  1.1117 -    if (mparams.magic == 0) {
  1.1118 -      mparams.magic = s;
  1.1119 -      /* Set up lock for main malloc area */
  1.1120 -      INITIAL_LOCK(&gm->mutex);
  1.1121 -      gm->mflags = mparams.default_mflags;
  1.1122 -    }
  1.1123 -    RELEASE_MAGIC_INIT_LOCK();
  1.1124 +        ACQUIRE_MAGIC_INIT_LOCK ();
  1.1125 +        if (mparams.magic == 0) {
  1.1126 +            mparams.magic = s;
  1.1127 +            /* Set up lock for main malloc area */
  1.1128 +            INITIAL_LOCK (&gm->mutex);
  1.1129 +            gm->mflags = mparams.default_mflags;
  1.1130 +        }
  1.1131 +        RELEASE_MAGIC_INIT_LOCK ();
  1.1132  
  1.1133  #ifndef WIN32
  1.1134 -    mparams.page_size = malloc_getpagesize;
  1.1135 -    mparams.granularity = ((DEFAULT_GRANULARITY != 0)?
  1.1136 -                           DEFAULT_GRANULARITY : mparams.page_size);
  1.1137 +        mparams.page_size = malloc_getpagesize;
  1.1138 +        mparams.granularity = ((DEFAULT_GRANULARITY != 0) ?
  1.1139 +                               DEFAULT_GRANULARITY : mparams.page_size);
  1.1140  #else /* WIN32 */
  1.1141 -    {
  1.1142 -      SYSTEM_INFO system_info;
  1.1143 -      GetSystemInfo(&system_info);
  1.1144 -      mparams.page_size = system_info.dwPageSize;
  1.1145 -      mparams.granularity = system_info.dwAllocationGranularity;
  1.1146 -    }
  1.1147 +        {
  1.1148 +            SYSTEM_INFO system_info;
  1.1149 +            GetSystemInfo (&system_info);
  1.1150 +            mparams.page_size = system_info.dwPageSize;
  1.1151 +            mparams.granularity = system_info.dwAllocationGranularity;
  1.1152 +        }
  1.1153  #endif /* WIN32 */
  1.1154  
  1.1155 -    /* Sanity-check configuration:
  1.1156 -       size_t must be unsigned and as wide as pointer type.
  1.1157 -       ints must be at least 4 bytes.
  1.1158 -       alignment must be at least 8.
  1.1159 -       Alignment, min chunk size, and page size must all be powers of 2.
  1.1160 -    */
  1.1161 -    if ((sizeof(size_t) != sizeof(char*)) ||
  1.1162 -        (MAX_SIZE_T < MIN_CHUNK_SIZE)  ||
  1.1163 -        (sizeof(int) < 4)  ||
  1.1164 -        (MALLOC_ALIGNMENT < (size_t)8U) ||
  1.1165 -        ((MALLOC_ALIGNMENT    & (MALLOC_ALIGNMENT-SIZE_T_ONE))    != 0) ||
  1.1166 -        ((MCHUNK_SIZE         & (MCHUNK_SIZE-SIZE_T_ONE))         != 0) ||
  1.1167 -        ((mparams.granularity & (mparams.granularity-SIZE_T_ONE)) != 0) ||
  1.1168 -        ((mparams.page_size   & (mparams.page_size-SIZE_T_ONE))   != 0))
  1.1169 -      ABORT;
  1.1170 -  }
  1.1171 -  return 0;
  1.1172 +        /* Sanity-check configuration:
  1.1173 +           size_t must be unsigned and as wide as pointer type.
  1.1174 +           ints must be at least 4 bytes.
  1.1175 +           alignment must be at least 8.
  1.1176 +           Alignment, min chunk size, and page size must all be powers of 2.
  1.1177 +         */
  1.1178 +        if ((sizeof (size_t) != sizeof (char *)) ||
  1.1179 +            (MAX_SIZE_T < MIN_CHUNK_SIZE) ||
  1.1180 +            (sizeof (int) < 4) ||
  1.1181 +            (MALLOC_ALIGNMENT < (size_t) 8U) ||
  1.1182 +            ((MALLOC_ALIGNMENT & (MALLOC_ALIGNMENT - SIZE_T_ONE)) != 0) ||
  1.1183 +            ((MCHUNK_SIZE & (MCHUNK_SIZE - SIZE_T_ONE)) != 0) ||
  1.1184 +            ((mparams.granularity & (mparams.granularity - SIZE_T_ONE)) != 0)
  1.1185 +            || ((mparams.page_size & (mparams.page_size - SIZE_T_ONE)) != 0))
  1.1186 +            ABORT;
  1.1187 +    }
  1.1188 +    return 0;
  1.1189  }
  1.1190  
  1.1191  /* support for mallopt */
  1.1192 -static int change_mparam(int param_number, int value) {
  1.1193 -  size_t val = (size_t)value;
  1.1194 -  init_mparams();
  1.1195 -  switch(param_number) {
  1.1196 -  case M_TRIM_THRESHOLD:
  1.1197 -    mparams.trim_threshold = val;
  1.1198 -    return 1;
  1.1199 -  case M_GRANULARITY:
  1.1200 -    if (val >= mparams.page_size && ((val & (val-1)) == 0)) {
  1.1201 -      mparams.granularity = val;
  1.1202 -      return 1;
  1.1203 +static int
  1.1204 +change_mparam (int param_number, int value)
  1.1205 +{
  1.1206 +    size_t val = (size_t) value;
  1.1207 +    init_mparams ();
  1.1208 +    switch (param_number) {
  1.1209 +    case M_TRIM_THRESHOLD:
  1.1210 +        mparams.trim_threshold = val;
  1.1211 +        return 1;
  1.1212 +    case M_GRANULARITY:
  1.1213 +        if (val >= mparams.page_size && ((val & (val - 1)) == 0)) {
  1.1214 +            mparams.granularity = val;
  1.1215 +            return 1;
  1.1216 +        } else
  1.1217 +            return 0;
  1.1218 +    case M_MMAP_THRESHOLD:
  1.1219 +        mparams.mmap_threshold = val;
  1.1220 +        return 1;
  1.1221 +    default:
  1.1222 +        return 0;
  1.1223      }
  1.1224 -    else
  1.1225 -      return 0;
  1.1226 -  case M_MMAP_THRESHOLD:
  1.1227 -    mparams.mmap_threshold = val;
  1.1228 -    return 1;
  1.1229 -  default:
  1.1230 -    return 0;
  1.1231 -  }
  1.1232  }
  1.1233  
  1.1234  #if DEBUG
  1.1235  /* ------------------------- Debugging Support --------------------------- */
  1.1236  
  1.1237  /* Check properties of any chunk, whether free, inuse, mmapped etc  */
  1.1238 -static void do_check_any_chunk(mstate m, mchunkptr p) {
  1.1239 -  assert((is_aligned(chunk2mem(p))) || (p->head == FENCEPOST_HEAD));
  1.1240 -  assert(ok_address(m, p));
  1.1241 +static void
  1.1242 +do_check_any_chunk (mstate m, mchunkptr p)
  1.1243 +{
  1.1244 +    assert ((is_aligned (chunk2mem (p))) || (p->head == FENCEPOST_HEAD));
  1.1245 +    assert (ok_address (m, p));
  1.1246  }
  1.1247  
  1.1248  /* Check properties of top chunk */
  1.1249 -static void do_check_top_chunk(mstate m, mchunkptr p) {
  1.1250 -  msegmentptr sp = segment_holding(m, (char*)p);
  1.1251 -  size_t  sz = chunksize(p);
  1.1252 -  assert(sp != 0);
  1.1253 -  assert((is_aligned(chunk2mem(p))) || (p->head == FENCEPOST_HEAD));
  1.1254 -  assert(ok_address(m, p));
  1.1255 -  assert(sz == m->topsize);
  1.1256 -  assert(sz > 0);
  1.1257 -  assert(sz == ((sp->base + sp->size) - (char*)p) - TOP_FOOT_SIZE);
  1.1258 -  assert(pinuse(p));
  1.1259 -  assert(!next_pinuse(p));
  1.1260 +static void
  1.1261 +do_check_top_chunk (mstate m, mchunkptr p)
  1.1262 +{
  1.1263 +    msegmentptr sp = segment_holding (m, (char *) p);
  1.1264 +    size_t sz = chunksize (p);
  1.1265 +    assert (sp != 0);
  1.1266 +    assert ((is_aligned (chunk2mem (p))) || (p->head == FENCEPOST_HEAD));
  1.1267 +    assert (ok_address (m, p));
  1.1268 +    assert (sz == m->topsize);
  1.1269 +    assert (sz > 0);
  1.1270 +    assert (sz == ((sp->base + sp->size) - (char *) p) - TOP_FOOT_SIZE);
  1.1271 +    assert (pinuse (p));
  1.1272 +    assert (!next_pinuse (p));
  1.1273  }
  1.1274  
  1.1275  /* Check properties of (inuse) mmapped chunks */
  1.1276 -static void do_check_mmapped_chunk(mstate m, mchunkptr p) {
  1.1277 -  size_t  sz = chunksize(p);
  1.1278 -  size_t len = (sz + (p->prev_foot & ~IS_MMAPPED_BIT) + MMAP_FOOT_PAD);
  1.1279 -  assert(is_mmapped(p));
  1.1280 -  assert(use_mmap(m));
  1.1281 -  assert((is_aligned(chunk2mem(p))) || (p->head == FENCEPOST_HEAD));
  1.1282 -  assert(ok_address(m, p));
  1.1283 -  assert(!is_small(sz));
  1.1284 -  assert((len & (mparams.page_size-SIZE_T_ONE)) == 0);
  1.1285 -  assert(chunk_plus_offset(p, sz)->head == FENCEPOST_HEAD);
  1.1286 -  assert(chunk_plus_offset(p, sz+SIZE_T_SIZE)->head == 0);
  1.1287 +static void
  1.1288 +do_check_mmapped_chunk (mstate m, mchunkptr p)
  1.1289 +{
  1.1290 +    size_t sz = chunksize (p);
  1.1291 +    size_t len = (sz + (p->prev_foot & ~IS_MMAPPED_BIT) + MMAP_FOOT_PAD);
  1.1292 +    assert (is_mmapped (p));
  1.1293 +    assert (use_mmap (m));
  1.1294 +    assert ((is_aligned (chunk2mem (p))) || (p->head == FENCEPOST_HEAD));
  1.1295 +    assert (ok_address (m, p));
  1.1296 +    assert (!is_small (sz));
  1.1297 +    assert ((len & (mparams.page_size - SIZE_T_ONE)) == 0);
  1.1298 +    assert (chunk_plus_offset (p, sz)->head == FENCEPOST_HEAD);
  1.1299 +    assert (chunk_plus_offset (p, sz + SIZE_T_SIZE)->head == 0);
  1.1300  }
  1.1301  
  1.1302  /* Check properties of inuse chunks */
  1.1303 -static void do_check_inuse_chunk(mstate m, mchunkptr p) {
  1.1304 -  do_check_any_chunk(m, p);
  1.1305 -  assert(cinuse(p));
  1.1306 -  assert(next_pinuse(p));
  1.1307 -  /* If not pinuse and not mmapped, previous chunk has OK offset */
  1.1308 -  assert(is_mmapped(p) || pinuse(p) || next_chunk(prev_chunk(p)) == p);
  1.1309 -  if (is_mmapped(p))
  1.1310 -    do_check_mmapped_chunk(m, p);
  1.1311 +static void
  1.1312 +do_check_inuse_chunk (mstate m, mchunkptr p)
  1.1313 +{
  1.1314 +    do_check_any_chunk (m, p);
  1.1315 +    assert (cinuse (p));
  1.1316 +    assert (next_pinuse (p));
  1.1317 +    /* If not pinuse and not mmapped, previous chunk has OK offset */
  1.1318 +    assert (is_mmapped (p) || pinuse (p) || next_chunk (prev_chunk (p)) == p);
  1.1319 +    if (is_mmapped (p))
  1.1320 +        do_check_mmapped_chunk (m, p);
  1.1321  }
  1.1322  
  1.1323  /* Check properties of free chunks */
  1.1324 -static void do_check_free_chunk(mstate m, mchunkptr p) {
  1.1325 -  size_t sz = p->head & ~(PINUSE_BIT|CINUSE_BIT);
  1.1326 -  mchunkptr next = chunk_plus_offset(p, sz);
  1.1327 -  do_check_any_chunk(m, p);
  1.1328 -  assert(!cinuse(p));
  1.1329 -  assert(!next_pinuse(p));
  1.1330 -  assert (!is_mmapped(p));
  1.1331 -  if (p != m->dv && p != m->top) {
  1.1332 -    if (sz >= MIN_CHUNK_SIZE) {
  1.1333 -      assert((sz & CHUNK_ALIGN_MASK) == 0);
  1.1334 -      assert(is_aligned(chunk2mem(p)));
  1.1335 -      assert(next->prev_foot == sz);
  1.1336 -      assert(pinuse(p));
  1.1337 -      assert (next == m->top || cinuse(next));
  1.1338 -      assert(p->fd->bk == p);
  1.1339 -      assert(p->bk->fd == p);
  1.1340 +static void
  1.1341 +do_check_free_chunk (mstate m, mchunkptr p)
  1.1342 +{
  1.1343 +    size_t sz = p->head & ~(PINUSE_BIT | CINUSE_BIT);
  1.1344 +    mchunkptr next = chunk_plus_offset (p, sz);
  1.1345 +    do_check_any_chunk (m, p);
  1.1346 +    assert (!cinuse (p));
  1.1347 +    assert (!next_pinuse (p));
  1.1348 +    assert (!is_mmapped (p));
  1.1349 +    if (p != m->dv && p != m->top) {
  1.1350 +        if (sz >= MIN_CHUNK_SIZE) {
  1.1351 +            assert ((sz & CHUNK_ALIGN_MASK) == 0);
  1.1352 +            assert (is_aligned (chunk2mem (p)));
  1.1353 +            assert (next->prev_foot == sz);
  1.1354 +            assert (pinuse (p));
  1.1355 +            assert (next == m->top || cinuse (next));
  1.1356 +            assert (p->fd->bk == p);
  1.1357 +            assert (p->bk->fd == p);
  1.1358 +        } else                  /* markers are always of size SIZE_T_SIZE */
  1.1359 +            assert (sz == SIZE_T_SIZE);
  1.1360      }
  1.1361 -    else  /* markers are always of size SIZE_T_SIZE */
  1.1362 -      assert(sz == SIZE_T_SIZE);
  1.1363 -  }
  1.1364  }
  1.1365  
  1.1366  /* Check properties of malloced chunks at the point they are malloced */
  1.1367 -static void do_check_malloced_chunk(mstate m, void* mem, size_t s) {
  1.1368 -  if (mem != 0) {
  1.1369 -    mchunkptr p = mem2chunk(mem);
  1.1370 -    size_t sz = p->head & ~(PINUSE_BIT|CINUSE_BIT);
  1.1371 -    do_check_inuse_chunk(m, p);
  1.1372 -    assert((sz & CHUNK_ALIGN_MASK) == 0);
  1.1373 -    assert(sz >= MIN_CHUNK_SIZE);
  1.1374 -    assert(sz >= s);
  1.1375 -    /* unless mmapped, size is less than MIN_CHUNK_SIZE more than request */
  1.1376 -    assert(is_mmapped(p) || sz < (s + MIN_CHUNK_SIZE));
  1.1377 -  }
  1.1378 +static void
  1.1379 +do_check_malloced_chunk (mstate m, void *mem, size_t s)
  1.1380 +{
  1.1381 +    if (mem != 0) {
  1.1382 +        mchunkptr p = mem2chunk (mem);
  1.1383 +        size_t sz = p->head & ~(PINUSE_BIT | CINUSE_BIT);
  1.1384 +        do_check_inuse_chunk (m, p);
  1.1385 +        assert ((sz & CHUNK_ALIGN_MASK) == 0);
  1.1386 +        assert (sz >= MIN_CHUNK_SIZE);
  1.1387 +        assert (sz >= s);
  1.1388 +        /* unless mmapped, size is less than MIN_CHUNK_SIZE more than request */
  1.1389 +        assert (is_mmapped (p) || sz < (s + MIN_CHUNK_SIZE));
  1.1390 +    }
  1.1391  }
  1.1392  
  1.1393  /* Check a tree and its subtrees.  */
  1.1394 -static void do_check_tree(mstate m, tchunkptr t) {
  1.1395 -  tchunkptr head = 0;
  1.1396 -  tchunkptr u = t;
  1.1397 -  bindex_t tindex = t->index;
  1.1398 -  size_t tsize = chunksize(t);
  1.1399 -  bindex_t idx;
  1.1400 -  compute_tree_index(tsize, idx);
  1.1401 -  assert(tindex == idx);
  1.1402 -  assert(tsize >= MIN_LARGE_SIZE);
  1.1403 -  assert(tsize >= minsize_for_tree_index(idx));
  1.1404 -  assert((idx == NTREEBINS-1) || (tsize < minsize_for_tree_index((idx+1))));
  1.1405 -
  1.1406 -  do { /* traverse through chain of same-sized nodes */
  1.1407 -    do_check_any_chunk(m, ((mchunkptr)u));
  1.1408 -    assert(u->index == tindex);
  1.1409 -    assert(chunksize(u) == tsize);
  1.1410 -    assert(!cinuse(u));
  1.1411 -    assert(!next_pinuse(u));
  1.1412 -    assert(u->fd->bk == u);
  1.1413 -    assert(u->bk->fd == u);
  1.1414 -    if (u->parent == 0) {
  1.1415 -      assert(u->child[0] == 0);
  1.1416 -      assert(u->child[1] == 0);
  1.1417 +static void
  1.1418 +do_check_tree (mstate m, tchunkptr t)
  1.1419 +{
  1.1420 +    tchunkptr head = 0;
  1.1421 +    tchunkptr u = t;
  1.1422 +    bindex_t tindex = t->index;
  1.1423 +    size_t tsize = chunksize (t);
  1.1424 +    bindex_t idx;
  1.1425 +    compute_tree_index (tsize, idx);
  1.1426 +    assert (tindex == idx);
  1.1427 +    assert (tsize >= MIN_LARGE_SIZE);
  1.1428 +    assert (tsize >= minsize_for_tree_index (idx));
  1.1429 +    assert ((idx == NTREEBINS - 1)
  1.1430 +            || (tsize < minsize_for_tree_index ((idx + 1))));
  1.1431 +
  1.1432 +    do {                        /* traverse through chain of same-sized nodes */
  1.1433 +        do_check_any_chunk (m, ((mchunkptr) u));
  1.1434 +        assert (u->index == tindex);
  1.1435 +        assert (chunksize (u) == tsize);
  1.1436 +        assert (!cinuse (u));
  1.1437 +        assert (!next_pinuse (u));
  1.1438 +        assert (u->fd->bk == u);
  1.1439 +        assert (u->bk->fd == u);
  1.1440 +        if (u->parent == 0) {
  1.1441 +            assert (u->child[0] == 0);
  1.1442 +            assert (u->child[1] == 0);
  1.1443 +        } else {
  1.1444 +            assert (head == 0); /* only one node on chain has parent */
  1.1445 +            head = u;
  1.1446 +            assert (u->parent != u);
  1.1447 +            assert (u->parent->child[0] == u ||
  1.1448 +                    u->parent->child[1] == u ||
  1.1449 +                    *((tbinptr *) (u->parent)) == u);
  1.1450 +            if (u->child[0] != 0) {
  1.1451 +                assert (u->child[0]->parent == u);
  1.1452 +                assert (u->child[0] != u);
  1.1453 +                do_check_tree (m, u->child[0]);
  1.1454 +            }
  1.1455 +            if (u->child[1] != 0) {
  1.1456 +                assert (u->child[1]->parent == u);
  1.1457 +                assert (u->child[1] != u);
  1.1458 +                do_check_tree (m, u->child[1]);
  1.1459 +            }
  1.1460 +            if (u->child[0] != 0 && u->child[1] != 0) {
  1.1461 +                assert (chunksize (u->child[0]) < chunksize (u->child[1]));
  1.1462 +            }
  1.1463 +        }
  1.1464 +        u = u->fd;
  1.1465      }
  1.1466 -    else {
  1.1467 -      assert(head == 0); /* only one node on chain has parent */
  1.1468 -      head = u;
  1.1469 -      assert(u->parent != u);
  1.1470 -      assert (u->parent->child[0] == u ||
  1.1471 -              u->parent->child[1] == u ||
  1.1472 -              *((tbinptr*)(u->parent)) == u);
  1.1473 -      if (u->child[0] != 0) {
  1.1474 -        assert(u->child[0]->parent == u);
  1.1475 -        assert(u->child[0] != u);
  1.1476 -        do_check_tree(m, u->child[0]);
  1.1477 -      }
  1.1478 -      if (u->child[1] != 0) {
  1.1479 -        assert(u->child[1]->parent == u);
  1.1480 -        assert(u->child[1] != u);
  1.1481 -        do_check_tree(m, u->child[1]);
  1.1482 -      }
  1.1483 -      if (u->child[0] != 0 && u->child[1] != 0) {
  1.1484 -        assert(chunksize(u->child[0]) < chunksize(u->child[1]));
  1.1485 -      }
  1.1486 -    }
  1.1487 -    u = u->fd;
  1.1488 -  } while (u != t);
  1.1489 -  assert(head != 0);
  1.1490 +    while (u != t);
  1.1491 +    assert (head != 0);
  1.1492  }
  1.1493  
  1.1494  /*  Check all the chunks in a treebin.  */
  1.1495 -static void do_check_treebin(mstate m, bindex_t i) {
  1.1496 -  tbinptr* tb = treebin_at(m, i);
  1.1497 -  tchunkptr t = *tb;
  1.1498 -  int empty = (m->treemap & (1U << i)) == 0;
  1.1499 -  if (t == 0)
  1.1500 -    assert(empty);
  1.1501 -  if (!empty)
  1.1502 -    do_check_tree(m, t);
  1.1503 +static void
  1.1504 +do_check_treebin (mstate m, bindex_t i)
  1.1505 +{
  1.1506 +    tbinptr *tb = treebin_at (m, i);
  1.1507 +    tchunkptr t = *tb;
  1.1508 +    int empty = (m->treemap & (1U << i)) == 0;
  1.1509 +    if (t == 0)
  1.1510 +        assert (empty);
  1.1511 +    if (!empty)
  1.1512 +        do_check_tree (m, t);
  1.1513  }
  1.1514  
  1.1515  /*  Check all the chunks in a smallbin.  */
  1.1516 -static void do_check_smallbin(mstate m, bindex_t i) {
  1.1517 -  sbinptr b = smallbin_at(m, i);
  1.1518 -  mchunkptr p = b->bk;
  1.1519 -  unsigned int empty = (m->smallmap & (1U << i)) == 0;
  1.1520 -  if (p == b)
  1.1521 -    assert(empty);
  1.1522 -  if (!empty) {
  1.1523 -    for (; p != b; p = p->bk) {
  1.1524 -      size_t size = chunksize(p);
  1.1525 -      mchunkptr q;
  1.1526 -      /* each chunk claims to be free */
  1.1527 -      do_check_free_chunk(m, p);
  1.1528 -      /* chunk belongs in bin */
  1.1529 -      assert(small_index(size) == i);
  1.1530 -      assert(p->bk == b || chunksize(p->bk) == chunksize(p));
  1.1531 -      /* chunk is followed by an inuse chunk */
  1.1532 -      q = next_chunk(p);
  1.1533 -      if (q->head != FENCEPOST_HEAD)
  1.1534 -        do_check_inuse_chunk(m, q);
  1.1535 +static void
  1.1536 +do_check_smallbin (mstate m, bindex_t i)
  1.1537 +{
  1.1538 +    sbinptr b = smallbin_at (m, i);
  1.1539 +    mchunkptr p = b->bk;
  1.1540 +    unsigned int empty = (m->smallmap & (1U << i)) == 0;
  1.1541 +    if (p == b)
  1.1542 +        assert (empty);
  1.1543 +    if (!empty) {
  1.1544 +        for (; p != b; p = p->bk) {
  1.1545 +            size_t size = chunksize (p);
  1.1546 +            mchunkptr q;
  1.1547 +            /* each chunk claims to be free */
  1.1548 +            do_check_free_chunk (m, p);
  1.1549 +            /* chunk belongs in bin */
  1.1550 +            assert (small_index (size) == i);
  1.1551 +            assert (p->bk == b || chunksize (p->bk) == chunksize (p));
  1.1552 +            /* chunk is followed by an inuse chunk */
  1.1553 +            q = next_chunk (p);
  1.1554 +            if (q->head != FENCEPOST_HEAD)
  1.1555 +                do_check_inuse_chunk (m, q);
  1.1556 +        }
  1.1557      }
  1.1558 -  }
  1.1559  }
  1.1560  
  1.1561  /* Find x in a bin. Used in other check functions. */
  1.1562 -static int bin_find(mstate m, mchunkptr x) {
  1.1563 -  size_t size = chunksize(x);
  1.1564 -  if (is_small(size)) {
  1.1565 -    bindex_t sidx = small_index(size);
  1.1566 -    sbinptr b = smallbin_at(m, sidx);
  1.1567 -    if (smallmap_is_marked(m, sidx)) {
  1.1568 -      mchunkptr p = b;
  1.1569 -      do {
  1.1570 -        if (p == x)
  1.1571 -          return 1;
  1.1572 -      } while ((p = p->fd) != b);
  1.1573 +static int
  1.1574 +bin_find (mstate m, mchunkptr x)
  1.1575 +{
  1.1576 +    size_t size = chunksize (x);
  1.1577 +    if (is_small (size)) {
  1.1578 +        bindex_t sidx = small_index (size);
  1.1579 +        sbinptr b = smallbin_at (m, sidx);
  1.1580 +        if (smallmap_is_marked (m, sidx)) {
  1.1581 +            mchunkptr p = b;
  1.1582 +            do {
  1.1583 +                if (p == x)
  1.1584 +                    return 1;
  1.1585 +            }
  1.1586 +            while ((p = p->fd) != b);
  1.1587 +        }
  1.1588 +    } else {
  1.1589 +        bindex_t tidx;
  1.1590 +        compute_tree_index (size, tidx);
  1.1591 +        if (treemap_is_marked (m, tidx)) {
  1.1592 +            tchunkptr t = *treebin_at (m, tidx);
  1.1593 +            size_t sizebits = size << leftshift_for_tree_index (tidx);
  1.1594 +            while (t != 0 && chunksize (t) != size) {
  1.1595 +                t = t->child[(sizebits >> (SIZE_T_BITSIZE - SIZE_T_ONE)) & 1];
  1.1596 +                sizebits <<= 1;
  1.1597 +            }
  1.1598 +            if (t != 0) {
  1.1599 +                tchunkptr u = t;
  1.1600 +                do {
  1.1601 +                    if (u == (tchunkptr) x)
  1.1602 +                        return 1;
  1.1603 +                }
  1.1604 +                while ((u = u->fd) != t);
  1.1605 +            }
  1.1606 +        }
  1.1607      }
  1.1608 -  }
  1.1609 -  else {
  1.1610 -    bindex_t tidx;
  1.1611 -    compute_tree_index(size, tidx);
  1.1612 -    if (treemap_is_marked(m, tidx)) {
  1.1613 -      tchunkptr t = *treebin_at(m, tidx);
  1.1614 -      size_t sizebits = size << leftshift_for_tree_index(tidx);
  1.1615 -      while (t != 0 && chunksize(t) != size) {
  1.1616 -        t = t->child[(sizebits >> (SIZE_T_BITSIZE-SIZE_T_ONE)) & 1];
  1.1617 -        sizebits <<= 1;
  1.1618 -      }
  1.1619 -      if (t != 0) {
  1.1620 -        tchunkptr u = t;
  1.1621 -        do {
  1.1622 -          if (u == (tchunkptr)x)
  1.1623 -            return 1;
  1.1624 -        } while ((u = u->fd) != t);
  1.1625 -      }
  1.1626 -    }
  1.1627 -  }
  1.1628 -  return 0;
  1.1629 +    return 0;
  1.1630  }
  1.1631  
  1.1632  /* Traverse each chunk and check it; return total */
  1.1633 -static size_t traverse_and_check(mstate m) {
  1.1634 -  size_t sum = 0;
  1.1635 -  if (is_initialized(m)) {
  1.1636 -    msegmentptr s = &m->seg;
  1.1637 -    sum += m->topsize + TOP_FOOT_SIZE;
  1.1638 -    while (s != 0) {
  1.1639 -      mchunkptr q = align_as_chunk(s->base);
  1.1640 -      mchunkptr lastq = 0;
  1.1641 -      assert(pinuse(q));
  1.1642 -      while (segment_holds(s, q) &&
  1.1643 -             q != m->top && q->head != FENCEPOST_HEAD) {
  1.1644 -        sum += chunksize(q);
  1.1645 -        if (cinuse(q)) {
  1.1646 -          assert(!bin_find(m, q));
  1.1647 -          do_check_inuse_chunk(m, q);
  1.1648 +static size_t
  1.1649 +traverse_and_check (mstate m)
  1.1650 +{
  1.1651 +    size_t sum = 0;
  1.1652 +    if (is_initialized (m)) {
  1.1653 +        msegmentptr s = &m->seg;
  1.1654 +        sum += m->topsize + TOP_FOOT_SIZE;
  1.1655 +        while (s != 0) {
  1.1656 +            mchunkptr q = align_as_chunk (s->base);
  1.1657 +            mchunkptr lastq = 0;
  1.1658 +            assert (pinuse (q));
  1.1659 +            while (segment_holds (s, q) &&
  1.1660 +                   q != m->top && q->head != FENCEPOST_HEAD) {
  1.1661 +                sum += chunksize (q);
  1.1662 +                if (cinuse (q)) {
  1.1663 +                    assert (!bin_find (m, q));
  1.1664 +                    do_check_inuse_chunk (m, q);
  1.1665 +                } else {
  1.1666 +                    assert (q == m->dv || bin_find (m, q));
  1.1667 +                    assert (lastq == 0 || cinuse (lastq));      /* Not 2 consecutive free */
  1.1668 +                    do_check_free_chunk (m, q);
  1.1669 +                }
  1.1670 +                lastq = q;
  1.1671 +                q = next_chunk (q);
  1.1672 +            }
  1.1673 +            s = s->next;
  1.1674          }
  1.1675 -        else {
  1.1676 -          assert(q == m->dv || bin_find(m, q));
  1.1677 -          assert(lastq == 0 || cinuse(lastq)); /* Not 2 consecutive free */
  1.1678 -          do_check_free_chunk(m, q);
  1.1679 -        }
  1.1680 -        lastq = q;
  1.1681 -        q = next_chunk(q);
  1.1682 -      }
  1.1683 -      s = s->next;
  1.1684      }
  1.1685 -  }
  1.1686 -  return sum;
  1.1687 +    return sum;
  1.1688  }
  1.1689  
  1.1690  /* Check all properties of malloc_state. */
  1.1691 -static void do_check_malloc_state(mstate m) {
  1.1692 -  bindex_t i;
  1.1693 -  size_t total;
  1.1694 -  /* check bins */
  1.1695 -  for (i = 0; i < NSMALLBINS; ++i)
  1.1696 -    do_check_smallbin(m, i);
  1.1697 -  for (i = 0; i < NTREEBINS; ++i)
  1.1698 -    do_check_treebin(m, i);
  1.1699 -
  1.1700 -  if (m->dvsize != 0) { /* check dv chunk */
  1.1701 -    do_check_any_chunk(m, m->dv);
  1.1702 -    assert(m->dvsize == chunksize(m->dv));
  1.1703 -    assert(m->dvsize >= MIN_CHUNK_SIZE);
  1.1704 -    assert(bin_find(m, m->dv) == 0);
  1.1705 -  }
  1.1706 -
  1.1707 -  if (m->top != 0) {   /* check top chunk */
  1.1708 -    do_check_top_chunk(m, m->top);
  1.1709 -    assert(m->topsize == chunksize(m->top));
  1.1710 -    assert(m->topsize > 0);
  1.1711 -    assert(bin_find(m, m->top) == 0);
  1.1712 -  }
  1.1713 -
  1.1714 -  total = traverse_and_check(m);
  1.1715 -  assert(total <= m->footprint);
  1.1716 -  assert(m->footprint <= m->max_footprint);
  1.1717 +static void
  1.1718 +do_check_malloc_state (mstate m)
  1.1719 +{
  1.1720 +    bindex_t i;
  1.1721 +    size_t total;
  1.1722 +    /* check bins */
  1.1723 +    for (i = 0; i < NSMALLBINS; ++i)
  1.1724 +        do_check_smallbin (m, i);
  1.1725 +    for (i = 0; i < NTREEBINS; ++i)
  1.1726 +        do_check_treebin (m, i);
  1.1727 +
  1.1728 +    if (m->dvsize != 0) {       /* check dv chunk */
  1.1729 +        do_check_any_chunk (m, m->dv);
  1.1730 +        assert (m->dvsize == chunksize (m->dv));
  1.1731 +        assert (m->dvsize >= MIN_CHUNK_SIZE);
  1.1732 +        assert (bin_find (m, m->dv) == 0);
  1.1733 +    }
  1.1734 +
  1.1735 +    if (m->top != 0) {          /* check top chunk */
  1.1736 +        do_check_top_chunk (m, m->top);
  1.1737 +        assert (m->topsize == chunksize (m->top));
  1.1738 +        assert (m->topsize > 0);
  1.1739 +        assert (bin_find (m, m->top) == 0);
  1.1740 +    }
  1.1741 +
  1.1742 +    total = traverse_and_check (m);
  1.1743 +    assert (total <= m->footprint);
  1.1744 +    assert (m->footprint <= m->max_footprint);
  1.1745  }
  1.1746  #endif /* DEBUG */
  1.1747  
  1.1748  /* ----------------------------- statistics ------------------------------ */
  1.1749  
  1.1750  #if !NO_MALLINFO
  1.1751 -static struct mallinfo internal_mallinfo(mstate m) {
  1.1752 -  struct mallinfo nm = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
  1.1753 -  if (!PREACTION(m)) {
  1.1754 -    check_malloc_state(m);
  1.1755 -    if (is_initialized(m)) {
  1.1756 -      size_t nfree = SIZE_T_ONE; /* top always free */
  1.1757 -      size_t mfree = m->topsize + TOP_FOOT_SIZE;
  1.1758 -      size_t sum = mfree;
  1.1759 -      msegmentptr s = &m->seg;
  1.1760 -      while (s != 0) {
  1.1761 -        mchunkptr q = align_as_chunk(s->base);
  1.1762 -        while (segment_holds(s, q) &&
  1.1763 -               q != m->top && q->head != FENCEPOST_HEAD) {
  1.1764 -          size_t sz = chunksize(q);
  1.1765 -          sum += sz;
  1.1766 -          if (!cinuse(q)) {
  1.1767 -            mfree += sz;
  1.1768 -            ++nfree;
  1.1769 -          }
  1.1770 -          q = next_chunk(q);
  1.1771 +static struct mallinfo
  1.1772 +internal_mallinfo (mstate m)
  1.1773 +{
  1.1774 +    struct mallinfo nm = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
  1.1775 +    if (!PREACTION (m)) {
  1.1776 +        check_malloc_state (m);
  1.1777 +        if (is_initialized (m)) {
  1.1778 +            size_t nfree = SIZE_T_ONE;  /* top always free */
  1.1779 +            size_t mfree = m->topsize + TOP_FOOT_SIZE;
  1.1780 +            size_t sum = mfree;
  1.1781 +            msegmentptr s = &m->seg;
  1.1782 +            while (s != 0) {
  1.1783 +                mchunkptr q = align_as_chunk (s->base);
  1.1784 +                while (segment_holds (s, q) &&
  1.1785 +                       q != m->top && q->head != FENCEPOST_HEAD) {
  1.1786 +                    size_t sz = chunksize (q);
  1.1787 +                    sum += sz;
  1.1788 +                    if (!cinuse (q)) {
  1.1789 +                        mfree += sz;
  1.1790 +                        ++nfree;
  1.1791 +                    }
  1.1792 +                    q = next_chunk (q);
  1.1793 +                }
  1.1794 +                s = s->next;
  1.1795 +            }
  1.1796 +
  1.1797 +            nm.arena = sum;
  1.1798 +            nm.ordblks = nfree;
  1.1799 +            nm.hblkhd = m->footprint - sum;
  1.1800 +            nm.usmblks = m->max_footprint;
  1.1801 +            nm.uordblks = m->footprint - mfree;
  1.1802 +            nm.fordblks = mfree;
  1.1803 +            nm.keepcost = m->topsize;
  1.1804          }
  1.1805 -        s = s->next;
  1.1806 -      }
  1.1807 -
  1.1808 -      nm.arena    = sum;
  1.1809 -      nm.ordblks  = nfree;
  1.1810 -      nm.hblkhd   = m->footprint - sum;
  1.1811 -      nm.usmblks  = m->max_footprint;
  1.1812 -      nm.uordblks = m->footprint - mfree;
  1.1813 -      nm.fordblks = mfree;
  1.1814 -      nm.keepcost = m->topsize;
  1.1815 +
  1.1816 +        POSTACTION (m);
  1.1817      }
  1.1818 -
  1.1819 -    POSTACTION(m);
  1.1820 -  }
  1.1821 -  return nm;
  1.1822 +    return nm;
  1.1823  }
  1.1824  #endif /* !NO_MALLINFO */
  1.1825  
  1.1826 -static void internal_malloc_stats(mstate m) {
  1.1827 -  if (!PREACTION(m)) {
  1.1828 -    size_t maxfp = 0;
  1.1829 -    size_t fp = 0;
  1.1830 -    size_t used = 0;
  1.1831 -    check_malloc_state(m);
  1.1832 -    if (is_initialized(m)) {
  1.1833 -      msegmentptr s = &m->seg;
  1.1834 -      maxfp = m->max_footprint;
  1.1835 -      fp = m->footprint;
  1.1836 -      used = fp - (m->topsize + TOP_FOOT_SIZE);
  1.1837 -
  1.1838 -      while (s != 0) {
  1.1839 -        mchunkptr q = align_as_chunk(s->base);
  1.1840 -        while (segment_holds(s, q) &&
  1.1841 -               q != m->top && q->head != FENCEPOST_HEAD) {
  1.1842 -          if (!cinuse(q))
  1.1843 -            used -= chunksize(q);
  1.1844 -          q = next_chunk(q);
  1.1845 +static void
  1.1846 +internal_malloc_stats (mstate m)
  1.1847 +{
  1.1848 +    if (!PREACTION (m)) {
  1.1849 +        size_t maxfp = 0;
  1.1850 +        size_t fp = 0;
  1.1851 +        size_t used = 0;
  1.1852 +        check_malloc_state (m);
  1.1853 +        if (is_initialized (m)) {
  1.1854 +            msegmentptr s = &m->seg;
  1.1855 +            maxfp = m->max_footprint;
  1.1856 +            fp = m->footprint;
  1.1857 +            used = fp - (m->topsize + TOP_FOOT_SIZE);
  1.1858 +
  1.1859 +            while (s != 0) {
  1.1860 +                mchunkptr q = align_as_chunk (s->base);
  1.1861 +                while (segment_holds (s, q) &&
  1.1862 +                       q != m->top && q->head != FENCEPOST_HEAD) {
  1.1863 +                    if (!cinuse (q))
  1.1864 +                        used -= chunksize (q);
  1.1865 +                    q = next_chunk (q);
  1.1866 +                }
  1.1867 +                s = s->next;
  1.1868 +            }
  1.1869          }
  1.1870 -        s = s->next;
  1.1871 -      }
  1.1872 +#ifndef LACKS_STDIO_H
  1.1873 +        fprintf (stderr, "max system bytes = %10lu\n",
  1.1874 +                 (unsigned long) (maxfp));
  1.1875 +        fprintf (stderr, "system bytes     = %10lu\n", (unsigned long) (fp));
  1.1876 +        fprintf (stderr, "in use bytes     = %10lu\n",
  1.1877 +                 (unsigned long) (used));
  1.1878 +#endif
  1.1879 +
  1.1880 +        POSTACTION (m);
  1.1881      }
  1.1882 -
  1.1883 -#ifndef LACKS_STDIO_H
  1.1884 -    fprintf(stderr, "max system bytes = %10lu\n", (unsigned long)(maxfp));
  1.1885 -    fprintf(stderr, "system bytes     = %10lu\n", (unsigned long)(fp));
  1.1886 -    fprintf(stderr, "in use bytes     = %10lu\n", (unsigned long)(used));
  1.1887 -#endif
  1.1888 -
  1.1889 -    POSTACTION(m);
  1.1890 -  }
  1.1891  }
  1.1892  
  1.1893  /* ----------------------- Operations on smallbins ----------------------- */
  1.1894 @@ -3162,905 +3218,925 @@
  1.1895  */
  1.1896  
  1.1897  /* Malloc using mmap */
  1.1898 -static void* mmap_alloc(mstate m, size_t nb) {
  1.1899 -  size_t mmsize = granularity_align(nb + SIX_SIZE_T_SIZES + CHUNK_ALIGN_MASK);
  1.1900 -  if (mmsize > nb) {     /* Check for wrap around 0 */
  1.1901 -    char* mm = (char*)(DIRECT_MMAP(mmsize));
  1.1902 -    if (mm != CMFAIL) {
  1.1903 -      size_t offset = align_offset(chunk2mem(mm));
  1.1904 -      size_t psize = mmsize - offset - MMAP_FOOT_PAD;
  1.1905 -      mchunkptr p = (mchunkptr)(mm + offset);
  1.1906 -      p->prev_foot = offset | IS_MMAPPED_BIT;
  1.1907 -      (p)->head = (psize|CINUSE_BIT);
  1.1908 -      mark_inuse_foot(m, p, psize);
  1.1909 -      chunk_plus_offset(p, psize)->head = FENCEPOST_HEAD;
  1.1910 -      chunk_plus_offset(p, psize+SIZE_T_SIZE)->head = 0;
  1.1911 -
  1.1912 -      if (mm < m->least_addr)
  1.1913 -        m->least_addr = mm;
  1.1914 -      if ((m->footprint += mmsize) > m->max_footprint)
  1.1915 -        m->max_footprint = m->footprint;
  1.1916 -      assert(is_aligned(chunk2mem(p)));
  1.1917 -      check_mmapped_chunk(m, p);
  1.1918 -      return chunk2mem(p);
  1.1919 +static void *
  1.1920 +mmap_alloc (mstate m, size_t nb)
  1.1921 +{
  1.1922 +    size_t mmsize =
  1.1923 +        granularity_align (nb + SIX_SIZE_T_SIZES + CHUNK_ALIGN_MASK);
  1.1924 +    if (mmsize > nb) {          /* Check for wrap around 0 */
  1.1925 +        char *mm = (char *) (DIRECT_MMAP (mmsize));
  1.1926 +        if (mm != CMFAIL) {
  1.1927 +            size_t offset = align_offset (chunk2mem (mm));
  1.1928 +            size_t psize = mmsize - offset - MMAP_FOOT_PAD;
  1.1929 +            mchunkptr p = (mchunkptr) (mm + offset);
  1.1930 +            p->prev_foot = offset | IS_MMAPPED_BIT;
  1.1931 +            (p)->head = (psize | CINUSE_BIT);
  1.1932 +            mark_inuse_foot (m, p, psize);
  1.1933 +            chunk_plus_offset (p, psize)->head = FENCEPOST_HEAD;
  1.1934 +            chunk_plus_offset (p, psize + SIZE_T_SIZE)->head = 0;
  1.1935 +
  1.1936 +            if (mm < m->least_addr)
  1.1937 +                m->least_addr = mm;
  1.1938 +            if ((m->footprint += mmsize) > m->max_footprint)
  1.1939 +                m->max_footprint = m->footprint;
  1.1940 +            assert (is_aligned (chunk2mem (p)));
  1.1941 +            check_mmapped_chunk (m, p);
  1.1942 +            return chunk2mem (p);
  1.1943 +        }
  1.1944      }
  1.1945 -  }
  1.1946 -  return 0;
  1.1947 +    return 0;
  1.1948  }
  1.1949  
  1.1950  /* Realloc using mmap */
  1.1951 -static mchunkptr mmap_resize(mstate m, mchunkptr oldp, size_t nb) {
  1.1952 -  size_t oldsize = chunksize(oldp);
  1.1953 -  if (is_small(nb)) /* Can't shrink mmap regions below small size */
  1.1954 +static mchunkptr
  1.1955 +mmap_resize (mstate m, mchunkptr oldp, size_t nb)
  1.1956 +{
  1.1957 +    size_t oldsize = chunksize (oldp);
  1.1958 +    if (is_small (nb))          /* Can't shrink mmap regions below small size */
  1.1959 +        return 0;
  1.1960 +    /* Keep old chunk if big enough but not too big */
  1.1961 +    if (oldsize >= nb + SIZE_T_SIZE &&
  1.1962 +        (oldsize - nb) <= (mparams.granularity << 1))
  1.1963 +        return oldp;
  1.1964 +    else {
  1.1965 +        size_t offset = oldp->prev_foot & ~IS_MMAPPED_BIT;
  1.1966 +        size_t oldmmsize = oldsize + offset + MMAP_FOOT_PAD;
  1.1967 +        size_t newmmsize = granularity_align (nb + SIX_SIZE_T_SIZES +
  1.1968 +                                              CHUNK_ALIGN_MASK);
  1.1969 +        char *cp = (char *) CALL_MREMAP ((char *) oldp - offset,
  1.1970 +                                         oldmmsize, newmmsize, 1);
  1.1971 +        if (cp != CMFAIL) {
  1.1972 +            mchunkptr newp = (mchunkptr) (cp + offset);
  1.1973 +            size_t psize = newmmsize - offset - MMAP_FOOT_PAD;
  1.1974 +            newp->head = (psize | CINUSE_BIT);
  1.1975 +            mark_inuse_foot (m, newp, psize);
  1.1976 +            chunk_plus_offset (newp, psize)->head = FENCEPOST_HEAD;
  1.1977 +            chunk_plus_offset (newp, psize + SIZE_T_SIZE)->head = 0;
  1.1978 +
  1.1979 +            if (cp < m->least_addr)
  1.1980 +                m->least_addr = cp;
  1.1981 +            if ((m->footprint += newmmsize - oldmmsize) > m->max_footprint)
  1.1982 +                m->max_footprint = m->footprint;
  1.1983 +            check_mmapped_chunk (m, newp);
  1.1984 +            return newp;
  1.1985 +        }
  1.1986 +    }
  1.1987      return 0;
  1.1988 -  /* Keep old chunk if big enough but not too big */
  1.1989 -  if (oldsize >= nb + SIZE_T_SIZE &&
  1.1990 -      (oldsize - nb) <= (mparams.granularity << 1))
  1.1991 -    return oldp;
  1.1992 -  else {
  1.1993 -    size_t offset = oldp->prev_foot & ~IS_MMAPPED_BIT;
  1.1994 -    size_t oldmmsize = oldsize + offset + MMAP_FOOT_PAD;
  1.1995 -    size_t newmmsize = granularity_align(nb + SIX_SIZE_T_SIZES +
  1.1996 -                                         CHUNK_ALIGN_MASK);
  1.1997 -    char* cp = (char*)CALL_MREMAP((char*)oldp - offset,
  1.1998 -                                  oldmmsize, newmmsize, 1);
  1.1999 -    if (cp != CMFAIL) {
  1.2000 -      mchunkptr newp = (mchunkptr)(cp + offset);
  1.2001 -      size_t psize = newmmsize - offset - MMAP_FOOT_PAD;
  1.2002 -      newp->head = (psize|CINUSE_BIT);
  1.2003 -      mark_inuse_foot(m, newp, psize);
  1.2004 -      chunk_plus_offset(newp, psize)->head = FENCEPOST_HEAD;
  1.2005 -      chunk_plus_offset(newp, psize+SIZE_T_SIZE)->head = 0;
  1.2006 -
  1.2007 -      if (cp < m->least_addr)
  1.2008 -        m->least_addr = cp;
  1.2009 -      if ((m->footprint += newmmsize - oldmmsize) > m->max_footprint)
  1.2010 -        m->max_footprint = m->footprint;
  1.2011 -      check_mmapped_chunk(m, newp);
  1.2012 -      return newp;
  1.2013 -    }
  1.2014 -  }
  1.2015 -  return 0;
  1.2016  }
  1.2017  
  1.2018  /* -------------------------- mspace management -------------------------- */
  1.2019  
  1.2020  /* Initialize top chunk and its size */
  1.2021 -static void init_top(mstate m, mchunkptr p, size_t psize) {
  1.2022 -  /* Ensure alignment */
  1.2023 -  size_t offset = align_offset(chunk2mem(p));
  1.2024 -  p = (mchunkptr)((char*)p + offset);
  1.2025 -  psize -= offset;
  1.2026 -
  1.2027 -  m->top = p;
  1.2028 -  m->topsize = psize;
  1.2029 -  p->head = psize | PINUSE_BIT;
  1.2030 -  /* set size of fake trailing chunk holding overhead space only once */
  1.2031 -  chunk_plus_offset(p, psize)->head = TOP_FOOT_SIZE;
  1.2032 -  m->trim_check = mparams.trim_threshold; /* reset on each update */
  1.2033 +static void
  1.2034 +init_top (mstate m, mchunkptr p, size_t psize)
  1.2035 +{
  1.2036 +    /* Ensure alignment */
  1.2037 +    size_t offset = align_offset (chunk2mem (p));
  1.2038 +    p = (mchunkptr) ((char *) p + offset);
  1.2039 +    psize -= offset;
  1.2040 +
  1.2041 +    m->top = p;
  1.2042 +    m->topsize = psize;
  1.2043 +    p->head = psize | PINUSE_BIT;
  1.2044 +    /* set size of fake trailing chunk holding overhead space only once */
  1.2045 +    chunk_plus_offset (p, psize)->head = TOP_FOOT_SIZE;
  1.2046 +    m->trim_check = mparams.trim_threshold;     /* reset on each update */
  1.2047  }
  1.2048  
  1.2049  /* Initialize bins for a new mstate that is otherwise zeroed out */
  1.2050 -static void init_bins(mstate m) {
  1.2051 -  /* Establish circular links for smallbins */
  1.2052 -  bindex_t i;
  1.2053 -  for (i = 0; i < NSMALLBINS; ++i) {
  1.2054 -    sbinptr bin = smallbin_at(m,i);
  1.2055 -    bin->fd = bin->bk = bin;
  1.2056 -  }
  1.2057 +static void
  1.2058 +init_bins (mstate m)
  1.2059 +{
  1.2060 +    /* Establish circular links for smallbins */
  1.2061 +    bindex_t i;
  1.2062 +    for (i = 0; i < NSMALLBINS; ++i) {
  1.2063 +        sbinptr bin = smallbin_at (m, i);
  1.2064 +        bin->fd = bin->bk = bin;
  1.2065 +    }
  1.2066  }
  1.2067  
  1.2068  #if PROCEED_ON_ERROR
  1.2069  
  1.2070  /* default corruption action */
  1.2071 -static void reset_on_error(mstate m) {
  1.2072 -  int i;
  1.2073 -  ++malloc_corruption_error_count;
  1.2074 -  /* Reinitialize fields to forget about all memory */
  1.2075 -  m->smallbins = m->treebins = 0;
  1.2076 -  m->dvsize = m->topsize = 0;
  1.2077 -  m->seg.base = 0;
  1.2078 -  m->seg.size = 0;
  1.2079 -  m->seg.next = 0;
  1.2080 -  m->top = m->dv = 0;
  1.2081 -  for (i = 0; i < NTREEBINS; ++i)
  1.2082 -    *treebin_at(m, i) = 0;
  1.2083 -  init_bins(m);
  1.2084 +static void
  1.2085 +reset_on_error (mstate m)
  1.2086 +{
  1.2087 +    int i;
  1.2088 +    ++malloc_corruption_error_count;
  1.2089 +    /* Reinitialize fields to forget about all memory */
  1.2090 +    m->smallbins = m->treebins = 0;
  1.2091 +    m->dvsize = m->topsize = 0;
  1.2092 +    m->seg.base = 0;
  1.2093 +    m->seg.size = 0;
  1.2094 +    m->seg.next = 0;
  1.2095 +    m->top = m->dv = 0;
  1.2096 +    for (i = 0; i < NTREEBINS; ++i)
  1.2097 +        *treebin_at (m, i) = 0;
  1.2098 +    init_bins (m);
  1.2099  }
  1.2100  #endif /* PROCEED_ON_ERROR */
  1.2101  
  1.2102  /* Allocate chunk and prepend remainder with chunk in successor base. */
  1.2103 -static void* prepend_alloc(mstate m, char* newbase, char* oldbase,
  1.2104 -                           size_t nb) {
  1.2105 -  mchunkptr p = align_as_chunk(newbase);
  1.2106 -  mchunkptr oldfirst = align_as_chunk(oldbase);
  1.2107 -  size_t psize = (char*)oldfirst - (char*)p;
  1.2108 -  mchunkptr q = chunk_plus_offset(p, nb);
  1.2109 -  size_t qsize = psize - nb;
  1.2110 -  set_size_and_pinuse_of_inuse_chunk(m, p, nb);
  1.2111 -
  1.2112 -  assert((char*)oldfirst > (char*)q);
  1.2113 -  assert(pinuse(oldfirst));
  1.2114 -  assert(qsize >= MIN_CHUNK_SIZE);
  1.2115 -
  1.2116 -  /* consolidate remainder with first chunk of old base */
  1.2117 -  if (oldfirst == m->top) {
  1.2118 -    size_t tsize = m->topsize += qsize;
  1.2119 -    m->top = q;
  1.2120 -    q->head = tsize | PINUSE_BIT;
  1.2121 -    check_top_chunk(m, q);
  1.2122 -  }
  1.2123 -  else if (oldfirst == m->dv) {
  1.2124 -    size_t dsize = m->dvsize += qsize;
  1.2125 -    m->dv = q;
  1.2126 -    set_size_and_pinuse_of_free_chunk(q, dsize);
  1.2127 -  }
  1.2128 -  else {
  1.2129 -    if (!cinuse(oldfirst)) {
  1.2130 -      size_t nsize = chunksize(oldfirst);
  1.2131 -      unlink_chunk(m, oldfirst, nsize);
  1.2132 -      oldfirst = chunk_plus_offset(oldfirst, nsize);
  1.2133 -      qsize += nsize;
  1.2134 +static void *
  1.2135 +prepend_alloc (mstate m, char *newbase, char *oldbase, size_t nb)
  1.2136 +{
  1.2137 +    mchunkptr p = align_as_chunk (newbase);
  1.2138 +    mchunkptr oldfirst = align_as_chunk (oldbase);
  1.2139 +    size_t psize = (char *) oldfirst - (char *) p;
  1.2140 +    mchunkptr q = chunk_plus_offset (p, nb);
  1.2141 +    size_t qsize = psize - nb;
  1.2142 +    set_size_and_pinuse_of_inuse_chunk (m, p, nb);
  1.2143 +
  1.2144 +    assert ((char *) oldfirst > (char *) q);
  1.2145 +    assert (pinuse (oldfirst));
  1.2146 +    assert (qsize >= MIN_CHUNK_SIZE);
  1.2147 +
  1.2148 +    /* consolidate remainder with first chunk of old base */
  1.2149 +    if (oldfirst == m->top) {
  1.2150 +        size_t tsize = m->topsize += qsize;
  1.2151 +        m->top = q;
  1.2152 +        q->head = tsize | PINUSE_BIT;
  1.2153 +        check_top_chunk (m, q);
  1.2154 +    } else if (oldfirst == m->dv) {
  1.2155 +        size_t dsize = m->dvsize += qsize;
  1.2156 +        m->dv = q;
  1.2157 +        set_size_and_pinuse_of_free_chunk (q, dsize);
  1.2158 +    } else {
  1.2159 +        if (!cinuse (oldfirst)) {
  1.2160 +            size_t nsize = chunksize (oldfirst);
  1.2161 +            unlink_chunk (m, oldfirst, nsize);
  1.2162 +            oldfirst = chunk_plus_offset (oldfirst, nsize);
  1.2163 +            qsize += nsize;
  1.2164 +        }
  1.2165 +        set_free_with_pinuse (q, qsize, oldfirst);
  1.2166 +        insert_chunk (m, q, qsize);
  1.2167 +        check_free_chunk (m, q);
  1.2168      }
  1.2169 -    set_free_with_pinuse(q, qsize, oldfirst);
  1.2170 -    insert_chunk(m, q, qsize);
  1.2171 -    check_free_chunk(m, q);
  1.2172 -  }
  1.2173 -
  1.2174 -  check_malloced_chunk(m, chunk2mem(p), nb);
  1.2175 -  return chunk2mem(p);
  1.2176 +
  1.2177 +    check_malloced_chunk (m, chunk2mem (p), nb);
  1.2178 +    return chunk2mem (p);
  1.2179  }
  1.2180  
  1.2181  
  1.2182  /* Add a segment to hold a new noncontiguous region */
  1.2183 -static void add_segment(mstate m, char* tbase, size_t tsize, flag_t mmapped) {
  1.2184 -  /* Determine locations and sizes of segment, fenceposts, old top */
  1.2185 -  char* old_top = (char*)m->top;
  1.2186 -  msegmentptr oldsp = segment_holding(m, old_top);
  1.2187 -  char* old_end = oldsp->base + oldsp->size;
  1.2188 -  size_t ssize = pad_request(sizeof(struct malloc_segment));
  1.2189 -  char* rawsp = old_end - (ssize + FOUR_SIZE_T_SIZES + CHUNK_ALIGN_MASK);
  1.2190 -  size_t offset = align_offset(chunk2mem(rawsp));
  1.2191 -  char* asp = rawsp + offset;
  1.2192 -  char* csp = (asp < (old_top + MIN_CHUNK_SIZE))? old_top : asp;
  1.2193 -  mchunkptr sp = (mchunkptr)csp;
  1.2194 -  msegmentptr ss = (msegmentptr)(chunk2mem(sp));
  1.2195 -  mchunkptr tnext = chunk_plus_offset(sp, ssize);
  1.2196 -  mchunkptr p = tnext;
  1.2197 -  int nfences = 0;
  1.2198 -
  1.2199 -  /* reset top to new space */
  1.2200 -  init_top(m, (mchunkptr)tbase, tsize - TOP_FOOT_SIZE);
  1.2201 -
  1.2202 -  /* Set up segment record */
  1.2203 -  assert(is_aligned(ss));
  1.2204 -  set_size_and_pinuse_of_inuse_chunk(m, sp, ssize);
  1.2205 -  *ss = m->seg; /* Push current record */
  1.2206 -  m->seg.base = tbase;
  1.2207 -  m->seg.size = tsize;
  1.2208 -  m->seg.sflags = mmapped;
  1.2209 -  m->seg.next = ss;
  1.2210 -
  1.2211 -  /* Insert trailing fenceposts */
  1.2212 -  for (;;) {
  1.2213 -    mchunkptr nextp = chunk_plus_offset(p, SIZE_T_SIZE);
  1.2214 -    p->head = FENCEPOST_HEAD;
  1.2215 -    ++nfences;
  1.2216 -    if ((char*)(&(nextp->head)) < old_end)
  1.2217 -      p = nextp;
  1.2218 -    else
  1.2219 -      break;
  1.2220 -  }
  1.2221 -  assert(nfences >= 2);
  1.2222 -
  1.2223 -  /* Insert the rest of old top into a bin as an ordinary free chunk */
  1.2224 -  if (csp != old_top) {
  1.2225 -    mchunkptr q = (mchunkptr)old_top;
  1.2226 -    size_t psize = csp - old_top;
  1.2227 -    mchunkptr tn = chunk_plus_offset(q, psize);
  1.2228 -    set_free_with_pinuse(q, psize, tn);
  1.2229 -    insert_chunk(m, q, psize);
  1.2230 -  }
  1.2231 -
  1.2232 -  check_top_chunk(m, m->top);
  1.2233 +static void
  1.2234 +add_segment (mstate m, char *tbase, size_t tsize, flag_t mmapped)
  1.2235 +{
  1.2236 +    /* Determine locations and sizes of segment, fenceposts, old top */
  1.2237 +    char *old_top = (char *) m->top;
  1.2238 +    msegmentptr oldsp = segment_holding (m, old_top);
  1.2239 +    char *old_end = oldsp->base + oldsp->size;
  1.2240 +    size_t ssize = pad_request (sizeof (struct malloc_segment));
  1.2241 +    char *rawsp = old_end - (ssize + FOUR_SIZE_T_SIZES + CHUNK_ALIGN_MASK);
  1.2242 +    size_t offset = align_offset (chunk2mem (rawsp));
  1.2243 +    char *asp = rawsp + offset;
  1.2244 +    char *csp = (asp < (old_top + MIN_CHUNK_SIZE)) ? old_top : asp;
  1.2245 +    mchunkptr sp = (mchunkptr) csp;
  1.2246 +    msegmentptr ss = (msegmentptr) (chunk2mem (sp));
  1.2247 +    mchunkptr tnext = chunk_plus_offset (sp, ssize);
  1.2248 +    mchunkptr p = tnext;
  1.2249 +    int nfences = 0;
  1.2250 +
  1.2251 +    /* reset top to new space */
  1.2252 +    init_top (m, (mchunkptr) tbase, tsize - TOP_FOOT_SIZE);
  1.2253 +
  1.2254 +    /* Set up segment record */
  1.2255 +    assert (is_aligned (ss));
  1.2256 +    set_size_and_pinuse_of_inuse_chunk (m, sp, ssize);
  1.2257 +    *ss = m->seg;               /* Push current record */
  1.2258 +    m->seg.base = tbase;
  1.2259 +    m->seg.size = tsize;
  1.2260 +    m->seg.sflags = mmapped;
  1.2261 +    m->seg.next = ss;
  1.2262 +
  1.2263 +    /* Insert trailing fenceposts */
  1.2264 +    for (;;) {
  1.2265 +        mchunkptr nextp = chunk_plus_offset (p, SIZE_T_SIZE);
  1.2266 +        p->head = FENCEPOST_HEAD;
  1.2267 +        ++nfences;
  1.2268 +        if ((char *) (&(nextp->head)) < old_end)
  1.2269 +            p = nextp;
  1.2270 +        else
  1.2271 +            break;
  1.2272 +    }
  1.2273 +    assert (nfences >= 2);
  1.2274 +
  1.2275 +    /* Insert the rest of old top into a bin as an ordinary free chunk */
  1.2276 +    if (csp != old_top) {
  1.2277 +        mchunkptr q = (mchunkptr) old_top;
  1.2278 +        size_t psize = csp - old_top;
  1.2279 +        mchunkptr tn = chunk_plus_offset (q, psize);
  1.2280 +        set_free_with_pinuse (q, psize, tn);
  1.2281 +        insert_chunk (m, q, psize);
  1.2282 +    }
  1.2283 +
  1.2284 +    check_top_chunk (m, m->top);
  1.2285  }
  1.2286  
  1.2287  /* -------------------------- System allocation -------------------------- */
  1.2288  
  1.2289  /* Get memory from system using MORECORE or MMAP */
  1.2290 -static void* sys_alloc(mstate m, size_t nb) {
  1.2291 -  char* tbase = CMFAIL;
  1.2292 -  size_t tsize = 0;
  1.2293 -  flag_t mmap_flag = 0;
  1.2294 -
  1.2295 -  init_mparams();
  1.2296 -
  1.2297 -  /* Directly map large chunks */
  1.2298 -  if (use_mmap(m) && nb >= mparams.mmap_threshold) {
  1.2299 -    void* mem = mmap_alloc(m, nb);
  1.2300 -    if (mem != 0)
  1.2301 -      return mem;
  1.2302 -  }
  1.2303 -
  1.2304 -  /*
  1.2305 -    Try getting memory in any of three ways (in most-preferred to
  1.2306 -    least-preferred order):
  1.2307 -    1. A call to MORECORE that can normally contiguously extend memory.
  1.2308 +static void *
  1.2309 +sys_alloc (mstate m, size_t nb)
  1.2310 +{
  1.2311 +    char *tbase = CMFAIL;
  1.2312 +    size_t tsize = 0;
  1.2313 +    flag_t mmap_flag = 0;
  1.2314 +
  1.2315 +    init_mparams ();
  1.2316 +
  1.2317 +    /* Directly map large chunks */
  1.2318 +    if (use_mmap (m) && nb >= mparams.mmap_threshold) {
  1.2319 +        void *mem = mmap_alloc (m, nb);
  1.2320 +        if (mem != 0)
  1.2321 +            return mem;
  1.2322 +    }
  1.2323 +
  1.2324 +    /*
  1.2325 +       Try getting memory in any of three ways (in most-preferred to
  1.2326 +       least-preferred order):
  1.2327 +       1. A call to MORECORE that can normally contiguously extend memory.
  1.2328         (disabled if not MORECORE_CONTIGUOUS or not HAVE_MORECORE or
  1.2329         or main space is mmapped or a previous contiguous call failed)
  1.2330 -    2. A call to MMAP new space (disabled if not HAVE_MMAP).
  1.2331 +       2. A call to MMAP new space (disabled if not HAVE_MMAP).
  1.2332         Note that under the default settings, if MORECORE is unable to
  1.2333         fulfill a request, and HAVE_MMAP is true, then mmap is
  1.2334         used as a noncontiguous system allocator. This is a useful backup
  1.2335         strategy for systems with holes in address spaces -- in this case
  1.2336         sbrk cannot contiguously expand the heap, but mmap may be able to
  1.2337         find space.
  1.2338 -    3. A call to MORECORE that cannot usually contiguously extend memory.
  1.2339 +       3. A call to MORECORE that cannot usually contiguously extend memory.
  1.2340         (disabled if not HAVE_MORECORE)
  1.2341 -  */
  1.2342 -
  1.2343 -  if (MORECORE_CONTIGUOUS && !use_noncontiguous(m)) {
  1.2344 -    char* br = CMFAIL;
  1.2345 -    msegmentptr ss = (m->top == 0)? 0 : segment_holding(m, (char*)m->top);
  1.2346 -    size_t asize = 0;
  1.2347 -    ACQUIRE_MORECORE_LOCK();
  1.2348 -
  1.2349 -    if (ss == 0) {  /* First time through or recovery */
  1.2350 -      char* base = (char*)CALL_MORECORE(0);
  1.2351 -      if (base != CMFAIL) {
  1.2352 -        asize = granularity_align(nb + TOP_FOOT_SIZE + SIZE_T_ONE);
  1.2353 -        /* Adjust to end on a page boundary */
  1.2354 -        if (!is_page_aligned(base))
  1.2355 -          asize += (page_align((size_t)base) - (size_t)base);
  1.2356 -        /* Can't call MORECORE if size is negative when treated as signed */
  1.2357 -        if (asize < HALF_MAX_SIZE_T &&
  1.2358 -            (br = (char*)(CALL_MORECORE(asize))) == base) {
  1.2359 -          tbase = base;
  1.2360 -          tsize = asize;
  1.2361 +     */
  1.2362 +
  1.2363 +    if (MORECORE_CONTIGUOUS && !use_noncontiguous (m)) {
  1.2364 +        char *br = CMFAIL;
  1.2365 +        msegmentptr ss =
  1.2366 +            (m->top == 0) ? 0 : segment_holding (m, (char *) m->top);
  1.2367 +        size_t asize = 0;
  1.2368 +        ACQUIRE_MORECORE_LOCK ();
  1.2369 +
  1.2370 +        if (ss == 0) {          /* First time through or recovery */
  1.2371 +            char *base = (char *) CALL_MORECORE (0);
  1.2372 +            if (base != CMFAIL) {
  1.2373 +                asize = granularity_align (nb + TOP_FOOT_SIZE + SIZE_T_ONE);
  1.2374 +                /* Adjust to end on a page boundary */
  1.2375 +                if (!is_page_aligned (base))
  1.2376 +                    asize += (page_align ((size_t) base) - (size_t) base);
  1.2377 +                /* Can't call MORECORE if size is negative when treated as signed */
  1.2378 +                if (asize < HALF_MAX_SIZE_T &&
  1.2379 +                    (br = (char *) (CALL_MORECORE (asize))) == base) {
  1.2380 +                    tbase = base;
  1.2381 +                    tsize = asize;
  1.2382 +                }
  1.2383 +            }
  1.2384 +        } else {
  1.2385 +            /* Subtract out existing available top space from MORECORE request. */
  1.2386 +            asize =
  1.2387 +                granularity_align (nb - m->topsize + TOP_FOOT_SIZE +
  1.2388 +                                   SIZE_T_ONE);
  1.2389 +            /* Use mem here only if it did continuously extend old space */
  1.2390 +            if (asize < HALF_MAX_SIZE_T &&
  1.2391 +                (br =
  1.2392 +                 (char *) (CALL_MORECORE (asize))) == ss->base + ss->size) {
  1.2393 +                tbase = br;
  1.2394 +                tsize = asize;
  1.2395 +            }
  1.2396          }
  1.2397 -      }
  1.2398 -    }
  1.2399 -    else {
  1.2400 -      /* Subtract out existing available top space from MORECORE request. */
  1.2401 -      asize = granularity_align(nb - m->topsize + TOP_FOOT_SIZE + SIZE_T_ONE);
  1.2402 -      /* Use mem here only if it did continuously extend old space */
  1.2403 -      if (asize < HALF_MAX_SIZE_T &&
  1.2404 -          (br = (char*)(CALL_MORECORE(asize))) == ss->base+ss->size) {
  1.2405 -        tbase = br;
  1.2406 -        tsize = asize;
  1.2407 -      }
  1.2408 +
  1.2409 +        if (tbase == CMFAIL) {  /* Cope with partial failure */
  1.2410 +            if (br != CMFAIL) { /* Try to use/extend the space we did get */
  1.2411 +                if (asize < HALF_MAX_SIZE_T &&
  1.2412 +                    asize < nb + TOP_FOOT_SIZE + SIZE_T_ONE) {
  1.2413 +                    size_t esize =
  1.2414 +                        granularity_align (nb + TOP_FOOT_SIZE +
  1.2415 +                                           SIZE_T_ONE - asize);
  1.2416 +                    if (esize < HALF_MAX_SIZE_T) {
  1.2417 +                        char *end = (char *) CALL_MORECORE (esize);
  1.2418 +                        if (end != CMFAIL)
  1.2419 +                            asize += esize;
  1.2420 +                        else {  /* Can't use; try to release */
  1.2421 +                            end = (char *) CALL_MORECORE (-asize);
  1.2422 +                            br = CMFAIL;
  1.2423 +                        }
  1.2424 +                    }
  1.2425 +                }
  1.2426 +            }
  1.2427 +            if (br != CMFAIL) { /* Use the space we did get */
  1.2428 +                tbase = br;
  1.2429 +                tsize = asize;
  1.2430 +            } else
  1.2431 +                disable_contiguous (m); /* Don't try contiguous path in the future */
  1.2432 +        }
  1.2433 +
  1.2434 +        RELEASE_MORECORE_LOCK ();
  1.2435      }
  1.2436  
  1.2437 -    if (tbase == CMFAIL) {    /* Cope with partial failure */
  1.2438 -      if (br != CMFAIL) {    /* Try to use/extend the space we did get */
  1.2439 -        if (asize < HALF_MAX_SIZE_T &&
  1.2440 -            asize < nb + TOP_FOOT_SIZE + SIZE_T_ONE) {
  1.2441 -          size_t esize = granularity_align(nb + TOP_FOOT_SIZE + SIZE_T_ONE - asize);
  1.2442 -          if (esize < HALF_MAX_SIZE_T) {
  1.2443 -            char* end = (char*)CALL_MORECORE(esize);
  1.2444 -            if (end != CMFAIL)
  1.2445 -              asize += esize;
  1.2446 -            else {            /* Can't use; try to release */
  1.2447 -              end = (char*)CALL_MORECORE(-asize);
  1.2448 -              br = CMFAIL;
  1.2449 +    if (HAVE_MMAP && tbase == CMFAIL) { /* Try MMAP */
  1.2450 +        size_t req = nb + TOP_FOOT_SIZE + SIZE_T_ONE;
  1.2451 +        size_t rsize = granularity_align (req);
  1.2452 +        if (rsize > nb) {       /* Fail if wraps around zero */
  1.2453 +            char *mp = (char *) (CALL_MMAP (rsize));
  1.2454 +            if (mp != CMFAIL) {
  1.2455 +                tbase = mp;
  1.2456 +                tsize = rsize;
  1.2457 +                mmap_flag = IS_MMAPPED_BIT;
  1.2458              }
  1.2459 -          }
  1.2460          }
  1.2461 -      }
  1.2462 -      if (br != CMFAIL) {    /* Use the space we did get */
  1.2463 -        tbase = br;
  1.2464 -        tsize = asize;
  1.2465 -      }
  1.2466 -      else
  1.2467 -        disable_contiguous(m); /* Don't try contiguous path in the future */
  1.2468 -    }
  1.2469 -
  1.2470 -    RELEASE_MORECORE_LOCK();
  1.2471 -  }
  1.2472 -
  1.2473 -  if (HAVE_MMAP && tbase == CMFAIL) {  /* Try MMAP */
  1.2474 -    size_t req = nb + TOP_FOOT_SIZE + SIZE_T_ONE;
  1.2475 -    size_t rsize = granularity_align(req);
  1.2476 -    if (rsize > nb) { /* Fail if wraps around zero */
  1.2477 -      char* mp = (char*)(CALL_MMAP(rsize));
  1.2478 -      if (mp != CMFAIL) {
  1.2479 -        tbase = mp;
  1.2480 -        tsize = rsize;
  1.2481 -        mmap_flag = IS_MMAPPED_BIT;
  1.2482 -      }
  1.2483      }
  1.2484 -  }
  1.2485 -
  1.2486 -  if (HAVE_MORECORE && tbase == CMFAIL) { /* Try noncontiguous MORECORE */
  1.2487 -    size_t asize = granularity_align(nb + TOP_FOOT_SIZE + SIZE_T_ONE);
  1.2488 -    if (asize < HALF_MAX_SIZE_T) {
  1.2489 -      char* br = CMFAIL;
  1.2490 -      char* end = CMFAIL;
  1.2491 -      ACQUIRE_MORECORE_LOCK();
  1.2492 -      br = (char*)(CALL_MORECORE(asize));
  1.2493 -      end = (char*)(CALL_MORECORE(0));
  1.2494 -      RELEASE_MORECORE_LOCK();
  1.2495 -      if (br != CMFAIL && end != CMFAIL && br < end) {
  1.2496 -        size_t ssize = end - br;
  1.2497 -        if (ssize > nb + TOP_FOOT_SIZE) {
  1.2498 -          tbase = br;
  1.2499 -          tsize = ssize;
  1.2500 +
  1.2501 +    if (HAVE_MORECORE && tbase == CMFAIL) {     /* Try noncontiguous MORECORE */
  1.2502 +        size_t asize = granularity_align (nb + TOP_FOOT_SIZE + SIZE_T_ONE);
  1.2503 +        if (asize < HALF_MAX_SIZE_T) {
  1.2504 +            char *br = CMFAIL;
  1.2505 +            char *end = CMFAIL;
  1.2506 +            ACQUIRE_MORECORE_LOCK ();
  1.2507 +            br = (char *) (CALL_MORECORE (asize));
  1.2508 +            end = (char *) (CALL_MORECORE (0));
  1.2509 +            RELEASE_MORECORE_LOCK ();
  1.2510 +            if (br != CMFAIL && end != CMFAIL && br < end) {
  1.2511 +                size_t ssize = end - br;
  1.2512 +                if (ssize > nb + TOP_FOOT_SIZE) {
  1.2513 +                    tbase = br;
  1.2514 +                    tsize = ssize;
  1.2515 +                }
  1.2516 +            }
  1.2517          }
  1.2518 -      }
  1.2519 -    }
  1.2520 -  }
  1.2521 -
  1.2522 -  if (tbase != CMFAIL) {
  1.2523 -
  1.2524 -    if ((m->footprint += tsize) > m->max_footprint)
  1.2525 -      m->max_footprint = m->footprint;
  1.2526 -
  1.2527 -    if (!is_initialized(m)) { /* first-time initialization */
  1.2528 -      m->seg.base = m->least_addr = tbase;
  1.2529 -      m->seg.size = tsize;
  1.2530 -      m->seg.sflags = mmap_flag;
  1.2531 -      m->magic = mparams.magic;
  1.2532 -      init_bins(m);
  1.2533 -      if (is_global(m)) 
  1.2534 -        init_top(m, (mchunkptr)tbase, tsize - TOP_FOOT_SIZE);
  1.2535 -      else {
  1.2536 -        /* Offset top by embedded malloc_state */
  1.2537 -        mchunkptr mn = next_chunk(mem2chunk(m));
  1.2538 -        init_top(m, mn, (size_t)((tbase + tsize) - (char*)mn) -TOP_FOOT_SIZE);
  1.2539 -      }
  1.2540      }
  1.2541  
  1.2542 -    else {
  1.2543 -      /* Try to merge with an existing segment */
  1.2544 -      msegmentptr sp = &m->seg;
  1.2545 -      while (sp != 0 && tbase != sp->base + sp->size)
  1.2546 -        sp = sp->next;
  1.2547 -      if (sp != 0 &&
  1.2548 -          !is_extern_segment(sp) &&
  1.2549 -          (sp->sflags & IS_MMAPPED_BIT) == mmap_flag &&
  1.2550 -          segment_holds(sp, m->top)) { /* append */
  1.2551 -        sp->size += tsize;
  1.2552 -        init_top(m, m->top, m->topsize + tsize);
  1.2553 -      }
  1.2554 -      else {
  1.2555 -        if (tbase < m->least_addr)
  1.2556 -          m->least_addr = tbase;
  1.2557 -        sp = &m->seg;
  1.2558 -        while (sp != 0 && sp->base != tbase + tsize)
  1.2559 -          sp = sp->next;
  1.2560 -        if (sp != 0 &&
  1.2561 -            !is_extern_segment(sp) &&
  1.2562 -            (sp->sflags & IS_MMAPPED_BIT) == mmap_flag) {
  1.2563 -          char* oldbase = sp->base;
  1.2564 -          sp->base = tbase;
  1.2565 -          sp->size += tsize;
  1.2566 -          return prepend_alloc(m, tbase, oldbase, nb);
  1.2567 +    if (tbase != CMFAIL) {
  1.2568 +
  1.2569 +        if ((m->footprint += tsize) > m->max_footprint)
  1.2570 +            m->max_footprint = m->footprint;
  1.2571 +
  1.2572 +        if (!is_initialized (m)) {      /* first-time initialization */
  1.2573 +            m->seg.base = m->least_addr = tbase;
  1.2574 +            m->seg.size = tsize;
  1.2575 +            m->seg.sflags = mmap_flag;
  1.2576 +            m->magic = mparams.magic;
  1.2577 +            init_bins (m);
  1.2578 +            if (is_global (m))
  1.2579 +                init_top (m, (mchunkptr) tbase, tsize - TOP_FOOT_SIZE);
  1.2580 +            else {
  1.2581 +                /* Offset top by embedded malloc_state */
  1.2582 +                mchunkptr mn = next_chunk (mem2chunk (m));
  1.2583 +                init_top (m, mn,
  1.2584 +                          (size_t) ((tbase + tsize) - (char *) mn) -
  1.2585 +                          TOP_FOOT_SIZE);
  1.2586 +            }
  1.2587          }
  1.2588 -        else
  1.2589 -          add_segment(m, tbase, tsize, mmap_flag);
  1.2590 -      }
  1.2591 +
  1.2592 +        else {
  1.2593 +            /* Try to merge with an existing segment */
  1.2594 +            msegmentptr sp = &m->seg;
  1.2595 +            while (sp != 0 && tbase != sp->base + sp->size)
  1.2596 +                sp = sp->next;
  1.2597 +            if (sp != 0 && !is_extern_segment (sp) && (sp->sflags & IS_MMAPPED_BIT) == mmap_flag && segment_holds (sp, m->top)) {       /* append */
  1.2598 +                sp->size += tsize;
  1.2599 +                init_top (m, m->top, m->topsize + tsize);
  1.2600 +            } else {
  1.2601 +                if (tbase < m->least_addr)
  1.2602 +                    m->least_addr = tbase;
  1.2603 +                sp = &m->seg;
  1.2604 +                while (sp != 0 && sp->base != tbase + tsize)
  1.2605 +                    sp = sp->next;
  1.2606 +                if (sp != 0 &&
  1.2607 +                    !is_extern_segment (sp) &&
  1.2608 +                    (sp->sflags & IS_MMAPPED_BIT) == mmap_flag) {
  1.2609 +                    char *oldbase = sp->base;
  1.2610 +                    sp->base = tbase;
  1.2611 +                    sp->size += tsize;
  1.2612 +                    return prepend_alloc (m, tbase, oldbase, nb);
  1.2613 +                } else
  1.2614 +                    add_segment (m, tbase, tsize, mmap_flag);
  1.2615 +            }
  1.2616 +        }
  1.2617 +
  1.2618 +        if (nb < m->topsize) {  /* Allocate from new or extended top space */
  1.2619 +            size_t rsize = m->topsize -= nb;
  1.2620 +            mchunkptr p = m->top;
  1.2621 +            mchunkptr r = m->top = chunk_plus_offset (p, nb);
  1.2622 +            r->head = rsize | PINUSE_BIT;
  1.2623 +            set_size_and_pinuse_of_inuse_chunk (m, p, nb);
  1.2624 +            check_top_chunk (m, m->top);
  1.2625 +            check_malloced_chunk (m, chunk2mem (p), nb);
  1.2626 +            return chunk2mem (p);
  1.2627 +        }
  1.2628      }
  1.2629  
  1.2630 -    if (nb < m->topsize) { /* Allocate from new or extended top space */
  1.2631 -      size_t rsize = m->topsize -= nb;
  1.2632 -      mchunkptr p = m->top;
  1.2633 -      mchunkptr r = m->top = chunk_plus_offset(p, nb);
  1.2634 -      r->head = rsize | PINUSE_BIT;
  1.2635 -      set_size_and_pinuse_of_inuse_chunk(m, p, nb);
  1.2636 -      check_top_chunk(m, m->top);
  1.2637 -      check_malloced_chunk(m, chunk2mem(p), nb);
  1.2638 -      return chunk2mem(p);
  1.2639 -    }
  1.2640 -  }
  1.2641 -
  1.2642 -  MALLOC_FAILURE_ACTION;
  1.2643 -  return 0;
  1.2644 +    MALLOC_FAILURE_ACTION;
  1.2645 +    return 0;
  1.2646  }
  1.2647  
  1.2648  /* -----------------------  system deallocation -------------------------- */
  1.2649  
  1.2650  /* Unmap and unlink any mmapped segments that don't contain used chunks */
  1.2651 -static size_t release_unused_segments(mstate m) {
  1.2652 -  size_t released = 0;
  1.2653 -  msegmentptr pred = &m->seg;
  1.2654 -  msegmentptr sp = pred->next;
  1.2655 -  while (sp != 0) {
  1.2656 -    char* base = sp->base;
  1.2657 -    size_t size = sp->size;
  1.2658 -    msegmentptr next = sp->next;
  1.2659 -    if (is_mmapped_segment(sp) && !is_extern_segment(sp)) {
  1.2660 -      mchunkptr p = align_as_chunk(base);
  1.2661 -      size_t psize = chunksize(p);
  1.2662 -      /* Can unmap if first chunk holds entire segment and not pinned */
  1.2663 -      if (!cinuse(p) && (char*)p + psize >= base + size - TOP_FOOT_SIZE) {
  1.2664 -        tchunkptr tp = (tchunkptr)p;
  1.2665 -        assert(segment_holds(sp, (char*)sp));
  1.2666 -        if (p == m->dv) {
  1.2667 -          m->dv = 0;
  1.2668 -          m->dvsize = 0;
  1.2669 +static size_t
  1.2670 +release_unused_segments (mstate m)
  1.2671 +{
  1.2672 +    size_t released = 0;
  1.2673 +    msegmentptr pred = &m->seg;
  1.2674 +    msegmentptr sp = pred->next;
  1.2675 +    while (sp != 0) {
  1.2676 +        char *base = sp->base;
  1.2677 +        size_t size = sp->size;
  1.2678 +        msegmentptr next = sp->next;
  1.2679 +        if (is_mmapped_segment (sp) && !is_extern_segment (sp)) {
  1.2680 +            mchunkptr p = align_as_chunk (base);
  1.2681 +            size_t psize = chunksize (p);
  1.2682 +            /* Can unmap if first chunk holds entire segment and not pinned */
  1.2683 +            if (!cinuse (p)
  1.2684 +                && (char *) p + psize >= base + size - TOP_FOOT_SIZE) {
  1.2685 +                tchunkptr tp = (tchunkptr) p;
  1.2686 +                assert (segment_holds (sp, (char *) sp));
  1.2687 +                if (p == m->dv) {
  1.2688 +                    m->dv = 0;
  1.2689 +                    m->dvsize = 0;
  1.2690 +                } else {
  1.2691 +                    unlink_large_chunk (m, tp);
  1.2692 +                }
  1.2693 +                if (CALL_MUNMAP (base, size) == 0) {
  1.2694 +                    released += size;
  1.2695 +                    m->footprint -= size;
  1.2696 +                    /* unlink obsoleted record */
  1.2697 +                    sp = pred;
  1.2698 +                    sp->next = next;
  1.2699 +                } else {        /* back out if cannot unmap */
  1.2700 +                    insert_large_chunk (m, tp, psize);
  1.2701 +                }
  1.2702 +            }
  1.2703          }
  1.2704 -        else {
  1.2705 -          unlink_large_chunk(m, tp);
  1.2706 -        }
  1.2707 -        if (CALL_MUNMAP(base, size) == 0) {
  1.2708 -          released += size;
  1.2709 -          m->footprint -= size;
  1.2710 -          /* unlink obsoleted record */
  1.2711 -          sp = pred;
  1.2712 -          sp->next = next;
  1.2713 -        }
  1.2714 -        else { /* back out if cannot unmap */
  1.2715 -          insert_large_chunk(m, tp, psize);
  1.2716 -        }
  1.2717 -      }
  1.2718 +        pred = sp;
  1.2719 +        sp = next;
  1.2720      }
  1.2721 -    pred = sp;
  1.2722 -    sp = next;
  1.2723 -  }
  1.2724 -  return released;
  1.2725 +    return released;
  1.2726  }
  1.2727  
  1.2728 -static int sys_trim(mstate m, size_t pad) {
  1.2729 -  size_t released = 0;
  1.2730 -  if (pad < MAX_REQUEST && is_initialized(m)) {
  1.2731 -    pad += TOP_FOOT_SIZE; /* ensure enough room for segment overhead */
  1.2732 -
  1.2733 -    if (m->topsize > pad) {
  1.2734 -      /* Shrink top space in granularity-size units, keeping at least one */
  1.2735 -      size_t unit = mparams.granularity;
  1.2736 -      size_t extra = ((m->topsize - pad + (unit - SIZE_T_ONE)) / unit -
  1.2737 -                      SIZE_T_ONE) * unit;
  1.2738 -      msegmentptr sp = segment_holding(m, (char*)m->top);
  1.2739 -
  1.2740 -      if (!is_extern_segment(sp)) {
  1.2741 -        if (is_mmapped_segment(sp)) {
  1.2742 -          if (HAVE_MMAP &&
  1.2743 -              sp->size >= extra &&
  1.2744 -              !has_segment_link(m, sp)) { /* can't shrink if pinned */
  1.2745 -            size_t newsize = sp->size - extra;
  1.2746 -            /* Prefer mremap, fall back to munmap */
  1.2747 -            if ((CALL_MREMAP(sp->base, sp->size, newsize, 0) != MFAIL) ||
  1.2748 -                (CALL_MUNMAP(sp->base + newsize, extra) == 0)) {
  1.2749 -              released = extra;
  1.2750 +static int
  1.2751 +sys_trim (mstate m, size_t pad)
  1.2752 +{
  1.2753 +    size_t released = 0;
  1.2754 +    if (pad < MAX_REQUEST && is_initialized (m)) {
  1.2755 +        pad += TOP_FOOT_SIZE;   /* ensure enough room for segment overhead */
  1.2756 +
  1.2757 +        if (m->topsize > pad) {
  1.2758 +            /* Shrink top space in granularity-size units, keeping at least one */
  1.2759 +            size_t unit = mparams.granularity;
  1.2760 +            size_t extra =
  1.2761 +                ((m->topsize - pad + (unit - SIZE_T_ONE)) / unit -
  1.2762 +                 SIZE_T_ONE) * unit;
  1.2763 +            msegmentptr sp = segment_holding (m, (char *) m->top);
  1.2764 +
  1.2765 +            if (!is_extern_segment (sp)) {
  1.2766 +                if (is_mmapped_segment (sp)) {
  1.2767 +                    if (HAVE_MMAP && sp->size >= extra && !has_segment_link (m, sp)) {  /* can't shrink if pinned */
  1.2768 +                        size_t newsize = sp->size - extra;
  1.2769 +                        /* Prefer mremap, fall back to munmap */
  1.2770 +                        if ((CALL_MREMAP
  1.2771 +                             (sp->base, sp->size, newsize, 0) != MFAIL)
  1.2772 +                            || (CALL_MUNMAP (sp->base + newsize, extra)
  1.2773 +                                == 0)) {
  1.2774 +                            released = extra;
  1.2775 +                        }
  1.2776 +                    }
  1.2777 +                } else if (HAVE_MORECORE) {
  1.2778 +                    if (extra >= HALF_MAX_SIZE_T)       /* Avoid wrapping negative */
  1.2779 +                        extra = (HALF_MAX_SIZE_T) + SIZE_T_ONE - unit;
  1.2780 +                    ACQUIRE_MORECORE_LOCK ();
  1.2781 +                    {
  1.2782 +                        /* Make sure end of memory is where we last set it. */
  1.2783 +                        char *old_br = (char *) (CALL_MORECORE (0));
  1.2784 +                        if (old_br == sp->base + sp->size) {
  1.2785 +                            char *rel_br = (char *) (CALL_MORECORE (-extra));
  1.2786 +                            char *new_br = (char *) (CALL_MORECORE (0));
  1.2787 +                            if (rel_br != CMFAIL && new_br < old_br)
  1.2788 +                                released = old_br - new_br;
  1.2789 +                        }
  1.2790 +                    }
  1.2791 +                    RELEASE_MORECORE_LOCK ();
  1.2792 +                }
  1.2793              }
  1.2794 -          }
  1.2795 +
  1.2796 +            if (released != 0) {
  1.2797 +                sp->size -= released;
  1.2798 +                m->footprint -= released;
  1.2799 +                init_top (m, m->top, m->topsize - released);
  1.2800 +                check_top_chunk (m, m->top);
  1.2801 +            }
  1.2802          }
  1.2803 -        else if (HAVE_MORECORE) {
  1.2804 -          if (extra >= HALF_MAX_SIZE_T) /* Avoid wrapping negative */
  1.2805 -            extra = (HALF_MAX_SIZE_T) + SIZE_T_ONE - unit;
  1.2806 -          ACQUIRE_MORECORE_LOCK();
  1.2807 -          {
  1.2808 -            /* Make sure end of memory is where we last set it. */
  1.2809 -            char* old_br = (char*)(CALL_MORECORE(0));
  1.2810 -            if (old_br == sp->base + sp->size) {
  1.2811 -              char* rel_br = (char*)(CALL_MORECORE(-extra));
  1.2812 -              char* new_br = (char*)(CALL_MORECORE(0));
  1.2813 -              if (rel_br != CMFAIL && new_br < old_br)
  1.2814 -                released = old_br - new_br;
  1.2815 -            }
  1.2816 -          }
  1.2817 -          RELEASE_MORECORE_LOCK();
  1.2818 -        }
  1.2819 -      }
  1.2820 -
  1.2821 -      if (released != 0) {
  1.2822 -        sp->size -= released;
  1.2823 -        m->footprint -= released;
  1.2824 -        init_top(m, m->top, m->topsize - released);
  1.2825 -        check_top_chunk(m, m->top);
  1.2826 -      }
  1.2827 +
  1.2828 +        /* Unmap any unused mmapped segments */
  1.2829 +        if (HAVE_MMAP)
  1.2830 +            released += release_unused_segments (m);
  1.2831 +
  1.2832 +        /* On failure, disable autotrim to avoid repeated failed future calls */
  1.2833 +        if (released == 0)
  1.2834 +            m->trim_check = MAX_SIZE_T;
  1.2835      }
  1.2836  
  1.2837 -    /* Unmap any unused mmapped segments */
  1.2838 -    if (HAVE_MMAP) 
  1.2839 -      released += release_unused_segments(m);
  1.2840 -
  1.2841 -    /* On failure, disable autotrim to avoid repeated failed future calls */
  1.2842 -    if (released == 0)
  1.2843 -      m->trim_check = MAX_SIZE_T;
  1.2844 -  }
  1.2845 -
  1.2846 -  return (released != 0)? 1 : 0;
  1.2847 +    return (released != 0) ? 1 : 0;
  1.2848  }
  1.2849  
  1.2850  /* ---------------------------- malloc support --------------------------- */
  1.2851  
  1.2852  /* allocate a large request from the best fitting chunk in a treebin */
  1.2853 -static void* tmalloc_large(mstate m, size_t nb) {
  1.2854 -  tchunkptr v = 0;
  1.2855 -  size_t rsize = -nb; /* Unsigned negation */
  1.2856 -  tchunkptr t;
  1.2857 -  bindex_t idx;
  1.2858 -  compute_tree_index(nb, idx);
  1.2859 -
  1.2860 -  if ((t = *treebin_at(m, idx)) != 0) {
  1.2861 -    /* Traverse tree for this bin looking for node with size == nb */
  1.2862 -    size_t sizebits = nb << leftshift_for_tree_index(idx);
  1.2863 -    tchunkptr rst = 0;  /* The deepest untaken right subtree */
  1.2864 -    for (;;) {
  1.2865 -      tchunkptr rt;
  1.2866 -      size_t trem = chunksize(t) - nb;
  1.2867 -      if (trem < rsize) {
  1.2868 -        v = t;
  1.2869 -        if ((rsize = trem) == 0)
  1.2870 -          break;
  1.2871 -      }
  1.2872 -      rt = t->child[1];
  1.2873 -      t = t->child[(sizebits >> (SIZE_T_BITSIZE-SIZE_T_ONE)) & 1];
  1.2874 -      if (rt != 0 && rt != t)
  1.2875 -        rst = rt;
  1.2876 -      if (t == 0) {
  1.2877 -        t = rst; /* set t to least subtree holding sizes > nb */
  1.2878 -        break;
  1.2879 -      }
  1.2880 -      sizebits <<= 1;
  1.2881 +static void *
  1.2882 +tmalloc_large (mstate m, size_t nb)
  1.2883 +{
  1.2884 +    tchunkptr v = 0;
  1.2885 +    size_t rsize = -nb;         /* Unsigned negation */
  1.2886 +    tchunkptr t;
  1.2887 +    bindex_t idx;
  1.2888 +    compute_tree_index (nb, idx);
  1.2889 +
  1.2890 +    if ((t = *treebin_at (m, idx)) != 0) {
  1.2891 +        /* Traverse tree for this bin looking for node with size == nb */
  1.2892 +        size_t sizebits = nb << leftshift_for_tree_index (idx);
  1.2893 +        tchunkptr rst = 0;      /* The deepest untaken right subtree */
  1.2894 +        for (;;) {
  1.2895 +            tchunkptr rt;
  1.2896 +            size_t trem = chunksize (t) - nb;
  1.2897 +            if (trem < rsize) {
  1.2898 +                v = t;
  1.2899 +                if ((rsize = trem) == 0)
  1.2900 +                    break;
  1.2901 +            }
  1.2902 +            rt = t->child[1];
  1.2903 +            t = t->child[(sizebits >> (SIZE_T_BITSIZE - SIZE_T_ONE)) & 1];
  1.2904 +            if (rt != 0 && rt != t)
  1.2905 +                rst = rt;
  1.2906 +            if (t == 0) {
  1.2907 +                t = rst;        /* set t to least subtree holding sizes > nb */
  1.2908 +                break;
  1.2909 +            }
  1.2910 +            sizebits <<= 1;
  1.2911 +        }
  1.2912      }
  1.2913 -  }
  1.2914 -
  1.2915 -  if (t == 0 && v == 0) { /* set t to root of next non-empty treebin */
  1.2916 -    binmap_t leftbits = left_bits(idx2bit(idx)) & m->treemap;
  1.2917 -    if (leftbits != 0) {
  1.2918 -      bindex_t i;
  1.2919 -      binmap_t leastbit = least_bit(leftbits);
  1.2920 -      compute_bit2idx(leastbit, i);
  1.2921 -      t = *treebin_at(m, i);
  1.2922 +
  1.2923 +    if (t == 0 && v == 0) {     /* set t to root of next non-empty treebin */
  1.2924 +        binmap_t leftbits = left_bits (idx2bit (idx)) & m->treemap;
  1.2925 +        if (leftbits != 0) {
  1.2926 +            bindex_t i;
  1.2927 +            binmap_t leastbit = least_bit (leftbits);
  1.2928 +            compute_bit2idx (leastbit, i);
  1.2929 +            t = *treebin_at (m, i);
  1.2930 +        }
  1.2931      }
  1.2932 -  }
  1.2933 -
  1.2934 -  while (t != 0) { /* find smallest of tree or subtree */
  1.2935 -    size_t trem = chunksize(t) - nb;
  1.2936 -    if (trem < rsize) {
  1.2937 -      rsize = trem;
  1.2938 -      v = t;
  1.2939 +
  1.2940 +    while (t != 0) {            /* find smallest of tree or subtree */
  1.2941 +        size_t trem = chunksize (t) - nb;
  1.2942 +        if (trem < rsize) {
  1.2943 +            rsize = trem;
  1.2944 +            v = t;
  1.2945 +        }
  1.2946 +        t = leftmost_child (t);
  1.2947      }
  1.2948 -    t = leftmost_child(t);
  1.2949 -  }
  1.2950 -
  1.2951 -  /*  If dv is a better fit, return 0 so malloc will use it */
  1.2952 -  if (v != 0 && rsize < (size_t)(m->dvsize - nb)) {
  1.2953 -    if (RTCHECK(ok_address(m, v))) { /* split */
  1.2954 -      mchunkptr r = chunk_plus_offset(v, nb);
  1.2955 -      assert(chunksize(v) == rsize + nb);
  1.2956 -      if (RTCHECK(ok_next(v, r))) {
  1.2957 -        unlink_large_chunk(m, v);
  1.2958 -        if (rsize < MIN_CHUNK_SIZE)
  1.2959 -          set_inuse_and_pinuse(m, v, (rsize + nb));
  1.2960 -        else {
  1.2961 -          set_size_and_pinuse_of_inuse_chunk(m, v, nb);
  1.2962 -          set_size_and_pinuse_of_free_chunk(r, rsize);
  1.2963 -          insert_chunk(m, r, rsize);
  1.2964 +
  1.2965 +    /*  If dv is a better fit, return 0 so malloc will use it */
  1.2966 +    if (v != 0 && rsize < (size_t) (m->dvsize - nb)) {
  1.2967 +        if (RTCHECK (ok_address (m, v))) {      /* split */
  1.2968 +            mchunkptr r = chunk_plus_offset (v, nb);
  1.2969 +            assert (chunksize (v) == rsize + nb);
  1.2970 +            if (RTCHECK (ok_next (v, r))) {
  1.2971 +                unlink_large_chunk (m, v);
  1.2972 +                if (rsize < MIN_CHUNK_SIZE)
  1.2973 +                    set_inuse_and_pinuse (m, v, (rsize + nb));
  1.2974 +                else {
  1.2975 +                    set_size_and_pinuse_of_inuse_chunk (m, v, nb);
  1.2976 +                    set_size_and_pinuse_of_free_chunk (r, rsize);
  1.2977 +                    insert_chunk (m, r, rsize);
  1.2978 +                }
  1.2979 +                return chunk2mem (v);
  1.2980 +            }
  1.2981          }
  1.2982 -        return chunk2mem(v);
  1.2983 -      }
  1.2984 +        CORRUPTION_ERROR_ACTION (m);
  1.2985      }
  1.2986 -    CORRUPTION_ERROR_ACTION(m);
  1.2987 -  }
  1.2988 -  return 0;
  1.2989 +    return 0;
  1.2990  }
  1.2991  
  1.2992  /* allocate a small request from the best fitting chunk in a treebin */
  1.2993 -static void* tmalloc_small(mstate m, size_t nb) {
  1.2994 -  tchunkptr t, v;
  1.2995 -  size_t rsize;
  1.2996 -  bindex_t i;
  1.2997 -  binmap_t leastbit = least_bit(m->treemap);
  1.2998 -  compute_bit2idx(leastbit, i);
  1.2999 -
  1.3000 -  v = t = *treebin_at(m, i);
  1.3001 -  rsize = chunksize(t) - nb;
  1.3002 -
  1.3003 -  while ((t = leftmost_child(t)) != 0) {
  1.3004 -    size_t trem = chunksize(t) - nb;
  1.3005 -    if (trem < rsize) {
  1.3006 -      rsize = trem;
  1.3007 -      v = t;
  1.3008 +static void *
  1.3009 +tmalloc_small (mstate m, size_t nb)
  1.3010 +{
  1.3011 +    tchunkptr t, v;
  1.3012 +    size_t rsize;
  1.3013 +    bindex_t i;
  1.3014 +    binmap_t leastbit = least_bit (m->treemap);
  1.3015 +    compute_bit2idx (leastbit, i);
  1.3016 +
  1.3017 +    v = t = *treebin_at (m, i);
  1.3018 +    rsize = chunksize (t) - nb;
  1.3019 +
  1.3020 +    while ((t = leftmost_child (t)) != 0) {
  1.3021 +        size_t trem = chunksize (t) - nb;
  1.3022 +        if (trem < rsize) {
  1.3023 +            rsize = trem;
  1.3024 +            v = t;
  1.3025 +        }
  1.3026      }
  1.3027 -  }
  1.3028 -
  1.3029 -  if (RTCHECK(ok_address(m, v))) {
  1.3030 -    mchunkptr r = chunk_plus_offset(v, nb);
  1.3031 -    assert(chunksize(v) == rsize + nb);
  1.3032 -    if (RTCHECK(ok_next(v, r))) {
  1.3033 -      unlink_large_chunk(m, v);
  1.3034 -      if (rsize < MIN_CHUNK_SIZE)
  1.3035 -        set_inuse_and_pinuse(m, v, (rsize + nb));
  1.3036 -      else {
  1.3037 -        set_size_and_pinuse_of_inuse_chunk(m, v, nb);
  1.3038 -        set_size_and_pinuse_of_free_chunk(r, rsize);
  1.3039 -        replace_dv(m, r, rsize);
  1.3040 -      }
  1.3041 -      return chunk2mem(v);
  1.3042 +
  1.3043 +    if (RTCHECK (ok_address (m, v))) {
  1.3044 +        mchunkptr r = chunk_plus_offset (v, nb);
  1.3045 +        assert (chunksize (v) == rsize + nb);
  1.3046 +        if (RTCHECK (ok_next (v, r))) {
  1.3047 +            unlink_large_chunk (m, v);
  1.3048 +            if (rsize < MIN_CHUNK_SIZE)
  1.3049 +                set_inuse_and_pinuse (m, v, (rsize + nb));
  1.3050 +            else {
  1.3051 +                set_size_and_pinuse_of_inuse_chunk (m, v, nb);
  1.3052 +                set_size_and_pinuse_of_free_chunk (r, rsize);
  1.3053 +                replace_dv (m, r, rsize);
  1.3054 +            }
  1.3055 +            return chunk2mem (v);
  1.3056 +        }
  1.3057      }
  1.3058 -  }
  1.3059 -
  1.3060 -  CORRUPTION_ERROR_ACTION(m);
  1.3061 -  return 0;
  1.3062 +
  1.3063 +    CORRUPTION_ERROR_ACTION (m);
  1.3064 +    return 0;
  1.3065  }
  1.3066  
  1.3067  /* --------------------------- realloc support --------------------------- */
  1.3068  
  1.3069 -static void* internal_realloc(mstate m, void* oldmem, size_t bytes) {
  1.3070 -  if (bytes >= MAX_REQUEST) {
  1.3071 -    MALLOC_FAILURE_ACTION;
  1.3072 -    return 0;
  1.3073 -  }
  1.3074 -  if (!PREACTION(m)) {
  1.3075 -    mchunkptr oldp = mem2chunk(oldmem);
  1.3076 -    size_t oldsize = chunksize(oldp);
  1.3077 -    mchunkptr next = chunk_plus_offset(oldp, oldsize);
  1.3078 -    mchunkptr newp = 0;
  1.3079 -    void* extra = 0;
  1.3080 -
  1.3081 -    /* Try to either shrink or extend into top. Else malloc-copy-free */
  1.3082 -
  1.3083 -    if (RTCHECK(ok_address(m, oldp) && ok_cinuse(oldp) &&
  1.3084 -                ok_next(oldp, next) && ok_pinuse(next))) {
  1.3085 -      size_t nb = request2size(bytes);
  1.3086 -      if (is_mmapped(oldp))
  1.3087 -        newp = mmap_resize(m, oldp, nb);
  1.3088 -      else if (oldsize >= nb) { /* already big enough */
  1.3089 -        size_t rsize = oldsize - nb;
  1.3090 -        newp = oldp;
  1.3091 -        if (rsize >= MIN_CHUNK_SIZE) {
  1.3092 -          mchunkptr remainder = chunk_plus_offset(newp, nb);
  1.3093 -          set_inuse(m, newp, nb);
  1.3094 -          set_inuse(m, remainder, rsize);
  1.3095 -          extra = chunk2mem(remainder);
  1.3096 +static void *
  1.3097 +internal_realloc (mstate m, void *oldmem, size_t bytes)
  1.3098 +{
  1.3099 +    if (bytes >= MAX_REQUEST) {
  1.3100 +        MALLOC_FAILURE_ACTION;
  1.3101 +        return 0;
  1.3102 +    }
  1.3103 +    if (!PREACTION (m)) {
  1.3104 +        mchunkptr oldp = mem2chunk (oldmem);
  1.3105 +        size_t oldsize = chunksize (oldp);
  1.3106 +        mchunkptr next = chunk_plus_offset (oldp, oldsize);
  1.3107 +        mchunkptr newp = 0;
  1.3108 +        void *extra = 0;
  1.3109 +
  1.3110 +        /* Try to either shrink or extend into top. Else malloc-copy-free */
  1.3111 +
  1.3112 +        if (RTCHECK (ok_address (m, oldp) && ok_cinuse (oldp) &&
  1.3113 +                     ok_next (oldp, next) && ok_pinuse (next))) {
  1.3114 +            size_t nb = request2size (bytes);
  1.3115 +            if (is_mmapped (oldp))
  1.3116 +                newp = mmap_resize (m, oldp, nb);
  1.3117 +            else if (oldsize >= nb) {   /* already big enough */
  1.3118 +                size_t rsize = oldsize - nb;
  1.3119 +                newp = oldp;
  1.3120 +                if (rsize >= MIN_CHUNK_SIZE) {
  1.3121 +                    mchunkptr remainder = chunk_plus_offset (newp, nb);
  1.3122 +                    set_inuse (m, newp, nb);
  1.3123 +                    set_inuse (m, remainder, rsize);
  1.3124 +                    extra = chunk2mem (remainder);
  1.3125 +                }
  1.3126 +            } else if (next == m->top && oldsize + m->topsize > nb) {
  1.3127 +                /* Expand into top */
  1.3128 +                size_t newsize = oldsize + m->topsize;
  1.3129 +                size_t newtopsize = newsize - nb;
  1.3130 +                mchunkptr newtop = chunk_plus_offset (oldp, nb);
  1.3131 +                set_inuse (m, oldp, nb);
  1.3132 +                newtop->head = newtopsize | PINUSE_BIT;
  1.3133 +                m->top = newtop;
  1.3134 +                m->topsize = newtopsize;
  1.3135 +                newp = oldp;
  1.3136 +            }
  1.3137 +        } else {
  1.3138 +            USAGE_ERROR_ACTION (m, oldmem);
  1.3139 +            POSTACTION (m);
  1.3140 +            return 0;
  1.3141          }
  1.3142 -      }
  1.3143 -      else if (next == m->top && oldsize + m->topsize > nb) {
  1.3144 -        /* Expand into top */
  1.3145 -        size_t newsize = oldsize + m->topsize;
  1.3146 -        size_t newtopsize = newsize - nb;
  1.3147 -        mchunkptr newtop = chunk_plus_offset(oldp, nb);
  1.3148 -        set_inuse(m, oldp, nb);
  1.3149 -        newtop->head = newtopsize |PINUSE_BIT;
  1.3150 -        m->top = newtop;
  1.3151 -        m->topsize = newtopsize;
  1.3152 -        newp = oldp;
  1.3153 -      }
  1.3154 -    }
  1.3155 -    else {
  1.3156 -      USAGE_ERROR_ACTION(m, oldmem);
  1.3157 -      POSTACTION(m);
  1.3158 -      return 0;
  1.3159 +
  1.3160 +        POSTACTION (m);
  1.3161 +
  1.3162 +        if (newp != 0) {
  1.3163 +            if (extra != 0) {
  1.3164 +                internal_free (m, extra);
  1.3165 +            }
  1.3166 +            check_inuse_chunk (m, newp);
  1.3167 +            return chunk2mem (newp);
  1.3168 +        } else {
  1.3169 +            void *newmem = internal_malloc (m, bytes);
  1.3170 +            if (newmem != 0) {
  1.3171 +                size_t oc = oldsize - overhead_for (oldp);
  1.3172 +                memcpy (newmem, oldmem, (oc < bytes) ? oc : bytes);
  1.3173 +                internal_free (m, oldmem);
  1.3174 +            }
  1.3175 +            return newmem;
  1.3176 +        }
  1.3177      }
  1.3178 -
  1.3179 -    POSTACTION(m);
  1.3180 -
  1.3181 -    if (newp != 0) {
  1.3182 -      if (extra != 0) {
  1.3183 -        internal_free(m, extra);
  1.3184 -      }
  1.3185 -      check_inuse_chunk(m, newp);
  1.3186 -      return chunk2mem(newp);
  1.3187 -    }
  1.3188 -    else {
  1.3189 -      void* newmem = internal_malloc(m, bytes);
  1.3190 -      if (newmem != 0) {
  1.3191 -        size_t oc = oldsize - overhead_for(oldp);
  1.3192 -        memcpy(newmem, oldmem, (oc < bytes)? oc : bytes);
  1.3193 -        internal_free(m, oldmem);
  1.3194 -      }
  1.3195 -      return newmem;
  1.3196 -    }
  1.3197 -  }
  1.3198 -  return 0;
  1.3199 +    return 0;
  1.3200  }
  1.3201  
  1.3202  /* --------------------------- memalign support -------------------------- */
  1.3203  
  1.3204 -static void* internal_memalign(mstate m, size_t alignment, size_t bytes) {
  1.3205 -  if (alignment <= MALLOC_ALIGNMENT)    /* Can just use malloc */
  1.3206 -    return internal_malloc(m, bytes);
  1.3207 -  if (alignment <  MIN_CHUNK_SIZE) /* must be at least a minimum chunk size */
  1.3208 -    alignment = MIN_CHUNK_SIZE;
  1.3209 -  if ((alignment & (alignment-SIZE_T_ONE)) != 0) {/* Ensure a power of 2 */
  1.3210 -    size_t a = MALLOC_ALIGNMENT << 1;
  1.3211 -    while (a < alignment) a <<= 1;
  1.3212 -    alignment = a;
  1.3213 -  }
  1.3214 -  
  1.3215 -  if (bytes >= MAX_REQUEST - alignment) {
  1.3216 -    if (m != 0)  { /* Test isn't needed but avoids compiler warning */
  1.3217 -      MALLOC_FAILURE_ACTION;
  1.3218 +static void *
  1.3219 +internal_memalign (mstate m, size_t alignment, size_t bytes)
  1.3220 +{
  1.3221 +    if (alignment <= MALLOC_ALIGNMENT)  /* Can just use malloc */
  1.3222 +        return internal_malloc (m, bytes);
  1.3223 +    if (alignment < MIN_CHUNK_SIZE)     /* must be at least a minimum chunk size */
  1.3224 +        alignment = MIN_CHUNK_SIZE;
  1.3225 +    if ((alignment & (alignment - SIZE_T_ONE)) != 0) {  /* Ensure a power of 2 */
  1.3226 +        size_t a = MALLOC_ALIGNMENT << 1;
  1.3227 +        while (a < alignment)
  1.3228 +            a <<= 1;
  1.3229 +        alignment = a;
  1.3230      }
  1.3231 -  }
  1.3232 -  else {
  1.3233 -    size_t nb = request2size(bytes);
  1.3234 -    size_t req = nb + alignment + MIN_CHUNK_SIZE - CHUNK_OVERHEAD;
  1.3235 -    char* mem = (char*)internal_malloc(m, req);
  1.3236 -    if (mem != 0) {
  1.3237 -      void* leader = 0;
  1.3238 -      void* trailer = 0;
  1.3239 -      mchunkptr p = mem2chunk(mem);
  1.3240 -
  1.3241 -      if (PREACTION(m)) return 0;
  1.3242 -      if ((((size_t)(mem)) % alignment) != 0) { /* misaligned */
  1.3243 -        /*
  1.3244 -          Find an aligned spot inside chunk.  Since we need to give
  1.3245 -          back leading space in a chunk of at least MIN_CHUNK_SIZE, if
  1.3246 -          the first calculation places us at a spot with less than
  1.3247 -          MIN_CHUNK_SIZE leader, we can move to the next aligned spot.
  1.3248 -          We've allocated enough total room so that this is always
  1.3249 -          possible.
  1.3250 -        */
  1.3251 -        char* br = (char*)mem2chunk((size_t)(((size_t)(mem +
  1.3252 -                                                       alignment -
  1.3253 -                                                       SIZE_T_ONE)) &
  1.3254 -                                             -alignment));
  1.3255 -        char* pos = ((size_t)(br - (char*)(p)) >= MIN_CHUNK_SIZE)?
  1.3256 -          br : br+alignment;
  1.3257 -        mchunkptr newp = (mchunkptr)pos;
  1.3258 -        size_t leadsize = pos - (char*)(p);
  1.3259 -        size_t newsize = chunksize(p) - leadsize;
  1.3260 -
  1.3261 -        if (is_mmapped(p)) { /* For mmapped chunks, just adjust offset */
  1.3262 -          newp->prev_foot = p->prev_foot + leadsize;
  1.3263 -          newp->head = (newsize|CINUSE_BIT);
  1.3264 +
  1.3265 +    if (bytes >= MAX_REQUEST - alignment) {
  1.3266 +        if (m != 0) {           /* Test isn't needed but avoids compiler warning */
  1.3267 +            MALLOC_FAILURE_ACTION;
  1.3268          }
  1.3269 -        else { /* Otherwise, give back leader, use the rest */
  1.3270 -          set_inuse(m, newp, newsize);
  1.3271 -          set_inuse(m, p, leadsize);
  1.3272 -          leader = chunk2mem(p);
  1.3273 +    } else {
  1.3274 +        size_t nb = request2size (bytes);
  1.3275 +        size_t req = nb + alignment + MIN_CHUNK_SIZE - CHUNK_OVERHEAD;
  1.3276 +        char *mem = (char *) internal_malloc (m, req);
  1.3277 +        if (mem != 0) {
  1.3278 +            void *leader = 0;
  1.3279 +            void *trailer = 0;
  1.3280 +            mchunkptr p = mem2chunk (mem);
  1.3281 +
  1.3282 +            if (PREACTION (m))
  1.3283 +                return 0;
  1.3284 +            if ((((size_t) (mem)) % alignment) != 0) {  /* misaligned */
  1.3285 +                /*
  1.3286 +                   Find an aligned spot inside chunk.  Since we need to give
  1.3287 +                   back leading space in a chunk of at least MIN_CHUNK_SIZE, if
  1.3288 +                   the first calculation places us at a spot with less than
  1.3289 +                   MIN_CHUNK_SIZE leader, we can move to the next aligned spot.
  1.3290 +                   We've allocated enough total room so that this is always
  1.3291 +                   possible.
  1.3292 +                 */
  1.3293 +                char *br = (char *)
  1.3294 +                    mem2chunk ((size_t)
  1.3295 +                               (((size_t)
  1.3296 +                                 (mem + alignment -
  1.3297 +                                  SIZE_T_ONE)) & -alignment));
  1.3298 +                char *pos =
  1.3299 +                    ((size_t) (br - (char *) (p)) >=
  1.3300 +                     MIN_CHUNK_SIZE) ? br : br + alignment;
  1.3301 +                mchunkptr newp = (mchunkptr) pos;
  1.3302 +                size_t leadsize = pos - (char *) (p);
  1.3303 +                size_t newsize = chunksize (p) - leadsize;
  1.3304 +
  1.3305 +                if (is_mmapped (p)) {   /* For mmapped chunks, just adjust offset */
  1.3306 +                    newp->prev_foot = p->prev_foot + leadsize;
  1.3307 +                    newp->head = (newsize | CINUSE_BIT);
  1.3308 +                } else {        /* Otherwise, give back leader, use the rest */
  1.3309 +                    set_inuse (m, newp, newsize);
  1.3310 +                    set_inuse (m, p, leadsize);
  1.3311 +                    leader = chunk2mem (p);
  1.3312 +                }
  1.3313 +                p = newp;
  1.3314 +            }
  1.3315 +
  1.3316 +            /* Give back spare room at the end */
  1.3317 +            if (!is_mmapped (p)) {
  1.3318 +                size_t size = chunksize (p);
  1.3319 +                if (size > nb + MIN_CHUNK_SIZE) {
  1.3320 +                    size_t remainder_size = size - nb;
  1.3321 +                    mchunkptr remainder = chunk_plus_offset (p, nb);
  1.3322 +                    set_inuse (m, p, nb);
  1.3323 +                    set_inuse (m, remainder, remainder_size);
  1.3324 +                    trailer = chunk2mem (remainder);
  1.3325 +                }
  1.3326 +            }
  1.3327 +
  1.3328 +            assert (chunksize (p) >= nb);
  1.3329 +            assert ((((size_t) (chunk2mem (p))) % alignment) == 0);
  1.3330 +            check_inuse_chunk (m, p);
  1.3331 +            POSTACTION (m);
  1.3332 +            if (leader != 0) {
  1.3333 +                internal_free (m, leader);
  1.3334 +            }
  1.3335 +            if (trailer != 0) {
  1.3336 +                internal_free (m, trailer);
  1.3337 +            }
  1.3338 +            return chunk2mem (p);
  1.3339          }
  1.3340 -        p = newp;
  1.3341 -      }
  1.3342 -
  1.3343 -      /* Give back spare room at the end */
  1.3344 -      if (!is_mmapped(p)) {
  1.3345 -        size_t size = chunksize(p);
  1.3346 -        if (size > nb + MIN_CHUNK_SIZE) {
  1.3347 -          size_t remainder_size = size - nb;
  1.3348 -          mchunkptr remainder = chunk_plus_offset(p, nb);
  1.3349 -          set_inuse(m, p, nb);
  1.3350 -          set_inuse(m, remainder, remainder_size);
  1.3351 -          trailer = chunk2mem(remainder);
  1.3352 -        }
  1.3353 -      }
  1.3354 -
  1.3355 -      assert (chunksize(p) >= nb);
  1.3356 -      assert((((size_t)(chunk2mem(p))) % alignment) == 0);
  1.3357 -      check_inuse_chunk(m, p);
  1.3358 -      POSTACTION(m);
  1.3359 -      if (leader != 0) {
  1.3360 -        internal_free(m, leader);
  1.3361 -      }
  1.3362 -      if (trailer != 0) {
  1.3363 -        internal_free(m, trailer);
  1.3364 -      }
  1.3365 -      return chunk2mem(p);
  1.3366      }
  1.3367 -  }
  1.3368 -  return 0;
  1.3369 +    return 0;
  1.3370  }
  1.3371  
  1.3372  /* ------------------------ comalloc/coalloc support --------------------- */
  1.3373  
  1.3374 -static void** ialloc(mstate m,
  1.3375 -                     size_t n_elements,
  1.3376 -                     size_t* sizes,
  1.3377 -                     int opts,
  1.3378 -                     void* chunks[]) {
  1.3379 -  /*
  1.3380 -    This provides common support for independent_X routines, handling
  1.3381 -    all of the combinations that can result.
  1.3382 -
  1.3383 -    The opts arg has:
  1.3384 -    bit 0 set if all elements are same size (using sizes[0])
  1.3385 -    bit 1 set if elements should be zeroed
  1.3386 -  */
  1.3387 -
  1.3388 -  size_t    element_size;   /* chunksize of each element, if all same */
  1.3389 -  size_t    contents_size;  /* total size of elements */
  1.3390 -  size_t    array_size;     /* request size of pointer array */
  1.3391 -  void*     mem;            /* malloced aggregate space */
  1.3392 -  mchunkptr p;              /* corresponding chunk */
  1.3393 -  size_t    remainder_size; /* remaining bytes while splitting */
  1.3394 -  void**    marray;         /* either "chunks" or malloced ptr array */
  1.3395 -  mchunkptr array_chunk;    /* chunk for malloced ptr array */
  1.3396 -  flag_t    was_enabled;    /* to disable mmap */
  1.3397 -  size_t    size;
  1.3398 -  size_t    i;
  1.3399 -
  1.3400 -  /* compute array length, if needed */
  1.3401 -  if (chunks != 0) {
  1.3402 -    if (n_elements == 0)
  1.3403 -      return chunks; /* nothing to do */
  1.3404 -    marray = chunks;
  1.3405 -    array_size = 0;
  1.3406 -  }
  1.3407 -  else {
  1.3408 -    /* if empty req, must still return chunk representing empty array */
  1.3409 -    if (n_elements == 0)
  1.3410 -      return (void**)internal_malloc(m, 0);
  1.3411 -    marray = 0;
  1.3412 -    array_size = request2size(n_elements * (sizeof(void*)));
  1.3413 -  }
  1.3414 -
  1.3415 -  /* compute total element size */
  1.3416 -  if (opts & 0x1) { /* all-same-size */
  1.3417 -    element_size = request2size(*sizes);
  1.3418 -    contents_size = n_elements * element_size;
  1.3419 -  }
  1.3420 -  else { /* add up all the sizes */
  1.3421 -    element_size = 0;
  1.3422 -    contents_size = 0;
  1.3423 -    for (i = 0; i != n_elements; ++i)
  1.3424 -      contents_size += request2size(sizes[i]);
  1.3425 -  }
  1.3426 -
  1.3427 -  size = contents_size + array_size;
  1.3428 -
  1.3429 -  /*
  1.3430 -     Allocate the aggregate chunk.  First disable direct-mmapping so
  1.3431 -     malloc won't use it, since we would not be able to later
  1.3432 -     free/realloc space internal to a segregated mmap region.
  1.3433 -  */
  1.3434 -  was_enabled = use_mmap(m);
  1.3435 -  disable_mmap(m);
  1.3436 -  mem = internal_malloc(m, size - CHUNK_OVERHEAD);
  1.3437 -  if (was_enabled)
  1.3438 -    enable_mmap(m);
  1.3439 -  if (mem == 0)
  1.3440 -    return 0;
  1.3441 -
  1.3442 -  if (PREACTION(m)) return 0;
  1.3443 -  p = mem2chunk(mem);
  1.3444 -  remainder_size = chunksize(p);
  1.3445 -
  1.3446 -  assert(!is_mmapped(p));
  1.3447 -
  1.3448 -  if (opts & 0x2) {       /* optionally clear the elements */
  1.3449 -    memset((size_t*)mem, 0, remainder_size - SIZE_T_SIZE - array_size);
  1.3450 -  }
  1.3451 -
  1.3452 -  /* If not provided, allocate the pointer array as final part of chunk */
  1.3453 -  if (marray == 0) {
  1.3454 -    size_t  array_chunk_size;
  1.3455 -    array_chunk = chunk_plus_offset(p, contents_size);
  1.3456 -    array_chunk_size = remainder_size - contents_size;
  1.3457 -    marray = (void**) (chunk2mem(array_chunk));
  1.3458 -    set_size_and_pinuse_of_inuse_chunk(m, array_chunk, array_chunk_size);
  1.3459 -    remainder_size = contents_size;
  1.3460 -  }
  1.3461 -
  1.3462 -  /* split out elements */
  1.3463 -  for (i = 0; ; ++i) {
  1.3464 -    marray[i] = chunk2mem(p);
  1.3465 -    if (i != n_elements-1) {
  1.3466 -      if (element_size != 0)
  1.3467 -        size = element_size;
  1.3468 -      else
  1.3469 -        size = request2size(sizes[i]);
  1.3470 -      remainder_size -= size;
  1.3471 -      set_size_and_pinuse_of_inuse_chunk(m, p, size);
  1.3472 -      p = chunk_plus_offset(p, size);
  1.3473 +static void **
  1.3474 +ialloc (mstate m, size_t n_elements, size_t * sizes, int opts, void *chunks[])
  1.3475 +{
  1.3476 +    /*
  1.3477 +       This provides common support for independent_X routines, handling
  1.3478 +       all of the combinations that can result.
  1.3479 +
  1.3480 +       The opts arg has:
  1.3481 +       bit 0 set if all elements are same size (using sizes[0])
  1.3482 +       bit 1 set if elements should be zeroed
  1.3483 +     */
  1.3484 +
  1.3485 +    size_t element_size;        /* chunksize of each element, if all same */
  1.3486 +    size_t contents_size;       /* total size of elements */
  1.3487 +    size_t array_size;          /* request size of pointer array */
  1.3488 +    void *mem;                  /* malloced aggregate space */
  1.3489 +    mchunkptr p;                /* corresponding chunk */
  1.3490 +    size_t remainder_size;      /* remaining bytes while splitting */
  1.3491 +    void **marray;              /* either "chunks" or malloced ptr array */
  1.3492 +    mchunkptr array_chunk;      /* chunk for malloced ptr array */
  1.3493 +    flag_t was_enabled;         /* to disable mmap */
  1.3494 +    size_t size;
  1.3495 +    size_t i;
  1.3496 +
  1.3497 +    /* compute array length, if needed */
  1.3498 +    if (chunks != 0) {
  1.3499 +        if (n_elements == 0)
  1.3500 +            return chunks;      /* nothing to do */
  1.3501 +        marray = chunks;
  1.3502 +        array_size = 0;
  1.3503 +    } else {
  1.3504 +        /* if empty req, must still return chunk representing empty array */
  1.3505 +        if (n_elements == 0)
  1.3506 +            return (void **) internal_malloc (m, 0);
  1.3507 +        marray = 0;
  1.3508 +        array_size = request2size (n_elements * (sizeof (void *)));
  1.3509 +    }
  1.3510 +
  1.3511 +    /* compute total element size */
  1.3512 +    if (opts & 0x1) {           /* all-same-size */
  1.3513 +        element_size = request2size (*sizes);
  1.3514 +        contents_size = n_elements * element_size;
  1.3515 +    } else {                    /* add up all the sizes */
  1.3516 +        element_size = 0;
  1.3517 +        contents_size = 0;
  1.3518 +        for (i = 0; i != n_elements; ++i)
  1.3519 +            contents_size += request2size (sizes[i]);
  1.3520      }
  1.3521 -    else { /* the final element absorbs any overallocation slop */
  1.3522 -      set_size_and_pinuse_of_inuse_chunk(m, p, remainder_size);
  1.3523 -      break;
  1.3524 +
  1.3525 +    size = contents_size + array_size;
  1.3526 +
  1.3527 +    /*
  1.3528 +       Allocate the aggregate chunk.  First disable direct-mmapping so
  1.3529 +       malloc won't use it, since we would not be able to later
  1.3530 +       free/realloc space internal to a segregated mmap region.
  1.3531 +     */
  1.3532 +    was_enabled = use_mmap (m);
  1.3533 +    disable_mmap (m);
  1.3534 +    mem = internal_malloc (m, size - CHUNK_OVERHEAD);
  1.3535 +    if (was_enabled)
  1.3536 +        enable_mmap (m);
  1.3537 +    if (mem == 0)
  1.3538 +        return 0;
  1.3539 +
  1.3540 +    if (PREACTION (m))
  1.3541 +        return 0;
  1.3542 +    p = mem2chunk (mem);
  1.3543 +    remainder_size = chunksize (p);
  1.3544 +
  1.3545 +    assert (!is_mmapped (p));
  1.3546 +
  1.3547 +    if (opts & 0x2) {           /* optionally clear the elements */
  1.3548 +        memset ((size_t *) mem, 0, remainder_size - SIZE_T_SIZE - array_size);
  1.3549      }
  1.3550 -  }
  1.3551 +
  1.3552 +    /* If not provided, allocate the pointer array as final part of chunk */
  1.3553 +    if (marray == 0) {
  1.3554 +        size_t array_chunk_size;
  1.3555 +        array_chunk = chunk_plus_offset (p, contents_size);
  1.3556 +        array_chunk_size = remainder_size - contents_size;
  1.3557 +        marray = (void **) (chunk2mem (array_chunk));
  1.3558 +        set_size_and_pinuse_of_inuse_chunk (m, array_chunk, array_chunk_size);
  1.3559 +        remainder_size = contents_size;
  1.3560 +    }
  1.3561 +
  1.3562 +    /* split out elements */
  1.3563 +    for (i = 0;; ++i) {
  1.3564 +        marray[i] = chunk2mem (p);
  1.3565 +        if (i != n_elements - 1) {
  1.3566 +            if (element_size != 0)
  1.3567 +                size = element_size;
  1.3568 +            else
  1.3569 +                size = request2size (sizes[i]);
  1.3570 +            remainder_size -= size;
  1.3571 +            set_size_and_pinuse_of_inuse_chunk (m, p, size);
  1.3572 +            p = chunk_plus_offset (p, size);
  1.3573 +        } else {                /* the final element absorbs any overallocation slop */
  1.3574 +            set_size_and_pinuse_of_inuse_chunk (m, p, remainder_size);
  1.3575 +            break;
  1.3576 +        }
  1.3577 +    }
  1.3578  
  1.3579  #if DEBUG
  1.3580 -  if (marray != chunks) {
  1.3581 -    /* final element must have exactly exhausted chunk */
  1.3582 -    if (element_size != 0) {
  1.3583 -      assert(remainder_size == element_size);
  1.3584 +    if (marray != chunks) {
  1.3585 +        /* final element must have exactly exhausted chunk */
  1.3586 +        if (element_size != 0) {
  1.3587 +            assert (remainder_size == element_size);
  1.3588 +        } else {
  1.3589 +            assert (remainder_size == request2size (sizes[i]));
  1.3590 +        }
  1.3591 +        check_inuse_chunk (m, mem2chunk (marray));
  1.3592      }
  1.3593 -    else {
  1.3594 -      assert(remainder_size == request2size(sizes[i]));
  1.3595 -    }
  1.3596 -    check_inuse_chunk(m, mem2chunk(marray));
  1.3597 -  }
  1.3598 -  for (i = 0; i != n_elements; ++i)
  1.3599 -    check_inuse_chunk(m, mem2chunk(marray[i]));
  1.3600 +    for (i = 0; i != n_elements; ++i)
  1.3601 +        check_inuse_chunk (m, mem2chunk (marray[i]));
  1.3602  
  1.3603  #endif /* DEBUG */
  1.3604  
  1.3605 -  POSTACTION(m);
  1.3606 -  return marray;
  1.3607 +    POSTACTION (m);
  1.3608 +    return marray;
  1.3609  }
  1.3610  
  1.3611  
  1.3612 @@ -4068,343 +4144,370 @@
  1.3613  
  1.3614  #if !ONLY_MSPACES
  1.3615  
  1.3616 -void* dlmalloc(size_t bytes) {
  1.3617 -  /*
  1.3618 -     Basic algorithm:
  1.3619 -     If a small request (< 256 bytes minus per-chunk overhead):
  1.3620 +void *
  1.3621 +dlmalloc (size_t bytes)
  1.3622 +{
  1.3623 +    /*
  1.3624 +       Basic algorithm:
  1.3625 +       If a small request (< 256 bytes minus per-chunk overhead):
  1.3626         1. If one exists, use a remainderless chunk in associated smallbin.
  1.3627 -          (Remainderless means that there are too few excess bytes to
  1.3628 -          represent as a chunk.)
  1.3629 +       (Remainderless means that there are too few excess bytes to
  1.3630 +       represent as a chunk.)
  1.3631         2. If it is big enough, use the dv chunk, which is normally the
  1.3632 -          chunk adjacent to the one used for the most recent small request.
  1.3633 +       chunk adjacent to the one used for the most recent small request.
  1.3634         3. If one exists, split the smallest available chunk in a bin,
  1.3635 -          saving remainder in dv.
  1.3636 +       saving remainder in dv.
  1.3637         4. If it is big enough, use the top chunk.
  1.3638         5. If available, get memory from system and use it
  1.3639 -     Otherwise, for a large request:
  1.3640 +       Otherwise, for a large request:
  1.3641         1. Find the smallest available binned chunk that fits, and use it
  1.3642 -          if it is better fitting than dv chunk, splitting if necessary.
  1.3643 +       if it is better fitting than dv chunk, splitting if necessary.
  1.3644         2. If better fitting than any binned chunk, use the dv chunk.
  1.3645         3. If it is big enough, use the top chunk.
  1.3646         4. If request size >= mmap threshold, try to directly mmap this chunk.
  1.3647         5. If available, get memory from system and use it
  1.3648  
  1.3649 -     The ugly goto's here ensure that postaction occurs along all paths.
  1.3650 -  */
  1.3651 -
  1.3652 -  if (!PREACTION(gm)) {
  1.3653 -    void* mem;
  1.3654 -    size_t nb;
  1.3655 -    if (bytes <= MAX_SMALL_REQUEST) {
  1.3656 -      bindex_t idx;
  1.3657 -      binmap_t smallbits;
  1.3658 -      nb = (bytes < MIN_REQUEST)? MIN_CHUNK_SIZE : pad_request(bytes);
  1.3659 -      idx = small_index(nb);
  1.3660 -      smallbits = gm->smallmap >> idx;
  1.3661 -
  1.3662 -      if ((smallbits & 0x3U) != 0) { /* Remainderless fit to a smallbin. */
  1.3663 -        mchunkptr b, p;
  1.3664 -        idx += ~smallbits & 1;       /* Uses next bin if idx empty */
  1.3665 -        b = smallbin_at(gm, idx);
  1.3666 -        p = b->fd;
  1.3667 -        assert(chunksize(p) == small_index2size(idx));
  1.3668 -        unlink_first_small_chunk(gm, b, p, idx);
  1.3669 -        set_inuse_and_pinuse(gm, p, small_index2size(idx));
  1.3670 -        mem = chunk2mem(p);
  1.3671 -        check_malloced_chunk(gm, mem, nb);
  1.3672 -        goto postaction;
  1.3673 -      }
  1.3674 -
  1.3675 -      else if (nb > gm->dvsize) {
  1.3676 -        if (smallbits != 0) { /* Use chunk in next nonempty smallbin */
  1.3677 -          mchunkptr b, p, r;
  1.3678 -          size_t rsize;
  1.3679 -          bindex_t i;
  1.3680 -          binmap_t leftbits = (smallbits << idx) & left_bits(idx2bit(idx));
  1.3681 -          binmap_t leastbit = least_bit(leftbits);
  1.3682 -          compute_bit2idx(leastbit, i);
  1.3683 -          b = smallbin_at(gm, i);
  1.3684 -          p = b->fd;
  1.3685 -          assert(chunksize(p) == small_index2size(i));
  1.3686 -          unlink_first_small_chunk(gm, b, p, i);
  1.3687 -          rsize = small_index2size(i) - nb;
  1.3688 -          /* Fit here cannot be remainderless if 4byte sizes */
  1.3689 -          if (SIZE_T_SIZE != 4 && rsize < MIN_CHUNK_SIZE)
  1.3690 -            set_inuse_and_pinuse(gm, p, small_index2size(i));
  1.3691 -          else {
  1.3692 -            set_size_and_pinuse_of_inuse_chunk(gm, p, nb);
  1.3693 -            r = chunk_plus_offset(p, nb);
  1.3694 -            set_size_and_pinuse_of_free_chunk(r, rsize);
  1.3695 -            replace_dv(gm, r, rsize);
  1.3696 -          }
  1.3697 -          mem = chunk2mem(p);
  1.3698 -          check_malloced_chunk(gm, mem, nb);
  1.3699 -          goto postaction;
  1.3700 +       The ugly goto's here ensure that postaction occurs along all paths.
  1.3701 +     */
  1.3702 +
  1.3703 +    if (!PREACTION (gm)) {
  1.3704 +        void *mem;
  1.3705 +        size_t nb;
  1.3706 +        if (bytes <= MAX_SMALL_REQUEST) {
  1.3707 +            bindex_t idx;
  1.3708 +            binmap_t smallbits;
  1.3709 +            nb = (bytes < MIN_REQUEST) ? MIN_CHUNK_SIZE : pad_request (bytes);
  1.3710 +            idx = small_index (nb);
  1.3711 +            smallbits = gm->smallmap >> idx;
  1.3712 +
  1.3713 +            if ((smallbits & 0x3U) != 0) {      /* Remainderless fit to a smallbin. */
  1.3714 +                mchunkptr b, p;
  1.3715 +                idx += ~smallbits & 1;  /* Uses next bin if idx empty */
  1.3716 +                b = smallbin_at (gm, idx);
  1.3717 +                p = b->fd;
  1.3718 +                assert (chunksize (p) == small_index2size (idx));
  1.3719 +                unlink_first_small_chunk (gm, b, p, idx);
  1.3720 +                set_inuse_and_pinuse (gm, p, small_index2size (idx));
  1.3721 +                mem = chunk2mem (p);
  1.3722 +                check_malloced_chunk (gm, mem, nb);
  1.3723 +                goto postaction;
  1.3724 +            }
  1.3725 +
  1.3726 +            else if (nb > gm->dvsize) {
  1.3727 +                if (smallbits != 0) {   /* Use chunk in next nonempty smallbin */
  1.3728 +                    mchunkptr b, p, r;
  1.3729 +                    size_t rsize;
  1.3730 +                    bindex_t i;
  1.3731 +                    binmap_t leftbits =
  1.3732 +                        (smallbits << idx) & left_bits (idx2bit (idx));
  1.3733 +                    binmap_t leastbit = least_bit (leftbits);
  1.3734 +                    compute_bit2idx (leastbit, i);
  1.3735 +                    b = smallbin_at (gm, i);
  1.3736 +                    p = b->fd;
  1.3737 +                    assert (chunksize (p) == small_index2size (i));
  1.3738 +                    unlink_first_small_chunk (gm, b, p, i);
  1.3739 +                    rsize = small_index2size (i) - nb;
  1.3740 +                    /* Fit here cannot be remainderless if 4byte sizes */
  1.3741 +                    if (SIZE_T_SIZE != 4 && rsize < MIN_CHUNK_SIZE)
  1.3742 +                        set_inuse_and_pinuse (gm, p, small_index2size (i));
  1.3743 +                    else {
  1.3744 +                        set_size_and_pinuse_of_inuse_chunk (gm, p, nb);
  1.3745 +                        r = chunk_plus_offset (p, nb);
  1.3746 +                        set_size_and_pinuse_of_free_chunk (r, rsize);
  1.3747 +                        replace_dv (gm, r, rsize);
  1.3748 +                    }
  1.3749 +                    mem = chunk2mem (p);
  1.3750 +                    check_malloced_chunk (gm, mem, nb);
  1.3751 +                    goto postaction;
  1.3752 +                }
  1.3753 +
  1.3754 +                else if (gm->treemap != 0
  1.3755 +                         && (mem = tmalloc_small (gm, nb)) != 0) {
  1.3756 +                    check_malloced_chunk (gm, mem, nb);
  1.3757 +                    goto postaction;
  1.3758 +                }
  1.3759 +            }
  1.3760 +        } else if (bytes >= MAX_REQUEST)
  1.3761 +            nb = MAX_SIZE_T;    /* Too big to allocate. Force failure (in sys alloc) */
  1.3762 +        else {
  1.3763 +            nb = pad_request (bytes);
  1.3764 +            if (gm->treemap != 0 && (mem = tmalloc_large (gm, nb)) != 0) {
  1.3765 +                check_malloced_chunk (gm, mem, nb);
  1.3766 +                goto postaction;
  1.3767 +            }
  1.3768          }
  1.3769  
  1.3770 -        else if (gm->treemap != 0 && (mem = tmalloc_small(gm, nb)) != 0) {
  1.3771 -          check_malloced_chunk(gm, mem, nb);
  1.3772 -          goto postaction;
  1.3773 +        if (nb <= gm->dvsize) {
  1.3774 +            size_t rsize = gm->dvsize - nb;
  1.3775 +            mchunkptr p = gm->dv;
  1.3776 +            if (rsize >= MIN_CHUNK_SIZE) {      /* split dv */
  1.3777 +                mchunkptr r = gm->dv = chunk_plus_offset (p, nb);
  1.3778 +                gm->dvsize = rsize;
  1.3779 +                set_size_and_pinuse_of_free_chunk (r, rsize);
  1.3780 +                set_size_and_pinuse_of_inuse_chunk (gm, p, nb);
  1.3781 +            } else {            /* exhaust dv */
  1.3782 +                size_t dvs = gm->dvsize;
  1.3783 +                gm->dvsize = 0;
  1.3784 +                gm->dv = 0;
  1.3785 +                set_inuse_and_pinuse (gm, p, dvs);
  1.3786 +            }
  1.3787 +            mem = chunk2mem (p);
  1.3788 +            check_malloced_chunk (gm, mem, nb);
  1.3789 +            goto postaction;
  1.3790          }
  1.3791 -      }
  1.3792 +
  1.3793 +        else if (nb < gm->topsize) {    /* Split top */
  1.3794 +            size_t rsize = gm->topsize -= nb;
  1.3795 +            mchunkptr p = gm->top;
  1.3796 +            mchunkptr r = gm->top = chunk_plus_offset (p, nb);
  1.3797 +            r->head = rsize | PINUSE_BIT;
  1.3798 +            set_size_and_pinuse_of_inuse_chunk (gm, p, nb);
  1.3799 +            mem = chunk2mem (p);
  1.3800 +            check_top_chunk (gm, gm->top);
  1.3801 +            check_malloced_chunk (gm, mem, nb);
  1.3802 +            goto postaction;
  1.3803 +        }
  1.3804 +
  1.3805 +        mem = sys_alloc (gm, nb);
  1.3806 +
  1.3807 +      postaction:
  1.3808 +        POSTACTION (gm);
  1.3809 +        return mem;
  1.3810      }
  1.3811 -    else if (bytes >= MAX_REQUEST)
  1.3812 -      nb = MAX_SIZE_T; /* Too big to allocate. Force failure (in sys alloc) */
  1.3813 -    else {
  1.3814 -      nb = pad_request(bytes);
  1.3815 -      if (gm->treemap != 0 && (mem = tmalloc_large(gm, nb)) != 0) {
  1.3816 -        check_malloced_chunk(gm, mem, nb);
  1.3817 -        goto postaction;
  1.3818 -      }
  1.3819 -    }
  1.3820 -
  1.3821 -    if (nb <= gm->dvsize) {
  1.3822 -      size_t rsize = gm->dvsize - nb;
  1.3823 -      mchunkptr p = gm->dv;
  1.3824 -      if (rsize >= MIN_CHUNK_SIZE) { /* split dv */
  1.3825 -        mchunkptr r = gm->dv = chunk_plus_offset(p, nb);
  1.3826 -        gm->dvsize = rsize;
  1.3827 -        set_size_and_pinuse_of_free_chunk(r, rsize);
  1.3828 -        set_size_and_pinuse_of_inuse_chunk(gm, p, nb);
  1.3829 -      }
  1.3830 -      else { /* exhaust dv */
  1.3831 -        size_t dvs = gm->dvsize;
  1.3832 -        gm->dvsize = 0;
  1.3833 -        gm->dv = 0;
  1.3834 -        set_inuse_and_pinuse(gm, p, dvs);
  1.3835 -      }
  1.3836 -      mem = chunk2mem(p);
  1.3837 -      check_malloced_chunk(gm, mem, nb);
  1.3838 -      goto postaction;
  1.3839 -    }
  1.3840 -
  1.3841 -    else if (nb < gm->topsize) { /* Split top */
  1.3842 -      size_t rsize = gm->topsize -= nb;
  1.3843 -      mchunkptr p = gm->top;
  1.3844 -      mchunkptr r = gm->top = chunk_plus_offset(p, nb);
  1.3845 -      r->head = rsize | PINUSE_BIT;
  1.3846 -      set_size_and_pinuse_of_inuse_chunk(gm, p, nb);
  1.3847 -      mem = chunk2mem(p);
  1.3848 -      check_top_chunk(gm, gm->top);
  1.3849 -      check_malloced_chunk(gm, mem, nb);
  1.3850 -      goto postaction;
  1.3851 -    }
  1.3852 -
  1.3853 -    mem = sys_alloc(gm, nb);
  1.3854 -
  1.3855 -  postaction:
  1.3856 -    POSTACTION(gm);
  1.3857 -    return mem;
  1.3858 -  }
  1.3859 -
  1.3860 -  return 0;
  1.3861 +
  1.3862 +    return 0;
  1.3863  }
  1.3864  
  1.3865 -void dlfree(void* mem) {
  1.3866 -  /*
  1.3867 -     Consolidate freed chunks with preceeding or succeeding bordering
  1.3868 -     free chunks, if they exist, and then place in a bin.  Intermixed
  1.3869 -     with special cases for top, dv, mmapped chunks, and usage errors.
  1.3870 -  */
  1.3871 -
  1.3872 -  if (mem != 0) {
  1.3873 -    mchunkptr p  = mem2chunk(mem);
  1.3874 +void
  1.3875 +dlfree (void *mem)
  1.3876 +{
  1.3877 +    /*
  1.3878 +       Consolidate freed chunks with preceeding or succeeding bordering
  1.3879 +       free chunks, if they exist, and then place in a bin.  Intermixed
  1.3880 +       with special cases for top, dv, mmapped chunks, and usage errors.
  1.3881 +     */
  1.3882 +
  1.3883 +    if (mem != 0) {
  1.3884 +        mchunkptr p = mem2chunk (mem);
  1.3885  #if FOOTERS
  1.3886 -    mstate fm = get_mstate_for(p);
  1.3887 -    if (!ok_magic(fm)) {
  1.3888 -      USAGE_ERROR_ACTION(fm, p);
  1.3889 -      return;
  1.3890 -    }
  1.3891 +        mstate fm = get_mstate_for (p);
  1.3892 +        if (!ok_magic (fm)) {
  1.3893 +            USAGE_ERROR_ACTION (fm, p);
  1.3894 +            return;
  1.3895 +        }
  1.3896  #else /* FOOTERS */
  1.3897  #define fm gm
  1.3898  #endif /* FOOTERS */
  1.3899 -    if (!PREACTION(fm)) {
  1.3900 -      check_inuse_chunk(fm, p);
  1.3901 -      if (RTCHECK(ok_address(fm, p) && ok_cinuse(p))) {
  1.3902 -        size_t psize = chunksize(p);
  1.3903 -        mchunkptr next = chunk_plus_offset(p, psize);
  1.3904 -        if (!pinuse(p)) {
  1.3905 -          size_t prevsize = p->prev_foot;
  1.3906 -          if ((prevsize & IS_MMAPPED_BIT) != 0) {
  1.3907 -            prevsize &= ~IS_MMAPPED_BIT;
  1.3908 -            psize += prevsize + MMAP_FOOT_PAD;
  1.3909 -            if (CALL_MUNMAP((char*)p - prevsize, psize) == 0)
  1.3910 -              fm->footprint -= psize;
  1.3911 -            goto postaction;
  1.3912 -          }
  1.3913 -          else {
  1.3914 -            mchunkptr prev = chunk_minus_offset(p, prevsize);
  1.3915 -            psize += prevsize;
  1.3916 -            p = prev;
  1.3917 -            if (RTCHECK(ok_address(fm, prev))) { /* consolidate backward */
  1.3918 -              if (p != fm->dv) {
  1.3919 -                unlink_chunk(fm, p, prevsize);
  1.3920 -              }
  1.3921 -              else if ((next->head & INUSE_BITS) == INUSE_BITS) {
  1.3922 -                fm->dvsize = psize;
  1.3923 -                set_free_with_pinuse(p, psize, next);
  1.3924 -                goto postaction;
  1.3925 -              }
  1.3926 +        if (!PREACTION (fm)) {
  1.3927 +            check_inuse_chunk (fm, p);
  1.3928 +            if (RTCHECK (ok_address (fm, p) && ok_cinuse (p))) {
  1.3929 +                size_t psize = chunksize (p);
  1.3930 +                mchunkptr next = chunk_plus_offset (p, psize);
  1.3931 +                if (!pinuse (p)) {
  1.3932 +                    size_t prevsize = p->prev_foot;
  1.3933 +                    if ((prevsize & IS_MMAPPED_BIT) != 0) {
  1.3934 +                        prevsize &= ~IS_MMAPPED_BIT;
  1.3935 +                        psize += prevsize + MMAP_FOOT_PAD;
  1.3936 +                        if (CALL_MUNMAP ((char *) p - prevsize, psize) == 0)
  1.3937 +                            fm->footprint -= psize;
  1.3938 +                        goto postaction;
  1.3939 +                    } else {
  1.3940 +                        mchunkptr prev = chunk_minus_offset (p, prevsize);
  1.3941 +                        psize += prevsize;
  1.3942 +                        p = prev;
  1.3943 +                        if (RTCHECK (ok_address (fm, prev))) {  /* consolidate backward */
  1.3944 +                            if (p != fm->dv) {
  1.3945 +                                unlink_chunk (fm, p, prevsize);
  1.3946 +                            } else if ((next->head & INUSE_BITS) ==
  1.3947 +                                       INUSE_BITS) {
  1.3948 +                                fm->dvsize = psize;
  1.3949 +                                set_free_with_pinuse (p, psize, next);
  1.3950 +                                goto postaction;
  1.3951 +                            }
  1.3952 +                        } else
  1.3953 +                            goto erroraction;
  1.3954 +                    }
  1.3955 +                }
  1.3956 +
  1.3957 +                if (RTCHECK (ok_next (p, next) && ok_pinuse (next))) {
  1.3958 +                    if (!cinuse (next)) {       /* consolidate forward */
  1.3959 +                        if (next == fm->top) {
  1.3960 +                            size_t tsize = fm->topsize += psize;
  1.3961 +                            fm->top = p;
  1.3962 +                            p->head = tsize | PINUSE_BIT;
  1.3963 +                            if (p == fm->dv) {
  1.3964 +                                fm->dv = 0;
  1.3965 +                                fm->dvsize = 0;
  1.3966 +                            }
  1.3967 +                            if (should_trim (fm, tsize))
  1.3968 +                                sys_trim (fm, 0);
  1.3969 +                            goto postaction;
  1.3970 +                        } else if (next == fm->dv) {
  1.3971 +                            size_t dsize = fm->dvsize += psize;
  1.3972 +                            fm->dv = p;
  1.3973 +                            set_size_and_pinuse_of_free_chunk (p, dsize);
  1.3974 +                            goto postaction;
  1.3975 +                        } else {
  1.3976 +                            size_t nsize = chunksize (next);
  1.3977 +                            psize += nsize;
  1.3978 +                            unlink_chunk (fm, next, nsize);
  1.3979 +                            set_size_and_pinuse_of_free_chunk (p, psize);
  1.3980 +                            if (p == fm->dv) {
  1.3981 +                                fm->dvsize = psize;
  1.3982 +                                goto postaction;
  1.3983 +                            }
  1.3984 +                        }
  1.3985 +                    } else
  1.3986 +                        set_free_with_pinuse (p, psize, next);
  1.3987 +                    insert_chunk (fm, p, psize);
  1.3988 +                    check_free_chunk (fm, p);
  1.3989 +                    goto postaction;
  1.3990 +                }
  1.3991              }
  1.3992 -            else
  1.3993 -              goto erroraction;
  1.3994 -          }
  1.3995 +          erroraction:
  1.3996 +            USAGE_ERROR_ACTION (fm, p);
  1.3997 +          postaction:
  1.3998 +            POSTACTION (fm);
  1.3999          }
  1.4000 -
  1.4001 -        if (RTCHECK(ok_next(p, next) && ok_pinuse(next))) {
  1.4002 -          if (!cinuse(next)) {  /* consolidate forward */
  1.4003 -            if (next == fm->top) {
  1.4004 -              size_t tsize = fm->topsize += psize;
  1.4005 -              fm->top = p;
  1.4006 -              p->head = tsize | PINUSE_BIT;
  1.4007 -              if (p == fm->dv) {
  1.4008 -                fm->dv = 0;
  1.4009 -                fm->dvsize = 0;
  1.4010 -              }
  1.4011 -              if (should_trim(fm, tsize))
  1.4012 -                sys_trim(fm, 0);
  1.4013 -              goto postaction;
  1.4014 -            }
  1.4015 -            else if (next == fm->dv) {
  1.4016 -              size_t dsize = fm->dvsize += psize;
  1.4017 -              fm->dv = p;
  1.4018 -              set_size_and_pinuse_of_free_chunk(p, dsize);
  1.4019 -              goto postaction;
  1.4020 -            }
  1.4021 -            else {
  1.4022 -              size_t nsize = chunksize(next);
  1.4023 -              psize += nsize;
  1.4024 -              unlink_chunk(fm, next, nsize);
  1.4025 -              set_size_and_pinuse_of_free_chunk(p, psize);
  1.4026 -              if (p == fm->dv) {
  1.4027 -                fm->dvsize = psize;
  1.4028 -                goto postaction;
  1.4029 -              }
  1.4030 -            }
  1.4031 -          }
  1.4032 -          else
  1.4033 -            set_free_with_pinuse(p, psize, next);
  1.4034 -          insert_chunk(fm, p, psize);
  1.4035 -          check_free_chunk(fm, p);
  1.4036 -          goto postaction;
  1.4037 -        }
  1.4038 -      }
  1.4039 -    erroraction:
  1.4040 -      USAGE_ERROR_ACTION(fm, p);
  1.4041 -    postaction:
  1.4042 -      POSTACTION(fm);
  1.4043      }
  1.4044 -  }
  1.4045  #if !FOOTERS
  1.4046  #undef fm
  1.4047  #endif /* FOOTERS */
  1.4048  }
  1.4049  
  1.4050 -void* dlcalloc(size_t n_elements, size_t elem_size) {
  1.4051 -  void* mem;
  1.4052 -  size_t req = 0;
  1.4053 -  if (n_elements != 0) {
  1.4054 -    req = n_elements * elem_size;
  1.4055 -    if (((n_elements | elem_size) & ~(size_t)0xffff) &&
  1.4056 -        (req / n_elements != elem_size))
  1.4057 -      req = MAX_SIZE_T; /* force downstream failure on overflow */
  1.4058 -  }
  1.4059 -  mem = dlmalloc(req);
  1.4060 -  if (mem != 0 && calloc_must_clear(mem2chunk(mem)))
  1.4061 -    memset(mem, 0, req);
  1.4062 -  return mem;
  1.4063 +void *
  1.4064 +dlcalloc (size_t n_elements, size_t elem_size)
  1.4065 +{
  1.4066 +    void *mem;
  1.4067 +    size_t req = 0;
  1.4068 +    if (n_elements != 0) {
  1.4069 +        req = n_elements * elem_size;
  1.4070 +        if (((n_elements | elem_size) & ~(size_t) 0xffff) &&
  1.4071 +            (req / n_elements != elem_size))
  1.4072 +            req = MAX_SIZE_T;   /* force downstream failure on overflow */
  1.4073 +    }
  1.4074 +    mem = dlmalloc (req);
  1.4075 +    if (mem != 0 && calloc_must_clear (mem2chunk (mem)))
  1.4076 +        memset (mem, 0, req);
  1.4077 +    return mem;
  1.4078  }
  1.4079  
  1.4080 -void* dlrealloc(void* oldmem, size_t bytes) {
  1.4081 -  if (oldmem == 0)
  1.4082 -    return dlmalloc(bytes);
  1.4083 +void *
  1.4084 +dlrealloc (void *oldmem, size_t bytes)
  1.4085 +{
  1.4086 +    if (oldmem == 0)
  1.4087 +        return dlmalloc (bytes);
  1.4088  #ifdef REALLOC_ZERO_BYTES_FREES
  1.4089 -  if (bytes == 0) {
  1.4090 -    dlfree(oldmem);
  1.4091 -    return 0;
  1.4092 -  }
  1.4093 +    if (bytes == 0) {
  1.4094 +        dlfree (oldmem);
  1.4095 +        return 0;
  1.4096 +    }
  1.4097  #endif /* REALLOC_ZERO_BYTES_FREES */
  1.4098 -  else {
  1.4099 +    else {
  1.4100  #if ! FOOTERS
  1.4101 -    mstate m = gm;
  1.4102 +        mstate m = gm;
  1.4103  #else /* FOOTERS */
  1.4104 -    mstate m = get_mstate_for(mem2chunk(oldmem));
  1.4105 -    if (!ok_magic(m)) {
  1.4106 -      USAGE_ERROR_ACTION(m, oldmem);
  1.4107 -      return 0;
  1.4108 -    }
  1.4109 +        mstate m = get_mstate_for (mem2chunk (oldmem));
  1.4110 +        if (!ok_magic (m)) {
  1.4111 +            USAGE_ERROR_ACTION (m, oldmem);
  1.4112 +            return 0;
  1.4113 +        }
  1.4114  #endif /* FOOTERS */
  1.4115 -    return internal_realloc(m, oldmem, bytes);
  1.4116 -  }
  1.4117 +        return internal_realloc (m, oldmem, bytes);
  1.4118 +    }
  1.4119  }
  1.4120  
  1.4121 -void* dlmemalign(size_t alignment, size_t bytes) {
  1.4122 -  return internal_memalign(gm, alignment, bytes);
  1.4123 +void *
  1.4124 +dlmemalign (size_t alignment, size_t bytes)
  1.4125 +{
  1.4126 +    return internal_memalign (gm, alignment, bytes);
  1.4127  }
  1.4128  
  1.4129 -void** dlindependent_calloc(size_t n_elements, size_t elem_size,
  1.4130 -                                 void* chunks[]) {
  1.4131 -  size_t sz = elem_size; /* serves as 1-element array */
  1.4132 -  return ialloc(gm, n_elements, &sz, 3, chunks);
  1.4133 +void **
  1.4134 +dlindependent_calloc (size_t n_elements, size_t elem_size, void *chunks[])
  1.4135 +{
  1.4136 +    size_t sz = elem_size;      /* serves as 1-element array */
  1.4137 +    return ialloc (gm, n_elements, &sz, 3, chunks);
  1.4138  }
  1.4139  
  1.4140 -void** dlindependent_comalloc(size_t n_elements, size_t sizes[],
  1.4141 -                                   void* chunks[]) {
  1.4142 -  return ialloc(gm, n_elements, sizes, 0, chunks);
  1.4143 +void **
  1.4144 +dlindependent_comalloc (size_t n_elements, size_t sizes[], void *chunks[])
  1.4145 +{
  1.4146 +    return ialloc (gm, n_elements, sizes, 0, chunks);
  1.4147  }
  1.4148  
  1.4149 -void* dlvalloc(size_t bytes) {
  1.4150 -  size_t pagesz;
  1.4151 -  init_mparams();
  1.4152 -  pagesz = mparams.page_size;
  1.4153 -  return dlmemalign(pagesz, bytes);
  1.4154 +void *
  1.4155 +dlvalloc (size_t bytes)
  1.4156 +{
  1.4157 +    size_t pagesz;
  1.4158 +    init_mparams ();
  1.4159 +    pagesz = mparams.page_size;
  1.4160 +    return dlmemalign (pagesz, bytes);
  1.4161  }
  1.4162  
  1.4163 -void* dlpvalloc(size_t bytes) {
  1.4164 -  size_t pagesz;
  1.4165 -  init_mparams();
  1.4166 -  pagesz = mparams.page_size;
  1.4167 -  return dlmemalign(pagesz, (bytes + pagesz - SIZE_T_ONE) & ~(pagesz - SIZE_T_ONE));
  1.4168 +void *
  1.4169 +dlpvalloc (size_t bytes)
  1.4170 +{
  1.4171 +    size_t pagesz;
  1.4172 +    init_mparams ();
  1.4173 +    pagesz = mparams.page_size;
  1.4174 +    return dlmemalign (pagesz,
  1.4175 +                       (bytes + pagesz - SIZE_T_ONE) & ~(pagesz -
  1.4176 +                                                         SIZE_T_ONE));
  1.4177  }
  1.4178  
  1.4179 -int dlmalloc_trim(size_t pad) {
  1.4180 -  int result = 0;
  1.4181 -  if (!PREACTION(gm)) {
  1.4182 -    result = sys_trim(gm, pad);
  1.4183 -    POSTACTION(gm);
  1.4184 -  }
  1.4185 -  return result;
  1.4186 +int
  1.4187 +dlmalloc_trim (size_t pad)
  1.4188 +{
  1.4189 +    int result = 0;
  1.4190 +    if (!PREACTION (gm)) {
  1.4191 +        result = sys_trim (gm, pad);
  1.4192 +        POSTACTION (gm);
  1.4193 +    }
  1.4194 +    return result;
  1.4195  }
  1.4196  
  1.4197 -size_t dlmalloc_footprint(void) {
  1.4198 -  return gm->footprint;
  1.4199 +size_t
  1.4200 +dlmalloc_footprint (void)
  1.4201 +{
  1.4202 +    return gm->footprint;
  1.4203  }
  1.4204  
  1.4205 -size_t dlmalloc_max_footprint(void) {
  1.4206 -  return gm->max_footprint;
  1.4207 +size_t
  1.4208 +dlmalloc_max_footprint (void)
  1.4209 +{
  1.4210 +    return gm->max_footprint;
  1.4211  }
  1.4212  
  1.4213  #if !NO_MALLINFO
  1.4214 -struct mallinfo dlmallinfo(void) {
  1.4215 -  return internal_mallinfo(gm);
  1.4216 +struct mallinfo
  1.4217 +dlmallinfo (void)
  1.4218 +{
  1.4219 +    return internal_mallinfo (gm);
  1.4220  }
  1.4221  #endif /* NO_MALLINFO */
  1.4222  
  1.4223 -void dlmalloc_stats() {
  1.4224 -  internal_malloc_stats(gm);
  1.4225 +void
  1.4226 +dlmalloc_stats ()
  1.4227 +{
  1.4228 +    internal_malloc_stats (gm);
  1.4229  }
  1.4230  
  1.4231 -size_t dlmalloc_usable_size(void* mem) {
  1.4232 -  if (mem != 0) {
  1.4233 -    mchunkptr p = mem2chunk(mem);
  1.4234 -    if (cinuse(p))
  1.4235 -      return chunksize(p) - overhead_for(p);
  1.4236 -  }
  1.4237 -  return 0;
  1.4238 +size_t
  1.4239 +dlmalloc_usable_size (void *mem)
  1.4240 +{
  1.4241 +    if (mem != 0) {
  1.4242 +        mchunkptr p = mem2chunk (mem);
  1.4243 +        if (cinuse (p))
  1.4244 +            return chunksize (p) - overhead_for (p);
  1.4245 +    }
  1.4246 +    return 0;
  1.4247  }
  1.4248  
  1.4249 -int dlmallopt(int param_number, int value) {
  1.4250 -  return change_mparam(param_number, value);
  1.4251 +int
  1.4252 +dlmallopt (int param_number, int value)
  1.4253 +{
  1.4254 +    return change_mparam (param_number, value);
  1.4255  }
  1.4256  
  1.4257  #endif /* !ONLY_MSPACES */
  1.4258 @@ -4413,78 +4516,86 @@
  1.4259  
  1.4260  #if MSPACES
  1.4261  
  1.4262 -static mstate init_user_mstate(char* tbase, size_t tsize) {
  1.4263 -  size_t msize = pad_request(sizeof(struct malloc_state));
  1.4264 -  mchunkptr mn;
  1.4265 -  mchunkptr msp = align_as_chunk(tbase);
  1.4266 -  mstate m = (mstate)(chunk2mem(msp));
  1.4267 -  memset(m, 0, msize);
  1.4268 -  INITIAL_LOCK(&m->mutex);
  1.4269 -  msp->head = (msize|PINUSE_BIT|CINUSE_BIT);
  1.4270 -  m->seg.base = m->least_addr = tbase;
  1.4271 -  m->seg.size = m->footprint = m->max_footprint = tsize;
  1.4272 -  m->magic = mparams.magic;
  1.4273 -  m->mflags = mparams.default_mflags;
  1.4274 -  disable_contiguous(m);
  1.4275 -  init_bins(m);
  1.4276 -  mn = next_chunk(mem2chunk(m));
  1.4277 -  init_top(m, mn, (size_t)((tbase + tsize) - (char*)mn) - TOP_FOOT_SIZE);
  1.4278 -  check_top_chunk(m, m->top);
  1.4279 -  return m;
  1.4280 +static mstate
  1.4281 +init_user_mstate (char *tbase, size_t tsize)
  1.4282 +{
  1.4283 +    size_t msize = pad_request (sizeof (struct malloc_state));
  1.4284 +    mchunkptr mn;
  1.4285 +    mchunkptr msp = align_as_chunk (tbase);
  1.4286 +    mstate m = (mstate) (chunk2mem (msp));
  1.4287 +    memset (m, 0, msize);
  1.4288 +    INITIAL_LOCK (&m->mutex);
  1.4289 +    msp->head = (msize | PINUSE_BIT | CINUSE_BIT);
  1.4290 +    m->seg.base = m->least_addr = tbase;
  1.4291 +    m->seg.size = m->footprint = m->max_footprint = tsize;
  1.4292 +    m->magic = mparams.magic;
  1.4293 +    m->mflags = mparams.default_mflags;
  1.4294 +    disable_contiguous (m);
  1.4295 +    init_bins (m);
  1.4296 +    mn = next_chunk (mem2chunk (m));
  1.4297 +    init_top (m, mn,
  1.4298 +              (size_t) ((tbase + tsize) - (char *) mn) - TOP_FOOT_SIZE);
  1.4299 +    check_top_chunk (m, m->top);
  1.4300 +    return m;
  1.4301  }
  1.4302  
  1.4303 -mspace create_mspace(size_t capacity, int locked) {
  1.4304 -  mstate m = 0;
  1.4305 -  size_t msize = pad_request(sizeof(struct malloc_state));
  1.4306 -  init_mparams(); /* Ensure pagesize etc initialized */
  1.4307 -
  1.4308 -  if (capacity < (size_t) -(msize + TOP_FOOT_SIZE + mparams.page_size)) {
  1.4309 -    size_t rs = ((capacity == 0)? mparams.granularity :
  1.4310 -                 (capacity + TOP_FOOT_SIZE + msize));
  1.4311 -    size_t tsize = granularity_align(rs);
  1.4312 -    char* tbase = (char*)(CALL_MMAP(tsize));
  1.4313 -    if (tbase != CMFAIL) {
  1.4314 -      m = init_user_mstate(tbase, tsize);
  1.4315 -      m->seg.sflags = IS_MMAPPED_BIT;
  1.4316 -      set_lock(m, locked);
  1.4317 +mspace
  1.4318 +create_mspace (size_t capacity, int locked)
  1.4319 +{
  1.4320 +    mstate m = 0;
  1.4321 +    size_t msize = pad_request (sizeof (struct malloc_state));
  1.4322 +    init_mparams ();            /* Ensure pagesize etc initialized */
  1.4323 +
  1.4324 +    if (capacity < (size_t) - (msize + TOP_FOOT_SIZE + mparams.page_size)) {
  1.4325 +        size_t rs = ((capacity == 0) ? mparams.granularity :
  1.4326 +                     (capacity + TOP_FOOT_SIZE + msize));
  1.4327 +        size_t tsize = granularity_align (rs);
  1.4328 +        char *tbase = (char *) (CALL_MMAP (tsize));
  1.4329 +        if (tbase != CMFAIL) {
  1.4330 +            m = init_user_mstate (tbase, tsize);
  1.4331 +            m->seg.sflags = IS_MMAPPED_BIT;
  1.4332 +            set_lock (m, locked);
  1.4333 +        }
  1.4334      }
  1.4335 -  }
  1.4336 -  return (mspace)m;
  1.4337 +    return (mspace) m;
  1.4338  }
  1.4339  
  1.4340 -mspace create_mspace_with_base(void* base, size_t capacity, int locked) {
  1.4341 -  mstate m = 0;
  1.4342 -  size_t msize = pad_request(sizeof(struct malloc_state));
  1.4343 -  init_mparams(); /* Ensure pagesize etc initialized */
  1.4344 -
  1.4345 -  if (capacity > msize + TOP_FOOT_SIZE &&
  1.4346 -      capacity < (size_t) -(msize + TOP_FOOT_SIZE + mparams.page_size)) {
  1.4347 -    m = init_user_mstate((char*)base, capacity);
  1.4348 -    m->seg.sflags = EXTERN_BIT;
  1.4349 -    set_lock(m, locked);
  1.4350 -  }
  1.4351 -  return (mspace)m;
  1.4352 +mspace
  1.4353 +create_mspace_with_base (void *base, size_t capacity, int locked)
  1.4354 +{
  1.4355 +    mstate m = 0;
  1.4356 +    size_t msize = pad_request (sizeof (struct malloc_state));
  1.4357 +    init_mparams ();            /* Ensure pagesize etc initialized */
  1.4358 +
  1.4359 +    if (capacity > msize + TOP_FOOT_SIZE &&
  1.4360 +        capacity < (size_t) - (msize + TOP_FOOT_SIZE + mparams.page_size)) {
  1.4361 +        m = init_user_mstate ((char *) base, capacity);
  1.4362 +        m->seg.sflags = EXTERN_BIT;
  1.4363 +        set_lock (m, locked);
  1.4364 +    }
  1.4365 +    return (mspace) m;
  1.4366  }
  1.4367  
  1.4368 -size_t destroy_mspace(mspace msp) {
  1.4369 -  size_t freed = 0;
  1.4370 -  mstate ms = (mstate)msp;
  1.4371 -  if (ok_magic(ms)) {
  1.4372 -    msegmentptr sp = &ms->seg;
  1.4373 -    while (sp != 0) {
  1.4374 -      char* base = sp->base;
  1.4375 -      size_t size = sp->size;
  1.4376 -      flag_t flag = sp->sflags;
  1.4377 -      sp = sp->next;
  1.4378 -      if ((flag & IS_MMAPPED_BIT) && !(flag & EXTERN_BIT) &&
  1.4379 -          CALL_MUNMAP(base, size) == 0)
  1.4380 -        freed += size;
  1.4381 +size_t
  1.4382 +destroy_mspace (mspace msp)
  1.4383 +{
  1.4384 +    size_t freed = 0;
  1.4385 +    mstate ms = (mstate) msp;
  1.4386 +    if (ok_magic (ms)) {
  1.4387 +        msegmentptr sp = &ms->seg;
  1.4388 +        while (sp != 0) {
  1.4389 +            char *base = sp->base;
  1.4390 +            size_t size = sp->size;
  1.4391 +            flag_t flag = sp->sflags;
  1.4392 +            sp = sp->next;
  1.4393 +            if ((flag & IS_MMAPPED_BIT) && !(flag & EXTERN_BIT) &&
  1.4394 +                CALL_MUNMAP (base, size) == 0)
  1.4395 +                freed += size;
  1.4396 +        }
  1.4397 +    } else {
  1.4398 +        USAGE_ERROR_ACTION (ms, ms);
  1.4399      }
  1.4400 -  }
  1.4401 -  else {
  1.4402 -    USAGE_ERROR_ACTION(ms,ms);
  1.4403 -  }
  1.4404 -  return freed;
  1.4405 +    return freed;
  1.4406  }
  1.4407  
  1.4408  /*
  1.4409 @@ -4493,344 +4604,363 @@
  1.4410  */
  1.4411  
  1.4412  
  1.4413 -void* mspace_malloc(mspace msp, size_t bytes) {
  1.4414 -  mstate ms = (mstate)msp;
  1.4415 -  if (!ok_magic(ms)) {
  1.4416 -    USAGE_ERROR_ACTION(ms,ms);
  1.4417 -    return 0;
  1.4418 -  }
  1.4419 -  if (!PREACTION(ms)) {
  1.4420 -    void* mem;
  1.4421 -    size_t nb;
  1.4422 -    if (bytes <= MAX_SMALL_REQUEST) {
  1.4423 -      bindex_t idx;
  1.4424 -      binmap_t smallbits;
  1.4425 -      nb = (bytes < MIN_REQUEST)? MIN_CHUNK_SIZE : pad_request(bytes);
  1.4426 -      idx = small_index(nb);
  1.4427 -      smallbits = ms->smallmap >> idx;
  1.4428 -
  1.4429 -      if ((smallbits & 0x3U) != 0) { /* Remainderless fit to a smallbin. */
  1.4430 -        mchunkptr b, p;
  1.4431 -        idx += ~smallbits & 1;       /* Uses next bin if idx empty */
  1.4432 -        b = smallbin_at(ms, idx);
  1.4433 -        p = b->fd;
  1.4434 -        assert(chunksize(p) == small_index2size(idx));
  1.4435 -        unlink_first_small_chunk(ms, b, p, idx);
  1.4436 -        set_inuse_and_pinuse(ms, p, small_index2size(idx));
  1.4437 -        mem = chunk2mem(p);
  1.4438 -        check_malloced_chunk(ms, mem, nb);
  1.4439 -        goto postaction;
  1.4440 -      }
  1.4441 -
  1.4442 -      else if (nb > ms->dvsize) {
  1.4443 -        if (smallbits != 0) { /* Use chunk in next nonempty smallbin */
  1.4444 -          mchunkptr b, p, r;
  1.4445 -          size_t rsize;
  1.4446 -          bindex_t i;
  1.4447 -          binmap_t leftbits = (smallbits << idx) & left_bits(idx2bit(idx));
  1.4448 -          binmap_t leastbit = least_bit(leftbits);
  1.4449 -          compute_bit2idx(leastbit, i);
  1.4450 -          b = smallbin_at(ms, i);
  1.4451 -          p = b->fd;
  1.4452 -          assert(chunksize(p) == small_index2size(i));
  1.4453 -          unlink_first_small_chunk(ms, b, p, i);
  1.4454 -          rsize = small_index2size(i) - nb;
  1.4455 -          /* Fit here cannot be remainderless if 4byte sizes */
  1.4456 -          if (SIZE_T_SIZE != 4 && rsize < MIN_CHUNK_SIZE)
  1.4457 -            set_inuse_and_pinuse(ms, p, small_index2size(i));
  1.4458 -          else {
  1.4459 -            set_size_and_pinuse_of_inuse_chunk(ms, p, nb);
  1.4460 -            r = chunk_plus_offset(p, nb);
  1.4461 -            set_size_and_pinuse_of_free_chunk(r, rsize);
  1.4462 -            replace_dv(ms, r, rsize);
  1.4463 -          }
  1.4464 -          mem = chunk2mem(p);
  1.4465 -          check_malloced_chunk(ms, mem, nb);
  1.4466 -          goto postaction;
  1.4467 -        }
  1.4468 -
  1.4469 -        else if (ms->treemap != 0 && (mem = tmalloc_small(ms, nb)) != 0) {
  1.4470 -          check_malloced_chunk(ms, mem, nb);
  1.4471 -          goto postaction;
  1.4472 -        }
  1.4473 -      }
  1.4474 -    }
  1.4475 -    else if (bytes >= MAX_REQUEST)
  1.4476 -      nb = MAX_SIZE_T; /* Too big to allocate. Force failure (in sys alloc) */
  1.4477 -    else {
  1.4478 -      nb = pad_request(bytes);
  1.4479 -      if (ms->treemap != 0 && (mem = tmalloc_large(ms, nb)) != 0) {
  1.4480 -        check_malloced_chunk(ms, mem, nb);
  1.4481 -        goto postaction;
  1.4482 -      }
  1.4483 +void *
  1.4484 +mspace_malloc (mspace msp, size_t bytes)
  1.4485 +{
  1.4486 +    mstate ms = (mstate) msp;
  1.4487 +    if (!ok_magic (ms)) {
  1.4488 +        USAGE_ERROR_ACTION (ms, ms);
  1.4489 +        return 0;
  1.4490      }
  1.4491 -
  1.4492 -    if (nb <= ms->dvsize) {
  1.4493 -      size_t rsize = ms->dvsize - nb;
  1.4494 -      mchunkptr p = ms->dv;
  1.4495 -      if (rsize >= MIN_CHUNK_SIZE) { /* split dv */
  1.4496 -        mchunkptr r = ms->dv = chunk_plus_offset(p, nb);
  1.4497 -        ms->dvsize = rsize;
  1.4498 -        set_size_and_pinuse_of_free_chunk(r, rsize);
  1.4499 -        set_size_and_pinuse_of_inuse_chunk(ms, p, nb);
  1.4500 -      }
  1.4501 -      else { /* exhaust dv */
  1.4502 -        size_t dvs = ms->dvsize;
  1.4503 -        ms->dvsize = 0;
  1.4504 -        ms->dv = 0;
  1.4505 -        set_inuse_and_pinuse(ms, p, dvs);
  1.4506 -      }
  1.4507 -      mem = chunk2mem(p);
  1.4508 -      check_malloced_chunk(ms, mem, nb);
  1.4509 -      goto postaction;
  1.4510 +    if (!PREACTION (ms)) {
  1.4511 +        void *mem;
  1.4512 +        size_t nb;
  1.4513 +        if (bytes <= MAX_SMALL_REQUEST) {
  1.4514 +            bindex_t idx;
  1.4515 +            binmap_t smallbits;
  1.4516 +            nb = (bytes < MIN_REQUEST) ? MIN_CHUNK_SIZE : pad_request (bytes);
  1.4517 +            idx = small_index (nb);
  1.4518 +            smallbits = ms->smallmap >> idx;
  1.4519 +
  1.4520 +            if ((smallbits & 0x3U) != 0) {      /* Remainderless fit to a smallbin. */
  1.4521 +                mchunkptr b, p;
  1.4522 +                idx += ~smallbits & 1;  /* Uses next bin if idx empty */
  1.4523 +                b = smallbin_at (ms, idx);
  1.4524 +                p = b->fd;
  1.4525 +                assert (chunksize (p) == small_index2size (idx));
  1.4526 +                unlink_first_small_chunk (ms, b, p, idx);
  1.4527 +                set_inuse_and_pinuse (ms, p, small_index2size (idx));
  1.4528 +                mem = chunk2mem (p);
  1.4529 +                check_malloced_chunk (ms, mem, nb);
  1.4530 +                goto postaction;
  1.4531 +            }
  1.4532 +
  1.4533 +            else if (nb > ms->dvsize) {
  1.4534 +                if (smallbits != 0) {   /* Use chunk in next nonempty smallbin */
  1.4535 +                    mchunkptr b, p, r;
  1.4536 +                    size_t rsize;
  1.4537 +                    bindex_t i;
  1.4538 +                    binmap_t leftbits =
  1.4539 +                        (smallbits << idx) & left_bits (idx2bit (idx));
  1.4540 +                    binmap_t leastbit = least_bit (leftbits);
  1.4541 +                    compute_bit2idx (leastbit, i);
  1.4542 +                    b = smallbin_at (ms, i);
  1.4543 +                    p = b->fd;
  1.4544 +                    assert (chunksize (p) == small_index2size (i));
  1.4545 +                    unlink_first_small_chunk (ms, b, p, i);
  1.4546 +                    rsize = small_index2size (i) - nb;
  1.4547 +                    /* Fit here cannot be remainderless if 4byte sizes */
  1.4548 +                    if (SIZE_T_SIZE != 4 && rsize < MIN_CHUNK_SIZE)
  1.4549 +                        set_inuse_and_pinuse (ms, p, small_index2size (i));
  1.4550 +                    else {
  1.4551 +                        set_size_and_pinuse_of_inuse_chunk (ms, p, nb);
  1.4552 +                        r = chunk_plus_offset (p, nb);
  1.4553 +                        set_size_and_pinuse_of_free_chunk (r, rsize);
  1.4554 +                        replace_dv (ms, r, rsize);
  1.4555 +                    }
  1.4556 +                    mem = chunk2mem (p);
  1.4557 +                    check_malloced_chunk (ms, mem, nb);
  1.4558 +                    goto postaction;
  1.4559 +                }
  1.4560 +
  1.4561 +                else if (ms->treemap != 0
  1.4562 +                         && (mem = tmalloc_small (ms, nb)) != 0) {
  1.4563 +                    check_malloced_chunk (ms, mem, nb);
  1.4564 +                    goto postaction;
  1.4565 +                }
  1.4566 +            }
  1.4567 +        } else if (bytes >= MAX_REQUEST)
  1.4568 +            nb = MAX_SIZE_T;    /* Too big to allocate. Force failure (in sys alloc) */
  1.4569 +        else {
  1.4570 +            nb = pad_request (bytes);
  1.4571 +            if (ms->treemap != 0 && (mem = tmalloc_large (ms, nb)) != 0) {
  1.4572 +                check_malloced_chunk (ms, mem, nb);
  1.4573 +                goto postaction;
  1.4574 +            }
  1.4575 +        }
  1.4576 +
  1.4577 +        if (nb <= ms->dvsize) {
  1.4578 +            size_t rsize = ms->dvsize - nb;
  1.4579 +            mchunkptr p = ms->dv;
  1.4580 +            if (rsize >= MIN_CHUNK_SIZE) {      /* split dv */
  1.4581 +                mchunkptr r = ms->dv = chunk_plus_offset (p, nb);
  1.4582 +                ms->dvsize = rsize;
  1.4583 +                set_size_and_pinuse_of_free_chunk (r, rsize);
  1.4584 +                set_size_and_pinuse_of_inuse_chunk (ms, p, nb);
  1.4585 +            } else {            /* exhaust dv */
  1.4586 +                size_t dvs = ms->dvsize;
  1.4587 +                ms->dvsize = 0;
  1.4588 +                ms->dv = 0;
  1.4589 +                set_inuse_and_pinuse (ms, p, dvs);
  1.4590 +            }
  1.4591 +            mem = chunk2mem (p);
  1.4592 +            check_malloced_chunk (ms, mem, nb);
  1.4593 +            goto postaction;
  1.4594 +        }
  1.4595 +
  1.4596 +        else if (nb < ms->topsize) {    /* Split top */
  1.4597 +            size_t rsize = ms->topsize -= nb;
  1.4598 +            mchunkptr p = ms->top;
  1.4599 +            mchunkptr r = ms->top = chunk_plus_offset (p, nb);
  1.4600 +            r->head = rsize | PINUSE_BIT;
  1.4601 +            set_size_and_pinuse_of_inuse_chunk (ms, p, nb);
  1.4602 +            mem = chunk2mem (p);
  1.4603 +            check_top_chunk (ms, ms->top);
  1.4604 +            check_malloced_chunk (ms, mem, nb);
  1.4605 +            goto postaction;
  1.4606 +        }
  1.4607 +
  1.4608 +        mem = sys_alloc (ms, nb);
  1.4609 +
  1.4610 +      postaction:
  1.4611 +        POSTACTION (ms);
  1.4612 +        return mem;
  1.4613      }
  1.4614  
  1.4615 -    else if (nb < ms->topsize) { /* Split top */
  1.4616 -      size_t rsize = ms->topsize -= nb;
  1.4617 -      mchunkptr p = ms->top;
  1.4618 -      mchunkptr r = ms->top = chunk_plus_offset(p, nb);
  1.4619 -      r->head = rsize | PINUSE_BIT;
  1.4620 -      set_size_and_pinuse_of_inuse_chunk(ms, p, nb);
  1.4621 -      mem = chunk2mem(p);
  1.4622 -      check_top_chunk(ms, ms->top);
  1.4623 -      check_malloced_chunk(ms, mem, nb);
  1.4624 -      goto postaction;
  1.4625 -    }
  1.4626 -
  1.4627 -    mem = sys_alloc(ms, nb);
  1.4628 -
  1.4629 -  postaction:
  1.4630 -    POSTACTION(ms);
  1.4631 -    return mem;
  1.4632 -  }
  1.4633 -
  1.4634 -  return 0;
  1.4635 +    return 0;
  1.4636  }
  1.4637  
  1.4638 -void mspace_free(mspace msp, void* mem) {
  1.4639 -  if (mem != 0) {
  1.4640 -    mchunkptr p  = mem2chunk(mem);
  1.4641 +void
  1.4642 +mspace_free (mspace msp, void *mem)
  1.4643 +{
  1.4644 +    if (mem != 0) {
  1.4645 +        mchunkptr p = mem2chunk (mem);
  1.4646  #if FOOTERS
  1.4647 -    mstate fm = get_mstate_for(p);
  1.4648 +        mstate fm = get_mstate_for (p);
  1.4649  #else /* FOOTERS */
  1.4650 -    mstate fm = (mstate)msp;
  1.4651 +        mstate fm = (mstate) msp;
  1.4652  #endif /* FOOTERS */
  1.4653 -    if (!ok_magic(fm)) {
  1.4654 -      USAGE_ERROR_ACTION(fm, p);
  1.4655 -      return;
  1.4656 -    }
  1.4657 -    if (!PREACTION(fm)) {
  1.4658 -      check_inuse_chunk(fm, p);
  1.4659 -      if (RTCHECK(ok_address(fm, p) && ok_cinuse(p))) {
  1.4660 -        size_t psize = chunksize(p);
  1.4661 -        mchunkptr next = chunk_plus_offset(p, psize);
  1.4662 -        if (!pinuse(p)) {
  1.4663 -          size_t prevsize = p->prev_foot;
  1.4664 -          if ((prevsize & IS_MMAPPED_BIT) != 0) {
  1.4665 -            prevsize &= ~IS_MMAPPED_BIT;
  1.4666 -            psize += prevsize + MMAP_FOOT_PAD;
  1.4667 -            if (CALL_MUNMAP((char*)p - prevsize, psize) == 0)
  1.4668 -              fm->footprint -= psize;
  1.4669 -            goto postaction;
  1.4670 -          }
  1.4671 -          else {
  1.4672 -            mchunkptr prev = chunk_minus_offset(p, prevsize);
  1.4673 -            psize += prevsize;
  1.4674 -            p = prev;
  1.4675 -            if (RTCHECK(ok_address(fm, prev))) { /* consolidate backward */
  1.4676 -              if (p != fm->dv) {
  1.4677 -                unlink_chunk(fm, p, prevsize);
  1.4678 -              }
  1.4679 -              else if ((next->head & INUSE_BITS) == INUSE_BITS) {
  1.4680 -                fm->dvsize = psize;
  1.4681 -                set_free_with_pinuse(p, psize, next);
  1.4682 -                goto postaction;
  1.4683 -              }
  1.4684 -            }
  1.4685 -            else
  1.4686 -              goto erroraction;
  1.4687 -          }
  1.4688 +        if (!ok_magic (fm)) {
  1.4689 +            USAGE_ERROR_ACTION (fm, p);
  1.4690 +            return;
  1.4691          }
  1.4692 -
  1.4693 -        if (RTCHECK(ok_next(p, next) && ok_pinuse(next))) {
  1.4694 -          if (!cinuse(next)) {  /* consolidate forward */
  1.4695 -            if (next == fm->top) {
  1.4696 -              size_t tsize = fm->topsize += psize;
  1.4697 -              fm->top = p;
  1.4698 -              p->head = tsize | PINUSE_BIT;
  1.4699 -              if (p == fm->dv) {
  1.4700 -                fm->dv = 0;
  1.4701 -                fm->dvsize = 0;
  1.4702 -              }
  1.4703 -              if (should_trim(fm, tsize))
  1.4704 -                sys_trim(fm, 0);
  1.4705 -              goto postaction;
  1.4706 +        if (!PREACTION (fm)) {
  1.4707 +            check_inuse_chunk (fm, p);
  1.4708 +            if (RTCHECK (ok_address (fm, p) && ok_cinuse (p))) {
  1.4709 +                size_t psize = chunksize (p);
  1.4710 +                mchunkptr next = chunk_plus_offset (p, psize);
  1.4711 +                if (!pinuse (p)) {
  1.4712 +                    size_t prevsize = p->prev_foot;
  1.4713 +                    if ((prevsize & IS_MMAPPED_BIT) != 0) {
  1.4714 +                        prevsize &= ~IS_MMAPPED_BIT;
  1.4715 +                        psize += prevsize + MMAP_FOOT_PAD;
  1.4716 +                        if (CALL_MUNMAP ((char *) p - prevsize, psize) == 0)
  1.4717 +                            fm->footprint -= psize;
  1.4718 +                        goto postaction;
  1.4719 +                    } else {
  1.4720 +                        mchunkptr prev = chunk_minus_offset (p, prevsize);
  1.4721 +                        psize += prevsize;
  1.4722 +                        p = prev;
  1.4723 +                        if (RTCHECK (ok_address (fm, prev))) {  /* consolidate backward */
  1.4724 +                            if (p != fm->dv) {
  1.4725 +                                unlink_chunk (fm, p, prevsize);
  1.4726 +                            } else if ((next->head & INUSE_BITS) ==
  1.4727 +                                       INUSE_BITS) {
  1.4728 +                                fm->dvsize = psize;
  1.4729 +                                set_free_with_pinuse (p, psize, next);
  1.4730 +                                goto postaction;
  1.4731 +                            }
  1.4732 +                        } else
  1.4733 +                            goto erroraction;
  1.4734 +                    }
  1.4735 +                }
  1.4736 +
  1.4737 +                if (RTCHECK (ok_next (p, next) && ok_pinuse (next))) {
  1.4738 +                    if (!cinuse (next)) {       /* consolidate forward */
  1.4739 +                        if (next == fm->top) {
  1.4740 +                            size_t tsize = fm->topsize += psize;
  1.4741 +                            fm->top = p;
  1.4742 +                            p->head = tsize | PINUSE_BIT;
  1.4743 +                            if (p == fm->dv) {
  1.4744 +                                fm->dv = 0;
  1.4745 +                                fm->dvsize = 0;
  1.4746 +                            }
  1.4747 +                            if (should_trim (fm, tsize))
  1.4748 +                                sys_trim (fm, 0);
  1.4749 +                            goto postaction;
  1.4750 +                        } else if (next == fm->dv) {
  1.4751 +                            size_t dsize = fm->dvsize += psize;
  1.4752 +                            fm->dv = p;
  1.4753 +                            set_size_and_pinuse_of_free_chunk (p, dsize);
  1.4754 +                            goto postaction;
  1.4755 +                        } else {
  1.4756 +                            size_t nsize = chunksize (next);
  1.4757 +                            psize += nsize;
  1.4758 +                            unlink_chunk (fm, next, nsize);
  1.4759 +                            set_size_and_pinuse_of_free_chunk (p, psize);
  1.4760 +                            if (p == fm->dv) {
  1.4761 +                                fm->dvsize = psize;
  1.4762 +                                goto postaction;
  1.4763 +                            }
  1.4764 +                        }
  1.4765 +                    } else
  1.4766 +                        set_free_with_pinuse (p, psize, next);
  1.4767 +                    insert_chunk (fm, p, psize);
  1.4768 +                    check_free_chunk (fm, p);
  1.4769 +                    goto postaction;
  1.4770 +                }
  1.4771              }
  1.4772 -            else if (next == fm->dv) {
  1.4773 -              size_t dsize = fm->dvsize += psize;
  1.4774 -              fm->dv = p;
  1.4775 -              set_size_and_pinuse_of_free_chunk(p, dsize);
  1.4776 -              goto postaction;
  1.4777 -            }
  1.4778 -            else {
  1.4779 -              size_t nsize = chunksize(next);
  1.4780 -              psize += nsize;
  1.4781 -              unlink_chunk(fm, next, nsize);
  1.4782 -              set_size_and_pinuse_of_free_chunk(p, psize);
  1.4783 -              if (p == fm->dv) {
  1.4784 -                fm->dvsize = psize;
  1.4785 -                goto postaction;
  1.4786 -              }
  1.4787 -            }
  1.4788 -          }
  1.4789 -          else
  1.4790 -            set_free_with_pinuse(p, psize, next);
  1.4791 -          insert_chunk(fm, p, psize);
  1.4792 -          check_free_chunk(fm, p);
  1.4793 -          goto postaction;
  1.4794 +          erroraction:
  1.4795 +            USAGE_ERROR_ACTION (fm, p);
  1.4796 +          postaction:
  1.4797 +            POSTACTION (fm);
  1.4798          }
  1.4799 -      }
  1.4800 -    erroraction:
  1.4801 -      USAGE_ERROR_ACTION(fm, p);
  1.4802 -    postaction:
  1.4803 -      POSTACTION(fm);
  1.4804      }
  1.4805 -  }
  1.4806  }
  1.4807  
  1.4808 -void* mspace_calloc(mspace msp, size_t n_elements, size_t elem_size) {
  1.4809 -  void* mem;
  1.4810 -  size_t req = 0;
  1.4811 -  mstate ms = (mstate)msp;
  1.4812 -  if (!ok_magic(ms)) {
  1.4813 -    USAGE_ERROR_ACTION(ms,ms);
  1.4814 -    return 0;
  1.4815 -  }
  1.4816 -  if (n_elements != 0) {
  1.4817 -    req = n_elements * elem_size;
  1.4818 -    if (((n_elements | elem_size) & ~(size_t)0xffff) &&
  1.4819 -        (req / n_elements != elem_size))
  1.4820 -      req = MAX_SIZE_T; /* force downstream failure on overflow */
  1.4821 -  }
  1.4822 -  mem = internal_malloc(ms, req);
  1.4823 -  if (mem != 0 && calloc_must_clear(mem2chunk(mem)))
  1.4824 -    memset(mem, 0, req);
  1.4825 -  return mem;
  1.4826 +void *
  1.4827 +mspace_calloc (mspace msp, size_t n_elements, size_t elem_size)
  1.4828 +{
  1.4829 +    void *mem;
  1.4830 +    size_t req = 0;
  1.4831 +    mstate ms = (mstate) msp;
  1.4832 +    if (!ok_magic (ms)) {
  1.4833 +        USAGE_ERROR_ACTION (ms, ms);
  1.4834 +        return 0;
  1.4835 +    }
  1.4836 +    if (n_elements != 0) {
  1.4837 +        req = n_elements * elem_size;
  1.4838 +        if (((n_elements | elem_size) & ~(size_t) 0xffff) &&
  1.4839 +            (req / n_elements != elem_size))
  1.4840 +            req = MAX_SIZE_T;   /* force downstream failure on overflow */
  1.4841 +    }
  1.4842 +    mem = internal_malloc (ms, req);
  1.4843 +    if (mem != 0 && calloc_must_clear (mem2chunk (mem)))
  1.4844 +        memset (mem, 0, req);
  1.4845 +    return mem;
  1.4846  }
  1.4847  
  1.4848 -void* mspace_realloc(mspace msp, void* oldmem, size_t bytes) {
  1.4849 -  if (oldmem == 0)
  1.4850 -    return mspace_malloc(msp, bytes);
  1.4851 +void *
  1.4852 +mspace_realloc (mspace msp, void *oldmem, size_t bytes)
  1.4853 +{
  1.4854 +    if (oldmem == 0)
  1.4855 +        return mspace_malloc (msp, bytes);
  1.4856  #ifdef REALLOC_ZERO_BYTES_FREES
  1.4857 -  if (bytes == 0) {
  1.4858 -    mspace_free(msp, oldmem);
  1.4859 -    return 0;
  1.4860 -  }
  1.4861 +    if (bytes == 0) {
  1.4862 +        mspace_free (msp, oldmem);
  1.4863 +        return 0;
  1.4864 +    }
  1.4865  #endif /* REALLOC_ZERO_BYTES_FREES */
  1.4866 -  else {
  1.4867 +    else {
  1.4868  #if FOOTERS
  1.4869 -    mchunkptr p  = mem2chunk(oldmem);
  1.4870 -    mstate ms = get_mstate_for(p);
  1.4871 +        mchunkptr p = mem2chunk (oldmem);
  1.4872 +        mstate ms = get_mstate_for (p);
  1.4873  #else /* FOOTERS */
  1.4874 -    mstate ms = (mstate)msp;
  1.4875 +        mstate ms = (mstate) msp;
  1.4876  #endif /* FOOTERS */
  1.4877 -    if (!ok_magic(ms)) {
  1.4878 -      USAGE_ERROR_ACTION(ms,ms);
  1.4879 -      return 0;
  1.4880 +        if (!ok_magic (ms)) {
  1.4881 +            USAGE_ERROR_ACTION (ms, ms);
  1.4882 +            return 0;
  1.4883 +        }
  1.4884 +        return internal_realloc (ms, oldmem, bytes);
  1.4885      }
  1.4886 -    return internal_realloc(ms, oldmem, bytes);
  1.4887 -  }
  1.4888  }
  1.4889  
  1.4890 -void* mspace_memalign(mspace msp, size_t alignment, size_t bytes) {
  1.4891 -  mstate ms = (mstate)msp;
  1.4892 -  if (!ok_magic(ms)) {
  1.4893 -    USAGE_ERROR_ACTION(ms,ms);
  1.4894 -    return 0;
  1.4895 -  }
  1.4896 -  return internal_memalign(ms, alignment, bytes);
  1.4897 +void *
  1.4898 +mspace_memalign (mspace msp, size_t alignment, size_t bytes)
  1.4899 +{
  1.4900 +    mstate ms = (mstate) msp;
  1.4901 +    if (!ok_magic (ms)) {
  1.4902 +        USAGE_ERROR_ACTION (ms, ms);
  1.4903 +        return 0;
  1.4904 +    }
  1.4905 +    return internal_memalign (ms, alignment, bytes);
  1.4906  }
  1.4907  
  1.4908 -void** mspace_independent_calloc(mspace msp, size_t n_elements,
  1.4909 -                                 size_t elem_size, void* chunks[]) {
  1.4910 -  size_t sz = elem_size; /* serves as 1-element array */
  1.4911 -  mstate ms = (mstate)msp;
  1.4912 -  if (!ok_magic(ms)) {
  1.4913 -    USAGE_ERROR_ACTION(ms,ms);
  1.4914 -    return 0;
  1.4915 -  }
  1.4916 -  return ialloc(ms, n_elements, &sz, 3, chunks);
  1.4917 +void **
  1.4918 +mspace_independent_calloc (mspace msp, size_t n_elements,
  1.4919 +                           size_t elem_size, void *chunks[])
  1.4920 +{
  1.4921 +    size_t sz = elem_size;      /* serves as 1-element array */
  1.4922 +    mstate ms = (mstate) msp;
  1.4923 +    if (!ok_magic (ms)) {
  1.4924 +        USAGE_ERROR_ACTION (ms, ms);
  1.4925 +        return 0;
  1.4926 +    }
  1.4927 +    return ialloc (ms, n_elements, &sz, 3, chunks);
  1.4928  }
  1.4929  
  1.4930 -void** mspace_independent_comalloc(mspace msp, size_t n_elements,
  1.4931 -                                   size_t sizes[], void* chunks[]) {
  1.4932 -  mstate ms = (mstate)msp;
  1.4933 -  if (!ok_magic(ms)) {
  1.4934 -    USAGE_ERROR_ACTION(ms,ms);
  1.4935 -    return 0;
  1.4936 -  }
  1.4937 -  return ialloc(ms, n_elements, sizes, 0, chunks);
  1.4938 +void **
  1.4939 +mspace_independent_comalloc (mspace msp, size_t n_elements,
  1.4940 +                             size_t sizes[], void *chunks[])
  1.4941 +{
  1.4942 +    mstate ms = (mstate) msp;
  1.4943 +    if (!ok_magic (ms)) {
  1.4944 +        USAGE_ERROR_ACTION (ms, ms);
  1.4945 +        return 0;
  1.4946 +    }
  1.4947 +    return ialloc (ms, n_elements, sizes, 0, chunks);
  1.4948  }
  1.4949  
  1.4950 -int mspace_trim(mspace msp, size_t pad) {
  1.4951 -  int result = 0;
  1.4952 -  mstate ms = (mstate)msp;
  1.4953 -  if (ok_magic(ms)) {
  1.4954 -    if (!PREACTION(ms)) {
  1.4955 -      result = sys_trim(ms, pad);
  1.4956 -      POSTACTION(ms);
  1.4957 +int
  1.4958 +mspace_trim (mspace msp, size_t pad)
  1.4959 +{
  1.4960 +    int result = 0;
  1.4961 +    mstate ms = (mstate) msp;
  1.4962 +    if (ok_magic (ms)) {
  1.4963 +        if (!PREACTION (ms)) {
  1.4964 +            result = sys_trim (ms, pad);
  1.4965 +            POSTACTION (ms);
  1.4966 +        }
  1.4967 +    } else {
  1.4968 +        USAGE_ERROR_ACTION (ms, ms);
  1.4969      }
  1.4970 -  }
  1.4971 -  else {
  1.4972 -    USAGE_ERROR_ACTION(ms,ms);
  1.4973 -  }
  1.4974 -  return result;
  1.4975 +    return result;
  1.4976  }
  1.4977  
  1.4978 -void mspace_malloc_stats(mspace msp) {
  1.4979 -  mstate ms = (mstate)msp;
  1.4980 -  if (ok_magic(ms)) {
  1.4981 -    internal_malloc_stats(ms);
  1.4982 -  }
  1.4983 -  else {
  1.4984 -    USAGE_ERROR_ACTION(ms,ms);
  1.4985 -  }
  1.4986 +void
  1.4987 +mspace_malloc_stats (mspace msp)
  1.4988 +{
  1.4989 +    mstate ms = (mstate) msp;
  1.4990 +    if (ok_magic (ms)) {
  1.4991 +        internal_malloc_stats (ms);
  1.4992 +    } else {
  1.4993 +        USAGE_ERROR_ACTION (ms, ms);
  1.4994 +    }
  1.4995  }
  1.4996  
  1.4997 -size_t mspace_footprint(mspace msp) {
  1.4998 -  size_t result;
  1.4999 -  mstate ms = (mstate)msp;
  1.5000 -  if (ok_magic(ms)) {
  1.5001 -    result = ms->footprint;
  1.5002 -  }
  1.5003 -  USAGE_ERROR_ACTION(ms,ms);
  1.5004 -  return result;
  1.5005 +size_t
  1.5006 +mspace_footprint (mspace msp)
  1.5007 +{
  1.5008 +    size_t result;
  1.5009 +    mstate ms = (mstate) msp;
  1.5010 +    if (ok_magic (ms)) {
  1.5011 +        result = ms->footprint;
  1.5012 +    }
  1.5013 +    USAGE_ERROR_ACTION (ms, ms);
  1.5014 +    return result;
  1.5015  }
  1.5016  
  1.5017  
  1.5018 -size_t mspace_max_footprint(mspace msp) {
  1.5019 -  size_t result;
  1.5020 -  mstate ms = (mstate)msp;
  1.5021 -  if (ok_magic(ms)) {
  1.5022 -    result = ms->max_footprint;
  1.5023 -  }
  1.5024 -  USAGE_ERROR_ACTION(ms,ms);
  1.5025 -  return result;
  1.5026 +size_t
  1.5027 +mspace_max_footprint (mspace msp)
  1.5028 +{
  1.5029 +    size_t result;
  1.5030 +    mstate ms = (mstate) msp;
  1.5031 +    if (ok_magic (ms)) {
  1.5032 +        result = ms->max_footprint;
  1.5033 +    }
  1.5034 +    USAGE_ERROR_ACTION (ms, ms);
  1.5035 +    return result;
  1.5036  }
  1.5037  
  1.5038  
  1.5039  #if !NO_MALLINFO
  1.5040 -struct mallinfo mspace_mallinfo(mspace msp) {
  1.5041 -  mstate ms = (mstate)msp;
  1.5042 -  if (!ok_magic(ms)) {
  1.5043 -    USAGE_ERROR_ACTION(ms,ms);
  1.5044 -  }
  1.5045 -  return internal_mallinfo(ms);
  1.5046 +struct mallinfo
  1.5047 +mspace_mallinfo (mspace msp)
  1.5048 +{
  1.5049 +    mstate ms = (mstate) msp;
  1.5050 +    if (!ok_magic (ms)) {
  1.5051 +        USAGE_ERROR_ACTION (ms, ms);
  1.5052 +    }
  1.5053 +    return internal_mallinfo (ms);
  1.5054  }
  1.5055  #endif /* NO_MALLINFO */
  1.5056  
  1.5057 -int mspace_mallopt(int param_number, int value) {
  1.5058 -  return change_mparam(param_number, value);
  1.5059 +int
  1.5060 +mspace_mallopt (int param_number, int value)
  1.5061 +{
  1.5062 +    return change_mparam (param_number, value);
  1.5063  }
  1.5064  
  1.5065  #endif /* MSPACES */
  1.5066 @@ -5109,3 +5239,4 @@
  1.5067  */
  1.5068  
  1.5069  #endif /* !HAVE_MALLOC */
  1.5070 +/* vi: set ts=4 sw=4 expandtab: */