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