Question

for the 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 to know:

1) for pointer parameter ev in the second function event_add, the function event_add makes a local copy of the ev structure or not?

for example, if I do something 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 is different from event 2 because parameter list 1 is different from parameter list 2. if event_add makes a local copy, then it is no problem, but if event_add doesn't make a local copy, then these two event_add actually add only event 2?

besides, if I have a main function:

   void func(){
     struct event ev;
     event_set(&ev, ...);
     event_add(&ev, ...)
   }

   int main(){
      func();
      event_base_dispatch(base);
   }

after func() is called, the execution returns to main(). since ev is a local variable inside func(). if event_add(&ev,...) doesn't make a local copy, then ev is nowhere to find and there will be a problem. so can I call event_add() on a local event structure?

I want to add many timer events(use something like evtimer_set) from time to time, and the adding happens in some callback functions. so I can't define global variabbles for the timeout events in advance, if event_add() can't be called on local variables, are there any solutions for this?

2) event_new returns a structure pointer, I want to know where is the structure, it is in a stack/heap memory or static memory?

MY special case::

        in the main.c

               int main(){
                    struct event_base *base;
                    struct event pcap_ev;
                    .....                     // here I get a 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);  // this function put all the cached packets into pkt_queue
                 process_pcap(pkt_queue);
                }

          the sub-routine process_pcap():

                void process_pcap(pkt_queue);{
                      for (pkt in pkt_queue){      // here is pseudo code
                           insert(table, pkt);     // here insert the pkt into a certain table 
                           struct event pkt_ev;
                           evtimer_set(&pkt_ev, timer_cb, NULL);  // I want to call timer_cb after timeout
                           event_base_set(base, &pkt_ev);
                           event_add(&pkt_ev, timeout);                     
                      }
                }

         the callback function timer_cb():

                timer_cb(...){
                     if(...)  delete(table, pkt);
                     .......
                }

I'm just afraid timer_cb() won't be called because pkt_ev is a local variable.

Was it helpful?

Solution

You must use a different struct event instance for each event you want to know about. You can only call event_add() on a local struct event variable if that variable has a lifetime that spans across all calls to the event loop API up until it is removed with event_del().

The allocation functions default to the heap, but you can substitute your own allocation routines in its place with event_set_mem_functions().

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top