Libevent Source Analysis: Event_assign, Event_new

Source: Internet
Author: User

In Libevent, there are two ways to get an event type object, 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 used5 In the future calls to Event_add () and Event_del (). Unlike Event_new (), it6 doesn ' t allocate memory itself:it requires that's already7 allocated a struct event, probably on the heap. Doing This would8 typically make your code depend on the size of the event structure, and9 thereby create incompatibility with the future versions of Libevent.Ten  One The easiest-avoid this problem is just-to-use Event_new () and A Event_free () instead. -  - A slightly harder-future-proof your code is-to-use the event_get_struct_event_size () to determine the required size of an event - At runtime. -  - Note that it isn't safe to call the This function on a event is + active or pending. Doing so would corrupt internal data structures in - Libevent, and leads to strange, hard-to-diagnose bugs. _can_ Use + Event_assign to a existing event, but only if it 's not active A or pending! at  - The arguments for this function, and the behavior of the events, it - makes, is as for Event_new (). -  - @param ev An event struct -to-be modified - @param base the event base to which EV should is attached. in @param fd The file descriptor to be monitored - @param events desired events to monitor; can ev_read and/or Ev_write to @param callback callback function to being invoked when the event occurs + @param callback_arg An argument to being passed to the callback function -  the @return 0 If success, or-1 on invalid arguments. *  $ @see event_new (), Event_add (), Event_del (), Event_base_once (),Panax Notoginseng event_get_struct_event_size () -   */ the Event2_export_symbol + intEvent_assign (struct Event*,structEvent_base *, evutil_socket_t, Short, EVENT_CALLBACK_FN,void*);

Realize:

In fact, the function of event_assign is to assign each member of the given event type object a specified value.

1 int2Event_assign (struct Event*ev,structEvent_base *Base, evutil_socket_t FD, ShortEventsvoid(*callback) (evutil_socket_t, Short,void*),void*Arg)3 {4     if(!Base)5         Base=current_base;6     if(arg = = &event_self_cbarg_ptr_)7arg =ev;8 9 event_debug_assert_not_added_ (EV);Ten  OneEv->ev_base =Base; A  -Ev->ev_callback =callback; -Ev->ev_arg =Arg; theEV->EV_FD =FD; -Ev->ev_events =events; -Ev->ev_res =0; -Ev->ev_flags =Evlist_init; +Ev->ev_ncalls =0; -Ev->ev_pncalls =NULL; +  A     if(Events &ev_signal) { at         if(Events & (ev_read| ev_write| ev_closed))! =0) { -EVENT_WARNX ("%s:ev_signal is isn't compatible with" -                 "Ev_read, ev_write or ev_closed", __func__); -             return-1; -         } -Ev->ev_closure =ev_closure_event_signal; in}Else { -         if(Events &ev_persist) { toEvutil_timerclear (&ev->ev_io_timeout); +Ev->ev_closure =ev_closure_event_persist; -}Else { theEv->ev_closure =ev_closure_event; *         } $     }Panax Notoginseng  - min_heap_elem_init_ (EV); the  +     if(Base!=NULL) { A         /*By default, we put new events into the middle priority*/ theEv->ev_pri =Base->nactivequeues/2; +     } -  $ event_debug_note_setup_ (EV); $  -     return 0; -}
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 is used in5 calls to Event_add () and Event_del (). The FD and Events6 arguments determine which conditions would trigger the event;7 callback and Callback_arg arguments tell Libevent the8 event becomes active.9 Ten If Events contains one of Ev_read, Ev_write, or ev_read| Ev_write, then. One FD is a file descriptor or socket this should get monitored for A readiness to read, readiness to write, or readiness for either Operation - (respectively). If events contains ev_signal, then FD is a SIGNAL - Number to wait for. If events contains none of those flags, then the the event can be triggered-a timeout or by manual activation with - event_active (): In the case, FD must be-1. -  - The ev_persist flag can also is passed in the events Argument:it makes + Event_add () persistent until Event_del () is called. -  + the EV_ET flag is compatible with Ev_read and Ev_write, and supported A Only by certain backends. It tells Libevent to use edge-triggered at events. -  - The ev_timeout flag has a no effect here. -  - It is okay to has multiple events all listening on the same FDS; - they must either all is edge-triggered, or all is not edge triggerd. in  - When the event becomes active, the event loop would run the provided to Callbuck function, with three arguments. the first would be the provided + FD value. The second is a bitfield of the events that triggered: - Ev_read, Ev_write, or ev_signal. Here the Ev_timeout flag indicates the that a-timeout occurred, and Ev_et indicates that a edge-triggered * event occurred. The third event would be is the Callback_arg pointer that $ You provide.Panax Notoginseng  - @param base the event base to which the event should is attached. the @param fd The file descriptor or signal to be monitored, or-1. + @param events desired events to Monitor:bitfield of Ev_read, Ev_write, A ev_signal, Ev_persist, Ev_et. the @param callback callback function to being invoked when the event occurs + @param callback_arg An argument to being passed to the callback function -  $ @return A newly allocated struct event that must later is freed with $ Event_free (). - @see Event_free (), Event_add (), Event_del (), event_assign () -  */ the Event2_export_symbol - struct Event*event_new (structEvent_base *, evutil_socket_t, Short, EVENT_CALLBACK_FN,void*);

Realize:

The implementation of Event_new is actually an indirect invocation of event_assign, first calling Mm_malloc allocates a piece of memory, and then calls Event_assign to assign values to each member of the event type object.

1 struct Event*2Event_new (structEvent_base *Base, evutil_socket_t FD, ShortEventsvoid(*CB) (evutil_socket_t, Short,void*),void*Arg)3 {4     struct Event*ev;5EV = 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);Ten         return(NULL); One     } A  -     return(EV); -}

3. Difference

The difference between the two approaches is that event_assign allocates an object on the stack and assigns a value to the member, whereas Event_new allocates an object on the heap and assigns a value to the member.

Libevent Source Analysis: Event_assign, Event_new

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.