logo
Free, unlimited AI code reviews that run on commit
git-lrc git-lrc GitHub Install Now We'd appreciate a star git-lrc - Free, unlimited AI code reviews that run on commit | Product Hunt git-lrc - Free, unlimited AI code reviews that run on commit | Product Hunt

fduserdata_create, fduserdata_destroy, fduserdata_destroy_cb, fduserdata_new, fduserdata_get,

Author

       VirtualSquare. Project leader: Renzo Davoli.

VirtualSquare                                     November 2019                                    FDUSERDATA(3)

Description

       This library permits one to associate file descriptors with user defined data, more precisely it  manages
       a data structure whose searching key is a file descriptor.

       fduserdata_create  and  fduserdata_destroy  are  the  constructor  and  destructor of the data structure,
       respectively.  The  data  structure  has  been  implemented  as  a  hash  table,  the  argument  size  of
       fduserdata_create  is  the  size of the hash array. When size is zero the hash array has its default size
       (64).

       fduserdata_destroy_cb is an alternative destructor which calls the function  callback  for  each  element
       still in the data structure.

       fduserdata_new creates a new element. It is a macro: type is the type of the user data.

       fduserdata_get search the user data associated to the fd.

       Both fduserdata_new and fduserdata_get lock the access to the element, so that fduserdata is thread safe.
       fduserdata_put unlocks the element and makes it available for further requests.

       fduserdata_del can be used instead of fduserdata_put to delete the element.

Example

       fduserdata uses a trivial hash table, the optional arg is the
       size of the hash table: default value = 64

           FDUSERDATA table = fduserdata_create(0);

           struct mydata {
           // fd data fields ...
           };

       create a struct mydata for the file descriptor fd.

           struct mydata *data = fduserdata_new(table, fd, struct mydata);

       .... set user defined data (data->fields)

           fduserdata_put(data);

       search for data
       there is mutual exclusion between new/put, get/put (or new/del, get/del)
       so do not insert time consuming or blocking ops.

           struct mydata *fddata = fduserdata_get(table, fd);
           if (fddata) {

       ... read/update user defined data (data->fields)
       (use fduserdata_del instead of fduserdata_put to delete the element)

                 fduserdata_put(data);
           }

       at the end... when table is no longer required

           fduserdata_destroy(table);

Name

       fduserdata_create,    fduserdata_destroy,    fduserdata_destroy_cb,    fduserdata_new,    fduserdata_get,
       fduserdata_put, fduserdata_del - associate file descriptors with user defined data

Return Value

fduserdata_create returns the descriptor of the data structure (NULL in case of error).

       fduserdata_new returns the element of type type just created (NULL in case of error).

       fduserdata_get returns the element or NULL if no data corresponds to the file descriptor fd.

       fduserdata_del On success, zero is returned. On error, -1 is returned.

       On error, errno is set appropriately.

Synopsis

#include<fduserdata.h>FDUSERDATA*fduserdata_create(intsize);voidfduserdata_destroy(FDUSERDATA*fdtable);typedefvoid(*fduserdata_destr_cb_t)(intfd,void*data,void*arg);voidfduserdata_destroy_cb(FDUSERDATA*fdtable,fduserdata_destr_cb_tcallback,void*arg);void*fduserdata_new(FDUSERDATA*fdtable,intfd,type);void*fduserdata_get(FDUSERDATA*fdtable,intfd);voidfduserdata_put(void*data);intfduserdata_del(void*data);

See Also