Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2968880
  • 博文数量: 685
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 5303
  • 用 户 组: 普通用户
  • 注册时间: 2014-04-19 14:17
个人简介

文章分类

全部博文(685)

文章存档

2015年(116)

2014年(569)

分类: LINUX

2014-09-26 17:30:47

原文地址:http://blog.csdn.net/tyheist/article/details/37053327

实用函数 - utils.c/h


  1. /*
  2. * calloc_a(size_t len, [void **addr, size_t len,...], NULL)
  3. *
  4. * allocate a block of memory big enough to hold multiple aligned objects.
  5. * the pointer to the full object (starting with the first chunk) is returned,
  6. * all other pointers are stored in the locations behind extra addr arguments.
  7. * the last argument needs to be a NULL pointer
  8. *
  9. * 分配一段连续内存给多个指针对象使用,多个指针对象必须保证同时进行free()操作
  10. */
  11. #define calloc_a(len, ...) __calloc_a(len, ##__VA_ARGS__, NULL)
/*
 * calloc_a(size_t len, [void **addr, size_t len,...], NULL)
 *
 * allocate a block of memory big enough to hold multiple aligned objects.
 * the pointer to the full object (starting with the first chunk) is returned,
 * all other pointers are stored in the locations behind extra addr arguments.
 * the last argument needs to be a NULL pointer
 *
 * 分配一段连续内存给多个指针对象使用,多个指针对象必须保证同时进行free()操作
 */
#define calloc_a(len, ...) __calloc_a(len, ##__VA_ARGS__, NULL)
  1. /**
  2. * 计算对象数组个数
  3. */
  4. #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
/**
 * 计算对象数组个数
 */
#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))

Socket帮助函数 - usock.c/h

类型标志

  1. #define USOCK_TCP 0
  2. #define USOCK_UDP 1
  3. #define USOCK_SERVER 0x0100
  4. #define USOCK_NOCLOEXEC 0x0200
  5. #define USOCK_NONBLOCK 0x0400
  6. #define USOCK_NUMERIC 0x0800
  7. #define USOCK_IPV6ONLY 0x2000
  8. #define USOCK_IPV4ONLY 0x4000
  9. #define USOCK_UNIX 0x8000
#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

接口说明

  1. /**
  2. * 创建一个新的网络sock
  3. *
  4. * @param type - 类型标志
  5. * @param host - 作为server表示绑定本地地址;作为client表示需连接的地址
  6. * @param service - 端口
  7. * @return - sock fd > 0; 错误 < 0
  8. */
  9. int usock(int type, const char *host, const char *service)
/**
 * 创建一个新的网络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

数据结构

  1. struct list_head {
  2. struct list_head *next;
  3. struct list_head *prev;
  4. };
struct list_head {
	struct list_head *next;
	struct list_head *prev;
};

初始化

  1. #define LIST_HEAD_INIT(name) { &(name), &(name) }
  2. #undef LIST_HEAD
  3.   #define LIST_HEAD(name) struct list_head name = LIST_HEAD_INIT(name)
  4. static inline void INIT_LIST_HEAD(struct list_head *list)
#define LIST_HEAD_INIT(name) { &(name), &(name) }
#undef LIST_HEAD
  #define LIST_HEAD(name)	struct list_head name = LIST_HEAD_INIT(name)
static inline void INIT_LIST_HEAD(struct list_head *list)

基本操作

加入

  1. /**
  2. * 加入链表头部
  3. */
  4. list_add(struct list_head *_new, struct list_head *head)
  5. /**
  6. * 加入链表尾部
  7. */
  8. list_add_tail(struct list_head *_new, struct list_head *head)
/** 
 * 加入链表头部
 */
list_add(struct list_head *_new, struct list_head *head)

/**
 * 加入链表尾部
 */
list_add_tail(struct list_head *_new, struct list_head *head)

移动

  1. /**
  2. * 把指定节点移动到链表头部
  3. */
  4. list_move(struct list_head *list, struct list_head *head)
  5. /**
  6. * 把指定节点移动到链表尾部
  7. */
  8. list_move_tail(struct list_head *entry, 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)

拼接

  1. /**
  2. * 将list链表拼接到head链表头部
  3. */
  4. list_splice(const struct list_head *list, struct list_head *head)
  5. /**
  6. * 将list链表拼接到head链表尾部
  7. */
  8. list_splice_tail(struct list_head *list, struct list_head *head)
  9. /**
  10. * 将list链表拼接到head链表头部,并初始化list
  11. */
  12. list_splice_init(struct list_head *list, struct list_head *head)
  13. /**
  14. * 将list链表拼接到head链表尾部,并初始化list
  15. */
  16. list_splice_tail_init(struct list_head *list, 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)

删除

  1. /**
  2. * 把指定节点从链表中删除
  3. */
  4. list_del(struct list_head *entry)
  5. /**
  6. * 把指定节点从链表中删除,并初始此节点
  7. */
  8. list_del_init(struct list_head *entry)
/**
 * 把指定节点从链表中删除
 */
list_del(struct list_head *entry)

/**
 * 把指定节点从链表中删除,并初始此节点
 */
list_del_init(struct list_head *entry)

获取链表节点元素

  1. /**
  2. * 获取当前节点元素
  3. */
  4. list_entry(ptr, type, field)
  5. /**
  6. * 获取后一个节点元素
  7. */
  8. list_first_entry(ptr, type, field)
  9. /**
  10. * 获取前一个节点元素
  11. */
  12. list_last_entry(ptr, type, field)
/**
 * 获取当前节点元素
 */
list_entry(ptr, type, field)

/**
 * 获取后一个节点元素
 */
list_first_entry(ptr, type, field)

/**
 * 获取前一个节点元素
 */
list_last_entry(ptr, type, field)

状态判断

  1. /**
  2. * 是否为空链表
  3. */
  4. bool list_empty(const struct list_head *head)
  5. /**
  6. * 指定节点是否为链表第一个节点
  7. */
  8. bool list_is_first(const struct list_head *list, const struct list_head *head)
  9. /**
  10. * 指定节点是否为链表最后一个节点
  11. */
  12. bool list_is_last(const struct list_head *list, const struct list_head *head)
/**
 * 是否为空链表
 */
bool list_empty(const struct list_head *head)

/**
 * 指定节点是否为链表第一个节点
 */
bool list_is_first(const struct list_head *list, const struct list_head *head)

/**
 * 指定节点是否为链表最后一个节点
 */
bool list_is_last(const struct list_head *list, const struct list_head *head)

遍历链表

  1. /**
  2. * 向后遍历链表,遍历过程不能操作链表,p为链表结构体
  3. */
  4. list_for_each(p, head)
  5. /**
  6. * 向后遍历链表,遍历过程可操作链表,p为链表结构体
  7. */
  8. list_for_each_safe(p, n, head)
  9. /**
  10. * 向前遍历链表,遍历过程不能操作链表,p为链表结构体
  11. */
  12. list_for_each_prev(p, h)
  13. /**
  14. * 向前遍历链表,遍历过程可操作链表,p为链表结构体
  15. */
  16. list_for_each_prev_safe(p, n, h)
  17. /**
  18. * 向后遍历链表,遍历过程不能操作链表,p为节点元素结构体
  19. */
  20. list_for_each_entry(p, h, field)
  21. /**
  22. * 向后遍历链表,遍历过程可操作链表,p为节点元素结构体
  23. */
  24. list_for_each_entry_safe(p, n, h, field)
  25. /**
  26. * 向前遍历链表,遍历过程不能操作链表,p为节点元素结构体
  27. */
  28. list_for_each_entry_reverse(p, h, 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)

平衡二叉树 - avl.c/h


数据结构

  1. /**
  2. * This element is a member of a avl-tree. It must be contained in all
  3. * larger structs that should be put into a tree.
  4. */
  5. struct avl_node {
  6. /**
  7. * Linked list node for supporting easy iteration and multiple
  8. * elments with the same key.
  9. *
  10. * this must be the first element of an avl_node to
  11. * make casting for lists easier
  12. */
  13. struct list_head list;
  14. /**
  15. * Pointer to parent node in tree, NULL if root node
  16. */
  17. struct avl_node *parent;
  18. /**
  19. * Pointer to left child
  20. */
  21. struct avl_node *left;
  22. /**
  23. * Pointer to right child
  24. */
  25. struct avl_node *right;
  26. /**
  27. * pointer to key of node
  28. */
  29. const void *key;
  30. /**
  31. * balance state of AVL tree (0,-1,+1)
  32. */
  33. signed char balance;
  34. /**
  35. * true if first of a series of nodes with same key
  36. */
  37. bool leader;
  38. };
/**
 * This element is a member of a avl-tree. It must be contained in all
 * larger structs that should be put into a tree.
 */
struct avl_node {
  /**
   * Linked list node for supporting easy iteration and multiple
   * elments with the same key.
   *
   * this must be the first element of an avl_node to
   * make casting for lists easier
   */
  struct list_head list;

  /**
   * Pointer to parent node in tree, NULL if root node
   */
  struct avl_node *parent;

  /**
   * Pointer to left child
   */
  struct avl_node *left;

  /**
   * Pointer to right child
   */
  struct avl_node *right;

  /**
   * pointer to key of node
   */
  const void *key;

  /**
   * balance state of AVL tree (0,-1,+1)
   */
  signed char balance;

