FDUSERDATA(3)
π FDUSERDATA π
okay bestie, welcome to the Library Functions Manual entry for the fduserdata crew β this is the fduserdata(3) page and it's genuinely so useful, like a little friendship bracelet between your file descriptors and literally any data you want π
β¨ NAME β¨
fduserdata_create, fduserdata_destroy, fduserdata_destroy_cb, fduserdata_new, fduserdata_get, fduserdata_put, fduserdata_del β associate file descriptors with user defined data πΈ
β¨ SYNOPSIS β¨
#include <fduserdata.h>
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);
void *fduserdata_new(FDUSERDATA *fdtable, int fd, type);
void *fduserdata_get(FDUSERDATA *fdtable, int fd);
void fduserdata_put(void *data);
int fduserdata_del(void *data);
β¨ DESCRIPTION β¨
So this whole library is giving matchmaker π β it lets you pair up file descriptors with user defined data. Like, more specifically, it runs a data structure where the searching key is a file descriptor. that's it. that's the vibe.
fduserdata_create and fduserdata_destroy are the constructor and destructor of the data structure, respectively. The data structure is implemented as a hash table, and the size argument of fduserdata_create is the size of the hash array. When size is zero, the hash array just uses its default size, which is 64. slay, defaults π«Ά
fduserdata_destroy_cb is an alternative destructor, and what it does is call the function callback for each element still hanging out in the data structure. bye besties, one at a time β¨
fduserdata_new creates a new element. Important: it's a macro, and type is the type of the user data. no cap.
fduserdata_get searches for 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. it's giving mutual exclusion, it's giving respect.
fduserdata_del can be used instead of fduserdata_put to delete the element. two paths, one destiny π
β¨ RETURN VALUE β¨
fduserdata_createβ returns the descriptor of the data structure, orNULLin case of error πfduserdata_newβ returns the element of typetypejust created, orNULLin case of error πfduserdata_getβ returns the element, orNULLif no data corresponds to the file descriptorfdπfduserdata_delβ On success, zero is returned. On error,-1is returned.
On error, errno is set appropriately. as it should be, periodt.
β¨ EXAMPLE β¨
fduserdata uses a trivial hash table, and the optional arg is the size of the hash table: default value = 64 π
FDUSERDATA table = fduserdata_create(0);
struct mydata {
// fd data fields ...
};
now 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 π β and listen, there is mutual exclusion between new/put, get/put (or new/del, get/del) so do not insert time consuming or blocking ops. fr fr, don't do it.
struct mydata *fddata = fduserdata_get(table, fd);
if (fddata) {
... read/update user defined data (data->fields) π β and psst, 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);
β¨ AUTHOR β¨
VirtualSquare. Project leader: Renzo Davoli. icons only π
and that's the whole page bestie, no notes π
VirtualSquare Β· November 2019 Β· FDUSERDATA(3) β ty for coming to my ted talk, go forth and hash responsibly πβ¨
π¦ where this page comes from
- debian bookworm: libfduserdata-dev 0.1.2-3 Β· LGPL-2.1-or-later
- debian trixie: libfduserdata-dev 0.1.2-3 Β· LGPL-2.1-or-later
- ubuntu jammy: libfduserdata-dev 0.1.2-2 Β· LGPL-2.1-or-later
- ubuntu noble: libfduserdata-dev 0.1.2-3 Β· LGPL-2.1-or-later
- ubuntu questing: libfduserdata-dev 0.1.2-3
- ubuntu resolute: libfduserdata-dev 0.1.2-3build1 Β· LGPL-2.1-or-later
- ubuntu stonking: libfduserdata-dev 0.1.2-3build1 Β· LGPL-2.1-or-later
- Copyright (C) 2019 VirtualSquare. Project Leader: Renzo Davoli
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms