Home | History | Annotate | Line # | Download | only in stdlib
jemalloc.c revision 1.49
      1  1.49     joerg /*	$NetBSD: jemalloc.c,v 1.49 2020/04/21 22:22:55 joerg Exp $	*/
      2   1.2        ad 
      3   1.1        ad /*-
      4   1.1        ad  * Copyright (C) 2006,2007 Jason Evans <jasone (at) FreeBSD.org>.
      5   1.1        ad  * All rights reserved.
      6   1.1        ad  *
      7   1.1        ad  * Redistribution and use in source and binary forms, with or without
      8   1.1        ad  * modification, are permitted provided that the following conditions
      9   1.1        ad  * are met:
     10   1.1        ad  * 1. Redistributions of source code must retain the above copyright
     11   1.1        ad  *    notice(s), this list of conditions and the following disclaimer as
     12   1.1        ad  *    the first lines of this file unmodified other than the possible
     13   1.1        ad  *    addition of one or more copyright notices.
     14   1.1        ad  * 2. Redistributions in binary form must reproduce the above copyright
     15   1.1        ad  *    notice(s), this list of conditions and the following disclaimer in
     16   1.1        ad  *    the documentation and/or other materials provided with the
     17   1.1        ad  *    distribution.
     18   1.1        ad  *
     19   1.1        ad  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY
     20   1.1        ad  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21   1.1        ad  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22   1.1        ad  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE
     23   1.1        ad  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24   1.1        ad  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25   1.1        ad  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
     26   1.1        ad  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     27   1.1        ad  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
     28   1.1        ad  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
     29   1.1        ad  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30   1.1        ad  *
     31   1.1        ad  *******************************************************************************
     32   1.1        ad  *
     33   1.1        ad  * This allocator implementation is designed to provide scalable performance
     34   1.1        ad  * for multi-threaded programs on multi-processor systems.  The following
     35   1.1        ad  * features are included for this purpose:
     36   1.1        ad  *
     37   1.1        ad  *   + Multiple arenas are used if there are multiple CPUs, which reduces lock
     38   1.1        ad  *     contention and cache sloshing.
     39   1.1        ad  *
     40   1.1        ad  *   + Cache line sharing between arenas is avoided for internal data
     41   1.1        ad  *     structures.
     42   1.1        ad  *
     43   1.1        ad  *   + Memory is managed in chunks and runs (chunks can be split into runs),
     44   1.1        ad  *     rather than as individual pages.  This provides a constant-time
     45   1.1        ad  *     mechanism for associating allocations with particular arenas.
     46   1.1        ad  *
     47   1.1        ad  * Allocation requests are rounded up to the nearest size class, and no record
     48   1.1        ad  * of the original request size is maintained.  Allocations are broken into
     49   1.1        ad  * categories according to size class.  Assuming runtime defaults, 4 kB pages
     50   1.1        ad  * and a 16 byte quantum, the size classes in each category are as follows:
     51   1.1        ad  *
     52   1.1        ad  *   |=====================================|
     53   1.1        ad  *   | Category | Subcategory    |    Size |
     54   1.1        ad  *   |=====================================|
     55   1.1        ad  *   | Small    | Tiny           |       2 |
     56   1.1        ad  *   |          |                |       4 |
     57   1.1        ad  *   |          |                |       8 |
     58   1.1        ad  *   |          |----------------+---------|
     59   1.1        ad  *   |          | Quantum-spaced |      16 |
     60   1.1        ad  *   |          |                |      32 |
     61   1.1        ad  *   |          |                |      48 |
     62   1.1        ad  *   |          |                |     ... |
     63   1.1        ad  *   |          |                |     480 |
     64   1.1        ad  *   |          |                |     496 |
     65   1.1        ad  *   |          |                |     512 |
     66   1.1        ad  *   |          |----------------+---------|
     67   1.1        ad  *   |          | Sub-page       |    1 kB |
     68   1.1        ad  *   |          |                |    2 kB |
     69   1.1        ad  *   |=====================================|
     70   1.1        ad  *   | Large                     |    4 kB |
     71   1.1        ad  *   |                           |    8 kB |
     72   1.1        ad  *   |                           |   12 kB |
     73   1.1        ad  *   |                           |     ... |
     74   1.1        ad  *   |                           | 1012 kB |
     75   1.1        ad  *   |                           | 1016 kB |
     76   1.1        ad  *   |                           | 1020 kB |
     77   1.1        ad  *   |=====================================|
     78   1.1        ad  *   | Huge                      |    1 MB |
     79   1.1        ad  *   |                           |    2 MB |
     80   1.1        ad  *   |                           |    3 MB |
     81   1.1        ad  *   |                           |     ... |
     82   1.1        ad  *   |=====================================|
     83   1.1        ad  *
     84   1.1        ad  * A different mechanism is used for each category:
     85   1.1        ad  *
     86   1.1        ad  *   Small : Each size class is segregated into its own set of runs.  Each run
     87   1.1        ad  *           maintains a bitmap of which regions are free/allocated.
     88   1.1        ad  *
     89   1.1        ad  *   Large : Each allocation is backed by a dedicated run.  Metadata are stored
     90   1.1        ad  *           in the associated arena chunk header maps.
     91   1.1        ad  *
     92   1.1        ad  *   Huge : Each allocation is backed by a dedicated contiguous set of chunks.
     93   1.1        ad  *          Metadata are stored in a separate red-black tree.
     94   1.1        ad  *
     95   1.1        ad  *******************************************************************************
     96   1.1        ad  */
     97   1.1        ad 
     98   1.2        ad /* LINTLIBRARY */
     99   1.2        ad 
    100   1.2        ad #ifdef __NetBSD__
    101   1.2        ad #  define xutrace(a, b)		utrace("malloc", (a), (b))
    102   1.2        ad #  define __DECONST(x, y)	((x)__UNCONST(y))
    103   1.2        ad #else
    104   1.2        ad #  define xutrace(a, b)		utrace((a), (b))
    105   1.2        ad #endif	/* __NetBSD__ */
    106   1.2        ad 
    107   1.1        ad /*
    108   1.1        ad  * MALLOC_PRODUCTION disables assertions and statistics gathering.  It also
    109   1.1        ad  * defaults the A and J runtime options to off.  These settings are appropriate
    110   1.1        ad  * for production systems.
    111   1.1        ad  */
    112   1.2        ad #define MALLOC_PRODUCTION
    113   1.1        ad 
    114   1.1        ad #ifndef MALLOC_PRODUCTION
    115   1.1        ad #  define MALLOC_DEBUG
    116   1.1        ad #endif
    117   1.1        ad 
    118   1.1        ad #include <sys/cdefs.h>
    119   1.2        ad /* __FBSDID("$FreeBSD: src/lib/libc/stdlib/malloc.c,v 1.147 2007/06/15 22:00:16 jasone Exp $"); */
    120  1.49     joerg __RCSID("$NetBSD: jemalloc.c,v 1.49 2020/04/21 22:22:55 joerg Exp $");
    121   1.1        ad 
    122   1.2        ad #ifdef __FreeBSD__
    123   1.1        ad #include "libc_private.h"
    124   1.1        ad #ifdef MALLOC_DEBUG
    125   1.1        ad #  define _LOCK_DEBUG
    126   1.1        ad #endif
    127   1.1        ad #include "spinlock.h"
    128   1.2        ad #endif
    129   1.1        ad #include "namespace.h"
    130   1.1        ad #include <sys/mman.h>
    131   1.1        ad #include <sys/param.h>
    132   1.2        ad #ifdef __FreeBSD__
    133   1.1        ad #include <sys/stddef.h>
    134   1.2        ad #endif
    135   1.1        ad #include <sys/time.h>
    136   1.1        ad #include <sys/types.h>
    137   1.1        ad #include <sys/sysctl.h>
    138   1.1        ad #include <sys/tree.h>
    139   1.1        ad #include <sys/uio.h>
    140   1.1        ad #include <sys/ktrace.h> /* Must come after several other sys/ includes. */
    141   1.1        ad 
    142   1.2        ad #ifdef __FreeBSD__
    143   1.1        ad #include <machine/atomic.h>
    144   1.1        ad #include <machine/cpufunc.h>
    145  1.39  christos #include <machine/vmparam.h>
    146   1.2        ad #endif
    147   1.1        ad 
    148   1.1        ad #include <errno.h>
    149   1.1        ad #include <limits.h>
    150   1.1        ad #include <pthread.h>
    151   1.1        ad #include <sched.h>
    152   1.1        ad #include <stdarg.h>
    153   1.1        ad #include <stdbool.h>
    154   1.1        ad #include <stdio.h>
    155   1.1        ad #include <stdint.h>
    156   1.1        ad #include <stdlib.h>
    157   1.1        ad #include <string.h>
    158   1.1        ad #include <strings.h>
    159   1.1        ad #include <unistd.h>
    160   1.1        ad 
    161   1.2        ad #ifdef __NetBSD__
    162   1.2        ad #  include <reentrant.h>
    163  1.16  christos #  include "extern.h"
    164  1.16  christos 
    165  1.41  christos #define STRERROR_R(a, b, c)	strerror_r_ss(a, b, c);
    166   1.2        ad #endif
    167   1.2        ad 
    168   1.2        ad #ifdef __FreeBSD__
    169  1.16  christos #define STRERROR_R(a, b, c)	strerror_r(a, b, c);
    170   1.1        ad #include "un-namespace.h"
    171   1.2        ad #endif
    172   1.1        ad 
    173   1.1        ad /* MALLOC_STATS enables statistics calculation. */
    174   1.1        ad #ifndef MALLOC_PRODUCTION
    175   1.1        ad #  define MALLOC_STATS
    176   1.1        ad #endif
    177   1.1        ad 
    178   1.1        ad #ifdef MALLOC_DEBUG
    179   1.1        ad #  ifdef NDEBUG
    180   1.1        ad #    undef NDEBUG
    181   1.1        ad #  endif
    182   1.1        ad #else
    183   1.1        ad #  ifndef NDEBUG
    184   1.1        ad #    define NDEBUG
    185   1.1        ad #  endif
    186   1.1        ad #endif
    187   1.1        ad #include <assert.h>
    188   1.1        ad 
    189   1.1        ad #ifdef MALLOC_DEBUG
    190   1.1        ad    /* Disable inlining to make debugging easier. */
    191   1.1        ad #  define inline
    192   1.1        ad #endif
    193   1.1        ad 
    194   1.1        ad /* Size of stack-allocated buffer passed to strerror_r(). */
    195   1.1        ad #define	STRERROR_BUF		64
    196   1.1        ad 
    197   1.1        ad /* Minimum alignment of allocations is 2^QUANTUM_2POW_MIN bytes. */
    198  1.31    martin 
    199  1.31    martin /*
    200  1.31    martin  * If you touch the TINY_MIN_2POW definition for any architecture, please
    201  1.31    martin  * make sure to adjust the corresponding definition for JEMALLOC_TINY_MIN_2POW
    202  1.31    martin  * in the gcc 4.8 tree in dist/gcc/tree-ssa-ccp.c and verify that a native
    203  1.31    martin  * gcc is still buildable!
    204  1.31    martin  */
    205  1.31    martin 
    206   1.1        ad #ifdef __i386__
    207   1.1        ad #  define QUANTUM_2POW_MIN	4
    208   1.1        ad #  define SIZEOF_PTR_2POW	2
    209   1.1        ad #  define USE_BRK
    210   1.1        ad #endif
    211   1.1        ad #ifdef __ia64__
    212   1.1        ad #  define QUANTUM_2POW_MIN	4
    213   1.1        ad #  define SIZEOF_PTR_2POW	3
    214   1.1        ad #endif
    215  1.34      matt #ifdef __aarch64__
    216  1.34      matt #  define QUANTUM_2POW_MIN	4
    217  1.34      matt #  define SIZEOF_PTR_2POW	3
    218  1.47     joerg #  define TINY_MIN_2POW		3
    219  1.34      matt #endif
    220   1.1        ad #ifdef __alpha__
    221   1.1        ad #  define QUANTUM_2POW_MIN	4
    222   1.1        ad #  define SIZEOF_PTR_2POW	3
    223  1.30     skrll #  define TINY_MIN_2POW		3
    224   1.1        ad #endif
    225   1.1        ad #ifdef __sparc64__
    226   1.1        ad #  define QUANTUM_2POW_MIN	4
    227   1.1        ad #  define SIZEOF_PTR_2POW	3
    228  1.31    martin #  define TINY_MIN_2POW		3
    229   1.1        ad #endif
    230   1.1        ad #ifdef __amd64__
    231   1.1        ad #  define QUANTUM_2POW_MIN	4
    232   1.1        ad #  define SIZEOF_PTR_2POW	3
    233  1.31    martin #  define TINY_MIN_2POW		3
    234   1.1        ad #endif
    235   1.1        ad #ifdef __arm__
    236   1.1        ad #  define QUANTUM_2POW_MIN	3
    237   1.1        ad #  define SIZEOF_PTR_2POW	2
    238   1.1        ad #  define USE_BRK
    239  1.30     skrll #  ifdef __ARM_EABI__
    240  1.30     skrll #    define TINY_MIN_2POW	3
    241  1.30     skrll #  endif
    242   1.1        ad #endif
    243   1.1        ad #ifdef __powerpc__
    244   1.1        ad #  define QUANTUM_2POW_MIN	4
    245   1.1        ad #  define SIZEOF_PTR_2POW	2
    246   1.1        ad #  define USE_BRK
    247  1.32    martin #  define TINY_MIN_2POW		3
    248   1.1        ad #endif
    249   1.3        he #if defined(__sparc__) && !defined(__sparc64__)
    250   1.2        ad #  define QUANTUM_2POW_MIN	4
    251   1.2        ad #  define SIZEOF_PTR_2POW	2
    252   1.2        ad #  define USE_BRK
    253   1.2        ad #endif
    254  1.35      matt #ifdef __or1k__
    255  1.35      matt #  define QUANTUM_2POW_MIN	4
    256  1.35      matt #  define SIZEOF_PTR_2POW	2
    257  1.35      matt #  define USE_BRK
    258  1.35      matt #endif
    259   1.2        ad #ifdef __vax__
    260   1.2        ad #  define QUANTUM_2POW_MIN	4
    261   1.2        ad #  define SIZEOF_PTR_2POW	2
    262   1.2        ad #  define USE_BRK
    263  1.49     joerg #  define NO_TLS
    264   1.2        ad #endif
    265   1.2        ad #ifdef __sh__
    266   1.2        ad #  define QUANTUM_2POW_MIN	4
    267   1.2        ad #  define SIZEOF_PTR_2POW	2
    268   1.2        ad #  define USE_BRK
    269   1.2        ad #endif
    270   1.2        ad #ifdef __m68k__
    271   1.2        ad #  define QUANTUM_2POW_MIN	4
    272   1.2        ad #  define SIZEOF_PTR_2POW	2
    273   1.2        ad #  define USE_BRK
    274  1.49     joerg #  ifdef __mc68000__
    275  1.49     joerg #  define NO_TLS
    276  1.49     joerg #  endif
    277   1.2        ad #endif
    278  1.36      matt #if defined(__mips__) || defined(__riscv__)
    279  1.44       mrg #  ifdef _LP64
    280  1.44       mrg #    define SIZEOF_PTR_2POW	3
    281  1.44       mrg #    define TINY_MIN_2POW	3
    282  1.44       mrg #  else
    283  1.44       mrg #    define SIZEOF_PTR_2POW	2
    284  1.44       mrg #  endif
    285  1.44       mrg #  define QUANTUM_2POW_MIN	4
    286  1.44       mrg #  define USE_BRK
    287   1.2        ad #endif
    288   1.4        ad #ifdef __hppa__
    289  1.44       mrg #  define QUANTUM_2POW_MIN	4
    290  1.44       mrg #  define TINY_MIN_2POW		4
    291  1.44       mrg #  define SIZEOF_PTR_2POW	2
    292   1.4        ad #  define USE_BRK
    293   1.4        ad #endif
    294   1.1        ad 
    295   1.1        ad #define	SIZEOF_PTR		(1 << SIZEOF_PTR_2POW)
    296   1.1        ad 
    297   1.1        ad /* sizeof(int) == (1 << SIZEOF_INT_2POW). */
    298   1.1        ad #ifndef SIZEOF_INT_2POW
    299   1.1        ad #  define SIZEOF_INT_2POW	2
    300   1.1        ad #endif
    301   1.1        ad 
    302   1.1        ad /*
    303   1.1        ad  * Size and alignment of memory chunks that are allocated by the OS's virtual
    304   1.1        ad  * memory system.
    305   1.1        ad  */
    306   1.1        ad #define	CHUNK_2POW_DEFAULT	20
    307   1.1        ad 
    308   1.1        ad /*
    309   1.1        ad  * Maximum size of L1 cache line.  This is used to avoid cache line aliasing,
    310   1.1        ad  * so over-estimates are okay (up to a point), but under-estimates will
    311   1.1        ad  * negatively affect performance.
    312   1.1        ad  */
    313   1.1        ad #define	CACHELINE_2POW		6
    314   1.1        ad #define	CACHELINE		((size_t)(1 << CACHELINE_2POW))
    315   1.1        ad 
    316   1.1        ad /* Smallest size class to support. */
    317  1.30     skrll #ifndef TINY_MIN_2POW
    318  1.30     skrll #define	TINY_MIN_2POW		2
    319  1.30     skrll #endif
    320   1.1        ad 
    321   1.1        ad /*
    322   1.1        ad  * Maximum size class that is a multiple of the quantum, but not (necessarily)
    323   1.1        ad  * a power of 2.  Above this size, allocations are rounded up to the nearest
    324   1.1        ad  * power of 2.
    325   1.1        ad  */
    326   1.1        ad #define	SMALL_MAX_2POW_DEFAULT	9
    327   1.1        ad #define	SMALL_MAX_DEFAULT	(1 << SMALL_MAX_2POW_DEFAULT)
    328   1.1        ad 
    329   1.1        ad /*
    330  1.22     njoly  * RUN_MAX_OVRHD indicates maximum desired run header overhead.  Runs are sized
    331  1.22     njoly  * as small as possible such that this setting is still honored, without
    332  1.22     njoly  * violating other constraints.  The goal is to make runs as small as possible
    333  1.22     njoly  * without exceeding a per run external fragmentation threshold.
    334   1.1        ad  *
    335  1.22     njoly  * We use binary fixed point math for overhead computations, where the binary
    336  1.22     njoly  * point is implicitly RUN_BFP bits to the left.
    337   1.1        ad  *
    338  1.22     njoly  * Note that it is possible to set RUN_MAX_OVRHD low enough that it cannot be
    339  1.22     njoly  * honored for some/all object sizes, since there is one bit of header overhead
    340  1.22     njoly  * per object (plus a constant).  This constraint is relaxed (ignored) for runs
    341  1.22     njoly  * that are so small that the per-region overhead is greater than:
    342  1.22     njoly  *
    343  1.22     njoly  *   (RUN_MAX_OVRHD / (reg_size << (3+RUN_BFP))
    344   1.1        ad  */
    345  1.22     njoly #define RUN_BFP			12
    346  1.22     njoly /*                              \/   Implicit binary fixed point. */
    347  1.22     njoly #define RUN_MAX_OVRHD		0x0000003dU
    348  1.22     njoly #define RUN_MAX_OVRHD_RELAX	0x00001800U
    349   1.1        ad 
    350   1.1        ad /* Put a cap on small object run size.  This overrides RUN_MAX_OVRHD. */
    351   1.1        ad #define RUN_MAX_SMALL_2POW	15
    352   1.1        ad #define RUN_MAX_SMALL		(1 << RUN_MAX_SMALL_2POW)
    353   1.1        ad 
    354   1.1        ad /******************************************************************************/
    355   1.1        ad 
    356   1.2        ad #ifdef __FreeBSD__
    357   1.1        ad /*
    358   1.1        ad  * Mutexes based on spinlocks.  We can't use normal pthread mutexes, because
    359   1.1        ad  * they require malloc()ed memory.
    360   1.1        ad  */
    361   1.1        ad typedef struct {
    362   1.1        ad 	spinlock_t	lock;
    363   1.1        ad } malloc_mutex_t;
    364   1.1        ad 
    365   1.1        ad /* Set to true once the allocator has been initialized. */
    366   1.1        ad static bool malloc_initialized = false;
    367   1.1        ad 
    368   1.1        ad /* Used to avoid initialization races. */
    369   1.1        ad static malloc_mutex_t init_lock = {_SPINLOCK_INITIALIZER};
    370   1.2        ad #else
    371   1.2        ad #define	malloc_mutex_t	mutex_t
    372   1.2        ad 
    373   1.2        ad /* Set to true once the allocator has been initialized. */
    374   1.2        ad static bool malloc_initialized = false;
    375   1.2        ad 
    376  1.37  christos #ifdef _REENTRANT
    377   1.2        ad /* Used to avoid initialization races. */
    378   1.2        ad static mutex_t init_lock = MUTEX_INITIALIZER;
    379   1.2        ad #endif
    380  1.37  christos #endif
    381   1.1        ad 
    382   1.1        ad /******************************************************************************/
    383   1.1        ad /*
    384   1.1        ad  * Statistics data structures.
    385   1.1        ad  */
    386   1.1        ad 
    387   1.1        ad #ifdef MALLOC_STATS
    388   1.1        ad 
    389   1.1        ad typedef struct malloc_bin_stats_s malloc_bin_stats_t;
    390   1.1        ad struct malloc_bin_stats_s {
    391   1.1        ad 	/*
    392   1.1        ad 	 * Number of allocation requests that corresponded to the size of this
    393   1.1        ad 	 * bin.
    394   1.1        ad 	 */
    395   1.1        ad 	uint64_t	nrequests;
    396   1.1        ad 
    397   1.1        ad 	/* Total number of runs created for this bin's size class. */
    398   1.1        ad 	uint64_t	nruns;
    399   1.1        ad 
    400   1.1        ad 	/*
    401   1.1        ad 	 * Total number of runs reused by extracting them from the runs tree for
    402   1.1        ad 	 * this bin's size class.
    403   1.1        ad 	 */
    404   1.1        ad 	uint64_t	reruns;
    405   1.1        ad 
    406   1.1        ad 	/* High-water mark for this bin. */
    407   1.1        ad 	unsigned long	highruns;
    408   1.1        ad 
    409   1.1        ad 	/* Current number of runs in this bin. */
    410   1.1        ad 	unsigned long	curruns;
    411   1.1        ad };
    412   1.1        ad 
    413   1.1        ad typedef struct arena_stats_s arena_stats_t;
    414   1.1        ad struct arena_stats_s {
    415   1.1        ad 	/* Number of bytes currently mapped. */
    416   1.1        ad 	size_t		mapped;
    417   1.1        ad 
    418   1.1        ad 	/* Per-size-category statistics. */
    419   1.1        ad 	size_t		allocated_small;
    420   1.1        ad 	uint64_t	nmalloc_small;
    421   1.1        ad 	uint64_t	ndalloc_small;
    422   1.1        ad 
    423   1.1        ad 	size_t		allocated_large;
    424   1.1        ad 	uint64_t	nmalloc_large;
    425   1.1        ad 	uint64_t	ndalloc_large;
    426   1.1        ad };
    427   1.1        ad 
    428   1.1        ad typedef struct chunk_stats_s chunk_stats_t;
    429   1.1        ad struct chunk_stats_s {
    430   1.1        ad 	/* Number of chunks that were allocated. */
    431   1.1        ad 	uint64_t	nchunks;
    432   1.1        ad 
    433   1.1        ad 	/* High-water mark for number of chunks allocated. */
    434   1.1        ad 	unsigned long	highchunks;
    435   1.1        ad 
    436   1.1        ad 	/*
    437   1.1        ad 	 * Current number of chunks allocated.  This value isn't maintained for
    438   1.1        ad 	 * any other purpose, so keep track of it in order to be able to set
    439   1.1        ad 	 * highchunks.
    440   1.1        ad 	 */
    441   1.1        ad 	unsigned long	curchunks;
    442   1.1        ad };
    443   1.1        ad 
    444   1.1        ad #endif /* #ifdef MALLOC_STATS */
    445   1.1        ad 
    446   1.1        ad /******************************************************************************/
    447   1.1        ad /*
    448   1.1        ad  * Chunk data structures.
    449   1.1        ad  */
    450   1.1        ad 
    451   1.1        ad /* Tree of chunks. */
    452   1.1        ad typedef struct chunk_node_s chunk_node_t;
    453   1.1        ad struct chunk_node_s {
    454   1.1        ad 	/* Linkage for the chunk tree. */
    455   1.1        ad 	RB_ENTRY(chunk_node_s) link;
    456   1.1        ad 
    457   1.1        ad 	/*
    458   1.1        ad 	 * Pointer to the chunk that this tree node is responsible for.  In some
    459   1.1        ad 	 * (but certainly not all) cases, this data structure is placed at the
    460   1.1        ad 	 * beginning of the corresponding chunk, so this field may point to this
    461   1.1        ad 	 * node.
    462   1.1        ad 	 */
    463   1.1        ad 	void	*chunk;
    464   1.1        ad 
    465   1.1        ad 	/* Total chunk size. */
    466   1.1        ad 	size_t	size;
    467   1.1        ad };
    468   1.1        ad typedef struct chunk_tree_s chunk_tree_t;
    469   1.1        ad RB_HEAD(chunk_tree_s, chunk_node_s);
    470   1.1        ad 
    471   1.1        ad /******************************************************************************/
    472   1.1        ad /*
    473   1.1        ad  * Arena data structures.
    474   1.1        ad  */
    475   1.1        ad 
    476   1.1        ad typedef struct arena_s arena_t;
    477   1.1        ad typedef struct arena_bin_s arena_bin_t;
    478   1.1        ad 
    479   1.1        ad typedef struct arena_chunk_map_s arena_chunk_map_t;
    480   1.1        ad struct arena_chunk_map_s {
    481   1.1        ad 	/* Number of pages in run. */
    482   1.1        ad 	uint32_t	npages;
    483   1.1        ad 	/*
    484   1.1        ad 	 * Position within run.  For a free run, this is POS_FREE for the first
    485   1.1        ad 	 * and last pages.  The POS_FREE special value makes it possible to
    486   1.1        ad 	 * quickly coalesce free runs.
    487   1.1        ad 	 *
    488   1.1        ad 	 * This is the limiting factor for chunksize; there can be at most 2^31
    489   1.1        ad 	 * pages in a run.
    490   1.1        ad 	 */
    491   1.1        ad #define POS_FREE ((uint32_t)0xffffffffU)
    492   1.1        ad 	uint32_t	pos;
    493   1.1        ad };
    494   1.1        ad 
    495   1.1        ad /* Arena chunk header. */
    496   1.1        ad typedef struct arena_chunk_s arena_chunk_t;
    497   1.1        ad struct arena_chunk_s {
    498   1.1        ad 	/* Arena that owns the chunk. */
    499   1.1        ad 	arena_t *arena;
    500   1.1        ad 
    501   1.1        ad 	/* Linkage for the arena's chunk tree. */
    502   1.1        ad 	RB_ENTRY(arena_chunk_s) link;
    503   1.1        ad 
    504   1.1        ad 	/*
    505   1.1        ad 	 * Number of pages in use.  This is maintained in order to make
    506   1.1        ad 	 * detection of empty chunks fast.
    507   1.1        ad 	 */
    508   1.1        ad 	uint32_t pages_used;
    509   1.1        ad 
    510   1.1        ad 	/*
    511   1.1        ad 	 * Every time a free run larger than this value is created/coalesced,
    512   1.1        ad 	 * this value is increased.  The only way that the value decreases is if
    513   1.1        ad 	 * arena_run_alloc() fails to find a free run as large as advertised by
    514   1.1        ad 	 * this value.
    515   1.1        ad 	 */
    516   1.1        ad 	uint32_t max_frun_npages;
    517   1.1        ad 
    518   1.1        ad 	/*
    519   1.1        ad 	 * Every time a free run that starts at an earlier page than this value
    520   1.1        ad 	 * is created/coalesced, this value is decreased.  It is reset in a
    521   1.1        ad 	 * similar fashion to max_frun_npages.
    522   1.1        ad 	 */
    523   1.1        ad 	uint32_t min_frun_ind;
    524   1.1        ad 
    525   1.1        ad 	/*
    526   1.1        ad 	 * Map of pages within chunk that keeps track of free/large/small.  For
    527   1.1        ad 	 * free runs, only the map entries for the first and last pages are
    528   1.1        ad 	 * kept up to date, so that free runs can be quickly coalesced.
    529   1.1        ad 	 */
    530   1.1        ad 	arena_chunk_map_t map[1]; /* Dynamically sized. */
    531   1.1        ad };
    532   1.1        ad typedef struct arena_chunk_tree_s arena_chunk_tree_t;
    533   1.1        ad RB_HEAD(arena_chunk_tree_s, arena_chunk_s);
    534   1.1        ad 
    535   1.1        ad typedef struct arena_run_s arena_run_t;
    536   1.1        ad struct arena_run_s {
    537   1.1        ad 	/* Linkage for run trees. */
    538   1.1        ad 	RB_ENTRY(arena_run_s) link;
    539   1.1        ad 
    540   1.1        ad #ifdef MALLOC_DEBUG
    541   1.1        ad 	uint32_t	magic;
    542   1.1        ad #  define ARENA_RUN_MAGIC 0x384adf93
    543   1.1        ad #endif
    544   1.1        ad 
    545   1.1        ad 	/* Bin this run is associated with. */
    546   1.1        ad 	arena_bin_t	*bin;
    547   1.1        ad 
    548   1.1        ad 	/* Index of first element that might have a free region. */
    549   1.1        ad 	unsigned	regs_minelm;
    550   1.1        ad 
    551   1.1        ad 	/* Number of free regions in run. */
    552   1.1        ad 	unsigned	nfree;
    553   1.1        ad 
    554   1.1        ad 	/* Bitmask of in-use regions (0: in use, 1: free). */
    555   1.1        ad 	unsigned	regs_mask[1]; /* Dynamically sized. */
    556   1.1        ad };
    557   1.1        ad typedef struct arena_run_tree_s arena_run_tree_t;
    558   1.1        ad RB_HEAD(arena_run_tree_s, arena_run_s);
    559   1.1        ad 
    560   1.1        ad struct arena_bin_s {
    561   1.1        ad 	/*
    562   1.1        ad 	 * Current run being used to service allocations of this bin's size
    563   1.1        ad 	 * class.
    564   1.1        ad 	 */
    565   1.1        ad 	arena_run_t	*runcur;
    566   1.1        ad 
    567   1.1        ad 	/*
    568   1.1        ad 	 * Tree of non-full runs.  This tree is used when looking for an
    569   1.1        ad 	 * existing run when runcur is no longer usable.  We choose the
    570   1.1        ad 	 * non-full run that is lowest in memory; this policy tends to keep
    571   1.1        ad 	 * objects packed well, and it can also help reduce the number of
    572   1.1        ad 	 * almost-empty chunks.
    573   1.1        ad 	 */
    574   1.1        ad 	arena_run_tree_t runs;
    575   1.1        ad 
    576   1.1        ad 	/* Size of regions in a run for this bin's size class. */
    577   1.1        ad 	size_t		reg_size;
    578   1.1        ad 
    579   1.1        ad 	/* Total size of a run for this bin's size class. */
    580   1.1        ad 	size_t		run_size;
    581   1.1        ad 
    582   1.1        ad 	/* Total number of regions in a run for this bin's size class. */
    583   1.1        ad 	uint32_t	nregs;
    584   1.1        ad 
    585   1.1        ad 	/* Number of elements in a run's regs_mask for this bin's size class. */
    586   1.1        ad 	uint32_t	regs_mask_nelms;
    587   1.1        ad 
    588   1.1        ad 	/* Offset of first region in a run for this bin's size class. */
    589   1.1        ad 	uint32_t	reg0_offset;
    590   1.1        ad 
    591   1.1        ad #ifdef MALLOC_STATS
    592   1.1        ad 	/* Bin statistics. */
    593   1.1        ad 	malloc_bin_stats_t stats;
    594   1.1        ad #endif
    595   1.1        ad };
    596   1.1        ad 
    597   1.1        ad struct arena_s {
    598   1.1        ad #ifdef MALLOC_DEBUG
    599   1.1        ad 	uint32_t		magic;
    600   1.1        ad #  define ARENA_MAGIC 0x947d3d24
    601   1.1        ad #endif
    602   1.1        ad 
    603   1.1        ad 	/* All operations on this arena require that mtx be locked. */
    604   1.1        ad 	malloc_mutex_t		mtx;
    605   1.1        ad 
    606   1.1        ad #ifdef MALLOC_STATS
    607   1.1        ad 	arena_stats_t		stats;
    608   1.1        ad #endif
    609   1.1        ad 
    610   1.1        ad 	/*
    611   1.1        ad 	 * Tree of chunks this arena manages.
    612   1.1        ad 	 */
    613   1.1        ad 	arena_chunk_tree_t	chunks;
    614   1.1        ad 
    615   1.1        ad 	/*
    616   1.1        ad 	 * In order to avoid rapid chunk allocation/deallocation when an arena
    617   1.1        ad 	 * oscillates right on the cusp of needing a new chunk, cache the most
    618   1.1        ad 	 * recently freed chunk.  This caching is disabled by opt_hint.
    619   1.1        ad 	 *
    620   1.1        ad 	 * There is one spare chunk per arena, rather than one spare total, in
    621   1.1        ad 	 * order to avoid interactions between multiple threads that could make
    622   1.1        ad 	 * a single spare inadequate.
    623   1.1        ad 	 */
    624   1.1        ad 	arena_chunk_t *spare;
    625   1.1        ad 
    626   1.1        ad 	/*
    627   1.1        ad 	 * bins is used to store rings of free regions of the following sizes,
    628   1.1        ad 	 * assuming a 16-byte quantum, 4kB pagesize, and default MALLOC_OPTIONS.
    629   1.1        ad 	 *
    630   1.1        ad 	 *   bins[i] | size |
    631   1.1        ad 	 *   --------+------+
    632   1.1        ad 	 *        0  |    2 |
    633   1.1        ad 	 *        1  |    4 |
    634   1.1        ad 	 *        2  |    8 |
    635   1.1        ad 	 *   --------+------+
    636   1.1        ad 	 *        3  |   16 |
    637   1.1        ad 	 *        4  |   32 |
    638   1.1        ad 	 *        5  |   48 |
    639   1.1        ad 	 *        6  |   64 |
    640   1.1        ad 	 *           :      :
    641   1.1        ad 	 *           :      :
    642   1.1        ad 	 *       33  |  496 |
    643   1.1        ad 	 *       34  |  512 |
    644   1.1        ad 	 *   --------+------+
    645   1.1        ad 	 *       35  | 1024 |
    646   1.1        ad 	 *       36  | 2048 |
    647   1.1        ad 	 *   --------+------+
    648   1.1        ad 	 */
    649   1.1        ad 	arena_bin_t		bins[1]; /* Dynamically sized. */
    650   1.1        ad };
    651   1.1        ad 
    652   1.1        ad /******************************************************************************/
    653   1.1        ad /*
    654   1.1        ad  * Data.
    655   1.1        ad  */
    656   1.1        ad 
    657   1.1        ad /* Number of CPUs. */
    658   1.1        ad static unsigned		ncpus;
    659   1.1        ad 
    660   1.1        ad /* VM page size. */
    661   1.1        ad static size_t		pagesize;
    662   1.1        ad static size_t		pagesize_mask;
    663   1.9  christos static int		pagesize_2pow;
    664   1.1        ad 
    665   1.1        ad /* Various bin-related settings. */
    666   1.1        ad static size_t		bin_maxclass; /* Max size class for bins. */
    667   1.1        ad static unsigned		ntbins; /* Number of (2^n)-spaced tiny bins. */
    668   1.1        ad static unsigned		nqbins; /* Number of quantum-spaced bins. */
    669   1.1        ad static unsigned		nsbins; /* Number of (2^n)-spaced sub-page bins. */
    670   1.1        ad static size_t		small_min;
    671   1.1        ad static size_t		small_max;
    672   1.1        ad 
    673   1.1        ad /* Various quantum-related settings. */
    674   1.1        ad static size_t		quantum;
    675   1.1        ad static size_t		quantum_mask; /* (quantum - 1). */
    676   1.1        ad 
    677   1.1        ad /* Various chunk-related settings. */
    678   1.1        ad static size_t		chunksize;
    679   1.1        ad static size_t		chunksize_mask; /* (chunksize - 1). */
    680   1.5      yamt static int		chunksize_2pow;
    681   1.1        ad static unsigned		chunk_npages;
    682   1.1        ad static unsigned		arena_chunk_header_npages;
    683   1.1        ad static size_t		arena_maxclass; /* Max size class for arenas. */
    684   1.1        ad 
    685   1.1        ad /********/
    686   1.1        ad /*
    687   1.1        ad  * Chunks.
    688   1.1        ad  */
    689   1.1        ad 
    690  1.37  christos #ifdef _REENTRANT
    691   1.1        ad /* Protects chunk-related data structures. */
    692   1.1        ad static malloc_mutex_t	chunks_mtx;
    693  1.37  christos #endif
    694   1.1        ad 
    695   1.1        ad /* Tree of chunks that are stand-alone huge allocations. */
    696   1.1        ad static chunk_tree_t	huge;
    697   1.1        ad 
    698   1.1        ad #ifdef USE_BRK
    699   1.1        ad /*
    700   1.1        ad  * Try to use brk for chunk-size allocations, due to address space constraints.
    701   1.1        ad  */
    702   1.1        ad /*
    703   1.1        ad  * Protects sbrk() calls.  This must be separate from chunks_mtx, since
    704   1.1        ad  * base_pages_alloc() also uses sbrk(), but cannot lock chunks_mtx (doing so
    705   1.1        ad  * could cause recursive lock acquisition).
    706   1.1        ad  */
    707  1.46  christos #ifdef _REENTRANT
    708   1.1        ad static malloc_mutex_t	brk_mtx;
    709  1.46  christos #endif
    710   1.1        ad /* Result of first sbrk(0) call. */
    711   1.1        ad static void		*brk_base;
    712   1.1        ad /* Current end of brk, or ((void *)-1) if brk is exhausted. */
    713   1.1        ad static void		*brk_prev;
    714   1.1        ad /* Current upper limit on brk addresses. */
    715   1.1        ad static void		*brk_max;
    716   1.1        ad #endif
    717   1.1        ad 
    718   1.1        ad #ifdef MALLOC_STATS
    719   1.1        ad /* Huge allocation statistics. */
    720   1.1        ad static uint64_t		huge_nmalloc;
    721   1.1        ad static uint64_t		huge_ndalloc;
    722   1.8      yamt static uint64_t		huge_nralloc;
    723   1.1        ad static size_t		huge_allocated;
    724   1.1        ad #endif
    725   1.1        ad 
    726   1.1        ad /*
    727   1.1        ad  * Tree of chunks that were previously allocated.  This is used when allocating
    728   1.1        ad  * chunks, in an attempt to re-use address space.
    729   1.1        ad  */
    730   1.1        ad static chunk_tree_t	old_chunks;
    731   1.1        ad 
    732   1.1        ad /****************************/
    733   1.1        ad /*
    734   1.1        ad  * base (internal allocation).
    735   1.1        ad  */
    736   1.1        ad 
    737   1.1        ad /*
    738   1.1        ad  * Current pages that are being used for internal memory allocations.  These
    739   1.1        ad  * pages are carved up in cacheline-size quanta, so that there is no chance of
    740   1.1        ad  * false cache line sharing.
    741   1.1        ad  */
    742   1.1        ad static void		*base_pages;
    743   1.1        ad static void		*base_next_addr;
    744   1.1        ad static void		*base_past_addr; /* Addr immediately past base_pages. */
    745   1.1        ad static chunk_node_t	*base_chunk_nodes; /* LIFO cache of chunk nodes. */
    746  1.37  christos #ifdef _REENTRANT
    747   1.1        ad static malloc_mutex_t	base_mtx;
    748  1.37  christos #endif
    749   1.1        ad #ifdef MALLOC_STATS
    750   1.1        ad static size_t		base_mapped;
    751   1.1        ad #endif
    752   1.1        ad 
    753   1.1        ad /********/
    754   1.1        ad /*
    755   1.1        ad  * Arenas.
    756   1.1        ad  */
    757   1.1        ad 
    758   1.1        ad /*
    759   1.1        ad  * Arenas that are used to service external requests.  Not all elements of the
    760   1.1        ad  * arenas array are necessarily used; arenas are created lazily as needed.
    761   1.1        ad  */
    762   1.1        ad static arena_t		**arenas;
    763   1.1        ad static unsigned		narenas;
    764   1.1        ad static unsigned		next_arena;
    765  1.37  christos #ifdef _REENTRANT
    766   1.1        ad static malloc_mutex_t	arenas_mtx; /* Protects arenas initialization. */
    767  1.37  christos #endif
    768   1.1        ad 
    769  1.15        ad /*
    770  1.15        ad  * Map of pthread_self() --> arenas[???], used for selecting an arena to use
    771  1.15        ad  * for allocations.
    772  1.15        ad  */
    773  1.38    martin #ifndef NO_TLS
    774  1.49     joerg static __attribute__((tls_model("initial-exec")))
    775  1.49     joerg __thread arena_t	**arenas_map;
    776  1.15        ad #else
    777  1.38    martin static arena_t	**arenas_map;
    778  1.49     joerg static thread_key_t arenas_map_key = -1;
    779  1.37  christos #endif
    780  1.38    martin 
    781  1.38    martin #if !defined(NO_TLS) || !defined(_REENTRANT)
    782  1.38    martin # define	get_arenas_map()	(arenas_map)
    783  1.38    martin # define	set_arenas_map(x)	(arenas_map = x)
    784  1.38    martin #else
    785  1.38    martin 
    786  1.38    martin 
    787  1.38    martin static inline arena_t **
    788  1.38    martin get_arenas_map(void)
    789  1.38    martin {
    790  1.38    martin 	if (!__isthreaded)
    791  1.38    martin 		return arenas_map;
    792  1.38    martin 
    793  1.38    martin 	if (arenas_map_key == -1) {
    794  1.38    martin 		(void)thr_keycreate(&arenas_map_key, NULL);
    795  1.38    martin 		if (arenas_map != NULL) {
    796  1.38    martin 			thr_setspecific(arenas_map_key, arenas_map);
    797  1.38    martin 			arenas_map = NULL;
    798  1.38    martin 		}
    799  1.38    martin 	}
    800  1.38    martin 
    801  1.38    martin 	return thr_getspecific(arenas_map_key);
    802  1.38    martin }
    803  1.38    martin 
    804  1.38    martin static __inline void
    805  1.38    martin set_arenas_map(arena_t **a)
    806  1.38    martin {
    807  1.38    martin 	if (!__isthreaded) {
    808  1.38    martin 		arenas_map = a;
    809  1.38    martin 		return;
    810  1.38    martin 	}
    811  1.38    martin 
    812  1.38    martin 	if (arenas_map_key == -1) {
    813  1.49     joerg #ifndef NO_TLS
    814  1.38    martin 		(void)thr_keycreate(&arenas_map_key, NULL);
    815  1.49     joerg #endif
    816  1.38    martin 		if (arenas_map != NULL) {
    817  1.38    martin 			_DIAGASSERT(arenas_map == a);
    818  1.38    martin 			arenas_map = NULL;
    819  1.38    martin 		}
    820  1.38    martin 	}
    821  1.38    martin 
    822  1.49     joerg #ifndef NO_TLS
    823  1.38    martin 	thr_setspecific(arenas_map_key, a);
    824  1.49     joerg #endif
    825  1.38    martin }
    826  1.15        ad #endif
    827   1.1        ad 
    828   1.1        ad #ifdef MALLOC_STATS
    829   1.1        ad /* Chunk statistics. */
    830   1.1        ad static chunk_stats_t	stats_chunks;
    831   1.1        ad #endif
    832   1.1        ad 
    833   1.1        ad /*******************************/
    834   1.1        ad /*
    835   1.1        ad  * Runtime configuration options.
    836   1.1        ad  */
    837   1.1        ad const char	*_malloc_options;
    838   1.1        ad 
    839   1.1        ad #ifndef MALLOC_PRODUCTION
    840   1.1        ad static bool	opt_abort = true;
    841   1.1        ad static bool	opt_junk = true;
    842   1.1        ad #else
    843   1.1        ad static bool	opt_abort = false;
    844   1.1        ad static bool	opt_junk = false;
    845   1.1        ad #endif
    846   1.1        ad static bool	opt_hint = false;
    847   1.1        ad static bool	opt_print_stats = false;
    848   1.9  christos static int	opt_quantum_2pow = QUANTUM_2POW_MIN;
    849   1.9  christos static int	opt_small_max_2pow = SMALL_MAX_2POW_DEFAULT;
    850   1.9  christos static int	opt_chunk_2pow = CHUNK_2POW_DEFAULT;
    851   1.1        ad static bool	opt_utrace = false;
    852   1.1        ad static bool	opt_sysv = false;
    853   1.1        ad static bool	opt_xmalloc = false;
    854   1.1        ad static bool	opt_zero = false;
    855   1.1        ad static int32_t	opt_narenas_lshift = 0;
    856   1.1        ad 
    857   1.1        ad typedef struct {
    858   1.1        ad 	void	*p;
    859   1.1        ad 	size_t	s;
    860   1.1        ad 	void	*r;
    861   1.1        ad } malloc_utrace_t;
    862   1.1        ad 
    863   1.1        ad #define	UTRACE(a, b, c)							\
    864   1.1        ad 	if (opt_utrace) {						\
    865   1.2        ad 		malloc_utrace_t ut;					\
    866   1.2        ad 		ut.p = a;						\
    867   1.2        ad 		ut.s = b;						\
    868   1.2        ad 		ut.r = c;						\
    869   1.2        ad 		xutrace(&ut, sizeof(ut));				\
    870   1.1        ad 	}
    871   1.1        ad 
    872   1.1        ad /******************************************************************************/
    873   1.1        ad /*
    874   1.1        ad  * Begin function prototypes for non-inline static functions.
    875   1.1        ad  */
    876   1.1        ad 
    877   1.1        ad static void	wrtmessage(const char *p1, const char *p2, const char *p3,
    878   1.1        ad 		const char *p4);
    879   1.1        ad #ifdef MALLOC_STATS
    880   1.1        ad static void	malloc_printf(const char *format, ...);
    881   1.1        ad #endif
    882  1.28  christos static char	*size_t2s(size_t x, char *s);
    883   1.1        ad static bool	base_pages_alloc(size_t minsize);
    884   1.1        ad static void	*base_alloc(size_t size);
    885   1.1        ad static chunk_node_t *base_chunk_node_alloc(void);
    886   1.1        ad static void	base_chunk_node_dealloc(chunk_node_t *node);
    887   1.1        ad #ifdef MALLOC_STATS
    888   1.1        ad static void	stats_print(arena_t *arena);
    889   1.1        ad #endif
    890   1.1        ad static void	*pages_map(void *addr, size_t size);
    891   1.5      yamt static void	*pages_map_align(void *addr, size_t size, int align);
    892   1.1        ad static void	pages_unmap(void *addr, size_t size);
    893   1.1        ad static void	*chunk_alloc(size_t size);
    894   1.1        ad static void	chunk_dealloc(void *chunk, size_t size);
    895   1.1        ad static void	arena_run_split(arena_t *arena, arena_run_t *run, size_t size);
    896   1.1        ad static arena_chunk_t *arena_chunk_alloc(arena_t *arena);
    897   1.1        ad static void	arena_chunk_dealloc(arena_t *arena, arena_chunk_t *chunk);
    898   1.1        ad static arena_run_t *arena_run_alloc(arena_t *arena, size_t size);
    899   1.1        ad static void	arena_run_dalloc(arena_t *arena, arena_run_t *run, size_t size);
    900   1.1        ad static arena_run_t *arena_bin_nonfull_run_get(arena_t *arena, arena_bin_t *bin);
    901   1.1        ad static void *arena_bin_malloc_hard(arena_t *arena, arena_bin_t *bin);
    902   1.1        ad static size_t arena_bin_run_size_calc(arena_bin_t *bin, size_t min_run_size);
    903   1.1        ad static void	*arena_malloc(arena_t *arena, size_t size);
    904   1.1        ad static void	*arena_palloc(arena_t *arena, size_t alignment, size_t size,
    905   1.1        ad     size_t alloc_size);
    906   1.1        ad static size_t	arena_salloc(const void *ptr);
    907   1.1        ad static void	*arena_ralloc(void *ptr, size_t size, size_t oldsize);
    908   1.1        ad static void	arena_dalloc(arena_t *arena, arena_chunk_t *chunk, void *ptr);
    909   1.1        ad static bool	arena_new(arena_t *arena);
    910   1.1        ad static arena_t	*arenas_extend(unsigned ind);
    911   1.1        ad static void	*huge_malloc(size_t size);
    912   1.1        ad static void	*huge_palloc(size_t alignment, size_t size);
    913   1.1        ad static void	*huge_ralloc(void *ptr, size_t size, size_t oldsize);
    914   1.1        ad static void	huge_dalloc(void *ptr);
    915   1.1        ad static void	*imalloc(size_t size);
    916   1.1        ad static void	*ipalloc(size_t alignment, size_t size);
    917   1.1        ad static void	*icalloc(size_t size);
    918   1.1        ad static size_t	isalloc(const void *ptr);
    919   1.1        ad static void	*iralloc(void *ptr, size_t size);
    920   1.1        ad static void	idalloc(void *ptr);
    921   1.1        ad static void	malloc_print_stats(void);
    922   1.1        ad static bool	malloc_init_hard(void);
    923   1.1        ad 
    924   1.1        ad /*
    925   1.1        ad  * End function prototypes.
    926   1.1        ad  */
    927   1.1        ad /******************************************************************************/
    928   1.1        ad /*
    929   1.1        ad  * Begin mutex.
    930   1.1        ad  */
    931   1.1        ad 
    932  1.15        ad #ifdef __NetBSD__
    933   1.2        ad #define	malloc_mutex_init(m)	mutex_init(m, NULL)
    934   1.2        ad #define	malloc_mutex_lock(m)	mutex_lock(m)
    935   1.2        ad #define	malloc_mutex_unlock(m)	mutex_unlock(m)
    936  1.15        ad #else	/* __NetBSD__ */
    937  1.15        ad static inline void
    938  1.15        ad malloc_mutex_init(malloc_mutex_t *a_mutex)
    939  1.15        ad {
    940  1.15        ad 	static const spinlock_t lock = _SPINLOCK_INITIALIZER;
    941  1.15        ad 
    942  1.15        ad 	a_mutex->lock = lock;
    943  1.15        ad }
    944  1.15        ad 
    945  1.15        ad static inline void
    946  1.15        ad malloc_mutex_lock(malloc_mutex_t *a_mutex)
    947  1.15        ad {
    948  1.15        ad 
    949  1.15        ad 	if (__isthreaded)
    950  1.15        ad 		_SPINLOCK(&a_mutex->lock);
    951  1.15        ad }
    952  1.15        ad 
    953  1.15        ad static inline void
    954  1.15        ad malloc_mutex_unlock(malloc_mutex_t *a_mutex)
    955  1.15        ad {
    956  1.15        ad 
    957  1.15        ad 	if (__isthreaded)
    958  1.15        ad 		_SPINUNLOCK(&a_mutex->lock);
    959  1.15        ad }
    960  1.15        ad #endif	/* __NetBSD__ */
    961   1.1        ad 
    962   1.1        ad /*
    963   1.1        ad  * End mutex.
    964   1.1        ad  */
    965   1.1        ad /******************************************************************************/
    966   1.1        ad /*
    967   1.1        ad  * Begin Utility functions/macros.
    968   1.1        ad  */
    969   1.1        ad 
    970   1.1        ad /* Return the chunk address for allocation address a. */
    971   1.1        ad #define	CHUNK_ADDR2BASE(a)						\
    972   1.1        ad 	((void *)((uintptr_t)(a) & ~chunksize_mask))
    973   1.1        ad 
    974   1.1        ad /* Return the chunk offset of address a. */
    975   1.1        ad #define	CHUNK_ADDR2OFFSET(a)						\
    976   1.1        ad 	((size_t)((uintptr_t)(a) & chunksize_mask))
    977   1.1        ad 
    978   1.1        ad /* Return the smallest chunk multiple that is >= s. */
    979   1.1        ad #define	CHUNK_CEILING(s)						\
    980   1.1        ad 	(((s) + chunksize_mask) & ~chunksize_mask)
    981   1.1        ad 
    982   1.1        ad /* Return the smallest cacheline multiple that is >= s. */
    983   1.1        ad #define	CACHELINE_CEILING(s)						\
    984   1.1        ad 	(((s) + (CACHELINE - 1)) & ~(CACHELINE - 1))
    985   1.1        ad 
    986   1.1        ad /* Return the smallest quantum multiple that is >= a. */
    987   1.1        ad #define	QUANTUM_CEILING(a)						\
    988   1.1        ad 	(((a) + quantum_mask) & ~quantum_mask)
    989   1.1        ad 
    990   1.1        ad /* Return the smallest pagesize multiple that is >= s. */
    991   1.1        ad #define	PAGE_CEILING(s)							\
    992   1.1        ad 	(((s) + pagesize_mask) & ~pagesize_mask)
    993   1.1        ad 
    994   1.1        ad /* Compute the smallest power of 2 that is >= x. */
    995   1.1        ad static inline size_t
    996   1.1        ad pow2_ceil(size_t x)
    997   1.1        ad {
    998   1.1        ad 
    999   1.1        ad 	x--;
   1000   1.1        ad 	x |= x >> 1;
   1001   1.1        ad 	x |= x >> 2;
   1002   1.1        ad 	x |= x >> 4;
   1003   1.1        ad 	x |= x >> 8;
   1004   1.1        ad 	x |= x >> 16;
   1005   1.1        ad #if (SIZEOF_PTR == 8)
   1006   1.1        ad 	x |= x >> 32;
   1007   1.1        ad #endif
   1008   1.1        ad 	x++;
   1009   1.1        ad 	return (x);
   1010   1.1        ad }
   1011   1.1        ad 
   1012   1.1        ad static void
   1013   1.1        ad wrtmessage(const char *p1, const char *p2, const char *p3, const char *p4)
   1014   1.1        ad {
   1015   1.1        ad 
   1016  1.16  christos 	write(STDERR_FILENO, p1, strlen(p1));
   1017  1.16  christos 	write(STDERR_FILENO, p2, strlen(p2));
   1018  1.16  christos 	write(STDERR_FILENO, p3, strlen(p3));
   1019  1.16  christos 	write(STDERR_FILENO, p4, strlen(p4));
   1020   1.1        ad }
   1021   1.1        ad 
   1022   1.1        ad void	(*_malloc_message)(const char *p1, const char *p2, const char *p3,
   1023   1.1        ad 	    const char *p4) = wrtmessage;
   1024   1.1        ad 
   1025   1.1        ad #ifdef MALLOC_STATS
   1026   1.1        ad /*
   1027   1.1        ad  * Print to stderr in such a way as to (hopefully) avoid memory allocation.
   1028   1.1        ad  */
   1029   1.1        ad static void
   1030   1.1        ad malloc_printf(const char *format, ...)
   1031   1.1        ad {
   1032   1.1        ad 	char buf[4096];
   1033   1.1        ad 	va_list ap;
   1034   1.1        ad 
   1035   1.1        ad 	va_start(ap, format);
   1036   1.1        ad 	vsnprintf(buf, sizeof(buf), format, ap);
   1037   1.1        ad 	va_end(ap);
   1038   1.1        ad 	_malloc_message(buf, "", "", "");
   1039   1.1        ad }
   1040   1.1        ad #endif
   1041   1.1        ad 
   1042   1.1        ad /*
   1043   1.1        ad  * We don't want to depend on vsnprintf() for production builds, since that can
   1044  1.28  christos  * cause unnecessary bloat for static binaries.  size_t2s() provides minimal
   1045   1.1        ad  * integer printing functionality, so that malloc_printf() use can be limited to
   1046   1.1        ad  * MALLOC_STATS code.
   1047   1.1        ad  */
   1048   1.1        ad #define UMAX2S_BUFSIZE	21
   1049   1.1        ad static char *
   1050  1.28  christos size_t2s(size_t x, char *s)
   1051   1.1        ad {
   1052   1.1        ad 	unsigned i;
   1053   1.1        ad 
   1054   1.1        ad 	/* Make sure UMAX2S_BUFSIZE is large enough. */
   1055   1.7      yamt 	/* LINTED */
   1056  1.25  christos 	assert(sizeof(size_t) <= 8);
   1057   1.1        ad 
   1058   1.1        ad 	i = UMAX2S_BUFSIZE - 1;
   1059   1.1        ad 	s[i] = '\0';
   1060   1.1        ad 	do {
   1061   1.1        ad 		i--;
   1062   1.2        ad 		s[i] = "0123456789"[(int)x % 10];
   1063   1.2        ad 		x /= (uintmax_t)10LL;
   1064   1.1        ad 	} while (x > 0);
   1065   1.1        ad 
   1066   1.1        ad 	return (&s[i]);
   1067   1.1        ad }
   1068   1.1        ad 
   1069   1.1        ad /******************************************************************************/
   1070   1.1        ad 
   1071   1.1        ad static bool
   1072   1.1        ad base_pages_alloc(size_t minsize)
   1073   1.1        ad {
   1074   1.2        ad 	size_t csize = 0;
   1075   1.1        ad 
   1076   1.1        ad #ifdef USE_BRK
   1077   1.1        ad 	/*
   1078   1.1        ad 	 * Do special brk allocation here, since base allocations don't need to
   1079   1.1        ad 	 * be chunk-aligned.
   1080   1.1        ad 	 */
   1081   1.1        ad 	if (brk_prev != (void *)-1) {
   1082   1.1        ad 		void *brk_cur;
   1083   1.1        ad 		intptr_t incr;
   1084   1.1        ad 
   1085   1.1        ad 		if (minsize != 0)
   1086   1.1        ad 			csize = CHUNK_CEILING(minsize);
   1087   1.1        ad 
   1088   1.1        ad 		malloc_mutex_lock(&brk_mtx);
   1089   1.1        ad 		do {
   1090   1.1        ad 			/* Get the current end of brk. */
   1091   1.1        ad 			brk_cur = sbrk(0);
   1092   1.1        ad 
   1093   1.1        ad 			/*
   1094   1.1        ad 			 * Calculate how much padding is necessary to
   1095   1.1        ad 			 * chunk-align the end of brk.  Don't worry about
   1096   1.1        ad 			 * brk_cur not being chunk-aligned though.
   1097   1.1        ad 			 */
   1098   1.1        ad 			incr = (intptr_t)chunksize
   1099   1.1        ad 			    - (intptr_t)CHUNK_ADDR2OFFSET(brk_cur);
   1100  1.20     lukem 			assert(incr >= 0);
   1101  1.20     lukem 			if ((size_t)incr < minsize)
   1102   1.1        ad 				incr += csize;
   1103   1.1        ad 
   1104   1.1        ad 			brk_prev = sbrk(incr);
   1105   1.1        ad 			if (brk_prev == brk_cur) {
   1106   1.1        ad 				/* Success. */
   1107   1.1        ad 				malloc_mutex_unlock(&brk_mtx);
   1108   1.1        ad 				base_pages = brk_cur;
   1109   1.1        ad 				base_next_addr = base_pages;
   1110   1.1        ad 				base_past_addr = (void *)((uintptr_t)base_pages
   1111   1.1        ad 				    + incr);
   1112   1.1        ad #ifdef MALLOC_STATS
   1113   1.1        ad 				base_mapped += incr;
   1114   1.1        ad #endif
   1115   1.1        ad 				return (false);
   1116   1.1        ad 			}
   1117   1.1        ad 		} while (brk_prev != (void *)-1);
   1118   1.1        ad 		malloc_mutex_unlock(&brk_mtx);
   1119   1.1        ad 	}
   1120   1.1        ad 	if (minsize == 0) {
   1121   1.1        ad 		/*
   1122   1.1        ad 		 * Failure during initialization doesn't matter, so avoid
   1123   1.1        ad 		 * falling through to the mmap-based page mapping code.
   1124   1.1        ad 		 */
   1125   1.1        ad 		return (true);
   1126   1.1        ad 	}
   1127   1.1        ad #endif
   1128   1.1        ad 	assert(minsize != 0);
   1129   1.1        ad 	csize = PAGE_CEILING(minsize);
   1130   1.1        ad 	base_pages = pages_map(NULL, csize);
   1131   1.1        ad 	if (base_pages == NULL)
   1132   1.1        ad 		return (true);
   1133   1.1        ad 	base_next_addr = base_pages;
   1134   1.1        ad 	base_past_addr = (void *)((uintptr_t)base_pages + csize);
   1135   1.1        ad #ifdef MALLOC_STATS
   1136   1.1        ad 	base_mapped += csize;
   1137   1.1        ad #endif
   1138   1.1        ad 	return (false);
   1139   1.1        ad }
   1140   1.1        ad 
   1141   1.1        ad static void *
   1142   1.1        ad base_alloc(size_t size)
   1143   1.1        ad {
   1144   1.1        ad 	void *ret;
   1145   1.1        ad 	size_t csize;
   1146   1.1        ad 
   1147   1.1        ad 	/* Round size up to nearest multiple of the cacheline size. */
   1148   1.1        ad 	csize = CACHELINE_CEILING(size);
   1149   1.1        ad 
   1150   1.1        ad 	malloc_mutex_lock(&base_mtx);
   1151   1.1        ad 
   1152   1.1        ad 	/* Make sure there's enough space for the allocation. */
   1153   1.1        ad 	if ((uintptr_t)base_next_addr + csize > (uintptr_t)base_past_addr) {
   1154   1.1        ad 		if (base_pages_alloc(csize)) {
   1155   1.1        ad 			ret = NULL;
   1156   1.1        ad 			goto RETURN;
   1157   1.1        ad 		}
   1158   1.1        ad 	}
   1159   1.1        ad 
   1160   1.1        ad 	/* Allocate. */
   1161   1.1        ad 	ret = base_next_addr;
   1162   1.1        ad 	base_next_addr = (void *)((uintptr_t)base_next_addr + csize);
   1163   1.1        ad 
   1164   1.1        ad RETURN:
   1165   1.1        ad 	malloc_mutex_unlock(&base_mtx);
   1166   1.1        ad 	return (ret);
   1167   1.1        ad }
   1168   1.1        ad 
   1169   1.1        ad static chunk_node_t *
   1170   1.1        ad base_chunk_node_alloc(void)
   1171   1.1        ad {
   1172   1.1        ad 	chunk_node_t *ret;
   1173   1.1        ad 
   1174   1.1        ad 	malloc_mutex_lock(&base_mtx);
   1175   1.1        ad 	if (base_chunk_nodes != NULL) {
   1176   1.1        ad 		ret = base_chunk_nodes;
   1177   1.2        ad 		/* LINTED */
   1178   1.1        ad 		base_chunk_nodes = *(chunk_node_t **)ret;
   1179   1.1        ad 		malloc_mutex_unlock(&base_mtx);
   1180   1.1        ad 	} else {
   1181   1.1        ad 		malloc_mutex_unlock(&base_mtx);
   1182   1.1        ad 		ret = (chunk_node_t *)base_alloc(sizeof(chunk_node_t));
   1183   1.1        ad 	}
   1184   1.1        ad 
   1185   1.1        ad 	return (ret);
   1186   1.1        ad }
   1187   1.1        ad 
   1188   1.1        ad static void
   1189   1.1        ad base_chunk_node_dealloc(chunk_node_t *node)
   1190   1.1        ad {
   1191   1.1        ad 
   1192   1.1        ad 	malloc_mutex_lock(&base_mtx);
   1193   1.2        ad 	/* LINTED */
   1194   1.1        ad 	*(chunk_node_t **)node = base_chunk_nodes;
   1195   1.1        ad 	base_chunk_nodes = node;
   1196   1.1        ad 	malloc_mutex_unlock(&base_mtx);
   1197   1.1        ad }
   1198   1.1        ad 
   1199   1.1        ad /******************************************************************************/
   1200   1.1        ad 
   1201   1.1        ad #ifdef MALLOC_STATS
   1202   1.1        ad static void
   1203   1.1        ad stats_print(arena_t *arena)
   1204   1.1        ad {
   1205   1.1        ad 	unsigned i;
   1206   1.1        ad 	int gap_start;
   1207   1.1        ad 
   1208   1.1        ad 	malloc_printf(
   1209   1.1        ad 	    "          allocated/mapped            nmalloc      ndalloc\n");
   1210   1.2        ad 
   1211   1.2        ad 	malloc_printf("small: %12zu %-12s %12llu %12llu\n",
   1212   1.1        ad 	    arena->stats.allocated_small, "", arena->stats.nmalloc_small,
   1213   1.1        ad 	    arena->stats.ndalloc_small);
   1214   1.2        ad 	malloc_printf("large: %12zu %-12s %12llu %12llu\n",
   1215   1.1        ad 	    arena->stats.allocated_large, "", arena->stats.nmalloc_large,
   1216   1.1        ad 	    arena->stats.ndalloc_large);
   1217   1.2        ad 	malloc_printf("total: %12zu/%-12zu %12llu %12llu\n",
   1218   1.1        ad 	    arena->stats.allocated_small + arena->stats.allocated_large,
   1219   1.1        ad 	    arena->stats.mapped,
   1220   1.1        ad 	    arena->stats.nmalloc_small + arena->stats.nmalloc_large,
   1221   1.1        ad 	    arena->stats.ndalloc_small + arena->stats.ndalloc_large);
   1222   1.1        ad 
   1223   1.1        ad 	malloc_printf("bins:     bin   size regs pgs  requests   newruns"
   1224   1.1        ad 	    "    reruns maxruns curruns\n");
   1225   1.1        ad 	for (i = 0, gap_start = -1; i < ntbins + nqbins + nsbins; i++) {
   1226   1.1        ad 		if (arena->bins[i].stats.nrequests == 0) {
   1227   1.1        ad 			if (gap_start == -1)
   1228   1.1        ad 				gap_start = i;
   1229   1.1        ad 		} else {
   1230   1.1        ad 			if (gap_start != -1) {
   1231   1.1        ad 				if (i > gap_start + 1) {
   1232   1.1        ad 					/* Gap of more than one size class. */
   1233   1.1        ad 					malloc_printf("[%u..%u]\n",
   1234   1.1        ad 					    gap_start, i - 1);
   1235   1.1        ad 				} else {
   1236   1.1        ad 					/* Gap of one size class. */
   1237   1.1        ad 					malloc_printf("[%u]\n", gap_start);
   1238   1.1        ad 				}
   1239   1.1        ad 				gap_start = -1;
   1240   1.1        ad 			}
   1241   1.1        ad 			malloc_printf(
   1242   1.1        ad 			    "%13u %1s %4u %4u %3u %9llu %9llu"
   1243   1.1        ad 			    " %9llu %7lu %7lu\n",
   1244   1.1        ad 			    i,
   1245   1.1        ad 			    i < ntbins ? "T" : i < ntbins + nqbins ? "Q" : "S",
   1246   1.1        ad 			    arena->bins[i].reg_size,
   1247   1.1        ad 			    arena->bins[i].nregs,
   1248   1.1        ad 			    arena->bins[i].run_size >> pagesize_2pow,
   1249   1.1        ad 			    arena->bins[i].stats.nrequests,
   1250   1.1        ad 			    arena->bins[i].stats.nruns,
   1251   1.1        ad 			    arena->bins[i].stats.reruns,
   1252   1.1        ad 			    arena->bins[i].stats.highruns,
   1253   1.1        ad 			    arena->bins[i].stats.curruns);
   1254   1.1        ad 		}
   1255   1.1        ad 	}
   1256   1.1        ad 	if (gap_start != -1) {
   1257   1.1        ad 		if (i > gap_start + 1) {
   1258   1.1        ad 			/* Gap of more than one size class. */
   1259   1.1        ad 			malloc_printf("[%u..%u]\n", gap_start, i - 1);
   1260   1.1        ad 		} else {
   1261   1.1        ad 			/* Gap of one size class. */
   1262   1.1        ad 			malloc_printf("[%u]\n", gap_start);
   1263   1.1        ad 		}
   1264   1.1        ad 	}
   1265   1.1        ad }
   1266   1.1        ad #endif
   1267   1.1        ad 
   1268   1.1        ad /*
   1269   1.1        ad  * End Utility functions/macros.
   1270   1.1        ad  */
   1271   1.1        ad /******************************************************************************/
   1272   1.1        ad /*
   1273   1.1        ad  * Begin chunk management functions.
   1274   1.1        ad  */
   1275   1.1        ad 
   1276   1.7      yamt #ifndef lint
   1277   1.1        ad static inline int
   1278   1.1        ad chunk_comp(chunk_node_t *a, chunk_node_t *b)
   1279   1.1        ad {
   1280   1.1        ad 
   1281   1.1        ad 	assert(a != NULL);
   1282   1.1        ad 	assert(b != NULL);
   1283   1.1        ad 
   1284   1.1        ad 	if ((uintptr_t)a->chunk < (uintptr_t)b->chunk)
   1285   1.1        ad 		return (-1);
   1286   1.1        ad 	else if (a->chunk == b->chunk)
   1287   1.1        ad 		return (0);
   1288   1.1        ad 	else
   1289   1.1        ad 		return (1);
   1290   1.1        ad }
   1291   1.1        ad 
   1292   1.1        ad /* Generate red-black tree code for chunks. */
   1293   1.1        ad RB_GENERATE_STATIC(chunk_tree_s, chunk_node_s, link, chunk_comp);
   1294   1.2        ad #endif
   1295   1.1        ad 
   1296   1.1        ad static void *
   1297   1.5      yamt pages_map_align(void *addr, size_t size, int align)
   1298   1.1        ad {
   1299   1.1        ad 	void *ret;
   1300   1.1        ad 
   1301   1.1        ad 	/*
   1302   1.1        ad 	 * We don't use MAP_FIXED here, because it can cause the *replacement*
   1303   1.1        ad 	 * of existing mappings, and we only want to create new mappings.
   1304   1.1        ad 	 */
   1305   1.5      yamt 	ret = mmap(addr, size, PROT_READ | PROT_WRITE,
   1306   1.5      yamt 	    MAP_PRIVATE | MAP_ANON | MAP_ALIGNED(align), -1, 0);
   1307   1.1        ad 	assert(ret != NULL);
   1308   1.1        ad 
   1309   1.1        ad 	if (ret == MAP_FAILED)
   1310   1.1        ad 		ret = NULL;
   1311   1.1        ad 	else if (addr != NULL && ret != addr) {
   1312   1.1        ad 		/*
   1313   1.1        ad 		 * We succeeded in mapping memory, but not in the right place.
   1314   1.1        ad 		 */
   1315   1.1        ad 		if (munmap(ret, size) == -1) {
   1316   1.1        ad 			char buf[STRERROR_BUF];
   1317   1.1        ad 
   1318  1.16  christos 			STRERROR_R(errno, buf, sizeof(buf));
   1319  1.16  christos 			_malloc_message(getprogname(),
   1320   1.1        ad 			    ": (malloc) Error in munmap(): ", buf, "\n");
   1321   1.1        ad 			if (opt_abort)
   1322   1.1        ad 				abort();
   1323   1.1        ad 		}
   1324   1.1        ad 		ret = NULL;
   1325   1.1        ad 	}
   1326   1.1        ad 
   1327   1.1        ad 	assert(ret == NULL || (addr == NULL && ret != addr)
   1328   1.1        ad 	    || (addr != NULL && ret == addr));
   1329   1.1        ad 	return (ret);
   1330   1.1        ad }
   1331   1.1        ad 
   1332   1.5      yamt static void *
   1333   1.5      yamt pages_map(void *addr, size_t size)
   1334   1.5      yamt {
   1335   1.5      yamt 
   1336   1.5      yamt 	return pages_map_align(addr, size, 0);
   1337   1.5      yamt }
   1338   1.5      yamt 
   1339   1.1        ad static void
   1340   1.1        ad pages_unmap(void *addr, size_t size)
   1341   1.1        ad {
   1342   1.1        ad 
   1343   1.1        ad 	if (munmap(addr, size) == -1) {
   1344   1.1        ad 		char buf[STRERROR_BUF];
   1345   1.1        ad 
   1346  1.16  christos 		STRERROR_R(errno, buf, sizeof(buf));
   1347  1.16  christos 		_malloc_message(getprogname(),
   1348   1.1        ad 		    ": (malloc) Error in munmap(): ", buf, "\n");
   1349   1.1        ad 		if (opt_abort)
   1350   1.1        ad 			abort();
   1351   1.1        ad 	}
   1352   1.1        ad }
   1353   1.1        ad 
   1354   1.1        ad static void *
   1355   1.1        ad chunk_alloc(size_t size)
   1356   1.1        ad {
   1357   1.1        ad 	void *ret, *chunk;
   1358   1.1        ad 	chunk_node_t *tchunk, *delchunk;
   1359   1.1        ad 
   1360   1.1        ad 	assert(size != 0);
   1361   1.1        ad 	assert((size & chunksize_mask) == 0);
   1362   1.1        ad 
   1363   1.1        ad 	malloc_mutex_lock(&chunks_mtx);
   1364   1.1        ad 
   1365   1.1        ad 	if (size == chunksize) {
   1366   1.1        ad 		/*
   1367   1.1        ad 		 * Check for address ranges that were previously chunks and try
   1368   1.1        ad 		 * to use them.
   1369   1.1        ad 		 */
   1370   1.1        ad 
   1371   1.2        ad 		/* LINTED */
   1372   1.1        ad 		tchunk = RB_MIN(chunk_tree_s, &old_chunks);
   1373   1.1        ad 		while (tchunk != NULL) {
   1374   1.1        ad 			/* Found an address range.  Try to recycle it. */
   1375   1.1        ad 
   1376   1.1        ad 			chunk = tchunk->chunk;
   1377   1.1        ad 			delchunk = tchunk;
   1378   1.2        ad 			/* LINTED */
   1379   1.1        ad 			tchunk = RB_NEXT(chunk_tree_s, &old_chunks, delchunk);
   1380   1.1        ad 
   1381   1.1        ad 			/* Remove delchunk from the tree. */
   1382   1.2        ad 			/* LINTED */
   1383   1.1        ad 			RB_REMOVE(chunk_tree_s, &old_chunks, delchunk);
   1384   1.1        ad 			base_chunk_node_dealloc(delchunk);
   1385   1.1        ad 
   1386   1.1        ad #ifdef USE_BRK
   1387   1.1        ad 			if ((uintptr_t)chunk >= (uintptr_t)brk_base
   1388   1.1        ad 			    && (uintptr_t)chunk < (uintptr_t)brk_max) {
   1389   1.1        ad 				/* Re-use a previously freed brk chunk. */
   1390   1.1        ad 				ret = chunk;
   1391   1.1        ad 				goto RETURN;
   1392   1.1        ad 			}
   1393   1.1        ad #endif
   1394   1.1        ad 			if ((ret = pages_map(chunk, size)) != NULL) {
   1395   1.1        ad 				/* Success. */
   1396   1.1        ad 				goto RETURN;
   1397   1.1        ad 			}
   1398   1.1        ad 		}
   1399   1.1        ad 	}
   1400   1.1        ad 
   1401   1.1        ad 	/*
   1402   1.1        ad 	 * Try to over-allocate, but allow the OS to place the allocation
   1403   1.1        ad 	 * anywhere.  Beware of size_t wrap-around.
   1404   1.1        ad 	 */
   1405   1.1        ad 	if (size + chunksize > size) {
   1406   1.5      yamt 		if ((ret = pages_map_align(NULL, size, chunksize_2pow))
   1407   1.5      yamt 		    != NULL) {
   1408   1.1        ad 			goto RETURN;
   1409   1.1        ad 		}
   1410   1.1        ad 	}
   1411   1.1        ad 
   1412   1.1        ad #ifdef USE_BRK
   1413   1.1        ad 	/*
   1414   1.1        ad 	 * Try to create allocations in brk, in order to make full use of
   1415   1.1        ad 	 * limited address space.
   1416   1.1        ad 	 */
   1417   1.1        ad 	if (brk_prev != (void *)-1) {
   1418   1.1        ad 		void *brk_cur;
   1419   1.1        ad 		intptr_t incr;
   1420   1.1        ad 
   1421   1.1        ad 		/*
   1422   1.1        ad 		 * The loop is necessary to recover from races with other
   1423   1.1        ad 		 * threads that are using brk for something other than malloc.
   1424   1.1        ad 		 */
   1425   1.1        ad 		malloc_mutex_lock(&brk_mtx);
   1426   1.1        ad 		do {
   1427   1.1        ad 			/* Get the current end of brk. */
   1428   1.1        ad 			brk_cur = sbrk(0);
   1429   1.1        ad 
   1430   1.1        ad 			/*
   1431   1.1        ad 			 * Calculate how much padding is necessary to
   1432   1.1        ad 			 * chunk-align the end of brk.
   1433   1.1        ad 			 */
   1434   1.1        ad 			incr = (intptr_t)size
   1435   1.1        ad 			    - (intptr_t)CHUNK_ADDR2OFFSET(brk_cur);
   1436  1.20     lukem 			if (incr == (intptr_t)size) {
   1437   1.1        ad 				ret = brk_cur;
   1438   1.1        ad 			} else {
   1439   1.1        ad 				ret = (void *)((intptr_t)brk_cur + incr);
   1440   1.1        ad 				incr += size;
   1441   1.1        ad 			}
   1442   1.1        ad 
   1443   1.1        ad 			brk_prev = sbrk(incr);
   1444   1.1        ad 			if (brk_prev == brk_cur) {
   1445   1.1        ad 				/* Success. */
   1446   1.1        ad 				malloc_mutex_unlock(&brk_mtx);
   1447   1.1        ad 				brk_max = (void *)((intptr_t)ret + size);
   1448   1.1        ad 				goto RETURN;
   1449   1.1        ad 			}
   1450   1.1        ad 		} while (brk_prev != (void *)-1);
   1451   1.1        ad 		malloc_mutex_unlock(&brk_mtx);
   1452   1.1        ad 	}
   1453   1.1        ad #endif
   1454   1.1        ad 
   1455   1.1        ad 	/* All strategies for allocation failed. */
   1456   1.1        ad 	ret = NULL;
   1457   1.1        ad RETURN:
   1458   1.1        ad 	if (ret != NULL) {
   1459   1.1        ad 		chunk_node_t key;
   1460   1.1        ad 		/*
   1461   1.1        ad 		 * Clean out any entries in old_chunks that overlap with the
   1462   1.1        ad 		 * memory we just allocated.
   1463   1.1        ad 		 */
   1464   1.1        ad 		key.chunk = ret;
   1465   1.2        ad 		/* LINTED */
   1466   1.1        ad 		tchunk = RB_NFIND(chunk_tree_s, &old_chunks, &key);
   1467   1.1        ad 		while (tchunk != NULL
   1468   1.1        ad 		    && (uintptr_t)tchunk->chunk >= (uintptr_t)ret
   1469   1.1        ad 		    && (uintptr_t)tchunk->chunk < (uintptr_t)ret + size) {
   1470   1.1        ad 			delchunk = tchunk;
   1471   1.2        ad 			/* LINTED */
   1472   1.1        ad 			tchunk = RB_NEXT(chunk_tree_s, &old_chunks, delchunk);
   1473   1.2        ad 			/* LINTED */
   1474   1.1        ad 			RB_REMOVE(chunk_tree_s, &old_chunks, delchunk);
   1475   1.1        ad 			base_chunk_node_dealloc(delchunk);
   1476   1.1        ad 		}
   1477   1.1        ad 
   1478   1.1        ad 	}
   1479   1.1        ad #ifdef MALLOC_STATS
   1480   1.1        ad 	if (ret != NULL) {
   1481   1.1        ad 		stats_chunks.nchunks += (size / chunksize);
   1482   1.1        ad 		stats_chunks.curchunks += (size / chunksize);
   1483   1.1        ad 	}
   1484   1.1        ad 	if (stats_chunks.curchunks > stats_chunks.highchunks)
   1485   1.1        ad 		stats_chunks.highchunks = stats_chunks.curchunks;
   1486   1.1        ad #endif
   1487   1.1        ad 	malloc_mutex_unlock(&chunks_mtx);
   1488   1.1        ad 
   1489   1.1        ad 	assert(CHUNK_ADDR2BASE(ret) == ret);
   1490   1.1        ad 	return (ret);
   1491   1.1        ad }
   1492   1.1        ad 
   1493   1.1        ad static void
   1494   1.1        ad chunk_dealloc(void *chunk, size_t size)
   1495   1.1        ad {
   1496   1.1        ad 	chunk_node_t *node;
   1497   1.1        ad 
   1498   1.1        ad 	assert(chunk != NULL);
   1499   1.1        ad 	assert(CHUNK_ADDR2BASE(chunk) == chunk);
   1500   1.1        ad 	assert(size != 0);
   1501   1.1        ad 	assert((size & chunksize_mask) == 0);
   1502   1.1        ad 
   1503   1.1        ad 	malloc_mutex_lock(&chunks_mtx);
   1504   1.1        ad 
   1505   1.1        ad #ifdef USE_BRK
   1506   1.1        ad 	if ((uintptr_t)chunk >= (uintptr_t)brk_base
   1507   1.1        ad 	    && (uintptr_t)chunk < (uintptr_t)brk_max) {
   1508   1.1        ad 		void *brk_cur;
   1509   1.1        ad 
   1510   1.1        ad 		malloc_mutex_lock(&brk_mtx);
   1511   1.1        ad 		/* Get the current end of brk. */
   1512   1.1        ad 		brk_cur = sbrk(0);
   1513   1.1        ad 
   1514   1.1        ad 		/*
   1515   1.1        ad 		 * Try to shrink the data segment if this chunk is at the end
   1516   1.1        ad 		 * of the data segment.  The sbrk() call here is subject to a
   1517   1.1        ad 		 * race condition with threads that use brk(2) or sbrk(2)
   1518   1.1        ad 		 * directly, but the alternative would be to leak memory for
   1519   1.1        ad 		 * the sake of poorly designed multi-threaded programs.
   1520   1.1        ad 		 */
   1521   1.1        ad 		if (brk_cur == brk_max
   1522   1.1        ad 		    && (void *)((uintptr_t)chunk + size) == brk_max
   1523   1.1        ad 		    && sbrk(-(intptr_t)size) == brk_max) {
   1524   1.1        ad 			malloc_mutex_unlock(&brk_mtx);
   1525   1.1        ad 			if (brk_prev == brk_max) {
   1526   1.1        ad 				/* Success. */
   1527   1.1        ad 				brk_prev = (void *)((intptr_t)brk_max
   1528   1.1        ad 				    - (intptr_t)size);
   1529   1.1        ad 				brk_max = brk_prev;
   1530   1.1        ad 			}
   1531   1.1        ad 		} else {
   1532   1.1        ad 			size_t offset;
   1533   1.1        ad 
   1534   1.1        ad 			malloc_mutex_unlock(&brk_mtx);
   1535   1.1        ad 			madvise(chunk, size, MADV_FREE);
   1536   1.1        ad 
   1537   1.1        ad 			/*
   1538   1.1        ad 			 * Iteratively create records of each chunk-sized
   1539   1.1        ad 			 * memory region that 'chunk' is comprised of, so that
   1540   1.1        ad 			 * the address range can be recycled if memory usage
   1541   1.1        ad 			 * increases later on.
   1542   1.1        ad 			 */
   1543   1.1        ad 			for (offset = 0; offset < size; offset += chunksize) {
   1544   1.1        ad 				node = base_chunk_node_alloc();
   1545   1.1        ad 				if (node == NULL)
   1546   1.1        ad 					break;
   1547   1.1        ad 
   1548   1.1        ad 				node->chunk = (void *)((uintptr_t)chunk
   1549   1.1        ad 				    + (uintptr_t)offset);
   1550   1.1        ad 				node->size = chunksize;
   1551   1.2        ad 				/* LINTED */
   1552   1.1        ad 				RB_INSERT(chunk_tree_s, &old_chunks, node);
   1553   1.1        ad 			}
   1554   1.1        ad 		}
   1555   1.1        ad 	} else {
   1556   1.1        ad #endif
   1557   1.1        ad 		pages_unmap(chunk, size);
   1558   1.1        ad 
   1559   1.1        ad 		/*
   1560   1.1        ad 		 * Make a record of the chunk's address, so that the address
   1561   1.1        ad 		 * range can be recycled if memory usage increases later on.
   1562   1.1        ad 		 * Don't bother to create entries if (size > chunksize), since
   1563   1.1        ad 		 * doing so could cause scalability issues for truly gargantuan
   1564   1.1        ad 		 * objects (many gigabytes or larger).
   1565   1.1        ad 		 */
   1566   1.1        ad 		if (size == chunksize) {
   1567   1.1        ad 			node = base_chunk_node_alloc();
   1568   1.1        ad 			if (node != NULL) {
   1569   1.1        ad 				node->chunk = (void *)(uintptr_t)chunk;
   1570   1.1        ad 				node->size = chunksize;
   1571   1.2        ad 				/* LINTED */
   1572   1.1        ad 				RB_INSERT(chunk_tree_s, &old_chunks, node);
   1573   1.1        ad 			}
   1574   1.1        ad 		}
   1575   1.1        ad #ifdef USE_BRK
   1576   1.1        ad 	}
   1577   1.1        ad #endif
   1578   1.1        ad 
   1579   1.1        ad #ifdef MALLOC_STATS
   1580   1.1        ad 	stats_chunks.curchunks -= (size / chunksize);
   1581   1.1        ad #endif
   1582   1.1        ad 	malloc_mutex_unlock(&chunks_mtx);
   1583   1.1        ad }
   1584   1.1        ad 
   1585   1.1        ad /*
   1586   1.1        ad  * End chunk management functions.
   1587   1.1        ad  */
   1588   1.1        ad /******************************************************************************/
   1589   1.1        ad /*
   1590   1.1        ad  * Begin arena.
   1591   1.1        ad  */
   1592   1.1        ad 
   1593   1.1        ad /*
   1594  1.17        ad  * Choose an arena based on a per-thread and (optimistically) per-CPU value.
   1595  1.17        ad  *
   1596  1.17        ad  * We maintain at least one block of arenas.  Usually there are more.
   1597  1.17        ad  * The blocks are $ncpu arenas in size.  Whole blocks are 'hashed'
   1598  1.17        ad  * amongst threads.  To accomplish this, next_arena advances only in
   1599  1.17        ad  * ncpu steps.
   1600   1.1        ad  */
   1601  1.19        ad static __noinline arena_t *
   1602  1.19        ad choose_arena_hard(void)
   1603   1.1        ad {
   1604  1.17        ad 	unsigned i, curcpu;
   1605  1.17        ad 	arena_t **map;
   1606   1.1        ad 
   1607  1.17        ad 	/* Initialize the current block of arenas and advance to next. */
   1608   1.1        ad 	malloc_mutex_lock(&arenas_mtx);
   1609  1.17        ad 	assert(next_arena % ncpus == 0);
   1610  1.17        ad 	assert(narenas % ncpus == 0);
   1611  1.17        ad 	map = &arenas[next_arena];
   1612  1.17        ad 	set_arenas_map(map);
   1613  1.17        ad 	for (i = 0; i < ncpus; i++) {
   1614  1.17        ad 		if (arenas[next_arena] == NULL)
   1615  1.17        ad 			arenas_extend(next_arena);
   1616  1.17        ad 		next_arena = (next_arena + 1) % narenas;
   1617  1.15        ad 	}
   1618   1.1        ad 	malloc_mutex_unlock(&arenas_mtx);
   1619   1.1        ad 
   1620  1.17        ad 	/*
   1621  1.17        ad 	 * If we were unable to allocate an arena above, then default to
   1622  1.17        ad 	 * the first arena, which is always present.
   1623  1.17        ad 	 */
   1624  1.17        ad 	curcpu = thr_curcpu();
   1625  1.17        ad 	if (map[curcpu] != NULL)
   1626  1.17        ad 		return map[curcpu];
   1627  1.17        ad 	return arenas[0];
   1628   1.1        ad }
   1629   1.1        ad 
   1630  1.19        ad static inline arena_t *
   1631  1.19        ad choose_arena(void)
   1632  1.19        ad {
   1633  1.19        ad 	unsigned curcpu;
   1634  1.19        ad 	arena_t **map;
   1635  1.19        ad 
   1636  1.19        ad 	map = get_arenas_map();
   1637  1.19        ad 	curcpu = thr_curcpu();
   1638  1.19        ad 	if (__predict_true(map != NULL && map[curcpu] != NULL))
   1639  1.19        ad 		return map[curcpu];
   1640  1.19        ad 
   1641  1.19        ad         return choose_arena_hard();
   1642  1.19        ad }
   1643  1.19        ad 
   1644   1.7      yamt #ifndef lint
   1645   1.1        ad static inline int
   1646   1.1        ad arena_chunk_comp(arena_chunk_t *a, arena_chunk_t *b)
   1647   1.1        ad {
   1648   1.1        ad 
   1649   1.1        ad 	assert(a != NULL);
   1650   1.1        ad 	assert(b != NULL);
   1651   1.1        ad 
   1652  1.40     joerg 	if (a->max_frun_npages < b->max_frun_npages)
   1653  1.40     joerg 		return -1;
   1654  1.40     joerg 	if (a->max_frun_npages > b->max_frun_npages)
   1655  1.40     joerg 		return 1;
   1656  1.40     joerg 
   1657   1.1        ad 	if ((uintptr_t)a < (uintptr_t)b)
   1658   1.1        ad 		return (-1);
   1659   1.1        ad 	else if (a == b)
   1660   1.1        ad 		return (0);
   1661   1.1        ad 	else
   1662   1.1        ad 		return (1);
   1663   1.1        ad }
   1664   1.1        ad 
   1665   1.1        ad /* Generate red-black tree code for arena chunks. */
   1666   1.1        ad RB_GENERATE_STATIC(arena_chunk_tree_s, arena_chunk_s, link, arena_chunk_comp);
   1667   1.2        ad #endif
   1668   1.1        ad 
   1669   1.7      yamt #ifndef lint
   1670   1.1        ad static inline int
   1671   1.1        ad arena_run_comp(arena_run_t *a, arena_run_t *b)
   1672   1.1        ad {
   1673   1.1        ad 
   1674   1.1        ad 	assert(a != NULL);
   1675   1.1        ad 	assert(b != NULL);
   1676   1.1        ad 
   1677   1.1        ad 	if ((uintptr_t)a < (uintptr_t)b)
   1678   1.1        ad 		return (-1);
   1679   1.1        ad 	else if (a == b)
   1680   1.1        ad 		return (0);
   1681   1.1        ad 	else
   1682   1.1        ad 		return (1);
   1683   1.1        ad }
   1684   1.1        ad 
   1685   1.1        ad /* Generate red-black tree code for arena runs. */
   1686   1.1        ad RB_GENERATE_STATIC(arena_run_tree_s, arena_run_s, link, arena_run_comp);
   1687   1.2        ad #endif
   1688   1.1        ad 
   1689   1.1        ad static inline void *
   1690   1.1        ad arena_run_reg_alloc(arena_run_t *run, arena_bin_t *bin)
   1691   1.1        ad {
   1692   1.1        ad 	void *ret;
   1693   1.1        ad 	unsigned i, mask, bit, regind;
   1694   1.1        ad 
   1695   1.1        ad 	assert(run->magic == ARENA_RUN_MAGIC);
   1696   1.1        ad 	assert(run->regs_minelm < bin->regs_mask_nelms);
   1697   1.1        ad 
   1698   1.1        ad 	/*
   1699   1.1        ad 	 * Move the first check outside the loop, so that run->regs_minelm can
   1700   1.1        ad 	 * be updated unconditionally, without the possibility of updating it
   1701   1.1        ad 	 * multiple times.
   1702   1.1        ad 	 */
   1703   1.1        ad 	i = run->regs_minelm;
   1704   1.1        ad 	mask = run->regs_mask[i];
   1705   1.1        ad 	if (mask != 0) {
   1706   1.1        ad 		/* Usable allocation found. */
   1707   1.1        ad 		bit = ffs((int)mask) - 1;
   1708   1.1        ad 
   1709   1.1        ad 		regind = ((i << (SIZEOF_INT_2POW + 3)) + bit);
   1710   1.1        ad 		ret = (void *)(((uintptr_t)run) + bin->reg0_offset
   1711   1.1        ad 		    + (bin->reg_size * regind));
   1712   1.1        ad 
   1713   1.1        ad 		/* Clear bit. */
   1714  1.45     kamil 		mask ^= (1U << bit);
   1715   1.1        ad 		run->regs_mask[i] = mask;
   1716   1.1        ad 
   1717   1.1        ad 		return (ret);
   1718   1.1        ad 	}
   1719   1.1        ad 
   1720   1.1        ad 	for (i++; i < bin->regs_mask_nelms; i++) {
   1721   1.1        ad 		mask = run->regs_mask[i];
   1722   1.1        ad 		if (mask != 0) {
   1723   1.1        ad 			/* Usable allocation found. */
   1724   1.1        ad 			bit = ffs((int)mask) - 1;
   1725   1.1        ad 
   1726   1.1        ad 			regind = ((i << (SIZEOF_INT_2POW + 3)) + bit);
   1727   1.1        ad 			ret = (void *)(((uintptr_t)run) + bin->reg0_offset
   1728   1.1        ad 			    + (bin->reg_size * regind));
   1729   1.1        ad 
   1730   1.1        ad 			/* Clear bit. */
   1731  1.45     kamil 			mask ^= (1U << bit);
   1732   1.1        ad 			run->regs_mask[i] = mask;
   1733   1.1        ad 
   1734   1.1        ad 			/*
   1735   1.1        ad 			 * Make a note that nothing before this element
   1736   1.1        ad 			 * contains a free region.
   1737   1.1        ad 			 */
   1738   1.1        ad 			run->regs_minelm = i; /* Low payoff: + (mask == 0); */
   1739   1.1        ad 
   1740   1.1        ad 			return (ret);
   1741   1.1        ad 		}
   1742   1.1        ad 	}
   1743   1.1        ad 	/* Not reached. */
   1744   1.7      yamt 	/* LINTED */
   1745   1.1        ad 	assert(0);
   1746   1.1        ad 	return (NULL);
   1747   1.1        ad }
   1748   1.1        ad 
   1749   1.1        ad static inline void
   1750   1.1        ad arena_run_reg_dalloc(arena_run_t *run, arena_bin_t *bin, void *ptr, size_t size)
   1751   1.1        ad {
   1752   1.1        ad 	/*
   1753   1.1        ad 	 * To divide by a number D that is not a power of two we multiply
   1754   1.1        ad 	 * by (2^21 / D) and then right shift by 21 positions.
   1755   1.1        ad 	 *
   1756   1.1        ad 	 *   X / D
   1757   1.1        ad 	 *
   1758   1.1        ad 	 * becomes
   1759   1.1        ad 	 *
   1760   1.1        ad 	 *   (X * size_invs[(D >> QUANTUM_2POW_MIN) - 3]) >> SIZE_INV_SHIFT
   1761   1.1        ad 	 */
   1762   1.1        ad #define SIZE_INV_SHIFT 21
   1763   1.1        ad #define SIZE_INV(s) (((1 << SIZE_INV_SHIFT) / (s << QUANTUM_2POW_MIN)) + 1)
   1764   1.1        ad 	static const unsigned size_invs[] = {
   1765   1.1        ad 	    SIZE_INV(3),
   1766   1.1        ad 	    SIZE_INV(4), SIZE_INV(5), SIZE_INV(6), SIZE_INV(7),
   1767   1.1        ad 	    SIZE_INV(8), SIZE_INV(9), SIZE_INV(10), SIZE_INV(11),
   1768   1.1        ad 	    SIZE_INV(12),SIZE_INV(13), SIZE_INV(14), SIZE_INV(15),
   1769   1.1        ad 	    SIZE_INV(16),SIZE_INV(17), SIZE_INV(18), SIZE_INV(19),
   1770   1.1        ad 	    SIZE_INV(20),SIZE_INV(21), SIZE_INV(22), SIZE_INV(23),
   1771   1.1        ad 	    SIZE_INV(24),SIZE_INV(25), SIZE_INV(26), SIZE_INV(27),
   1772   1.1        ad 	    SIZE_INV(28),SIZE_INV(29), SIZE_INV(30), SIZE_INV(31)
   1773   1.1        ad #if (QUANTUM_2POW_MIN < 4)
   1774   1.1        ad 	    ,
   1775   1.1        ad 	    SIZE_INV(32), SIZE_INV(33), SIZE_INV(34), SIZE_INV(35),
   1776   1.1        ad 	    SIZE_INV(36), SIZE_INV(37), SIZE_INV(38), SIZE_INV(39),
   1777   1.1        ad 	    SIZE_INV(40), SIZE_INV(41), SIZE_INV(42), SIZE_INV(43),
   1778   1.1        ad 	    SIZE_INV(44), SIZE_INV(45), SIZE_INV(46), SIZE_INV(47),
   1779   1.1        ad 	    SIZE_INV(48), SIZE_INV(49), SIZE_INV(50), SIZE_INV(51),
   1780   1.1        ad 	    SIZE_INV(52), SIZE_INV(53), SIZE_INV(54), SIZE_INV(55),
   1781   1.1        ad 	    SIZE_INV(56), SIZE_INV(57), SIZE_INV(58), SIZE_INV(59),
   1782   1.1        ad 	    SIZE_INV(60), SIZE_INV(61), SIZE_INV(62), SIZE_INV(63)
   1783   1.1        ad #endif
   1784   1.1        ad 	};
   1785   1.1        ad 	unsigned diff, regind, elm, bit;
   1786   1.1        ad 
   1787   1.7      yamt 	/* LINTED */
   1788   1.1        ad 	assert(run->magic == ARENA_RUN_MAGIC);
   1789   1.1        ad 	assert(((sizeof(size_invs)) / sizeof(unsigned)) + 3
   1790   1.1        ad 	    >= (SMALL_MAX_DEFAULT >> QUANTUM_2POW_MIN));
   1791   1.1        ad 
   1792   1.1        ad 	/*
   1793   1.1        ad 	 * Avoid doing division with a variable divisor if possible.  Using
   1794   1.1        ad 	 * actual division here can reduce allocator throughput by over 20%!
   1795   1.1        ad 	 */
   1796   1.1        ad 	diff = (unsigned)((uintptr_t)ptr - (uintptr_t)run - bin->reg0_offset);
   1797   1.1        ad 	if ((size & (size - 1)) == 0) {
   1798   1.1        ad 		/*
   1799   1.1        ad 		 * log2_table allows fast division of a power of two in the
   1800   1.1        ad 		 * [1..128] range.
   1801   1.1        ad 		 *
   1802   1.1        ad 		 * (x / divisor) becomes (x >> log2_table[divisor - 1]).
   1803   1.1        ad 		 */
   1804   1.1        ad 		static const unsigned char log2_table[] = {
   1805   1.1        ad 		    0, 1, 0, 2, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 4,
   1806   1.1        ad 		    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5,
   1807   1.1        ad 		    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
   1808   1.1        ad 		    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6,
   1809   1.1        ad 		    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
   1810   1.1        ad 		    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
   1811   1.1        ad 		    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
   1812   1.1        ad 		    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7
   1813   1.1        ad 		};
   1814   1.1        ad 
   1815   1.1        ad 		if (size <= 128)
   1816   1.1        ad 			regind = (diff >> log2_table[size - 1]);
   1817   1.1        ad 		else if (size <= 32768)
   1818   1.1        ad 			regind = diff >> (8 + log2_table[(size >> 8) - 1]);
   1819   1.1        ad 		else {
   1820   1.1        ad 			/*
   1821   1.1        ad 			 * The page size is too large for us to use the lookup
   1822   1.1        ad 			 * table.  Use real division.
   1823   1.1        ad 			 */
   1824   1.9  christos 			regind = (unsigned)(diff / size);
   1825   1.1        ad 		}
   1826   1.1        ad 	} else if (size <= ((sizeof(size_invs) / sizeof(unsigned))
   1827   1.1        ad 	    << QUANTUM_2POW_MIN) + 2) {
   1828   1.1        ad 		regind = size_invs[(size >> QUANTUM_2POW_MIN) - 3] * diff;
   1829   1.1        ad 		regind >>= SIZE_INV_SHIFT;
   1830   1.1        ad 	} else {
   1831   1.1        ad 		/*
   1832   1.1        ad 		 * size_invs isn't large enough to handle this size class, so
   1833   1.1        ad 		 * calculate regind using actual division.  This only happens
   1834   1.1        ad 		 * if the user increases small_max via the 'S' runtime
   1835   1.1        ad 		 * configuration option.
   1836   1.1        ad 		 */
   1837   1.9  christos 		regind = (unsigned)(diff / size);
   1838   1.1        ad 	};
   1839   1.1        ad 	assert(diff == regind * size);
   1840   1.1        ad 	assert(regind < bin->nregs);
   1841   1.1        ad 
   1842   1.1        ad 	elm = regind >> (SIZEOF_INT_2POW + 3);
   1843   1.1        ad 	if (elm < run->regs_minelm)
   1844   1.1        ad 		run->regs_minelm = elm;
   1845   1.1        ad 	bit = regind - (elm << (SIZEOF_INT_2POW + 3));
   1846  1.45     kamil 	assert((run->regs_mask[elm] & (1U << bit)) == 0);
   1847  1.45     kamil 	run->regs_mask[elm] |= (1U << bit);
   1848   1.1        ad #undef SIZE_INV
   1849   1.1        ad #undef SIZE_INV_SHIFT
   1850   1.1        ad }
   1851   1.1        ad 
   1852   1.1        ad static void
   1853   1.1        ad arena_run_split(arena_t *arena, arena_run_t *run, size_t size)
   1854   1.1        ad {
   1855   1.1        ad 	arena_chunk_t *chunk;
   1856   1.1        ad 	unsigned run_ind, map_offset, total_pages, need_pages, rem_pages;
   1857   1.1        ad 	unsigned i;
   1858   1.1        ad 
   1859   1.1        ad 	chunk = (arena_chunk_t *)CHUNK_ADDR2BASE(run);
   1860   1.1        ad 	run_ind = (unsigned)(((uintptr_t)run - (uintptr_t)chunk)
   1861   1.1        ad 	    >> pagesize_2pow);
   1862   1.1        ad 	total_pages = chunk->map[run_ind].npages;
   1863   1.9  christos 	need_pages = (unsigned)(size >> pagesize_2pow);
   1864   1.1        ad 	assert(need_pages <= total_pages);
   1865   1.1        ad 	rem_pages = total_pages - need_pages;
   1866   1.1        ad 
   1867   1.1        ad 	/* Split enough pages from the front of run to fit allocation size. */
   1868   1.1        ad 	map_offset = run_ind;
   1869   1.1        ad 	for (i = 0; i < need_pages; i++) {
   1870   1.1        ad 		chunk->map[map_offset + i].npages = need_pages;
   1871   1.1        ad 		chunk->map[map_offset + i].pos = i;
   1872   1.1        ad 	}
   1873   1.1        ad 
   1874   1.1        ad 	/* Keep track of trailing unused pages for later use. */
   1875   1.1        ad 	if (rem_pages > 0) {
   1876   1.1        ad 		/* Update map for trailing pages. */
   1877   1.1        ad 		map_offset += need_pages;
   1878   1.1        ad 		chunk->map[map_offset].npages = rem_pages;
   1879   1.1        ad 		chunk->map[map_offset].pos = POS_FREE;
   1880   1.1        ad 		chunk->map[map_offset + rem_pages - 1].npages = rem_pages;
   1881   1.1        ad 		chunk->map[map_offset + rem_pages - 1].pos = POS_FREE;
   1882   1.1        ad 	}
   1883   1.1        ad 
   1884   1.1        ad 	chunk->pages_used += need_pages;
   1885   1.1        ad }
   1886   1.1        ad 
   1887   1.1        ad static arena_chunk_t *
   1888   1.1        ad arena_chunk_alloc(arena_t *arena)
   1889   1.1        ad {
   1890   1.1        ad 	arena_chunk_t *chunk;
   1891   1.1        ad 
   1892   1.1        ad 	if (arena->spare != NULL) {
   1893   1.1        ad 		chunk = arena->spare;
   1894   1.1        ad 		arena->spare = NULL;
   1895   1.1        ad 
   1896   1.2        ad 		/* LINTED */
   1897   1.1        ad 		RB_INSERT(arena_chunk_tree_s, &arena->chunks, chunk);
   1898   1.1        ad 	} else {
   1899   1.1        ad 		chunk = (arena_chunk_t *)chunk_alloc(chunksize);
   1900   1.1        ad 		if (chunk == NULL)
   1901   1.1        ad 			return (NULL);
   1902   1.1        ad #ifdef MALLOC_STATS
   1903   1.1        ad 		arena->stats.mapped += chunksize;
   1904   1.1        ad #endif
   1905   1.1        ad 
   1906   1.1        ad 		chunk->arena = arena;
   1907   1.1        ad 
   1908   1.1        ad 		/*
   1909   1.1        ad 		 * Claim that no pages are in use, since the header is merely
   1910   1.1        ad 		 * overhead.
   1911   1.1        ad 		 */
   1912   1.1        ad 		chunk->pages_used = 0;
   1913   1.1        ad 
   1914   1.1        ad 		chunk->max_frun_npages = chunk_npages -
   1915   1.1        ad 		    arena_chunk_header_npages;
   1916   1.1        ad 		chunk->min_frun_ind = arena_chunk_header_npages;
   1917   1.1        ad 
   1918   1.1        ad 		/*
   1919   1.1        ad 		 * Initialize enough of the map to support one maximal free run.
   1920   1.1        ad 		 */
   1921   1.1        ad 		chunk->map[arena_chunk_header_npages].npages = chunk_npages -
   1922   1.1        ad 		    arena_chunk_header_npages;
   1923   1.1        ad 		chunk->map[arena_chunk_header_npages].pos = POS_FREE;
   1924   1.1        ad 		chunk->map[chunk_npages - 1].npages = chunk_npages -
   1925   1.1        ad 		    arena_chunk_header_npages;
   1926   1.1        ad 		chunk->map[chunk_npages - 1].pos = POS_FREE;
   1927  1.40     joerg 
   1928  1.40     joerg 		RB_INSERT(arena_chunk_tree_s, &arena->chunks, chunk);
   1929   1.1        ad 	}
   1930   1.1        ad 
   1931   1.1        ad 	return (chunk);
   1932   1.1        ad }
   1933   1.1        ad 
   1934   1.1        ad static void
   1935   1.1        ad arena_chunk_dealloc(arena_t *arena, arena_chunk_t *chunk)
   1936   1.1        ad {
   1937   1.1        ad 
   1938   1.1        ad 	/*
   1939   1.1        ad 	 * Remove chunk from the chunk tree, regardless of whether this chunk
   1940   1.1        ad 	 * will be cached, so that the arena does not use it.
   1941   1.1        ad 	 */
   1942   1.2        ad 	/* LINTED */
   1943   1.1        ad 	RB_REMOVE(arena_chunk_tree_s, &chunk->arena->chunks, chunk);
   1944   1.1        ad 
   1945   1.1        ad 	if (opt_hint == false) {
   1946   1.1        ad 		if (arena->spare != NULL) {
   1947   1.1        ad 			chunk_dealloc((void *)arena->spare, chunksize);
   1948   1.1        ad #ifdef MALLOC_STATS
   1949   1.1        ad 			arena->stats.mapped -= chunksize;
   1950   1.1        ad #endif
   1951   1.1        ad 		}
   1952   1.1        ad 		arena->spare = chunk;
   1953   1.1        ad 	} else {
   1954   1.1        ad 		assert(arena->spare == NULL);
   1955   1.1        ad 		chunk_dealloc((void *)chunk, chunksize);
   1956   1.1        ad #ifdef MALLOC_STATS
   1957   1.1        ad 		arena->stats.mapped -= chunksize;
   1958   1.1        ad #endif
   1959   1.1        ad 	}
   1960   1.1        ad }
   1961   1.1        ad 
   1962   1.1        ad static arena_run_t *
   1963   1.1        ad arena_run_alloc(arena_t *arena, size_t size)
   1964   1.1        ad {
   1965  1.40     joerg 	arena_chunk_t *chunk, *chunk_tmp;
   1966   1.1        ad 	arena_run_t *run;
   1967  1.40     joerg 	unsigned need_npages;
   1968   1.1        ad 
   1969   1.1        ad 	assert(size <= (chunksize - (arena_chunk_header_npages <<
   1970   1.1        ad 	    pagesize_2pow)));
   1971   1.1        ad 	assert((size & pagesize_mask) == 0);
   1972   1.1        ad 
   1973   1.1        ad 	/*
   1974  1.40     joerg 	 * Search through the arena chunk tree for a large enough free run.
   1975  1.40     joerg 	 * Tree order ensures that any exact fit is picked immediately or
   1976  1.40     joerg 	 * otherwise the lowest address of the next size.
   1977   1.1        ad 	 */
   1978   1.9  christos 	need_npages = (unsigned)(size >> pagesize_2pow);
   1979   1.2        ad 	/* LINTED */
   1980  1.40     joerg 	for (;;) {
   1981  1.40     joerg 		chunk_tmp = RB_ROOT(&arena->chunks);
   1982  1.40     joerg 		chunk = NULL;
   1983  1.40     joerg 		while (chunk_tmp) {
   1984  1.40     joerg 			if (chunk_tmp->max_frun_npages == need_npages) {
   1985  1.40     joerg 				chunk = chunk_tmp;
   1986  1.40     joerg 				break;
   1987  1.40     joerg 			}
   1988  1.40     joerg 			if (chunk_tmp->max_frun_npages < need_npages) {
   1989  1.40     joerg 				chunk_tmp = RB_RIGHT(chunk_tmp, link);
   1990  1.40     joerg 				continue;
   1991  1.40     joerg 			}
   1992  1.40     joerg 			chunk = chunk_tmp;
   1993  1.40     joerg 			chunk_tmp = RB_LEFT(chunk, link);
   1994  1.40     joerg 		}
   1995  1.40     joerg 		if (chunk == NULL)
   1996  1.40     joerg 			break;
   1997   1.1        ad 		/*
   1998  1.40     joerg 		 * At this point, the chunk must have a cached run size large
   1999  1.40     joerg 		 * enough to fit the allocation.
   2000   1.1        ad 		 */
   2001  1.40     joerg 		assert(need_npages <= chunk->max_frun_npages);
   2002  1.40     joerg 		{
   2003   1.1        ad 			arena_chunk_map_t *mapelm;
   2004   1.1        ad 			unsigned i;
   2005   1.1        ad 			unsigned max_frun_npages = 0;
   2006   1.1        ad 			unsigned min_frun_ind = chunk_npages;
   2007   1.1        ad 
   2008   1.1        ad 			assert(chunk->min_frun_ind >=
   2009   1.1        ad 			    arena_chunk_header_npages);
   2010   1.1        ad 			for (i = chunk->min_frun_ind; i < chunk_npages;) {
   2011   1.1        ad 				mapelm = &chunk->map[i];
   2012   1.1        ad 				if (mapelm->pos == POS_FREE) {
   2013   1.1        ad 					if (mapelm->npages >= need_npages) {
   2014   1.1        ad 						run = (arena_run_t *)
   2015   1.1        ad 						    ((uintptr_t)chunk + (i <<
   2016   1.1        ad 						    pagesize_2pow));
   2017   1.1        ad 						/* Update page map. */
   2018   1.1        ad 						arena_run_split(arena, run,
   2019   1.1        ad 						    size);
   2020   1.1        ad 						return (run);
   2021   1.1        ad 					}
   2022   1.1        ad 					if (mapelm->npages >
   2023   1.1        ad 					    max_frun_npages) {
   2024   1.1        ad 						max_frun_npages =
   2025   1.1        ad 						    mapelm->npages;
   2026   1.1        ad 					}
   2027   1.1        ad 					if (i < min_frun_ind) {
   2028   1.1        ad 						min_frun_ind = i;
   2029   1.1        ad 						if (i < chunk->min_frun_ind)
   2030   1.1        ad 							chunk->min_frun_ind = i;
   2031   1.1        ad 					}
   2032   1.1        ad 				}
   2033   1.1        ad 				i += mapelm->npages;
   2034   1.1        ad 			}
   2035   1.1        ad 			/*
   2036   1.1        ad 			 * Search failure.  Reset cached chunk->max_frun_npages.
   2037   1.1        ad 			 * chunk->min_frun_ind was already reset above (if
   2038   1.1        ad 			 * necessary).
   2039   1.1        ad 			 */
   2040  1.40     joerg 			RB_REMOVE(arena_chunk_tree_s, &arena->chunks, chunk);
   2041   1.1        ad 			chunk->max_frun_npages = max_frun_npages;
   2042  1.40     joerg 			RB_INSERT(arena_chunk_tree_s, &arena->chunks, chunk);
   2043   1.1        ad 		}
   2044   1.1        ad 	}
   2045   1.1        ad 
   2046   1.1        ad 	/*
   2047   1.1        ad 	 * No usable runs.  Create a new chunk from which to allocate the run.
   2048   1.1        ad 	 */
   2049   1.1        ad 	chunk = arena_chunk_alloc(arena);
   2050   1.1        ad 	if (chunk == NULL)
   2051   1.1        ad 		return (NULL);
   2052   1.1        ad 	run = (arena_run_t *)((uintptr_t)chunk + (arena_chunk_header_npages <<
   2053   1.1        ad 	    pagesize_2pow));
   2054   1.1        ad 	/* Update page map. */
   2055   1.1        ad 	arena_run_split(arena, run, size);
   2056   1.1        ad 	return (run);
   2057   1.1        ad }
   2058   1.1        ad 
   2059   1.1        ad static void
   2060   1.1        ad arena_run_dalloc(arena_t *arena, arena_run_t *run, size_t size)
   2061   1.1        ad {
   2062   1.1        ad 	arena_chunk_t *chunk;
   2063   1.1        ad 	unsigned run_ind, run_pages;
   2064   1.1        ad 
   2065   1.1        ad 	chunk = (arena_chunk_t *)CHUNK_ADDR2BASE(run);
   2066   1.1        ad 
   2067   1.1        ad 	run_ind = (unsigned)(((uintptr_t)run - (uintptr_t)chunk)
   2068   1.1        ad 	    >> pagesize_2pow);
   2069   1.1        ad 	assert(run_ind >= arena_chunk_header_npages);
   2070   1.1        ad 	assert(run_ind < (chunksize >> pagesize_2pow));
   2071   1.9  christos 	run_pages = (unsigned)(size >> pagesize_2pow);
   2072   1.1        ad 	assert(run_pages == chunk->map[run_ind].npages);
   2073   1.1        ad 
   2074   1.1        ad 	/* Subtract pages from count of pages used in chunk. */
   2075   1.1        ad 	chunk->pages_used -= run_pages;
   2076   1.1        ad 
   2077   1.1        ad 	/* Mark run as deallocated. */
   2078   1.1        ad 	assert(chunk->map[run_ind].npages == run_pages);
   2079   1.1        ad 	chunk->map[run_ind].pos = POS_FREE;
   2080   1.1        ad 	assert(chunk->map[run_ind + run_pages - 1].npages == run_pages);
   2081   1.1        ad 	chunk->map[run_ind + run_pages - 1].pos = POS_FREE;
   2082   1.1        ad 
   2083   1.1        ad 	/*
   2084   1.1        ad 	 * Tell the kernel that we don't need the data in this run, but only if
   2085   1.1        ad 	 * requested via runtime configuration.
   2086   1.1        ad 	 */
   2087   1.1        ad 	if (opt_hint)
   2088   1.1        ad 		madvise(run, size, MADV_FREE);
   2089   1.1        ad 
   2090   1.1        ad 	/* Try to coalesce with neighboring runs. */
   2091   1.1        ad 	if (run_ind > arena_chunk_header_npages &&
   2092   1.1        ad 	    chunk->map[run_ind - 1].pos == POS_FREE) {
   2093   1.1        ad 		unsigned prev_npages;
   2094   1.1        ad 
   2095   1.1        ad 		/* Coalesce with previous run. */
   2096   1.1        ad 		prev_npages = chunk->map[run_ind - 1].npages;
   2097   1.1        ad 		run_ind -= prev_npages;
   2098   1.1        ad 		assert(chunk->map[run_ind].npages == prev_npages);
   2099   1.1        ad 		assert(chunk->map[run_ind].pos == POS_FREE);
   2100   1.1        ad 		run_pages += prev_npages;
   2101   1.1        ad 
   2102   1.1        ad 		chunk->map[run_ind].npages = run_pages;
   2103   1.1        ad 		assert(chunk->map[run_ind].pos == POS_FREE);
   2104   1.1        ad 		chunk->map[run_ind + run_pages - 1].npages = run_pages;
   2105   1.1        ad 		assert(chunk->map[run_ind + run_pages - 1].pos == POS_FREE);
   2106   1.1        ad 	}
   2107   1.1        ad 
   2108   1.1        ad 	if (run_ind + run_pages < chunk_npages &&
   2109   1.1        ad 	    chunk->map[run_ind + run_pages].pos == POS_FREE) {
   2110   1.1        ad 		unsigned next_npages;
   2111   1.1        ad 
   2112   1.1        ad 		/* Coalesce with next run. */
   2113   1.1        ad 		next_npages = chunk->map[run_ind + run_pages].npages;
   2114   1.1        ad 		run_pages += next_npages;
   2115   1.1        ad 		assert(chunk->map[run_ind + run_pages - 1].npages ==
   2116   1.1        ad 		    next_npages);
   2117   1.1        ad 		assert(chunk->map[run_ind + run_pages - 1].pos == POS_FREE);
   2118   1.1        ad 
   2119   1.1        ad 		chunk->map[run_ind].npages = run_pages;
   2120   1.1        ad 		chunk->map[run_ind].pos = POS_FREE;
   2121   1.1        ad 		chunk->map[run_ind + run_pages - 1].npages = run_pages;
   2122   1.1        ad 		assert(chunk->map[run_ind + run_pages - 1].pos == POS_FREE);
   2123   1.1        ad 	}
   2124   1.1        ad 
   2125  1.40     joerg 	if (chunk->map[run_ind].npages > chunk->max_frun_npages) {
   2126  1.40     joerg 		RB_REMOVE(arena_chunk_tree_s, &arena->chunks, chunk);
   2127   1.1        ad 		chunk->max_frun_npages = chunk->map[run_ind].npages;
   2128  1.40     joerg 		RB_INSERT(arena_chunk_tree_s, &arena->chunks, chunk);
   2129  1.40     joerg 	}
   2130   1.1        ad 	if (run_ind < chunk->min_frun_ind)
   2131   1.1        ad 		chunk->min_frun_ind = run_ind;
   2132   1.1        ad 
   2133   1.1        ad 	/* Deallocate chunk if it is now completely unused. */
   2134   1.1        ad 	if (chunk->pages_used == 0)
   2135   1.1        ad 		arena_chunk_dealloc(arena, chunk);
   2136   1.1        ad }
   2137   1.1        ad 
   2138   1.1        ad static arena_run_t *
   2139   1.1        ad arena_bin_nonfull_run_get(arena_t *arena, arena_bin_t *bin)
   2140   1.1        ad {
   2141   1.1        ad 	arena_run_t *run;
   2142   1.1        ad 	unsigned i, remainder;
   2143   1.1        ad 
   2144   1.1        ad 	/* Look for a usable run. */
   2145   1.2        ad 	/* LINTED */
   2146   1.1        ad 	if ((run = RB_MIN(arena_run_tree_s, &bin->runs)) != NULL) {
   2147   1.1        ad 		/* run is guaranteed to have available space. */
   2148   1.2        ad 		/* LINTED */
   2149   1.1        ad 		RB_REMOVE(arena_run_tree_s, &bin->runs, run);
   2150   1.1        ad #ifdef MALLOC_STATS
   2151   1.1        ad 		bin->stats.reruns++;
   2152   1.1        ad #endif
   2153   1.1        ad 		return (run);
   2154   1.1        ad 	}
   2155   1.1        ad 	/* No existing runs have any space available. */
   2156   1.1        ad 
   2157   1.1        ad 	/* Allocate a new run. */
   2158   1.1        ad 	run = arena_run_alloc(arena, bin->run_size);
   2159   1.1        ad 	if (run == NULL)
   2160   1.1        ad 		return (NULL);
   2161   1.1        ad 
   2162   1.1        ad 	/* Initialize run internals. */
   2163   1.1        ad 	run->bin = bin;
   2164   1.1        ad 
   2165   1.1        ad 	for (i = 0; i < bin->regs_mask_nelms; i++)
   2166   1.1        ad 		run->regs_mask[i] = UINT_MAX;
   2167   1.1        ad 	remainder = bin->nregs & ((1 << (SIZEOF_INT_2POW + 3)) - 1);
   2168   1.1        ad 	if (remainder != 0) {
   2169   1.1        ad 		/* The last element has spare bits that need to be unset. */
   2170   1.1        ad 		run->regs_mask[i] = (UINT_MAX >> ((1 << (SIZEOF_INT_2POW + 3))
   2171   1.1        ad 		    - remainder));
   2172   1.1        ad 	}
   2173   1.1        ad 
   2174   1.1        ad 	run->regs_minelm = 0;
   2175   1.1        ad 
   2176   1.1        ad 	run->nfree = bin->nregs;
   2177   1.1        ad #ifdef MALLOC_DEBUG
   2178   1.1        ad 	run->magic = ARENA_RUN_MAGIC;
   2179   1.1        ad #endif
   2180   1.1        ad 
   2181   1.1        ad #ifdef MALLOC_STATS
   2182   1.1        ad 	bin->stats.nruns++;
   2183   1.1        ad 	bin->stats.curruns++;
   2184   1.1        ad 	if (bin->stats.curruns > bin->stats.highruns)
   2185   1.1        ad 		bin->stats.highruns = bin->stats.curruns;
   2186   1.1        ad #endif
   2187   1.1        ad 	return (run);
   2188   1.1        ad }
   2189   1.1        ad 
   2190   1.1        ad /* bin->runcur must have space available before this function is called. */
   2191   1.1        ad static inline void *
   2192   1.1        ad arena_bin_malloc_easy(arena_t *arena, arena_bin_t *bin, arena_run_t *run)
   2193   1.1        ad {
   2194   1.1        ad 	void *ret;
   2195   1.1        ad 
   2196   1.1        ad 	assert(run->magic == ARENA_RUN_MAGIC);
   2197   1.1        ad 	assert(run->nfree > 0);
   2198   1.1        ad 
   2199   1.1        ad 	ret = arena_run_reg_alloc(run, bin);
   2200   1.1        ad 	assert(ret != NULL);
   2201   1.1        ad 	run->nfree--;
   2202   1.1        ad 
   2203   1.1        ad 	return (ret);
   2204   1.1        ad }
   2205   1.1        ad 
   2206   1.1        ad /* Re-fill bin->runcur, then call arena_bin_malloc_easy(). */
   2207   1.1        ad static void *
   2208   1.1        ad arena_bin_malloc_hard(arena_t *arena, arena_bin_t *bin)
   2209   1.1        ad {
   2210   1.1        ad 
   2211   1.1        ad 	bin->runcur = arena_bin_nonfull_run_get(arena, bin);
   2212   1.1        ad 	if (bin->runcur == NULL)
   2213   1.1        ad 		return (NULL);
   2214   1.1        ad 	assert(bin->runcur->magic == ARENA_RUN_MAGIC);
   2215   1.1        ad 	assert(bin->runcur->nfree > 0);
   2216   1.1        ad 
   2217   1.1        ad 	return (arena_bin_malloc_easy(arena, bin, bin->runcur));
   2218   1.1        ad }
   2219   1.1        ad 
   2220   1.1        ad /*
   2221   1.1        ad  * Calculate bin->run_size such that it meets the following constraints:
   2222   1.1        ad  *
   2223   1.1        ad  *   *) bin->run_size >= min_run_size
   2224   1.1        ad  *   *) bin->run_size <= arena_maxclass
   2225   1.1        ad  *   *) bin->run_size <= RUN_MAX_SMALL
   2226   1.1        ad  *   *) run header overhead <= RUN_MAX_OVRHD (or header overhead relaxed).
   2227   1.1        ad  *
   2228   1.1        ad  * bin->nregs, bin->regs_mask_nelms, and bin->reg0_offset are
   2229   1.1        ad  * also calculated here, since these settings are all interdependent.
   2230   1.1        ad  */
   2231   1.1        ad static size_t
   2232   1.1        ad arena_bin_run_size_calc(arena_bin_t *bin, size_t min_run_size)
   2233   1.1        ad {
   2234   1.1        ad 	size_t try_run_size, good_run_size;
   2235   1.1        ad 	unsigned good_nregs, good_mask_nelms, good_reg0_offset;
   2236   1.1        ad 	unsigned try_nregs, try_mask_nelms, try_reg0_offset;
   2237   1.1        ad 
   2238   1.1        ad 	assert(min_run_size >= pagesize);
   2239   1.1        ad 	assert(min_run_size <= arena_maxclass);
   2240   1.1        ad 	assert(min_run_size <= RUN_MAX_SMALL);
   2241   1.1        ad 
   2242   1.1        ad 	/*
   2243   1.1        ad 	 * Calculate known-valid settings before entering the run_size
   2244   1.1        ad 	 * expansion loop, so that the first part of the loop always copies
   2245   1.1        ad 	 * valid settings.
   2246   1.1        ad 	 *
   2247   1.1        ad 	 * The do..while loop iteratively reduces the number of regions until
   2248   1.1        ad 	 * the run header and the regions no longer overlap.  A closed formula
   2249   1.1        ad 	 * would be quite messy, since there is an interdependency between the
   2250   1.1        ad 	 * header's mask length and the number of regions.
   2251   1.1        ad 	 */
   2252   1.1        ad 	try_run_size = min_run_size;
   2253   1.9  christos 	try_nregs = (unsigned)(((try_run_size - sizeof(arena_run_t)) /
   2254  1.22     njoly 	    bin->reg_size) + 1); /* Counter-act try_nregs-- in loop. */
   2255   1.1        ad 	do {
   2256   1.1        ad 		try_nregs--;
   2257   1.1        ad 		try_mask_nelms = (try_nregs >> (SIZEOF_INT_2POW + 3)) +
   2258   1.1        ad 		    ((try_nregs & ((1 << (SIZEOF_INT_2POW + 3)) - 1)) ? 1 : 0);
   2259   1.9  christos 		try_reg0_offset = (unsigned)(try_run_size -
   2260   1.9  christos 		    (try_nregs * bin->reg_size));
   2261   1.1        ad 	} while (sizeof(arena_run_t) + (sizeof(unsigned) * (try_mask_nelms - 1))
   2262   1.1        ad 	    > try_reg0_offset);
   2263   1.1        ad 
   2264   1.1        ad 	/* run_size expansion loop. */
   2265   1.1        ad 	do {
   2266   1.1        ad 		/*
   2267   1.1        ad 		 * Copy valid settings before trying more aggressive settings.
   2268   1.1        ad 		 */
   2269   1.1        ad 		good_run_size = try_run_size;
   2270   1.1        ad 		good_nregs = try_nregs;
   2271   1.1        ad 		good_mask_nelms = try_mask_nelms;
   2272   1.1        ad 		good_reg0_offset = try_reg0_offset;
   2273   1.1        ad 
   2274   1.1        ad 		/* Try more aggressive settings. */
   2275   1.1        ad 		try_run_size += pagesize;
   2276   1.9  christos 		try_nregs = (unsigned)(((try_run_size - sizeof(arena_run_t)) /
   2277   1.9  christos 		    bin->reg_size) + 1); /* Counter-act try_nregs-- in loop. */
   2278   1.1        ad 		do {
   2279   1.1        ad 			try_nregs--;
   2280   1.1        ad 			try_mask_nelms = (try_nregs >> (SIZEOF_INT_2POW + 3)) +
   2281   1.1        ad 			    ((try_nregs & ((1 << (SIZEOF_INT_2POW + 3)) - 1)) ?
   2282   1.1        ad 			    1 : 0);
   2283   1.9  christos 			try_reg0_offset = (unsigned)(try_run_size - (try_nregs *
   2284   1.9  christos 			    bin->reg_size));
   2285   1.1        ad 		} while (sizeof(arena_run_t) + (sizeof(unsigned) *
   2286   1.1        ad 		    (try_mask_nelms - 1)) > try_reg0_offset);
   2287   1.1        ad 	} while (try_run_size <= arena_maxclass && try_run_size <= RUN_MAX_SMALL
   2288  1.22     njoly 	    && RUN_MAX_OVRHD * (bin->reg_size << 3) > RUN_MAX_OVRHD_RELAX
   2289  1.22     njoly 	    && (try_reg0_offset << RUN_BFP) > RUN_MAX_OVRHD * try_run_size);
   2290   1.1        ad 
   2291   1.1        ad 	assert(sizeof(arena_run_t) + (sizeof(unsigned) * (good_mask_nelms - 1))
   2292   1.1        ad 	    <= good_reg0_offset);
   2293   1.1        ad 	assert((good_mask_nelms << (SIZEOF_INT_2POW + 3)) >= good_nregs);
   2294   1.1        ad 
   2295   1.1        ad 	/* Copy final settings. */
   2296   1.1        ad 	bin->run_size = good_run_size;
   2297   1.1        ad 	bin->nregs = good_nregs;
   2298   1.1        ad 	bin->regs_mask_nelms = good_mask_nelms;
   2299   1.1        ad 	bin->reg0_offset = good_reg0_offset;
   2300   1.1        ad 
   2301   1.1        ad 	return (good_run_size);
   2302   1.1        ad }
   2303   1.1        ad 
   2304   1.1        ad static void *
   2305   1.1        ad arena_malloc(arena_t *arena, size_t size)
   2306   1.1        ad {
   2307   1.1        ad 	void *ret;
   2308   1.1        ad 
   2309   1.1        ad 	assert(arena != NULL);
   2310   1.1        ad 	assert(arena->magic == ARENA_MAGIC);
   2311   1.1        ad 	assert(size != 0);
   2312   1.1        ad 	assert(QUANTUM_CEILING(size) <= arena_maxclass);
   2313   1.1        ad 
   2314   1.1        ad 	if (size <= bin_maxclass) {
   2315   1.1        ad 		arena_bin_t *bin;
   2316   1.1        ad 		arena_run_t *run;
   2317   1.1        ad 
   2318   1.1        ad 		/* Small allocation. */
   2319   1.1        ad 
   2320   1.1        ad 		if (size < small_min) {
   2321   1.1        ad 			/* Tiny. */
   2322   1.1        ad 			size = pow2_ceil(size);
   2323   1.1        ad 			bin = &arena->bins[ffs((int)(size >> (TINY_MIN_2POW +
   2324   1.1        ad 			    1)))];
   2325   1.1        ad #if (!defined(NDEBUG) || defined(MALLOC_STATS))
   2326   1.1        ad 			/*
   2327   1.1        ad 			 * Bin calculation is always correct, but we may need
   2328   1.1        ad 			 * to fix size for the purposes of assertions and/or
   2329   1.1        ad 			 * stats accuracy.
   2330   1.1        ad 			 */
   2331   1.1        ad 			if (size < (1 << TINY_MIN_2POW))
   2332   1.1        ad 				size = (1 << TINY_MIN_2POW);
   2333   1.1        ad #endif
   2334   1.1        ad 		} else if (size <= small_max) {
   2335   1.1        ad 			/* Quantum-spaced. */
   2336   1.1        ad 			size = QUANTUM_CEILING(size);
   2337   1.1        ad 			bin = &arena->bins[ntbins + (size >> opt_quantum_2pow)
   2338   1.1        ad 			    - 1];
   2339   1.1        ad 		} else {
   2340   1.1        ad 			/* Sub-page. */
   2341   1.1        ad 			size = pow2_ceil(size);
   2342   1.1        ad 			bin = &arena->bins[ntbins + nqbins
   2343   1.1        ad 			    + (ffs((int)(size >> opt_small_max_2pow)) - 2)];
   2344   1.1        ad 		}
   2345   1.1        ad 		assert(size == bin->reg_size);
   2346   1.1        ad 
   2347   1.1        ad 		malloc_mutex_lock(&arena->mtx);
   2348   1.1        ad 		if ((run = bin->runcur) != NULL && run->nfree > 0)
   2349   1.1        ad 			ret = arena_bin_malloc_easy(arena, bin, run);
   2350   1.1        ad 		else
   2351   1.1        ad 			ret = arena_bin_malloc_hard(arena, bin);
   2352   1.1        ad 
   2353   1.1        ad 		if (ret == NULL) {
   2354   1.1        ad 			malloc_mutex_unlock(&arena->mtx);
   2355   1.1        ad 			return (NULL);
   2356   1.1        ad 		}
   2357   1.1        ad 
   2358   1.1        ad #ifdef MALLOC_STATS
   2359   1.1        ad 		bin->stats.nrequests++;
   2360   1.1        ad 		arena->stats.nmalloc_small++;
   2361   1.1        ad 		arena->stats.allocated_small += size;
   2362   1.1        ad #endif
   2363   1.1        ad 	} else {
   2364   1.1        ad 		/* Large allocation. */
   2365   1.1        ad 		size = PAGE_CEILING(size);
   2366   1.1        ad 		malloc_mutex_lock(&arena->mtx);
   2367   1.1        ad 		ret = (void *)arena_run_alloc(arena, size);
   2368   1.1        ad 		if (ret == NULL) {
   2369   1.1        ad 			malloc_mutex_unlock(&arena->mtx);
   2370   1.1        ad 			return (NULL);
   2371   1.1        ad 		}
   2372   1.1        ad #ifdef MALLOC_STATS
   2373   1.1        ad 		arena->stats.nmalloc_large++;
   2374   1.1        ad 		arena->stats.allocated_large += size;
   2375   1.1        ad #endif
   2376   1.1        ad 	}
   2377   1.1        ad 
   2378   1.1        ad 	malloc_mutex_unlock(&arena->mtx);
   2379   1.1        ad 
   2380   1.1        ad 	if (opt_junk)
   2381   1.1        ad 		memset(ret, 0xa5, size);
   2382   1.1        ad 	else if (opt_zero)
   2383   1.1        ad 		memset(ret, 0, size);
   2384   1.1        ad 	return (ret);
   2385   1.1        ad }
   2386   1.1        ad 
   2387   1.1        ad static inline void
   2388   1.1        ad arena_palloc_trim(arena_t *arena, arena_chunk_t *chunk, unsigned pageind,
   2389   1.1        ad     unsigned npages)
   2390   1.1        ad {
   2391   1.1        ad 	unsigned i;
   2392   1.1        ad 
   2393   1.1        ad 	assert(npages > 0);
   2394   1.1        ad 
   2395   1.1        ad 	/*
   2396   1.1        ad 	 * Modifiy the map such that arena_run_dalloc() sees the run as
   2397   1.1        ad 	 * separately allocated.
   2398   1.1        ad 	 */
   2399   1.1        ad 	for (i = 0; i < npages; i++) {
   2400   1.1        ad 		chunk->map[pageind + i].npages = npages;
   2401   1.1        ad 		chunk->map[pageind + i].pos = i;
   2402   1.1        ad 	}
   2403   1.1        ad 	arena_run_dalloc(arena, (arena_run_t *)((uintptr_t)chunk + (pageind <<
   2404   1.1        ad 	    pagesize_2pow)), npages << pagesize_2pow);
   2405   1.1        ad }
   2406   1.1        ad 
   2407   1.1        ad /* Only handles large allocations that require more than page alignment. */
   2408   1.1        ad static void *
   2409   1.1        ad arena_palloc(arena_t *arena, size_t alignment, size_t size, size_t alloc_size)
   2410   1.1        ad {
   2411   1.1        ad 	void *ret;
   2412   1.1        ad 	size_t offset;
   2413   1.1        ad 	arena_chunk_t *chunk;
   2414   1.1        ad 	unsigned pageind, i, npages;
   2415   1.1        ad 
   2416   1.1        ad 	assert((size & pagesize_mask) == 0);
   2417   1.1        ad 	assert((alignment & pagesize_mask) == 0);
   2418   1.1        ad 
   2419   1.9  christos 	npages = (unsigned)(size >> pagesize_2pow);
   2420   1.1        ad 
   2421   1.1        ad 	malloc_mutex_lock(&arena->mtx);
   2422   1.1        ad 	ret = (void *)arena_run_alloc(arena, alloc_size);
   2423   1.1        ad 	if (ret == NULL) {
   2424   1.1        ad 		malloc_mutex_unlock(&arena->mtx);
   2425   1.1        ad 		return (NULL);
   2426   1.1        ad 	}
   2427   1.1        ad 
   2428   1.1        ad 	chunk = (arena_chunk_t *)CHUNK_ADDR2BASE(ret);
   2429   1.1        ad 
   2430   1.1        ad 	offset = (uintptr_t)ret & (alignment - 1);
   2431   1.1        ad 	assert((offset & pagesize_mask) == 0);
   2432   1.1        ad 	assert(offset < alloc_size);
   2433   1.1        ad 	if (offset == 0) {
   2434   1.9  christos 		pageind = (unsigned)(((uintptr_t)ret - (uintptr_t)chunk) >>
   2435   1.1        ad 		    pagesize_2pow);
   2436   1.1        ad 
   2437   1.1        ad 		/* Update the map for the run to be kept. */
   2438   1.1        ad 		for (i = 0; i < npages; i++) {
   2439   1.1        ad 			chunk->map[pageind + i].npages = npages;
   2440   1.1        ad 			assert(chunk->map[pageind + i].pos == i);
   2441   1.1        ad 		}
   2442   1.1        ad 
   2443   1.1        ad 		/* Trim trailing space. */
   2444   1.1        ad 		arena_palloc_trim(arena, chunk, pageind + npages,
   2445   1.9  christos 		    (unsigned)((alloc_size - size) >> pagesize_2pow));
   2446   1.1        ad 	} else {
   2447   1.1        ad 		size_t leadsize, trailsize;
   2448   1.1        ad 
   2449   1.1        ad 		leadsize = alignment - offset;
   2450   1.1        ad 		ret = (void *)((uintptr_t)ret + leadsize);
   2451   1.9  christos 		pageind = (unsigned)(((uintptr_t)ret - (uintptr_t)chunk) >>
   2452   1.1        ad 		    pagesize_2pow);
   2453   1.1        ad 
   2454   1.1        ad 		/* Update the map for the run to be kept. */
   2455   1.1        ad 		for (i = 0; i < npages; i++) {
   2456   1.1        ad 			chunk->map[pageind + i].npages = npages;
   2457   1.1        ad 			chunk->map[pageind + i].pos = i;
   2458   1.1        ad 		}
   2459   1.1        ad 
   2460   1.1        ad 		/* Trim leading space. */
   2461   1.9  christos 		arena_palloc_trim(arena, chunk,
   2462   1.9  christos 		    (unsigned)(pageind - (leadsize >> pagesize_2pow)),
   2463   1.9  christos 		    (unsigned)(leadsize >> pagesize_2pow));
   2464   1.1        ad 
   2465   1.1        ad 		trailsize = alloc_size - leadsize - size;
   2466   1.1        ad 		if (trailsize != 0) {
   2467   1.1        ad 			/* Trim trailing space. */
   2468   1.1        ad 			assert(trailsize < alloc_size);
   2469   1.1        ad 			arena_palloc_trim(arena, chunk, pageind + npages,
   2470   1.9  christos 			    (unsigned)(trailsize >> pagesize_2pow));
   2471   1.1        ad 		}
   2472   1.1        ad 	}
   2473   1.1        ad 
   2474   1.1        ad #ifdef MALLOC_STATS
   2475   1.1        ad 	arena->stats.nmalloc_large++;
   2476   1.1        ad 	arena->stats.allocated_large += size;
   2477   1.1        ad #endif
   2478   1.1        ad 	malloc_mutex_unlock(&arena->mtx);
   2479   1.1        ad 
   2480   1.1        ad 	if (opt_junk)
   2481   1.1        ad 		memset(ret, 0xa5, size);
   2482   1.1        ad 	else if (opt_zero)
   2483   1.1        ad 		memset(ret, 0, size);
   2484   1.1        ad 	return (ret);
   2485   1.1        ad }
   2486   1.1        ad 
   2487   1.1        ad /* Return the size of the allocation pointed to by ptr. */
   2488   1.1        ad static size_t
   2489   1.1        ad arena_salloc(const void *ptr)
   2490   1.1        ad {
   2491   1.1        ad 	size_t ret;
   2492   1.1        ad 	arena_chunk_t *chunk;
   2493   1.1        ad 	arena_chunk_map_t *mapelm;
   2494   1.1        ad 	unsigned pageind;
   2495   1.1        ad 
   2496   1.1        ad 	assert(ptr != NULL);
   2497   1.1        ad 	assert(CHUNK_ADDR2BASE(ptr) != ptr);
   2498   1.1        ad 
   2499   1.1        ad 	/*
   2500   1.1        ad 	 * No arena data structures that we query here can change in a way that
   2501   1.1        ad 	 * affects this function, so we don't need to lock.
   2502   1.1        ad 	 */
   2503   1.1        ad 	chunk = (arena_chunk_t *)CHUNK_ADDR2BASE(ptr);
   2504   1.9  christos 	pageind = (unsigned)(((uintptr_t)ptr - (uintptr_t)chunk) >>
   2505   1.9  christos 	    pagesize_2pow);
   2506   1.1        ad 	mapelm = &chunk->map[pageind];
   2507  1.10    simonb 	if (mapelm->pos != 0 || ptr != (char *)((uintptr_t)chunk) + (pageind <<
   2508  1.10    simonb 	    pagesize_2pow)) {
   2509  1.10    simonb 		arena_run_t *run;
   2510   1.1        ad 
   2511   1.1        ad 		pageind -= mapelm->pos;
   2512   1.1        ad 
   2513  1.10    simonb 		run = (arena_run_t *)((uintptr_t)chunk + (pageind <<
   2514  1.10    simonb 		    pagesize_2pow));
   2515   1.1        ad 		assert(run->magic == ARENA_RUN_MAGIC);
   2516   1.1        ad 		ret = run->bin->reg_size;
   2517   1.1        ad 	} else
   2518   1.1        ad 		ret = mapelm->npages << pagesize_2pow;
   2519   1.1        ad 
   2520   1.1        ad 	return (ret);
   2521   1.1        ad }
   2522   1.1        ad 
   2523   1.1        ad static void *
   2524   1.1        ad arena_ralloc(void *ptr, size_t size, size_t oldsize)
   2525   1.1        ad {
   2526   1.1        ad 	void *ret;
   2527   1.1        ad 
   2528   1.1        ad 	/* Avoid moving the allocation if the size class would not change. */
   2529   1.1        ad 	if (size < small_min) {
   2530   1.1        ad 		if (oldsize < small_min &&
   2531   1.1        ad 		    ffs((int)(pow2_ceil(size) >> (TINY_MIN_2POW + 1)))
   2532   1.1        ad 		    == ffs((int)(pow2_ceil(oldsize) >> (TINY_MIN_2POW + 1))))
   2533   1.1        ad 			goto IN_PLACE;
   2534   1.1        ad 	} else if (size <= small_max) {
   2535   1.1        ad 		if (oldsize >= small_min && oldsize <= small_max &&
   2536   1.1        ad 		    (QUANTUM_CEILING(size) >> opt_quantum_2pow)
   2537   1.1        ad 		    == (QUANTUM_CEILING(oldsize) >> opt_quantum_2pow))
   2538   1.1        ad 			goto IN_PLACE;
   2539   1.1        ad 	} else {
   2540   1.1        ad 		/*
   2541   1.1        ad 		 * We make no attempt to resize runs here, though it would be
   2542   1.1        ad 		 * possible to do so.
   2543   1.1        ad 		 */
   2544   1.1        ad 		if (oldsize > small_max && PAGE_CEILING(size) == oldsize)
   2545   1.1        ad 			goto IN_PLACE;
   2546   1.1        ad 	}
   2547   1.1        ad 
   2548   1.1        ad 	/*
   2549   1.1        ad 	 * If we get here, then size and oldsize are different enough that we
   2550   1.1        ad 	 * need to use a different size class.  In that case, fall back to
   2551   1.1        ad 	 * allocating new space and copying.
   2552   1.1        ad 	 */
   2553   1.1        ad 	ret = arena_malloc(choose_arena(), size);
   2554   1.1        ad 	if (ret == NULL)
   2555   1.1        ad 		return (NULL);
   2556   1.1        ad 
   2557   1.1        ad 	/* Junk/zero-filling were already done by arena_malloc(). */
   2558   1.1        ad 	if (size < oldsize)
   2559   1.1        ad 		memcpy(ret, ptr, size);
   2560   1.1        ad 	else
   2561   1.1        ad 		memcpy(ret, ptr, oldsize);
   2562   1.1        ad 	idalloc(ptr);
   2563   1.1        ad 	return (ret);
   2564   1.1        ad IN_PLACE:
   2565   1.1        ad 	if (opt_junk && size < oldsize)
   2566   1.1        ad 		memset((void *)((uintptr_t)ptr + size), 0x5a, oldsize - size);
   2567   1.1        ad 	else if (opt_zero && size > oldsize)
   2568   1.1        ad 		memset((void *)((uintptr_t)ptr + oldsize), 0, size - oldsize);
   2569   1.1        ad 	return (ptr);
   2570   1.1        ad }
   2571   1.1        ad 
   2572   1.1        ad static void
   2573   1.1        ad arena_dalloc(arena_t *arena, arena_chunk_t *chunk, void *ptr)
   2574   1.1        ad {
   2575   1.1        ad 	unsigned pageind;
   2576   1.1        ad 	arena_chunk_map_t *mapelm;
   2577   1.1        ad 	size_t size;
   2578   1.1        ad 
   2579   1.1        ad 	assert(arena != NULL);
   2580   1.1        ad 	assert(arena->magic == ARENA_MAGIC);
   2581   1.1        ad 	assert(chunk->arena == arena);
   2582   1.1        ad 	assert(ptr != NULL);
   2583   1.1        ad 	assert(CHUNK_ADDR2BASE(ptr) != ptr);
   2584   1.1        ad 
   2585   1.9  christos 	pageind = (unsigned)(((uintptr_t)ptr - (uintptr_t)chunk) >>
   2586   1.9  christos 	    pagesize_2pow);
   2587   1.1        ad 	mapelm = &chunk->map[pageind];
   2588  1.10    simonb 	if (mapelm->pos != 0 || ptr != (char *)((uintptr_t)chunk) + (pageind <<
   2589  1.10    simonb 	    pagesize_2pow)) {
   2590  1.10    simonb 		arena_run_t *run;
   2591   1.1        ad 		arena_bin_t *bin;
   2592   1.1        ad 
   2593   1.1        ad 		/* Small allocation. */
   2594   1.1        ad 
   2595   1.1        ad 		pageind -= mapelm->pos;
   2596   1.1        ad 
   2597  1.10    simonb 		run = (arena_run_t *)((uintptr_t)chunk + (pageind <<
   2598  1.10    simonb 		    pagesize_2pow));
   2599   1.1        ad 		assert(run->magic == ARENA_RUN_MAGIC);
   2600   1.1        ad 		bin = run->bin;
   2601   1.1        ad 		size = bin->reg_size;
   2602   1.1        ad 
   2603   1.1        ad 		if (opt_junk)
   2604   1.1        ad 			memset(ptr, 0x5a, size);
   2605   1.1        ad 
   2606   1.1        ad 		malloc_mutex_lock(&arena->mtx);
   2607   1.1        ad 		arena_run_reg_dalloc(run, bin, ptr, size);
   2608   1.1        ad 		run->nfree++;
   2609   1.1        ad 
   2610   1.1        ad 		if (run->nfree == bin->nregs) {
   2611   1.1        ad 			/* Deallocate run. */
   2612   1.1        ad 			if (run == bin->runcur)
   2613   1.1        ad 				bin->runcur = NULL;
   2614   1.1        ad 			else if (bin->nregs != 1) {
   2615   1.1        ad 				/*
   2616   1.1        ad 				 * This block's conditional is necessary because
   2617   1.1        ad 				 * if the run only contains one region, then it
   2618   1.1        ad 				 * never gets inserted into the non-full runs
   2619   1.1        ad 				 * tree.
   2620   1.1        ad 				 */
   2621   1.2        ad 				/* LINTED */
   2622   1.1        ad 				RB_REMOVE(arena_run_tree_s, &bin->runs, run);
   2623   1.1        ad 			}
   2624   1.1        ad #ifdef MALLOC_DEBUG
   2625   1.1        ad 			run->magic = 0;
   2626   1.1        ad #endif
   2627   1.1        ad 			arena_run_dalloc(arena, run, bin->run_size);
   2628   1.1        ad #ifdef MALLOC_STATS
   2629   1.1        ad 			bin->stats.curruns--;
   2630   1.1        ad #endif
   2631   1.1        ad 		} else if (run->nfree == 1 && run != bin->runcur) {
   2632   1.1        ad 			/*
   2633   1.1        ad 			 * Make sure that bin->runcur always refers to the
   2634   1.1        ad 			 * lowest non-full run, if one exists.
   2635   1.1        ad 			 */
   2636   1.1        ad 			if (bin->runcur == NULL)
   2637   1.1        ad 				bin->runcur = run;
   2638   1.1        ad 			else if ((uintptr_t)run < (uintptr_t)bin->runcur) {
   2639   1.1        ad 				/* Switch runcur. */
   2640   1.1        ad 				if (bin->runcur->nfree > 0) {
   2641   1.1        ad 					/* Insert runcur. */
   2642   1.2        ad 					/* LINTED */
   2643   1.1        ad 					RB_INSERT(arena_run_tree_s, &bin->runs,
   2644   1.1        ad 					    bin->runcur);
   2645   1.1        ad 				}
   2646   1.1        ad 				bin->runcur = run;
   2647   1.2        ad 			} else {
   2648   1.2        ad 				/* LINTED */
   2649   1.1        ad 				RB_INSERT(arena_run_tree_s, &bin->runs, run);
   2650   1.2        ad 			}
   2651   1.1        ad 		}
   2652   1.1        ad #ifdef MALLOC_STATS
   2653   1.1        ad 		arena->stats.allocated_small -= size;
   2654   1.1        ad 		arena->stats.ndalloc_small++;
   2655   1.1        ad #endif
   2656   1.1        ad 	} else {
   2657   1.1        ad 		/* Large allocation. */
   2658   1.1        ad 
   2659   1.1        ad 		size = mapelm->npages << pagesize_2pow;
   2660   1.1        ad 		assert((((uintptr_t)ptr) & pagesize_mask) == 0);
   2661   1.1        ad 
   2662   1.1        ad 		if (opt_junk)
   2663   1.1        ad 			memset(ptr, 0x5a, size);
   2664   1.1        ad 
   2665   1.1        ad 		malloc_mutex_lock(&arena->mtx);
   2666   1.1        ad 		arena_run_dalloc(arena, (arena_run_t *)ptr, size);
   2667   1.1        ad #ifdef MALLOC_STATS
   2668   1.1        ad 		arena->stats.allocated_large -= size;
   2669   1.1        ad 		arena->stats.ndalloc_large++;
   2670   1.1        ad #endif
   2671   1.1        ad 	}
   2672   1.1        ad 
   2673   1.1        ad 	malloc_mutex_unlock(&arena->mtx);
   2674   1.1        ad }
   2675   1.1        ad 
   2676   1.1        ad static bool
   2677   1.1        ad arena_new(arena_t *arena)
   2678   1.1        ad {
   2679   1.1        ad 	unsigned i;
   2680   1.1        ad 	arena_bin_t *bin;
   2681   1.2        ad 	size_t prev_run_size;
   2682   1.1        ad 
   2683   1.1        ad 	malloc_mutex_init(&arena->mtx);
   2684   1.1        ad 
   2685   1.1        ad #ifdef MALLOC_STATS
   2686   1.1        ad 	memset(&arena->stats, 0, sizeof(arena_stats_t));
   2687   1.1        ad #endif
   2688   1.1        ad 
   2689   1.1        ad 	/* Initialize chunks. */
   2690   1.1        ad 	RB_INIT(&arena->chunks);
   2691   1.1        ad 	arena->spare = NULL;
   2692   1.1        ad 
   2693   1.1        ad 	/* Initialize bins. */
   2694   1.1        ad 	prev_run_size = pagesize;
   2695   1.1        ad 
   2696   1.1        ad 	/* (2^n)-spaced tiny bins. */
   2697   1.1        ad 	for (i = 0; i < ntbins; i++) {
   2698   1.1        ad 		bin = &arena->bins[i];
   2699   1.1        ad 		bin->runcur = NULL;
   2700   1.1        ad 		RB_INIT(&bin->runs);
   2701   1.1        ad 
   2702   1.1        ad 		bin->reg_size = (1 << (TINY_MIN_2POW + i));
   2703   1.1        ad 		prev_run_size = arena_bin_run_size_calc(bin, prev_run_size);
   2704   1.1        ad 
   2705   1.1        ad #ifdef MALLOC_STATS
   2706   1.1        ad 		memset(&bin->stats, 0, sizeof(malloc_bin_stats_t));
   2707   1.1        ad #endif
   2708   1.1        ad 	}
   2709   1.1        ad 
   2710   1.1        ad 	/* Quantum-spaced bins. */
   2711   1.1        ad 	for (; i < ntbins + nqbins; i++) {
   2712   1.1        ad 		bin = &arena->bins[i];
   2713   1.1        ad 		bin->runcur = NULL;
   2714   1.1        ad 		RB_INIT(&bin->runs);
   2715   1.1        ad 
   2716   1.1        ad 		bin->reg_size = quantum * (i - ntbins + 1);
   2717   1.2        ad /*
   2718   1.1        ad 		pow2_size = pow2_ceil(quantum * (i - ntbins + 1));
   2719   1.2        ad */
   2720   1.1        ad 		prev_run_size = arena_bin_run_size_calc(bin, prev_run_size);
   2721   1.1        ad 
   2722   1.1        ad #ifdef MALLOC_STATS
   2723   1.1        ad 		memset(&bin->stats, 0, sizeof(malloc_bin_stats_t));
   2724   1.1        ad #endif
   2725   1.1        ad 	}
   2726   1.1        ad 
   2727   1.1        ad 	/* (2^n)-spaced sub-page bins. */
   2728   1.1        ad 	for (; i < ntbins + nqbins + nsbins; i++) {
   2729   1.1        ad 		bin = &arena->bins[i];
   2730   1.1        ad 		bin->runcur = NULL;
   2731   1.1        ad 		RB_INIT(&bin->runs);
   2732   1.1        ad 
   2733   1.1        ad 		bin->reg_size = (small_max << (i - (ntbins + nqbins) + 1));
   2734   1.1        ad 
   2735   1.1        ad 		prev_run_size = arena_bin_run_size_calc(bin, prev_run_size);
   2736   1.1        ad 
   2737   1.1        ad #ifdef MALLOC_STATS
   2738   1.1        ad 		memset(&bin->stats, 0, sizeof(malloc_bin_stats_t));
   2739   1.1        ad #endif
   2740   1.1        ad 	}
   2741   1.1        ad 
   2742   1.1        ad #ifdef MALLOC_DEBUG
   2743   1.1        ad 	arena->magic = ARENA_MAGIC;
   2744   1.1        ad #endif
   2745   1.1        ad 
   2746   1.1        ad 	return (false);
   2747   1.1        ad }
   2748   1.1        ad 
   2749   1.1        ad /* Create a new arena and insert it into the arenas array at index ind. */
   2750   1.1        ad static arena_t *
   2751   1.1        ad arenas_extend(unsigned ind)
   2752   1.1        ad {
   2753   1.1        ad 	arena_t *ret;
   2754   1.1        ad 
   2755   1.1        ad 	/* Allocate enough space for trailing bins. */
   2756   1.1        ad 	ret = (arena_t *)base_alloc(sizeof(arena_t)
   2757   1.1        ad 	    + (sizeof(arena_bin_t) * (ntbins + nqbins + nsbins - 1)));
   2758   1.1        ad 	if (ret != NULL && arena_new(ret) == false) {
   2759   1.1        ad 		arenas[ind] = ret;
   2760   1.1        ad 		return (ret);
   2761   1.1        ad 	}
   2762   1.1        ad 	/* Only reached if there is an OOM error. */
   2763   1.1        ad 
   2764   1.1        ad 	/*
   2765   1.1        ad 	 * OOM here is quite inconvenient to propagate, since dealing with it
   2766   1.1        ad 	 * would require a check for failure in the fast path.  Instead, punt
   2767   1.1        ad 	 * by using arenas[0].  In practice, this is an extremely unlikely
   2768   1.1        ad 	 * failure.
   2769   1.1        ad 	 */
   2770  1.16  christos 	_malloc_message(getprogname(),
   2771   1.1        ad 	    ": (malloc) Error initializing arena\n", "", "");
   2772   1.1        ad 	if (opt_abort)
   2773   1.1        ad 		abort();
   2774   1.1        ad 
   2775   1.1        ad 	return (arenas[0]);
   2776   1.1        ad }
   2777   1.1        ad 
   2778   1.1        ad /*
   2779   1.1        ad  * End arena.
   2780   1.1        ad  */
   2781   1.1        ad /******************************************************************************/
   2782   1.1        ad /*
   2783   1.1        ad  * Begin general internal functions.
   2784   1.1        ad  */
   2785   1.1        ad 
   2786   1.1        ad static void *
   2787   1.1        ad huge_malloc(size_t size)
   2788   1.1        ad {
   2789   1.1        ad 	void *ret;
   2790   1.1        ad 	size_t csize;
   2791   1.1        ad 	chunk_node_t *node;
   2792   1.1        ad 
   2793   1.1        ad 	/* Allocate one or more contiguous chunks for this request. */
   2794   1.1        ad 
   2795   1.1        ad 	csize = CHUNK_CEILING(size);
   2796   1.1        ad 	if (csize == 0) {
   2797   1.1        ad 		/* size is large enough to cause size_t wrap-around. */
   2798   1.1        ad 		return (NULL);
   2799   1.1        ad 	}
   2800   1.1        ad 
   2801   1.1        ad 	/* Allocate a chunk node with which to track the chunk. */
   2802   1.1        ad 	node = base_chunk_node_alloc();
   2803   1.1        ad 	if (node == NULL)
   2804   1.1        ad 		return (NULL);
   2805   1.1        ad 
   2806   1.1        ad 	ret = chunk_alloc(csize);
   2807   1.1        ad 	if (ret == NULL) {
   2808   1.1        ad 		base_chunk_node_dealloc(node);
   2809   1.1        ad 		return (NULL);
   2810   1.1        ad 	}
   2811   1.1        ad 
   2812   1.1        ad 	/* Insert node into huge. */
   2813   1.1        ad 	node->chunk = ret;
   2814   1.1        ad 	node->size = csize;
   2815   1.1        ad 
   2816   1.1        ad 	malloc_mutex_lock(&chunks_mtx);
   2817   1.1        ad 	RB_INSERT(chunk_tree_s, &huge, node);
   2818   1.1        ad #ifdef MALLOC_STATS
   2819   1.1        ad 	huge_nmalloc++;
   2820   1.1        ad 	huge_allocated += csize;
   2821   1.1        ad #endif
   2822   1.1        ad 	malloc_mutex_unlock(&chunks_mtx);
   2823   1.1        ad 
   2824   1.1        ad 	if (opt_junk)
   2825   1.1        ad 		memset(ret, 0xa5, csize);
   2826   1.1        ad 	else if (opt_zero)
   2827   1.1        ad 		memset(ret, 0, csize);
   2828   1.1        ad 
   2829   1.1        ad 	return (ret);
   2830   1.1        ad }
   2831   1.1        ad 
   2832   1.1        ad /* Only handles large allocations that require more than chunk alignment. */
   2833   1.1        ad static void *
   2834   1.1        ad huge_palloc(size_t alignment, size_t size)
   2835   1.1        ad {
   2836   1.1        ad 	void *ret;
   2837   1.1        ad 	size_t alloc_size, chunk_size, offset;
   2838   1.1        ad 	chunk_node_t *node;
   2839   1.1        ad 
   2840   1.1        ad 	/*
   2841   1.1        ad 	 * This allocation requires alignment that is even larger than chunk
   2842   1.1        ad 	 * alignment.  This means that huge_malloc() isn't good enough.
   2843   1.1        ad 	 *
   2844   1.1        ad 	 * Allocate almost twice as many chunks as are demanded by the size or
   2845   1.1        ad 	 * alignment, in order to assure the alignment can be achieved, then
   2846   1.1        ad 	 * unmap leading and trailing chunks.
   2847   1.1        ad 	 */
   2848   1.1        ad 	assert(alignment >= chunksize);
   2849   1.1        ad 
   2850   1.1        ad 	chunk_size = CHUNK_CEILING(size);
   2851   1.1        ad 
   2852   1.1        ad 	if (size >= alignment)
   2853   1.1        ad 		alloc_size = chunk_size + alignment - chunksize;
   2854   1.1        ad 	else
   2855   1.1        ad 		alloc_size = (alignment << 1) - chunksize;
   2856   1.1        ad 
   2857   1.1        ad 	/* Allocate a chunk node with which to track the chunk. */
   2858   1.1        ad 	node = base_chunk_node_alloc();
   2859   1.1        ad 	if (node == NULL)
   2860   1.1        ad 		return (NULL);
   2861   1.1        ad 
   2862   1.1        ad 	ret = chunk_alloc(alloc_size);
   2863   1.1        ad 	if (ret == NULL) {
   2864   1.1        ad 		base_chunk_node_dealloc(node);
   2865   1.1        ad 		return (NULL);
   2866   1.1        ad 	}
   2867   1.1        ad 
   2868   1.1        ad 	offset = (uintptr_t)ret & (alignment - 1);
   2869   1.1        ad 	assert((offset & chunksize_mask) == 0);
   2870   1.1        ad 	assert(offset < alloc_size);
   2871   1.1        ad 	if (offset == 0) {
   2872   1.1        ad 		/* Trim trailing space. */
   2873   1.1        ad 		chunk_dealloc((void *)((uintptr_t)ret + chunk_size), alloc_size
   2874   1.1        ad 		    - chunk_size);
   2875   1.1        ad 	} else {
   2876   1.1        ad 		size_t trailsize;
   2877   1.1        ad 
   2878   1.1        ad 		/* Trim leading space. */
   2879   1.1        ad 		chunk_dealloc(ret, alignment - offset);
   2880   1.1        ad 
   2881   1.1        ad 		ret = (void *)((uintptr_t)ret + (alignment - offset));
   2882   1.1        ad 
   2883   1.1        ad 		trailsize = alloc_size - (alignment - offset) - chunk_size;
   2884   1.1        ad 		if (trailsize != 0) {
   2885   1.1        ad 		    /* Trim trailing space. */
   2886   1.1        ad 		    assert(trailsize < alloc_size);
   2887   1.1        ad 		    chunk_dealloc((void *)((uintptr_t)ret + chunk_size),
   2888   1.1        ad 			trailsize);
   2889   1.1        ad 		}
   2890   1.1        ad 	}
   2891   1.1        ad 
   2892   1.1        ad 	/* Insert node into huge. */
   2893   1.1        ad 	node->chunk = ret;
   2894   1.1        ad 	node->size = chunk_size;
   2895   1.1        ad 
   2896   1.1        ad 	malloc_mutex_lock(&chunks_mtx);
   2897   1.1        ad 	RB_INSERT(chunk_tree_s, &huge, node);
   2898   1.1        ad #ifdef MALLOC_STATS
   2899   1.1        ad 	huge_nmalloc++;
   2900   1.1        ad 	huge_allocated += chunk_size;
   2901   1.1        ad #endif
   2902   1.1        ad 	malloc_mutex_unlock(&chunks_mtx);
   2903   1.1        ad 
   2904   1.1        ad 	if (opt_junk)
   2905   1.1        ad 		memset(ret, 0xa5, chunk_size);
   2906   1.1        ad 	else if (opt_zero)
   2907   1.1        ad 		memset(ret, 0, chunk_size);
   2908   1.1        ad 
   2909   1.1        ad 	return (ret);
   2910   1.1        ad }
   2911   1.1        ad 
   2912   1.1        ad static void *
   2913   1.1        ad huge_ralloc(void *ptr, size_t size, size_t oldsize)
   2914   1.1        ad {
   2915   1.1        ad 	void *ret;
   2916   1.1        ad 
   2917   1.1        ad 	/* Avoid moving the allocation if the size class would not change. */
   2918   1.1        ad 	if (oldsize > arena_maxclass &&
   2919   1.1        ad 	    CHUNK_CEILING(size) == CHUNK_CEILING(oldsize)) {
   2920   1.1        ad 		if (opt_junk && size < oldsize) {
   2921   1.1        ad 			memset((void *)((uintptr_t)ptr + size), 0x5a, oldsize
   2922   1.1        ad 			    - size);
   2923   1.1        ad 		} else if (opt_zero && size > oldsize) {
   2924   1.1        ad 			memset((void *)((uintptr_t)ptr + oldsize), 0, size
   2925   1.1        ad 			    - oldsize);
   2926   1.1        ad 		}
   2927   1.1        ad 		return (ptr);
   2928   1.1        ad 	}
   2929   1.1        ad 
   2930   1.8      yamt 	if (CHUNK_ADDR2BASE(ptr) == ptr
   2931   1.8      yamt #ifdef USE_BRK
   2932   1.8      yamt 	    && ((uintptr_t)ptr < (uintptr_t)brk_base
   2933   1.8      yamt 	    || (uintptr_t)ptr >= (uintptr_t)brk_max)
   2934   1.8      yamt #endif
   2935   1.8      yamt 	    ) {
   2936   1.8      yamt 		chunk_node_t *node, key;
   2937   1.8      yamt 		void *newptr;
   2938   1.8      yamt 		size_t oldcsize;
   2939   1.8      yamt 		size_t newcsize;
   2940   1.8      yamt 
   2941   1.8      yamt 		newcsize = CHUNK_CEILING(size);
   2942   1.8      yamt 		oldcsize = CHUNK_CEILING(oldsize);
   2943   1.8      yamt 		assert(oldcsize != newcsize);
   2944   1.8      yamt 		if (newcsize == 0) {
   2945   1.8      yamt 			/* size_t wrap-around */
   2946   1.8      yamt 			return (NULL);
   2947   1.8      yamt 		}
   2948  1.21     enami 
   2949  1.21     enami 		/*
   2950  1.21     enami 		 * Remove the old region from the tree now.  If mremap()
   2951  1.21     enami 		 * returns the region to the system, other thread may
   2952  1.21     enami 		 * map it for same huge allocation and insert it to the
   2953  1.21     enami 		 * tree before we acquire the mutex lock again.
   2954  1.21     enami 		 */
   2955  1.21     enami 		malloc_mutex_lock(&chunks_mtx);
   2956  1.21     enami 		key.chunk = __DECONST(void *, ptr);
   2957  1.21     enami 		/* LINTED */
   2958  1.21     enami 		node = RB_FIND(chunk_tree_s, &huge, &key);
   2959  1.21     enami 		assert(node != NULL);
   2960  1.21     enami 		assert(node->chunk == ptr);
   2961  1.21     enami 		assert(node->size == oldcsize);
   2962  1.21     enami 		RB_REMOVE(chunk_tree_s, &huge, node);
   2963  1.21     enami 		malloc_mutex_unlock(&chunks_mtx);
   2964  1.21     enami 
   2965   1.8      yamt 		newptr = mremap(ptr, oldcsize, NULL, newcsize,
   2966   1.8      yamt 		    MAP_ALIGNED(chunksize_2pow));
   2967  1.21     enami 		if (newptr == MAP_FAILED) {
   2968  1.21     enami 			/* We still own the old region. */
   2969  1.21     enami 			malloc_mutex_lock(&chunks_mtx);
   2970  1.21     enami 			RB_INSERT(chunk_tree_s, &huge, node);
   2971  1.21     enami 			malloc_mutex_unlock(&chunks_mtx);
   2972  1.21     enami 		} else {
   2973   1.8      yamt 			assert(CHUNK_ADDR2BASE(newptr) == newptr);
   2974   1.8      yamt 
   2975  1.21     enami 			/* Insert new or resized old region. */
   2976   1.8      yamt 			malloc_mutex_lock(&chunks_mtx);
   2977   1.8      yamt 			node->size = newcsize;
   2978  1.21     enami 			node->chunk = newptr;
   2979  1.21     enami 			RB_INSERT(chunk_tree_s, &huge, node);
   2980   1.8      yamt #ifdef MALLOC_STATS
   2981   1.8      yamt 			huge_nralloc++;
   2982   1.8      yamt 			huge_allocated += newcsize - oldcsize;
   2983   1.8      yamt 			if (newcsize > oldcsize) {
   2984   1.8      yamt 				stats_chunks.curchunks +=
   2985   1.8      yamt 				    (newcsize - oldcsize) / chunksize;
   2986   1.8      yamt 				if (stats_chunks.curchunks >
   2987   1.8      yamt 				    stats_chunks.highchunks)
   2988   1.8      yamt 					stats_chunks.highchunks =
   2989   1.8      yamt 					    stats_chunks.curchunks;
   2990   1.8      yamt 			} else {
   2991   1.8      yamt 				stats_chunks.curchunks -=
   2992   1.8      yamt 				    (oldcsize - newcsize) / chunksize;
   2993   1.8      yamt 			}
   2994   1.8      yamt #endif
   2995   1.8      yamt 			malloc_mutex_unlock(&chunks_mtx);
   2996   1.8      yamt 
   2997   1.8      yamt 			if (opt_junk && size < oldsize) {
   2998   1.8      yamt 				memset((void *)((uintptr_t)newptr + size), 0x5a,
   2999   1.8      yamt 				    newcsize - size);
   3000   1.8      yamt 			} else if (opt_zero && size > oldsize) {
   3001   1.8      yamt 				memset((void *)((uintptr_t)newptr + oldsize), 0,
   3002   1.8      yamt 				    size - oldsize);
   3003   1.8      yamt 			}
   3004   1.8      yamt 			return (newptr);
   3005   1.8      yamt 		}
   3006   1.8      yamt 	}
   3007   1.8      yamt 
   3008   1.1        ad 	/*
   3009   1.1        ad 	 * If we get here, then size and oldsize are different enough that we
   3010   1.1        ad 	 * need to use a different size class.  In that case, fall back to
   3011   1.1        ad 	 * allocating new space and copying.
   3012   1.1        ad 	 */
   3013   1.1        ad 	ret = huge_malloc(size);
   3014   1.1        ad 	if (ret == NULL)
   3015   1.1        ad 		return (NULL);
   3016   1.1        ad 
   3017   1.1        ad 	if (CHUNK_ADDR2BASE(ptr) == ptr) {
   3018   1.1        ad 		/* The old allocation is a chunk. */
   3019   1.1        ad 		if (size < oldsize)
   3020   1.1        ad 			memcpy(ret, ptr, size);
   3021   1.1        ad 		else
   3022   1.1        ad 			memcpy(ret, ptr, oldsize);
   3023   1.1        ad 	} else {
   3024   1.1        ad 		/* The old allocation is a region. */
   3025   1.1        ad 		assert(oldsize < size);
   3026   1.1        ad 		memcpy(ret, ptr, oldsize);
   3027   1.1        ad 	}
   3028   1.1        ad 	idalloc(ptr);
   3029   1.1        ad 	return (ret);
   3030   1.1        ad }
   3031   1.1        ad 
   3032   1.1        ad static void
   3033   1.1        ad huge_dalloc(void *ptr)
   3034   1.1        ad {
   3035   1.1        ad 	chunk_node_t key;
   3036   1.1        ad 	chunk_node_t *node;
   3037   1.1        ad 
   3038   1.1        ad 	malloc_mutex_lock(&chunks_mtx);
   3039   1.1        ad 
   3040   1.1        ad 	/* Extract from tree of huge allocations. */
   3041   1.1        ad 	key.chunk = ptr;
   3042   1.2        ad 	/* LINTED */
   3043   1.1        ad 	node = RB_FIND(chunk_tree_s, &huge, &key);
   3044   1.1        ad 	assert(node != NULL);
   3045   1.1        ad 	assert(node->chunk == ptr);
   3046   1.2        ad 	/* LINTED */
   3047   1.1        ad 	RB_REMOVE(chunk_tree_s, &huge, node);
   3048   1.1        ad 
   3049   1.1        ad #ifdef MALLOC_STATS
   3050   1.1        ad 	huge_ndalloc++;
   3051   1.1        ad 	huge_allocated -= node->size;
   3052   1.1        ad #endif
   3053   1.1        ad 
   3054   1.1        ad 	malloc_mutex_unlock(&chunks_mtx);
   3055   1.1        ad 
   3056   1.1        ad 	/* Unmap chunk. */
   3057   1.1        ad #ifdef USE_BRK
   3058   1.1        ad 	if (opt_junk)
   3059   1.1        ad 		memset(node->chunk, 0x5a, node->size);
   3060   1.1        ad #endif
   3061   1.1        ad 	chunk_dealloc(node->chunk, node->size);
   3062   1.1        ad 
   3063   1.1        ad 	base_chunk_node_dealloc(node);
   3064   1.1        ad }
   3065   1.1        ad 
   3066   1.1        ad static void *
   3067   1.1        ad imalloc(size_t size)
   3068   1.1        ad {
   3069   1.1        ad 	void *ret;
   3070   1.1        ad 
   3071   1.1        ad 	assert(size != 0);
   3072   1.1        ad 
   3073   1.1        ad 	if (size <= arena_maxclass)
   3074   1.1        ad 		ret = arena_malloc(choose_arena(), size);
   3075   1.1        ad 	else
   3076   1.1        ad 		ret = huge_malloc(size);
   3077   1.1        ad 
   3078   1.1        ad 	return (ret);
   3079   1.1        ad }
   3080   1.1        ad 
   3081   1.1        ad static void *
   3082   1.1        ad ipalloc(size_t alignment, size_t size)
   3083   1.1        ad {
   3084   1.1        ad 	void *ret;
   3085   1.1        ad 	size_t ceil_size;
   3086   1.1        ad 
   3087   1.1        ad 	/*
   3088   1.1        ad 	 * Round size up to the nearest multiple of alignment.
   3089   1.1        ad 	 *
   3090   1.1        ad 	 * This done, we can take advantage of the fact that for each small
   3091   1.1        ad 	 * size class, every object is aligned at the smallest power of two
   3092   1.1        ad 	 * that is non-zero in the base two representation of the size.  For
   3093   1.1        ad 	 * example:
   3094   1.1        ad 	 *
   3095   1.1        ad 	 *   Size |   Base 2 | Minimum alignment
   3096   1.1        ad 	 *   -----+----------+------------------
   3097   1.1        ad 	 *     96 |  1100000 |  32
   3098   1.1        ad 	 *    144 | 10100000 |  32
   3099   1.1        ad 	 *    192 | 11000000 |  64
   3100   1.1        ad 	 *
   3101   1.1        ad 	 * Depending on runtime settings, it is possible that arena_malloc()
   3102   1.1        ad 	 * will further round up to a power of two, but that never causes
   3103   1.1        ad 	 * correctness issues.
   3104   1.1        ad 	 */
   3105   1.1        ad 	ceil_size = (size + (alignment - 1)) & (-alignment);
   3106   1.1        ad 	/*
   3107   1.1        ad 	 * (ceil_size < size) protects against the combination of maximal
   3108   1.1        ad 	 * alignment and size greater than maximal alignment.
   3109   1.1        ad 	 */
   3110   1.1        ad 	if (ceil_size < size) {
   3111   1.1        ad 		/* size_t overflow. */
   3112   1.1        ad 		return (NULL);
   3113   1.1        ad 	}
   3114   1.1        ad 
   3115   1.1        ad 	if (ceil_size <= pagesize || (alignment <= pagesize
   3116   1.1        ad 	    && ceil_size <= arena_maxclass))
   3117   1.1        ad 		ret = arena_malloc(choose_arena(), ceil_size);
   3118   1.1        ad 	else {
   3119   1.1        ad 		size_t run_size;
   3120   1.1        ad 
   3121   1.1        ad 		/*
   3122   1.1        ad 		 * We can't achieve sub-page alignment, so round up alignment
   3123   1.1        ad 		 * permanently; it makes later calculations simpler.
   3124   1.1        ad 		 */
   3125   1.1        ad 		alignment = PAGE_CEILING(alignment);
   3126   1.1        ad 		ceil_size = PAGE_CEILING(size);
   3127   1.1        ad 		/*
   3128   1.1        ad 		 * (ceil_size < size) protects against very large sizes within
   3129   1.1        ad 		 * pagesize of SIZE_T_MAX.
   3130   1.1        ad 		 *
   3131   1.1        ad 		 * (ceil_size + alignment < ceil_size) protects against the
   3132   1.1        ad 		 * combination of maximal alignment and ceil_size large enough
   3133   1.1        ad 		 * to cause overflow.  This is similar to the first overflow
   3134   1.1        ad 		 * check above, but it needs to be repeated due to the new
   3135   1.1        ad 		 * ceil_size value, which may now be *equal* to maximal
   3136   1.1        ad 		 * alignment, whereas before we only detected overflow if the
   3137   1.1        ad 		 * original size was *greater* than maximal alignment.
   3138   1.1        ad 		 */
   3139   1.1        ad 		if (ceil_size < size || ceil_size + alignment < ceil_size) {
   3140   1.1        ad 			/* size_t overflow. */
   3141   1.1        ad 			return (NULL);
   3142   1.1        ad 		}
   3143   1.1        ad 
   3144   1.1        ad 		/*
   3145   1.1        ad 		 * Calculate the size of the over-size run that arena_palloc()
   3146   1.1        ad 		 * would need to allocate in order to guarantee the alignment.
   3147   1.1        ad 		 */
   3148   1.1        ad 		if (ceil_size >= alignment)
   3149   1.1        ad 			run_size = ceil_size + alignment - pagesize;
   3150   1.1        ad 		else {
   3151   1.1        ad 			/*
   3152   1.1        ad 			 * It is possible that (alignment << 1) will cause
   3153   1.1        ad 			 * overflow, but it doesn't matter because we also
   3154   1.1        ad 			 * subtract pagesize, which in the case of overflow
   3155   1.1        ad 			 * leaves us with a very large run_size.  That causes
   3156   1.1        ad 			 * the first conditional below to fail, which means
   3157   1.1        ad 			 * that the bogus run_size value never gets used for
   3158   1.1        ad 			 * anything important.
   3159   1.1        ad 			 */
   3160   1.1        ad 			run_size = (alignment << 1) - pagesize;
   3161   1.1        ad 		}
   3162   1.1        ad 
   3163   1.1        ad 		if (run_size <= arena_maxclass) {
   3164   1.1        ad 			ret = arena_palloc(choose_arena(), alignment, ceil_size,
   3165   1.1        ad 			    run_size);
   3166   1.1        ad 		} else if (alignment <= chunksize)
   3167   1.1        ad 			ret = huge_malloc(ceil_size);
   3168   1.1        ad 		else
   3169   1.1        ad 			ret = huge_palloc(alignment, ceil_size);
   3170   1.1        ad 	}
   3171   1.1        ad 
   3172   1.1        ad 	assert(((uintptr_t)ret & (alignment - 1)) == 0);
   3173   1.1        ad 	return (ret);
   3174   1.1        ad }
   3175   1.1        ad 
   3176   1.1        ad static void *
   3177   1.1        ad icalloc(size_t size)
   3178   1.1        ad {
   3179   1.1        ad 	void *ret;
   3180   1.1        ad 
   3181   1.1        ad 	if (size <= arena_maxclass) {
   3182   1.1        ad 		ret = arena_malloc(choose_arena(), size);
   3183   1.1        ad 		if (ret == NULL)
   3184   1.1        ad 			return (NULL);
   3185   1.1        ad 		memset(ret, 0, size);
   3186   1.1        ad 	} else {
   3187   1.1        ad 		/*
   3188   1.1        ad 		 * The virtual memory system provides zero-filled pages, so
   3189   1.1        ad 		 * there is no need to do so manually, unless opt_junk is
   3190   1.1        ad 		 * enabled, in which case huge_malloc() fills huge allocations
   3191   1.1        ad 		 * with junk.
   3192   1.1        ad 		 */
   3193   1.1        ad 		ret = huge_malloc(size);
   3194   1.1        ad 		if (ret == NULL)
   3195   1.1        ad 			return (NULL);
   3196   1.1        ad 
   3197   1.1        ad 		if (opt_junk)
   3198   1.1        ad 			memset(ret, 0, size);
   3199   1.1        ad #ifdef USE_BRK
   3200   1.1        ad 		else if ((uintptr_t)ret >= (uintptr_t)brk_base
   3201   1.1        ad 		    && (uintptr_t)ret < (uintptr_t)brk_max) {
   3202   1.1        ad 			/*
   3203   1.1        ad 			 * This may be a re-used brk chunk.  Therefore, zero
   3204   1.1        ad 			 * the memory.
   3205   1.1        ad 			 */
   3206   1.1        ad 			memset(ret, 0, size);
   3207   1.1        ad 		}
   3208   1.1        ad #endif
   3209   1.1        ad 	}
   3210   1.1        ad 
   3211   1.1        ad 	return (ret);
   3212   1.1        ad }
   3213   1.1        ad 
   3214   1.1        ad static size_t
   3215   1.1        ad isalloc(const void *ptr)
   3216   1.1        ad {
   3217   1.1        ad 	size_t ret;
   3218   1.1        ad 	arena_chunk_t *chunk;
   3219   1.1        ad 
   3220   1.1        ad 	assert(ptr != NULL);
   3221   1.1        ad 
   3222   1.1        ad 	chunk = (arena_chunk_t *)CHUNK_ADDR2BASE(ptr);
   3223   1.1        ad 	if (chunk != ptr) {
   3224   1.1        ad 		/* Region. */
   3225   1.1        ad 		assert(chunk->arena->magic == ARENA_MAGIC);
   3226   1.1        ad 
   3227   1.1        ad 		ret = arena_salloc(ptr);
   3228   1.1        ad 	} else {
   3229   1.1        ad 		chunk_node_t *node, key;
   3230   1.1        ad 
   3231   1.1        ad 		/* Chunk (huge allocation). */
   3232   1.1        ad 
   3233   1.1        ad 		malloc_mutex_lock(&chunks_mtx);
   3234   1.1        ad 
   3235   1.1        ad 		/* Extract from tree of huge allocations. */
   3236   1.1        ad 		key.chunk = __DECONST(void *, ptr);
   3237   1.2        ad 		/* LINTED */
   3238   1.1        ad 		node = RB_FIND(chunk_tree_s, &huge, &key);
   3239   1.1        ad 		assert(node != NULL);
   3240   1.1        ad 
   3241   1.1        ad 		ret = node->size;
   3242   1.1        ad 
   3243   1.1        ad 		malloc_mutex_unlock(&chunks_mtx);
   3244   1.1        ad 	}
   3245   1.1        ad 
   3246   1.1        ad 	return (ret);
   3247   1.1        ad }
   3248   1.1        ad 
   3249   1.1        ad static void *
   3250   1.1        ad iralloc(void *ptr, size_t size)
   3251   1.1        ad {
   3252   1.1        ad 	void *ret;
   3253   1.1        ad 	size_t oldsize;
   3254   1.1        ad 
   3255   1.1        ad 	assert(ptr != NULL);
   3256   1.1        ad 	assert(size != 0);
   3257   1.1        ad 
   3258   1.1        ad 	oldsize = isalloc(ptr);
   3259   1.1        ad 
   3260   1.1        ad 	if (size <= arena_maxclass)
   3261   1.1        ad 		ret = arena_ralloc(ptr, size, oldsize);
   3262   1.1        ad 	else
   3263   1.1        ad 		ret = huge_ralloc(ptr, size, oldsize);
   3264   1.1        ad 
   3265   1.1        ad 	return (ret);
   3266   1.1        ad }
   3267   1.1        ad 
   3268   1.1        ad static void
   3269   1.1        ad idalloc(void *ptr)
   3270   1.1        ad {
   3271   1.1        ad 	arena_chunk_t *chunk;
   3272   1.1        ad 
   3273   1.1        ad 	assert(ptr != NULL);
   3274   1.1        ad 
   3275   1.1        ad 	chunk = (arena_chunk_t *)CHUNK_ADDR2BASE(ptr);
   3276   1.1        ad 	if (chunk != ptr) {
   3277   1.1        ad 		/* Region. */
   3278   1.1        ad 		arena_dalloc(chunk->arena, chunk, ptr);
   3279   1.1        ad 	} else
   3280   1.1        ad 		huge_dalloc(ptr);
   3281   1.1        ad }
   3282   1.1        ad 
   3283   1.1        ad static void
   3284   1.1        ad malloc_print_stats(void)
   3285   1.1        ad {
   3286   1.1        ad 
   3287   1.1        ad 	if (opt_print_stats) {
   3288   1.1        ad 		char s[UMAX2S_BUFSIZE];
   3289   1.1        ad 		_malloc_message("___ Begin malloc statistics ___\n", "", "",
   3290   1.1        ad 		    "");
   3291   1.1        ad 		_malloc_message("Assertions ",
   3292   1.1        ad #ifdef NDEBUG
   3293   1.1        ad 		    "disabled",
   3294   1.1        ad #else
   3295   1.1        ad 		    "enabled",
   3296   1.1        ad #endif
   3297   1.1        ad 		    "\n", "");
   3298   1.1        ad 		_malloc_message("Boolean MALLOC_OPTIONS: ",
   3299   1.1        ad 		    opt_abort ? "A" : "a",
   3300   1.1        ad 		    opt_junk ? "J" : "j",
   3301   1.1        ad 		    opt_hint ? "H" : "h");
   3302   1.1        ad 		_malloc_message(opt_utrace ? "PU" : "Pu",
   3303   1.1        ad 		    opt_sysv ? "V" : "v",
   3304   1.1        ad 		    opt_xmalloc ? "X" : "x",
   3305   1.1        ad 		    opt_zero ? "Z\n" : "z\n");
   3306   1.1        ad 
   3307  1.28  christos 		_malloc_message("CPUs: ", size_t2s(ncpus, s), "\n", "");
   3308  1.28  christos 		_malloc_message("Max arenas: ", size_t2s(narenas, s), "\n", "");
   3309  1.28  christos 		_malloc_message("Pointer size: ", size_t2s(sizeof(void *), s),
   3310   1.1        ad 		    "\n", "");
   3311  1.28  christos 		_malloc_message("Quantum size: ", size_t2s(quantum, s), "\n", "");
   3312  1.28  christos 		_malloc_message("Max small size: ", size_t2s(small_max, s), "\n",
   3313   1.1        ad 		    "");
   3314   1.1        ad 
   3315  1.28  christos 		_malloc_message("Chunk size: ", size_t2s(chunksize, s), "", "");
   3316  1.28  christos 		_malloc_message(" (2^", size_t2s((size_t)opt_chunk_2pow, s),
   3317  1.27      matt 		    ")\n", "");
   3318   1.1        ad 
   3319   1.1        ad #ifdef MALLOC_STATS
   3320   1.1        ad 		{
   3321   1.1        ad 			size_t allocated, mapped;
   3322   1.1        ad 			unsigned i;
   3323   1.1        ad 			arena_t *arena;
   3324   1.1        ad 
   3325   1.1        ad 			/* Calculate and print allocated/mapped stats. */
   3326   1.1        ad 
   3327   1.1        ad 			/* arenas. */
   3328   1.1        ad 			for (i = 0, allocated = 0; i < narenas; i++) {
   3329   1.1        ad 				if (arenas[i] != NULL) {
   3330   1.1        ad 					malloc_mutex_lock(&arenas[i]->mtx);
   3331   1.1        ad 					allocated +=
   3332   1.1        ad 					    arenas[i]->stats.allocated_small;
   3333   1.1        ad 					allocated +=
   3334   1.1        ad 					    arenas[i]->stats.allocated_large;
   3335   1.1        ad 					malloc_mutex_unlock(&arenas[i]->mtx);
   3336   1.1        ad 				}
   3337   1.1        ad 			}
   3338   1.1        ad 
   3339   1.1        ad 			/* huge/base. */
   3340   1.1        ad 			malloc_mutex_lock(&chunks_mtx);
   3341   1.1        ad 			allocated += huge_allocated;
   3342   1.1        ad 			mapped = stats_chunks.curchunks * chunksize;
   3343   1.1        ad 			malloc_mutex_unlock(&chunks_mtx);
   3344   1.1        ad 
   3345   1.1        ad 			malloc_mutex_lock(&base_mtx);
   3346   1.1        ad 			mapped += base_mapped;
   3347   1.1        ad 			malloc_mutex_unlock(&base_mtx);
   3348   1.1        ad 
   3349   1.1        ad 			malloc_printf("Allocated: %zu, mapped: %zu\n",
   3350   1.1        ad 			    allocated, mapped);
   3351   1.1        ad 
   3352   1.1        ad 			/* Print chunk stats. */
   3353   1.1        ad 			{
   3354   1.1        ad 				chunk_stats_t chunks_stats;
   3355   1.1        ad 
   3356   1.1        ad 				malloc_mutex_lock(&chunks_mtx);
   3357   1.1        ad 				chunks_stats = stats_chunks;
   3358   1.1        ad 				malloc_mutex_unlock(&chunks_mtx);
   3359   1.1        ad 
   3360   1.1        ad 				malloc_printf("chunks: nchunks   "
   3361   1.1        ad 				    "highchunks    curchunks\n");
   3362   1.1        ad 				malloc_printf("  %13llu%13lu%13lu\n",
   3363   1.1        ad 				    chunks_stats.nchunks,
   3364   1.1        ad 				    chunks_stats.highchunks,
   3365   1.1        ad 				    chunks_stats.curchunks);
   3366   1.1        ad 			}
   3367   1.1        ad 
   3368   1.1        ad 			/* Print chunk stats. */
   3369   1.1        ad 			malloc_printf(
   3370   1.8      yamt 			    "huge: nmalloc      ndalloc      "
   3371   1.8      yamt 			    "nralloc    allocated\n");
   3372   1.8      yamt 			malloc_printf(" %12llu %12llu %12llu %12zu\n",
   3373   1.8      yamt 			    huge_nmalloc, huge_ndalloc, huge_nralloc,
   3374   1.8      yamt 			    huge_allocated);
   3375   1.1        ad 
   3376   1.1        ad 			/* Print stats for each arena. */
   3377   1.1        ad 			for (i = 0; i < narenas; i++) {
   3378   1.1        ad 				arena = arenas[i];
   3379   1.1        ad 				if (arena != NULL) {
   3380   1.1        ad 					malloc_printf(
   3381   1.2        ad 					    "\narenas[%u] @ %p\n", i, arena);
   3382   1.1        ad 					malloc_mutex_lock(&arena->mtx);
   3383   1.1        ad 					stats_print(arena);
   3384   1.1        ad 					malloc_mutex_unlock(&arena->mtx);
   3385   1.1        ad 				}
   3386   1.1        ad 			}
   3387   1.1        ad 		}
   3388   1.1        ad #endif /* #ifdef MALLOC_STATS */
   3389   1.1        ad 		_malloc_message("--- End malloc statistics ---\n", "", "", "");
   3390   1.1        ad 	}
   3391   1.1        ad }
   3392   1.1        ad 
   3393   1.1        ad /*
   3394   1.1        ad  * FreeBSD's pthreads implementation calls malloc(3), so the malloc
   3395   1.1        ad  * implementation has to take pains to avoid infinite recursion during
   3396   1.1        ad  * initialization.
   3397   1.1        ad  */
   3398   1.1        ad static inline bool
   3399   1.1        ad malloc_init(void)
   3400   1.1        ad {
   3401   1.1        ad 
   3402   1.1        ad 	if (malloc_initialized == false)
   3403   1.1        ad 		return (malloc_init_hard());
   3404   1.1        ad 
   3405   1.1        ad 	return (false);
   3406   1.1        ad }
   3407   1.1        ad 
   3408   1.1        ad static bool
   3409   1.1        ad malloc_init_hard(void)
   3410   1.1        ad {
   3411   1.1        ad 	unsigned i, j;
   3412   1.9  christos 	ssize_t linklen;
   3413   1.1        ad 	char buf[PATH_MAX + 1];
   3414   1.2        ad 	const char *opts = "";
   3415  1.23  christos 	int serrno;
   3416   1.1        ad 
   3417   1.1        ad 	malloc_mutex_lock(&init_lock);
   3418   1.1        ad 	if (malloc_initialized) {
   3419   1.1        ad 		/*
   3420   1.1        ad 		 * Another thread initialized the allocator before this one
   3421   1.1        ad 		 * acquired init_lock.
   3422   1.1        ad 		 */
   3423   1.1        ad 		malloc_mutex_unlock(&init_lock);
   3424   1.1        ad 		return (false);
   3425   1.1        ad 	}
   3426   1.1        ad 
   3427  1.24  christos 	serrno = errno;
   3428   1.1        ad 	/* Get number of CPUs. */
   3429   1.1        ad 	{
   3430   1.1        ad 		int mib[2];
   3431   1.1        ad 		size_t len;
   3432   1.1        ad 
   3433   1.1        ad 		mib[0] = CTL_HW;
   3434   1.1        ad 		mib[1] = HW_NCPU;
   3435   1.1        ad 		len = sizeof(ncpus);
   3436   1.1        ad 		if (sysctl(mib, 2, &ncpus, &len, (void *) 0, 0) == -1) {
   3437   1.1        ad 			/* Error. */
   3438   1.1        ad 			ncpus = 1;
   3439   1.1        ad 		}
   3440   1.1        ad 	}
   3441   1.1        ad 
   3442   1.1        ad 	/* Get page size. */
   3443   1.1        ad 	{
   3444   1.1        ad 		long result;
   3445   1.1        ad 
   3446   1.1        ad 		result = sysconf(_SC_PAGESIZE);
   3447   1.1        ad 		assert(result != -1);
   3448   1.1        ad 		pagesize = (unsigned) result;
   3449   1.1        ad 
   3450   1.1        ad 		/*
   3451   1.1        ad 		 * We assume that pagesize is a power of 2 when calculating
   3452   1.1        ad 		 * pagesize_mask and pagesize_2pow.
   3453   1.1        ad 		 */
   3454   1.1        ad 		assert(((result - 1) & result) == 0);
   3455   1.1        ad 		pagesize_mask = result - 1;
   3456   1.1        ad 		pagesize_2pow = ffs((int)result) - 1;
   3457   1.1        ad 	}
   3458   1.1        ad 
   3459   1.1        ad 	for (i = 0; i < 3; i++) {
   3460   1.1        ad 		/* Get runtime configuration. */
   3461   1.1        ad 		switch (i) {
   3462   1.1        ad 		case 0:
   3463   1.1        ad 			if ((linklen = readlink("/etc/malloc.conf", buf,
   3464   1.1        ad 						sizeof(buf) - 1)) != -1) {
   3465   1.1        ad 				/*
   3466   1.1        ad 				 * Use the contents of the "/etc/malloc.conf"
   3467   1.1        ad 				 * symbolic link's name.
   3468   1.1        ad 				 */
   3469   1.1        ad 				buf[linklen] = '\0';
   3470   1.1        ad 				opts = buf;
   3471   1.1        ad 			} else {
   3472   1.1        ad 				/* No configuration specified. */
   3473   1.1        ad 				buf[0] = '\0';
   3474   1.1        ad 				opts = buf;
   3475   1.1        ad 			}
   3476   1.1        ad 			break;
   3477   1.1        ad 		case 1:
   3478  1.18        ad 			if ((opts = getenv("MALLOC_OPTIONS")) != NULL &&
   3479  1.18        ad 			    issetugid() == 0) {
   3480   1.1        ad 				/*
   3481   1.1        ad 				 * Do nothing; opts is already initialized to
   3482   1.1        ad 				 * the value of the MALLOC_OPTIONS environment
   3483   1.1        ad 				 * variable.
   3484   1.1        ad 				 */
   3485   1.1        ad 			} else {
   3486   1.1        ad 				/* No configuration specified. */
   3487   1.1        ad 				buf[0] = '\0';
   3488   1.1        ad 				opts = buf;
   3489   1.1        ad 			}
   3490   1.1        ad 			break;
   3491   1.1        ad 		case 2:
   3492   1.1        ad 			if (_malloc_options != NULL) {
   3493   1.1        ad 			    /*
   3494   1.1        ad 			     * Use options that were compiled into the program.
   3495   1.1        ad 			     */
   3496   1.1        ad 			    opts = _malloc_options;
   3497   1.1        ad 			} else {
   3498   1.1        ad 				/* No configuration specified. */
   3499   1.1        ad 				buf[0] = '\0';
   3500   1.1        ad 				opts = buf;
   3501   1.1        ad 			}
   3502   1.1        ad 			break;
   3503   1.1        ad 		default:
   3504   1.1        ad 			/* NOTREACHED */
   3505   1.7      yamt 			/* LINTED */
   3506   1.1        ad 			assert(false);
   3507   1.1        ad 		}
   3508   1.1        ad 
   3509   1.1        ad 		for (j = 0; opts[j] != '\0'; j++) {
   3510   1.1        ad 			switch (opts[j]) {
   3511   1.1        ad 			case 'a':
   3512   1.1        ad 				opt_abort = false;
   3513   1.1        ad 				break;
   3514   1.1        ad 			case 'A':
   3515   1.1        ad 				opt_abort = true;
   3516   1.1        ad 				break;
   3517   1.1        ad 			case 'h':
   3518   1.1        ad 				opt_hint = false;
   3519   1.1        ad 				break;
   3520   1.1        ad 			case 'H':
   3521   1.1        ad 				opt_hint = true;
   3522   1.1        ad 				break;
   3523   1.1        ad 			case 'j':
   3524   1.1        ad 				opt_junk = false;
   3525   1.1        ad 				break;
   3526   1.1        ad 			case 'J':
   3527   1.1        ad 				opt_junk = true;
   3528   1.1        ad 				break;
   3529   1.1        ad 			case 'k':
   3530   1.1        ad 				/*
   3531   1.1        ad 				 * Chunks always require at least one header
   3532   1.1        ad 				 * page, so chunks can never be smaller than
   3533   1.1        ad 				 * two pages.
   3534   1.1        ad 				 */
   3535   1.1        ad 				if (opt_chunk_2pow > pagesize_2pow + 1)
   3536   1.1        ad 					opt_chunk_2pow--;
   3537   1.1        ad 				break;
   3538   1.1        ad 			case 'K':
   3539  1.20     lukem 				if (opt_chunk_2pow + 1 <
   3540  1.20     lukem 				    (int)(sizeof(size_t) << 3))
   3541   1.1        ad 					opt_chunk_2pow++;
   3542   1.1        ad 				break;
   3543   1.1        ad 			case 'n':
   3544   1.1        ad 				opt_narenas_lshift--;
   3545   1.1        ad 				break;
   3546   1.1        ad 			case 'N':
   3547   1.1        ad 				opt_narenas_lshift++;
   3548   1.1        ad 				break;
   3549   1.1        ad 			case 'p':
   3550   1.1        ad 				opt_print_stats = false;
   3551   1.1        ad 				break;
   3552   1.1        ad 			case 'P':
   3553   1.1        ad 				opt_print_stats = true;
   3554   1.1        ad 				break;
   3555   1.1        ad 			case 'q':
   3556   1.1        ad 				if (opt_quantum_2pow > QUANTUM_2POW_MIN)
   3557   1.1        ad 					opt_quantum_2pow--;
   3558   1.1        ad 				break;
   3559   1.1        ad 			case 'Q':
   3560   1.1        ad 				if (opt_quantum_2pow < pagesize_2pow - 1)
   3561   1.1        ad 					opt_quantum_2pow++;
   3562   1.1        ad 				break;
   3563   1.1        ad 			case 's':
   3564   1.1        ad 				if (opt_small_max_2pow > QUANTUM_2POW_MIN)
   3565   1.1        ad 					opt_small_max_2pow--;
   3566   1.1        ad 				break;
   3567   1.1        ad 			case 'S':
   3568   1.1        ad 				if (opt_small_max_2pow < pagesize_2pow - 1)
   3569   1.1        ad 					opt_small_max_2pow++;
   3570   1.1        ad 				break;
   3571   1.1        ad 			case 'u':
   3572   1.1        ad 				opt_utrace = false;
   3573   1.1        ad 				break;
   3574   1.1        ad 			case 'U':
   3575   1.1        ad 				opt_utrace = true;
   3576   1.1        ad 				break;
   3577   1.1        ad 			case 'v':
   3578   1.1        ad 				opt_sysv = false;
   3579   1.1        ad 				break;
   3580   1.1        ad 			case 'V':
   3581   1.1        ad 				opt_sysv = true;
   3582   1.1        ad 				break;
   3583   1.1        ad 			case 'x':
   3584   1.1        ad 				opt_xmalloc = false;
   3585   1.1        ad 				break;
   3586   1.1        ad 			case 'X':
   3587   1.1        ad 				opt_xmalloc = true;
   3588   1.1        ad 				break;
   3589   1.1        ad 			case 'z':
   3590   1.1        ad 				opt_zero = false;
   3591   1.1        ad 				break;
   3592   1.1        ad 			case 'Z':
   3593   1.1        ad 				opt_zero = true;
   3594   1.1        ad 				break;
   3595   1.1        ad 			default: {
   3596   1.1        ad 				char cbuf[2];
   3597   1.1        ad 
   3598   1.1        ad 				cbuf[0] = opts[j];
   3599   1.1        ad 				cbuf[1] = '\0';
   3600  1.16  christos 				_malloc_message(getprogname(),
   3601   1.1        ad 				    ": (malloc) Unsupported character in "
   3602   1.1        ad 				    "malloc options: '", cbuf, "'\n");
   3603   1.1        ad 			}
   3604   1.1        ad 			}
   3605   1.1        ad 		}
   3606   1.1        ad 	}
   3607  1.24  christos 	errno = serrno;
   3608   1.1        ad 
   3609   1.1        ad 	/* Take care to call atexit() only once. */
   3610   1.1        ad 	if (opt_print_stats) {
   3611   1.1        ad 		/* Print statistics at exit. */
   3612   1.1        ad 		atexit(malloc_print_stats);
   3613   1.1        ad 	}
   3614   1.1        ad 
   3615   1.1        ad 	/* Set variables according to the value of opt_small_max_2pow. */
   3616   1.1        ad 	if (opt_small_max_2pow < opt_quantum_2pow)
   3617   1.1        ad 		opt_small_max_2pow = opt_quantum_2pow;
   3618   1.1        ad 	small_max = (1 << opt_small_max_2pow);
   3619   1.1        ad 
   3620   1.1        ad 	/* Set bin-related variables. */
   3621   1.1        ad 	bin_maxclass = (pagesize >> 1);
   3622   1.1        ad 	assert(opt_quantum_2pow >= TINY_MIN_2POW);
   3623   1.9  christos 	ntbins = (unsigned)(opt_quantum_2pow - TINY_MIN_2POW);
   3624   1.1        ad 	assert(ntbins <= opt_quantum_2pow);
   3625   1.9  christos 	nqbins = (unsigned)(small_max >> opt_quantum_2pow);
   3626   1.9  christos 	nsbins = (unsigned)(pagesize_2pow - opt_small_max_2pow - 1);
   3627   1.1        ad 
   3628   1.1        ad 	/* Set variables according to the value of opt_quantum_2pow. */
   3629   1.1        ad 	quantum = (1 << opt_quantum_2pow);
   3630   1.1        ad 	quantum_mask = quantum - 1;
   3631   1.1        ad 	if (ntbins > 0)
   3632   1.1        ad 		small_min = (quantum >> 1) + 1;
   3633   1.1        ad 	else
   3634   1.1        ad 		small_min = 1;
   3635   1.1        ad 	assert(small_min <= quantum);
   3636   1.1        ad 
   3637   1.1        ad 	/* Set variables according to the value of opt_chunk_2pow. */
   3638   1.1        ad 	chunksize = (1LU << opt_chunk_2pow);
   3639   1.1        ad 	chunksize_mask = chunksize - 1;
   3640   1.9  christos 	chunksize_2pow = (unsigned)opt_chunk_2pow;
   3641   1.9  christos 	chunk_npages = (unsigned)(chunksize >> pagesize_2pow);
   3642   1.1        ad 	{
   3643   1.1        ad 		unsigned header_size;
   3644   1.1        ad 
   3645   1.9  christos 		header_size = (unsigned)(sizeof(arena_chunk_t) +
   3646   1.9  christos 		    (sizeof(arena_chunk_map_t) * (chunk_npages - 1)));
   3647   1.1        ad 		arena_chunk_header_npages = (header_size >> pagesize_2pow);
   3648   1.1        ad 		if ((header_size & pagesize_mask) != 0)
   3649   1.1        ad 			arena_chunk_header_npages++;
   3650   1.1        ad 	}
   3651   1.1        ad 	arena_maxclass = chunksize - (arena_chunk_header_npages <<
   3652   1.1        ad 	    pagesize_2pow);
   3653   1.1        ad 
   3654   1.1        ad 	UTRACE(0, 0, 0);
   3655   1.1        ad 
   3656   1.1        ad #ifdef MALLOC_STATS
   3657   1.1        ad 	memset(&stats_chunks, 0, sizeof(chunk_stats_t));
   3658   1.1        ad #endif
   3659   1.1        ad 
   3660   1.1        ad 	/* Various sanity checks that regard configuration. */
   3661   1.1        ad 	assert(quantum >= sizeof(void *));
   3662   1.1        ad 	assert(quantum <= pagesize);
   3663   1.1        ad 	assert(chunksize >= pagesize);
   3664   1.1        ad 	assert(quantum * 4 <= chunksize);
   3665   1.1        ad 
   3666   1.1        ad 	/* Initialize chunks data. */
   3667   1.1        ad 	malloc_mutex_init(&chunks_mtx);
   3668   1.1        ad 	RB_INIT(&huge);
   3669   1.1        ad #ifdef USE_BRK
   3670   1.1        ad 	malloc_mutex_init(&brk_mtx);
   3671   1.1        ad 	brk_base = sbrk(0);
   3672   1.1        ad 	brk_prev = brk_base;
   3673   1.1        ad 	brk_max = brk_base;
   3674   1.1        ad #endif
   3675   1.1        ad #ifdef MALLOC_STATS
   3676   1.1        ad 	huge_nmalloc = 0;
   3677   1.1        ad 	huge_ndalloc = 0;
   3678   1.8      yamt 	huge_nralloc = 0;
   3679   1.1        ad 	huge_allocated = 0;
   3680   1.1        ad #endif
   3681   1.1        ad 	RB_INIT(&old_chunks);
   3682   1.1        ad 
   3683   1.1        ad 	/* Initialize base allocation data structures. */
   3684   1.1        ad #ifdef MALLOC_STATS
   3685   1.1        ad 	base_mapped = 0;
   3686   1.1        ad #endif
   3687   1.1        ad #ifdef USE_BRK
   3688   1.1        ad 	/*
   3689   1.1        ad 	 * Allocate a base chunk here, since it doesn't actually have to be
   3690   1.1        ad 	 * chunk-aligned.  Doing this before allocating any other chunks allows
   3691   1.1        ad 	 * the use of space that would otherwise be wasted.
   3692   1.1        ad 	 */
   3693   1.1        ad 	base_pages_alloc(0);
   3694   1.1        ad #endif
   3695   1.1        ad 	base_chunk_nodes = NULL;
   3696   1.1        ad 	malloc_mutex_init(&base_mtx);
   3697   1.1        ad 
   3698   1.1        ad 	if (ncpus > 1) {
   3699   1.1        ad 		/*
   3700   1.1        ad 		 * For SMP systems, create four times as many arenas as there
   3701   1.1        ad 		 * are CPUs by default.
   3702   1.1        ad 		 */
   3703   1.1        ad 		opt_narenas_lshift += 2;
   3704   1.1        ad 	}
   3705   1.1        ad 
   3706   1.1        ad 	/* Determine how many arenas to use. */
   3707   1.1        ad 	narenas = ncpus;
   3708   1.1        ad 	if (opt_narenas_lshift > 0) {
   3709   1.1        ad 		if ((narenas << opt_narenas_lshift) > narenas)
   3710   1.1        ad 			narenas <<= opt_narenas_lshift;
   3711   1.1        ad 		/*
   3712   1.1        ad 		 * Make sure not to exceed the limits of what base_malloc()
   3713   1.1        ad 		 * can handle.
   3714   1.1        ad 		 */
   3715   1.1        ad 		if (narenas * sizeof(arena_t *) > chunksize)
   3716   1.9  christos 			narenas = (unsigned)(chunksize / sizeof(arena_t *));
   3717   1.1        ad 	} else if (opt_narenas_lshift < 0) {
   3718   1.1        ad 		if ((narenas << opt_narenas_lshift) < narenas)
   3719   1.1        ad 			narenas <<= opt_narenas_lshift;
   3720  1.15        ad 		/* Make sure there is at least one arena. */
   3721  1.15        ad 		if (narenas == 0)
   3722  1.15        ad 			narenas = 1;
   3723   1.1        ad 	}
   3724   1.1        ad 
   3725   1.1        ad 	next_arena = 0;
   3726   1.1        ad 
   3727   1.1        ad 	/* Allocate and initialize arenas. */
   3728   1.1        ad 	arenas = (arena_t **)base_alloc(sizeof(arena_t *) * narenas);
   3729   1.1        ad 	if (arenas == NULL) {
   3730   1.1        ad 		malloc_mutex_unlock(&init_lock);
   3731   1.1        ad 		return (true);
   3732   1.1        ad 	}
   3733   1.1        ad 	/*
   3734   1.1        ad 	 * Zero the array.  In practice, this should always be pre-zeroed,
   3735   1.1        ad 	 * since it was just mmap()ed, but let's be sure.
   3736   1.1        ad 	 */
   3737   1.1        ad 	memset(arenas, 0, sizeof(arena_t *) * narenas);
   3738   1.1        ad 
   3739   1.1        ad 	/*
   3740   1.1        ad 	 * Initialize one arena here.  The rest are lazily created in
   3741   1.1        ad 	 * arena_choose_hard().
   3742   1.1        ad 	 */
   3743   1.1        ad 	arenas_extend(0);
   3744   1.1        ad 	if (arenas[0] == NULL) {
   3745   1.1        ad 		malloc_mutex_unlock(&init_lock);
   3746   1.1        ad 		return (true);
   3747   1.1        ad 	}
   3748   1.1        ad 
   3749   1.1        ad 	malloc_mutex_init(&arenas_mtx);
   3750   1.1        ad 
   3751   1.1        ad 	malloc_initialized = true;
   3752   1.1        ad 	malloc_mutex_unlock(&init_lock);
   3753   1.1        ad 	return (false);
   3754   1.1        ad }
   3755   1.1        ad 
   3756   1.1        ad /*
   3757   1.1        ad  * End general internal functions.
   3758   1.1        ad  */
   3759   1.1        ad /******************************************************************************/
   3760   1.1        ad /*
   3761   1.1        ad  * Begin malloc(3)-compatible functions.
   3762   1.1        ad  */
   3763   1.1        ad 
   3764   1.1        ad void *
   3765   1.1        ad malloc(size_t size)
   3766   1.1        ad {
   3767   1.1        ad 	void *ret;
   3768   1.1        ad 
   3769   1.1        ad 	if (malloc_init()) {
   3770   1.1        ad 		ret = NULL;
   3771   1.1        ad 		goto RETURN;
   3772   1.1        ad 	}
   3773   1.1        ad 
   3774   1.1        ad 	if (size == 0) {
   3775   1.1        ad 		if (opt_sysv == false)
   3776   1.1        ad 			size = 1;
   3777   1.1        ad 		else {
   3778   1.1        ad 			ret = NULL;
   3779   1.1        ad 			goto RETURN;
   3780   1.1        ad 		}
   3781   1.1        ad 	}
   3782   1.1        ad 
   3783   1.1        ad 	ret = imalloc(size);
   3784   1.1        ad 
   3785   1.1        ad RETURN:
   3786   1.1        ad 	if (ret == NULL) {
   3787   1.1        ad 		if (opt_xmalloc) {
   3788  1.16  christos 			_malloc_message(getprogname(),
   3789   1.1        ad 			    ": (malloc) Error in malloc(): out of memory\n", "",
   3790   1.1        ad 			    "");
   3791   1.1        ad 			abort();
   3792   1.1        ad 		}
   3793   1.1        ad 		errno = ENOMEM;
   3794   1.1        ad 	}
   3795   1.1        ad 
   3796   1.1        ad 	UTRACE(0, size, ret);
   3797   1.1        ad 	return (ret);
   3798   1.1        ad }
   3799   1.1        ad 
   3800   1.1        ad int
   3801   1.1        ad posix_memalign(void **memptr, size_t alignment, size_t size)
   3802   1.1        ad {
   3803   1.1        ad 	int ret;
   3804   1.1        ad 	void *result;
   3805   1.1        ad 
   3806   1.1        ad 	if (malloc_init())
   3807   1.1        ad 		result = NULL;
   3808   1.1        ad 	else {
   3809   1.1        ad 		/* Make sure that alignment is a large enough power of 2. */
   3810   1.1        ad 		if (((alignment - 1) & alignment) != 0
   3811   1.1        ad 		    || alignment < sizeof(void *)) {
   3812   1.1        ad 			if (opt_xmalloc) {
   3813  1.16  christos 				_malloc_message(getprogname(),
   3814   1.1        ad 				    ": (malloc) Error in posix_memalign(): "
   3815   1.1        ad 				    "invalid alignment\n", "", "");
   3816   1.1        ad 				abort();
   3817   1.1        ad 			}
   3818   1.1        ad 			result = NULL;
   3819   1.1        ad 			ret = EINVAL;
   3820   1.1        ad 			goto RETURN;
   3821   1.1        ad 		}
   3822   1.1        ad 
   3823   1.1        ad 		result = ipalloc(alignment, size);
   3824   1.1        ad 	}
   3825   1.1        ad 
   3826   1.1        ad 	if (result == NULL) {
   3827   1.1        ad 		if (opt_xmalloc) {
   3828  1.16  christos 			_malloc_message(getprogname(),
   3829   1.1        ad 			": (malloc) Error in posix_memalign(): out of memory\n",
   3830   1.1        ad 			"", "");
   3831   1.1        ad 			abort();
   3832   1.1        ad 		}
   3833   1.1        ad 		ret = ENOMEM;
   3834   1.1        ad 		goto RETURN;
   3835   1.1        ad 	}
   3836   1.1        ad 
   3837   1.1        ad 	*memptr = result;
   3838   1.1        ad 	ret = 0;
   3839   1.1        ad 
   3840   1.1        ad RETURN:
   3841   1.1        ad 	UTRACE(0, size, result);
   3842   1.1        ad 	return (ret);
   3843   1.1        ad }
   3844   1.1        ad 
   3845   1.1        ad void *
   3846   1.1        ad calloc(size_t num, size_t size)
   3847   1.1        ad {
   3848   1.1        ad 	void *ret;
   3849   1.1        ad 	size_t num_size;
   3850   1.1        ad 
   3851   1.1        ad 	if (malloc_init()) {
   3852   1.1        ad 		num_size = 0;
   3853   1.1        ad 		ret = NULL;
   3854   1.1        ad 		goto RETURN;
   3855   1.1        ad 	}
   3856   1.1        ad 
   3857   1.1        ad 	num_size = num * size;
   3858   1.1        ad 	if (num_size == 0) {
   3859   1.1        ad 		if ((opt_sysv == false) && ((num == 0) || (size == 0)))
   3860   1.1        ad 			num_size = 1;
   3861   1.1        ad 		else {
   3862   1.1        ad 			ret = NULL;
   3863   1.1        ad 			goto RETURN;
   3864   1.1        ad 		}
   3865   1.1        ad 	/*
   3866   1.1        ad 	 * Try to avoid division here.  We know that it isn't possible to
   3867   1.1        ad 	 * overflow during multiplication if neither operand uses any of the
   3868   1.1        ad 	 * most significant half of the bits in a size_t.
   3869   1.1        ad 	 */
   3870   1.2        ad 	} else if ((unsigned long long)((num | size) &
   3871   1.2        ad 	   ((unsigned long long)SIZE_T_MAX << (sizeof(size_t) << 2))) &&
   3872   1.2        ad 	   (num_size / size != num)) {
   3873   1.1        ad 		/* size_t overflow. */
   3874   1.1        ad 		ret = NULL;
   3875   1.1        ad 		goto RETURN;
   3876   1.1        ad 	}
   3877   1.1        ad 
   3878   1.1        ad 	ret = icalloc(num_size);
   3879   1.1        ad 
   3880   1.1        ad RETURN:
   3881   1.1        ad 	if (ret == NULL) {
   3882   1.1        ad 		if (opt_xmalloc) {
   3883  1.16  christos 			_malloc_message(getprogname(),
   3884   1.1        ad 			    ": (malloc) Error in calloc(): out of memory\n", "",
   3885   1.1        ad 			    "");
   3886   1.1        ad 			abort();
   3887   1.1        ad 		}
   3888   1.1        ad 		errno = ENOMEM;
   3889   1.1        ad 	}
   3890   1.1        ad 
   3891   1.1        ad 	UTRACE(0, num_size, ret);
   3892   1.1        ad 	return (ret);
   3893   1.1        ad }
   3894   1.1        ad 
   3895   1.1        ad void *
   3896   1.1        ad realloc(void *ptr, size_t size)
   3897   1.1        ad {
   3898   1.1        ad 	void *ret;
   3899   1.1        ad 
   3900   1.1        ad 	if (size == 0) {
   3901   1.1        ad 		if (opt_sysv == false)
   3902   1.1        ad 			size = 1;
   3903   1.1        ad 		else {
   3904   1.1        ad 			if (ptr != NULL)
   3905   1.1        ad 				idalloc(ptr);
   3906   1.1        ad 			ret = NULL;
   3907   1.1        ad 			goto RETURN;
   3908   1.1        ad 		}
   3909   1.1        ad 	}
   3910   1.1        ad 
   3911   1.1        ad 	if (ptr != NULL) {
   3912   1.1        ad 		assert(malloc_initialized);
   3913   1.1        ad 
   3914   1.1        ad 		ret = iralloc(ptr, size);
   3915   1.1        ad 
   3916   1.1        ad 		if (ret == NULL) {
   3917   1.1        ad 			if (opt_xmalloc) {
   3918  1.16  christos 				_malloc_message(getprogname(),
   3919   1.1        ad 				    ": (malloc) Error in realloc(): out of "
   3920   1.1        ad 				    "memory\n", "", "");
   3921   1.1        ad 				abort();
   3922   1.1        ad 			}
   3923   1.1        ad 			errno = ENOMEM;
   3924   1.1        ad 		}
   3925   1.1        ad 	} else {
   3926   1.1        ad 		if (malloc_init())
   3927   1.1        ad 			ret = NULL;
   3928   1.1        ad 		else
   3929   1.1        ad 			ret = imalloc(size);
   3930   1.1        ad 
   3931   1.1        ad 		if (ret == NULL) {
   3932   1.1        ad 			if (opt_xmalloc) {
   3933  1.16  christos 				_malloc_message(getprogname(),
   3934   1.1        ad 				    ": (malloc) Error in realloc(): out of "
   3935   1.1        ad 				    "memory\n", "", "");
   3936   1.1        ad 				abort();
   3937   1.1        ad 			}
   3938   1.1        ad 			errno = ENOMEM;
   3939   1.1        ad 		}
   3940   1.1        ad 	}
   3941   1.1        ad 
   3942   1.1        ad RETURN:
   3943   1.1        ad 	UTRACE(ptr, size, ret);
   3944   1.1        ad 	return (ret);
   3945   1.1        ad }
   3946   1.1        ad 
   3947   1.1        ad void
   3948   1.1        ad free(void *ptr)
   3949   1.1        ad {
   3950   1.1        ad 
   3951   1.1        ad 	UTRACE(ptr, 0, 0);
   3952   1.1        ad 	if (ptr != NULL) {
   3953   1.1        ad 		assert(malloc_initialized);
   3954   1.1        ad 
   3955   1.1        ad 		idalloc(ptr);
   3956   1.1        ad 	}
   3957   1.1        ad }
   3958   1.1        ad 
   3959   1.1        ad /*
   3960   1.1        ad  * End malloc(3)-compatible functions.
   3961   1.1        ad  */
   3962   1.1        ad /******************************************************************************/
   3963   1.1        ad /*
   3964   1.1        ad  * Begin non-standard functions.
   3965   1.1        ad  */
   3966   1.2        ad #ifndef __NetBSD__
   3967   1.1        ad size_t
   3968   1.1        ad malloc_usable_size(const void *ptr)
   3969   1.1        ad {
   3970   1.1        ad 
   3971   1.1        ad 	assert(ptr != NULL);
   3972   1.1        ad 
   3973   1.1        ad 	return (isalloc(ptr));
   3974   1.1        ad }
   3975   1.2        ad #endif
   3976   1.1        ad 
   3977   1.1        ad /*
   3978   1.1        ad  * End non-standard functions.
   3979   1.1        ad  */
   3980   1.1        ad /******************************************************************************/
   3981   1.1        ad /*
   3982   1.1        ad  * Begin library-private functions, used by threading libraries for protection
   3983   1.1        ad  * of malloc during fork().  These functions are only called if the program is
   3984   1.1        ad  * running in threaded mode, so there is no need to check whether the program
   3985   1.1        ad  * is threaded here.
   3986   1.1        ad  */
   3987   1.1        ad 
   3988   1.1        ad void
   3989   1.1        ad _malloc_prefork(void)
   3990   1.1        ad {
   3991   1.1        ad 	unsigned i;
   3992   1.1        ad 
   3993   1.1        ad 	/* Acquire all mutexes in a safe order. */
   3994  1.48     joerg 	malloc_mutex_lock(&init_lock);
   3995   1.1        ad 	malloc_mutex_lock(&arenas_mtx);
   3996   1.1        ad 	for (i = 0; i < narenas; i++) {
   3997   1.1        ad 		if (arenas[i] != NULL)
   3998   1.1        ad 			malloc_mutex_lock(&arenas[i]->mtx);
   3999   1.1        ad 	}
   4000  1.48     joerg 	malloc_mutex_lock(&chunks_mtx);
   4001   1.1        ad 	malloc_mutex_lock(&base_mtx);
   4002  1.48     joerg #ifdef USE_BRK
   4003  1.48     joerg 	malloc_mutex_lock(&brk_mtx);
   4004  1.48     joerg #endif
   4005   1.1        ad }
   4006   1.1        ad 
   4007   1.1        ad void
   4008   1.1        ad _malloc_postfork(void)
   4009   1.1        ad {
   4010   1.1        ad 	unsigned i;
   4011   1.1        ad 
   4012   1.1        ad 	/* Release all mutexes, now that fork() has completed. */
   4013  1.48     joerg #ifdef USE_BRK
   4014  1.48     joerg 	malloc_mutex_unlock(&brk_mtx);
   4015  1.48     joerg #endif
   4016  1.48     joerg 	malloc_mutex_unlock(&base_mtx);
   4017   1.1        ad 	malloc_mutex_unlock(&chunks_mtx);
   4018   1.1        ad 
   4019  1.48     joerg 	for (i = narenas; i-- > 0; ) {
   4020   1.1        ad 		if (arenas[i] != NULL)
   4021   1.1        ad 			malloc_mutex_unlock(&arenas[i]->mtx);
   4022   1.1        ad 	}
   4023   1.1        ad 	malloc_mutex_unlock(&arenas_mtx);
   4024  1.48     joerg 	malloc_mutex_unlock(&init_lock);
   4025   1.1        ad }
   4026   1.1        ad 
   4027   1.1        ad /*
   4028   1.1        ad  * End library-private functions.
   4029   1.1        ad  */
   4030   1.1        ad /******************************************************************************/
   4031