  /**
   * true if first of a series of nodes with same key
   */
  bool leader;
};
  1. /**
  2. * This struct is the central management part of an avl tree.
  3. * One of them is necessary for each avl_tree.
  4. */
  5. struct avl_tree {
  6. /**
  7. * Head of linked list node for supporting easy iteration
  8. * and multiple elments with the same key.
  9. */
  10. struct list_head list_head;
  11. /**
  12. * pointer to the root node of the avl tree, NULL if tree is empty
  13. */
  14. struct avl_node *root;
  15. /**
  16. * number of nodes in the avl tree
  17. */
  18. unsigned int count;
  19. /**
  20. * true if multiple nodes with the same key are
  21. * allowed in the tree, false otherwise
  22. */
  23. bool allow_dups;
  24. /**
  25. * pointer to the tree comparator
  26. *
  27. * First two parameters are keys to compare,
  28. * third parameter is a copy of cmp_ptr
  29. */
  30. avl_tree_comp comp;
  31. /**
  32. * custom pointer delivered to the tree comparator
  33. */
  34. void *cmp_ptr;
  35. };
/**
 * This struct is the central management part of an avl tree.
 * One of them is necessary for each avl_tree.
 */
struct avl_tree {
  /**
   * Head of linked list node for supporting easy iteration
   * and multiple elments with the same key.
   */
  struct list_head list_head;

  /**
   * pointer to the root node of the avl tree, NULL if tree is empty
   */
  struct avl_node *root;

  /**
   * number of nodes in the avl tree
   */
  unsigned int count;

  /**
   * true if multiple nodes with the same key are
   * allowed in the tree, false otherwise
   */
  bool allow_dups;

  /**
   * pointer to the tree comparator
   *
   * First two parameters are keys to compare,
   * third parameter is a copy of cmp_ptr
   */
  avl_tree_comp comp;

  /**
   * custom pointer delivered to the tree comparator
   */
  void *cmp_ptr;
};

初始化

  1. #define AVL_TREE_INIT(_name, _comp, _allow_dups, _cmp_ptr) \
  2. { \
  3. .list_head = LIST_HEAD_INIT(_name.list_head), \
  4. .comp = _comp, \
  5. .allow_dups = _allow_dups, \
  6. .cmp_ptr = _cmp_ptr \
  7. }
#define AVL_TREE_INIT(_name, _comp, _allow_dups, _cmp_ptr)	\
	{							\
		.list_head = LIST_HEAD_INIT(_name.list_head),	\
		.comp = _comp,					\
		.allow_dups = _allow_dups,			\
		.cmp_ptr = _cmp_ptr				\
	}
  1. #define AVL_TREE(_name, _comp, _allow_dups, _cmp_ptr) \
  2. struct avl_tree _name = \
  3. AVL_TREE_INIT(_name, _comp, _allow_dups, _cmp_ptr)
#define AVL_TREE(_name, _comp, _allow_dups, _cmp_ptr)		\
	struct avl_tree _name =					\
		AVL_TREE_INIT(_name, _comp, _allow_dups, _cmp_ptr)
  1. /**
  2. * Initialize a new avl_tree struct
  3. * @param tree pointer to avl-tree
  4. * @param comp pointer to comparator for the tree
  5. * @param allow_dups true if the tree allows multiple elements with the same
  6. * @param ptr custom parameter for comparator
  7. */
  8. void avl_init(struct avl_tree *tree, avl_tree_comp comp,
  9. bool allow_dups, void *ptr)
/**
 * Initialize a new avl_tree struct
 * @param tree pointer to avl-tree
 * @param comp pointer to comparator for the tree
 * @param allow_dups true if the tree allows multiple elements with the same
 * @param ptr custom parameter for comparator
 */
void avl_init(struct avl_tree *tree, avl_tree_comp comp, 
              bool allow_dups, void *ptr)

基本操作

加入

  1. /**
  2. * Inserts an avl_node into a tree
  3. * @param tree pointer to tree
  4. * @param new pointer to node
  5. * @return 0 if node was inserted successfully, -1 if it was not inserted
  6. * because of a key collision
  7. */
  8. int avl_insert(struct avl_tree *tree, struct avl_node *new)
/**
 * Inserts an avl_node into a tree
 * @param tree pointer to tree
 * @param new pointer to node
 * @return 0 if node was inserted successfully, -1 if it was not inserted
 *   because of a key collision
 */
int avl_insert(struct avl_tree *tree, struct avl_node *new)

删除

  1. /**
  2. * Remove a node from an avl tree
  3. * @param tree pointer to tree
  4. * @param node pointer to node
  5. */
  6. void avl_delete(struct avl_tree *tree, struct avl_node *node)
/**
 * Remove a node from an avl tree
 * @param tree pointer to tree
 * @param node pointer to node
 */
void avl_delete(struct avl_tree *tree, struct avl_node *node)

查找

  1. /**
  2. * Finds a node in an avl-tree with a certain key
  3. * @param tree pointer to avl-tree
  4. * @param key pointer to key
  5. * @return pointer to avl-node with key, NULL if no node with
  6. * this key exists.
  7. */
  8. struct avl_node * avl_find(const struct avl_tree *tree, const void *key)
/**
 * Finds a node in an avl-tree with a certain key
 * @param tree pointer to avl-tree
 * @param key pointer to key
 * @return pointer to avl-node with key, NULL if no node with
 *    this key exists.
 */
struct avl_node * avl_find(const struct avl_tree *tree, const void *key)
  1. /**
  2. * Finds the first node in an avl-tree with a key greater or equal
  3. * than the specified key
  4. * @param tree pointer to avl-tree
  5. * @param key pointer to specified key
  6. * @return pointer to avl-node, NULL if no node with
  7. * key greater or equal specified key exists.
  8. */
  9. struct avl_node * avl_find_greaterequal(const struct avl_tree *tree,
  10. const void *key)
/**
 * Finds the first node in an avl-tree with a key greater or equal
 * than the specified key
 * @param tree pointer to avl-tree
 * @param key pointer to specified key
 * @return pointer to avl-node, NULL if no node with
 *    key greater or equal specified key exists.
 */
struct avl_node * avl_find_greaterequal(const struct avl_tree *tree, 
                                         const void *key)
  1. /**
  2. * Finds the last node in an avl-tree with a key less or equal
  3. * than the specified key
  4. *
  5. * @param tree pointer to avl-tree
  6. * @param key pointer to specified key
  7. * @return pointer to avl-node, NULL if no node with
  8. * key less or equal specified key exists.
  9. */
  10. struct avl_node * avl_find_lessequal(const struct avl_tree *tree, const void *key)
/**
 * Finds the last node in an avl-tree with a key less or equal
 * than the specified key
 *
 * @param tree pointer to avl-tree
 * @param key pointer to specified key
 * @return pointer to avl-node, NULL if no node with
 *         key less or equal specified key exists.
 */
struct avl_node * avl_find_lessequal(const struct avl_tree *tree, const void *key)
  1. /**
  2. * @param tree pointer to avl-tree
  3. * @param key pointer to key
  4. * @param element pointer to a node element(don't need to be initialized)
  5. * @param node_element name of the avl_node element inside the larger struct
  6. * return pointer to tree element with the specified key,
  7. * NULL if no element was found
  8. */
  9. #define avl_find_element(tree, key, element, node_element)
/**
 * @param tree pointer to avl-tree
 * @param key pointer to key
 * @param element pointer to a node element(don't need to be initialized)
 * @param node_element name of the avl_node element inside the larger struct
 * return pointer to tree element with the specified key,
 *        NULL if no element was found
 */
#define avl_find_element(tree, key, element, node_element)
  1. /**
  2. * @param tree pointer to avl-tree
  3. * @param key pointer to specified key
  4. * @param element pointer to a node element (don't need to be initialized)
  5. * @param node_element name of the avl_node element inside the larger struct
  6. * return pointer to last tree element with less or equal key than specified key,
  7. * NULL if no element was found
  8. */
  9. #define avl_find_le_element(tree, key, element, node_element)
/**
 * @param tree pointer to avl-tree
 * @param key pointer to specified key
 * @param element pointer to a node element (don't need to be initialized)
 * @param node_element name of the avl_node element inside the larger struct
 * return pointer to last tree element with less or equal key than specified key,
 *        NULL if no element was found
 */
#define avl_find_le_element(tree, key, element, node_element)
  1. /**
  2. * @param tree pointer to avl-tree
  3. * @param key pointer to specified key
  4. * @param element pointer to a node element (don't need to be initialized)
  5. * @param node_element name of the avl_node element inside the larger struct
  6. * return pointer to first tree element with greater or equal key than
  7. * specified key, NULL if no element was found
  8. */
  9. #define avl_find_ge_element(tree, key, element, node_element)
/**
 * @param tree pointer to avl-tree
 * @param key pointer to specified key
 * @param element pointer to a node element (don't need to be initialized)
 * @param node_element name of the avl_node element inside the larger struct
 * return pointer to first tree element with greater or equal key than 
 *        specified key, NULL if no element was found
 */
#define avl_find_ge_element(tree, key, element, node_element)

获取节点元素

  1. /**
  2. * This function must not be called for an empty tree
  3. *
  4. * @param tree pointer to avl-tree
  5. * @param element pointer to a node element (don't need to be initialized)
  6. * @param node_member name of the avl_node element inside the larger struct
  7. * @return pointer to the first element of the avl_tree
  8. * (automatically converted to type 'element')
  9. */
  10. #define avl_first_element(tree, element, node_member)
/**
 * This function must not be called for an empty tree
 *
 * @param tree pointer to avl-tree
 * @param element pointer to a node element (don't need to be initialized)
 * @param node_member name of the avl_node element inside the larger struct
 * @return pointer to the first element of the avl_tree
 *         (automatically converted to type 'element')
 */
