Home | History | Annotate | Line # | Download | only in chfs
chfs_pool.c revision 1.3.4.1
      1  1.3.4.1    martin /*	$NetBSD: chfs_pool.c,v 1.3.4.1 2020/04/13 08:05:20 martin 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 <uvm/uvm.h>
     44      1.1     ahoka 
     45      1.1     ahoka #include "chfs.h"
     46      1.1     ahoka 
     47      1.1     ahoka /* --------------------------------------------------------------------- */
     48      1.1     ahoka 
     49      1.1     ahoka void *	chfs_pool_page_alloc(struct pool *, int);
     50      1.1     ahoka void	chfs_pool_page_free(struct pool *, void *);
     51      1.1     ahoka 
     52      1.1     ahoka /* --------------------------------------------------------------------- */
     53      1.1     ahoka 
     54      1.1     ahoka struct pool_allocator chfs_pool_allocator = {
     55      1.1     ahoka 	.pa_alloc = chfs_pool_page_alloc,
     56      1.1     ahoka 	.pa_free = chfs_pool_page_free,
     57      1.1     ahoka };
     58      1.1     ahoka 
     59      1.1     ahoka /* --------------------------------------------------------------------- */
     60      1.1     ahoka 
     61      1.1     ahoka void
     62      1.1     ahoka chfs_pool_init(struct chfs_pool *chpp, size_t size, const char *what,
     63      1.1     ahoka     struct chfs_mount *chmp)
     64      1.1     ahoka {
     65  1.3.4.1    martin 	int cnt __diagused;
     66      1.1     ahoka 
     67      1.1     ahoka 	cnt = snprintf(chpp->chp_name, sizeof(chpp->chp_name),
     68      1.1     ahoka 	    "%s_chfs_%p", what, chmp);
     69      1.1     ahoka 	KASSERT(cnt < sizeof(chpp->chp_name));
     70      1.1     ahoka 
     71      1.1     ahoka 	pool_init(&chpp->chp_pool, size, 0, 0, 0, chpp->chp_name,
     72      1.1     ahoka 	    &chfs_pool_allocator, IPL_NONE);
     73      1.1     ahoka 	chpp->chp_mount = chmp;
     74      1.1     ahoka }
     75      1.1     ahoka 
     76      1.1     ahoka /* --------------------------------------------------------------------- */
     77      1.1     ahoka 
     78      1.1     ahoka void
     79      1.1     ahoka chfs_pool_destroy(struct chfs_pool *chpp)
     80      1.1     ahoka {
     81      1.1     ahoka 	pool_destroy((struct pool *)chpp);
     82      1.1     ahoka }
     83      1.1     ahoka 
     84      1.1     ahoka /* --------------------------------------------------------------------- */
     85      1.1     ahoka 
     86      1.1     ahoka void *
     87      1.1     ahoka chfs_pool_page_alloc(struct pool *pp, int flags)
     88      1.1     ahoka {
     89      1.1     ahoka 	struct chfs_pool *chpp;
     90      1.1     ahoka 	struct chfs_mount *chmp;
     91      1.1     ahoka 	unsigned int pages;
     92      1.1     ahoka 	void *page;
     93      1.1     ahoka 	dbg("CHFS: pool_page_alloc()\n");
     94      1.1     ahoka 
     95      1.1     ahoka 	chpp = (struct chfs_pool *)pp;
     96      1.1     ahoka 	chmp = chpp->chp_mount;
     97      1.1     ahoka 
     98      1.1     ahoka 	pages = atomic_inc_uint_nv(&chmp->chm_pages_used);
     99      1.1     ahoka 	if (pages >= CHFS_PAGES_MAX(chmp)) {
    100      1.1     ahoka 		atomic_dec_uint(&chmp->chm_pages_used);
    101      1.1     ahoka 		return NULL;
    102      1.1     ahoka 	}
    103      1.2  christos 	page = pool_get(pp, flags | PR_WAITOK);
    104      1.1     ahoka 	if (page == NULL) {
    105      1.1     ahoka 		atomic_dec_uint(&chmp->chm_pages_used);
    106      1.1     ahoka 	}
    107      1.1     ahoka 
    108      1.1     ahoka 	return page;
    109      1.1     ahoka }
    110      1.1     ahoka 
    111      1.1     ahoka /* --------------------------------------------------------------------- */
    112      1.1     ahoka 
    113      1.1     ahoka void
    114      1.1     ahoka chfs_pool_page_free(struct pool *pp, void *v)
    115      1.1     ahoka {
    116      1.1     ahoka 	struct chfs_pool *chpp;
    117      1.1     ahoka 	struct chfs_mount *chmp;
    118      1.1     ahoka 	dbg("CHFS: pool_page_free()\n");
    119      1.1     ahoka 
    120      1.1     ahoka 	chpp = (struct chfs_pool *)pp;
    121      1.1     ahoka 	chmp = chpp->chp_mount;
    122      1.1     ahoka 
    123      1.1     ahoka 	atomic_dec_uint(&chmp->chm_pages_used);
    124      1.2  christos 	pool_put(pp,v);
    125      1.1     ahoka }
    126      1.1     ahoka 
    127      1.1     ahoka /* --------------------------------------------------------------------- */
    128      1.1     ahoka 
    129      1.1     ahoka void
    130      1.1     ahoka chfs_str_pool_init(struct chfs_str_pool *chsp, struct chfs_mount *chmp)
    131      1.1     ahoka {
    132      1.1     ahoka 	dbg("CHFS: str_pool_init()\n");
    133      1.1     ahoka 
    134      1.1     ahoka 	chfs_pool_init(&chsp->chsp_pool_16,   16,   "str", chmp);
    135      1.1     ahoka 	chfs_pool_init(&chsp->chsp_pool_32,   32,   "str", chmp);
    136      1.1     ahoka 	chfs_pool_init(&chsp->chsp_pool_64,   64,   "str", chmp);
    137      1.1     ahoka 	chfs_pool_init(&chsp->chsp_pool_128,  128,  "str", chmp);
    138      1.1     ahoka 	chfs_pool_init(&chsp->chsp_pool_256,  256,  "str", chmp);
    139      1.1     ahoka 	chfs_pool_init(&chsp->chsp_pool_512,  512,  "str", chmp);
    140      1.1     ahoka 	chfs_pool_init(&chsp->chsp_pool_1024, 1024, "str", chmp);
    141      1.1     ahoka }
    142      1.1     ahoka 
    143      1.1     ahoka /* --------------------------------------------------------------------- */
    144      1.1     ahoka 
    145      1.1     ahoka void
    146      1.1     ahoka chfs_str_pool_destroy(struct chfs_str_pool *chsp)
    147      1.1     ahoka {
    148      1.1     ahoka 	dbg("CHFS: str_pool_destroy()\n");
    149      1.1     ahoka 
    150      1.1     ahoka 	chfs_pool_destroy(&chsp->chsp_pool_16);
    151      1.1     ahoka 	chfs_pool_destroy(&chsp->chsp_pool_32);
    152      1.1     ahoka 	chfs_pool_destroy(&chsp->chsp_pool_64);
    153      1.1     ahoka 	chfs_pool_destroy(&chsp->chsp_pool_128);
    154      1.1     ahoka 	chfs_pool_destroy(&chsp->chsp_pool_256);
    155      1.1     ahoka 	chfs_pool_destroy(&chsp->chsp_pool_512);
    156      1.1     ahoka 	chfs_pool_destroy(&chsp->chsp_pool_1024);
    157      1.1     ahoka }
    158      1.1     ahoka 
    159      1.1     ahoka /* --------------------------------------------------------------------- */
    160      1.1     ahoka 
    161      1.1     ahoka char *
    162      1.1     ahoka chfs_str_pool_get(struct chfs_str_pool *chsp, size_t len, int flags)
    163      1.1     ahoka {
    164      1.1     ahoka 	struct chfs_pool *p;
    165      1.1     ahoka 	dbg("CHFS: str_pool_get()\n");
    166      1.1     ahoka 
    167      1.1     ahoka 	KASSERT(len <= 1024);
    168      1.1     ahoka 
    169      1.1     ahoka 	if      (len <= 16)   p = &chsp->chsp_pool_16;
    170      1.1     ahoka 	else if (len <= 32)   p = &chsp->chsp_pool_32;
    171      1.1     ahoka 	else if (len <= 64)   p = &chsp->chsp_pool_64;
    172      1.1     ahoka 	else if (len <= 128)  p = &chsp->chsp_pool_128;
    173      1.1     ahoka 	else if (len <= 256)  p = &chsp->chsp_pool_256;
    174      1.1     ahoka 	else if (len <= 512)  p = &chsp->chsp_pool_512;
    175      1.1     ahoka 	else if (len <= 1024) p = &chsp->chsp_pool_1024;
    176      1.1     ahoka 	else {
    177      1.1     ahoka 		KASSERT(0);
    178      1.1     ahoka 		p = NULL; /* Silence compiler warnings */
    179      1.1     ahoka 	}
    180      1.1     ahoka 
    181      1.1     ahoka 	return (char *)CHFS_POOL_GET(p, flags);
    182      1.1     ahoka }
    183      1.1     ahoka 
    184      1.1     ahoka /* --------------------------------------------------------------------- */
    185      1.1     ahoka 
    186      1.1     ahoka void
    187      1.1     ahoka chfs_str_pool_put(struct chfs_str_pool *chsp, char *str, size_t len)
    188      1.1     ahoka {
    189      1.1     ahoka 	struct chfs_pool *p;
    190      1.1     ahoka 	dbg("CHFS: str_pool_put()\n");
    191      1.1     ahoka 
    192      1.1     ahoka 	KASSERT(len <= 1024);
    193      1.1     ahoka 
    194      1.1     ahoka 	if      (len <= 16)   p = &chsp->chsp_pool_16;
    195      1.1     ahoka 	else if (len <= 32)   p = &chsp->chsp_pool_32;
    196      1.1     ahoka 	else if (len <= 64)   p = &chsp->chsp_pool_64;
    197      1.1     ahoka 	else if (len <= 128)  p = &chsp->chsp_pool_128;
    198      1.1     ahoka 	else if (len <= 256)  p = &chsp->chsp_pool_256;
    199      1.1     ahoka 	else if (len <= 512)  p = &chsp->chsp_pool_512;
    200      1.1     ahoka 	else if (len <= 1024) p = &chsp->chsp_pool_1024;
    201      1.1     ahoka 	else {
    202      1.1     ahoka 		KASSERT(0);
    203      1.1     ahoka 		p = NULL; /* Silence compiler warnings */
    204      1.1     ahoka 	}
    205      1.1     ahoka 
    206      1.1     ahoka 	CHFS_POOL_PUT(p, str);
    207      1.1     ahoka }
    208