The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/sys/malloc.h

Version: -  FREEBSD  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-2  -  FREEBSD-11-1  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-4  -  FREEBSD-10-3  -  FREEBSD-10-2  -  FREEBSD-10-1  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-3  -  FREEBSD-9-2  -  FREEBSD-9-1  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-4  -  FREEBSD-8-3  -  FREEBSD-8-2  -  FREEBSD-8-1  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-4  -  FREEBSD-7-3  -  FREEBSD-7-2  -  FREEBSD-7-1  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-4  -  FREEBSD-6-3  -  FREEBSD-6-2  -  FREEBSD-6-1  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-5  -  FREEBSD-5-4  -  FREEBSD-5-3  -  FREEBSD-5-2  -  FREEBSD-5-1  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  linux-2.6  -  linux-2.4.22  -  MK83  -  MK84  -  PLAN9  -  DFBSD  -  NETBSD  -  NETBSD5  -  NETBSD4  -  NETBSD3  -  NETBSD20  -  OPENBSD  -  xnu-517  -  xnu-792  -  xnu-792.6.70  -  xnu-1228  -  xnu-1456.1.26  -  xnu-1699.24.8  -  xnu-2050.18.24  -  OPENSOLARIS  -  minix-3-1-1 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * Copyright (c) 1987, 1993
    3  *      The Regents of the University of California.
    4  * Copyright (c) 2005, 2009 Robert N. M. Watson
    5  * All rights reserved.
    6  *
    7  * Redistribution and use in source and binary forms, with or without
    8  * modification, are permitted provided that the following conditions
    9  * are met:
   10  * 1. Redistributions of source code must retain the above copyright
   11  *    notice, this list of conditions and the following disclaimer.
   12  * 2. Redistributions in binary form must reproduce the above copyright
   13  *    notice, this list of conditions and the following disclaimer in the
   14  *    documentation and/or other materials provided with the distribution.
   15  * 4. Neither the name of the University nor the names of its contributors
   16  *    may be used to endorse or promote products derived from this software
   17  *    without specific prior written permission.
   18  *
   19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   29  * SUCH DAMAGE.
   30  *
   31  *      @(#)malloc.h    8.5 (Berkeley) 5/3/95
   32  * $FreeBSD: stable/11/sys/sys/malloc.h 331722 2018-03-29 02:50:57Z eadler $
   33  */
   34 
   35 #ifndef _SYS_MALLOC_H_
   36 #define _SYS_MALLOC_H_
   37 
   38 #include <sys/param.h>
   39 #include <sys/queue.h>
   40 #include <sys/_lock.h>
   41 #include <sys/_mutex.h>
   42 #include <machine/_limits.h>
   43 
   44 #define MINALLOCSIZE    UMA_SMALLEST_UNIT
   45 
   46 /*
   47  * flags to malloc.
   48  */
   49 #define M_NOWAIT        0x0001          /* do not block */
   50 #define M_WAITOK        0x0002          /* ok to block */
   51 #define M_ZERO          0x0100          /* bzero the allocation */
   52 #define M_NOVM          0x0200          /* don't ask VM for pages */
   53 #define M_USE_RESERVE   0x0400          /* can alloc out of reserve memory */
   54 #define M_NODUMP        0x0800          /* don't dump pages in this allocation */
   55 #define M_FIRSTFIT      0x1000          /* Only for vmem, fast fit. */
   56 #define M_BESTFIT       0x2000          /* Only for vmem, low fragmentation. */
   57 
   58 #define M_MAGIC         877983977       /* time when first defined :-) */
   59 
   60 /*
   61  * Two malloc type structures are present: malloc_type, which is used by a
   62  * type owner to declare the type, and malloc_type_internal, which holds
   63  * malloc-owned statistics and other ABI-sensitive fields, such as the set of
   64  * malloc statistics indexed by the compile-time MAXCPU constant.
   65  * Applications should avoid introducing dependence on the allocator private
   66  * data layout and size.
   67  *
   68  * The malloc_type ks_next field is protected by malloc_mtx.  Other fields in
   69  * malloc_type are static after initialization so unsynchronized.
   70  *
   71  * Statistics in malloc_type_stats are written only when holding a critical
   72  * section and running on the CPU associated with the index into the stat
   73  * array, but read lock-free resulting in possible (minor) races, which the
   74  * monitoring app should take into account.
   75  */
   76 struct malloc_type_stats {
   77         uint64_t        mts_memalloced; /* Bytes allocated on CPU. */
   78         uint64_t        mts_memfreed;   /* Bytes freed on CPU. */
   79         uint64_t        mts_numallocs;  /* Number of allocates on CPU. */
   80         uint64_t        mts_numfrees;   /* number of frees on CPU. */
   81         uint64_t        mts_size;       /* Bitmask of sizes allocated on CPU. */
   82         uint64_t        _mts_reserved1; /* Reserved field. */
   83         uint64_t        _mts_reserved2; /* Reserved field. */
   84         uint64_t        _mts_reserved3; /* Reserved field. */
   85 };
   86 
   87 /*
   88  * Index definitions for the mti_probes[] array.
   89  */
   90 #define DTMALLOC_PROBE_MALLOC           0
   91 #define DTMALLOC_PROBE_FREE             1
   92 #define DTMALLOC_PROBE_MAX              2
   93 
   94 struct malloc_type_internal {
   95         uint32_t        mti_probes[DTMALLOC_PROBE_MAX];
   96                                         /* DTrace probe ID array. */
   97         u_char          mti_zone;
   98         struct malloc_type_stats        mti_stats[MAXCPU];
   99 };
  100 
  101 /*
  102  * Public data structure describing a malloc type.  Private data is hung off
  103  * of ks_handle to avoid encoding internal malloc(9) data structures in
  104  * modules, which will statically allocate struct malloc_type.
  105  */
  106 struct malloc_type {
  107         struct malloc_type *ks_next;    /* Next in global chain. */
  108         u_long           ks_magic;      /* Detect programmer error. */
  109         const char      *ks_shortdesc;  /* Printable type name. */
  110         void            *ks_handle;     /* Priv. data, was lo_class. */
  111 };
  112 
  113 /*
  114  * Statistics structure headers for user space.  The kern.malloc sysctl
  115  * exposes a structure stream consisting of a stream header, then a series of
  116  * malloc type headers and statistics structures (quantity maxcpus).  For
  117  * convenience, the kernel will provide the current value of maxcpus at the
  118  * head of the stream.
  119  */
  120 #define MALLOC_TYPE_STREAM_VERSION      0x00000001
  121 struct malloc_type_stream_header {
  122         uint32_t        mtsh_version;   /* Stream format version. */
  123         uint32_t        mtsh_maxcpus;   /* Value of MAXCPU for stream. */
  124         uint32_t        mtsh_count;     /* Number of records. */
  125         uint32_t        _mtsh_pad;      /* Pad/reserved field. */
  126 };
  127 
  128 #define MALLOC_MAX_NAME 32
  129 struct malloc_type_header {
  130         char                            mth_name[MALLOC_MAX_NAME];
  131 };
  132 
  133 #ifdef _KERNEL
  134 #define MALLOC_DEFINE(type, shortdesc, longdesc)                        \
  135         struct malloc_type type[1] = {                                  \
  136                 { NULL, M_MAGIC, shortdesc, NULL }                      \
  137         };                                                              \
  138         SYSINIT(type##_init, SI_SUB_KMEM, SI_ORDER_THIRD, malloc_init,  \
  139             type);                                                      \
  140         SYSUNINIT(type##_uninit, SI_SUB_KMEM, SI_ORDER_ANY,             \
  141             malloc_uninit, type)
  142 
  143 #define MALLOC_DECLARE(type) \
  144         extern struct malloc_type type[1]
  145 
  146 MALLOC_DECLARE(M_CACHE);
  147 MALLOC_DECLARE(M_DEVBUF);
  148 MALLOC_DECLARE(M_TEMP);
  149 
  150 /*
  151  * Deprecated macro versions of not-quite-malloc() and free().
  152  */
  153 #define MALLOC(space, cast, size, type, flags) \
  154         ((space) = (cast)malloc((u_long)(size), (type), (flags)))
  155 #define FREE(addr, type) free((addr), (type))
  156 
  157 /*
  158  * XXX this should be declared in <sys/uio.h>, but that tends to fail
  159  * because <sys/uio.h> is included in a header before the source file
  160  * has a chance to include <sys/malloc.h> to get MALLOC_DECLARE() defined.
  161  */
  162 MALLOC_DECLARE(M_IOV);
  163 
  164 extern struct mtx malloc_mtx;
  165 
  166 /*
  167  * Function type used when iterating over the list of malloc types.
  168  */
  169 typedef void malloc_type_list_func_t(struct malloc_type *, void *);
  170 
  171 void    contigfree(void *addr, unsigned long size, struct malloc_type *type);
  172 void    *contigmalloc(unsigned long size, struct malloc_type *type, int flags,
  173             vm_paddr_t low, vm_paddr_t high, unsigned long alignment,
  174             vm_paddr_t boundary) __malloc_like __result_use_check
  175             __alloc_size(1) __alloc_align(6);
  176 void    free(void *addr, struct malloc_type *type);
  177 void    *malloc(unsigned long size, struct malloc_type *type, int flags)
  178             __malloc_like __result_use_check __alloc_size(1);
  179 void    *mallocarray(size_t nmemb, size_t size, struct malloc_type *type,
  180             int flags) __malloc_like __result_use_check
  181             __alloc_size2(1, 2);
  182 void    malloc_init(void *);
  183 int     malloc_last_fail(void);
  184 void    malloc_type_allocated(struct malloc_type *type, unsigned long size);
  185 void    malloc_type_freed(struct malloc_type *type, unsigned long size);
  186 void    malloc_type_list(malloc_type_list_func_t *, void *);
  187 void    malloc_uninit(void *);
  188 void    *realloc(void *addr, unsigned long size, struct malloc_type *type,
  189             int flags) __result_use_check __alloc_size(2);
  190 void    *reallocf(void *addr, unsigned long size, struct malloc_type *type,
  191             int flags) __result_use_check __alloc_size(2);
  192 
  193 struct malloc_type *malloc_desc2type(const char *desc);
  194 
  195 /*
  196  * This is sqrt(SIZE_MAX+1), as s1*s2 <= SIZE_MAX
  197  * if both s1 < MUL_NO_OVERFLOW and s2 < MUL_NO_OVERFLOW
  198  */
  199 #define MUL_NO_OVERFLOW         (1UL << (sizeof(size_t) * 8 / 2))
  200 static inline bool
  201 WOULD_OVERFLOW(size_t nmemb, size_t size)
  202 {
  203 
  204         return ((nmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) &&
  205             nmemb > 0 && __SIZE_T_MAX / nmemb < size);
  206 }
  207 #undef MUL_NO_OVERFLOW
  208 #endif /* _KERNEL */
  209 
  210 #endif /* !_SYS_MALLOC_H_ */

Cache object: 08b7858862e7eacdd5b7ba8dcb506cc4


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.