#define avl_first_element(tree, element, node_member)
  1. /**
  2. * @param tree pointer to tree
  3. * @param element pointer to a node struct that contains the avl_node
  4. * (don't need to be initialized)
  5. * @param node_member name of the avl_node element inside the
  6. * larger struct
  7. * @return pointer to the last element of the avl_tree
  8. * (automatically converted to type 'element')
  9. */
  10. #define avl_last_element(tree, element, node_member)
/**
 * @param tree pointer to tree
 * @param element pointer to a node struct that contains the avl_node
 *        (don't need to be initialized)
 * @param node_member name of the avl_node element inside the
 *        larger struct
 * @return pointer to the last element of the avl_tree
 *         (automatically converted to type 'element')
 */
#define avl_last_element(tree, element, node_member)
  1. /**
  2. * This function must not be called for the last element of an avl tree
  3. *
  4. * @param element pointer to a node of the tree
  5. * @param node_member name of the avl_node element inside the larger struct
  6. * @return pointer to the node after 'element'
  7. * (automatically converted to type 'element')
  8. */
  9. #define avl_next_element(element, node_member)
/**
 * This function must not be called for the last element of an avl tree
 *
 * @param element pointer to a node of the tree
 * @param node_member name of the avl_node element inside the larger struct
 * @return pointer to the node after 'element'
 *         (automatically converted to type 'element')
 */
#define avl_next_element(element, node_member)
  1. /**
  2. * This function must not be called for the first element of
  3. * an avl tree
  4. *
  5. * @param element pointer to a node of the tree
  6. * @param node_member name of the avl_node element inside the larger struct
  7. * @return pointer to the node before 'element'
  8. * (automatically converted to type 'element')
  9. */
  10. #define avl_prev_element(element, node_member)
/**
 * This function must not be called for the first element of
 * an avl tree
 *
 * @param element pointer to a node of the tree
 * @param node_member name of the avl_node element inside the larger struct
 * @return pointer to the node before 'element'
 *         (automatically converted to type 'element')
 */
#define avl_prev_element(element, node_member)

状态判断

  1. /**
  2. * @param tree pointer to avl-tree
  3. * @param node pointer to node of the tree
  4. * @return true if node is the first one of the tree, false otherwise
  5. */
  6. static inline bool avl_is_first(struct avl_tree *tree, struct avl_node *node)
/**
 * @param tree pointer to avl-tree
 * @param node pointer to node of the tree
 * @return true if node is the first one of the tree, false otherwise
 */
static inline bool avl_is_first(struct avl_tree *tree, struct avl_node *node)
  1. /**
  2. * @param tree pointer to avl-tree
  3. * @param node pointer to node of the tree
  4. * @return true if node is the last one of the tree, false otherwise
  5. */
  6. static inline bool avl_is_last(struct avl_tree *tree, struct avl_node *node)
/**
 * @param tree pointer to avl-tree
 * @param node pointer to node of the tree
 * @return true if node is the last one of the tree, false otherwise
 */
static inline bool avl_is_last(struct avl_tree *tree, struct avl_node *node)
  1. /**
  2. * @param tree pointer to avl-tree
  3. * @return true if the tree is empty, false otherwise
  4. */
  5. static inline bool avl_is_empty(struct avl_tree *tree)
/**
 * @param tree pointer to avl-tree
 * @return true if the tree is empty, false otherwise
 */
static inline bool avl_is_empty(struct avl_tree *tree) 

遍历

  1. /**
  2. * Loop over all elements of an avl_tree, used similar to a for() command.
  3. * This loop should not be used if elements are removed from the tree during
  4. * the loop.
  5. *
  6. * @param tree pointer to avl-tree
  7. * @param element pointer to a node of the tree, this element will
  8. * contain the current node of the tree during the loop
  9. * @param node_member name of the avl_node element inside the
  10. * larger struct
  11. */
  12. #define avl_for_each_element(tree, element, node_member)
/**
 * Loop over all elements of an avl_tree, used similar to a for() command.
 * This loop should not be used if elements are removed from the tree during
 * the loop.
 *
 * @param tree pointer to avl-tree
 * @param element pointer to a node of the tree, this element will
 *        contain the current node of the tree during the loop
 * @param node_member name of the avl_node element inside the
 *        larger struct
 */
#define avl_for_each_element(tree, element, node_member)
  1. /**
  2. * Loop over all elements of an avl_tree backwards, used similar to a for() command.
  3. * This loop should not be used if elements are removed from the tree during
  4. * the loop.
  5. *
  6. * @param tree pointer to avl-tree
  7. * @param element pointer to a node of the tree, this element will
  8. * contain the current node of the tree during the loop
  9. * @param node_member name of the avl_node element inside the
  10. * larger struct
  11. */
  12. #define avl_for_each_element_reverse(tree, element, node_member)
/**
 * Loop over all elements of an avl_tree backwards, used similar to a for() command.
 * This loop should not be used if elements are removed from the tree during
 * the loop.
 *
 * @param tree pointer to avl-tree
 * @param element pointer to a node of the tree, this element will
 *        contain the current node of the tree during the loop
 * @param node_member name of the avl_node element inside the
 *        larger struct
 */
#define avl_for_each_element_reverse(tree, element, node_member)
  1. /**
  2. * Loop over all elements of an avl_tree, used similar to a for() command.
  3. * This loop can be used if the current element might be removed from
  4. * the tree during the loop. Other elements should not be removed during
  5. * the loop.
  6. *
  7. * @param tree pointer to avl-tree
  8. * @param element pointer to a node of the tree, this element will
  9. * contain the current node of the tree during the loop
  10. * @param node_member name of the avl_node element inside the
  11. * larger struct
  12. * @param ptr pointer to a tree element which is used to store
  13. * the next node during the loop
  14. */
  15. #define avl_for_each_element_safe(tree, element, node_member, ptr)
/**
 * Loop over all elements of an avl_tree, used similar to a for() command.
 * This loop can be used if the current element might be removed from
 * the tree during the loop. Other elements should not be removed during
 * the loop.
 *
 * @param tree pointer to avl-tree
 * @param element pointer to a node of the tree, this element will
 *        contain the current node of the tree during the loop
 * @param node_member name of the avl_node element inside the
 *        larger struct
 * @param ptr pointer to a tree element which is used to store
 *        the next node during the loop
 */
#define avl_for_each_element_safe(tree, element, node_member, ptr)
  1. /**
  2. * Loop over all elements of an avl_tree backwards, used similar to a for() command.
  3. * This loop can be used if the current element might be removed from
  4. * the tree during the loop. Other elements should not be removed during
  5. * the loop.
  6. *
  7. * @param tree pointer to avl-tree
  8. * @param element pointer to a node of the tree, this element will
  9. * contain the current node of the tree during the loop
  10. * @param node_member name of the avl_node element inside the
  11. * larger struct
  12. * @param ptr pointer to a tree element which is used to store
  13. * the next node during the loop
  14. */
  15. #define avl_for_each_element_reverse_safe(tree, element, node_member, ptr)
/**
 * Loop over all elements of an avl_tree backwards, used similar to a for() command.
 * This loop can be used if the current element might be removed from
 * the tree during the loop. Other elements should not be removed during
 * the loop.
 *
 * @param tree pointer to avl-tree
 * @param element pointer to a node of the tree, this element will
 *        contain the current node of the tree during the loop
 * @param node_member name of the avl_node element inside the
 *        larger struct
 * @param ptr pointer to a tree element which is used to store
 *        the next node during the loop
 */
#define avl_for_each_element_reverse_safe(tree, element, node_member, ptr)
  1. /**
  2. * A special loop that removes all elements of the tree and cleans up the tree
  3. * root. The loop body is responsible to free the node elements of the tree.
  4. *
  5. * This loop is much faster than a normal one for clearing the tree because it
  6. * does not rebalance the tree after each removal. Do NOT use a break command
  7. * inside.
  8. * You can free the memory of the elements within the loop.
  9. * Do NOT call avl_delete() on the elements within the loop,
  10. *
  11. * @param tree pointer to avl-tree
  12. * @param element pointer to a node of the tree, this element will
  13. * contain the current node of the tree during the loop
  14. * @param node_member name of the avl_node element inside the
  15. * larger struct
  16. * @param ptr pointer to a tree element which is used to store
  17. * the next node during the loop
  18. */
  19. #define avl_remove_all_elements(tree, element, node_member, ptr)
/**
 * A special loop that removes all elements of the tree and cleans up the tree
 * root. The loop body is responsible to free the node elements of the tree.
 *
 * This loop is much faster than a normal one for clearing the tree because it
 * does not rebalance the tree after each removal. Do NOT use a break command
 * inside.
 * You can free the memory of the elements within the loop.
 * Do NOT call avl_delete() on the elements within the loop,
 *
 * @param tree pointer to avl-tree
 * @param element pointer to a node of the tree, this element will
 *        contain the current node of the tree during the loop
 * @param node_member name of the avl_node element inside the
 *        larger struct
 * @param ptr pointer to a tree element which is used to store
 *        the next node during the loop
 */
#define avl_remove_all_elements(tree, element, node_member, ptr)

比较回调函数

查找操作时被调用,此函数在初始化由调用者设置

  1. /**
  2. * Prototype for avl comparators
  3. * @param k1 first key
  4. * @param k2 second key
  5. * @param ptr custom data for tree comparator
  6. * @return +1 if k1>k2, -1 if k1
  7. */
  8. typedef int (*avl_tree_comp) (const void *k1, const void *k2, void *ptr)
/**
 * Prototype for avl comparators
 * @param k1 first key
 * @param k2 second key
 * @param ptr custom data for tree comparator
 * @return +1 if k1>k2, -1 if k1

平衡二叉树Key比较 - avl-cmp.c/h


  1. /**
  2. * 用于初始化平衡二叉树,将k1和k2进行字符串比较
  3. */
  4. int avl_strcmp(const void *k1, const void *k2, void *ptr)
