Memory pool resource utility.

This commit is contained in:
pk 1997-12-15 11:14:57 +00:00
parent 4702594559
commit 327c0046f9
2 changed files with 264 additions and 0 deletions

207
sys/kern/subr_pool.c Normal file
View File

@ -0,0 +1,207 @@
/* $NetBSD: subr_pool.c,v 1.1 1997/12/15 11:14:57 pk Exp $ */
/*-
* Copyright (c) 1997 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Paul Kranenburg.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the NetBSD
* Foundation, Inc. and its contributors.
* 4. Neither the name of The NetBSD Foundation nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/proc.h>
#include <sys/errno.h>
#include <sys/kernel.h>
#include <sys/malloc.h>
#include <sys/lock.h>
#include <sys/pool.h>
/*
* Pool resource management utility.
*/
struct pool_item {
struct pool_item *pi_next;
};
struct pool *
pool_create(size, nitems, wchan, mtype)
size_t size;
int nitems;
char *wchan;
int mtype;
{
struct pool *pp;
if (size < sizeof(struct pool_item)) {
printf("pool_create: size %lu too small\n", (u_long)size);
return (NULL);
}
pp = (struct pool *)malloc(sizeof(*pp), mtype, M_NOWAIT);
if (pp == NULL)
return (NULL);
pp->pr_freelist = NULL;
pp->pr_freecount = 0;
pp->pr_hiwat = 0;
pp->pr_flags = 0;
pp->pr_size = size;
pp->pr_wchan = wchan;
pp->pr_mtype = mtype;
simple_lock_init(&pp->pr_lock);
if (nitems != 0) {
if (pool_prime(pp, nitems) != 0)
pool_destroy(pp);
return (NULL);
}
return (pp);
}
/*
* De-commision a pool resource.
*/
void
pool_destroy(pp)
struct pool *pp;
{
struct pool_item *pi;
while ((pi = pp->pr_freelist) != NULL) {
pp->pr_freelist = pi->pi_next;
free(pi, pp->pr_mtype);
}
free(pp, pp->pr_mtype);
}
/*
* Grab an item from the pool; must be called at splbio
*/
void *
pool_get(pp, flags)
struct pool *pp;
int flags;
{
void *v;
struct pool_item *pi;
again:
simple_lock(&pp->pr_lock);
if ((v = pp->pr_freelist) == NULL) {
if (flags & PR_MALLOCOK)
v = (void *)malloc(pp->pr_size, pp->pr_mtype, M_NOWAIT);
if (v == NULL) {
if ((flags & PR_WAITOK) == 0)
return (NULL);
pp->pr_flags |= PR_WANTED;
simple_unlock(&pp->pr_lock);
tsleep((caddr_t)pp, PSWP, pp->pr_wchan, 0);
goto again;
}
} else {
pi = v;
pp->pr_freelist = pi->pi_next;
pp->pr_freecount--;
}
simple_unlock(&pp->pr_lock);
return (v);
}
/*
* Return resource to the pool; must be called at splbio
*/
void
pool_put(pp, v)
struct pool *pp;
void *v;
{
struct pool_item *pi = v;
simple_lock(&pp->pr_lock);
if ((pp->pr_flags & PR_WANTED) || pp->pr_freecount < pp->pr_hiwat) {
/* Return to pool */
pi->pi_next = pp->pr_freelist;
pp->pr_freelist = pi;
pp->pr_freecount++;
if (pp->pr_flags & PR_WANTED) {
pp->pr_flags &= ~PR_WANTED;
wakeup((caddr_t)pp);
}
} else {
/* Return to system */
free(v, M_DEVBUF);
/*
* Return any excess items allocated during periods of
* contention.
*/
while (pp->pr_freecount > pp->pr_hiwat) {
pi = pp->pr_freelist;
pp->pr_freelist = pi->pi_next;
pp->pr_freecount--;
free(pi, M_DEVBUF);
}
}
simple_unlock(&pp->pr_lock);
}
/*
* Add N items to the pool
*/
int
pool_prime(pp, n)
struct pool *pp;
int n;
{
struct pool_item *pi;
simple_lock(&pp->pr_lock);
pp->pr_hiwat += n;
while (n--) {
pi = malloc(pp->pr_size, pp->pr_mtype, M_NOWAIT);
if (pi == NULL) {
simple_unlock(&pp->pr_lock);
return (ENOMEM);
}
pi->pi_next = pp->pr_freelist;
pp->pr_freelist = pi;
pp->pr_freecount++;
}
simple_unlock(&pp->pr_lock);
return (0);
}

57
sys/sys/pool.h Normal file
View File

@ -0,0 +1,57 @@
/* $NetBSD: pool.h,v 1.1 1997/12/15 11:14:57 pk Exp $ */
/*-
* Copyright (c) 1997 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Paul Kranenburg.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the NetBSD
* Foundation, Inc. and its contributors.
* 4. Neither the name of The NetBSD Foundation nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
typedef struct pool {
struct pool_item *pr_freelist; /* Free items in pool */
int pr_size; /* Size of item */
int pr_freecount; /* # of free items */
int pr_hiwat; /* max # of pooled items */
char *pr_wchan; /* tsleep(9) identifier */
int pr_mtype; /* malloc(9) tag */
int pr_flags;
#define PR_MALLOCOK 1
#define PR_WAITOK 2
#define PR_WANTED 4
struct simplelock pr_lock;
} *pool_handle_t;
pool_handle_t pool_create __P((size_t, int, char *, int));
void pool_destroy __P((pool_handle_t));
void *pool_get __P((pool_handle_t, int));
void pool_put __P((pool_handle_t, void *));
int pool_prime __P((pool_handle_t, int));