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