xref: /netbsd-src/sys/ufs/chfs/ebh_misc.h (revision bca84b9e1f0116a6e5e48923f6e7eb9be2c90ef8)
1*bca84b9eSttoth /*	$NetBSD: ebh_misc.h,v 1.2 2012/10/19 12:44:39 ttoth Exp $	*/
2288addd0Sahoka 
3288addd0Sahoka /*-
4288addd0Sahoka  * Copyright (c) 2010 Department of Software Engineering,
5288addd0Sahoka  *		      University of Szeged, Hungary
6288addd0Sahoka  * All rights reserved.
7288addd0Sahoka  *
8288addd0Sahoka  * This code is derived from software contributed to The NetBSD Foundation
9288addd0Sahoka  * by the Department of Software Engineering, University of Szeged, Hungary
10288addd0Sahoka  *
11288addd0Sahoka  * Redistribution and use in source and binary forms, with or without
12288addd0Sahoka  * modification, are permitted provided that the following conditions
13288addd0Sahoka  * are met:
14288addd0Sahoka  * 1. Redistributions of source code must retain the above copyright
15288addd0Sahoka  *    notice, this list of conditions and the following disclaimer.
16288addd0Sahoka  * 2. Redistributions in binary form must reproduce the above copyright
17288addd0Sahoka  *    notice, this list of conditions and the following disclaimer in the
18288addd0Sahoka  *    documentation and/or other materials provided with the distribution.
19288addd0Sahoka  *
20288addd0Sahoka  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21288addd0Sahoka  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22288addd0Sahoka  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23288addd0Sahoka  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24288addd0Sahoka  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25288addd0Sahoka  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26288addd0Sahoka  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27288addd0Sahoka  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28288addd0Sahoka  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29288addd0Sahoka  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30288addd0Sahoka  * SUCH DAMAGE.
31288addd0Sahoka  */
32288addd0Sahoka 
33288addd0Sahoka #ifndef EBH_MISC_H_
34288addd0Sahoka #define EBH_MISC_H_
35288addd0Sahoka 
36288addd0Sahoka /* EBH specific functions */
37288addd0Sahoka 
38288addd0Sahoka #define CHFS_GET_MEMBER_POS(type, member)				      \
39288addd0Sahoka 	((unsigned long)(&((type *)0)->member))
40288addd0Sahoka 
41288addd0Sahoka #define CHFS_GET_LID(lid) (le32toh(lid) & CHFS_LID_DIRTY_BIT_MASK)
42288addd0Sahoka 
43*bca84b9eSttoth /*
44288addd0Sahoka  * EBH_TREE_DESTROY - destroys an RB-tree and frees the memory of its elements.
45288addd0Sahoka  */
46288addd0Sahoka #define EBH_TREE_DESTROY(name, head, type)				      \
47288addd0Sahoka 	{								      \
48288addd0Sahoka 		type *var, *nxt;					      \
49288addd0Sahoka 		for (var = RB_MIN(name, head); var != NULL; var = nxt) {      \
50288addd0Sahoka 			nxt = RB_NEXT(name, head, var);			      \
51288addd0Sahoka 			RB_REMOVE(name, head, var);			      \
52288addd0Sahoka 			kmem_free(var, sizeof(type));			      \
53288addd0Sahoka 		}							      \
54288addd0Sahoka 	}
55288addd0Sahoka 
56288addd0Sahoka /* XXX HACK! we need a clean solution for destroying mutexes in trees */
57288addd0Sahoka #define EBH_TREE_DESTROY_MUTEX(name, head, type)			      \
58288addd0Sahoka 	{								      \
59288addd0Sahoka 		type *var, *nxt;					      \
60288addd0Sahoka 		for (var = RB_MIN(name, head); var != NULL; var = nxt) {      \
61288addd0Sahoka 			nxt = RB_NEXT(name, head, var);			      \
62288addd0Sahoka 			RB_REMOVE(name, head, var);			      \
63288addd0Sahoka 			rw_destroy(&var->mutex);			      \
64288addd0Sahoka 			kmem_free(var, sizeof(type));			      \
65288addd0Sahoka 		}							      \
66288addd0Sahoka 	}
67288addd0Sahoka 
68*bca84b9eSttoth /*
69288addd0Sahoka  * EBH_QUEUE_DESTROY - destroys a TAILQ and frees the memory of its elements.
70288addd0Sahoka  */
71288addd0Sahoka #define EBH_QUEUE_DESTROY(head, type, entry)				      \
72288addd0Sahoka 	{								      \
73288addd0Sahoka 		type *var;						      \
74288addd0Sahoka 		while ((var = TAILQ_FIRST(head))) {			      \
75288addd0Sahoka 			TAILQ_REMOVE(head, var, entry);			      \
76288addd0Sahoka 			kmem_free(var, sizeof(type));			      \
77288addd0Sahoka 		}							      \
78288addd0Sahoka 	}
79288addd0Sahoka 
80288addd0Sahoka #endif /* EBH_MISC_H_ */
81