glib.h revision ad5a0ac8a11793472af654411a522270a4f1e5a1
1// Copyright 2014 The Android Open Source Project
2//
3// This software is licensed under the terms of the GNU General Public
4// License version 2, as published by the Free Software Foundation, and
5// may be copied, distributed, and modified under those terms.
6//
7// This program is distributed in the hope that it will be useful,
8// but WITHOUT ANY WARRANTY; without even the implied warranty of
9// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10// GNU General Public License for more details.
11
12#ifndef GLIB_H
13#define GLIB_H
14
15#include <stdarg.h>
16#include <stddef.h>
17
18// Types
19
20typedef char gchar;
21typedef int gint;
22typedef unsigned int guint;
23typedef int gboolean;
24typedef void* gpointer;
25typedef const void* gconstpointer;
26
27typedef void (*GFunc)(gpointer data, gpointer user_data);
28
29typedef int (*GCompareFunc)(gconstpointer a,
30                            gconstpointer b);
31
32typedef int (*GCompareDataFunc)(gconstpointer a,
33                                gconstpointer b,
34                                gpointer user_data);
35
36typedef gboolean (*GEqualFunc)(gconstpointer a, gconstpointer b);
37
38typedef guint (*GHashFunc)(gconstpointer key);
39
40typedef void (*GHFunc)(gpointer key,
41                       gpointer value,
42                       gpointer user_data);
43
44typedef gboolean (*GHRFunc)(gpointer key,
45                            gpointer value,
46                            gpointer user_data);
47
48// Testing
49
50// TODO(digit): Turn assertions on.
51
52
53void g_panic(const char* fmt, ...) __attribute__((noreturn));
54
55#define g_assert(condition)  do { \
56    if (!(condition)) { \
57      g_panic("%s:%d: Assertion failure: %s\n", \
58              __FILE__, \
59              __LINE__, \
60              #condition); \
61      } \
62  } while (0)
63
64#define g_assert_not_reached()  \
65    g_panic("%s:%d: Assertion failure: NOT REACHED\n", __FILE__, __LINE__)
66
67// Heap allocation.
68void* g_malloc(size_t size);
69void* g_malloc0(size_t size);
70void* g_realloc(void* ptr, size_t size);
71void g_free(void* ptr);
72
73#define g_new(type, count)         ((type*) g_malloc(sizeof(type) * (count)))
74#define g_new0(type, count)        ((type*) g_malloc0(sizeof(type) * (count)))
75
76#define g_renew(type, mem, count)  \
77    ((type*) g_realloc((mem), sizeof(type) * (count)))
78
79// Strings.
80int g_vasprintf(char** str, const char* fmt, va_list args);
81char* g_strdup(const char* str);
82char* g_strndup(const char* str, size_t size);
83char* g_strdup_printf(const char* fmt, ...);
84char* g_strdup_vprintf(const char* fmt, va_list args);
85
86gboolean g_str_equal(const void* s1, const void* s2);
87guint g_str_hash(const void* str);
88
89// Atomic operations
90
91void g_atomic_int_inc(int volatile* atomic);
92
93gboolean g_atomic_int_dec_and_test(int volatile* atomic);
94
95// Single-linked lists
96
97typedef struct _GSList {
98  void* data;
99  struct _GSList* next;
100} GSList;
101
102void g_slist_free(GSList* list);
103GSList* g_slist_last(GSList* list);
104GSList* g_slist_find(GSList* list, gconstpointer data);
105GSList* g_slist_append(GSList* list, gpointer data);
106GSList* g_slist_prepend(GSList* list, gpointer data);
107GSList* g_slist_remove(GSList* list, gconstpointer data);
108void g_slist_foreach(GSList* list, GFunc func, gpointer user_data);
109GSList* g_slist_sort(GSList* list, GCompareFunc compare_func);
110
111// Hash tables
112
113typedef struct _GHashTable GHashTable;
114
115GHashTable* g_hash_table_new(GHashFunc hash_func,
116                             GEqualFunc key_equal_func);
117
118void g_hash_table_destroy(GHashTable* hash_table);
119
120void g_hash_table_insert(GHashTable* hash_table,
121                         void* key,
122                         void* value);
123
124void* g_hash_table_lookup(GHashTable* hash_table,
125                          const void* key);
126
127gboolean g_hash_table_remove(GHashTable* hash_table,
128                         const void* key);
129
130void g_hash_table_foreach(GHashTable* hash_table,
131                          GHFunc func,
132                          gpointer user_data);
133
134gpointer g_hash_table_find(GHashTable* hash_table,
135                           GHRFunc predicate,
136                           gpointer user_data);
137
138guint g_hash_table_size(GHashTable* hash_table);
139
140GHashTable* g_hash_table_ref(GHashTable* hash_table);
141
142void g_hash_table_unref(GHashTable* hash_table);
143
144
145#ifdef _WIN32
146char* g_win32_error_message(int error);
147#endif
148
149#endif  // GLIB_H
150