1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
|
/*
* Associate user defined data to file descriptors. (thread-safe).
*
* Copyright (C) 2019 Renzo Davoli <renzo@cs.unibo.it> VirtualSquare team.
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or (at
* your option) any later version.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the Free Software Foundation,
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Example of usage:
* //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)
* fduserdata_put(data);
* // use fduserdata_del instead of fduserdata_put to
* // delete the element
* }
*
* // at the end... when table is no longer required
* fduserdata_destroy(table);
*
*/
#ifndef FDUSERDATA_H
#define FDUSERDATA_H
#include <stddef.h>
struct fduserdata_table;
typedef struct fduserdata_table FDUSERDATA;
FDUSERDATA *fduserdata_create(int size);
void fduserdata_destroy(FDUSERDATA *fdtable);
typedef void (*fduserdata_destr_cb_t)(int fd, void *data, void *arg);
void fduserdata_destroy_cb(FDUSERDATA *fdtable, fduserdata_destr_cb_t callback, void *arg);
#define fduserdata_new(fdtable, fd, type) ((type *)(__fduserdata_new((fdtable),(fd),sizeof(type))))
void *__fduserdata_new(FDUSERDATA *fdtable, int fd, size_t count);
void *fduserdata_get(FDUSERDATA *fdtable, int fd);
void fduserdata_put(void *data);
int fduserdata_del(void *data);
#endif
|