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 SetSize(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
101 static Memory::Heap* DataHeap;
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
118}
119
120//------------------------------------------------------------------------------
123__forceinline void
124Blob::operator delete(void* ptr)
125{
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 this->size = s;
232}
233
234//------------------------------------------------------------------------------
237inline bool
239{
240 return (0 != this->ptr);
241}
242
243//------------------------------------------------------------------------------
246inline void
248{
249 if (this->IsValid())
250 {
251 n_assert(0 != DataHeap);
252 DataHeap->Free((void*)this->ptr);
253 this->ptr = 0;
254 this->size = 0;
255 this->allocSize = 0;
256 }
257}
258
259//------------------------------------------------------------------------------
262inline
264{
265 this->Delete();
266}
267
268//------------------------------------------------------------------------------
271inline void
273{
274 n_assert(!this->IsValid());
275 n_assert(0 != DataHeap);
276 this->ptr = DataHeap->Alloc(s);
277 this->allocSize = 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
303{
304 n_assert(this->allocSize < size);
305 n_assert(0 != DataHeap);
306
307 const size_t oldSize = this->size;
308 void* newPtr = DataHeap->Alloc(size);
309 if (this->ptr && oldSize > 0)
310 {
311 Memory::Copy(this->ptr, newPtr, oldSize);
312 }
313
314 this->Delete();
315
316 this->ptr = newPtr;
317 this->size = oldSize;
318 this->allocSize = size;
319}
320
321//------------------------------------------------------------------------------
324inline void
326{
327 if (rhs.IsValid())
328 {
329 this->Copy(rhs.ptr, rhs.size);
330 }
331 else
332 {
333 this->Delete();
334 }
335}
336
337//------------------------------------------------------------------------------
340inline void
341Blob::operator=(Blob&& rhs) noexcept
342{
343 if (this != &rhs)
344 {
345 if (this->IsValid())
346 {
347 this->Delete();
348 }
349 this->ptr = rhs.ptr;
350 this->size = rhs.size;
351 this->allocSize = rhs.allocSize;
352 rhs.ptr = nullptr;
353 rhs.size = 0;
354 rhs.allocSize = 0;
355 }
356}
357
358//------------------------------------------------------------------------------
361inline bool
362Blob::operator==(const Blob& rhs) const
363{
364 return (this->BinaryCompare(rhs) == 0);
365}
366
367//------------------------------------------------------------------------------
370inline bool
371Blob::operator!=(const Blob& rhs) const
372{
373 return (this->BinaryCompare(rhs) != 0);
374}
375
376//------------------------------------------------------------------------------
379inline bool
380Blob::operator>(const Blob& rhs) const
381{
382 return (this->BinaryCompare(rhs) > 0);
383}
384
385//------------------------------------------------------------------------------
388inline bool
389Blob::operator<(const Blob& rhs) const
390{
391 return (this->BinaryCompare(rhs) < 0);
392}
393
394//------------------------------------------------------------------------------
397inline bool
398Blob::operator>=(const Blob& rhs) const
399{
400 return (this->BinaryCompare(rhs) >= 0);
401}
402
403//------------------------------------------------------------------------------
406inline bool
407Blob::operator<=(const Blob& rhs) const
408{
409 return (this->BinaryCompare(rhs) <= 0);
410}
411
412//------------------------------------------------------------------------------
415inline void
417{
418 if (s > this->allocSize)
419 {
420 if (this->IsValid())
421 {
422 this->GrowTo(s);
423 }
424 else
425 {
426 this->Allocate(s);
427 }
428 }
429 n_assert(s <= this->allocSize);
430 this->size = s;
431}
432
433//------------------------------------------------------------------------------
436inline void
437Blob::Trim(size_t trimSize)
438{
439 n_assert(trimSize <= this->size);
440 this->size = trimSize;
441}
442
443//------------------------------------------------------------------------------
446inline void
447Blob::Set(const void* fromPtr, size_t fromSize)
448{
449 this->Copy(fromPtr, fromSize);
450}
451
452//------------------------------------------------------------------------------
455inline void
456Blob::SetChunk(const void* from, size_t size, size_t internalOffset)
457{
458 n_assert((0 != from) && (size > 0));
459 size_t newSize = (internalOffset + size);
460
461 if (!this->IsValid())
462 {
463 this->Allocate(newSize);
464 this->size = 0;
465 }
466 else if (newSize > this->allocSize)
467 {
468 this->GrowTo(newSize);
469 }
470
471 Memory::Copy(from, (void*)((byte*)this->ptr + internalOffset), size);
472 this->size = Math::max(this->size, newSize);
473}
474
475//------------------------------------------------------------------------------
478inline void*
480{
481 return this->ptr;
482}
483
484//------------------------------------------------------------------------------
487inline size_t
489{
490 return this->size;
491}
492
493//------------------------------------------------------------------------------
496inline uint32_t
498{
499 IndexT hash = 0;
500 const char* charPtr = (const char*) this->ptr;
501 for (size_t i = 0; i < this->size; i++)
502 {
503 hash += charPtr[i];
504 hash += hash << 10;
505 hash ^= hash >> 6;
506 }
507 hash += hash << 3;
508 hash ^= hash >> 11;
509 hash += hash << 15;
510 hash &= ~(1<<31); // don't return a negative number (in case IndexT is defined signed)
511 return hash;
512}
513
514} // namespace Util
515//------------------------------------------------------------------------------
An URI object can split a Uniform Resource Identifier string into its components or build a string fr...
Definition uri.h:67
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:71
void Copy(const void *ptr, size_t size)
copy content
Definition blob.h:284
void * GetPtr() const
get blob ptr
Definition blob.h:479
bool operator>=(const Blob &rhs) const
greater-equal operator
Definition blob.h:398
void SetSize(size_t size)
set size in bytes, allocating if needed
Definition blob.h:416
void Set(const void *ptr, size_t size)
set blob contents
Definition blob.h:447
void * ptr
Definition blob.h:103
void Delete()
delete content
Definition blob.h:247
void Trim(size_t size)
trim the size member (without re-allocating!)
Definition blob.h:437
bool operator<(const Blob &rhs) const
less operator
Definition blob.h:389
bool IsValid() const
return true if the blob contains data
Definition blob.h:238
bool operator!=(const Blob &rhs) const
inequality operator
Definition blob.h:371
size_t allocSize
Definition blob.h:105
bool operator==(const Blob &rhs) const
equality operator
Definition blob.h:362
uint32_t HashCode() const
get a hash code (compatible with Util::HashTable)
Definition blob.h:497
~Blob()
destructor
Definition blob.h:263
void SetFromFile(const IO::URI &uri)
set from file
Definition blob.cc:87
void Allocate(size_t size)
allocate internal buffer
Definition blob.h:272
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:380
size_t Size() const
get blob size
Definition blob.h:488
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:456
void operator=(const Blob &rhs)
assignment operator
Definition blob.h:325
void SetFromBase64(const void *ptr, size_t size)
set from base64 enconded
Definition blob.cc:55
static Memory::Heap * DataHeap
Definition blob.h:101
bool operator<=(const Blob &rhs) const
less-eqial operator
Definition blob.h:407
size_t size
Definition blob.h:104
#define n_assert(exp)
Definition debug.h:50
__forceinline TYPE max(TYPE a, TYPE b)
Definition scalar.h:368
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 quad tree designed to return regions of free 2D space.
Definition String.cs:6
int IndexT
Definition types.h:41