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-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*
    2  * Copyright (c) 1987, 1993
    3  *      The Regents of the University of California.  All rights reserved.
    4  *
    5  * Redistribution and use in source and binary forms, with or without
    6  * modification, are permitted provided that the following conditions
    7  * are met:
    8  * 1. Redistributions of source code must retain the above copyright
    9  *    notice, this list of conditions and the following disclaimer.
   10  * 2. Redistributions in binary form must reproduce the above copyright
   11  *    notice, this list of conditions and the following disclaimer in the
   12  *    documentation and/or other materials provided with the distribution.
   13  * 3. All advertising materials mentioning features or use of this software
   14  *    must display the following acknowledgement:
   15  *      This product includes software developed by the University of
   16  *      California, Berkeley and its contributors.
   17  * 4. Neither the name of the University nor the names of its contributors
   18  *    may be used to endorse or promote products derived from this software
   19  *    without specific prior written permission.
   20  *
   21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   31  * SUCH DAMAGE.
   32  *
   33  *      @(#)malloc.h    8.5 (Berkeley) 5/3/95
   34  * $FreeBSD$
   35  */
   36 
   37 #ifndef _SYS_MALLOC_H_
   38 #define _SYS_MALLOC_H_
   39 
   40 #define splmem splhigh
   41 
   42 #define KMEMSTATS
   43 
   44 /*
   45  * flags to malloc
   46  */
   47 #define M_WAITOK        0x0000
   48 #define M_NOWAIT        0x0001
   49 #define M_KERNEL        0x0002
   50 
   51 #define M_MAGIC         877983977       /* time when first defined :-) */
   52 
   53 struct malloc_type {
   54         struct malloc_type *ks_next;    /* next in list */
   55         long    ks_memuse;      /* total memory held in bytes */
   56         long    ks_limit;       /* most that are allowed to exist */
   57         long    ks_size;        /* sizes of this thing that are allocated */
   58         long    ks_inuse;       /* # of packets of this type currently in use */
   59         long    ks_calls;       /* total packets of this type ever allocated */
   60         long    ks_maxused;     /* maximum number ever used */
   61         u_long  ks_magic;       /* if it's not magic, don't touch it */
   62         const char *ks_shortdesc;       /* short description */
   63         u_short ks_limblocks;   /* number of times blocked for hitting limit */
   64         u_short ks_mapblocks;   /* number of times blocked for kernel map */
   65 };
   66 
   67 #ifdef KERNEL
   68 
   69 void    malloc_init __P((void *));
   70 void    malloc_uninit __P((void *));
   71 
   72 #define MALLOC_DEFINE(type, shortdesc, longdesc) \
   73         struct malloc_type type[1] = { \
   74                 { NULL, 0, 0, 0, 0, 0, 0, M_MAGIC, shortdesc, 0, 0 } \
   75         }; \
   76         SYSINIT(type##_init, SI_SUB_KMEM, SI_ORDER_ANY, malloc_init, type); \
   77         SYSUNINIT(type##_uninit, SI_SUB_KMEM, SI_ORDER_ANY, malloc_uninit, type)
   78 
   79 #define MALLOC_DECLARE(type) \
   80         extern struct malloc_type type[1]
   81 
   82 #ifdef MALLOC_INSTANTIATE
   83 #define MALLOC_MAKE_TYPE(type, shortdesc, longdesc) \
   84         MALLOC_DEFINE(type, shortdesc, longdesc)
   85 #else
   86 #define MALLOC_MAKE_TYPE(type, shortdesc, longdesc) \
   87         MALLOC_DECLARE(type)
   88 #endif
   89 
   90 MALLOC_MAKE_TYPE(M_CACHE, "namecache", "Dynamically allocated cache entries");
   91 MALLOC_MAKE_TYPE(M_DEVBUF, "devbuf", "device driver memory");
   92 MALLOC_MAKE_TYPE(M_TEMP, "temp", "misc temporary data buffers");
   93 #endif  /* KERNEL */
   94 
   95 /*
   96  * Array of descriptors that describe the contents of each page
   97  */
   98 struct kmemusage {
   99         short ku_indx;          /* bucket index */
  100         union {
  101                 u_short freecnt;/* for small allocations, free pieces in page */
  102                 u_short pagecnt;/* for large allocations, pages alloced */
  103         } ku_un;
  104 };
  105 #define ku_freecnt ku_un.freecnt
  106 #define ku_pagecnt ku_un.pagecnt
  107 
  108 /*
  109  * Set of buckets for each size of memory block that is retained
  110  */
  111 struct kmembuckets {
  112         caddr_t kb_next;        /* list of free blocks */
  113         caddr_t kb_last;        /* last free block */
  114         long    kb_total;       /* total number of blocks allocated */
  115         long    kb_elmpercl;    /* # of elements in this sized allocation */
  116         long    kb_totalfree;   /* # of free elements in this bucket */
  117         long    kb_calls;       /* total calls to allocate this size */
  118         long    kb_highwat;     /* high water mark */
  119         long    kb_couldfree;   /* over high water mark and could free */
  120 };
  121 
  122 #ifdef KERNEL
  123 
  124 #define MINALLOCSIZE    (1 << MINBUCKET)
  125 #define BUCKETINDX(size) \
  126         ((size) <= (MINALLOCSIZE * 128) \
  127                 ? (size) <= (MINALLOCSIZE * 8) \
  128                         ? (size) <= (MINALLOCSIZE * 2) \
  129                                 ? (size) <= (MINALLOCSIZE * 1) \
  130                                         ? (MINBUCKET + 0) \
  131                                         : (MINBUCKET + 1) \
  132                                 : (size) <= (MINALLOCSIZE * 4) \
  133                                         ? (MINBUCKET + 2) \
  134                                         : (MINBUCKET + 3) \
  135                         : (size) <= (MINALLOCSIZE* 32) \
  136                                 ? (size) <= (MINALLOCSIZE * 16) \
  137                                         ? (MINBUCKET + 4) \
  138                                         : (MINBUCKET + 5) \
  139                                 : (size) <= (MINALLOCSIZE * 64) \
  140                                         ? (MINBUCKET + 6) \
  141                                         : (MINBUCKET + 7) \
  142                 : (size) <= (MINALLOCSIZE * 2048) \
  143                         ? (size) <= (MINALLOCSIZE * 512) \
  144                                 ? (size) <= (MINALLOCSIZE * 256) \
  145                                         ? (MINBUCKET + 8) \
  146                                         : (MINBUCKET + 9) \
  147                                 : (size) <= (MINALLOCSIZE * 1024) \
  148                                         ? (MINBUCKET + 10) \
  149                                         : (MINBUCKET + 11) \
  150                         : (size) <= (MINALLOCSIZE * 8192) \
  151                                 ? (size) <= (MINALLOCSIZE * 4096) \
  152                                         ? (MINBUCKET + 12) \
  153                                         : (MINBUCKET + 13) \
  154                                 : (size) <= (MINALLOCSIZE * 16384) \
  155                                         ? (MINBUCKET + 14) \
  156                                         : (MINBUCKET + 15))
  157 
  158 /*
  159  * Turn virtual addresses into kmem map indices
  160  */
  161 #define kmemxtob(alloc) (kmembase + (alloc) * PAGE_SIZE)
  162 #define btokmemx(addr)  (((caddr_t)(addr) - kmembase) / PAGE_SIZE)
  163 #define btokup(addr)    (&kmemusage[(caddr_t)(addr) - kmembase >> PAGE_SHIFT])
  164 
  165 /*
  166  * Macro versions for the usual cases of malloc/free
  167  */
  168 #if defined(KMEMSTATS) || defined(DIAGNOSTIC)
  169 #define MALLOC(space, cast, size, type, flags) \
  170         (space) = (cast)malloc((u_long)(size), type, flags)
  171 #define FREE(addr, type) free((addr), type)
  172 
  173 #else /* do not collect statistics */
  174 #define MALLOC(space, cast, size, type, flags) do { \
  175         register struct kmembuckets *kbp = &bucket[BUCKETINDX(size)]; \
  176         long s = splmem(); \
  177         if (kbp->kb_next == NULL) { \
  178                 (space) = (cast)malloc((u_long)(size), type, flags); \
  179         } else { \
  180                 (space) = (cast)kbp->kb_next; \
  181                 kbp->kb_next = *(caddr_t *)(space); \
  182         } \
  183         splx(s); \
  184 } while (0)
  185 
  186 #define FREE(addr, type) do { \
  187         register struct kmembuckets *kbp; \
  188         register struct kmemusage *kup = btokup(addr); \
  189         long s = splmem(); \
  190         if (1 << kup->ku_indx > MAXALLOCSAVE) { \
  191                 free((addr), type); \
  192         } else { \
  193                 kbp = &bucket[kup->ku_indx]; \
  194                 if (kbp->kb_next == NULL) \
  195                         kbp->kb_next = (caddr_t)(addr); \
  196                 else \
  197                         *(caddr_t *)(kbp->kb_last) = (caddr_t)(addr); \
  198                 *(caddr_t *)(addr) = NULL; \
  199                 kbp->kb_last = (caddr_t)(addr); \
  200         } \
  201         splx(s); \
  202 } while (0)
  203 
  204 extern struct kmemusage *kmemusage;
  205 extern char *kmembase;
  206 extern struct kmembuckets bucket[];
  207 #endif /* do not collect statistics */
  208 
  209 /*
  210  * XXX this should be declared in <sys/uio.h>, but that tends to fail
  211  * because <sys/uio.h> is included in a header before the source file
  212  * has a chance to include <sys/malloc.h> to get MALLOC_DECLARE() defined.
  213  */
  214 MALLOC_DECLARE(M_IOV);
  215 
  216 void    *contigmalloc __P((unsigned long size, struct malloc_type *type,
  217                            int flags,
  218                            unsigned long low, unsigned long high,
  219                            unsigned long alignment, unsigned long boundary));
  220 void    free __P((void *addr, struct malloc_type *type));
  221 void    *malloc __P((unsigned long size, struct malloc_type *type, int flags));
  222 #endif /* KERNEL */
  223 
  224 #endif /* !_SYS_MALLOC_H_ */

Cache object: d9d0280b58414d510d6b0ce52cda3c31


[ 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.