forked from LeenkxTeam/LNXSDK
Update Files
This commit is contained in:
53
Kha/Backends/Kinc-hxcpp/khacpp/include/hx/StringAlloc.h
Executable file
53
Kha/Backends/Kinc-hxcpp/khacpp/include/hx/StringAlloc.h
Executable file
@ -0,0 +1,53 @@
|
||||
#ifndef HX_STRING_ALLOC
|
||||
#define HX_STRING_ALLOC
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
|
||||
namespace hx
|
||||
{
|
||||
|
||||
class IStringAlloc
|
||||
{
|
||||
public:
|
||||
virtual void *allocBytes(size_t inBytes) = 0;
|
||||
|
||||
protected:
|
||||
~IStringAlloc(){};
|
||||
};
|
||||
|
||||
template<int STACK>
|
||||
class StringAlloc : public IStringAlloc
|
||||
{
|
||||
char buffer[STACK];
|
||||
size_t alloced;
|
||||
char *heap;
|
||||
|
||||
public:
|
||||
inline StringAlloc() : alloced(0), heap(0) { }
|
||||
~StringAlloc()
|
||||
{
|
||||
if (heap)
|
||||
free(heap);
|
||||
}
|
||||
void *allocBytes(size_t inBytes)
|
||||
{
|
||||
if (inBytes<=STACK)
|
||||
return buffer;
|
||||
if (inBytes>alloced)
|
||||
{
|
||||
alloced = inBytes;
|
||||
heap = (char *)realloc(heap, alloced);
|
||||
}
|
||||
return heap;
|
||||
}
|
||||
private:
|
||||
StringAlloc(const StringAlloc &);
|
||||
void operator=(const StringAlloc &);
|
||||
};
|
||||
|
||||
typedef StringAlloc<100> strbuf;
|
||||
|
||||
} // end namespace hx
|
||||
|
||||
#endif
|
Reference in New Issue
Block a user