首页 > 代码库 > libevent源码分析:event_assign、event_new
libevent源码分析:event_assign、event_new
在libevent中,获取event类型对象的方法有两种,event_assign、event_new
1、event_assign()
1 /** 2 Prepare a new, already-allocated event structure to be added. 3 4 The function event_assign() prepares the event structure ev to be used 5 in future calls to event_add() and event_del(). Unlike event_new(), it 6 doesn‘t allocate memory itself: it requires that you have already 7 allocated a struct event, probably on the heap. Doing this will 8 typically make your code depend on the size of the event structure, and 9 thereby create incompatibility with future versions of Libevent.10 11 The easiest way to avoid this problem is just to use event_new() and12 event_free() instead.13 14 A slightly harder way to future-proof your code is to use15 event_get_struct_event_size() to determine the required size of an event16 at runtime.17 18 Note that it is NOT safe to call this function on an event that is19 active or pending. Doing so WILL corrupt internal data structures in20 Libevent, and lead to strange, hard-to-diagnose bugs. You _can_ use21 event_assign to change an existing event, but only if it is not active22 or pending!23 24 The arguments for this function, and the behavior of the events that it25 makes, are as for event_new().26 27 @param ev an event struct to be modified28 @param base the event base to which ev should be attached.29 @param fd the file descriptor to be monitored30 @param events desired events to monitor; can be EV_READ and/or EV_WRITE31 @param callback callback function to be invoked when the event occurs32 @param callback_arg an argument to be passed to the callback function33 34 @return 0 if success, or -1 on invalid arguments.35 36 @see event_new(), event_add(), event_del(), event_base_once(),37 event_get_struct_event_size()38 */39 EVENT2_EXPORT_SYMBOL40 int event_assign(struct event *, struct event_base *, evutil_socket_t, short, event_callback_fn, void *);
实现:
其实event_assign的作用就是把给定的event类型对象的每一个成员赋予一个指定的值。
1 int 2 event_assign(struct event *ev, struct event_base *base, evutil_socket_t fd, short events, void (*callback)(evutil_socket_t, short, void *), void *arg) 3 { 4 if (!base) 5 base = current_base; 6 if (arg == &event_self_cbarg_ptr_) 7 arg = ev; 8 9 event_debug_assert_not_added_(ev);10 11 ev->ev_base = base;12 13 ev->ev_callback = callback;14 ev->ev_arg = arg;15 ev->ev_fd = fd;16 ev->ev_events = events;17 ev->ev_res = 0;18 ev->ev_flags = EVLIST_INIT;19 ev->ev_ncalls = 0;20 ev->ev_pncalls = NULL;21 22 if (events & EV_SIGNAL) {23 if ((events & (EV_READ|EV_WRITE|EV_CLOSED)) != 0) {24 event_warnx("%s: EV_SIGNAL is not compatible with "25 "EV_READ, EV_WRITE or EV_CLOSED", __func__);26 return -1;27 }28 ev->ev_closure = EV_CLOSURE_EVENT_SIGNAL;29 } else {30 if (events & EV_PERSIST) {31 evutil_timerclear(&ev->ev_io_timeout);32 ev->ev_closure = EV_CLOSURE_EVENT_PERSIST;33 } else {34 ev->ev_closure = EV_CLOSURE_EVENT;35 }36 }37 38 min_heap_elem_init_(ev);39 40 if (base != NULL) {41 /* by default, we put new events into the middle priority */42 ev->ev_pri = base->nactivequeues / 2;43 }44 45 event_debug_note_setup_(ev);46 47 return 0;48 }
2、event_new()
1 /** 2 Allocate and asssign a new event structure, ready to be added. 3 4 The function event_new() returns a new event that can be used in 5 future calls to event_add() and event_del(). The fd and events 6 arguments determine which conditions will trigger the event; the 7 callback and callback_arg arguments tell Libevent what to do when the 8 event becomes active. 9 10 If events contains one of EV_READ, EV_WRITE, or EV_READ|EV_WRITE, then11 fd is a file descriptor or socket that should get monitored for12 readiness to read, readiness to write, or readiness for either operation13 (respectively). If events contains EV_SIGNAL, then fd is a signal14 number to wait for. If events contains none of those flags, then the15 event can be triggered only by a timeout or by manual activation with16 event_active(): In this case, fd must be -1.17 18 The EV_PERSIST flag can also be passed in the events argument: it makes19 event_add() persistent until event_del() is called.20 21 The EV_ET flag is compatible with EV_READ and EV_WRITE, and supported22 only by certain backends. It tells Libevent to use edge-triggered23 events.24 25 The EV_TIMEOUT flag has no effect here.26 27 It is okay to have multiple events all listening on the same fds; but28 they must either all be edge-triggered, or all not be edge triggerd.29 30 When the event becomes active, the event loop will run the provided31 callbuck function, with three arguments. The first will be the provided32 fd value. The second will be a bitfield of the events that triggered:33 EV_READ, EV_WRITE, or EV_SIGNAL. Here the EV_TIMEOUT flag indicates34 that a timeout occurred, and EV_ET indicates that an edge-triggered35 event occurred. The third event will be the callback_arg pointer that36 you provide.37 38 @param base the event base to which the event should be attached.39 @param fd the file descriptor or signal to be monitored, or -1.40 @param events desired events to monitor: bitfield of EV_READ, EV_WRITE,41 EV_SIGNAL, EV_PERSIST, EV_ET.42 @param callback callback function to be invoked when the event occurs43 @param callback_arg an argument to be passed to the callback function44 45 @return a newly allocated struct event that must later be freed with46 event_free().47 @see event_free(), event_add(), event_del(), event_assign()48 */49 EVENT2_EXPORT_SYMBOL50 struct event *event_new(struct event_base *, evutil_socket_t, short, event_callback_fn, void *);
实现:
event_new的实现其实是间接的调用的event_assign,首先调用mm_malloc分配一块内存,然后调用event_assign来给event类型的对象各个成员赋值。
1 struct event * 2 event_new(struct event_base *base, evutil_socket_t fd, short events, void (*cb)(evutil_socket_t, short, void *), void *arg) 3 { 4 struct event *ev; 5 ev = mm_malloc(sizeof(struct event)); 6 if (ev == NULL) 7 return (NULL); 8 if (event_assign(ev, base, fd, events, cb, arg) < 0) { 9 mm_free(ev);10 return (NULL);11 }12 13 return (ev);14 }
3、区别
这两种方式的区别就是,event_assign是在栈上分配一个对象,然后给成员赋值;而event_new是在堆上分配一个对象,然后给成员赋值。
libevent源码分析:event_assign、event_new
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。