aboutsummaryrefslogtreecommitdiffstats
path: root/libccnx-common/ccnx/common/codec/ccnxCodec_EncodingBuffer.h
blob: 055637621ac5a13aa5e94f0379fab492210788f4 (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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/*
 * Copyright (c) 2017 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * @file ccnxCodec_EncodingBuffer
 * @brief An encoding buffer is a zero-copy vectored I/O for PARCBuffers
 *
 * An Encoding Buffer is an ordered list of PARCBuffers that can be written by functions like
 * writev().  You can append and append to the list and the buffers are stored only by reference.
 *
 * You can also append one encoding buffer to another.  In this case, the buffers are moved from
 * the previous list to the end of the new list.
 *
 * @code
 * {
 *    PARCBuffer *name = parcBuffer_Wrap("marc", 4, 0, 4);
 *    PARCBuffer *space= parcBuffer_Wrap(" ", 1, 0 ,1);
 *    PARCBuffer *email= parcBuffer_Wrap("<marc@example.com>", 18, 0, 18);
 *
 *    CCNxCodecEncodingBuffer *encodingBuffer = ccnxCodecEncodingBuffer_Create();
 *    ccnxCodecEncodingBuffer_BufferInsertTail(encodingBuffer, name);
 *    ccnxCodecEncodingBuffer_BufferInsertTail(encodingBuffer, space);
 *    parcBuffer_Release(&space);
 *    parcBuffer_Release(&name);
 *
 *    CCNxCodecEncodingBuffer *emailBuffer = ccnxCodecEncodingBuffer_Create();
 *    ccnxCodecEncodingBuffer_BufferInsertTail(emailBuffer, email);
 *    parcBuffer_Release(&email);
 *
 *    ccnxCodecEncodingBuffer_MoveToTail(encodingBuffer, emailBuffer);
 *    ccnxCodecEncodingBuffer_Release(&emailBuffer);
 *
 *    CCNxCodecEncodingBufferIOVec *iov = ccnxCodecEncodingBuffer_CreateIOVec(encodingBuffer);
 *    writev(STDOUT_FILENO, iov->iov, iov->iovcnt);
 *    ccnxCodecEncodingBufferIOVec_Release(&iov);
 *
 *    ccnxCodecEncodingBuffer_Release(&encodingBuffer);
 * }
 * @endcode
 *
 */

#ifndef libccnx_ccnxCodec_EncodingBuffer_h
#define libccnx_ccnxCodec_EncodingBuffer_h

#include <sys/uio.h>
#include <parc/algol/parc_Buffer.h>

struct ccnx_codec_encoding_buffer;
typedef struct ccnx_codec_encoding_buffer CCNxCodecEncodingBuffer;

/**
 * @typedef CCNxCodecEncodingBufferIOVec
 * @abstract Used for writev() or similar functions
 * @constant encodingBuffer A reference counted copy of the encoding Buffer
 * @constant iov An allocated array of iovec entries
 * @constant iovcnt The number of array entries
 * @discussion <#Discussion#>
 */
typedef struct ccnx_tlv_encoding_buffer_iovec {
    CCNxCodecEncodingBuffer *encodingBuffer;
    int iovcnt;
    struct iovec iov[];
} CCNxCodecEncodingBufferIOVec;

// ======================================================================================

/**
 * Creates an empty encoding buffer
 *
 * <#Paragraphs Of Explanation#>
 *
 * @return non-null An allocated encoding buffer
 * @return null An error
 *
 * Example:
 * @code
 * <#example#>
 * @endcode
 */
CCNxCodecEncodingBuffer *ccnxCodecEncodingBuffer_Create(void);

/**
 * Returns a reference counted copy
 *
 * Caller must call ccnxCodecEncodingBuffer_Release() on all copies.
 *
 * @param [<#in out in,out#>] <#name#> <#description#>
 *
 * @return non-null A reference counted copy
 * @return null An error
 *
 * Example:
 * @code
 * <#example#>
 * @endcode
 */
CCNxCodecEncodingBuffer *ccnxCodecEncodingBuffer_Acquire(const CCNxCodecEncodingBuffer *encodingBuffer);


/**
 * Release the encoding buffer and all internal references
 *
 * will release the list and release our reference to all enclosed PARCBuffers
 *
 * @param [<#in out in,out#>] <#name#> <#description#>
 *
 * Example:
 * @code
 * <#example#>
 * @endcode
 */
void ccnxCodecEncodingBuffer_Release(CCNxCodecEncodingBuffer **listBufferPtr);

/**
 * Displays the structure of the encoding buffer to STDOUT
 *
 * <#Paragraphs Of Explanation#>
 *
 * @param [<#in out in,out#>] <#name#> <#description#>
 *
 * @return <#value#> <#explanation#>
 *
 * Example:
 * @code
 * <#example#>
 * @endcode
 */
void ccnxCodecEncodingBuffer_Display(const CCNxCodecEncodingBuffer *encodingBuffer, int indentation);

/**
 * Appends a PARCBuffer to the encoding buffer
 *
 * Appends to the encoding buffer a reference count to the given buffer.
 * The return value is the storage node used in the internal data structure.
 *
 * The buffer will be used from its position at the time of use (i.e. when
 * ccnxCodecEncodingBuffer_CreateIOVec() is called).  It is important that no other
 * use of the PARCBuffer move the Position.
 *
 * @param [in] encodingBuffer The buffer to append to
 * @param [in] bufferToInsert The PARCBuffer to insert at the tail of the encoding buffer.
 *
 * @return number The position in the encoding buffer list
 *
 * Example:
 * @code
 * <#example#>
 * @endcode
 */
size_t ccnxCodecEncodingBuffer_AppendBuffer(CCNxCodecEncodingBuffer *encodingBuffer, PARCBuffer *bufferToInsert);

/**
 * Prepends a PARCBuffer to the encoding buffer
 *
 * Prepends to the encoding buffer a reference count to the given buffer.
 * The return value is the storage node used in the internal data structure.
 *
 * The buffer will be used from its position at the time of use (i.e. when
 * ccnxCodecEncodingBuffer_CreateIOVec() is called).  It is important that no other
 * use of the PARCBuffer move the Position.
 *
 * @param [in] encodingBuffer The buffer to prepend to
 * @param [in] bufferToInsert The PARCBuffer to insert at the head of the encoding buffer.
 *
 * @return number The position in the encoding buffer list
 *
 * Example:
 * @code
 * <#example#>
 * @endcode
 */
size_t ccnxCodecEncodingBuffer_PrependBuffer(CCNxCodecEncodingBuffer *encodingBuffer, PARCBuffer *bufferToPrepend);

/**
 * Puts the value in scratch memory
 *
 * <#Paragraphs Of Explanation#>
 *
 * @param [<#in out in,out#>] <#name#> <#description#>
 *
 * @return <#value#> <#explanation#>
 *
 * Example:
 * @code
 * <#example#>
 * @endcode
 */
void ccnxTlvEncodingbuffer_AppendUint16(CCNxCodecEncodingBuffer *encodingBuffer, uint16_t value);

/**
 * The number of elements in the list
 *
 * <#Paragraphs Of Explanation#>
 *
 * @param [<#in out in,out#>] <#name#> <#description#>
 *
 * @return <#value#> <#explanation#>
 *
 * Example:
 * @code
 * <#example#>
 * @endcode
 */
size_t ccnxCodecEncodingBuffer_Size(const CCNxCodecEncodingBuffer *encodingBuffer);

/**
 * The total number of bytes in the list
 *
 * This is calculated as the sum of all PARCBuffer Remaining lengths in
 * the encoding buffer.
 *
 * @param [<#in out in,out#>] <#name#> <#description#>
 *
 * @return <#value#> <#explanation#>
 *
 * Example:
 * @code
 * <#example#>
 * @endcode
 */
size_t ccnxCodecEncodingBuffer_Length(const CCNxCodecEncodingBuffer *encodingBuffer);

// ======================================================================================

/**
 * Constructs an iovec array based on the buffers in the list
 *
 * The elements of the iovec array will be in the list order.
 * Each iovec entry will point to the backing array of each PARCBuffer
 * based on that buffers current position.
 *
 * This object contains a reference counted copy to the encoding buffer, so
 * the caller can release the encoding buffer and hold on to only this object
 * until the writev (or similar function) is done.
 *
 * @param [<#in out in,out#>] <#name#> <#description#>
 *
 * @return non-null The allocated IOVec structure
 * @return null An error
 *
 * Example:
 * @code
 * <#example#>
 * @endcode
 */
CCNxCodecEncodingBufferIOVec *ccnxCodecEncodingBuffer_CreateIOVec(CCNxCodecEncodingBuffer *encodingBuffer);

/**
 * Constructs an iovec array based on the buffers in the list that cooresponds to offset and length
 *
 * The elements of the iovec array will be in the list order.
 * Each iovec entry will point to the backing array of each PARCBuffer
 * based on that buffers current position.
 *
 * This object contains a reference counted copy to the encoding buffer, so
 * the caller can release the encoding buffer and hold on to only this object
 * until the writev (or similar function) is done.
 *
 * @param [<#in out in,out#>] <#name#> <#description#>
 *
 * @return non-null The allocated IOVec structure
 * @return null An error, or the specified offset/length is not contained in the extent
 *
 * Example:
 * @code
 * <#example#>
 * @endcode
 */
CCNxCodecEncodingBuffer *ccnxCodecEncodingBuffer_Slice(CCNxCodecEncodingBuffer *encodingBuffer, size_t offset, size_t length);

/**
 * Release the iovec object.
 *
 * This will release the IOVec object and release its reference to the encoding
 * buffer.  If this was the last reference to the encoding buffer, all references to
 * the underlying PARCBuffers will be released too.
 *
 * @param [<#in out in,out#>] <#name#> <#description#>
 *
 * Example:
 * @code
 * <#example#>
 * @endcode
 */
void ccnxCodecEncodingBufferIOVec_Release(CCNxCodecEncodingBufferIOVec **iovecPtr);

#endif // libccnx_ccnxCodec_EncodingBuffer_h