sys/types.h ==> sys/param.h
[dragonfly.git] / sys / sys / mpipe.h
1 /*
2  * Copyright (c) 2003,2004 The DragonFly Project.  All rights reserved.
3  * 
4  * This code is derived from software contributed to The DragonFly Project
5  * by Matthew Dillon <dillon@backplane.com>
6  * 
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in
15  *    the documentation and/or other materials provided with the
16  *    distribution.
17  * 3. Neither the name of The DragonFly Project nor the names of its
18  *    contributors may be used to endorse or promote products derived
19  *    from this software without specific, prior written permission.
20  * 
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  * 
34  * $DragonFly: src/sys/sys/mpipe.h,v 1.4 2004/07/16 05:51:57 dillon Exp $
35  */
36
37 #ifndef _SYS_MPIPE_H_
38 #define _SYS_MPIPE_H_
39
40 #ifndef _SYS_MALLOC_H_
41 #include <sys/malloc.h>
42 #endif
43
44 /*
45  * Pipeline memory allocations with persistent store capabilities.  This
46  * implements a pipeline for allocations of a particular size.  It is used
47  * in order to allow memory allocations to block while at the same time
48  * guarenteeing that no deadlocks will occur.
49  *
50  * By default new allocations are zero'd out. 
51  *
52  * MPF_NOZERO           If specified the underlying buffers are not zero'd.
53  *                      Note this also means you have no way of knowing which
54  *                      buffers are coming from the cache and which are new
55  *                      allocations.
56  *
57  * MPF_CACHEDATA        If specified the deconstructor will be called when
58  *                      the underlying buffer is free()'d, but the buffer may
59  *                      be reused many times before/if that happens.  The
60  *                      buffer is NOT zero'd on reuse regardless of the
61  *                      MPF_NOZERO flag.
62  *
63  *                      If not specified and MPF_NOZERO is also not specified,
64  *                      then buffers reused from the cache will be zero'd as
65  *                      well as new allocations.
66  *
67  *                      Note that the deconstructor function may still be NULL
68  *                      if this flag is specified, meaning that you don't need
69  *                      notification when the cached contents is physically
70  *                      free()'d.
71  *
72  * MPF_INT              Use the interrupt reserve if necessary.
73  */
74 struct mpipe_buf;
75
76 struct malloc_pipe {
77     malloc_type_t type;         /* malloc bucket */
78     int         bytes;          /* allocation size */
79     int         mpflags;        /* MPF_ flags */
80     int         mflags;         /* M_ flags (used internally) */
81     int         pending;        /* there is a request pending */
82     int         free_count;     /* entries in array[] */
83     int         total_count;    /* total outstanding allocations incl free */
84     int         ary_count;      /* guarenteed allocation count */
85     int         max_count;      /* maximum count (M_NOWAIT used beyond nom) */
86     void        **array;        /* array[ary_count] */
87     void        (*deconstruct)(struct malloc_pipe *, void *buf);
88 };
89
90 #define MPF_CACHEDATA           0x0001  /* cache old buffers (do not zero) */ 
91 #define MPF_NOZERO              0x0002  /* do not zero-out new allocations */
92 #define MPF_INT                 0x0004  /* use the interrupt memory reserve */
93
94 typedef struct malloc_pipe *malloc_pipe_t;
95
96 #ifdef _KERNEL
97
98 void mpipe_init(malloc_pipe_t mpipe, malloc_type_t type,
99                 int bytes, int nnom, int nmax, 
100                 int mpflags, void (*deconstruct)(struct malloc_pipe *, void *));
101 void mpipe_done(malloc_pipe_t mpipe);
102 void *mpipe_alloc_waitok(malloc_pipe_t mpipe);
103 void *mpipe_alloc_nowait(malloc_pipe_t mpipe);
104 void mpipe_free(malloc_pipe_t mpipe, void *vbuf);
105
106 #endif
107
108 #endif
109