newlisp and union

Q&A's, tips, howto's
Locked
dexter
Posts: 74
Joined: Fri Nov 11, 2011 12:55 am

newlisp and union

Post by dexter »

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?

ryuo
Posts: 43
Joined: Wed May 21, 2014 4:40 pm

Re: newlisp and union

Post by ryuo »

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.

dexter
Posts: 74
Joined: Fri Nov 11, 2011 12:55 am

Re: newlisp and union

Post by dexter »

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.

Locked