Glib

First test if you have the right enviroment:

 pkg-config gtk+-2.0 --cflags --libs
should give some pathes. Otherwise install
 apt-get install libgtk2.0-dev
 gint g_ascii_strcasecmp(const gchar *str1, const gchar *str2);
like G_OS_WIN32, G_OS_UNIX, G_DIR_SEPARATOR
#define G_E     2.7182818284590452353602874713526624977572470937000
->The base of natural logarithms.

#define G_LN2   0.69314718055994530941723212145817656807550013436026
-> The natural logarithm of 2.

#define G_LN10  2.3025850929940456840179914546843642076011014886288
->The natural logarithm of 10.

#define G_PI    3.1415926535897932384626433832795028841971693993751
->The value of pi (ratio of circle's circumference to its diameter).

#define G_PI_2  1.5707963267948966192313216916397514420985846996876
->Pi devided by 2

#define G_PI_4  1.5707963267948966192313216916397514420985846996876
->Pi devided by 4

#define G_SQRT2 1.4142135623730950488016887242096980785696718753769
->The square root of two.

Message Logging

  1. define g_message (...)
  2. define g_warning (...)
  3. define g_critical (...)
  4. define g_error (...)
  5. define g_debug (...)

Memory Management

Memory Slices

  1. define FIX_LENGTH 16
 gchar *strings[256];
 for(i=0;i<256;i++)
   strings[i] = g_slice_alloc(FIX_LENGTH);
 for(i=0;i<256;i++)
   g_slice_free1(FIX_LENGTH, strings[i]);

g_slice_alloc0() allocates memory with initialization with zero.

When you need to allocate only a single instance of an object, g_slice_new() is available.

 GtkWindow *win = g_slice_new(GtkWindow);
 g_slice_free(GtkWindow, win);
where last function is not a function but a macro:
 #define g_slice_new(type) ((type*) g_slice_alloc(sizeof(type))

Memory Allocation

 struct_type* g_new(struct_type, number_of_structs);
 gpointer g_malloc(gulong number_of_bytes);
 gpointer g_try_malloc(gulong number_of_bytes);
 void g_free(gpointer memory);
 g_memmove()

Memory Profiling

 g_mem_profile()

Before using memory profiling, you must always set the GMemVTable

 GSList *list = NULL;
 g_mem_set_vtable(glib_mem_profiler_tyble);
 g_atexit(g_mem_profile);
 /* some heap operations */
 return 0;
 /* if main exits g_mem_profile() will be called */

Enviroment Variables

 const gchar*        g_get_user_name                     (void);
 const gchar*        g_get_real_name                     (void);
 const gchar*        g_get_user_cache_dir                (void);
 const gchar*        g_get_user_data_dir                 (void);
 const gchar*        g_get_user_config_dir               (void);
 const gchar*        g_get_host_name                     (void);
 const gchar*        g_get_home_dir                      (void);
 const gchar*        g_get_tmp_dir                       (void);
 gchar*              g_get_current_dir                   (void);

 const gchar*        g_getenv                            (const gchar *variable);
 gboolean            g_setenv                            (const gchar *variable,
                                                         const gchar *value,
                                                         gboolean overwrite);

Timers

Timers — keep track of elapsed time

 GTimer*             g_timer_new                         (void);
 void                g_timer_start                       (GTimer *timer);
 void                g_timer_stop                        (GTimer *timer);
 void                g_timer_continue                    (GTimer *timer);
 gdouble             g_timer_elapsed                     (GTimer *timer,
                                                         gulong *microseconds);
 void                g_timer_reset                       (GTimer *timer);
 void                g_timer_destroy                     (GTimer *timer);
 gboolean            g_file_get_contents                 (const gchar *filename,
                                                         gchar **contents,
                                                         gsize *length,
                                                         GError **error);
 gboolean            g_file_set_contents                 (const gchar *filename,
                                                         const gchar *contents,
                                                         gssize length,
                                                         GError **error);
 gboolean            g_file_test                         (const gchar *filename,
                                                         GFileTest test);
 if(!g_file_test(g_get_home_dir(), G_FILE_TEST_IS_DIR))
    g_error("Error: You do not have a home directory!");
 while((file=g_dir_read_name(dir)))
   g_print("%s\n", file);
 g_dir_close(dir);

  G_FILE_TEST_IS_REGULAR    = 1 << 0,
  G_FILE_TEST_IS_SYMLINK    = 1 << 1,
  G_FILE_TEST_IS_DIR        = 1 << 2,
  G_FILE_TEST_IS_EXECUTABLE = 1 << 3,
  G_FILE_TEST_EXISTS        = 1 << 4

For reference of functions as g_open(), g_rename(), g_mkdir(), g_stat(), g_unlink(), g_remove(), g_rmdir(), g_fopen(), g_chmod(), g_access(), g_creat(), g_chdir() visit api reference.

Main Loop

gtk_init() creates a GLib main loop for you. The GTK+ main loop is invoked with gtk_main(). This function can actually be called multiple timers; the call on the top of the stack is removed when you call gtk_main_quit(). You can retrieve the current main loop stack level with gtk_main_level().

The GLib main loop is implemented as a number of structures, which allow multiple instances to be run concurrently. GMainContext is used to represent a number of event sources. Each thread has its own context, which can be retrieved with g_main_context_get() or the default context g_main_context_get_default().

GLib also provides GMainLoop which represents one instance of the main loop. A new main loop can be created with g_main_loop_new(), where a NULL context will use the default. Call g_main_llop_run() to start loop and to set is_running to TRUE.

The gtk+ main lop implements the GLib main loop by creating a GMainLoop with the default context in gtk_main(). In short, the main loop functionality provided by functions in gtk+ is implemented in GLib.

GLib supports the ability to create new event sources. Deriving from GSource creates new sources. GLib provides the ability to create new timeout and idle function sources with g_timeout_source_new() and g_idle_source_new().

g_source_new() accepts a table of functions and the structure size of the new source. These functions are used to define the behavior of the new source type. You should then associate the source with a GMainContext by calling g_source_attach().

For more look here.

Timeouts

Timeout functions are added with g_timeout_add() or g_timeout_add_full(). The only difference between these two functions is that the latter allows you to specify a GDestroyNotify function, which will be called when you return FALSE to remove the timeout function.

The first parameter of g_timeout_add_full() allows you to fefine the priority of the timeout. In most cases, you will want to use G_PRIORITY_DEFAULT as the the timeout function's priority. A list of the available priorities follows:

 ...
 progress = gtk_progress_bar_new();
 gtk_progress_bar_set_pulse_step(GTK_PROGRESS_BAR(progress), 0.1);
 g_timeout_add(100, (GSourceFunc) pulse_progress, (gpointer) progress);
 gtk_container_add(GTK_CONTAINER(window), progress);
 ...

Idle Functions

A special type of function called an idle function that will be called when there are no events pending with a higher priority. They run over and over when there is nothing else to do in the main loop.

Idle functions are added with g_idle_add() or g_idle_add_full(). Only difference latter allows you to specify a destroy function and a priority instead of using the default of G_PRIORITY_DEFAULT_IDLE. In almost all cases, idle functions should have a priority of G_PRIORITY_HIGH_IDLE or G_PRIORITY_DEFAULT_IDLE.

Destroy functions should not return any value but do receive a gpointer as teir parameter. This gpointer is the same value originally received by the idle function, which gives you an opportunity to free it from memory.

You can remove an idle function be returning FALSE from the callback or use g_idle_remove_data().

Data Types

Strings

 typedef struct 
 {
   gchar *str;
   gsize len;
   gsize allocated_len;
 } GString;

 GString* g_string_new(const gchar*);
 GString* g_string_new_len(const gchar* init_str, gssize length);
 GString* g_string_sized_new(gsize default_size);
 void g_string_printf(GString *string, const gchar *frm, ...);
 GString* g_string_append();
 GString* g_string_append_len();
 GString* g_string_append_c();
 GString* g_string_append_unichar();
 GString* g_string_erase();
 gchar* g_string_free();

Linked Lists

 typedef struct
 {
   gpointer data;
   GSList *next;
 } GSList;

 typedef struct
 {
   gpointer data;
   GList *next;
   GList *prev;
 } GList;

 g_list_prepend();
 g_list_insert();
 g_list_remove();
 g_list_free();
 g_list_sort();
 g_list_find();
 g_list_foreach();

Balanced Binary Trees

 g_tree_new();
 g_tree_new_with_data();
 g_tree_new_full();
 g_tree_insert();
 g_tree_lookup();
 g_tree_foreach();
 g_tree_search();
 g_tree_remove();

N-ary Trees

 typedef struct 
 {
   gpointer data;
   GNode *next;
   GNode *prev;
   GNode *parent;
   GNode *children;
 } GNode;

 g_node_append()
 g_node_append_data()
 g_node_insert()
 g_node_insert_after()
 g_node_insert_before()
 g_node_insert_data()
 g_node_insert_data_before()
 g_node_prepend()
 g_node_prepend_data()

 g_node_traverse()
 GTraverseType:
 G_IN_ORDER
 G_PRE_ORDER
 G_POST_ORDER
 G_LEVEL_ORDER

 GTraverseFlags:
 G_TRAVERSE_LEAVES
 G_TRAVERSE_NON_LEAVES
 G_TRAVERSE_ALL
 G_TRAVERSE_MASK

 g_node_destroy(node_root);

Arrays

GArray

 g_array_sized_new()
 g_array_new();
 g_array_append_vals();
 g_array_append_val()
 g_array_append_vals();
 g_array_remove_index();
 g_array_remove_range();
 g_array_sort();
 g_array_free();

Pointer Arrays

 GPtrArray
 g_ptr_array_remove();

Byte Arrays

 GByteArray
 g_byte_array_free()

Hash Tables

 GHashTable* g_hash_table_new();
 g_hash_table_insert();
 g_hash_table_remove();
 g_hash_table_lookup();
 g_hash_table_destroy();

Quarks

 typedef guint32 GQuark;
 GQuark g_quark_from_string();
 g_quark_to_string();

Keyed Data Lists

Akeyed data list is a special type of linked list that uses quarks for indexing.

 void g_datalist_init(GData **datalist);
 void g_datalist_id_set_data_full();
 g_datalist_id_remove_no_notify();
 g_datalist_foreach()

Input-Output Channels

Only for UNIX like machines because file descriptor and socket domains overlap in Windows.

fork() on Unix

 switch(fork())
 {
   case -1:
    g_error("error");
   case 0:
     g_message("child process!");
   default:
     gint status_of_child;
     wait(&status_of_child);
 }

Spawning Processes

The GIOChannel example used pipe() and fork() to set up the communication between the applications. That is not cross-platform, because some commands will not be supported on Microsoft Windows.

To spawn processes in a way supported by multiple platforms, use g_spawn_async_with_pipes()

When you are finished with a GPid, you should use g_spawn_close_pid() to close it. This is especially important when spawning processes on Microsoft Windows.

 void g_spawn_close_pid(GPid pid);

Dynamic Modules

Load libraries and explicitly call functions from those libraries using the GModule structure. A cross-platform solution for dynamic libraries makes things much easier.

lib

 #include <glib.h>
 #include <gmodule.h>
 G_MODULE_EXPORT void here_i_am(void)
 {
    g_message("asdf");
 }

 gcc -shared libmylib.c -o libmylib.so $(pkg-config --libs --cflags glib-2.0)
 sudo mv libmylib.so /usr/lib

For purposes other than loading with GModule you need to run ldconfig after copying into /usr/lib.

Using of the lib:

 #include <gmodule.h>
 #include <glib.h>
 typedef void (*my_func_from_lib) (void);

 int main()
 {
 my_func_from_lib this_will_be_the_func_from_lib;
 g_assert(g_module_supported());
 module = g_module_open("/usr/lib/libmylib.so", G_MODULE_BIN_LAZY);
 if(module)
   g_module_symbol(module, "here_i_am", (gpointer*) this_will_be_the_func_from_lib));
 this_will_be_the_func_from_lib();
 g_module_close(module);
 }

 gcc main.c -o main $(pkg-config --cflags --libs glib-2.0 gmodule-2.0)