Glib系统学习

1. 解析配置文件
    gkeyfile.c/gkeyfile.h
    1.1 三个对象
        config_file  --load--> GKeyFile  --init--> 自定义对象(保存key和其他成员)
    1.2 如何设计
        config_file: "\n" "\0" "#" <- comment(key=NULL)
                    group <- GKeyFileGroup(GKeyFileKeyValuePair)
                        struct _GKeyFileKeyValuePair
                        {
                          gchar *key;  /* NULL for comments */
                          gchar *value;
                        };
            
        GKeyFile
            创建:
                g_key_file_new
                g_key_file_save_to_file
                g_key_file_to_data
            初始化:
                g_key_file_load_from_file -> g_key_file_load_from_fd  -> g_key_file_parse_data   g_key_file_flush_parse_buffer
                g_key_file_load_from_data -> g_key_file_parse_data   g_key_file_flush_parse_buffer
                g_key_file_load_from_dirs -> g_key_file_load_from_fd
                g_key_file_load_from_data_dirs -> g_key_file_load_from_dirs
                g_key_file_load_from_bytes -> g_key_file_load_from_data
            调用:
                group >>>
                    g_key_file_get_groups/g_key_file_remove_group/g_key_file_has_group
                    g_key_file_get_start_group
                key >>>
                    g_key_file_get_keys/g_key_file_remove_key/g_key_file_has_key
                value >>>
                    g_key_file_get_value/g_key_file_set_value
                    g_key_file_get_[integer|string|boolean|double|uint64|int64|integer_list|string_list|boolean_list|double_list]
                    g_key_file_set_[integer|string|boolean|double|uint64|int64|integer_list|string_list|boolean_list|double_list]
                locale >>>(utf8)
                    g_key_file_get_locale_string/g_key_file_set_locale_string
                    g_key_file_get_locale_string_list/g_key_file_set_locale_string_list
                comment >>>
                    g_key_file_get_comment/g_key_file_set_comment/g_key_file_remove_comment
            其他设置:
                separator >>>
                    g_key_file_set_list_separator
            内置接口: 无须调用,已经被封装了。
                g_key_file_parse_*
            释放:
                Use g_strfreev() to free *_list and group(gchar**).

2. List双链表
    glist.c/glist.h
    gslice.c/gslice.h
        >>>    fast threaded memory chunk allocator
        >>> 参考memory相关
    核心数据结构
        /* Doubly linked lists */
        struct _GList
        {
          gpointer data;
          GList *next;
          GList *prev;
        };
    关于内存的ops主要是封装了glice
    申请/分配:
        g_list_alloc -> _g_list_alloc0 -> g_slice_new0 -> g_slice_alloc0 -> g_slice_alloc
    释放:
        g_list_free    -> g_slice_free_chain -> g_slice_free_chain_with_offset
        g_list_free_1  -> _g_list_free1 -> g_slice_free -> g_slice_free1
        g_list_free1  -> g_list_free_1
        g_list_free_full -> g_list_foreach g_list_free
    add:
        g_list_append -> _g_list_alloc g_list_last
        g_list_prepend -> _g_list_alloc
        g_list_insert -> g_list_prepend g_list_append g_list_nth _g_list_alloc
        g_list_insert_sorted -> g_list_insert_sorted_real
        g_list_insert_sorted_with_data -> g_list_insert_sorted_real
        g_list_insert_before
    delete:
        g_list_remove      -> _g_list_remove_link
        g_list_remove_all  -> _g_list_free1
        g_list_remove_link -> _g_list_remove_link
        g_list_delete_link -> _g_list_remove_link _g_list_free1
    change:
        g_list_concat -> g_list_last
        g_list_reverse
    copy:
        g_list_copy -> g_list_copy_deep
        g_list_copy_deep -> _g_list_alloc
    find:
        g_list_find
        g_list_find_custom
        g_list_position
        g_list_index
        g_list_last
        g_list_first
        g_list_nth
        g_list_nth_prev
        g_list_nth_data
        g_list_previous
        g_list_next
    sort:
        g_list_sort -> g_list_sort_real <-> g_list_sort_merge
        g_list_sort_with_data -> g_list_sort_real
    others:
        g_list_length
        g_list_foreach
        
