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