4 * Copyright (c) 2003,2004 The DragonFly Project. All rights reserved.
6 * This code is derived from software contributed to The DragonFly Project
7 * by Matthew Dillon <dillon@backplane.com>
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
19 * 3. Neither the name of The DragonFly Project nor the names of its
20 * contributors may be used to endorse or promote products derived
21 * from this software without specific, prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
29 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
31 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
32 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
33 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/slaballoc.h>
42 #include <sys/vmmeter.h>
44 #include <sys/thread.h>
45 #include <sys/globaldata.h>
46 #include <sys/mpipe.h>
47 #include <sys/thread2.h>
49 #define arysize(ary) (sizeof(ary)/sizeof((ary)[0]))
51 static MALLOC_DEFINE(M_MPIPEARY, "MPipe Array", "Auxillary MPIPE structure");
54 * Initialize a malloc pipeline for the specified malloc type and allocation
55 * size. Create an array to cache up to nom_count buffers and preallocate
59 mpipe_init(malloc_pipe_t mpipe, malloc_type_t type, int bytes,
62 void (*construct)(void *, void *),
63 void (*deconstruct)(void *, void *),
71 nmax = 0x7FFF0000; /* some very large number */
74 bzero(mpipe, sizeof(struct malloc_pipe));
77 mpipe->mpflags = mpflags;
78 mpipe->construct = construct;
79 mpipe->deconstruct = deconstruct;
81 if ((mpflags & MPF_NOZERO) == 0)
82 mpipe->mflags |= M_ZERO;
83 if (mpflags & MPF_INT)
84 mpipe->mflags |= M_USE_RESERVE | M_USE_INTERRUPT_RESERVE;
85 mpipe->ary_count = nnom;
86 mpipe->max_count = nmax;
87 mpipe->array = kmalloc(nnom * sizeof(mpipe->array[0]), M_MPIPEARY,
90 while (mpipe->free_count < nnom) {
91 n = mpipe->free_count;
92 mpipe->array[n] = kmalloc(bytes, mpipe->type, M_WAITOK | mpipe->mflags);
93 construct(mpipe->array[n], priv);
98 lwkt_token_init(&mpipe->token, 1, "mpipe token");
102 * Destroy a previously initialized mpipe. This routine can also safely be
103 * called on an uninitialized mpipe structure if it was zero'd or mpipe_done()
104 * was previously called on it.
107 mpipe_done(malloc_pipe_t mpipe)
112 KKASSERT(mpipe->free_count == mpipe->total_count); /* no outstanding mem */
113 for (n = mpipe->free_count - 1; n >= 0; --n) {
114 buf = mpipe->array[n];
115 mpipe->array[n] = NULL;
116 KKASSERT(buf != NULL);
117 if (mpipe->deconstruct)
118 mpipe->deconstruct(buf, mpipe->priv);
119 kfree(buf, mpipe->type);
121 mpipe->free_count = 0;
122 mpipe->total_count = 0;
124 kfree(mpipe->array, M_MPIPEARY);
128 lwkt_token_uninit(&mpipe->token);
132 * Allocate an entry, nominally non-blocking. The allocation is guarenteed
133 * to return non-NULL up to the nominal count after which it may return NULL.
134 * Note that the implementation is defined to be allowed to block for short
135 * periods of time. Use mpipe_alloc_waitok() to guarentee the allocation.
138 mpipe_alloc_nowait(malloc_pipe_t mpipe)
143 lwkt_gettoken(&mpipe->token);
144 if ((n = mpipe->free_count) != 0) {
146 * Use a free entry if it exists.
149 buf = mpipe->array[n];
150 mpipe->array[n] = NULL; /* sanity check, not absolutely needed */
151 mpipe->free_count = n;
152 } else if (mpipe->total_count >= mpipe->max_count) {
154 * Return NULL if we have hit our limit
159 * Otherwise try to malloc() non-blocking.
161 buf = kmalloc(mpipe->bytes, mpipe->type, M_NOWAIT | mpipe->mflags);
163 ++mpipe->total_count;
164 mpipe->construct(buf, mpipe->priv);
167 lwkt_reltoken(&mpipe->token);
172 * Allocate an entry, block until the allocation succeeds. This may cause
173 * us to block waiting for a prior allocation to be freed.
176 mpipe_alloc_waitok(malloc_pipe_t mpipe)
182 lwkt_gettoken(&mpipe->token);
185 if ((n = mpipe->free_count) != 0) {
187 * Use a free entry if it exists.
190 buf = mpipe->array[n];
191 mpipe->array[n] = NULL;
192 mpipe->free_count = n;
195 if (mpipe->total_count >= mpipe->max_count || mfailed) {
197 * Block if we have hit our limit
200 tsleep(mpipe, 0, "mpipe1", 0);
204 * Otherwise try to malloc() non-blocking. If that fails loop to
205 * recheck, and block instead of trying to malloc() again.
207 buf = kmalloc(mpipe->bytes, mpipe->type, M_NOWAIT | mpipe->mflags);
209 ++mpipe->total_count;
210 mpipe->construct(buf, mpipe->priv);
215 lwkt_reltoken(&mpipe->token);
220 * Free an entry, unblock any waiters. Allow NULL.
223 mpipe_free(malloc_pipe_t mpipe, void *buf)
230 lwkt_gettoken(&mpipe->token);
231 if ((n = mpipe->free_count) < mpipe->ary_count) {
233 * Free slot available in free array (LIFO)
235 mpipe->array[n] = buf;
237 if ((mpipe->mpflags & (MPF_CACHEDATA|MPF_NOZERO)) == 0)
238 bzero(buf, mpipe->bytes);
239 lwkt_reltoken(&mpipe->token);
242 * Wakeup anyone blocked in mpipe_alloc_*().
244 if (mpipe->pending) {
250 * All the free slots are full, free the buffer directly.
252 --mpipe->total_count;
253 KKASSERT(mpipe->total_count >= mpipe->free_count);
254 if (mpipe->deconstruct)
255 mpipe->deconstruct(buf, mpipe->priv);
256 lwkt_reltoken(&mpipe->token);
257 kfree(buf, mpipe->type);