3. Slist单链表
    gslist.c/gslist.h
    gslice.c/gslice.h
        >>>    fast threaded memory chunk allocator
        >>> 参考memory相关
    核心数据结构
        /* Singly linked lists */
        struct _GSList
        {
          gpointer data;
          GSList *next;
        };
    关于内存的ops主要是封装了glice
    申请/分配:
        g_slist_alloc -> _g_slist_alloc0 -> g_slice_new0 -> g_slice_alloc0 -> g_slice_alloc
    释放:
        g_slist_free    -> g_slice_free_chain -> g_slice_free_chain_with_offset
        g_slist_free_1  -> _g_slist_free1 -> g_slice_free -> g_slice_free1
        g_slist_free1 -> g_slist_free_1
        g_slist_free_full -> g_slist_foreach g_slist_free
    add:
        g_slist_append -> _g_slist_alloc g_slist_last
        g_slist_prepend -> _g_slist_alloc
        g_slist_insert -> g_slist_prepend g_slist_append _g_slist_alloc
        g_slist_insert_sorted -> g_slist_insert_sorted_real
        g_slist_insert_sorted_with_data -> g_slist_insert_sorted_real
        g_slist_insert_before
    delete:
        g_slist_remove      -> _g_slist_remove_link
        g_slist_remove_all  -> _g_slist_free1
        g_slist_remove_link -> _g_slist_remove_link
        g_slist_delete_link -> _g_slist_remove_link _g_slist_free1
    change:
        g_slist_concat -> g_slist_last
        g_slist_reverse
    copy:
        g_slist_copy -> g_slist_copy_deep
        g_slist_copy_deep -> _g_slist_alloc
    find:
        g_slist_find
        g_slist_find_custom
        g_slist_position
        g_slist_index
        g_slist_last
        g_slist_nth
        g_slist_nth_data
        g_slist_next
    sort:
        g_slist_sort -> g_slist_sort_real <-> g_slist_sort_merge
        g_slist_sort_with_data -> g_slist_sort_real
    others:
        g_slist_length
        g_slist_foreach

4. log系统
    核心定义
    /* Glib log levels and flags.     */
    typedef enum
    {
      /* log flags */
      G_LOG_FLAG_RECURSION          = 1 << 0,
      G_LOG_FLAG_FATAL              = 1 << 1,

      /* GLib log levels */
      G_LOG_LEVEL_ERROR             = 1 << 2,       /* always fatal */
      G_LOG_LEVEL_CRITICAL          = 1 << 3,
      G_LOG_LEVEL_WARNING           = 1 << 4,
      G_LOG_LEVEL_MESSAGE           = 1 << 5,
      G_LOG_LEVEL_INFO              = 1 << 6,
      G_LOG_LEVEL_DEBUG             = 1 << 7,

      G_LOG_LEVEL_MASK              = ~(G_LOG_FLAG_RECURSION | G_LOG_FLAG_FATAL)
    } GLogLevelFlags;

    核心函数:
        g_log -> g_logv(log_domain, log_level, format, ...)
    handler:
        g_log_set_handler
        g_log_set_handler_full
        g_log_remove_handler
    init/uninit:
        __btd_log_init
        __btd_log_cleanup
    api:
        g_message G_LOG_LEVEL_MESSAGE
        g_critical G_LOG_LEVEL_CRITICAL
        g_warning G_LOG_LEVEL_WARNING
        g_info    G_LOG_LEVEL_INFO
        g_error G_LOG_LEVEL_ERROR
        g_debug    G_LOG_LEVEL_DEBUG
    以下是bluez中自定义的log接口(syslog.h)
    error()  -> vsyslog(LOG_ERR)         logging_log(HCI_DEV_NONE, LOG_ERR)
    warn()   -> vsyslog(LOG_WARNING)     logging_log(HCI_DEV_NONE, LOG_WARNING)
    info()   -> vsyslog(LOG_INFO)         logging_log(HCI_DEV_NONE, LOG_INFO)
                                            #define HCI_DEV_NONE 0xffff

    btd_log            -> vsyslog(priority)         logging_log(index, priority)
    btd_error        -> vsyslog(LOG_ERR)         logging_log(index, LOG_INFO)
    btd_warn        -> vsyslog(LOG_WARNING)     logging_log(index, LOG_INFO)
    btd_info        -> vsyslog(LOG_INFO)         logging_log(index, LOG_INFO)
    btd_debug        -> vsyslog(LOG_DEBUG)         logging_log(index, LOG_INFO)
    __attribute__((format(printf, 3, 4))); 这个表示可变参数从第4个开始

