xref: /dragonfly/sys/sys/mpipe.h (revision 89a89091)
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 #ifndef _SYS_THREAD_H_
44 #include <sys/thread.h>
45 #endif
46 #ifndef _SYS_QUEUE_H_
47 #include <sys/queue.h>
48 #endif
49 
50 /*
51  * Pipeline memory allocations with persistent store capabilities.  This
52  * implements a pipeline for allocations of a particular size.  It is used
53  * in order to allow memory allocations to block while at the same time
54  * guarenteeing that no deadlocks will occur.
55  *
56  * By default new allocations are zero'd out.
57  *
58  * MPF_NOZERO		If specified the underlying buffers are not zero'd.
59  *			Note this also means you have no way of knowing which
60  *			buffers are coming from the cache and which are new
61  *			allocations.
62  *
63  * MPF_CACHEDATA	If specified the deconstructor will be called when
64  *			the underlying buffer is free()'d, but the buffer may
65  *			be reused many times before/if that happens.  The
66  *			buffer is NOT zero'd on reuse regardless of the
67  *			MPF_NOZERO flag.
68  *
69  *			If not specified and MPF_NOZERO is also not specified,
70  *			then buffers reused from the cache will be zero'd as
71  *			well as new allocations.
72  *
73  *			Note that the deconstructor function may still be NULL
74  *			if this flag is specified, meaning that you don't need
75  *			notification when the cached contents is physically
76  *			free()'d.
77  *
78  * MPF_INT		Use the interrupt reserve if necessary.
79  */
80 struct mpipe_buf;
81 struct mpipe_callback;
82 
83 struct malloc_pipe {
84     malloc_type_t type;		/* malloc bucket */
85     int		bytes;		/* allocation size */
86     int		mpflags;	/* MPF_ flags */
87     int		mflags;		/* M_ flags (used internally) */
88     int		pending;	/* there is a request pending */
89     int		free_count;	/* entries in array[] */
90     int		total_count;	/* total outstanding allocations incl free */
91     int		ary_count;	/* guarenteed allocation count */
92     int		max_count;	/* maximum count (M_NOWAIT used beyond nom) */
93     lwkt_token	token;
94     void	**array;	/* array[ary_count] */
95     void	(*construct)(void *buf, void *priv);
96     void	(*deconstruct)(void *buf, void *priv);
97     void	*priv;
98     struct thread *thread;	/* support thread for mpipe */
99     STAILQ_HEAD(, mpipe_callback) queue;
100 };
101 
102 #define MPF_CACHEDATA		0x0001	/* cache old buffers (do not zero) */
103 #define MPF_NOZERO		0x0002	/* do not zero-out new allocations */
104 #define MPF_INT			0x0004	/* use the interrupt memory reserve */
105 #define MPF_QUEUEWAIT		0x0008
106 #define MPF_CALLBACK		0x0010	/* callback will be used */
107 #define MPF_EXITING		0x80000000
108 
109 typedef struct malloc_pipe *malloc_pipe_t;
110 
111 #ifdef _KERNEL
112 
113 void mpipe_init(malloc_pipe_t mpipe, malloc_type_t type,
114 		int bytes, int nnom, int nmax,
115 		int mpflags,
116 		void (*construct)(void *, void *),
117 		void (*deconstruct)(void *, void *),
118 		void *priv);
119 void mpipe_done(malloc_pipe_t mpipe);
120 void *mpipe_alloc_waitok(malloc_pipe_t mpipe);
121 void *mpipe_alloc_nowait(malloc_pipe_t mpipe);
122 void *mpipe_alloc_callback(malloc_pipe_t mpipe,
123 		void (*func)(void *arg1, void *arg2), void *arg1, void *arg2);
124 void mpipe_wait(malloc_pipe_t mpipe);
125 void mpipe_free(malloc_pipe_t mpipe, void *vbuf);
126 
127 #endif
128 
129 #endif
130 
131