xref: /openbmc/qemu/include/qemu/qtree.h (revision e3feb2cc)
1*e3feb2ccSEmilio Cota /*
2*e3feb2ccSEmilio Cota  * GLIB - Library of useful routines for C programming
3*e3feb2ccSEmilio Cota  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
4*e3feb2ccSEmilio Cota  *
5*e3feb2ccSEmilio Cota  * SPDX-License-Identifier: LGPL-2.1-or-later
6*e3feb2ccSEmilio Cota  *
7*e3feb2ccSEmilio Cota  * This library is free software; you can redistribute it and/or
8*e3feb2ccSEmilio Cota  * modify it under the terms of the GNU Lesser General Public
9*e3feb2ccSEmilio Cota  * License as published by the Free Software Foundation; either
10*e3feb2ccSEmilio Cota  * version 2.1 of the License, or (at your option) any later version.
11*e3feb2ccSEmilio Cota  *
12*e3feb2ccSEmilio Cota  * This library is distributed in the hope that it will be useful,
13*e3feb2ccSEmilio Cota  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14*e3feb2ccSEmilio Cota  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15*e3feb2ccSEmilio Cota  * Lesser General Public License for more details.
16*e3feb2ccSEmilio Cota  *
17*e3feb2ccSEmilio Cota  * You should have received a copy of the GNU Lesser General Public
18*e3feb2ccSEmilio Cota  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
19*e3feb2ccSEmilio Cota  */
20*e3feb2ccSEmilio Cota 
21*e3feb2ccSEmilio Cota /*
22*e3feb2ccSEmilio Cota  * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
23*e3feb2ccSEmilio Cota  * file for a list of people on the GLib Team.  See the ChangeLog
24*e3feb2ccSEmilio Cota  * files for a list of changes.  These files are distributed with
25*e3feb2ccSEmilio Cota  * GLib at ftp://ftp.gtk.org/pub/gtk/.
26*e3feb2ccSEmilio Cota  */
27*e3feb2ccSEmilio Cota 
28*e3feb2ccSEmilio Cota /*
29*e3feb2ccSEmilio Cota  * QTree is a partial import of Glib's GTree. The parts excluded correspond
30*e3feb2ccSEmilio Cota  * to API calls either deprecated (e.g. g_tree_traverse) or recently added
31*e3feb2ccSEmilio Cota  * (e.g. g_tree_search_node, added in 2.68); neither have callers in QEMU.
32*e3feb2ccSEmilio Cota  *
33*e3feb2ccSEmilio Cota  * The reason for this import is to allow us to control the memory allocator
34*e3feb2ccSEmilio Cota  * used by the tree implementation. Until Glib 2.75.3, GTree uses Glib's
35*e3feb2ccSEmilio Cota  * slice allocator, which causes problems when forking in user-mode;
36*e3feb2ccSEmilio Cota  * see https://gitlab.com/qemu-project/qemu/-/issues/285 and glib's
37*e3feb2ccSEmilio Cota  * "45b5a6c1e gslice: Remove slice allocator and use malloc() instead".
38*e3feb2ccSEmilio Cota  *
39*e3feb2ccSEmilio Cota  * TODO: remove QTree when QEMU's minimum Glib version is >= 2.75.3.
40*e3feb2ccSEmilio Cota  */
41*e3feb2ccSEmilio Cota 
42*e3feb2ccSEmilio Cota #ifndef QEMU_QTREE_H
43*e3feb2ccSEmilio Cota #define QEMU_QTREE_H
44*e3feb2ccSEmilio Cota 
45*e3feb2ccSEmilio Cota #include "qemu/osdep.h"
46*e3feb2ccSEmilio Cota 
47*e3feb2ccSEmilio Cota #ifdef HAVE_GLIB_WITH_SLICE_ALLOCATOR
48*e3feb2ccSEmilio Cota 
49*e3feb2ccSEmilio Cota typedef struct _QTree  QTree;
50*e3feb2ccSEmilio Cota 
51*e3feb2ccSEmilio Cota typedef struct _QTreeNode QTreeNode;
52*e3feb2ccSEmilio Cota 
53*e3feb2ccSEmilio Cota typedef gboolean (*QTraverseNodeFunc)(QTreeNode *node,
54*e3feb2ccSEmilio Cota                                       gpointer user_data);
55*e3feb2ccSEmilio Cota 
56*e3feb2ccSEmilio Cota /*
57*e3feb2ccSEmilio Cota  * Balanced binary trees
58*e3feb2ccSEmilio Cota  */
59*e3feb2ccSEmilio Cota QTree *q_tree_new(GCompareFunc key_compare_func);
60*e3feb2ccSEmilio Cota QTree *q_tree_new_with_data(GCompareDataFunc key_compare_func,
61*e3feb2ccSEmilio Cota                             gpointer key_compare_data);
62*e3feb2ccSEmilio Cota QTree *q_tree_new_full(GCompareDataFunc key_compare_func,
63*e3feb2ccSEmilio Cota                        gpointer key_compare_data,
64*e3feb2ccSEmilio Cota                        GDestroyNotify key_destroy_func,
65*e3feb2ccSEmilio Cota                        GDestroyNotify value_destroy_func);
66*e3feb2ccSEmilio Cota QTree *q_tree_ref(QTree *tree);
67*e3feb2ccSEmilio Cota void q_tree_unref(QTree *tree);
68*e3feb2ccSEmilio Cota void q_tree_destroy(QTree *tree);
69*e3feb2ccSEmilio Cota void q_tree_insert(QTree *tree,
70*e3feb2ccSEmilio Cota                    gpointer key,
71*e3feb2ccSEmilio Cota                    gpointer value);
72*e3feb2ccSEmilio Cota void q_tree_replace(QTree *tree,
73*e3feb2ccSEmilio Cota                     gpointer key,
74*e3feb2ccSEmilio Cota                     gpointer value);
75*e3feb2ccSEmilio Cota gboolean q_tree_remove(QTree *tree,
76*e3feb2ccSEmilio Cota                        gconstpointer key);
77*e3feb2ccSEmilio Cota gboolean q_tree_steal(QTree *tree,
78*e3feb2ccSEmilio Cota                       gconstpointer key);
79*e3feb2ccSEmilio Cota gpointer q_tree_lookup(QTree *tree,
80*e3feb2ccSEmilio Cota                        gconstpointer key);
81*e3feb2ccSEmilio Cota gboolean q_tree_lookup_extended(QTree *tree,
82*e3feb2ccSEmilio Cota                                 gconstpointer lookup_key,
83*e3feb2ccSEmilio Cota                                 gpointer *orig_key,
84*e3feb2ccSEmilio Cota                                 gpointer *value);
85*e3feb2ccSEmilio Cota void q_tree_foreach(QTree *tree,
86*e3feb2ccSEmilio Cota                     GTraverseFunc func,
87*e3feb2ccSEmilio Cota                     gpointer user_data);
88*e3feb2ccSEmilio Cota gpointer q_tree_search(QTree *tree,
89*e3feb2ccSEmilio Cota                        GCompareFunc search_func,
90*e3feb2ccSEmilio Cota                        gconstpointer user_data);
91*e3feb2ccSEmilio Cota gint q_tree_height(QTree *tree);
92*e3feb2ccSEmilio Cota gint q_tree_nnodes(QTree *tree);
93*e3feb2ccSEmilio Cota 
94*e3feb2ccSEmilio Cota #else /* !HAVE_GLIB_WITH_SLICE_ALLOCATOR */
95*e3feb2ccSEmilio Cota 
96*e3feb2ccSEmilio Cota typedef GTree QTree;
97*e3feb2ccSEmilio Cota typedef GTreeNode QTreeNode;
98*e3feb2ccSEmilio Cota typedef GTraverseNodeFunc QTraverseNodeFunc;
99*e3feb2ccSEmilio Cota 
q_tree_new(GCompareFunc key_compare_func)100*e3feb2ccSEmilio Cota static inline QTree *q_tree_new(GCompareFunc key_compare_func)
101*e3feb2ccSEmilio Cota {
102*e3feb2ccSEmilio Cota     return g_tree_new(key_compare_func);
103*e3feb2ccSEmilio Cota }
104*e3feb2ccSEmilio Cota 
q_tree_new_with_data(GCompareDataFunc key_compare_func,gpointer key_compare_data)105*e3feb2ccSEmilio Cota static inline QTree *q_tree_new_with_data(GCompareDataFunc key_compare_func,
106*e3feb2ccSEmilio Cota                                           gpointer key_compare_data)
107*e3feb2ccSEmilio Cota {
108*e3feb2ccSEmilio Cota     return g_tree_new_with_data(key_compare_func, key_compare_data);
109*e3feb2ccSEmilio Cota }
110*e3feb2ccSEmilio Cota 
q_tree_new_full(GCompareDataFunc key_compare_func,gpointer key_compare_data,GDestroyNotify key_destroy_func,GDestroyNotify value_destroy_func)111*e3feb2ccSEmilio Cota static inline QTree *q_tree_new_full(GCompareDataFunc key_compare_func,
112*e3feb2ccSEmilio Cota                                      gpointer key_compare_data,
113*e3feb2ccSEmilio Cota                                      GDestroyNotify key_destroy_func,
114*e3feb2ccSEmilio Cota                                      GDestroyNotify value_destroy_func)
115*e3feb2ccSEmilio Cota {
116*e3feb2ccSEmilio Cota     return g_tree_new_full(key_compare_func, key_compare_data,
117*e3feb2ccSEmilio Cota                            key_destroy_func, value_destroy_func);
118*e3feb2ccSEmilio Cota }
119*e3feb2ccSEmilio Cota 
q_tree_ref(QTree * tree)120*e3feb2ccSEmilio Cota static inline QTree *q_tree_ref(QTree *tree)
121*e3feb2ccSEmilio Cota {
122*e3feb2ccSEmilio Cota     return g_tree_ref(tree);
123*e3feb2ccSEmilio Cota }
124*e3feb2ccSEmilio Cota 
q_tree_unref(QTree * tree)125*e3feb2ccSEmilio Cota static inline void q_tree_unref(QTree *tree)
126*e3feb2ccSEmilio Cota {
127*e3feb2ccSEmilio Cota     g_tree_unref(tree);
128*e3feb2ccSEmilio Cota }
129*e3feb2ccSEmilio Cota 
q_tree_destroy(QTree * tree)130*e3feb2ccSEmilio Cota static inline void q_tree_destroy(QTree *tree)
131*e3feb2ccSEmilio Cota {
132*e3feb2ccSEmilio Cota     g_tree_destroy(tree);
133*e3feb2ccSEmilio Cota }
134*e3feb2ccSEmilio Cota 
q_tree_insert(QTree * tree,gpointer key,gpointer value)135*e3feb2ccSEmilio Cota static inline void q_tree_insert(QTree *tree,
136*e3feb2ccSEmilio Cota                                  gpointer key,
137*e3feb2ccSEmilio Cota                                  gpointer value)
138*e3feb2ccSEmilio Cota {
139*e3feb2ccSEmilio Cota     g_tree_insert(tree, key, value);
140*e3feb2ccSEmilio Cota }
141*e3feb2ccSEmilio Cota 
q_tree_replace(QTree * tree,gpointer key,gpointer value)142*e3feb2ccSEmilio Cota static inline void q_tree_replace(QTree *tree,
143*e3feb2ccSEmilio Cota                                   gpointer key,
144*e3feb2ccSEmilio Cota                                   gpointer value)
145*e3feb2ccSEmilio Cota {
146*e3feb2ccSEmilio Cota     g_tree_replace(tree, key, value);
147*e3feb2ccSEmilio Cota }
148*e3feb2ccSEmilio Cota 
q_tree_remove(QTree * tree,gconstpointer key)149*e3feb2ccSEmilio Cota static inline gboolean q_tree_remove(QTree *tree,
150*e3feb2ccSEmilio Cota                                      gconstpointer key)
151*e3feb2ccSEmilio Cota {
152*e3feb2ccSEmilio Cota     return g_tree_remove(tree, key);
153*e3feb2ccSEmilio Cota }
154*e3feb2ccSEmilio Cota 
q_tree_steal(QTree * tree,gconstpointer key)155*e3feb2ccSEmilio Cota static inline gboolean q_tree_steal(QTree *tree,
156*e3feb2ccSEmilio Cota                                     gconstpointer key)
157*e3feb2ccSEmilio Cota {
158*e3feb2ccSEmilio Cota     return g_tree_steal(tree, key);
159*e3feb2ccSEmilio Cota }
160*e3feb2ccSEmilio Cota 
q_tree_lookup(QTree * tree,gconstpointer key)161*e3feb2ccSEmilio Cota static inline gpointer q_tree_lookup(QTree *tree,
162*e3feb2ccSEmilio Cota                                      gconstpointer key)
163*e3feb2ccSEmilio Cota {
164*e3feb2ccSEmilio Cota     return g_tree_lookup(tree, key);
165*e3feb2ccSEmilio Cota }
166*e3feb2ccSEmilio Cota 
q_tree_lookup_extended(QTree * tree,gconstpointer lookup_key,gpointer * orig_key,gpointer * value)167*e3feb2ccSEmilio Cota static inline gboolean q_tree_lookup_extended(QTree *tree,
168*e3feb2ccSEmilio Cota                                               gconstpointer lookup_key,
169*e3feb2ccSEmilio Cota                                               gpointer *orig_key,
170*e3feb2ccSEmilio Cota                                               gpointer *value)
171*e3feb2ccSEmilio Cota {
172*e3feb2ccSEmilio Cota     return g_tree_lookup_extended(tree, lookup_key, orig_key, value);
173*e3feb2ccSEmilio Cota }
174*e3feb2ccSEmilio Cota 
q_tree_foreach(QTree * tree,GTraverseFunc func,gpointer user_data)175*e3feb2ccSEmilio Cota static inline void q_tree_foreach(QTree *tree,
176*e3feb2ccSEmilio Cota                                   GTraverseFunc func,
177*e3feb2ccSEmilio Cota                                   gpointer user_data)
178*e3feb2ccSEmilio Cota {
179*e3feb2ccSEmilio Cota     return g_tree_foreach(tree, func, user_data);
180*e3feb2ccSEmilio Cota }
181*e3feb2ccSEmilio Cota 
q_tree_search(QTree * tree,GCompareFunc search_func,gconstpointer user_data)182*e3feb2ccSEmilio Cota static inline gpointer q_tree_search(QTree *tree,
183*e3feb2ccSEmilio Cota                                      GCompareFunc search_func,
184*e3feb2ccSEmilio Cota                                      gconstpointer user_data)
185*e3feb2ccSEmilio Cota {
186*e3feb2ccSEmilio Cota     return g_tree_search(tree, search_func, user_data);
187*e3feb2ccSEmilio Cota }
188*e3feb2ccSEmilio Cota 
q_tree_height(QTree * tree)189*e3feb2ccSEmilio Cota static inline gint q_tree_height(QTree *tree)
190*e3feb2ccSEmilio Cota {
191*e3feb2ccSEmilio Cota     return g_tree_height(tree);
192*e3feb2ccSEmilio Cota }
193*e3feb2ccSEmilio Cota 
q_tree_nnodes(QTree * tree)194*e3feb2ccSEmilio Cota static inline gint q_tree_nnodes(QTree *tree)
195*e3feb2ccSEmilio Cota {
196*e3feb2ccSEmilio Cota     return g_tree_nnodes(tree);
197*e3feb2ccSEmilio Cota }
198*e3feb2ccSEmilio Cota 
199*e3feb2ccSEmilio Cota #endif /* HAVE_GLIB_WITH_SLICE_ALLOCATOR */
200*e3feb2ccSEmilio Cota 
201*e3feb2ccSEmilio Cota #endif /* QEMU_QTREE_H */
202