5. HashTable
    ghash.c/ghash.h
    核心数据结构
    struct _GHashTable
    {
      gint             size;
      gint             mod;
      guint            mask;
      gint             nnodes;
      gint             noccupied;  /* nnodes + tombstones */

      gpointer        *keys;
      guint           *hashes;
      gpointer        *values;

      GHashFunc        hash_func;
      GEqualFunc       key_equal_func;
      gint             ref_count;
    #ifndef G_DISABLE_ASSERT
      /*
       * Tracks the structure of the hash table, not its contents: is only
       * incremented when a node is added or removed (is not incremented
       * when the key or data of a node is modified).
       */
      int              version;
    #endif
      GDestroyNotify   key_destroy_func;
      GDestroyNotify   value_destroy_func;
    };
    
    #define G_BEGIN_DECLS  extern "C" {
    #define G_END_DECLS    }

    创建:
        g_hash_table_new(GHashFunc, GEqualFunc) -> g_hash_table_new_full -> g_slice_new(分配内存) g_hash_table_set_shift(size=2^3)
                         g_new0(key,value,hash分配空间 -> g_malloc0_n)
                        #define g_new0(struct_type, n_structs)            _G_NEW (struct_type, n_structs, malloc0)
                        #define g_renew(struct_type, mem, n_structs)        _G_RENEW (struct_type, mem, n_structs, realloc)
                        #define _G_NEW(struct_type, n_structs, func) \
                                ((struct_type *) g_##func##_n ((n_structs), sizeof (struct_type)))
                        #define _G_RENEW(struct_type, mem, n_structs, func) \
                                ((struct_type *) g_##func##_n (mem, (n_structs), sizeof (struct_type)))

        g_hash_table_new_full
    销毁:
        g_hash_table_destroy -> g_hash_table_remove_all g_hash_table_unref
    插入:
        g_hash_table_insert  -> g_hash_table_insert_internal(keep=FALSE) ->  g_hash_table_lookup_node g_hash_table_insert_node
        g_hash_table_replace -> g_hash_table_insert_internal(keep=TRUE)
        g_hash_table_add     -> g_hash_table_insert_internal(keep=TRUE)
    移除:
        g_hash_table_remove      -> g_hash_table_remove_internal(notify=TRUE) -> g_hash_table_lookup_node g_hash_table_remove_node g_hash_table_maybe_resize
        g_hash_table_remove_all  -> g_hash_table_remove_all_nodes(notify=TRUE,destruction=FALSE) g_hash_table_maybe_resize
        g_hash_table_steal       -> g_hash_table_remove_internal(notify=FALSE)
        g_hash_table_steal_all   -> g_hash_table_remove_all_nodes(notify=TRUE,destruction=TRUE) g_hash_table_maybe_resize
    查找:
        g_hash_table_lookup            -> g_hash_table_lookup_node
        g_hash_table_contains          -> g_hash_table_lookup_node
        g_hash_table_lookup_extended   -> g_hash_table_lookup_node
        g_hash_table_find               -> 调用predicate
    遍历:
        g_hash_table_foreach
        g_hash_table_foreach_remove  -> g_hash_table_foreach_remove_or_steal(notify=TRUE) -> g_hash_table_remove_node g_hash_table_maybe_resize
        g_hash_table_foreach_steal   -> g_hash_table_foreach_remove_or_steal(notify=FALSE)
    迭代器:
        g_hash_table_iter_init
        g_hash_table_iter_next
        g_hash_table_iter_get_hash_table
        g_hash_table_iter_remove  -> iter_remove_or_steal(notify=TRUE) -> g_hash_table_remove_node
        g_hash_table_iter_steal   -> iter_remove_or_steal(notify=FALSE) -> g_hash_table_remove_node
    key/value:
        g_hash_table_get_keys            -> g_list_prepend
        g_hash_table_get_values          -> g_list_prepend
        g_hash_table_get_keys_as_array   -> g_new
    others:
        g_hash_table_size
        g_hash_table_ref    -> g_atomic_int_inc
        g_hash_table_unref  -> g_atomic_int_dec_and_test  g_hash_table_remove_all_nodes(notify=TRUE,destruction=TRUE)
    /* Hash Functions  */
        g_[str|int|int64|double|direct]_equal
        g_[str|int|int64|double|direct]_hash
        注: steal表示强制操作,不通知,不执行销毁回调函数.

6. GString
    gstring.c/gstring.h
    gstrfuncs.c/gstrfuncs.h
    核心数据结构
        struct _GString
        {
          gchar  *str;
          gsize len;
          gsize allocated_len;
        };
    申请/分配:
        g_string_new      -> g_string_sized_new g_string_append_len( -> g_string_insert_len -> g_string_maybe_expand  memmove/memcpy)
        g_string_new_len  -> g_string_new
                          -> g_string_sized_new  g_string_append_len
        g_string_sized_new -> g_slice_new  g_string_maybe_expand( -> g_realloc)
    释放:
        g_string_free          -> g_free/g_slice_free
        g_string_free_to_bytes -> g_string_free  g_bytes_new_take( -> g_bytes_new_with_free_func)
    api:
        g_string_equal
        g_string_hash
        g_string_assign -> g_string_truncate g_string_append
        g_string_truncate (字符串截断=0)
        g_string_erase
        g_string_set_size   -> g_string_maybe_expand (扩展2^N < n)

        g_string_insert_len -> g_string_maybe_expand
        g_string_insert     -> g_string_insert_len
        g_string_insert_c   -> g_string_maybe_expand
        g_string_insert_unichar  -> g_string_maybe_expand

        g_string_append     -> g_string_insert_len
        g_string_append_len
        g_string_append_c   -> g_string_insert_c -> g_string_maybe_expand
        g_string_append_unichar  -> g_string_insert_unichar -> g_string_maybe_expand

        g_string_prepend    -> g_string_insert_len
        g_string_prepend_len
        g_string_prepend_c  -> g_string_insert_c -> g_string_maybe_expand
        g_string_prepend_unichar -> g_string_insert_unichar -> g_string_maybe_expand

        g_string_overwrite -> g_string_overwrite_len
        g_string_overwrite_len -> g_string_maybe_expand

        g_string_ascii_down -> g_ascii_tolower -> g_ascii_islower
        g_string_ascii_up   -> g_ascii_toupper -> g_ascii_islower

        g_string_vprintf    -> g_string_truncate g_string_append_vprintf
        g_string_printf     -> g_string_truncate g_string_append_vprintf
        g_string_append_vprintf -> g_vasprintf g_string_maybe_expand
        g_string_append_printf  -> g_string_append_vprintf
        g_string_append_uri_escaped

        deprecated:
            g_string_down
            g_string_up
            g_string_sprintf  -> g_string_printf
            g_string_sprintfa -> g_string_append_printf

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值