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