2 * GLIB - Library of useful routines for C programming
3 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
5 * SPDX-License-Identifier: LGPL-2.1-or-later
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
22 * Modified by the GLib Team and others 1997-2000. See the AUTHORS
23 * file for a list of people on the GLib Team. See the ChangeLog
24 * files for a list of changes. These files are distributed with
25 * GLib at ftp://ftp.gtk.org/pub/gtk/.
29 * QTree is a partial import of Glib's GTree. The parts excluded correspond
30 * to API calls either deprecated (e.g. g_tree_traverse) or recently added
31 * (e.g. g_tree_search_node, added in 2.68); neither have callers in QEMU.
33 * The reason for this import is to allow us to control the memory allocator
34 * used by the tree implementation. Until Glib 2.75.3, GTree uses Glib's
35 * slice allocator, which causes problems when forking in user-mode;
36 * see https://gitlab.com/qemu-project/qemu/-/issues/285 and glib's
37 * "45b5a6c1e gslice: Remove slice allocator and use malloc() instead".
39 * TODO: remove QTree when QEMU's minimum Glib version is >= 2.75.3.
46 #ifdef HAVE_GLIB_WITH_SLICE_ALLOCATOR
48 typedef struct _QTree QTree
;
50 typedef struct _QTreeNode QTreeNode
;
52 typedef gboolean (*QTraverseNodeFunc
)(QTreeNode
*node
,
56 * Balanced binary trees
58 QTree
*q_tree_new(GCompareFunc key_compare_func
);
59 QTree
*q_tree_new_with_data(GCompareDataFunc key_compare_func
,
60 gpointer key_compare_data
);
61 QTree
*q_tree_new_full(GCompareDataFunc key_compare_func
,
62 gpointer key_compare_data
,
63 GDestroyNotify key_destroy_func
,
64 GDestroyNotify value_destroy_func
);
65 QTree
*q_tree_ref(QTree
*tree
);
66 void q_tree_unref(QTree
*tree
);
67 void q_tree_destroy(QTree
*tree
);
68 void q_tree_insert(QTree
*tree
,
71 void q_tree_replace(QTree
*tree
,
74 gboolean
q_tree_remove(QTree
*tree
,
76 gboolean
q_tree_steal(QTree
*tree
,
78 gpointer
q_tree_lookup(QTree
*tree
,
80 gboolean
q_tree_lookup_extended(QTree
*tree
,
81 gconstpointer lookup_key
,
84 void q_tree_foreach(QTree
*tree
,
87 gpointer
q_tree_search(QTree
*tree
,
88 GCompareFunc search_func
,
89 gconstpointer user_data
);
90 gint
q_tree_height(QTree
*tree
);
91 gint
q_tree_nnodes(QTree
*tree
);
93 #else /* !HAVE_GLIB_WITH_SLICE_ALLOCATOR */
96 typedef GTreeNode QTreeNode
;
97 typedef GTraverseNodeFunc QTraverseNodeFunc
;
99 static inline QTree
*q_tree_new(GCompareFunc key_compare_func
)
101 return g_tree_new(key_compare_func
);
104 static inline QTree
*q_tree_new_with_data(GCompareDataFunc key_compare_func
,
105 gpointer key_compare_data
)
107 return g_tree_new_with_data(key_compare_func
, key_compare_data
);
110 static inline QTree
*q_tree_new_full(GCompareDataFunc key_compare_func
,
111 gpointer key_compare_data
,
112 GDestroyNotify key_destroy_func
,
113 GDestroyNotify value_destroy_func
)
115 return g_tree_new_full(key_compare_func
, key_compare_data
,
116 key_destroy_func
, value_destroy_func
);
119 static inline QTree
*q_tree_ref(QTree
*tree
)
121 return g_tree_ref(tree
);
124 static inline void q_tree_unref(QTree
*tree
)
129 static inline void q_tree_destroy(QTree
*tree
)
131 g_tree_destroy(tree
);
134 static inline void q_tree_insert(QTree
*tree
,
138 g_tree_insert(tree
, key
, value
);
141 static inline void q_tree_replace(QTree
*tree
,
145 g_tree_replace(tree
, key
, value
);
148 static inline gboolean
q_tree_remove(QTree
*tree
,
151 return g_tree_remove(tree
, key
);
154 static inline gboolean
q_tree_steal(QTree
*tree
,
157 return g_tree_steal(tree
, key
);
160 static inline gpointer
q_tree_lookup(QTree
*tree
,
163 return g_tree_lookup(tree
, key
);
166 static inline gboolean
q_tree_lookup_extended(QTree
*tree
,
167 gconstpointer lookup_key
,
171 return g_tree_lookup_extended(tree
, lookup_key
, orig_key
, value
);
174 static inline void q_tree_foreach(QTree
*tree
,
178 return g_tree_foreach(tree
, func
, user_data
);
181 static inline gpointer
q_tree_search(QTree
*tree
,
182 GCompareFunc search_func
,
183 gconstpointer user_data
)
185 return g_tree_search(tree
, search_func
, user_data
);
188 static inline gint
q_tree_height(QTree
*tree
)
190 return g_tree_height(tree
);
193 static inline gint
q_tree_nnodes(QTree
*tree
)
195 return g_tree_nnodes(tree
);
198 #endif /* HAVE_GLIB_WITH_SLICE_ALLOCATOR */
200 #endif /* QEMU_QTREE_H */