mirror of
https://github.com/bkaradzic/bx.git
synced 2026-02-17 20:52:37 +01:00
Removed dynamic allocation from HandleAlloc.
This commit is contained in:
@@ -8,24 +8,31 @@
|
||||
|
||||
#include "bx.h"
|
||||
|
||||
#include <memory.h>
|
||||
#include <new>
|
||||
|
||||
#if BX_CONFIG_ALLOCATOR_CRT
|
||||
# include <malloc.h>
|
||||
#endif // BX_CONFIG_ALLOCATOR_CRT
|
||||
|
||||
#if BX_CONFIG_ALLOCATOR_DEBUG
|
||||
# define BX_ALLOC(_allocator, _size) bx::alloc(_allocator, _size, __FILE__, __LINE__)
|
||||
# define BX_REALLOC(_allocator, _ptr, _size) bx::realloc(_allocator, _ptr, _size, __FILE__, __LINE__)
|
||||
# define BX_FREE(_allocator, _ptr) bx::free(_allocator, _ptr, __FILE__, __LINE__)
|
||||
# define BX_ALIGNED_ALLOC(_allocator, _size) bx::alignedAlloc(_allocator, _size, __FILE__, __LINE__)
|
||||
# define BX_ALIGNED_REALLOC(_allocator, _ptr, _size) bx::alignedRealloc(_allocator, _ptr, _size, __FILE__, __LINE__)
|
||||
# define BX_ALIGNED_FREE(_allocator, _ptr) bx::alignedFree(_allocator, _ptr, __FILE__, __LINE__)
|
||||
# define BX_ALLOC(_allocator, _size) bx::alloc(_allocator, _size, __FILE__, __LINE__)
|
||||
# define BX_REALLOC(_allocator, _ptr, _size) bx::realloc(_allocator, _ptr, _size, __FILE__, __LINE__)
|
||||
# define BX_FREE(_allocator, _ptr) bx::free(_allocator, _ptr, __FILE__, __LINE__)
|
||||
# define BX_ALIGNED_ALLOC(_allocator, _size, _align) bx::alignedAlloc(_allocator, _size, _align, __FILE__, __LINE__)
|
||||
# define BX_ALIGNED_REALLOC(_allocator, _ptr, _size, _align) bx::alignedRealloc(_allocator, _ptr, _size, _align, __FILE__, __LINE__)
|
||||
# define BX_ALIGNED_FREE(_allocator, _ptr) bx::alignedFree(_allocator, _ptr, __FILE__, __LINE__)
|
||||
# define BX_NEW(_allocator, _type) new(BX_ALLOC(_allocator, sizeof(_type) ) ) _type
|
||||
# define BX_DELETE(_allocator, _ptr) bx::deleteObject(_allocator, _ptr, __FILE__, __LINE__)
|
||||
#else
|
||||
# define BX_ALLOC(_allocator, _size) bx::alloc(_allocator, _size)
|
||||
# define BX_REALLOC(_allocator, _ptr, _size) bx::realloc(_allocator, _ptr, _size)
|
||||
# define BX_FREE(_allocator, _ptr) bx::free(_allocator, _ptr)
|
||||
# define BX_ALIGNED_ALLOC(_allocator, _size) bx::alignedAlloc(_allocator, _size)
|
||||
# define BX_ALIGNED_REALLOC(_allocator, _ptr, _size) bx::alignedRealloc(_allocator, _ptr, _size)
|
||||
# define BX_ALIGNED_FREE(_allocator, _ptr) bx::alignedFree(_allocator, _ptr)
|
||||
# define BX_ALLOC(_allocator, _size) bx::alloc(_allocator, _size)
|
||||
# define BX_REALLOC(_allocator, _ptr, _size) bx::realloc(_allocator, _ptr, _size)
|
||||
# define BX_FREE(_allocator, _ptr) bx::free(_allocator, _ptr)
|
||||
# define BX_ALIGNED_ALLOC(_allocator, _size, _align) bx::alignedAlloc(_allocator, _size, _align)
|
||||
# define BX_ALIGNED_REALLOC(_allocator, _ptr, _size, _align) bx::alignedRealloc(_allocator, _ptr, _size, _align)
|
||||
# define BX_ALIGNED_FREE(_allocator, _ptr) bx::alignedFree(_allocator, _ptr)
|
||||
# define BX_NEW(_allocator, _type) ::new(BX_ALLOC(_allocator, sizeof(_type) ) ) _type
|
||||
# define BX_DELETE(_allocator, _ptr) bx::deleteObject(_allocator, _ptr)
|
||||
#endif // BX_CONFIG_DEBUG_ALLOC
|
||||
|
||||
#ifndef BX_CONFIG_ALLOCATOR_NATURAL_ALIGNMENT
|
||||
@@ -170,6 +177,16 @@ namespace bx
|
||||
return _allocator->alignedRealloc(_ptr, _size, _align, _file, _line);
|
||||
}
|
||||
|
||||
template <typename ObjectT>
|
||||
inline void deleteObject(AllocatorI* _allocator, ObjectT* _object, const char* _file = NULL, uint32_t _line = 0)
|
||||
{
|
||||
if (NULL != _object)
|
||||
{
|
||||
_object->~ObjectT();
|
||||
free(_allocator, _object, _file, _line);
|
||||
}
|
||||
}
|
||||
|
||||
#if BX_CONFIG_ALLOCATOR_CRT
|
||||
class CrtAllocator : public ReallocatorI, public AlignedReallocatorI
|
||||
{
|
||||
|
||||
@@ -7,39 +7,109 @@
|
||||
#define __BX_HANDLE_ALLOC_H__
|
||||
|
||||
#include "bx.h"
|
||||
#include "allocator.h"
|
||||
|
||||
namespace bx
|
||||
{
|
||||
template <uint16_t MaxHandlesT>
|
||||
class HandleAllocT
|
||||
{
|
||||
public:
|
||||
static const uint16_t invalid = 0xffff;
|
||||
|
||||
HandleAllocT()
|
||||
: m_numHandles(0)
|
||||
{
|
||||
for (uint16_t ii = 0; ii < MaxHandlesT; ++ii)
|
||||
{
|
||||
m_handles[ii] = ii;
|
||||
}
|
||||
}
|
||||
|
||||
~HandleAllocT()
|
||||
{
|
||||
}
|
||||
|
||||
const uint16_t* getHandles() const
|
||||
{
|
||||
return m_handles;
|
||||
}
|
||||
|
||||
uint16_t getHandleAt(uint16_t _at) const
|
||||
{
|
||||
return m_handles[_at];
|
||||
}
|
||||
|
||||
uint16_t getNumHandles() const
|
||||
{
|
||||
return m_numHandles;
|
||||
}
|
||||
|
||||
uint16_t getMaxHandles() const
|
||||
{
|
||||
return MaxHandlesT;
|
||||
}
|
||||
|
||||
uint16_t alloc()
|
||||
{
|
||||
if (m_numHandles < MaxHandlesT)
|
||||
{
|
||||
uint16_t index = m_numHandles;
|
||||
++m_numHandles;
|
||||
|
||||
uint16_t handle = m_handles[index];
|
||||
uint16_t* sparse = &m_handles[MaxHandlesT];
|
||||
sparse[handle] = index;
|
||||
return handle;
|
||||
}
|
||||
|
||||
return invalid;
|
||||
}
|
||||
|
||||
void free(uint16_t _handle)
|
||||
{
|
||||
uint16_t* sparse = &m_handles[MaxHandlesT];
|
||||
uint16_t index = sparse[_handle];
|
||||
--m_numHandles;
|
||||
uint16_t temp = m_handles[m_numHandles];
|
||||
m_handles[m_numHandles] = _handle;
|
||||
sparse[temp] = index;
|
||||
m_handles[index] = temp;
|
||||
}
|
||||
|
||||
private:
|
||||
uint16_t m_handles[MaxHandlesT*2];
|
||||
uint16_t m_numHandles;
|
||||
};
|
||||
|
||||
class HandleAlloc
|
||||
{
|
||||
public:
|
||||
static const uint16_t invalid = 0xffff;
|
||||
|
||||
HandleAlloc(uint16_t _maxHandles)
|
||||
: m_dense(new uint16_t[_maxHandles*2])
|
||||
, m_sparse(&m_dense[_maxHandles])
|
||||
HandleAlloc(uint16_t _maxHandles, void* _handles)
|
||||
: m_handles( (uint16_t*)_handles)
|
||||
, m_numHandles(0)
|
||||
, m_maxHandles(_maxHandles)
|
||||
{
|
||||
for (uint16_t ii = 0; ii < _maxHandles; ++ii)
|
||||
{
|
||||
m_dense[ii] = ii;
|
||||
m_handles[ii] = ii;
|
||||
}
|
||||
}
|
||||
|
||||
~HandleAlloc()
|
||||
{
|
||||
delete [] m_dense;
|
||||
}
|
||||
|
||||
const uint16_t* getHandles() const
|
||||
{
|
||||
return m_dense;
|
||||
return m_handles;
|
||||
}
|
||||
|
||||
uint16_t getHandleAt(uint16_t _at) const
|
||||
{
|
||||
return m_dense[_at];
|
||||
return m_handles[_at];
|
||||
}
|
||||
|
||||
uint16_t getNumHandles() const
|
||||
@@ -59,8 +129,9 @@ namespace bx
|
||||
uint16_t index = m_numHandles;
|
||||
++m_numHandles;
|
||||
|
||||
uint16_t handle = m_dense[index];
|
||||
m_sparse[handle] = index;
|
||||
uint16_t handle = m_handles[index];
|
||||
uint16_t* sparse = &m_handles[m_maxHandles];
|
||||
sparse[handle] = index;
|
||||
return handle;
|
||||
}
|
||||
|
||||
@@ -69,20 +140,33 @@ namespace bx
|
||||
|
||||
void free(uint16_t _handle)
|
||||
{
|
||||
uint16_t index = m_sparse[_handle];
|
||||
uint16_t* sparse = &m_handles[m_maxHandles];
|
||||
uint16_t index = sparse[_handle];
|
||||
--m_numHandles;
|
||||
uint16_t temp = m_dense[m_numHandles];
|
||||
m_dense[m_numHandles] = _handle;
|
||||
m_sparse[temp] = index;
|
||||
m_dense[index] = temp;
|
||||
uint16_t temp = m_handles[m_numHandles];
|
||||
m_handles[m_numHandles] = _handle;
|
||||
sparse[temp] = index;
|
||||
m_handles[index] = temp;
|
||||
}
|
||||
|
||||
private:
|
||||
uint16_t* m_dense;
|
||||
uint16_t* m_sparse;
|
||||
uint16_t* m_handles;
|
||||
uint16_t m_numHandles;
|
||||
uint16_t m_maxHandles;
|
||||
};
|
||||
|
||||
inline HandleAlloc* createHandleAlloc(AllocatorI* _allocator, uint16_t _maxHandles)
|
||||
{
|
||||
uint8_t* ptr = (uint8_t*)BX_ALLOC(_allocator, sizeof(HandleAlloc) + 2*_maxHandles*sizeof(uint16_t) );
|
||||
return ::new (ptr) HandleAlloc(_maxHandles, &ptr[sizeof(HandleAlloc)]);
|
||||
}
|
||||
|
||||
inline void destroyHandleAlloc(AllocatorI* _allocator, HandleAlloc* _handleAlloc)
|
||||
{
|
||||
_handleAlloc->~HandleAlloc();
|
||||
BX_FREE(_allocator, _handleAlloc);
|
||||
}
|
||||
|
||||
} // namespace bx
|
||||
|
||||
#endif // __HANDLE_ALLOC_H__
|
||||
|
||||
@@ -32,6 +32,7 @@
|
||||
|
||||
#if BX_COMPILER_GCC || BX_COMPILER_CLANG
|
||||
# define BX_ALIGN_STRUCT(_align, _struct) _struct __attribute__( (aligned(_align) ) )
|
||||
# define BX_ALIGNOF(_type) alignof(_type)
|
||||
# define BX_ALLOW_UNUSED __attribute__( (unused) )
|
||||
# define BX_FORCE_INLINE __extension__ static __inline __attribute__( (__always_inline__) )
|
||||
# define BX_FUNCTION __PRETTY_FUNCTION__
|
||||
@@ -48,6 +49,7 @@
|
||||
# endif // BX_COMPILER_CLANG
|
||||
#elif BX_COMPILER_MSVC
|
||||
# define BX_ALIGN_STRUCT(_align, _struct) __declspec(align(_align) ) _struct
|
||||
# define BX_ALIGNOF(_type) __alignof(_type)
|
||||
# define BX_ALLOW_UNUSED
|
||||
# define BX_FORCE_INLINE __forceinline
|
||||
# define BX_FUNCTION __FUNCTION__
|
||||
|
||||
Reference in New Issue
Block a user