1 1.5 riastrad /* $NetBSD: chfs_pool.c,v 1.5 2020/09/05 16:30:12 riastradh Exp $ */ 2 1.1 ahoka 3 1.1 ahoka /*- 4 1.1 ahoka * Copyright (c) 2010 Department of Software Engineering, 5 1.1 ahoka * University of Szeged, Hungary 6 1.1 ahoka * All rights reserved. 7 1.1 ahoka * 8 1.1 ahoka * This code is derived from software contributed to The NetBSD Foundation 9 1.1 ahoka * by the Department of Software Engineering, University of Szeged, Hungary 10 1.1 ahoka * 11 1.1 ahoka * Redistribution and use in source and binary forms, with or without 12 1.1 ahoka * modification, are permitted provided that the following conditions 13 1.1 ahoka * are met: 14 1.1 ahoka * 1. Redistributions of source code must retain the above copyright 15 1.1 ahoka * notice, this list of conditions and the following disclaimer. 16 1.1 ahoka * 2. Redistributions in binary form must reproduce the above copyright 17 1.1 ahoka * notice, this list of conditions and the following disclaimer in the 18 1.1 ahoka * documentation and/or other materials provided with the distribution. 19 1.1 ahoka * 20 1.1 ahoka * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 21 1.1 ahoka * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 22 1.1 ahoka * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 23 1.1 ahoka * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 24 1.1 ahoka * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 25 1.1 ahoka * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 26 1.1 ahoka * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 27 1.1 ahoka * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 1.1 ahoka * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 1.1 ahoka * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 1.1 ahoka * SUCH DAMAGE. 31 1.1 ahoka */ 32 1.1 ahoka 33 1.1 ahoka /* 34 1.1 ahoka * Pool allocator and convenience routines for chfs. 35 1.1 ahoka */ 36 1.1 ahoka 37 1.1 ahoka #include <sys/cdefs.h> 38 1.1 ahoka 39 1.1 ahoka #include <sys/param.h> 40 1.1 ahoka #include <sys/pool.h> 41 1.1 ahoka #include <sys/atomic.h> 42 1.1 ahoka 43 1.1 ahoka #include "chfs.h" 44 1.1 ahoka 45 1.1 ahoka /* --------------------------------------------------------------------- */ 46 1.1 ahoka 47 1.1 ahoka void * chfs_pool_page_alloc(struct pool *, int); 48 1.1 ahoka void chfs_pool_page_free(struct pool *, void *); 49 1.1 ahoka 50 1.1 ahoka /* --------------------------------------------------------------------- */ 51 1.1 ahoka 52 1.1 ahoka struct pool_allocator chfs_pool_allocator = { 53 1.1 ahoka .pa_alloc = chfs_pool_page_alloc, 54 1.1 ahoka .pa_free = chfs_pool_page_free, 55 1.1 ahoka }; 56 1.1 ahoka 57 1.1 ahoka /* --------------------------------------------------------------------- */ 58 1.1 ahoka 59 1.1 ahoka void 60 1.1 ahoka chfs_pool_init(struct chfs_pool *chpp, size_t size, const char *what, 61 1.1 ahoka struct chfs_mount *chmp) 62 1.1 ahoka { 63 1.4 ryoon int cnt __diagused; 64 1.1 ahoka 65 1.1 ahoka cnt = snprintf(chpp->chp_name, sizeof(chpp->chp_name), 66 1.1 ahoka "%s_chfs_%p", what, chmp); 67 1.1 ahoka KASSERT(cnt < sizeof(chpp->chp_name)); 68 1.1 ahoka 69 1.1 ahoka pool_init(&chpp->chp_pool, size, 0, 0, 0, chpp->chp_name, 70 1.1 ahoka &chfs_pool_allocator, IPL_NONE); 71 1.1 ahoka chpp->chp_mount = chmp; 72 1.1 ahoka } 73 1.1 ahoka 74 1.1 ahoka /* --------------------------------------------------------------------- */ 75 1.1 ahoka 76 1.1 ahoka void 77 1.1 ahoka chfs_pool_destroy(struct chfs_pool *chpp) 78 1.1 ahoka { 79 1.1 ahoka pool_destroy((struct pool *)chpp); 80 1.1 ahoka } 81 1.1 ahoka 82 1.1 ahoka /* --------------------------------------------------------------------- */ 83 1.1 ahoka 84 1.1 ahoka void * 85 1.1 ahoka chfs_pool_page_alloc(struct pool *pp, int flags) 86 1.1 ahoka { 87 1.1 ahoka struct chfs_pool *chpp; 88 1.1 ahoka struct chfs_mount *chmp; 89 1.1 ahoka unsigned int pages; 90 1.1 ahoka void *page; 91 1.1 ahoka dbg("CHFS: pool_page_alloc()\n"); 92 1.1 ahoka 93 1.1 ahoka chpp = (struct chfs_pool *)pp; 94 1.1 ahoka chmp = chpp->chp_mount; 95 1.1 ahoka 96 1.1 ahoka pages = atomic_inc_uint_nv(&chmp->chm_pages_used); 97 1.1 ahoka if (pages >= CHFS_PAGES_MAX(chmp)) { 98 1.1 ahoka atomic_dec_uint(&chmp->chm_pages_used); 99 1.1 ahoka return NULL; 100 1.1 ahoka } 101 1.2 christos page = pool_get(pp, flags | PR_WAITOK); 102 1.1 ahoka if (page == NULL) { 103 1.1 ahoka atomic_dec_uint(&chmp->chm_pages_used); 104 1.1 ahoka } 105 1.1 ahoka 106 1.1 ahoka return page; 107 1.1 ahoka } 108 1.1 ahoka 109 1.1 ahoka /* --------------------------------------------------------------------- */ 110 1.1 ahoka 111 1.1 ahoka void 112 1.1 ahoka chfs_pool_page_free(struct pool *pp, void *v) 113 1.1 ahoka { 114 1.1 ahoka struct chfs_pool *chpp; 115 1.1 ahoka struct chfs_mount *chmp; 116 1.1 ahoka dbg("CHFS: pool_page_free()\n"); 117 1.1 ahoka 118 1.1 ahoka chpp = (struct chfs_pool *)pp; 119 1.1 ahoka chmp = chpp->chp_mount; 120 1.1 ahoka 121 1.1 ahoka atomic_dec_uint(&chmp->chm_pages_used); 122 1.2 christos pool_put(pp,v); 123 1.1 ahoka } 124 1.1 ahoka 125 1.1 ahoka /* --------------------------------------------------------------------- */ 126 1.1 ahoka 127 1.1 ahoka void 128 1.1 ahoka chfs_str_pool_init(struct chfs_str_pool *chsp, struct chfs_mount *chmp) 129 1.1 ahoka { 130 1.1 ahoka dbg("CHFS: str_pool_init()\n"); 131 1.1 ahoka 132 1.1 ahoka chfs_pool_init(&chsp->chsp_pool_16, 16, "str", chmp); 133 1.1 ahoka chfs_pool_init(&chsp->chsp_pool_32, 32, "str", chmp); 134 1.1 ahoka chfs_pool_init(&chsp->chsp_pool_64, 64, "str", chmp); 135 1.1 ahoka chfs_pool_init(&chsp->chsp_pool_128, 128, "str", chmp); 136 1.1 ahoka chfs_pool_init(&chsp->chsp_pool_256, 256, "str", chmp); 137 1.1 ahoka chfs_pool_init(&chsp->chsp_pool_512, 512, "str", chmp); 138 1.1 ahoka chfs_pool_init(&chsp->chsp_pool_1024, 1024, "str", chmp); 139 1.1 ahoka } 140 1.1 ahoka 141 1.1 ahoka /* --------------------------------------------------------------------- */ 142 1.1 ahoka 143 1.1 ahoka void 144 1.1 ahoka chfs_str_pool_destroy(struct chfs_str_pool *chsp) 145 1.1 ahoka { 146 1.1 ahoka dbg("CHFS: str_pool_destroy()\n"); 147 1.1 ahoka 148 1.1 ahoka chfs_pool_destroy(&chsp->chsp_pool_16); 149 1.1 ahoka chfs_pool_destroy(&chsp->chsp_pool_32); 150 1.1 ahoka chfs_pool_destroy(&chsp->chsp_pool_64); 151 1.1 ahoka chfs_pool_destroy(&chsp->chsp_pool_128); 152 1.1 ahoka chfs_pool_destroy(&chsp->chsp_pool_256); 153 1.1 ahoka chfs_pool_destroy(&chsp->chsp_pool_512); 154 1.1 ahoka chfs_pool_destroy(&chsp->chsp_pool_1024); 155 1.1 ahoka } 156 1.1 ahoka 157 1.1 ahoka /* --------------------------------------------------------------------- */ 158 1.1 ahoka 159 1.1 ahoka char * 160 1.1 ahoka chfs_str_pool_get(struct chfs_str_pool *chsp, size_t len, int flags) 161 1.1 ahoka { 162 1.1 ahoka struct chfs_pool *p; 163 1.1 ahoka dbg("CHFS: str_pool_get()\n"); 164 1.1 ahoka 165 1.1 ahoka KASSERT(len <= 1024); 166 1.1 ahoka 167 1.1 ahoka if (len <= 16) p = &chsp->chsp_pool_16; 168 1.1 ahoka else if (len <= 32) p = &chsp->chsp_pool_32; 169 1.1 ahoka else if (len <= 64) p = &chsp->chsp_pool_64; 170 1.1 ahoka else if (len <= 128) p = &chsp->chsp_pool_128; 171 1.1 ahoka else if (len <= 256) p = &chsp->chsp_pool_256; 172 1.1 ahoka else if (len <= 512) p = &chsp->chsp_pool_512; 173 1.1 ahoka else if (len <= 1024) p = &chsp->chsp_pool_1024; 174 1.1 ahoka else { 175 1.1 ahoka KASSERT(0); 176 1.1 ahoka p = NULL; /* Silence compiler warnings */ 177 1.1 ahoka } 178 1.1 ahoka 179 1.1 ahoka return (char *)CHFS_POOL_GET(p, flags); 180 1.1 ahoka } 181 1.1 ahoka 182 1.1 ahoka /* --------------------------------------------------------------------- */ 183 1.1 ahoka 184 1.1 ahoka void 185 1.1 ahoka chfs_str_pool_put(struct chfs_str_pool *chsp, char *str, size_t len) 186 1.1 ahoka { 187 1.1 ahoka struct chfs_pool *p; 188 1.1 ahoka dbg("CHFS: str_pool_put()\n"); 189 1.1 ahoka 190 1.1 ahoka KASSERT(len <= 1024); 191 1.1 ahoka 192 1.1 ahoka if (len <= 16) p = &chsp->chsp_pool_16; 193 1.1 ahoka else if (len <= 32) p = &chsp->chsp_pool_32; 194 1.1 ahoka else if (len <= 64) p = &chsp->chsp_pool_64; 195 1.1 ahoka else if (len <= 128) p = &chsp->chsp_pool_128; 196 1.1 ahoka else if (len <= 256) p = &chsp->chsp_pool_256; 197 1.1 ahoka else if (len <= 512) p = &chsp->chsp_pool_512; 198 1.1 ahoka else if (len <= 1024) p = &chsp->chsp_pool_1024; 199 1.1 ahoka else { 200 1.1 ahoka KASSERT(0); 201 1.1 ahoka p = NULL; /* Silence compiler warnings */ 202 1.1 ahoka } 203 1.1 ahoka 204 1.1 ahoka CHFS_POOL_PUT(p, str); 205 1.1 ahoka } 206