-
Notifications
You must be signed in to change notification settings - Fork 6
/
avlib.hpp
executable file
·261 lines (221 loc) · 6.52 KB
/
avlib.hpp
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
/**
* @file avlib.hpp
* @brief Common admin methods for the AV Library
*
* @copyright Copyright 2015 Samir Sinha. All rights reserved.
* @license This project is released under the ISC license. See LICENSE
* for the full text.
*/
#ifndef CINEK_AVLIB_HPP
#define CINEK_AVLIB_HPP
#include "avdefs.hpp"
#if CINEK_AVLIB_IOSTREAMS
#include <streambuf>
#include <istream>
#endif
#if CINEK_AVLIB_EXCEPTIONS
#include <stdexcept>
#endif
namespace cinekav {
typedef void* (*AllocFn)(void* context, int region, size_t sz);
typedef void (*FreeFn)(void* context, int region, void* ptr);
void initialize(AllocFn allocFn, FreeFn freeFn, void* context);
struct Memory
{
Memory() : _region(0) {}
Memory(int region) : _region(region) {}
void* allocate(size_t sz);
void free(void* ptr);
template<typename T, typename... Args>
T* create(Args&&... args)
{
T* p = reinterpret_cast<T*>(allocate(sizeof(T)));
::new(p) T(std::forward<Args>(args)...);
return p;
}
template<typename T>
void destroy(T* ptr)
{
ptr->~T();
free(ptr);
}
private:
friend bool operator==(const Memory& lha, const Memory& rha);
friend bool operator!=(const Memory& lha, const Memory& rha);
int _region;
};
inline bool operator==(const Memory& lha, const Memory& rha)
{
return (lha._region == rha._region);
}
inline bool operator!=(const Memory& lha, const Memory& rha)
{
return (lha._region != rha._region);
}
class StringBuffer;
class Buffer
{
public:
Buffer(const Memory& memory=Memory());
Buffer(int sz, const Memory& memory=Memory());
Buffer(uint8_t* buffer, int sz);
Buffer(uint8_t* buffer, int sz, int limit);
~Buffer();
Buffer(const Buffer& other) = delete;
Buffer& operator=(const Buffer& other) = delete;
Buffer(Buffer&& other);
Buffer& operator=(Buffer&& other);
operator bool() const {
return _buffer != nullptr;
}
bool empty() const { return _head == _tail; }
bool overflow() const { return _overflow; }
int pushBytes(const uint8_t* bytes, int sz);
#if CINEK_AVLIB_IOSTREAMS
int pushBytesFromStream(std::basic_istream<char>& istr, int cnt);
#endif
void reset();
Buffer& pullBytesFrom(Buffer& target, int cnt, int* pulled);
uint8_t pullByte() {
_overflow = _overflow || (_head == _tail);
if (_overflow)
return 0;
return *(_head++);
}
uint16_t pullUInt16() {
uint16_t s = pullByte();
s <<= 8;
s |= pullByte();
return s;
}
uint32_t pullUInt32() {
uint32_t ui = pullUInt16();
ui <<= 16;
ui |= pullUInt16();
return ui;
}
void skip(uint32_t cnt) {
_head += cnt;
_overflow = _overflow || (_head > _tail);
if (_head > _tail)
_head = _tail;
}
int headAvailable() const {
return _head - _buffer;
}
int size() const {
return _tail - _head;
}
int available() const {
return _limit - _tail;
}
int capacity() const {
return _limit - _buffer;
}
const uint8_t* head() const {
return _head;
}
const uint8_t* tail() const {
return _tail;
}
uint8_t* obtain(int sz) {
if (_tail+sz > _limit)
return nullptr;
uint8_t* tail = _tail;
_tail += sz;
return tail;
}
// Generates a subuffer from the available space of the owning buffer.
// This is a buffer composed of [tail+offset, tail+offset+sz]
// If either the buffer offset or size result in a buffer falling outside
// this buffer's memory region, the returned buffer will reflect the
// difference
Buffer createSubBuffer(int offset, int sz);
// creates a buffer mapped to the memory from head to tail of this buffer.
Buffer createSubBufferFromUsed();
private:
friend class StringBuffer;
Memory _memory;
uint8_t* _buffer;
uint8_t* _head;
uint8_t* _tail;
uint8_t* _limit;
bool _overflow;
bool _owned;
};
class StringBuffer
{
public:
StringBuffer();
StringBuffer(int sz, const Memory& memory=Memory());
StringBuffer(Buffer&& buffer);
// skips null characters if delim != 0.
// else terminates on delim or end of buffer.
StringBuffer& getline(std::string& str, char delim='\n');
bool end() const;
private:
Buffer _buffer;
};
/**
* @class std_allocator
* @brief A std::allocator compliant allocator for STL containers.
*/
template <typename T, class Allocator=Memory>
struct std_allocator
{
/** @cond */
typedef size_t size_type;
typedef ptrdiff_t difference_type;
typedef T* pointer;
typedef const T* const_pointer;
typedef T& reference;
typedef const T& const_reference;
typedef T value_type;
template <class U> struct rebind {
typedef std_allocator<U, Allocator> other;
};
std_allocator() {}
std_allocator(const Allocator& allocator): _allocator(allocator) {}
std_allocator(const std_allocator& source): _allocator(source._allocator) {}
template <class U> std_allocator(const std_allocator<U, Allocator>& source): _allocator(source._allocator) {}
pointer address(reference x) const { return &x; }
const_pointer address(const_reference x) const { return &x; }
pointer allocate(size_type s, const void* = 0) {
if (s == 0)
return nullptr;
pointer temp = static_cast<pointer>(_allocator.allocate(s*sizeof(T)));
#if CINEK_AVLIB_EXCEPTIONS
if (temp == nullptr)
throw std::bad_alloc();
#endif
return temp;
}
void deallocate(pointer p, size_type) {
_allocator.free((void* )p);
}
size_type max_size() const {
return std::numeric_limits<size_t>::max() / sizeof(T);
}
template<class U, class... Args> void construct(U* p, Args&&... args) {
::new((void *)p) U(std::forward<Args>(args)...);
}
void destroy(pointer p) {
p->~T();
}
Allocator _allocator;
/** @endcond */
};
template<typename T, class Allocator>
inline bool operator==(const std_allocator<T, Allocator>& lha,
const std_allocator<T, Allocator>& rha)
{
return lha._allocator == rha._allocator;
}
template<typename T, class Allocator>
inline bool operator!=(const std_allocator<T, Allocator>& lha,
const std_allocator<T, Allocator>& rha)
{
return lha._allocator != rha._allocator;
}
} /* namespace cinekav */
#endif