libubox [1] - utils socket list

3644 ワード

ユーティリティ関数(utils.c/h)
/**
 *                  
 *               free()  
 */
#define calloc_a(len, ...) __calloc_a(len, ##__AV_ARGS__, NULL)

例:
struct obj {
    char *name; /** object name */
    int  id;    /** ojbect id */
};

struct obj* obj_new(char *name, int id)
{
    struct obj *new = NULL;
    char *obj_name = NULL;

    new = calloc_a(sizoef(*new), &obj_name, strlen(name)+1);
    new->id = id;
    new->name = strcpy(obj_name, name);

    return new;
}

void obj_free(struct obj *obj)
{
    /**     obj->name, obj        */
    free(obj);
}
/**
 *         
 */
#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))  

socketヘルプ関数(usock.c/h)
タイプフラグ
#define USOCK_TCP 0
#define USOCK_UDP 1

#define USOCK_SERVER        0x0100
#define USOCK_NOCLOEXEC     0x0200
#define USOCK_NONBLOCK      0x0400
#define USOCK_NUMERIC       0x0800
#define USOCK_IPV6ONLY      0x2000
#define USOCK_IPV4ONLY      0x4000
#define USOCK_UNIX          0x8000

インタフェースの説明
/**
 *         sock
 * 
 * @param type -     
 * @param host -   server        ;  client        
 * @param service -   
 * @return - sock fd > 0;    < 0
 */
int usock(int type, const char *host, const char *service)

双方向チェーンテーブル(list.h)
データ構造
struct list_head {
    struct list_head *next;
    struct list_head *prev;
};

初期化
#define LIST_HEAD_INIT(name) { &(name), &(name) }
#define LIST_HEAD(name) struct list_head name = LIST_HEAD_INIT(name)
static inline void INIT_LIST_HEAD(struct list_head *list)

きほんそうさ
に参加
/** 
 *       
 */
list_add(struct list_head *_new, struct list_head *head)

/**
 *       
 */
list_add_tail(struct list_head *_new, struct list_head *head)

移動
/**
 *             
 */
list_move(struct list_head *list, struct list_head *head)

/**
 *             
 */
list_move_tail(struct list_head *entry, struct list_head *head)

つなぎ合わせる
/**
 *  list     head    
 */
list_splice(const struct list_head *list, struct list_head *head)

/**
 *  list     head    
 */
list_splice_tail(struct list_head *list, struct list_head *head)

/**
 *  list     head    ,    list
 */
list_splice_init(struct list_head *list, struct list_head *head)

/**
 *  list     head    ,    list
 */
list_splice_tail_init(struct list_head *list, struct list_head *head)

削除
/**
 *            
 */
list_del(struct list_head *entry)

/**
 *            ,      
 */
list_del_init(struct list_head *entry)

ノード要素の取得
/**
 *         
 */
list_entry(ptr, type, field)

/**
 *          
 */
list_first_entry(ptr, type, field)

/**
 *          
 */
list_last_entry(ptr, type, field)

じょうたいけってい
/**
 *         
 */
list_entry(ptr, type, field)

/**
 *          
 */
list_first_entry(ptr, type, field)

/**
 *          
 */
list_last_entry(ptr, type, field)

チェーンテーブルを巡る
/**
 *       ,          ,p      
 */
list_for_each(p, head)

/**
 *       ,         ,p      
 */
list_for_each_safe(p, n, head)

/**
 *       ,          ,p      
 */
list_for_each_prev(p, h)

/**
 *       ,         ,p      
 */
list_for_each_prev_safe(p, n, h)

/**
 *       ,          ,p        
 */
list_for_each_entry(p, h, field)

/**
 *       ,         ,p        
 */
list_for_each_entry_safe(p, n, h, field)

/**
 *       ,          ,p        
 */
list_for_each_entry_reverse(p, h, field)