Nebula
Loading...
Searching...
No Matches
blob.h
Go to the documentation of this file.
1#pragma once
2//------------------------------------------------------------------------------
13#include "core/types.h"
14#include "memory/heap.h"
16#include "io/uri.h"
17
18//------------------------------------------------------------------------------
19namespace Util
20{
21class Blob
22{
23public:
25 static void Setup();
27 static void Shutdown();
29 void* operator new(size_t s);
31 void operator delete(void* ptr);
32
34 Blob();
36 Blob(std::nullptr_t t);
38 Blob(const void* ptr, size_t size);
40 Blob(size_t size);
42 Blob(const Blob& rhs);
44 Blob(Blob&& rhs) noexcept;
46 ~Blob();
48 void operator=(const Blob& rhs);
50 void operator=(Blob&& rhs) noexcept;
51
53 bool operator==(const Blob& rhs) const;
55 bool operator!=(const Blob& rhs) const;
57 bool operator>(const Blob& rhs) const;
59 bool operator<(const Blob& rhs) const;
61 bool operator>=(const Blob& rhs) const;
63 bool operator<=(const Blob& rhs) const;
64
66 bool IsValid() const;
68 void Reserve(size_t size);
70 void Trim(size_t size);
72 void Set(const void* ptr, size_t size);
74 void SetFromBase64(const void* ptr, size_t size);
76 void SetFromFile(const IO::URI & uri);
77
79 void SetChunk(const void* from, size_t size, size_t internalOffset);
81 void* GetPtr() const;
83 size_t Size() const;
85 uint32_t HashCode() const;
87 Util::Blob GetBase64() const;
88
89private:
91 void Delete();
93 void Allocate(size_t size);
95 void Copy(const void* ptr, size_t size);
97 void GrowTo(size_t size);
99 int BinaryCompare(const Blob& rhs) const;
100
102
103 void* ptr = nullptr;
104 size_t size = 0;
105 size_t allocSize = 0;
106};
107
108//------------------------------------------------------------------------------
111__forceinline void*
112Blob::operator new(size_t size)
113{
114#if NEBULA_DEBUG
115 n_assert(size == sizeof(Blob));
116#endif
117 return Memory::Alloc(Memory::ObjectHeap, size);
118}
119
120//------------------------------------------------------------------------------
123__forceinline void
124Blob::operator delete(void* ptr)
125{
126 return Memory::Free(Memory::ObjectHeap, ptr);
127}
128
129//------------------------------------------------------------------------------
132inline void
134{
135 n_assert(0 == DataHeap);
136 DataHeap = new Memory::Heap("Util.Blob.DataHeap");
137}
138
139//------------------------------------------------------------------------------
142inline void
144{
145 n_assert(0 != DataHeap);
146 delete DataHeap;
147 DataHeap = 0;
148}
149
150//------------------------------------------------------------------------------
153inline
155 ptr(0),
156 size(0),
157 allocSize(0)
158{
159 // empty
160}
161
162//------------------------------------------------------------------------------
165inline
166Blob::Blob(std::nullptr_t t) :
167 ptr(0),
168 size(0),
169 allocSize(0)
170{
171 // empty
172}
173
174//------------------------------------------------------------------------------
177inline
178Blob::Blob(const void* fromPtr, size_t fromSize) :
179 ptr(0),
180 size(0),
181 allocSize(0)
182{
183 this->Copy(fromPtr, fromSize);
184}
185
186//------------------------------------------------------------------------------
189inline
190Blob::Blob(const Blob& rhs) :
191 ptr(0),
192 size(0),
193 allocSize(0)
194{
195 if (rhs.IsValid())
196 {
197 this->Copy(rhs.ptr, rhs.size);
198 }
199}
200
201//------------------------------------------------------------------------------
204inline
205Blob::Blob(Blob&& rhs) noexcept :
206 ptr(0),
207 size(0),
208 allocSize(0)
209{
210 if (rhs.IsValid())
211 {
212 this->ptr = rhs.ptr;
213 this->size = rhs.size;
214 this->allocSize = rhs.allocSize;
215 rhs.ptr = nullptr;
216 rhs.size = 0;
217 rhs.allocSize = 0;
218 }
219}
220
221//------------------------------------------------------------------------------
224inline
225Blob::Blob(size_t s) :
226 ptr(0),
227 size(0),
228 allocSize(0)
229{
230 this->Allocate(s);
231}
232
233//------------------------------------------------------------------------------
236inline bool
238{
239 return (0 != this->ptr);
240}
241
242//------------------------------------------------------------------------------
245inline void
247{
248 if (this->IsValid())
249 {
250 n_assert(0 != DataHeap);
251 DataHeap->Free((void*)this->ptr);
252 this->ptr = 0;
253 this->size = 0;
254 this->allocSize = 0;
255 }
256}
257
258//------------------------------------------------------------------------------
261inline
263{
264 this->Delete();
265}
266
267//------------------------------------------------------------------------------
270inline void
272{
273 n_assert(!this->IsValid());
274 n_assert(0 != DataHeap);
275 this->ptr = DataHeap->Alloc(s);
276 this->allocSize = s;
277 this->size = s;
278}
279
280//------------------------------------------------------------------------------
283inline void
284Blob::Copy(const void* fromPtr, size_t fromSize)
285{
286 n_assert((0 != fromPtr) && (fromSize > 0));
287
288 // only re-allocate if not enough space
289 if ((0 == this->ptr) || (this->allocSize < fromSize))
290 {
291 this->Delete();
292 this->Allocate(fromSize);
293 }
294 this->size = fromSize;
295 Memory::Copy(fromPtr, (void*)this->ptr, fromSize);
296}
297
298//------------------------------------------------------------------------------
301inline void
302Blob::GrowTo(size_t size)
303{
304 n_assert(this->allocSize < size);
305
306 void* newPtr = DataHeap->Alloc(size);
307 Memory::Copy(this->ptr, newPtr, this->size);
308
309 this->Delete();
310
311 this->ptr = newPtr;
312 this->allocSize = size;
313}
314
315//------------------------------------------------------------------------------
318inline void
320{
321 if (rhs.IsValid())
322 {
323 this->Copy(rhs.ptr, rhs.size);
324 }
325}
326
327//------------------------------------------------------------------------------
330inline void
331Blob::operator=(Blob&& rhs) noexcept
332{
333 if (this != &rhs)
334 {
335 if (this->IsValid())
336 {
337 this->Delete();
338 }
339 this->ptr = rhs.ptr;
340 this->size = rhs.size;
341 this->allocSize = rhs.allocSize;
342 rhs.ptr = nullptr;
343 rhs.size = 0;
344 rhs.allocSize = 0;
345 }
346}
347
348//------------------------------------------------------------------------------
351inline bool
352Blob::operator==(const Blob& rhs) const
353{
354 return (this->BinaryCompare(rhs) == 0);
355}
356
357//------------------------------------------------------------------------------
360inline bool
361Blob::operator!=(const Blob& rhs) const
362{
363 return (this->BinaryCompare(rhs) != 0);
364}
365
366//------------------------------------------------------------------------------
369inline bool
370Blob::operator>(const Blob& rhs) const
371{
372 return (this->BinaryCompare(rhs) > 0);
373}
374
375//------------------------------------------------------------------------------
378inline bool
379Blob::operator<(const Blob& rhs) const
380{
381 return (this->BinaryCompare(rhs) < 0);
382}
383
384//------------------------------------------------------------------------------
387inline bool
388Blob::operator>=(const Blob& rhs) const
389{
390 return (this->BinaryCompare(rhs) >= 0);
391}
392
393//------------------------------------------------------------------------------
396inline bool
397Blob::operator<=(const Blob& rhs) const
398{
399 return (this->BinaryCompare(rhs) <= 0);
400}
401
402//------------------------------------------------------------------------------
405inline void
407{
408 if (this->IsValid())
409 {
410 if (this->allocSize < s)
411 {
412 this->Delete();
413 this->Allocate(s);
414 }
415 }
416 else
417 {
418 this->Allocate(s);
419 }
420 this->size = s;
421}
422
423//------------------------------------------------------------------------------
426inline void
427Blob::Trim(size_t trimSize)
428{
429 n_assert(trimSize <= this->size);
430 this->size = trimSize;
431}
432
433//------------------------------------------------------------------------------
436inline void
437Blob::Set(const void* fromPtr, size_t fromSize)
438{
439 this->Copy(fromPtr, fromSize);
440}
441
442//------------------------------------------------------------------------------
445inline void
446Blob::SetChunk(const void* from, size_t size, size_t internalOffset)
447{
448 n_assert((0 != from) && (size > 0));
449 n_assert(nullptr != this->ptr)
450
451 size_t newSize = (internalOffset + size);
452 if (newSize > this->allocSize)
453 {
454 this->GrowTo(newSize);
455 }
456
457 Memory::Copy(from, (void*)((byte*)this->ptr + internalOffset), size);
458}
459
460//------------------------------------------------------------------------------
463inline void*
465{
466 n_assert(this->IsValid());
467 return this->ptr;
468}
469
470//------------------------------------------------------------------------------
473inline size_t
475{
476 n_assert(this->IsValid());
477 return this->size;
478}
479
480//------------------------------------------------------------------------------
483inline uint32_t
485{
486 IndexT hash = 0;
487 const char* charPtr = (const char*) this->ptr;
488 for (size_t i = 0; i < this->size; i++)
489 {
490 hash += charPtr[i];
491 hash += hash << 10;
492 hash ^= hash >> 6;
493 }
494 hash += hash << 3;
495 hash ^= hash >> 11;
496 hash += hash << 15;
497 hash &= ~(1<<31); // don't return a negative number (in case IndexT is defined signed)
498 return hash;
499}
500
501} // namespace Util
502//------------------------------------------------------------------------------
An URI object can split a Uniform Resource Identifier string into its components or build a string fr...
Definition uri.h:67
Implements a private heap.
The Util::Blob class encapsulates a chunk of raw memory into a C++ object which can be copied,...
Definition blob.h:22
Util::Blob GetBase64() const
get as base64 encoded
Definition blob.cc:61
void Copy(const void *ptr, size_t size)
copy content
Definition blob.h:284
void * GetPtr() const
get blob ptr
Definition blob.h:464
bool operator>=(const Blob &rhs) const
greater-equal operator
Definition blob.h:388
void Set(const void *ptr, size_t size)
set blob contents
Definition blob.h:437
void * ptr
Definition blob.h:103
void Delete()
delete content
Definition blob.h:246
void Trim(size_t size)
trim the size member (without re-allocating!)
Definition blob.h:427
bool operator<(const Blob &rhs) const
less operator
Definition blob.h:379
bool IsValid() const
return true if the blob contains data
Definition blob.h:237
bool operator!=(const Blob &rhs) const
inequality operator
Definition blob.h:361
size_t allocSize
Definition blob.h:105
bool operator==(const Blob &rhs) const
equality operator
Definition blob.h:352
uint32_t HashCode() const
get a hash code (compatible with Util::HashTable)
Definition blob.h:484
~Blob()
destructor
Definition blob.h:262
void SetFromFile(const IO::URI &uri)
set from file
Definition blob.cc:77
void Allocate(size_t size)
allocate internal buffer
Definition blob.h:271
static void Setup()
static Setup method, called by SysFunc::Setup()
Definition blob.h:133
Blob()
default constructor
Definition blob.h:154
bool operator>(const Blob &rhs) const
greater operator
Definition blob.h:370
size_t Size() const
get blob size
Definition blob.h:474
void GrowTo(size_t size)
Increases allocated size without deleting existing data (reallocate and memcopy).
Definition blob.h:302
static void Shutdown()
static Shutdown method called by SysFunc::Exit
Definition blob.h:143
int BinaryCompare(const Blob &rhs) const
do a binary comparison between this and other blob
Definition blob.cc:23
void SetChunk(const void *from, size_t size, size_t internalOffset)
set chunk contents. Will allocate more memory if necessary.
Definition blob.h:446
void operator=(const Blob &rhs)
assignment operator
Definition blob.h:319
void SetFromBase64(const void *ptr, size_t size)
set from base64 enconded
Definition blob.cc:46
void Reserve(size_t size)
reserve N bytes
Definition blob.h:406
static Memory::Heap * DataHeap
Definition blob.h:101
bool operator<=(const Blob &rhs) const
less-eqial operator
Definition blob.h:397
size_t size
Definition blob.h:104
#define n_assert(exp)
Definition debug.h:50
void Copy(const void *from, void *to, size_t numBytes)
Copy a chunk of memory (note the argument order is different from memcpy()!!!)
Definition osxmemory.cc:213
void * Alloc(HeapType heapType, size_t size, size_t alignment)
Allocate a block of memory from one of the global heaps.
Definition osxmemory.cc:56
void Free(HeapType heapType, void *ptr)
Free a block of memory.
Definition osxmemory.cc:136
@ ObjectHeap
Definition osxmemoryconfig.h:27
A pinned array is an array which manages its own virtual memory.
Definition String.cs:6
int IndexT
Definition types.h:48