下图是给一个list(实际也是个event)两次添加一个event后的图示
下面是整理后的源代码,可以在gcc或者VC中编译debug
typedef void (*ril_event_cb)(int fd, short events, void *userdata);
struct ril_event {
struct ril_event *next;
struct ril_event *prev;
int fd;
int index;
bool persist;
ril_event_cb func;
void *param;
};
static void init_list(struct ril_event * list)
{
memset(list, 0, sizeof(struct ril_event));
list->next = list;
list->prev = list;
list->fd = -1;
}
static void addToList(struct ril_event * ev, struct ril_event * list)
{
ev->next = list;
ev->prev = list->prev;
ev->prev->next = ev;
list->prev = ev;
}
static void removeFromList(struct ril_event * ev)
{
ev->next->prev = ev->prev;
ev->prev->next = ev->next;
ev->next = NULL;
ev->prev = NULL;
}
static struct ril_event pending_list;
void test_list()
{
struct ril_event tev1;
struct ril_event tev2;
tev1.fd = 1;
tev2.fd = 2;
init_list(&pending_list);
addToList(&tev1, &pending_list);
addToList(&tev2, &pending_list);
removeFromList(&tev2);
addToList(&tev2, &pending_list);
removeFromList(&tev2);
addToList(&tev2, &pending_list);
}