1 /* $NetBSD: chfs_pool.c,v 1.2 2012/02/28 02:48:39 christos Exp $ */
4 * Copyright (c) 2010 Department of Software Engineering,
5 * University of Szeged, Hungary
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
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
34 * Pool allocator and convenience routines for chfs.
37 #include <sys/cdefs.h>
39 #include <sys/param.h>
41 #include <sys/atomic.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 /* --------------------------------------------------------------------- */
55 struct pool_allocator chfs_pool_allocator
= {
56 .pa_alloc
= chfs_pool_page_alloc
,
57 .pa_free
= chfs_pool_page_free
,
60 /* --------------------------------------------------------------------- */
63 chfs_pool_init(struct chfs_pool
*chpp
, size_t size
, const char *what
,
64 struct chfs_mount
*chmp
)
68 cnt
= snprintf(chpp
->chp_name
, sizeof(chpp
->chp_name
),
69 "%s_chfs_%p", what
, chmp
);
70 KASSERT(cnt
< sizeof(chpp
->chp_name
));
72 pool_init(&chpp
->chp_pool
, size
, 0, 0, 0, chpp
->chp_name
,
73 &chfs_pool_allocator
, IPL_NONE
);
74 chpp
->chp_mount
= chmp
;
77 /* --------------------------------------------------------------------- */
80 chfs_pool_destroy(struct chfs_pool
*chpp
)
82 pool_destroy((struct pool
*)chpp
);
85 /* --------------------------------------------------------------------- */
88 chfs_pool_page_alloc(struct pool
*pp
, int flags
)
90 struct chfs_pool
*chpp
;
91 struct chfs_mount
*chmp
;
94 dbg("CHFS: pool_page_alloc()\n");
96 chpp
= (struct chfs_pool
*)pp
;
97 chmp
= chpp
->chp_mount
;
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
);
104 page
= pool_get(pp
, flags
| PR_WAITOK
);
106 atomic_dec_uint(&chmp
->chm_pages_used
);
112 /* --------------------------------------------------------------------- */
115 chfs_pool_page_free(struct pool
*pp
, void *v
)
117 struct chfs_pool
*chpp
;
118 struct chfs_mount
*chmp
;
119 dbg("CHFS: pool_page_free()\n");
121 chpp
= (struct chfs_pool
*)pp
;
122 chmp
= chpp
->chp_mount
;
124 atomic_dec_uint(&chmp
->chm_pages_used
);
128 /* --------------------------------------------------------------------- */
131 chfs_str_pool_init(struct chfs_str_pool
*chsp
, struct chfs_mount
*chmp
)
133 dbg("CHFS: str_pool_init()\n");
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
);
144 /* --------------------------------------------------------------------- */
147 chfs_str_pool_destroy(struct chfs_str_pool
*chsp
)
149 dbg("CHFS: str_pool_destroy()\n");
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
);
160 /* --------------------------------------------------------------------- */
163 chfs_str_pool_get(struct chfs_str_pool
*chsp
, size_t len
, int flags
)
166 dbg("CHFS: str_pool_get()\n");
168 KASSERT(len
<= 1024);
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
;
179 p
= NULL
; /* Silence compiler warnings */
182 return (char *)CHFS_POOL_GET(p
, flags
);
185 /* --------------------------------------------------------------------- */
188 chfs_str_pool_put(struct chfs_str_pool
*chsp
, char *str
, size_t len
)
191 dbg("CHFS: str_pool_put()\n");
193 KASSERT(len
<= 1024);
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
;
204 p
= NULL
; /* Silence compiler warnings */
207 CHFS_POOL_PUT(p
, str
);