File: README.md

package info (click to toggle)
libfduserdata 0.1.2-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 144 kB
  • sloc: ansic: 208; makefile: 17
file content (59 lines) | stat: -rw-r--r-- 1,932 bytes parent folder | download | duplicates (2)
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
# libfduserdata

associate file descriptors with user defined data

This small library permits to associate user defined data to file descriptors. This library is thread safe.

The library uses a hash table.

## Initialization/Termination

The function `fduserdata_create` sets up the data structure.
e.g.

    FDUSERDATA table = fduserdata_create(0);

The optional parameter (here 0) is the size of the hashtable. The default size if 64.

When the data structure is no longer needed it can be deleted/freed using:

    fduserdata_destroy(table);

## create a new element:
Allocate a `struct mydata` element connected to the file descriptor `fd` in `table`:

    struct mydata *data = fduserdata_new(table, fd, struct mydata);
    // mydata.xxxx = yyyy
    // intialize all mydata  fields
    fduserdata_put(data)

Please note that `fduserdata_put` is required to close the critical section. No other threads can add, read or modify elements in `table` until the current element has been completely initialized and `fduserdata_put` has been called.

## search an element/modify

    struct mydata *fddata = fduserdata_get(table, fd);
    if (fddata) {
        //... read/update user defined data (data->fields)
        fduserdata_put(data);
    }

Again `fduserdata_put` closes the critical section. Slow or blocking operations should not take place between `fduserdata_get` and `fduserdata_put`

## delete an element
    struct mydata *fddata = fduserdata_get(table, fd);
    if (fddata)
        fduserdata_del(data);

`fduserdata_get` (or `fduserdata_new`) is needed before `fduserdata_del` because `fduserdata_del` deletes the element while it closes the critical section.
(in this way it is not possible to delete an element twice).

# compile and install the library:

This is a sequence of commands that can be used to compile install the library.

    mkdir build
    cd build
    cmake ..
    make
    sudo make install