aboutsummaryrefslogtreecommitdiffstats
path: root/man/rb_new.3
blob: f69c3664f9b8a104633602d970e3620d192cf77c (plain)
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
.TH "rb_new" "3" " 9 February 2018 (v1.0.0)" "bofc.pl"
.SH NAME
.PP
.B rb_new
- create new ring buffer
.SH SYNOPSIS
.PP
.BI "#include <librb.h>"
.PP
.BI "struct rb *rb_new(size_t " count ", size_t " object_size ", \
unsigned long " flags ");"
.SH DESCRIPTION
.PP
.BR rb_new (3)
creates new ring buffer object with maximum
.I count
elements, each of
.I object_size
bytes with
.IR flags.
Handle to ring buffer is returned on successfull initalization.
Loosing this pointer will lead to memory leak.
.PP
.I count
and
.I object_size
describes buffer sizes.
Library will allocate on callers heap
.RI ( count
*
.IR object_size )
bytes.
.Icount
must be a power of two number (ie. 4, 64, 1024), as library tries to avoid
unecessary conditional jump to improve performance.
.I object_size
determines how big a single element is.
This value is constant for lifetime of a ring buffer.
.I object_size
might be for example
.B sizeof(uint8_t)
or
.B sizeof(uint32_t)
or even
.BR "sizeof(struct my_msg)" .
In short,
.I object_size
should be a size of an object, ring buffer will hold.
You should not write nor read objects of different size than created one, as
this will lead to undefined behaviour or even crashes.
.PP
Function can accept following
.I flags
.TP
.B O_NONBLOCK
if set, all calls to library functions will return immediately even if buffer
is full or empty.
Proper functions will then return with errno set to
.B EAGAIN
and number of elements actually commited.
This is default behaviour when
.B O_MULTITHREAD
flag was
.B not
passed and
.B librb
is working in single thread mode.
.TP
.B O_MULTITHREAD
this can be set only when library has been compiled with
.BR ENABLE_THREADS ,
otherwise error will be returned. If set,
.B librb
functions will be using pthreads to provide synchronization of threads accessing
.I rb
object.
.SH RETURN VALUES
.PP
If no errors have been detected, function will return pointer to newly created
ring buffer, otherwise NULL is returned.
.SH ERRORS
.TP
.B EINVAL
.I count
is not a power of two number (like 4, 64, 1024).
.TP
.B ENOSYS
.B O_MULTITHREAD
was set but
.B librb
was not compiled with pthread support.
.TP
.B ENOMEM
couldn't allocate memory for given
.I count
and
.I object_size
.SH SEE ALSO
.PP
.BR rb_overview (7),
.BR rb_destroy (3),
.BR rb_discard (3),
.BR rb_stop (3),
.BR rb_read (3),
.BR rb_recv (3),
.BR rb_write (3),
.BR rb_send (3),
.BR rb_clear (3),
.BR rb_count (3),
.BR rb_space (3),
.BR rb_version (3)