15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright 2014 The Android Open Source Project 25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This software is licensed under the terms of the GNU General Public 45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// License version 2, as published by the Free Software Foundation, and 55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// may be copied, distributed, and modified under those terms. 65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This program is distributed in the hope that it will be useful, 85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// but WITHOUT ANY WARRANTY; without even the implied warranty of 95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// GNU General Public License for more details. 115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef GLIB_H 135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define GLIB_H 145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <stdarg.h> 165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <stddef.h> 175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Types 195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 20cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)typedef char gchar; 21cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)typedef int gint; 225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)typedef unsigned int guint; 235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)typedef unsigned short gushort; 245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)typedef int gboolean; 2590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)typedef void* gpointer; 2690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)typedef const void* gconstpointer; 2790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 2890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)typedef void (*GFunc)(gpointer data, gpointer user_data); 2990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 3090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)typedef int (*GCompareFunc)(gconstpointer a, 3190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) gconstpointer b); 3290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 3390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)typedef int (*GCompareDataFunc)(gconstpointer a, 3490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) gconstpointer b, 3590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) gpointer user_data); 36cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 37cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)typedef gboolean (*GEqualFunc)(gconstpointer a, gconstpointer b); 3890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 3990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)typedef guint (*GHashFunc)(gconstpointer key); 4090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)typedef void (*GHFunc)(gpointer key, 425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) gpointer value, 435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) gpointer user_data); 44 45typedef gboolean (*GHRFunc)(gpointer key, 46 gpointer value, 47 gpointer user_data); 48 49// Constants. 50 51#ifdef _WIN32 52#define G_DIR_SEPARATOR_S "\\" 53#else 54#define G_DIR_SEPARATOR_S "/" 55#endif 56 57// Testing 58 59// TODO(digit): Turn assertions on. 60 61void g_critical(const char* fmt, ...); 62 63void g_panic(const char* fmt, ...) __attribute__((noreturn)); 64 65#define g_assert(condition) do { \ 66 if (!(condition)) { \ 67 g_panic("%s:%d: Assertion failure: %s\n", \ 68 __FILE__, \ 69 __LINE__, \ 70 #condition); \ 71 } \ 72 } while (0) 73 74#define g_assert_not_reached() \ 75 g_panic("%s:%d: Assertion failure: NOT REACHED\n", __FILE__, __LINE__) 76 77// Heap allocation. 78void* g_malloc(size_t size); 79void* g_malloc0(size_t size); 80void* g_realloc(void* ptr, size_t size); 81void g_free(void* ptr); 82 83#define g_new(type, count) ((type*) g_malloc(sizeof(type) * (count))) 84#define g_new0(type, count) ((type*) g_malloc0(sizeof(type) * (count))) 85 86#define g_renew(type, mem, count) \ 87 ((type*) g_realloc((mem), sizeof(type) * (count))) 88 89// Strings. 90int g_vasprintf(char** str, const char* fmt, va_list args); 91char* g_strdup(const char* str); 92char* g_strndup(const char* str, size_t size); 93char* g_strdup_printf(const char* fmt, ...); 94char* g_strdup_vprintf(const char* fmt, va_list args); 95 96char** g_strsplit(const char* str, const char* sep, int max_tokens); 97void g_strfreev(char** strings); 98 99gboolean g_str_equal(const void* s1, const void* s2); 100guint g_str_hash(const void* str); 101 102// Atomic operations 103 104void g_atomic_int_inc(int volatile* atomic); 105 106gboolean g_atomic_int_dec_and_test(int volatile* atomic); 107 108// Single-linked lists 109 110typedef struct _GSList { 111 void* data; 112 struct _GSList* next; 113} GSList; 114 115void g_slist_free(GSList* list); 116GSList* g_slist_last(GSList* list); 117GSList* g_slist_find(GSList* list, gconstpointer data); 118GSList* g_slist_append(GSList* list, gpointer data); 119GSList* g_slist_prepend(GSList* list, gpointer data); 120GSList* g_slist_remove(GSList* list, gconstpointer data); 121void g_slist_foreach(GSList* list, GFunc func, gpointer user_data); 122GSList* g_slist_sort(GSList* list, GCompareFunc compare_func); 123 124// Hash tables 125 126typedef struct _GHashTable GHashTable; 127 128GHashTable* g_hash_table_new(GHashFunc hash_func, 129 GEqualFunc key_equal_func); 130 131void g_hash_table_destroy(GHashTable* hash_table); 132 133void g_hash_table_insert(GHashTable* hash_table, 134 void* key, 135 void* value); 136 137void* g_hash_table_lookup(GHashTable* hash_table, 138 const void* key); 139 140gboolean g_hash_table_remove(GHashTable* hash_table, 141 const void* key); 142 143void g_hash_table_foreach(GHashTable* hash_table, 144 GHFunc func, 145 gpointer user_data); 146 147gpointer g_hash_table_find(GHashTable* hash_table, 148 GHRFunc predicate, 149 gpointer user_data); 150 151guint g_hash_table_size(GHashTable* hash_table); 152 153GHashTable* g_hash_table_ref(GHashTable* hash_table); 154 155void g_hash_table_unref(GHashTable* hash_table); 156 157 158// Queues 159 160typedef struct _GQueueNode GQueueNode; 161 162typedef struct _GQueue { 163 GQueueNode* head; 164 GQueueNode* tail; 165 guint length; 166} GQueue; 167 168GQueue* g_queue_new(void); 169 170void g_queue_free(GQueue* queue); 171 172void g_queue_push_tail(GQueue* queue, void* data); 173 174void* g_queue_peek_head(GQueue* queue); 175 176void* g_queue_pop_head(GQueue* queue); 177 178gboolean g_queue_is_empty(GQueue* queue); 179 180#ifdef _WIN32 181char* g_win32_error_message(int error); 182#endif 183 184// GSource etc... 185 186// Opaque data type. 187typedef struct GSource GSource; 188 189typedef gboolean (*GSourceFunc)(gpointer user_data); 190 191typedef struct { 192 gboolean (*prepare)(GSource* source, gint* timeout); 193 gboolean (*check)(GSource* source); 194 gboolean (*dispatch)(GSource* source, 195 GSourceFunc callback, 196 gpointer user_data); 197 void (*finalize)(GSource* source); 198} GSourceFuncs; 199 200typedef struct GPollFD { 201#if defined(_WIN32) && defined(__LP64__) 202 int64_t fd; 203#else 204 int fd; 205#endif 206 gushort events; 207 gushort revents; 208} GPollFD; 209 210#endif // GLIB_H 211