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