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