1 /* 2 * Copyright (c) 2019 The DragonFly Project. All rights reserved. 3 * 4 * This code is derived from software contributed to The DragonFly Project 5 * by Matthew Dillon <dillon@backplane.com> 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 * 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 19 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 20 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, 22 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 24 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 25 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 26 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30 #ifndef _SYS__MALLOC_H_ 31 #define _SYS__MALLOC_H_ 32 33 /* 34 * Do not include this header outside _KERNEL or _KERNEL_STRUCTURES scopes. 35 * Used in <sys/user.h>. 36 */ 37 38 #if defined(_KERNEL) || defined(_KERNEL_STRUCTURES) 39 #include <sys/cdefs.h> /* for __cache_align */ 40 #include <machine/stdint.h> /* for __* types */ 41 #include <machine/param.h> /* for SMP_MAXCPU */ 42 43 /* 44 * The malloc tracking structure. Note that per-cpu entries must be 45 * aggregated for accurate statistics, they do not actually break the 46 * stats down by cpu (e.g. the cpu freeing memory will subtract from 47 * its slot, not the originating cpu's slot). 48 * 49 * SMP_MAXCPU is used so modules which use malloc remain compatible 50 * between UP and SMP. 51 */ 52 struct kmalloc_use { 53 __size_t memuse; 54 __size_t inuse; 55 __int64_t calls; /* total packets of this type ever allocated */ 56 57 /* 58 * This value will be added to ks_loosememuse and resetted, 59 * once it goes above certain threshold (ZoneSize). This 60 * is intended to reduce frequency of ks_loosememuse (global) 61 * updates. 62 */ 63 __size_t loosememuse; 64 } __cachealign; 65 66 struct malloc_type { 67 struct malloc_type *ks_next; /* next in list */ 68 __size_t ks_loosememuse; /* (inaccurate) aggregate memuse */ 69 __size_t ks_limit; /* most that are allowed to exist */ 70 struct kmalloc_use ks_use[SMP_MAXCPU]; 71 __uint32_t ks_magic; /* if it's not magic, don't touch it */ 72 const char *ks_shortdesc; /* short description */ 73 __ssize_t ks_reserved[4]; /* future use (module compatibility) */ 74 }; 75 76 typedef struct malloc_type *malloc_type_t; 77 78 #define MALLOC_DECLARE(type) \ 79 extern struct malloc_type type[1] /* placed here for portability */ 80 81 #endif 82 83 #endif /* !_SYS__MALLOC_H_ */ 84