1 /* 2 glib_compat.h replacement functionality for glib code used in qemu 3 Copyright (C) 2016 Chris Eagle cseagle at gmail dot com 4 5 This program is free software; you can redistribute it and/or 6 modify it under the terms of the GNU General Public License 7 as published by the Free Software Foundation; either version 2 8 of the License, or (at your option) any later version. 9 10 This program is distributed in the hope that it will be useful, 11 but WITHOUT ANY WARRANTY; without even the implied warranty of 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 GNU General Public License for more details. 14 15 You should have received a copy of the GNU General Public License 16 along with this program; if not, write to the Free Software 17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 18 */ 19 20 #ifndef __GLIB_COMPAT_H 21 #define __GLIB_COMPAT_H 22 23 #include "unicorn/platform.h" 24 #include <stdarg.h> 25 #include <stdlib.h> 26 #include <assert.h> 27 28 #ifndef TRUE 29 #define TRUE 1 30 #endif 31 32 #ifndef FALSE 33 #define FALSE 0 34 #endif 35 36 #define g_assert(expr) assert(expr) 37 #define g_assert_not_reached() assert(0) 38 39 /* typedefs for glib related types that may still be referenced */ 40 typedef void* gpointer; 41 typedef const void *gconstpointer; 42 typedef int gint; 43 typedef uint32_t guint32; 44 typedef uint64_t guint64; 45 typedef unsigned int guint; 46 typedef char gchar; 47 typedef int gboolean; 48 typedef unsigned long gulong; 49 typedef unsigned long gsize; 50 51 typedef gint (*GCompareDataFunc)(gconstpointer a, 52 gconstpointer b, 53 gpointer user_data); 54 typedef void (*GFunc)(gpointer data, gpointer user_data); 55 typedef gint (*GCompareFunc)(gconstpointer v1, gconstpointer v2); 56 typedef void (*GDestroyNotify)(gpointer data); 57 58 guint g_str_hash(gconstpointer v); 59 gboolean g_str_equal(gconstpointer v1, gconstpointer v2); 60 guint g_int_hash(gconstpointer v); 61 62 gboolean g_int_equal(gconstpointer v1, gconstpointer v2); 63 64 typedef struct _GList { 65 gpointer data; 66 struct _GList *next; 67 struct _GList *prev; 68 } GList; 69 70 GList *g_list_first(GList *list); 71 void g_list_foreach(GList *list, GFunc func, gpointer user_data); 72 void g_list_free(GList *list); 73 GList *g_list_insert_sorted(GList *list, gpointer data, GCompareFunc compare); 74 #define g_list_next(list) (list->next) 75 GList *g_list_prepend(GList *list, gpointer data); 76 GList *g_list_remove_link(GList *list, GList *llink); 77 GList *g_list_sort(GList *list, GCompareFunc compare); 78 79 typedef struct _GSList { 80 gpointer data; 81 struct _GSList *next; 82 } GSList; 83 84 GSList *g_slist_append(GSList *list, gpointer data); 85 void g_slist_foreach(GSList *list, GFunc func, gpointer user_data); 86 void g_slist_free(GSList *list); 87 GSList *g_slist_prepend(GSList *list, gpointer data); 88 GSList *g_slist_sort(GSList *list, GCompareFunc compare); 89 90 typedef guint (*GHashFunc)(gconstpointer key); 91 typedef gboolean (*GEqualFunc)(gconstpointer a, gconstpointer b); 92 typedef void (*GHFunc)(gpointer key, gpointer value, gpointer user_data); 93 typedef gboolean (*GHRFunc)(gpointer key, gpointer value, gpointer user_data); 94 95 typedef struct _GHashTable GHashTable; 96 97 void g_hash_table_destroy(GHashTable *hash_table); 98 gpointer g_hash_table_find(GHashTable *hash_table, GHRFunc predicate, gpointer user_data); 99 void g_hash_table_foreach(GHashTable *hash_table, GHFunc func, gpointer user_data); 100 void g_hash_table_insert(GHashTable *hash_table, gpointer key, gpointer value); 101 gpointer g_hash_table_lookup(GHashTable *hash_table, gconstpointer key); 102 GHashTable *g_hash_table_new(GHashFunc hash_func, GEqualFunc key_equal_func); 103 GHashTable *g_hash_table_new_full(GHashFunc hash_func, GEqualFunc key_equal_func, 104 GDestroyNotify key_destroy_func, GDestroyNotify value_destroy_func); 105 void g_hash_table_remove_all(GHashTable *hash_table); 106 gboolean g_hash_table_remove(GHashTable *hash_table, gconstpointer key); 107 void g_hash_table_unref(GHashTable *hash_table); 108 GHashTable *g_hash_table_ref(GHashTable *hash_table); 109 guint g_hash_table_size(GHashTable *hash_table); 110 111 /* replacement for g_malloc dependency */ 112 void g_free(gpointer ptr); 113 gpointer g_malloc(size_t size); 114 gpointer g_malloc0(size_t size); 115 gpointer g_try_malloc0(size_t size); 116 gpointer g_realloc(gpointer ptr, size_t size); 117 char *g_strdup(const char *str); 118 char *g_strdup_printf(const char *format, ...); 119 char *g_strdup_vprintf(const char *format, va_list ap); 120 char *g_strndup(const char *str, size_t n); 121 void g_strfreev(char **v); 122 gpointer g_memdup(gconstpointer mem, size_t byte_size); 123 gpointer g_new_(size_t sz, size_t n_structs); 124 gpointer g_new0_(size_t sz, size_t n_structs); 125 gpointer g_renew_(size_t sz, gpointer mem, size_t n_structs); 126 gchar* g_strconcat (const gchar *string1, ...); 127 gchar** g_strsplit (const gchar *string, 128 const gchar *delimiter, 129 gint max_tokens); 130 131 132 #define g_new(struct_type, n_structs) ((struct_type*)g_new_(sizeof(struct_type), n_structs)) 133 #define g_new0(struct_type, n_structs) ((struct_type*)g_new0_(sizeof(struct_type), n_structs)) 134 #define g_renew(struct_type, mem, n_structs) ((struct_type*)g_renew_(sizeof(struct_type), mem, n_structs)) 135 136 #endif 137