/**
 * 用于初始化平衡二叉树,将k1和k2进行字符串比较
 */
int avl_strcmp(const void *k1, const void *k2, void *ptr)

VList(vlist.c/h)

数据结构

  1. struct vlist_tree {
  2. struct avl_tree avl;
  3. vlist_update_cb update; /** 初始化时调用者设置 */
  4. bool keep_old; /** true-加入相同节点时不删除旧节点,false-删除
  5. bool no_delete; /** true-加入相同节点时不删除旧节点,
  6. * 删除节点时不做删除操作
  7. * false-加入相同节点时删除旧节点
  8. * 删除节点时做删除操作
  9. */
  10. int version;
  11. };
struct vlist_tree {
	struct avl_tree avl;

	vlist_update_cb update;     /** 初始化时调用者设置 */
	bool keep_old;				/** true-加入相同节点时不删除旧节点,false-删除
	bool no_delete;				/** true-加入相同节点时不删除旧节点,
                                 *       删除节点时不做删除操作
                                 *  false-加入相同节点时删除旧节点
                                 *        删除节点时做删除操作
                                 */
	int version;
};
  1. struct vlist_node {
  2. struct avl_node avl;
  3. int version;
  4. };
struct vlist_node {
	struct avl_node avl;
	int version;
};

初始化

  1. void vlist_init(struct vlist_tree *tree, avl_tree_comp cmp, vlist_update_cb update)
void vlist_init(struct vlist_tree *tree, avl_tree_comp cmp, vlist_update_cb update)

基本操作

加入

  1. void vlist_add(struct vlist_tree *tree, struct vlist_node *node, const void *key)
void vlist_add(struct vlist_tree *tree, struct vlist_node *node, const void *key)

删除

  1. void vlist_delete(struct vlist_tree *tree, struct vlist_node *node)
void vlist_delete(struct vlist_tree *tree, struct vlist_node *node)


清除

  1. void vlist_flush(struct vlist_tree *tree)
  2. void vlist_flush_all(struct vlist_tree *tree)
void vlist_flush(struct vlist_tree *tree)
void vlist_flush_all(struct vlist_tree *tree)


查找

  1. #define vlist_find(tree, name, element, node_member)
#define vlist_find(tree, name, element, node_member)

遍历

  1. #define vlist_for_each_element(tree, element, node_member)
#define vlist_for_each_element(tree, element, node_member)

更新回调函数


增加和删除节点被回调

  1. typedef void (*vlist_update_cb)(struct vlist_tree *tree,
  2. struct vlist_node *node_new,
  3.    struct vlist_node *node_old);
typedef void (*vlist_update_cb)(struct vlist_tree *tree,
								struct vlist_node *node_new,
  				struct vlist_node *node_old);

Key/Value存储 - kvlist.c/h

数据结构

  1. struct kvlist {
  2. struct avl_tree avl;
  3. int (*get_len)(struct kvlist *kv, const void *data);
  4. };
struct kvlist {
	struct avl_tree avl;
	int (*get_len)(struct kvlist *kv, const void *data);
};
  1. struct kvlist_node {
  2. struct avl_node avl;
  3. char data[0] __attribute__((aligned(4)));
  4. };
struct kvlist_node {
	struct avl_node avl;
	char data[0] __attribute__((aligned(4)));
};

初始化/销毁

  1. void kvlist_init(struct kvlist *kv, int (*get_len)(struct kvlist *kv, const void *data))
  2. void kvlist_free(struct kvlist *kv)
void kvlist_init(struct kvlist *kv, int (*get_len)(struct kvlist *kv, const void *data))
void kvlist_free(struct kvlist *kv)


基本操作

加入

  1. void kvlist_set(struct kvlist *kv, const char *name, const void *data)
void kvlist_set(struct kvlist *kv, const char *name, const void *data)

删除

  1. bool kvlist_delete(struct kvlist *kv, const char *name)
bool kvlist_delete(struct kvlist *kv, const char *name)

获取

  1. void *kvlist_get(struct kvlist *kv, const char *name)
void *kvlist_get(struct kvlist *kv, const char *name)

遍历

  1. #define kvlist_for_each(kv, name, value)
#define kvlist_for_each(kv, name, value)

回调用函数

加入操作时被回调,初始化时由调用者设置,用于计算data的内存空间大小

  1. int (*get_len)(struct kvlist *kv, const void *data)
int (*get_len)(struct kvlist *kv, const void *data)


BLOB二进制大对象 - blob.c/h

数据结构

  1. struct blob_attr {
  2. uint32_t id_len; /** 高1位为extend标志,高7位存储id,
  3. * 低24位存储data的内存大小 */
  4. char data[];
  5. } __packed;
struct blob_attr {
	uint32_t id_len;	/** 高1位为extend标志,高7位存储id,
                         *  低24位存储data的内存大小 */
	char data[];
} __packed;
  1. struct blob_attr_info {
  2. unsigned int type;
  3. unsigned int minlen;
  4. unsigned int maxlen;
  5. bool (*validate)(const struct blob_attr_info *, struct blob_attr *);
  6. };
struct blob_attr_info {
	unsigned int type;
	unsigned int minlen;
	unsigned int maxlen;
	bool (*validate)(const struct blob_attr_info *, struct blob_attr *);
};
  1. struct blob_buf {
  2. struct blob_attr *head;
  3. bool (*grow)(struct blob_buf *buf, int minlen);
  4. int buflen;
  5. void *buf;
  6. };
struct blob_buf {
	struct blob_attr *head;
	bool (*grow)(struct blob_buf *buf, int minlen);
	int buflen;
	void *buf;
};

存储结构


接口

获取BLOB属性信息

  1. /**
  2. * 返回指向BLOB属性数据区指针
  3. */
  4. static inline void * blob_data(const struct blob_attr *attr)
/**
 * 返回指向BLOB属性数据区指针
 */
static inline void * blob_data(const struct blob_attr *attr)
  1. /**
  2. * 返回BLOB属性ID
  3. */
  4. static inline unsigned int blob_id(const struct blob_attr *attr)
/**
 * 返回BLOB属性ID
 */
static inline unsigned int blob_id(const struct blob_attr *attr)
  1. /**
  2. * 判断BLOB属性扩展标志是否为真
  3. */
  4. static inline bool blob_is_extended(const struct blob_attr *attr)
/**
 * 判断BLOB属性扩展标志是否为真
 */
static inline bool blob_is_extended(const struct blob_attr *attr)
  1. /**
  2. * 返回BLOB属性有效存储空间大小
  3. */
  4. static inline unsigned int blob_len(const struct blob_attr *attr)
/**
 * 返回BLOB属性有效存储空间大小
 */
static inline unsigned int blob_len(const struct blob_attr *attr)
  1. /*
  2. * 返回BLOB属性完全存储空间大小(包括头部)
  3. */
  4. static inline unsigned int blob_raw_len(const struct blob_attr *attr)
/*
 * 返回BLOB属性完全存储空间大小(包括头部)
 */
static inline unsigned int blob_raw_len(const struct blob_attr *attr)
  1. /*
  2. * 返回BLOB属性填补后存储空间大小(包括头部)
  3. */
  4. static inline unsigned int blob_pad_len(const struct blob_attr *attr)
/*
 * 返回BLOB属性填补后存储空间大小(包括头部)
 */
static inline unsigned int blob_pad_len(const struct blob_attr *attr)

获取BLOB数据信息

  1. static inline uint8_t blob_get_u8(const struct blob_attr *attr)
  2. static inline uint16_t blob_get_u16(const struct blob_attr *attr)
  3. static inline uint32_t blob_get_u32(const struct blob_attr *attr)
  4. static inline uint64_t blob_get_u64(const struct blob_attr *attr)
  5. static inline int8_t blob_get_int8(const struct blob_attr *attr)
  6. static inline int16_t blob_get_int16(const struct blob_attr *attr)
  7. static inline int32_t blob_get_int32(const struct blob_attr *attr)
  8. static inline int64_t blob_get_int64(const struct blob_attr *attr)
  9. static inline const char * blob_get_string(const struct blob_attr *attr)
static inline uint8_t blob_get_u8(const struct blob_attr *attr)

static inline uint16_t blob_get_u16(const struct blob_attr *attr)

static inline uint32_t blob_get_u32(const struct blob_attr *attr)

static inline uint64_t blob_get_u64(const struct blob_attr *attr)

static inline int8_t blob_get_int8(const struct blob_attr *attr)

static inline int16_t blob_get_int16(const struct blob_attr *attr)

static inline int32_t blob_get_int32(const struct blob_attr *attr)

static inline int64_t blob_get_int64(const struct blob_attr *attr)

static inline const char * blob_get_string(const struct blob_attr *attr)


设置BLOB数据信息

  1. static inline struct blob_attr *
  2.   blob_put_string(struct blob_buf *buf, int id, const char *str)
  3.   
  4. static inline struct blob_attr *
  5.   blob_put_u8(struct blob_buf *buf, int id, uint8_t val)
  6.   
  7. static inline struct blob_attr *
  8.   blob_put_u16(struct blob_buf *buf, int id, uint16_t val)
  9.   
  10. static inline struct blob_attr *
  11.   blob_put_u32(struct blob_buf *buf, int id, uint32_t val)
  12.   
  13. static inline struct blob_attr *
  14.   blob_put_u64(struct blob_buf *buf, int id, uint64_t val)
static inline struct blob_attr *
  blob_put_string(struct blob_buf *buf, int id, const char *str)
  
static inline struct blob_attr *
  blob_put_u8(struct blob_buf *buf, int id, uint8_t val)
  
