newlisp and union

Q&A's, tips, howto's

newlisp and union

Postby dexter » Fri Sep 05, 2014 3:59 am

I am working on newlisp and epoll

but epoll has this union struct

Code: Select all
typedef union epoll_data
{
  void        *ptr;
  int          fd;
  __uint32_t   u32;
  __uint64_t   u64;
} epoll_data_t;

struct epoll_event
{
  __uint32_t   events; /* Epoll events */
  epoll_data_t data;   /* User data variable */
};


How can I translate this union to newlisp's code?
dexter
 
Posts: 74
Joined: Fri Nov 11, 2011 12:55 am

Re: newlisp and union

Postby ryuo » Fri Sep 05, 2014 2:10 pm

There is no direct support for unions, so you will have to emulate them in a sense. Unions in C all share the same chunk of storage, so the size of the union is therefore equal to the size of the largest member. Therefore, you would be best off mapping it to a single type that encompasses all of these, and perhaps changing how you interpret the results if their types differ too much.
Code: Select all
typedef union epoll_data
{
  void        *ptr;
  int          fd;
  __uint32_t   u32;
  __uint64_t   u64;
} epoll_data_t;

For this, the largest member is probably the u64 member. So you would want to use a 64 bit signed integer type. newLISP has limited supported for unsigned 64 bit. Also note that pointer types are essentially integral types, so they can be stored in integers if the integer is big enough.
Code: Select all
struct epoll_event
{
  __uint32_t   events; /* Epoll events */
  epoll_data_t data;   /* User data variable */
};

As for this, the first member maps to "unsigned int" and the second member maps to "long long". I'm not sure if the smaller members will be interpreted correctly for the union, as I do not do this frequently. But, it should be a good place to start from.
ryuo
 
Posts: 43
Joined: Wed May 21, 2014 4:40 pm

Re: newlisp and union

Postby dexter » Fri Sep 05, 2014 2:54 pm

thanks man
Finally I decide to write a share library,.so to wrapper epoll to newlisp

I think newlisp can have epoll support with the net-* functions in the feature.
dexter
 
Posts: 74
Joined: Fri Nov 11, 2011 12:55 am


Return to newLISP in the real world

Who is online

Users browsing this forum: No registered users and 2 guests