acl/lib_acl/include/stdlib/acl_ring.h

255 lines
6.7 KiB
C
Raw Normal View History

2014-11-19 00:25:21 +08:00
#ifndef ACL_RING_INCLUDE_H
#define ACL_RING_INCLUDE_H
#ifdef __cplusplus
extern "C" {
#endif
#include "acl_define.h"
#include <stddef.h>
typedef struct ACL_RING ACL_RING;
/**
* <EFBFBD><EFBFBD><EFBFBD>ݻ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ͷ<EFBFBD><EFBFBD><EFBFBD>
*/
struct ACL_RING {
ACL_RING *succ; /**< successor */
ACL_RING *pred; /**< predecessor */
ACL_RING *parent; /**< the header of all the rings */
int len; /**< the count in the ring */
};
typedef struct ACL_RING_ITER {
ACL_RING *ptr;
} ACL_RING_ITER;
/**
* <EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݻ<EFBFBD>
* @param ring {ACL_RING*} <EFBFBD><EFBFBD><EFBFBD>ݻ<EFBFBD>
*/
ACL_API void acl_ring_init(ACL_RING *ring);
/**
* <EFBFBD><EFBFBD><EFBFBD>õ<EFBFBD>ǰ<EFBFBD><EFBFBD><EFBFBD>ݻ<EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD>ظ<EFBFBD><EFBFBD><EFBFBD>
* @param ring {ACL_RING*} <EFBFBD><EFBFBD><EFBFBD>ݻ<EFBFBD>
* @return {int} <EFBFBD><EFBFBD><EFBFBD>ݻ<EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD>ظ<EFBFBD><EFBFBD><EFBFBD>
*/
ACL_API int acl_ring_size(const ACL_RING *ring);
/**
* <EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͷ<EFBFBD><EFBFBD>
* @param ring {ACL_RING*} <EFBFBD><EFBFBD><EFBFBD>ݻ<EFBFBD>
* @param entry {ACL_RING*} <EFBFBD>µ<EFBFBD>Ԫ<EFBFBD><EFBFBD>
*/
ACL_API void acl_ring_prepend(ACL_RING *ring, ACL_RING *entry);
/**
* <EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>β<EFBFBD><EFBFBD>
* @param ring {ACL_RING*} <EFBFBD><EFBFBD><EFBFBD>ݻ<EFBFBD>
* @param entry {ACL_RING*} <EFBFBD>µ<EFBFBD>Ԫ<EFBFBD><EFBFBD>
*/
ACL_API void acl_ring_append(ACL_RING *ring, ACL_RING *entry);
/**
* <EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD>ش<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݻ<EFBFBD><EFBFBD><EFBFBD>ɾ<EFBFBD><EFBFBD>
* @param entry {ACL_RING*} <EFBFBD><EFBFBD>Ԫ<EFBFBD><EFBFBD>
*/
ACL_API void acl_ring_detach(ACL_RING *entry);
/**
* <EFBFBD>ӻ<EFBFBD><EFBFBD>е<EFBFBD><EFBFBD><EFBFBD>ͷ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><EFBFBD>
* @param ring {ACL_RING*} <EFBFBD><EFBFBD><EFBFBD>ݻ<EFBFBD>
* @return {ACL_RING*} ͷ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD>أ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ؿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʾ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݻ<EFBFBD>Ϊ<EFBFBD><EFBFBD>
*/
ACL_API ACL_RING *acl_ring_pop_head(ACL_RING *ring);
/**
* <EFBFBD>ӻ<EFBFBD><EFBFBD>е<EFBFBD><EFBFBD><EFBFBD>β<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><EFBFBD>
* @param ring {ACL_RING*} <EFBFBD><EFBFBD><EFBFBD>ݻ<EFBFBD>
* @return {ACL_RING*} β<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD>أ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ؿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʾ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݻ<EFBFBD>Ϊ<EFBFBD><EFBFBD>
*/
ACL_API ACL_RING *acl_ring_pop_tail(ACL_RING *ring);
/*-------------------- һЩ<D2BB><D0A9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݵĺ<DDB5><C4BA><EFBFBD><EFBFBD><EFBFBD> --------------------------------*/
/**
* <EFBFBD><EFBFBD><EFBFBD>ص<EFBFBD>ǰ<EFBFBD><EFBFBD>Ԫ<EFBFBD>ص<EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><EFBFBD>
*/
#define ACL_RING_SUCC(c) ((c)->succ)
#define acl_ring_succ ACL_RING_SUCC
/**
* <EFBFBD><EFBFBD><EFBFBD>ص<EFBFBD>ǰ<EFBFBD><EFBFBD>Ԫ<EFBFBD>ص<EFBFBD>ǰһ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><EFBFBD>
*/
#define ACL_RING_PRED(c) ((c)->pred)
#define acl_ring_pred ACL_RING_PRED
/**
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD>ת<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD>õ<EFBFBD><EFBFBD>Զ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>͵<EFBFBD>ָ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ
* @param ring_ptr {ACL_RING*} <EFBFBD><EFBFBD>Ԫ<EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD>
* @param app_type Ӧ<EFBFBD><EFBFBD><EFBFBD>Զ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param ring_member {ACL_RING*} <EFBFBD><EFBFBD>Ԫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD><EFBFBD><EFBFBD>Զ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>еij<EFBFBD>Ա<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @return {app_type*} Ӧ<EFBFBD><EFBFBD><EFBFBD>Զ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>͵Ķ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ
*/
#define ACL_RING_TO_APPL(ring_ptr, app_type, ring_member) \
((app_type *) (((char *) (ring_ptr)) - offsetof(app_type,ring_member)))
#define acl_ring_to_appl ACL_RING_TO_APPL
/**
* <EFBFBD><EFBFBD>ͷ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>β<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݻ<EFBFBD><EFBFBD>е<EFBFBD><EFBFBD><EFBFBD><EFBFBD>л<EFBFBD>Ԫ<EFBFBD><EFBFBD>
* @param iter {ACL_RING_ITER}
* @param head_ptr {ACL_RING*} <EFBFBD><EFBFBD><EFBFBD>ݻ<EFBFBD><EFBFBD><EFBFBD>ͷָ<EFBFBD><EFBFBD>
* @example:
typedef struct {
char name[32];
ACL_RING entry;
} DUMMY;
void test()
{
ACL_RING head;
DUMMY *dummy;
ACL_RING_ITER iter;
int i;
acl_ring_init(&head);
for (i = 0; i < 10; i++) {
dummy = (DUMMY*) acl_mycalloc(1, sizeof(DUMMY));
snprintf(dummy->name, sizeof(dummy->name), "dummy:%d", i);
acl_ring_append(&head, &dummy->entry);
}
acl_ring_foreach(iter, &head) {
dummy = acl_ring_to_appl(iter.ptr, DUMMY, entry);
printf("name: %s\n", dummy->name);
}
while (1) {
iter.ptr = acl_ring_pop_head(&head);
if (iter.ptr == NULL)
break;
dummy = acl_ring_to_appl(iter.ptr, DUMMY, entry);
acl_myfree(dummy);
}
}
*/
#define ACL_RING_FOREACH(iter, head_ptr) \
for ((iter).ptr = acl_ring_succ((head_ptr)); (iter).ptr != (head_ptr); \
(iter).ptr = acl_ring_succ((iter).ptr))
#define acl_ring_foreach ACL_RING_FOREACH
/**
* <EFBFBD><EFBFBD>β<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͷ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݻ<EFBFBD><EFBFBD>е<EFBFBD><EFBFBD><EFBFBD><EFBFBD>л<EFBFBD>Ԫ<EFBFBD><EFBFBD>
* @param iter {ACL_RING_ITER}
* @param head_ptr {ACL_RING*} <EFBFBD><EFBFBD><EFBFBD>ݻ<EFBFBD><EFBFBD><EFBFBD>ͷָ<EFBFBD><EFBFBD>
*/
#define ACL_RING_FOREACH_REVERSE(iter, head_ptr) \
for ((iter).ptr = acl_ring_pred((head_ptr)); (iter).ptr != (head_ptr); \
(iter).ptr = acl_ring_pred((iter).ptr))
#define acl_ring_foreach_reverse ACL_RING_FOREACH_REVERSE
/**
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݻ<EFBFBD><EFBFBD>е<EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD>
* @param head {ACL_RING*} <EFBFBD><EFBFBD>ͷָ<EFBFBD><EFBFBD>
* @return {ACL_RING*} NULL: <EFBFBD><EFBFBD>Ϊ<EFBFBD><EFBFBD>
*/
#define ACL_RING_FIRST(head) \
(acl_ring_succ(head) != (head) ? acl_ring_succ(head) : 0)
#define acl_ring_first ACL_RING_FIRST
/**
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݻ<EFBFBD><EFBFBD><EFBFBD>ͷ<EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD>ͬʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ת<EFBFBD><EFBFBD>Ӧ<EFBFBD><EFBFBD><EFBFBD>Զ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>͵Ķ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ
* @param head {ACL_RING*} <EFBFBD><EFBFBD>ͷָ<EFBFBD><EFBFBD>
* @param app_type Ӧ<EFBFBD><EFBFBD><EFBFBD>Զ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param ring_member {ACL_RING*} <EFBFBD><EFBFBD>Ԫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD><EFBFBD><EFBFBD>Զ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>еij<EFBFBD>Ա<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @return {app_type*} Ӧ<EFBFBD><EFBFBD><EFBFBD>Զ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>͵Ķ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ
*/
#define ACL_RING_FIRST_APPL(head, app_type, ring_member) \
(acl_ring_succ(head) != (head) ? \
ACL_RING_TO_APPL(acl_ring_succ(head), app_type, ring_member) : 0)
#define acl_ring_first_appl ACL_RING_FIRST_APPL
/**
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݻ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD>
* @param head {ACL_RING*} <EFBFBD><EFBFBD>ͷָ<EFBFBD><EFBFBD>
* @return {ACL_RING*} NULL: <EFBFBD><EFBFBD>Ϊ<EFBFBD><EFBFBD>
*/
#define ACL_RING_LAST(head) \
(acl_ring_pred(head) != (head) ? acl_ring_pred(head) : 0)
#define acl_ring_last ACL_RING_LAST
/**
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݻ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD>ͬʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ת<EFBFBD><EFBFBD>Ӧ<EFBFBD><EFBFBD><EFBFBD>Զ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>͵Ķ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ
* @param head {ACL_RING*} <EFBFBD><EFBFBD>ͷָ<EFBFBD><EFBFBD>
* @param app_type Ӧ<EFBFBD><EFBFBD><EFBFBD>Զ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param ring_member {ACL_RING*} <EFBFBD><EFBFBD>Ԫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD><EFBFBD><EFBFBD>Զ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>еij<EFBFBD>Ա<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @return {app_type*} Ӧ<EFBFBD><EFBFBD><EFBFBD>Զ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>͵Ķ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ
*/
#define ACL_RING_LAST_APPL(head, app_type, ring_member) \
(acl_ring_pred(head) != (head) ? \
ACL_RING_TO_APPL(acl_ring_pred(head), app_type, ring_member) : 0)
#define acl_ring_last_appl ACL_RING_LAST_APPL
/**
* <EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>β<EFBFBD><EFBFBD>
* @param ring {ACL_RING*} <EFBFBD><EFBFBD><EFBFBD>ݻ<EFBFBD>
* @param entry {ACL_RING*} <EFBFBD>µ<EFBFBD>Ԫ<EFBFBD><EFBFBD>
*/
#define ACL_RING_APPEND(ring_in, entry_in) do { \
ACL_RING *ring_ptr = (ring_in), *entry_ptr = (entry_in); \
entry_ptr->succ = ring_ptr->succ; \
entry_ptr->pred = ring_ptr; \
entry_ptr->parent = ring_ptr->parent; \
ring_ptr->succ->pred = entry_ptr; \
ring_ptr->succ = entry_ptr; \
ring_ptr->parent->len++; \
} while (0)
/**
* <EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͷ<EFBFBD><EFBFBD>
* @param ring {ACL_RING*} <EFBFBD><EFBFBD><EFBFBD>ݻ<EFBFBD>
* @param entry {ACL_RING*} <EFBFBD>µ<EFBFBD>Ԫ<EFBFBD><EFBFBD>
*/
#define ACL_RING_PREPEND(ring_in, entry_in) do { \
ACL_RING *ring_ptr = (ring_in), *entry_ptr = (entry_in); \
entry_ptr->pred = ring_ptr->pred; \
entry_ptr->succ = ring_ptr; \
entry_ptr->parent = ring_ptr->parent; \
ring_ptr->pred->succ = entry_ptr; \
ring_ptr->pred = entry_ptr; \
ring_ptr->parent->len++; \
} while (0)
/**
* <EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD>ش<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݻ<EFBFBD><EFBFBD><EFBFBD>ɾ<EFBFBD><EFBFBD>
* @param entry {ACL_RING*} <EFBFBD><EFBFBD>Ԫ<EFBFBD><EFBFBD>
*/
#define ACL_RING_DETACH(entry_in) do { \
ACL_RING *succ, *pred, *entry_ptr = (entry_in); \
succ = entry_ptr->succ; \
pred = entry_ptr->pred; \
if (succ != NULL && pred != NULL) { \
pred->succ = succ; \
succ->pred = pred; \
entry_ptr->parent->len--; \
entry_ptr->succ = entry_ptr->pred = NULL; \
} \
} while (0)
#ifdef __cplusplus
}
#endif
#endif