static inline struct blob_attr *
  blob_put_u16(struct blob_buf *buf, int id, uint16_t val)
  
static inline struct blob_attr *
  blob_put_u32(struct blob_buf *buf, int id, uint32_t val)
  
static inline struct blob_attr *
  blob_put_u64(struct blob_buf *buf, int id, uint64_t val)
  1. #define blob_put_int8 blob_put_u8
  2. #define blob_put_int16 blob_put_u16
  3. #define blob_put_int32 blob_put_u32
  4. #define blob_put_int64 blob_put_u64
#define blob_put_int8	blob_put_u8
#define blob_put_int16	blob_put_u16
#define blob_put_int32	blob_put_u32
#define blob_put_int64	blob_put_u64
  1. struct blob_attr *
  2.   blob_put(struct blob_buf *buf, int id, const void *ptr, unsigned int len)
  3. /**
  4. * ptr - 指向struct blob_attr
  5. */
  6. struct blob_attr *
  7.   blob_put_raw(struct blob_buf *buf, const void *ptr, unsigned int len)
struct blob_attr *
  blob_put(struct blob_buf *buf, int id, const void *ptr, unsigned int len)

/**
 * ptr - 指向struct blob_attr
 */
struct blob_attr *
  blob_put_raw(struct blob_buf *buf, const void *ptr, unsigned int len)

遍历

  1. #define __blob_for_each_attr(pos, attr, rem)
  2. #define blob_for_each_attr(pos, attr, rem)
#define __blob_for_each_attr(pos, attr, rem)
#define blob_for_each_attr(pos, attr, rem)

复制

  1. struct blob_attr * blob_memdup(struct blob_attr *attr)
struct blob_attr * blob_memdup(struct blob_attr *attr)

数据类型判断

  1. enum {
  2. BLOB_ATTR_UNSPEC,
  3. BLOB_ATTR_NESTED, /** 嵌套 */
  4. BLOB_ATTR_BINARY,
  5. BLOB_ATTR_STRING,
  6. BLOB_ATTR_INT8,
  7. BLOB_ATTR_INT16,
  8. BLOB_ATTR_INT32,
  9. BLOB_ATTR_INT64,
  10. BLOB_ATTR_LAST
  11. };
  12. bool blob_check_type(const void *ptr, unsigned int len, int type)
enum {
	BLOB_ATTR_UNSPEC,
	BLOB_ATTR_NESTED,  /** 嵌套 */
	BLOB_ATTR_BINARY,
	BLOB_ATTR_STRING,
	BLOB_ATTR_INT8,
	BLOB_ATTR_INT16,
	BLOB_ATTR_INT32,
	BLOB_ATTR_INT64,
	BLOB_ATTR_LAST
};
bool blob_check_type(const void *ptr, unsigned int len, int type)

嵌套操作

  1. void * blob_nest_start(struct blob_buf *buf, int id)
  2. Void blob_nest_end(struct blob_buf *buf, void *cookie)
void * blob_nest_start(struct blob_buf *buf, int id)
Void blob_nest_end(struct blob_buf *buf, void *cookie)

判断

  1. bool blob_attr_equal(const struct blob_attr *a1, const struct blob_attr *a2)
bool blob_attr_equal(const struct blob_attr *a1, const struct blob_attr *a2)

初始化/销毁

  1. /**
  2. * 初始化BLOB buffer
  3. */
  4. int blob_buf_init(struct blob_buf *buf, int id)
  5. /**
  6. * 销毁BLOB buffer
  7. */
  8. void blob_buf_free(struct blob_buf *buf)
/**
 * 初始化BLOB buffer
 */
int blob_buf_init(struct blob_buf *buf, int id)

/**
 * 销毁BLOB buffer
 */
void blob_buf_free(struct blob_buf *buf)

解析BLOB

  1. /**
  2. * 从attr串中根据info策略过滤,得到的结果存储在data属性数组中
  3. *
  4. * @param attr 输入BLOB属性串
  5. * @param data 输出BLOB属性数组
  6. * @param info 属性过滤策略
  7. * @param max data数组大小
  8. */
  9. int blob_parse(struct blob_attr *attr, struct blob_attr **data,
  10. const struct blob_attr_info *info, int max)
/**
 * 从attr串中根据info策略过滤,得到的结果存储在data属性数组中
 *
 * @param  attr 输入BLOB属性串
 * @param  data 输出BLOB属性数组
 * @param  info 属性过滤策略
 * @param  max data数组大小
 */
int blob_parse(struct blob_attr *attr, struct blob_attr **data, 
               const struct blob_attr_info *info, int max)

BLOB消息对象 - blobmsg.c/h

数据结构

  1. struct blobmsg_hdr {
  2. uint16_t namelen;
  3. uint8_t name[];
  4. } __packed;
  5. struct blobmsg_policy {
  6. const char *name;
  7. enum blobmsg_type type;
  8. };
struct blobmsg_hdr {
	uint16_t namelen;
	uint8_t name[];
} __packed;

struct blobmsg_policy {
	const char *name;
	enum blobmsg_type type;
};

存储结构


消息类型

  1. enum blobmsg_type {
  2. BLOBMSG_TYPE_UNSPEC,
  3. BLOBMSG_TYPE_ARRAY,
  4. BLOBMSG_TYPE_TABLE,
  5. BLOBMSG_TYPE_STRING,
  6. BLOBMSG_TYPE_INT64,
  7. BLOBMSG_TYPE_INT32,
  8. BLOBMSG_TYPE_INT16,
  9. BLOBMSG_TYPE_INT8,
  10. __BLOBMSG_TYPE_LAST,
  11. BLOBMSG_TYPE_LAST = __BLOBMSG_TYPE_LAST - 1,
  12. BLOBMSG_TYPE_BOOL = BLOBMSG_TYPE_INT8,
  13. };
enum blobmsg_type {
	BLOBMSG_TYPE_UNSPEC,
	BLOBMSG_TYPE_ARRAY,
	BLOBMSG_TYPE_TABLE,
	BLOBMSG_TYPE_STRING,
	BLOBMSG_TYPE_INT64,
	BLOBMSG_TYPE_INT32,
	BLOBMSG_TYPE_INT16,
	BLOBMSG_TYPE_INT8,
	__BLOBMSG_TYPE_LAST,
	BLOBMSG_TYPE_LAST = __BLOBMSG_TYPE_LAST - 1,
	BLOBMSG_TYPE_BOOL = BLOBMSG_TYPE_INT8,
};

接口

基本操作

  1. /**
  2. * 根据BLOB消息名字长度计算出blobmsg头部大小
  3. */
  4. static inline int blobmsg_hdrlen(unsigned int namelen)
  5. /**
  6. * 获取BLOB消息名字
  7. */
  8. static inline const char *blobmsg_name(const struct blob_attr *attr)
  9. /**
  10. * 获取BLOB消息类型
  11. */
  12. static inline int blobmsg_type(const struct blob_attr *attr)
  13. /**
  14. * 获取BLOB消息数据内容
  15. */
  16. static inline void *blobmsg_data(const struct blob_attr *attr)
  17. /**
  18. * 获取BLOB消息数据内容大小
  19. */
  20. static inline int blobmsg_data_len(const struct blob_attr *attr)
  21. static inline int blobmsg_len(const struct blob_attr *attr)
/**
 * 根据BLOB消息名字长度计算出blobmsg头部大小
 */
static inline int blobmsg_hdrlen(unsigned int namelen)

/**
 * 获取BLOB消息名字
 */
static inline const char *blobmsg_name(const struct blob_attr *attr)

/**
 * 获取BLOB消息类型
 */
static inline int blobmsg_type(const struct blob_attr *attr)

/**
 * 获取BLOB消息数据内容
 */
static inline void *blobmsg_data(const struct blob_attr *attr)

/**
 * 获取BLOB消息数据内容大小
 */
static inline int blobmsg_data_len(const struct blob_attr *attr)
static inline int blobmsg_len(const struct blob_attr *attr)

数据类型判断

  1. /**
  2. * 判断BLOBMSG属性类型是否合法
  3. */
  4. bool blobmsg_check_attr(const struct blob_attr *attr, bool name)
/**
 * 判断BLOBMSG属性类型是否合法
 */
bool blobmsg_check_attr(const struct blob_attr *attr, bool name)

设置

  1. int blobmsg_add_field(struct blob_buf *buf, int type, const char *name,
  2. const void *data, unsigned int len)
  3. static inline int
  4. blobmsg_add_u8(struct blob_buf *buf, const char *name, uint8_t val)
  5. static inline int
  6. blobmsg_add_u16(struct blob_buf *buf, const char *name, uint16_t val)
  7. static inline int
  8. blobmsg_add_u32(struct blob_buf *buf, const char *name, uint32_t val)
  9. static inline int
  10. blobmsg_add_u64(struct blob_buf *buf, const char *name, uint64_t val)
  11. static inline int
  12. blobmsg_add_string(struct blob_buf *buf, const char *name, const char *string)
  13. static inline int
  14. blobmsg_add_blob(struct blob_buf *buf, struct blob_attr *attr)
  15. /**
  16. * 格式化设备BLOGMSG
  17. */
  18. void blobmsg_printf(struct blob_buf *buf, const char *name, const char *format, ...)
int blobmsg_add_field(struct blob_buf *buf, int type, const char *name,
                      const void *data, unsigned int len)

static inline int
blobmsg_add_u8(struct blob_buf *buf, const char *name, uint8_t val)

static inline int
blobmsg_add_u16(struct blob_buf *buf, const char *name, uint16_t val)

static inline int
blobmsg_add_u32(struct blob_buf *buf, const char *name, uint32_t val)

static inline int
blobmsg_add_u64(struct blob_buf *buf, const char *name, uint64_t val)

