c - libevent API: understanding the pointer parameters or return values -
for following libevent api:
void event_set(struct event *ev, int fd, short event, void (*cb)(int, short, void *), void *arg) event_add(struct event *ev, const struct timeval *timeout); struct event* event_new (struct event_base *, evutil_socket_t, short, event_callback_fn, void) i want know:
1) pointer parameter ev in second function event_add, function event_add makes local copy of ev structure or not?
for example, if like:
code snippet 1: struct event ev; event_set(&ev, ..para list 1...); // event 1 event_add(&ev, ...); event_set(&ev, ..para list 2...); // event 2 event_add(&ev, ...); event 1 different event 2 because parameter list 1 different parameter list 2. if event_add makes local copy, no problem, if event_add doesn't make local copy, these 2 event_add add event 2?
besides, if have main function:
void func(){ struct event ev; event_set(&ev, ...); event_add(&ev, ...) } int main(){ func(); event_base_dispatch(base); } after func() called, execution returns main(). since ev local variable inside func(). if event_add(&ev,...) doesn't make local copy, ev find , there problem. can call event_add() on local event structure?
i want add many timer events(use evtimer_set) time time, , adding happens in callback functions. can't define global variabbles timeout events in advance, if event_add() can't called on local variables, there solutions this?
2) event_new returns structure pointer, want know structure, in stack/heap memory or static memory?
my special case::
in main.c int main(){ struct event_base *base; struct event pcap_ev; ..... // here file descriptor pcapfd event_set(&pcap_ev, pcapfd, ev_read|ev_persist, on_capture, pcap_handle); event_base_set(base, &pcap_ev); event_add(&pcap_ev, null); ..... event_base_dispatch(base); } on_capture callback function: void *on_capture(int pcapfd, short op, void *arg) { pcap_t *handle; handle = (pcap_t *)arg; fqueue_t* pkt_queue; pkt_queue = init_fqueue(); pcap_dispatch(handle, -1, collect_pkt, pkt_queue); // function put cached packets pkt_queue process_pcap(pkt_queue); } sub-routine process_pcap(): void process_pcap(pkt_queue);{ (pkt in pkt_queue){ // here pseudo code insert(table, pkt); // here insert pkt table struct event pkt_ev; evtimer_set(&pkt_ev, timer_cb, null); // want call timer_cb after timeout event_base_set(base, &pkt_ev); event_add(&pkt_ev, timeout); } } callback function timer_cb(): timer_cb(...){ if(...) delete(table, pkt); ....... } i'm afraid timer_cb() won't called because pkt_ev local variable.
you must use different struct event instance each event want know about. can call event_add() on local struct event variable if variable has lifetime spans across calls event loop api until removed event_del().
the allocation functions default heap, can substitute own allocation routines in place event_set_mem_functions().
Comments
Post a Comment