xref: /netbsd/sys/sys/extent.h (revision bf9ec67e)
1 /*	$NetBSD: extent.h,v 1.10 1998/09/13 14:46:24 christos Exp $	*/
2 
3 /*-
4  * Copyright (c) 1996, 1998 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Jason R. Thorpe.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
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 the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by the NetBSD
21  *	Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 #ifndef _SYS_EXTENT_H_
40 #define _SYS_EXTENT_H_
41 
42 #include <sys/lock.h>
43 #include <sys/queue.h>
44 
45 struct extent_region {
46 	LIST_ENTRY(extent_region) er_link;	/* link in region list */
47 	u_long 	er_start;		/* start of region */
48 	u_long	er_end;			/* end of region */
49 	int	er_flags;		/* misc. flags */
50 };
51 
52 /* er_flags */
53 #define ER_ALLOC	0x01	/* region descriptor dynamically allocated */
54 
55 struct extent {
56 	const char *ex_name;		/* name of extent */
57 	struct simplelock ex_slock;	/* lock on this extent */
58 					/* allocated regions in extent */
59 	LIST_HEAD(, extent_region) ex_regions;
60 	u_long	ex_start;		/* start of extent */
61 	u_long	ex_end;			/* end of extent */
62 	int	ex_mtype;		/* memory type */
63 	int	ex_flags;		/* misc. information */
64 };
65 
66 struct extent_fixed {
67 	struct extent	fex_extent;	/* MUST BE FIRST */
68 					/* freelist of region descriptors */
69 	LIST_HEAD(, extent_region) fex_freelist;
70 	caddr_t		fex_storage;	/* storage space for descriptors */
71 	size_t		fex_storagesize; /* size of storage space */
72 };
73 
74 /* ex_flags; for internal use only */
75 #define EXF_FIXED	0x01		/* extent uses fixed storage */
76 #define EXF_NOCOALESCE	0x02		/* coalescing of regions not allowed */
77 #define EXF_WANTED	0x04		/* someone asleep on extent */
78 #define EXF_FLWANTED	0x08		/* someone asleep on freelist */
79 
80 #define EXF_BITS	"\20\4FLWANTED\3WANTED\2NOCOALESCE\1FIXED"
81 
82 /* misc. flags passed to extent functions */
83 #define EX_NOWAIT	0x00		/* not safe to sleep */
84 #define EX_WAITOK	0x01		/* safe to sleep */
85 #define EX_FAST		0x02		/* take first fit in extent_alloc() */
86 #define EX_CATCH	0x04		/* catch signals while sleeping */
87 #define EX_NOCOALESCE	0x08		/* create a non-coalescing extent */
88 #define EX_MALLOCOK	0x10		/* safe to call malloc() */
89 #define EX_WAITSPACE	0x20		/* wait for space to become free */
90 #define EX_BOUNDZERO	0x40		/* boundary lines start at 0 */
91 
92 /*
93  * Special place holders for "alignment" and "boundary" arguments,
94  * in the event the caller doesn't wish to use those features.
95  */
96 #define EX_NOALIGN	1		/* don't do alignment */
97 #define EX_NOBOUNDARY	0		/* don't do boundary checking */
98 
99 #if defined(_KERNEL) || defined(_EXTENT_TESTING)
100 #define EXTENT_FIXED_STORAGE_SIZE(_nregions)		\
101 	(ALIGN(sizeof(struct extent_fixed)) +		\
102 	((ALIGN(sizeof(struct extent_region))) *	\
103 	 (_nregions)))
104 
105 struct	extent *extent_create __P((const char *, u_long, u_long, int,
106 	    caddr_t, size_t, int));
107 void	extent_destroy __P((struct extent *));
108 int	extent_alloc_subregion1 __P((struct extent *, u_long, u_long,
109 	    u_long, u_long, u_long, u_long, int, u_long *));
110 int	extent_alloc_region __P((struct extent *, u_long, u_long, int));
111 int	extent_free __P((struct extent *, u_long, u_long, int));
112 void	extent_print __P((struct extent *));
113 
114 /* Simple case of extent_alloc_subregion() */
115 #define extent_alloc(_ex, _size, _alignment, _boundary, _flags, _result) \
116 	extent_alloc_subregion1((_ex), (_ex)->ex_start, (_ex)->ex_end,	\
117 	(_size), (_alignment), 0, (_boundary), (_flags), (_result))
118 #define extent_alloc1(_ex, _size, _alignment, _skew, _boundary, \
119 			_flags, _result) \
120 	extent_alloc_subregion1((_ex), (_ex)->ex_start, (_ex)->ex_end,	\
121 	(_size), (_alignment), (_skew), (_boundary), (_flags), (_result))
122 /* Compat version of extent_alloc_subregion() */
123 #define extent_alloc_subregion(_ex, _start, _end, _size, _alignment, \
124 				_boundary, _flags, _result) \
125 	extent_alloc_subregion1((_ex), (_start), (_end),	\
126 	(_size), (_alignment), 0, (_boundary), (_flags), (_result))
127 #endif /* _KERNEL || _EXTENT_TESTING */
128 
129 #endif /* ! _SYS_EXTENT_H_ */
130