1 /* $NetBSD: aml_memman.h,v 1.3 2016/10/04 22:22:16 joerg Exp $ */ 2 3 /*- 4 * Copyright (c) 1999, 2000 Mitsuru IWASAKI <iwasaki@FreeBSD.org> 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 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 * 28 * Id: aml_memman.h,v 1.9 2000/08/09 14:47:43 iwasaki Exp 29 * $FreeBSD: src/usr.sbin/acpi/amldb/aml/aml_memman.h,v 1.1 2000/08/24 09:33:08 takawata Exp $ 30 */ 31 32 #ifndef _MEMMAN_H_ 33 #define _MEMMAN_H_ 34 35 /* 36 * Generic Memory Management 37 */ 38 39 #include <sys/param.h> 40 #include <sys/queue.h> 41 42 /* memory block */ 43 struct memman_block { 44 LIST_ENTRY(memman_block) links; 45 void *block; 46 unsigned static_mem; /* static memory or not */ 47 unsigned int allocated; /* number of allocated chunks */ 48 unsigned int available; /* number of available chunks */ 49 unsigned int allocated_mem; /* block + misc (in bytes) */ 50 51 }; 52 53 LIST_HEAD(memman_block_list, memman_block); 54 55 /* memory node in block */ 56 struct memman_node { 57 LIST_ENTRY(memman_node) links; 58 void *node; 59 struct memman_block *memblock; 60 }; 61 62 LIST_HEAD(memman_node_list, memman_node); 63 64 /* memory type id */ 65 extern unsigned int memid_unkown; 66 67 /* memory block manager */ 68 struct memman_blockman { 69 unsigned int size; /* size of chunk */ 70 unsigned int available; /* total # of available chunks */ 71 void *initial_block; /* initial memory storage */ 72 unsigned initialized; /* initialized or not */ 73 74 struct memman_block_list block_list; 75 struct memman_node_list free_node_list; 76 struct memman_node_list occupied_node_list; 77 }; 78 79 /* memory size histogram */ 80 #define MEMMAN_HISTOGRAM_SIZE 20 81 struct memman_histogram { 82 size_t mem_size; 83 int count; 84 }; 85 86 /* flex size memory allocation info */ 87 struct memman_flexmem_info { 88 LIST_ENTRY(memman_flexmem_info) links; 89 void *addr; 90 size_t mem_size; 91 }; 92 93 LIST_HEAD(memman_flexmem_info_list, memman_flexmem_info); 94 95 /* memory manager */ 96 struct memman { 97 struct memman_blockman *blockman; 98 unsigned int max_memid; /* max number of valid memid */ 99 100 /* fixed size memory blocks */ 101 unsigned int alloc_called; /* memman_alloc() calling */ 102 unsigned int free_called; /* memman_free() calling */ 103 unsigned int salloc_called; /* malloc() calling */ 104 unsigned int sfree_called; /* free() calling */ 105 size_t required_mem; /* total required memory (in bytes) */ 106 size_t allocated_mem; /* total malloc()ed memory */ 107 size_t reclaimed_mem; /* total free()ed memory */ 108 /* flex size memory blocks */ 109 unsigned int flex_alloc_called; /* memman_alloc_flexsize() calling */ 110 unsigned int flex_free_called; /* memman_free_flexsize() calling */ 111 unsigned int flex_salloc_called;/* malloc() calling */ 112 unsigned int flex_sfree_called; /* free() calling */ 113 size_t flex_required_mem; /* total required memory (in bytes) */ 114 size_t flex_allocated_mem;/* total malloc()ed memory */ 115 size_t flex_reclaimed_mem;/* total free()ed memory */ 116 size_t flex_mem_size_min; /* min size of allocated memory */ 117 size_t flex_mem_size_max; /* max size of allocated memory */ 118 size_t flex_peak_mem_usage;/* memory usage at a peak period */ 119 120 /* stuff for more detailed statistical information */ 121 struct memman_histogram *flex_mem_histogram; 122 unsigned int flex_mem_histogram_ptr; 123 int flex_mem_histogram_initial_tolerance; 124 unsigned flex_mem_initialized; 125 struct memman_flexmem_info_list flexmem_info_list; 126 }; 127 128 #define MEMMAN_BLOCKNODE_SIZE(entries) sizeof(struct memman_block) + \ 129 sizeof(struct memman_node) * entries 130 131 #ifndef ROUNDUP_UNIT 132 #define ROUNDUP_UNIT 4 133 #endif 134 135 #if !defined(MEMMAN_INITIAL_SIZE) || MEMMAN_INITIAL_SIZE < 2048 136 #define MEMMAN_INITIAL_SIZE 2048 137 #endif 138 139 #if !defined(MEMMAN_INCR_SIZE) || MEMMAN_INCR_SIZE < 512 140 #define MEMMAN_INCR_SIZE 512 141 #endif 142 143 #define MEMMAN_INITIALSTORAGE_DESC(type, name) \ 144 static struct { \ 145 char blocknodes[MEMMAN_BLOCKNODE_SIZE(MEMMAN_INITIAL_SIZE)]; \ 146 type realblock[MEMMAN_INITIAL_SIZE]; \ 147 } name 148 149 #define MEMMAN_MEMBLOCK_DESC(siz, initial_storage) \ 150 { \ 151 .size = siz, \ 152 .available = MEMMAN_INITIAL_SIZE, \ 153 .initial_block = &initial_storage, \ 154 } 155 156 #define MEMMAN_MEMMANAGER_DESC(block_man, maxmemid, histogram, tolerance) \ 157 { \ 158 .blockman = block_man, \ 159 .max_memid = maxmemid, \ 160 .flex_mem_histogram = histogram, \ 161 .flex_mem_histogram_initial_tolerance = tolerance, \ 162 } 163 164 void *memman_alloc(struct memman *, unsigned int); 165 void *memman_alloc_flexsize(struct memman *, size_t); 166 void memman_free(struct memman *, unsigned int, void *); 167 void memman_free_flexsize(struct memman *, void *); 168 void memman_freeall(struct memman *); 169 void memman_statistics(struct memman *); 170 size_t memman_memid2size(struct memman *, unsigned int); 171 172 #ifdef _KERNEL 173 #define MEMMAN_SYSMALLOC(size) malloc(size, M_MEMMAN, M_WAITOK) 174 #define MEMMAN_SYSFREE(ptr) free(ptr, M_MEMMAN) 175 #define MEMMAN_SYSABORT() /* no abort in kernel */ 176 #else /* !_KERNEL */ 177 #define MEMMAN_SYSMALLOC(size) malloc(size) 178 #define MEMMAN_SYSFREE(ptr) free(ptr) 179 #define MEMMAN_SYSABORT() abort() 180 #endif /* _KERNEL */ 181 182 #endif /* !_MEMMAN_H_ */ 183