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