1 /*        $NetBSD: ebh_misc.h,v 1.2 2012/10/19 12:44:39 ttoth Exp $   */
2 
3 /*-
4  * Copyright (c) 2010 Department of Software Engineering,
5  *                        University of Szeged, Hungary
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to The NetBSD Foundation
9  * by the Department of Software Engineering, University of Szeged, Hungary
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 AUTHOR ``AS IS'' AND ANY EXPRESS OR
21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 #ifndef EBH_MISC_H_
34 #define EBH_MISC_H_
35 
36 /* EBH specific functions */
37 
38 #define CHFS_GET_MEMBER_POS(type, member)                                             \
39           ((unsigned long)(&((type *)0)->member))
40 
41 #define CHFS_GET_LID(lid) (le32toh(lid) & CHFS_LID_DIRTY_BIT_MASK)
42 
43 /*
44  * EBH_TREE_DESTROY - destroys an RB-tree and frees the memory of its elements.
45  */
46 #define EBH_TREE_DESTROY(name, head, type)                                            \
47           {                                                                                     \
48                     type *var, *nxt;                                                  \
49                     for (var = RB_MIN(name, head); var != NULL; var = nxt) {      \
50                               nxt = RB_NEXT(name, head, var);                                   \
51                               RB_REMOVE(name, head, var);                             \
52                               kmem_free(var, sizeof(type));                           \
53                     }                                                                           \
54           }
55 
56 /* XXX HACK! we need a clean solution for destroying mutexes in trees */
57 #define EBH_TREE_DESTROY_MUTEX(name, head, type)                            \
58           {                                                                                     \
59                     type *var, *nxt;                                                  \
60                     for (var = RB_MIN(name, head); var != NULL; var = nxt) {      \
61                               nxt = RB_NEXT(name, head, var);                                   \
62                               RB_REMOVE(name, head, var);                             \
63                               rw_destroy(&var->mutex);                                \
64                               kmem_free(var, sizeof(type));                           \
65                     }                                                                           \
66           }
67 
68 /*
69  * EBH_QUEUE_DESTROY - destroys a TAILQ and frees the memory of its elements.
70  */
71 #define EBH_QUEUE_DESTROY(head, type, entry)                                          \
72           {                                                                                     \
73                     type *var;                                                                  \
74                     while ((var = TAILQ_FIRST(head))) {                               \
75                               TAILQ_REMOVE(head, var, entry);                                   \
76                               kmem_free(var, sizeof(type));                           \
77                     }                                                                           \
78           }
79 
80 #endif /* EBH_MISC_H_ */
81