Home | History | Annotate | Download | only in include
      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 
     20 typedef char gchar;
     21 typedef int gint;
     22 typedef unsigned int guint;
     23 typedef unsigned short gushort;
     24 typedef int gboolean;
     25 typedef void* gpointer;
     26 typedef const void* gconstpointer;
     27 
     28 typedef void (*GFunc)(gpointer data, gpointer user_data);
     29 
     30 typedef int (*GCompareFunc)(gconstpointer a,
     31                             gconstpointer b);
     32 
     33 typedef int (*GCompareDataFunc)(gconstpointer a,
     34                                 gconstpointer b,
     35                                 gpointer user_data);
     36 
     37 typedef gboolean (*GEqualFunc)(gconstpointer a, gconstpointer b);
     38 
     39 typedef guint (*GHashFunc)(gconstpointer key);
     40 
     41 typedef void (*GHFunc)(gpointer key,
     42                        gpointer value,
     43                        gpointer user_data);
     44 
     45 typedef 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 
     61 void g_critical(const char* fmt, ...);
     62 
     63 void 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.
     78 void* g_malloc(size_t size);
     79 void* g_malloc0(size_t size);
     80 void* g_realloc(void* ptr, size_t size);
     81 void 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.
     90 int g_vasprintf(char** str, const char* fmt, va_list args);
     91 char* g_strdup(const char* str);
     92 char* g_strndup(const char* str, size_t size);
     93 char* g_strdup_printf(const char* fmt, ...);
     94 char* g_strdup_vprintf(const char* fmt, va_list args);
     95 
     96 char** g_strsplit(const char* str, const char* sep, int max_tokens);
     97 void g_strfreev(char** strings);
     98 
     99 gboolean g_str_equal(const void* s1, const void* s2);
    100 guint g_str_hash(const void* str);
    101 
    102 // Atomic operations
    103 
    104 void g_atomic_int_inc(int volatile* atomic);
    105 
    106 gboolean g_atomic_int_dec_and_test(int volatile* atomic);
    107 
    108 // Single-linked lists
    109 
    110 typedef struct _GSList {
    111   void* data;
    112   struct _GSList* next;
    113 } GSList;
    114 
    115 void g_slist_free(GSList* list);
    116 GSList* g_slist_last(GSList* list);
    117 GSList* g_slist_find(GSList* list, gconstpointer data);
    118 GSList* g_slist_append(GSList* list, gpointer data);
    119 GSList* g_slist_prepend(GSList* list, gpointer data);
    120 GSList* g_slist_remove(GSList* list, gconstpointer data);
    121 void g_slist_foreach(GSList* list, GFunc func, gpointer user_data);
    122 GSList* g_slist_sort(GSList* list, GCompareFunc compare_func);
    123 
    124 // Hash tables
    125 
    126 typedef struct _GHashTable GHashTable;
    127 
    128 GHashTable* g_hash_table_new(GHashFunc hash_func,
    129                              GEqualFunc key_equal_func);
    130 
    131 void g_hash_table_destroy(GHashTable* hash_table);
    132 
    133 void g_hash_table_insert(GHashTable* hash_table,
    134                          void* key,
    135                          void* value);
    136 
    137 void* g_hash_table_lookup(GHashTable* hash_table,
    138                           const void* key);
    139 
    140 gboolean g_hash_table_remove(GHashTable* hash_table,
    141                          const void* key);
    142 
    143 void g_hash_table_foreach(GHashTable* hash_table,
    144                           GHFunc func,
    145                           gpointer user_data);
    146 
    147 gpointer g_hash_table_find(GHashTable* hash_table,
    148                            GHRFunc predicate,
    149                            gpointer user_data);
    150 
    151 guint g_hash_table_size(GHashTable* hash_table);
    152 
    153 GHashTable* g_hash_table_ref(GHashTable* hash_table);
    154 
    155 void g_hash_table_unref(GHashTable* hash_table);
    156 
    157 
    158 // Queues
    159 
    160 typedef struct _GQueueNode GQueueNode;
    161 
    162 typedef struct _GQueue {
    163   GQueueNode* head;
    164   GQueueNode* tail;
    165   guint length;
    166 } GQueue;
    167 
    168 GQueue* g_queue_new(void);
    169 
    170 void g_queue_free(GQueue* queue);
    171 
    172 void g_queue_push_tail(GQueue* queue, void* data);
    173 
    174 void* g_queue_peek_head(GQueue* queue);
    175 
    176 void* g_queue_pop_head(GQueue* queue);
    177 
    178 gboolean g_queue_is_empty(GQueue* queue);
    179 
    180 #ifdef _WIN32
    181 char* g_win32_error_message(int error);
    182 #endif
    183 
    184 // GSource etc...
    185 
    186 // Opaque data type.
    187 typedef struct GSource GSource;
    188 
    189 typedef gboolean (*GSourceFunc)(gpointer user_data);
    190 
    191 typedef 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 
    200 typedef 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