内核链表使用举例
#ifndef _EVENT_LIST_H_
#define _EVENT_LIST_H_#include "list.h" // 内核链表头文件typedef int (*event_cb)(void *data);typedef struct
{void *params;event_cb handle;struct list_head list;
} event_elem_t;int EventListAdd(event_elem_t *event, struct list_head *event_head);
int EventListDel(event_elem_t *event, struct list_head *event_head);
void EventListProcess(struct list_head *event_head, void *ret);#endif /* _EVENT_LIST_H_ */
#include "event_list_.h"
#include <stddef.h>struct list_head proto_event_list;LIST_HEAD(proto_event_list);int EventListAdd(event_elem_t *event, struct list_head *event_head)
{if(event == NULL || event->handle == NULL){return -2;}list_add(&event->list, event_head);return 0;}int EventListDel(event_elem_t *event, struct list_head *event_head)
{event_elem_t *pevent;struct list_head *pos;if(event == NULL || event->handle == NULL){return -2;}list_for_each(pos, event_head){const struct list_head *__mptr = pos;pevent = (event_elem_t *)((char *)__mptr - offsetof(event_elem_t, list) );if(pevent == event_elem_t){list_del(pos);return 0;}}return -3;//ERR;}void EventListProcess(struct list_head *event_head, void *ret)
{event_elem_t *pevent;struct list_head *pos;const struct list_head *__mptr;if(list_empty(event_head)){return;}list_for_each(pos, event_head){__mptr = pos;pevent = (event_elem_t *)((char *)__mptr - offsetof(event_elem_t, list) );*(int *)ret = pevent->event_handle(pevent->params);}
}