Haka: include/haka/vbuffer.h Source File

Haka

- Main Page
- Modules
- Data Structures
- Files

- File List
- Globals

vbuffer.h
Go to the documentation of this file.
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
10 #ifndef HAKA_VBUFFER_H
11 #define HAKA_VBUFFER_H
12
13 #include <haka/types.h>
14 #include <haka/container/list2.h>
15 #include <haka/lua/object.h>
16
17
18 /*
19 * Structures
20 */
21
22 #define ALL (size_t)-1
24 typedef uint32 vbsize_t;
25 struct vbuffer_data;
26
30 typedef enum {
31 CLONE_COPY,
32 CLONE_RW,
33 CLONE_RO_ORIG,
34 CLONE_RO_CLONE,
35 } clone_mode;
36
40 struct vbuffer_data_ops {
41 void (*free)(struct vbuffer_data *data);
42 void (*addref)(struct vbuffer_data *data);
43 bool (*release)(struct vbuffer_data *data);
44 uint8 *(*get)(struct vbuffer_data *data, bool write);
45 };
46
50 struct vbuffer_data {
51 struct vbuffer_data_ops *ops;
52 };
53
54 struct vbuffer_chunk;
55
64 struct vbuffer {
65 struct lua_object lua_object;
66 struct vbuffer_chunk *chunks;
67 };
68
69 extern const struct vbuffer vbuffer_init;
77 struct vbuffer_iterator {
78 struct vbuffer_chunk *chunk;
79 vbsize_t offset;
80 vbsize_t meter;
81 bool registered:1;
82 };
83
84 extern const struct vbuffer_iterator vbuffer_iterator_init;
91 struct vbuffer_sub {
92 struct vbuffer_iterator begin;
93 bool use_size:1;
94 union {
95 struct vbuffer_iterator end;
96 vbsize_t length;
97 };
98 };
99
100 extern const struct vbuffer_sub vbuffer_sub_init;
105 struct vbuffer_sub_mmap {
106 struct vbuffer_chunk *data;
107 vbsize_t len;
108 vbsize_t meter;
109 };
110
111 extern const struct vbuffer_sub_mmap vbuffer_mmap_init;
114 /*
115 * Functions
116 */
117
121 bool vbuffer_isvalid(const struct vbuffer *buf);
122
126 bool vbuffer_isempty(const struct vbuffer *buf);
127
131 bool vbuffer_create_empty(struct vbuffer *buf);
132
136 bool vbuffer_create_new(struct vbuffer *buf, size_t size, bool zero);
137
141 bool vbuffer_create_from(struct vbuffer *buf, const char *str, size_t len);
142
146 void vbuffer_clear(struct vbuffer *buf);
147
151 void vbuffer_release(struct vbuffer *buf);
152
156 void vbuffer_position(const struct vbuffer *buf, struct vbuffer_iterator *position, size_t offset);
157
161 INLINE void vbuffer_begin(const struct vbuffer *buf, struct vbuffer_iterator *position);
162
166 INLINE void vbuffer_end(const struct vbuffer *buf, struct vbuffer_iterator *position);
167
174 void vbuffer_last(const struct vbuffer *buf, struct vbuffer_iterator *position);
175
179 void vbuffer_setwritable(struct vbuffer *buf, bool writable);
180
185 bool vbuffer_iswritable(struct vbuffer *buf);
186
190 bool vbuffer_ismodified(struct vbuffer *buf);
191
195 void vbuffer_clearmodified(struct vbuffer *buf);
196
200 bool vbuffer_append(struct vbuffer *buf, struct vbuffer *buffer);
201
206 size_t vbuffer_size(struct vbuffer *buf);
207
211 INLINE bool vbuffer_check_size(struct vbuffer *buf, size_t minsize, size_t *size);
212
216 INLINE bool vbuffer_isflat(struct vbuffer *buf);
217
221 INLINE const uint8 *vbuffer_flatten(struct vbuffer *buf, size_t *size);
222
226 INLINE bool vbuffer_clone(struct vbuffer *data, struct vbuffer *buffer, bool copy);
227
231 void vbuffer_swap(struct vbuffer *a, struct vbuffer *b);
232
236 bool vbuffer_iterator_isvalid(const struct vbuffer_iterator *position);
237
241 void vbuffer_iterator_copy(const struct vbuffer_iterator *src, struct vbuffer_iterator *dst);
242
246 void vbuffer_iterator_move(struct vbuffer_iterator *iter, const struct vbuffer_iterator *pos);
247
251 void vbuffer_iterator_clear(struct vbuffer_iterator *position);
252
257 size_t vbuffer_iterator_available(struct vbuffer_iterator *position);
258
262 bool vbuffer_iterator_check_available(struct vbuffer_iterator *position, size_t minsize, size_t *size);
263
269 bool vbuffer_iterator_register(struct vbuffer_iterator *position);
270
274 bool vbuffer_iterator_unregister(struct vbuffer_iterator *position);
275
279 bool vbuffer_iterator_insert(struct vbuffer_iterator *position, struct vbuffer *buffer, struct vbuffer_sub *sub);
280
286 size_t vbuffer_iterator_advance(struct vbuffer_iterator *position, size_t len);
287
291 void vbuffer_iterator_skip_empty(struct vbuffer_iterator *position);
292
296 bool vbuffer_iterator_isend(struct vbuffer_iterator *position);
297
302 bool vbuffer_iterator_iseof(struct vbuffer_iterator *position);
303
315 bool vbuffer_iterator_split(struct vbuffer_iterator *position);
316
320 size_t vbuffer_iterator_sub(struct vbuffer_iterator *position, size_t len, struct vbuffer_sub *sub, bool split);
321
326 uint8 vbuffer_iterator_getbyte(struct vbuffer_iterator *position);
327
332 bool vbuffer_iterator_setbyte(struct vbuffer_iterator *position, uint8 byte);
333
337 uint8 *vbuffer_iterator_mmap(struct vbuffer_iterator *position, size_t maxsize, size_t *size, bool write);
338
346 bool vbuffer_iterator_mark(struct vbuffer_iterator *position, bool readonly);
347
352 bool vbuffer_iterator_unmark(struct vbuffer_iterator *position);
353
358 bool vbuffer_iterator_isinsertable(struct vbuffer_iterator *position, struct vbuffer *buffer);
359
363 void vbuffer_sub_clear(struct vbuffer_sub *data);
364
369 bool vbuffer_sub_register(struct vbuffer_sub *data);
370
374 bool vbuffer_sub_unregister(struct vbuffer_sub *data);
375
379 void vbuffer_sub_create(struct vbuffer_sub *data, struct vbuffer *buffer, size_t offset, size_t length);
380
384 bool vbuffer_sub_create_from_position(struct vbuffer_sub *data, struct vbuffer_iterator *position, size_t length);
385
389 bool vbuffer_sub_create_between_position(struct vbuffer_sub *data, struct vbuffer_iterator *begin, struct vbuffer_iterator *end);
390
394 void vbuffer_sub_begin(struct vbuffer_sub *data, struct vbuffer_iterator *begin);
395
399 void vbuffer_sub_end(struct vbuffer_sub *data, struct vbuffer_iterator *end);
400
404 bool vbuffer_sub_position(struct vbuffer_sub *data, struct vbuffer_iterator *iter, size_t offset);
405
409 bool vbuffer_sub_sub(struct vbuffer_sub *data, size_t offset, size_t length, struct vbuffer_sub *buffer);
410
415 size_t vbuffer_sub_size(struct vbuffer_sub *data);
416
420 bool vbuffer_sub_check_size(struct vbuffer_sub *data, size_t minsize, size_t *size);
421
425 size_t vbuffer_sub_read(struct vbuffer_sub *data, uint8 *ptr, size_t size);
426
430 size_t vbuffer_sub_write(struct vbuffer_sub *data, const uint8 *ptr, size_t size);
431
435 const uint8 *vbuffer_sub_flatten(struct vbuffer_sub *data, size_t *size);
436
440 bool vbuffer_sub_compact(struct vbuffer_sub *data);
441
445 bool vbuffer_sub_isflat(struct vbuffer_sub *data);
446
450 bool vbuffer_sub_clone(struct vbuffer_sub *data, struct vbuffer *buffer, clone_mode mode);
451
455 uint8 *vbuffer_mmap(struct vbuffer_sub *data, size_t *len, bool write, struct vbuffer_sub_mmap *mmap_iter, struct vbuffer_iterator *iter);
456
460 bool vbuffer_zero(struct vbuffer_sub *data);
461
466 bool vbuffer_extract(struct vbuffer_sub *data, struct vbuffer *buffer);
467
473 bool vbuffer_select(struct vbuffer_sub *data, struct vbuffer *buffer, struct vbuffer_iterator *ref);
474
478 bool vbuffer_restore(struct vbuffer_iterator *position, struct vbuffer *data, bool clone);
479
483 bool vbuffer_erase(struct vbuffer_sub *data);
484
488 bool vbuffer_replace(struct vbuffer_sub *data, struct vbuffer *buffer);
489
494 int64 vbuffer_asnumber(struct vbuffer_sub *data, bool bigendian);
495
500 bool vbuffer_setnumber(struct vbuffer_sub *data, bool bigendian, int64 num);
501
506 int64 vbuffer_asbits(struct vbuffer_sub *data, size_t offset, size_t bits, bool bigendian);
507
512 bool vbuffer_setbits(struct vbuffer_sub *data, size_t offset, size_t bits, bool bigendian, int64 num);
513
517 size_t vbuffer_asstring(struct vbuffer_sub *data, char *str, size_t len);
518
523 size_t vbuffer_setfixedstring(struct vbuffer_sub *data, const char *str, size_t len);
524
529 bool vbuffer_setstring(struct vbuffer_sub *data, const char *str, size_t len);
530
534 uint8 vbuffer_getbyte(struct vbuffer_sub *data, size_t offset);
535
539 bool vbuffer_setbyte(struct vbuffer_sub *data, size_t offset, uint8 byte);
540
541
542 /*
543 * Inlines
544 */
545
546 INLINE void vbuffer_begin(const struct vbuffer *buf, struct vbuffer_iterator *position)
547 {
548 vbuffer_position(buf, position, 0);
549 }
550
551 INLINE void vbuffer_end(const struct vbuffer *buf, struct vbuffer_iterator *position)
552 {
553 vbuffer_position(buf, position, ALL);
554 }
555
556 INLINE bool vbuffer_check_size(struct vbuffer *buf, size_t minsize, size_t *size)
557 {
558 struct vbuffer_sub sub;
559 vbuffer_sub_create(&sub, buf, 0, ALL);
560 return vbuffer_sub_check_size(&sub, minsize, size);
561 }
562
563 INLINE bool vbuffer_isflat(struct vbuffer *buf)
564 {
565 struct vbuffer_sub sub;
566 vbuffer_sub_create(&sub, buf, 0, ALL);
567 return vbuffer_sub_isflat(&sub);
568 }
569
570 INLINE const uint8 *vbuffer_flatten(struct vbuffer *buf, size_t *size)
571 {
572 struct vbuffer_sub sub;
573 vbuffer_sub_create(&sub, buf, 0, ALL);
574 return vbuffer_sub_flatten(&sub, size);
575 }
576
577 INLINE bool vbuffer_clone(struct vbuffer *data, struct vbuffer *buffer, bool copy)
578 {
579 struct vbuffer_sub sub;
580 vbuffer_sub_create(&sub, data, 0, ALL);
581 return vbuffer_sub_clone(&sub, buffer, copy);
582 }
583
584 #endif /* HAKA_VBUFFER_H */
vbuffer_data
Definition: vbuffer.h:50
vbuffer_flatten
INLINE const uint8 * vbuffer_flatten(struct vbuffer *buf, size_t *size)
Definition: vbuffer.h:570
vbuffer_iterator_clear
void vbuffer_iterator_clear(struct vbuffer_iterator *position)
Definition: vbuffer.c:509
vbuffer_replace
bool vbuffer_replace(struct vbuffer_sub *data, struct vbuffer *buffer)
Definition: vbuffer.c:1490
vbuffer_mmap
uint8 * vbuffer_mmap(struct vbuffer_sub *data, size_t *len, bool write, struct vbuffer_sub_mmap *mmap_iter, struct vbuffer_iterator *iter)
Definition: vbuffer.c:1261
vbuffer_data_ops
Definition: vbuffer.h:40
types.h
vbuffer_iterator_unmark
bool vbuffer_iterator_unmark(struct vbuffer_iterator *position)
Definition: vbuffer.c:972
vbuffer_iterator_insert
bool vbuffer_iterator_insert(struct vbuffer_iterator *position, struct vbuffer *buffer, struct vbuffer_sub *sub)
Definition: vbuffer.c:712
vbuffer
Definition: vbuffer.h:64
vbuffer_iterator
Definition: vbuffer.h:77
CLONE_RO_ORIG
Definition: vbuffer.h:33
vbuffer_sub_check_size
bool vbuffer_sub_check_size(struct vbuffer_sub *data, size_t minsize, size_t *size)
Definition: vbuffer.c:1139
vbuffer_iterator_sub
size_t vbuffer_iterator_sub(struct vbuffer_iterator *position, size_t len, struct vbuffer_sub *sub, bool split)
Definition: vbuffer.c:822
vbuffer_asstring
size_t vbuffer_asstring(struct vbuffer_sub *data, char *str, size_t len)
Definition: vbuffer.c:1946
vbuffer_sub_flatten
const uint8 * vbuffer_sub_flatten(struct vbuffer_sub *data, size_t *size)
Definition: vbuffer.c:1619
vbuffer_clear
void vbuffer_clear(struct vbuffer *buf)
Definition: vbuffer.c:304
vbuffer_setnumber
bool vbuffer_setnumber(struct vbuffer_sub *data, bool bigendian, int64 num)
Definition: vbuffer.c:1745
vbuffer_create_empty
bool vbuffer_create_empty(struct vbuffer *buf)
Definition: vbuffer.c:215
vbsize_t
uint32 vbsize_t
Definition: vbuffer.h:24
vbuffer_zero
bool vbuffer_zero(struct vbuffer_sub *data)
Definition: vbuffer.c:1303
vbuffer_isvalid
bool vbuffer_isvalid(const struct vbuffer *buf)
Definition: vbuffer.c:209
vbuffer_sub_clone
bool vbuffer_sub_clone(struct vbuffer_sub *data, struct vbuffer *buffer, clone_mode mode)
Definition: vbuffer.c:1650
vbuffer_begin
INLINE void vbuffer_begin(const struct vbuffer *buf, struct vbuffer_iterator *position)
Definition: vbuffer.h:546
vbuffer_clone
INLINE bool vbuffer_clone(struct vbuffer *data, struct vbuffer *buffer, bool copy)
Definition: vbuffer.h:577
CLONE_COPY
Definition: vbuffer.h:31
vbuffer_size
size_t vbuffer_size(struct vbuffer *buf)
Definition: vbuffer.c:326
vbuffer_sub_sub
bool vbuffer_sub_sub(struct vbuffer_sub *data, size_t offset, size_t length, struct vbuffer_sub *buffer)
Definition: vbuffer.c:1116
vbuffer_setbits
bool vbuffer_setbits(struct vbuffer_sub *data, size_t offset, size_t bits, bool bigendian, int64 num)
Definition: vbuffer.c:1882
vbuffer_extract
bool vbuffer_extract(struct vbuffer_sub *data, struct vbuffer *buffer)
Definition: vbuffer.c:1420
vbuffer_ismodified
bool vbuffer_ismodified(struct vbuffer *buf)
Definition: vbuffer.c:397
vbuffer_restore
bool vbuffer_restore(struct vbuffer_iterator *position, struct vbuffer *data, bool clone)
Definition: vbuffer.c:1442
vbuffer_iswritable
bool vbuffer_iswritable(struct vbuffer *buf)
Definition: vbuffer.c:391
vbuffer_setfixedstring
size_t vbuffer_setfixedstring(struct vbuffer_sub *data, const char *str, size_t len)
Definition: vbuffer.c:1956
vbuffer_iterator_move
void vbuffer_iterator_move(struct vbuffer_iterator *iter, const struct vbuffer_iterator *pos)
Definition: vbuffer.c:498
vbuffer_iterator_split
bool vbuffer_iterator_split(struct vbuffer_iterator *position)
Definition: vbuffer.c:811
vbuffer_clearmodified
void vbuffer_clearmodified(struct vbuffer *buf)
Definition: vbuffer.c:411
vbuffer_iterator_advance
size_t vbuffer_iterator_advance(struct vbuffer_iterator *position, size_t len)
Definition: vbuffer.c:775
vbuffer_sub_begin
void vbuffer_sub_begin(struct vbuffer_sub *data, struct vbuffer_iterator *begin)
Definition: vbuffer.c:1085
vbuffer_setstring
bool vbuffer_setstring(struct vbuffer_sub *data, const char *str, size_t len)
Definition: vbuffer.c:1961
vbuffer_append
bool vbuffer_append(struct vbuffer *buf, struct vbuffer *buffer)
Definition: vbuffer.c:422
vbuffer_iterator_available
size_t vbuffer_iterator_available(struct vbuffer_iterator *position)
Definition: vbuffer.c:576
vbuffer_isflat
INLINE bool vbuffer_isflat(struct vbuffer *buf)
Definition: vbuffer.h:563
vbuffer_check_size
INLINE bool vbuffer_check_size(struct vbuffer *buf, size_t minsize, size_t *size)
Definition: vbuffer.h:556
vbuffer_sub_create
void vbuffer_sub_create(struct vbuffer_sub *data, struct vbuffer *buffer, size_t offset, size_t length)
Definition: vbuffer.c:1042
vbuffer_sub_position
bool vbuffer_sub_position(struct vbuffer_sub *data, struct vbuffer_iterator *iter, size_t offset)
Definition: vbuffer.c:1101
vbuffer_iterator_getbyte
uint8 vbuffer_iterator_getbyte(struct vbuffer_iterator *position)
Definition: vbuffer.c:851
vbuffer_sub_write
size_t vbuffer_sub_write(struct vbuffer_sub *data, const uint8 *ptr, size_t size)
Definition: vbuffer.c:1174
vbuffer_sub_compact
bool vbuffer_sub_compact(struct vbuffer_sub *data)
Definition: vbuffer.c:1645
int64
HAKA_64BIT_TYPE int64
Definition: types.h:25
vbuffer_sub_clear
void vbuffer_sub_clear(struct vbuffer_sub *data)
Definition: vbuffer.c:1022
vbuffer_create_new
bool vbuffer_create_new(struct vbuffer *buf, size_t size, bool zero)
Definition: vbuffer.c:253
vbuffer_iterator_isinsertable
bool vbuffer_iterator_isinsertable(struct vbuffer_iterator *position, struct vbuffer *buffer)
Definition: vbuffer.c:991
bool
unsigned char bool
Definition: types.h:17
vbuffer_iterator_isend
bool vbuffer_iterator_isend(struct vbuffer_iterator *position)
Definition: vbuffer.c:879
vbuffer_iterator_mark
bool vbuffer_iterator_mark(struct vbuffer_iterator *position, bool readonly)
Definition: vbuffer.c:949
vbuffer_position
void vbuffer_position(const struct vbuffer *buf, struct vbuffer_iterator *position, size_t offset)
Definition: vbuffer.c:340
vbuffer_iterator_mmap
uint8 * vbuffer_iterator_mmap(struct vbuffer_iterator *position, size_t maxsize, size_t *size, bool write)
Definition: vbuffer.c:913
ALL
#define ALL
Definition: vbuffer.h:22
vbuffer_sub_isflat
bool vbuffer_sub_isflat(struct vbuffer_sub *data)
Definition: vbuffer.c:1519
vbuffer_asbits
int64 vbuffer_asbits(struct vbuffer_sub *data, size_t offset, size_t bits, bool bigendian)
Definition: vbuffer.c:1832
vbuffer_swap
void vbuffer_swap(struct vbuffer *a, struct vbuffer *b)
Definition: vbuffer.c:436
vbuffer_setwritable
void vbuffer_setwritable(struct vbuffer *buf, bool writable)
Definition: vbuffer.c:380
vbuffer_iterator_copy
void vbuffer_iterator_copy(const struct vbuffer_iterator *src, struct vbuffer_iterator *dst)
Definition: vbuffer.c:487
vbuffer_iterator_isvalid
bool vbuffer_iterator_isvalid(const struct vbuffer_iterator *position)
Definition: vbuffer.c:481
vbuffer_create_from
bool vbuffer_create_from(struct vbuffer *buf, const char *str, size_t len)
Definition: vbuffer.c:267
vbuffer_sub_create_from_position
bool vbuffer_sub_create_from_position(struct vbuffer_sub *data, struct vbuffer_iterator *position, size_t length)
Definition: vbuffer.c:1063
vbuffer_iterator_unregister
bool vbuffer_iterator_unregister(struct vbuffer_iterator *position)
Definition: vbuffer.c:662
uint8
unsigned char uint8
Definition: types.h:27
vbuffer_select
bool vbuffer_select(struct vbuffer_sub *data, struct vbuffer *buffer, struct vbuffer_iterator *ref)
Definition: vbuffer.c:1427
CLONE_RO_CLONE
Definition: vbuffer.h:34
vbuffer_iterator_iseof
bool vbuffer_iterator_iseof(struct vbuffer_iterator *position)
Definition: vbuffer.c:896
vbuffer_isempty
bool vbuffer_isempty(const struct vbuffer *buf)
Definition: vbuffer.c:357
vbuffer_iterator_check_available
bool vbuffer_iterator_check_available(struct vbuffer_iterator *position, size_t minsize, size_t *size)
Definition: vbuffer.c:643
vbuffer_sub_unregister
bool vbuffer_sub_unregister(struct vbuffer_sub *data)
Definition: vbuffer.c:1035
vbuffer_sub_register
bool vbuffer_sub_register(struct vbuffer_sub *data)
Definition: vbuffer.c:1028
vbuffer_end
INLINE void vbuffer_end(const struct vbuffer *buf, struct vbuffer_iterator *position)
Definition: vbuffer.h:551
vbuffer_sub_mmap
Definition: vbuffer.h:105
vbuffer_sub_read
size_t vbuffer_sub_read(struct vbuffer_sub *data, uint8 *ptr, size_t size)
Definition: vbuffer.c:1154
vbuffer_iterator_setbyte
bool vbuffer_iterator_setbyte(struct vbuffer_iterator *position, uint8 byte)
Definition: vbuffer.c:865
vbuffer_asnumber
int64 vbuffer_asnumber(struct vbuffer_sub *data, bool bigendian)
Definition: vbuffer.c:1704
vbuffer_setbyte
bool vbuffer_setbyte(struct vbuffer_sub *data, size_t offset, uint8 byte)
Definition: vbuffer.c:1979
vbuffer_release
void vbuffer_release(struct vbuffer *buf)
Definition: vbuffer.c:320
vbuffer_iterator_register
bool vbuffer_iterator_register(struct vbuffer_iterator *position)
Definition: vbuffer.c:648
vbuffer_sub_create_between_position
bool vbuffer_sub_create_between_position(struct vbuffer_sub *data, struct vbuffer_iterator *begin, struct vbuffer_iterator *end)
Definition: vbuffer.c:1074
vbuffer_sub_end
void vbuffer_sub_end(struct vbuffer_sub *data, struct vbuffer_iterator *end)
Definition: vbuffer.c:1090
vbuffer_erase
bool vbuffer_erase(struct vbuffer_sub *data)
Definition: vbuffer.c:1480
vbuffer_iterator_skip_empty
void vbuffer_iterator_skip_empty(struct vbuffer_iterator *position)
Definition: vbuffer.c:755
vbuffer_last
void vbuffer_last(const struct vbuffer *buf, struct vbuffer_iterator *position)
Definition: vbuffer.c:363
vbuffer_sub
Definition: vbuffer.h:91
CLONE_RW
Definition: vbuffer.h:32
clone_mode
clone_mode
Definition: vbuffer.h:30
vbuffer_sub_size
size_t vbuffer_sub_size(struct vbuffer_sub *data)
Definition: vbuffer.c:1132
vbuffer_getbyte
uint8 vbuffer_getbyte(struct vbuffer_sub *data, size_t offset)
Definition: vbuffer.c:1971