VM: only single page chunks
[minix.git] / sys / ufs / chfs / chfs_pool.c
blob6e25d17f27ccdcbf9d82ab79af2db1622c2fe759
1 /* $NetBSD: chfs_pool.c,v 1.1 2011/11/24 15:51:31 ahoka Exp $ */
3 /*-
4 * Copyright (c) 2010 Department of Software Engineering,
5 * University of Szeged, Hungary
6 * All rights reserved.
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by the Department of Software Engineering, University of Szeged, Hungary
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.
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.
34 * Pool allocator and convenience routines for chfs.
37 #include <sys/cdefs.h>
39 #include <sys/param.h>
40 #include <sys/pool.h>
41 #include <sys/atomic.h>
43 #include <uvm/uvm.h>
45 #include "chfs.h"
46 //#include </root/xipffs/netbsd.chfs/chfs.h>
48 /* --------------------------------------------------------------------- */
50 void * chfs_pool_page_alloc(struct pool *, int);
51 void chfs_pool_page_free(struct pool *, void *);
53 extern void* pool_page_alloc_nointr(struct pool *, int);
54 extern void pool_page_free_nointr(struct pool *, void *);
56 /* --------------------------------------------------------------------- */
58 struct pool_allocator chfs_pool_allocator = {
59 .pa_alloc = chfs_pool_page_alloc,
60 .pa_free = chfs_pool_page_free,
63 /* --------------------------------------------------------------------- */
65 void
66 chfs_pool_init(struct chfs_pool *chpp, size_t size, const char *what,
67 struct chfs_mount *chmp)
69 int cnt;
71 cnt = snprintf(chpp->chp_name, sizeof(chpp->chp_name),
72 "%s_chfs_%p", what, chmp);
73 KASSERT(cnt < sizeof(chpp->chp_name));
75 pool_init(&chpp->chp_pool, size, 0, 0, 0, chpp->chp_name,
76 &chfs_pool_allocator, IPL_NONE);
77 chpp->chp_mount = chmp;
80 /* --------------------------------------------------------------------- */
82 void
83 chfs_pool_destroy(struct chfs_pool *chpp)
85 pool_destroy((struct pool *)chpp);
88 /* --------------------------------------------------------------------- */
90 void *
91 chfs_pool_page_alloc(struct pool *pp, int flags)
93 struct chfs_pool *chpp;
94 struct chfs_mount *chmp;
95 unsigned int pages;
96 void *page;
97 dbg("CHFS: pool_page_alloc()\n");
99 chpp = (struct chfs_pool *)pp;
100 chmp = chpp->chp_mount;
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;
107 page = pool_page_alloc_nointr(pp, flags | PR_WAITOK);
108 if (page == NULL) {
109 atomic_dec_uint(&chmp->chm_pages_used);
112 return page;
115 /* --------------------------------------------------------------------- */
117 void
118 chfs_pool_page_free(struct pool *pp, void *v)
120 struct chfs_pool *chpp;
121 struct chfs_mount *chmp;
122 dbg("CHFS: pool_page_free()\n");
124 chpp = (struct chfs_pool *)pp;
125 chmp = chpp->chp_mount;
127 atomic_dec_uint(&chmp->chm_pages_used);
128 pool_page_free_nointr(pp, v);
131 /* --------------------------------------------------------------------- */
133 void
134 chfs_str_pool_init(struct chfs_str_pool *chsp, struct chfs_mount *chmp)
136 dbg("CHFS: str_pool_init()\n");
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);
147 /* --------------------------------------------------------------------- */
149 void
150 chfs_str_pool_destroy(struct chfs_str_pool *chsp)
152 dbg("CHFS: str_pool_destroy()\n");
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);
163 /* --------------------------------------------------------------------- */
165 char *
166 chfs_str_pool_get(struct chfs_str_pool *chsp, size_t len, int flags)
168 struct chfs_pool *p;
169 dbg("CHFS: str_pool_get()\n");
171 KASSERT(len <= 1024);
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 */
185 return (char *)CHFS_POOL_GET(p, flags);
188 /* --------------------------------------------------------------------- */
190 void
191 chfs_str_pool_put(struct chfs_str_pool *chsp, char *str, size_t len)
193 struct chfs_pool *p;
194 dbg("CHFS: str_pool_put()\n");
196 KASSERT(len <= 1024);
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 */
210 CHFS_POOL_PUT(p, str);