static inline int
blobmsg_add_string(struct blob_buf *buf, const char *name, const char *string)

static inline int
blobmsg_add_blob(struct blob_buf *buf, struct blob_attr *attr)

/**
 * 格式化设备BLOGMSG
 */
void blobmsg_printf(struct blob_buf *buf, const char *name, const char *format, ...)

获取

  1. static inline uint8_t blobmsg_get_u8(struct blob_attr *attr)
  2. static inline bool blobmsg_get_bool(struct blob_attr *attr)
  3. static inline uint16_t blobmsg_get_u16(struct blob_attr *attr)
  4. static inline uint32_t blobmsg_get_u32(struct blob_attr *attr)
  5. static inline uint64_t blobmsg_get_u64(struct blob_attr *attr)
  6. static inline char *blobmsg_get_string(struct blob_attr *attr)
static inline uint8_t blobmsg_get_u8(struct blob_attr *attr)
static inline bool blobmsg_get_bool(struct blob_attr *attr)
static inline uint16_t blobmsg_get_u16(struct blob_attr *attr)
static inline uint32_t blobmsg_get_u32(struct blob_attr *attr)
static inline uint64_t blobmsg_get_u64(struct blob_attr *attr)
static inline char *blobmsg_get_string(struct blob_attr *attr)

创建

  1. /**
  2. * 创建BLOBMSG,返回数据区开始地址
  3. */
  4. void *blobmsg_alloc_string_buffer(struct blob_buf *buf, const char *name,
  5.   unsigned int maxlen)
  6. /**
  7. * 扩大BLOGMSG,返回数据区开始地址
  8. */
  9. void *blobmsg_realloc_string_buffer(struct blob_buf *buf, unsigned int maxlen)
  10. void blobmsg_add_string_buffer(struct blob_buf *buf)
/**
 * 创建BLOBMSG,返回数据区开始地址
 */
void *blobmsg_alloc_string_buffer(struct blob_buf *buf, const char *name, 
  unsigned int maxlen)

/**
 * 扩大BLOGMSG,返回数据区开始地址
 */
void *blobmsg_realloc_string_buffer(struct blob_buf *buf, unsigned int maxlen)

void blobmsg_add_string_buffer(struct blob_buf *buf)

遍历

  1. #define blobmsg_for_each_attr(pos, attr, rem)
#define blobmsg_for_each_attr(pos, attr, rem)

嵌套操作

  1. static inline void * blobmsg_open_array(struct blob_buf *buf, const char *name)
  2. static inline void blobmsg_close_array(struct blob_buf *buf, void *cookie)
  3. static inline void *blobmsg_open_table(struct blob_buf *buf, const char *name)
  4. static inline void blobmsg_close_table(struct blob_buf *buf, void *cookie)
static inline void * blobmsg_open_array(struct blob_buf *buf, const char *name)
static inline void blobmsg_close_array(struct blob_buf *buf, void *cookie)

static inline void *blobmsg_open_table(struct blob_buf *buf, const char *name)
static inline void blobmsg_close_table(struct blob_buf *buf, void *cookie)

解析BLOGMSG

  1. /**
  2. * 从data BLOGMSG串中根据policy策略过滤,得到的结果存储在tb BLOGATTR数组中
  3. *
  4. * @param policy 过滤策略
  5. * @param policy_len 策略个数
  6. * @param tb 返回属性数据
  7. * @param len data属性个数
  8. */
  9. int blobmsg_parse(const struct blobmsg_policy *policy, int policy_len,
  10. struct blob_attr **tb, void *data, unsigned int len)
/**
 * 从data BLOGMSG串中根据policy策略过滤,得到的结果存储在tb BLOGATTR数组中
 *
 * @param  policy 过滤策略
 * @param  policy_len 策略个数
 * @param  tb 返回属性数据
 * @param  len data属性个数
 */
int blobmsg_parse(const struct blobmsg_policy *policy, int policy_len,
                  struct blob_attr **tb, void *data, unsigned int len)

事件处理循环 - uloop.c/h

接口说明

主框架

  1. /**
  2. * 初始化事件循环
  3. */
  4. int uloop_init(void)
  5. /**
  6. * 事件循环主处理入口
  7. */
  8. void uloop_run(void)
  9. /**
  10. * 销毁事件循环
  11. */
  12. void uloop_done(void)
/**
 * 初始化事件循环
 */
int uloop_init(void)

/**
 * 事件循环主处理入口
 */
void uloop_run(void)

/**
 * 销毁事件循环
 */
void uloop_done(void)

描述符事件

  1. /**
  2. * 注册一个新描述符到事件处理循环
  3. */
  4. int uloop_fd_add(struct uloop_fd *sock, unsigned int flags)
  5. /**
  6. * 从事件处理循环中销毁指定描述符
  7. */
  8. int uloop_fd_delete(struct uloop_fd *sock)
/**
 * 注册一个新描述符到事件处理循环
 */
int uloop_fd_add(struct uloop_fd *sock, unsigned int flags)

/** 
 * 从事件处理循环中销毁指定描述符
 */
int uloop_fd_delete(struct uloop_fd *sock)

定时器事件

  1. /**
  2. * 注册一个新定时器
  3. */
  4. int uloop_timeout_add(struct uloop_timeout *timeout)
  5. /**
  6. * 设置定时器超时时间(毫秒),并添加
  7. */
  8. int uloop_timeout_set(struct uloop_timeout *timeout, int msecs)
  9. /**
  10. * 销毁指定定时器
  11. */
  12. int uloop_timeout_cancel(struct uloop_timeout *timeout)
  13. /**
  14. * 获取定时器还剩多长时间超时
  15. */
  16. int uloop_timeout_remaining(struct uloop_timeout *timeout)
/**
 * 注册一个新定时器
 */
int uloop_timeout_add(struct uloop_timeout *timeout)

/**
 * 设置定时器超时时间(毫秒),并添加
 */
int uloop_timeout_set(struct uloop_timeout *timeout, int msecs)

/**
 * 销毁指定定时器
 */
int uloop_timeout_cancel(struct uloop_timeout *timeout)

/**
 * 获取定时器还剩多长时间超时
 */
int uloop_timeout_remaining(struct uloop_timeout *timeout)

进程事件

  1. /**
  2. * 注册新进程到事件处理循环
  3. */
  4. int uloop_process_add(struct uloop_process *p)
  5. /**
  6. * 从事件处理循环中销毁指定进程
  7. */
  8. int uloop_process_delete(struct uloop_process *p)
/**
 * 注册新进程到事件处理循环
 */
int uloop_process_add(struct uloop_process *p)

/**
 * 从事件处理循环中销毁指定进程
 */
int uloop_process_delete(struct uloop_process *p)

数据结构

描述符

  1. struct uloop_fd
  2. {
  3. uloop_fd_handler cb; /** 文件描述符,调用者初始化 */
  4. int fd; /** 文件描述符,调用者初始化 */
  5. bool eof;
  6. bool error;
  7. bool registered;
  8. uint8_t flags;
  9. };
struct uloop_fd
{
	uloop_fd_handler cb;				/** 文件描述符,调用者初始化 */
	int fd;							/** 文件描述符,调用者初始化 */
	bool eof; 						
	bool error;						
	bool registered;					
	uint8_t flags;						
};

定时器

  1. struct uloop_timeout
  2. {
  3. struct list_head list;
  4. bool pending;
  5. uloop_timeout_handler cb; /** 文件描述符, 调用者初始化 */
  6. struct timeval time; /** 文件描述符, 调用者初始化 */
  7. };
struct uloop_timeout
{
	struct list_head list;				
	bool pending;				
	uloop_timeout_handler cb;			/** 文件描述符, 调用者初始化 */
	struct timeval time;				/** 文件描述符, 调用者初始化 */
};

进程

  1. struct uloop_process
  2. {
  3. struct list_head list;
  4. bool pending;
  5. uloop_process_handler cb; /** 文件描述符, 调用者初始化 */
  6. pid_t pid; /** 文件描述符, 调用者初始化 */
  7. };
struct uloop_process
{
	struct list_head list;				
	bool pending;					
	uloop_process_handler cb;		/** 文件描述符, 调用者初始化 */
	pid_t pid;						/** 文件描述符, 调用者初始化 */
};

事件回调函数

描述符

  1. typedef void (*uloop_fd_handler)(struct uloop_fd *u, unsigned int events)
typedef void (*uloop_fd_handler)(struct uloop_fd *u, unsigned int events)

定时器

  1. typedef void (*uloop_timeout_handler)(struct uloop_timeout *t)
typedef void (*uloop_timeout_handler)(struct uloop_timeout *t)

进程

  1. typedef void (*uloop_process_handler)(struct uloop_process *c, int ret)
typedef void (*uloop_process_handler)(struct uloop_process *c, int ret)

事件标志

  1. #define ULOOP_READ (1 << 0)
  2. #define ULOOP_WRITE (1 << 1)
  3. #define ULOOP_EDGE_TRIGGER (1 << 2)
  4. #define ULOOP_BLOCKING (1 << 3)
  5. #define ULOOP_EVENT_MASK (ULOOP_READ | ULOOP_WRITE)
#define ULOOP_READ		(1 << 0)
#define ULOOP_WRITE		(1 << 1)
#define ULOOP_EDGE_TRIGGER	(1 << 2)
#define ULOOP_BLOCKING		(1 << 3)
#define ULOOP_EVENT_MASK	(ULOOP_READ | ULOOP_WRITE)

流缓冲管理 - ustream.c/h/ustream-fd.c

