newlisp and union

Started by dexter, September 04, 2014, 08:59:00 PM

Previous topic - Next topic

dexter

I am working on newlisp and epoll



but epoll has this union struct



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

#1
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.

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.

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

#2
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.