数据结构

  1.   struct ustream_buf {
  2.    struct ustream_buf *next;
  3.   
  4.    char *data; /** 指向上次操作buff开始地址 */
  5.    char *tail; /** 指向未使用buff开始地址 */
  6.    char *end; /** 指向buf结束地址 */
  7.   
  8.    char head[]; /** 指向buf开始地址 */
  9.   };
  struct ustream_buf {
  	struct ustream_buf *next;
  
  	char *data;		/** 指向上次操作buff开始地址 */
  	char *tail;		/** 指向未使用buff开始地址 */
  	char *end;		/** 指向buf结束地址 */
  
  	char head[];	/** 指向buf开始地址 */
  };
  1.   struct ustream_buf_list {
  2.    struct ustream_buf *head; /** 指向第1块ustream_buf */
  3.    struct ustream_buf *data_tail; /** 指向未使用的ustream_buf */
  4.    struct ustream_buf *tail; /** 指向最后的ustream_buf */
  5.   
  6.    int (*alloc)(struct ustream *s, struct ustream_buf_list *l);
  7.   
  8.    int data_bytes; /** 已用存储空间大小 */
  9.   
  10.    int min_buffers; /** 可存储最小的ustream_buf块个数 */
  11.    int max_buffers; /** 可存储最大的ustream_buf块个数 */
  12.    int buffer_len; /** 每块ustream_buf块存储空间大小 */
  13.   
  14.    int buffers; /** ustream_buf块个数 */
  15.   };
  struct ustream_buf_list {
  	struct ustream_buf *head;       /** 指向第1块ustream_buf */
  	struct ustream_buf *data_tail;  /** 指向未使用的ustream_buf */
  	struct ustream_buf *tail;       /** 指向最后的ustream_buf */
  
  	int (*alloc)(struct ustream *s, struct ustream_buf_list *l);
  
  	int data_bytes;	   /** 已用存储空间大小 */
  
  	int min_buffers;   /** 可存储最小的ustream_buf块个数 */
  	int max_buffers;   /** 可存储最大的ustream_buf块个数 */
  	int buffer_len;	   /** 每块ustream_buf块存储空间大小 */
  
  	int buffers;       /** ustream_buf块个数 */
  };
  1.   struct ustream {
  2.    struct ustream_buf_list r, w;
  3.    struct uloop_timeout state_change;
  4.    struct ustream *next;
  5.   
  6.    /*
  7.    * notify_read: (optional)
  8.    * called by the ustream core to notify that new data is available
  9.    * for reading.
  10.    * must not free the ustream from this callback
  11.    */
  12.    void (*notify_read)(struct ustream *s, int bytes_new);
  13.   
  14.    /*
  15.    * notify_write: (optional)
  16.    * called by the ustream core to notify that some buffered data has
  17.    * been written to the stream.
  18.    * must not free the ustream from this callback
  19.    */
  20.    void (*notify_write)(struct ustream *s, int bytes);
  21.   
  22.    /*
  23.    * notify_state: (optional)
  24.    * called by the ustream implementation to notify that the read
  25.    * side of the stream is closed (eof is set) or there was a write
  26.    * error (write_error is set).
  27.    * will be called again after the write buffer has been emptied when
  28.    * the read side has hit EOF.
  29.    */
  30.    void (*notify_state)(struct ustream *s);
  31.   
  32.    /*
  33.    * write:
  34.    * must be defined by ustream implementation, accepts new write data.
  35.    * 'more' is used to indicate that a subsequent call will provide more
  36.    * data (useful for aggregating writes)
  37.    * returns the number of bytes accepted, or -1 if no more writes can
  38.    * be accepted (link error)
  39.    */
  40.    int (*write)(struct ustream *s, const char *buf, int len, bool more);
  41.   
  42.    /*
  43.    * free: (optional)
  44.    * defined by ustream implementation, tears down the ustream and frees data
  45.    */
  46.    void (*free)(struct ustream *s);
  47.   
  48.    /*
  49.    * set_read_blocked: (optional)
  50.    * defined by ustream implementation, called when the read_blocked flag
  51.    * changes
  52.    */
  53.    void (*set_read_blocked)(struct ustream *s);
  54.   
  55.    /*
  56.    * poll: (optional)
  57.    * defined by the upstream implementation, called to request polling for
  58.    * available data.
  59.    * returns true if data was fetched.
  60.    */
  61.    bool (*poll)(struct ustream *s);
  62.   
  63.    /*
  64.    * ustream user should set this if the input stream is expected
  65.    * to contain string data. the core will keep all data 0-terminated.
  66.    */
  67.    bool string_data; /** 此ustream是否为字符串,true-是;false-否 */
  68.    bool write_error; /** 写出错,true-是;false-否 */
  69.    bool eof, eof_write_done;
  70.   
  71.    enum read_blocked_reason read_blocked;
  72.   };
  struct ustream {
  	struct ustream_buf_list r, w;
  	struct uloop_timeout state_change;
  	struct ustream *next;
  
  	/*
  	 * notify_read: (optional)
  	 * called by the ustream core to notify that new data is available
  	 * for reading.
  	 * must not free the ustream from this callback
  	 */
  	void (*notify_read)(struct ustream *s, int bytes_new);
  
  	/*
  	 * notify_write: (optional)
  	 * called by the ustream core to notify that some buffered data has
  	 * been written to the stream.
  	 * must not free the ustream from this callback
  	 */
  	void (*notify_write)(struct ustream *s, int bytes);
  
  	/*
  	 * notify_state: (optional)
  	 * called by the ustream implementation to notify that the read
  	 * side of the stream is closed (eof is set) or there was a write
  	 * error (write_error is set).
  	 * will be called again after the write buffer has been emptied when
  	 * the read side has hit EOF.
  	 */
  	void (*notify_state)(struct ustream *s);
  
  	/*
  	 * write:
  	 * must be defined by ustream implementation, accepts new write data.
  	 * 'more' is used to indicate that a subsequent call will provide more
  	 * data (useful for aggregating writes)
  	 * returns the number of bytes accepted, or -1 if no more writes can
  	 * be accepted (link error)
  	 */
  	int (*write)(struct ustream *s, const char *buf, int len, bool more);
  
  	/*
  	 * free: (optional)
  	 * defined by ustream implementation, tears down the ustream and frees data
  	 */
  	void (*free)(struct ustream *s);
  
  	/*
  	 * set_read_blocked: (optional)
  	 * defined by ustream implementation, called when the read_blocked flag
  	 * changes
  	 */
  	void (*set_read_blocked)(struct ustream *s);
  
  	/*
  	 * poll: (optional)
  	 * defined by the upstream implementation, called to request polling for
  	 * available data.
  	 * returns true if data was fetched.
  	 */
  	bool (*poll)(struct ustream *s);
  
  	/*
  	 * ustream user should set this if the input stream is expected
  	 * to contain string data. the core will keep all data 0-terminated.
  	 */
  	bool string_data;     /** 此ustream是否为字符串,true-是;false-否 */
  	bool write_error;	  /** 写出错,true-是;false-否 */
  	bool eof, eof_write_done;
  
  	enum read_blocked_reason read_blocked;
  };
  1.   struct ustream_fd {
  2.    struct ustream stream;
  3.    struct uloop_fd fd;
  4.   };
  struct ustream_fd {
  	struct ustream stream;
  	struct uloop_fd fd;
  };

存储结构



接口

初始/销毁

  1. /**
  2. * ustream_fd_init: create a file descriptor ustream (uses uloop)
  3. */
  4. void ustream_fd_init(struct ustream_fd *s, int fd)
  5. /**
  6. * ustream_init_defaults: fill default callbacks and options
  7. */
  8. void ustream_init_defaults(struct ustream *s)
  9. /**
  10. * ustream_free: free all buffers and data associated with a ustream
  11. */
  12. void ustream_free(struct ustream *s)
/**
 * ustream_fd_init: create a file descriptor ustream (uses uloop) 
 */
void ustream_fd_init(struct ustream_fd *s, int fd)

/**
 * ustream_init_defaults: fill default callbacks and options 
 */
void ustream_init_defaults(struct ustream *s)

/**
 * ustream_free: free all buffers and data associated with a ustream 
 */
void ustream_free(struct ustream *s)

写入read buffer

  1. /*
  2. * ustream_reserve: allocate rx buffer space
  3. * 分配len大小的read buffer可用内存空间,与ustream_fill_read()配合使用
  4. *
  5. * len: hint for how much space is needed (not guaranteed to be met)
  6. * maxlen: pointer to where the actual buffer size is going to be stored
  7. */
  8. char *ustream_reserve(struct ustream *s, int len, int *maxlen)
/*
 * ustream_reserve: allocate rx buffer space
 * 		分配len大小的read buffer可用内存空间,与ustream_fill_read()配合使用
 *
 * len: hint for how much space is needed (not guaranteed to be met)
 * maxlen: pointer to where the actual buffer size is going to be stored
 */
char *ustream_reserve(struct ustream *s, int len, int *maxlen)
  1. /**
  2. * ustream_fill_read: mark rx buffer space as filled
  3. * 设置被ustream_reseve()分配read buffer后写入的数据大小,
  4. * 回调notify_read()接口,表示有数据可读
  5. */
  6. void ustream_fill_read(struct ustream *s, int len)
/**
 * ustream_fill_read: mark rx buffer space as filled 
 * 		设置被ustream_reseve()分配read buffer后写入的数据大小,
 *  	回调notify_read()接口,表示有数据可读
 */
void ustream_fill_read(struct ustream *s, int len)

读出read buffer

一般在notify_read()回调接口使用

  1. /*
  2. * ustream_get_read_buf: get a pointer to the next read buffer data
  3. * 获取新一次写入的内容,与ustream_consume()配置使用
  4. */
  5. char *ustream_get_read_buf(struct ustream *s, int *buflen)
/* 
 * ustream_get_read_buf: get a pointer to the next read buffer data 
 * 		获取新一次写入的内容,与ustream_consume()配置使用
 */
char *ustream_get_read_buf(struct ustream *s, int *buflen)
  1. /**
  2. * ustream_consume: remove data from the head of the read buffer
  3. */
  4. void ustream_consume(struct ustream *s, int len)
/**
 * ustream_consume: remove data from the head of the read buffer 
 */
void ustream_consume(struct ustream *s, int len)

操作write buffer

尽最大能力调用write()回调用接口写入,如果超出能力将把未写入的数据存储在write buffer中

  1. /*
  2. * ustream_write: add data to the write buffer
  3. */
  4. int ustream_write(struct ustream *s, const char *buf, int len, bool more)
  5. int ustream_printf(struct ustream *s, const char *format, ...)
  6. int ustream_vprintf(struct ustream *s, const char *format, va_list arg)
/* 
 * ustream_write: add data to the write buffer 
 */
int ustream_write(struct ustream *s, const char *buf, int len, bool more)
int ustream_printf(struct ustream *s, const char *format, ...)
int ustream_vprintf(struct ustream *s, const char *format, va_list arg)

把在write buffer中的数据写入实际地方,调用write()回调接口和notify_write()回调接口。一般在描述符的poll操作中调用,表示当描述符变为可写时立即把上一次未写入的内容进行写入操作。

  1. /*
  2. * ustream_write_pending: attempt to write more data from write buffers
  3. * returns true if all write buffers have been emptied.
  4. */
  5. bool ustream_write_pending(struct ustream *s)
/*
 * ustream_write_pending: attempt to write more data from write buffers
 * returns true if all write buffers have been emptied.
 */
bool ustream_write_pending(struct ustream *s)

任务队列 - runqueue.c/h

数据结构

  1. struct runqueue {
  2. struct safe_list tasks_active; /** 活动任务队列 */
  3. struct safe_list tasks_inactive; /** 不活动任务队列 */
  4. struct uloop_timeout timeout;
  5. int running_tasks; /** 当前活动任务数目 */
  6. int max_running_tasks; /** 允许最大活动任务数目 */
  7. bool stopped; /** 是否停止任务队列 */
  8. bool empty; /** 任务队列(包括活动和不活动)是否为空 */
  9. /* called when the runqueue is emptied */
  10. void (*empty_cb)(struct runqueue *q);
  11. };
struct runqueue {
	struct safe_list tasks_active;		/** 活动任务队列 */
	struct safe_list tasks_inactive; 	/** 不活动任务队列 */
	struct uloop_timeout timeout;

	int running_tasks; 		/** 当前活动任务数目 */
	int max_running_tasks;  /** 允许最大活动任务数目 */
	bool stopped;			/** 是否停止任务队列 */
	bool empty; 			/** 任务队列(包括活动和不活动)是否为空 */

	/* called when the runqueue is emptied */
	void (*empty_cb)(struct runqueue *q);
};
  1. struct runqueue_task_type {
  2. const char *name;
  3. /*
  4. * called when a task is requested to run
  5. *
  6. * The task is removed from the list before this callback is run. It
  7. * can re-arm itself using runqueue_task_add.
  8. */
  9. void (*run)(struct runqueue *q, struct runqueue_task *t);
  10. /*
  11. * called to request cancelling a task
  12. *
  13. * int type is used as an optional hint for the method to be used when
  14. * cancelling the task, e.g. a signal number for processes. Calls
  15. * runqueue_task_complete when done.
  16. */
  17. void (*cancel)(struct runqueue *q, struct runqueue_task *t, int type);
  18. /*
  19. * called to kill a task. must not make any calls to runqueue_task_complete,
  20. * it has already been removed from the list.
  21. */
  22. void (*kill)(struct runqueue *q, struct runqueue_task *t);
  23. };
struct runqueue_task_type {
	const char *name;

	/*
	 * called when a task is requested to run
	 *
	 * The task is removed from the list before this callback is run. It
	 * can re-arm itself using runqueue_task_add.
	 */
	void (*run)(struct runqueue *q, struct runqueue_task *t);

	/*
	 * called to request cancelling a task
	 *
	 * int type is used as an optional hint for the method to be used when
	 * cancelling the task, e.g. a signal number for processes. Calls
	 * runqueue_task_complete when done.
	 */
	void (*cancel)(struct runqueue *q, struct runqueue_task *t, int type);

	/*
	 * called to kill a task. must not make any calls to runqueue_task_complete,
	 * it has already been removed from the list.
	 */
	void (*kill)(struct runqueue *q, struct runqueue_task *t);
};
  1. struct runqueue_task {
  2. struct safe_list list;
  3. const struct runqueue_task_type *type;
  4. struct runqueue *q;
  5. void (*complete)(struct runqueue *q, struct runqueue_task *t);
  6. struct uloop_timeout timeout;
  7. int run_timeout; /** >0表示规定此任务执行只有run_timeout毫秒 */
  8. int cancel_timeout; /** >0表示规则任务延取消操作执行只有run_timeout毫秒*/
  9. int cancel_type;
  10. bool queued; /** 此任务是否已加入任务队列中 */
  11. bool running; /** 此任务是否活动,即已在活动队列中 */
  12. bool cancelled; /** 此任务是否已被取消 */
  13. };
struct runqueue_task {
	struct safe_list list;
	const struct runqueue_task_type *type;
	struct runqueue *q;

	void (*complete)(struct runqueue *q, struct runqueue_task *t);

	struct uloop_timeout timeout;
	int run_timeout; 	/** >0表示规定此任务执行只有run_timeout毫秒 */
	int cancel_timeout; /** >0表示规则任务延取消操作执行只有run_timeout毫秒*/
	int cancel_type;

	bool queued;		/** 此任务是否已加入任务队列中 */
	bool running;		/** 此任务是否活动,即已在活动队列中 */
	bool cancelled;     /** 此任务是否已被取消 */
};
  1. struct runqueue_process {
  2. struct runqueue_task task;
  3. struct uloop_process proc;
  4. };
struct runqueue_process {
	struct runqueue_task task;
	struct uloop_process proc;
};

接口说明

任务队列

  1. /**
  2. * 初始化任务队列
  3. */
  4. void runqueue_init(struct runqueue *q)
  5. /**
  6. * 取消所有任务队列
  7. */
  8. void runqueue_cancel(struct runqueue *q);
  9. /**
  10. * 取消活动中的任务
  11. */
  12. void runqueue_cancel_active(struct runqueue *q);
  13. /**
  14. * 取消不活动的任务
  15. */
  16. void runqueue_cancel_pending(struct runqueue *q);
  17. /**
  18. * 杀死所有任务
  19. */
  20. void runqueue_kill(struct runqueue *q);
  21. /**
  22. * 停止所有任务
  23. */
  24. void runqueue_stop(struct runqueue *q);
  25. /**
  26. * 重新开始任务
  27. */
  28. void runqueue_resume(struct runqueue *q);
/**
 * 初始化任务队列
 */
void runqueue_init(struct runqueue *q)

/** 
 * 取消所有任务队列
 */
void runqueue_cancel(struct runqueue *q);

/** 
 * 取消活动中的任务
 */
void runqueue_cancel_active(struct runqueue *q);

/** 
 * 取消不活动的任务 
 */
void runqueue_cancel_pending(struct runqueue *q);

/**
 * 杀死所有任务
 */
void runqueue_kill(struct runqueue *q);

/** 
 * 停止所有任务
 */
void runqueue_stop(struct runqueue *q);

/**
 * 重新开始任务
 */
void runqueue_resume(struct runqueue *q);

任务操作

  1. /**
  2. * 添加新任务到队列尾
  3. *
  4. * @running true-加入活动队列;false-加入不活动队列
  5. */
  6. void runqueue_task_add(struct runqueue *q, struct runqueue_task *t, bool running);
  7. /**
  8. * 添加新任务到队列头
  9. *
  10. * @running true-加入活动队列;false-加入不活动队列
  11. */
  12. void runqueue_task_add_first(struct runqueue *q, struct runqueue_task *t,
  13.   bool running);
  14. /**
  15. * 完全任务
  16. */
  17. void runqueue_task_complete(struct runqueue_task *t);
  18. /**
  19. * 取消任务
  20. */
  21. void runqueue_task_cancel(struct runqueue_task *t, int type);
  22. /**
  23. * 杀死任务
  24. */
  25. void runqueue_task_kill(struct runqueue_task *t);
/**
 * 添加新任务到队列尾
 *
 * @running true-加入活动队列;false-加入不活动队列
 */
void runqueue_task_add(struct runqueue *q, struct runqueue_task *t, bool running);

/**
 * 添加新任务到队列头
 *
 * @running true-加入活动队列;false-加入不活动队列
 */
void runqueue_task_add_first(struct runqueue *q, struct runqueue_task *t, 
  bool running);

/**
 * 完全任务
 */
void runqueue_task_complete(struct runqueue_task *t);

/**
 * 取消任务
 */
void runqueue_task_cancel(struct runqueue_task *t, int type);

/**
 * 杀死任务
 */
void runqueue_task_kill(struct runqueue_task *t);

进程任务

  1. void runqueue_process_add(struct runqueue *q, struct runqueue_process *p,
  2.   pid_t pid);
  3. /**
  4. * to be used only from runqueue_process callbacks
  5. */
  6. void runqueue_process_cancel_cb(struct runqueue *q, struct runqueue_task *t,
  7.   int type);
  8. void runqueue_process_kill_cb(struct runqueue *q, struct runqueue_task *t);


阅读(3578) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~