Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ option( BUILD_UNITTESTS "Build unit tests." ON )

add_definitions( -DCPPCORE_BUILD )
add_definitions( -D_VARIADIC_MAX=10 )
add_definitions( -D_SILENCE_TR1_NAMESPACE_DEPRECATION_WARNING=1)

INCLUDE_DIRECTORIES( BEFORE
${CMAKE_HOME_DIRECTORY}/..
Expand Down
8 changes: 6 additions & 2 deletions include/cppcore/CPPCoreCommon.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*-----------------------------------------------------------------------------------------------
The MIT License (MIT)

Copyright (c) 2014-2016 Kim Kulling
Copyright (c) 2014-2019 Kim Kulling

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
Expand All @@ -24,6 +24,9 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

#include <cstddef>
#include <string.h>
#include <stddef.h>
#include <stdio.h>
#include <stdarg.h>

namespace CPPCore {

Expand Down Expand Up @@ -81,8 +84,9 @@ void ContainerClear( T & ctr, void( *DeleterFunc )( T & ) = nullptr ) {
/// @param name [in] The name of the class.
//-------------------------------------------------------------------------------------------------
#define CPPCORE_NONE_COPYING( name ) \
private:\
public: \
name( const name & ) = delete ;\
name( name && ) = delete; \
name &operator = ( const name & ) = delete;

//-------------------------------------------------------------------------------------------------
Expand Down
7 changes: 6 additions & 1 deletion include/cppcore/Common/Variant.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*-----------------------------------------------------------------------------------------------
The MIT License (MIT)

Copyright (c) 2014-2017 Kim Kulling
Copyright (c) 2014-2019 Kim Kulling

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
Expand Down Expand Up @@ -87,7 +87,12 @@ class Variant {
/// @return TYpe enum of the current dynamic type of the instance.
Type getType() const;

/// @brief Will set the payload to the given value.
/// @param value [in] The new given payload.
void setByte(unsigned char value);

/// @brief Will return the current payload as a byte value.
/// @return The byte value.
unsigned char getByte() const;

/// @brief Sets a new integer value, old values will be released and destroyed.
Expand Down
156 changes: 117 additions & 39 deletions include/cppcore/Memory/TPoolAllocator.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*-----------------------------------------------------------------------------------------------
The MIT License (MIT)

Copyright (c) 2014-2016 Kim Kulling
Copyright (c) 2014-2019 Kim Kulling

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
Expand All @@ -22,23 +22,22 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-----------------------------------------------------------------------------------------------*/
#pragma once

#include <stddef.h>
#include <cppcore/CPPCoreCommon.h>
#include <cppcore/Common/CString.h>
#include <stdio.h>

namespace CPPCore {

//-------------------------------------------------------------------------------------------------
/// @class TPoolAllocator
/// @ingroup CPPCore
///
/// @brief This class implements a simple poll-based allocation scheme.
/// Initially you have to define its size. Each allocation will be done from this initially created
/// pool. You have to release all pooled instances after the usage.
/// This allocation scheme is fast and does no call any new-calls during the lifetime of the
/// allocator.
//-------------------------------------------------------------------------------------------------
template<class T>
//-------------------------------------------------------------------------------------------------
/// @class TPoolAllocator
/// @ingroup CPPCore
///
/// @brief This class implements a simple poll-based allocation scheme.
/// Initially you have to define its size. Each allocation will be done from this initially created
/// pool. You have to release all pooled instances after the usage.
/// This allocation scheme is fast and does no call any new-calls during the lifetime of the
/// allocator.
//-------------------------------------------------------------------------------------------------
template<class T>
class TPoolAllocator {
public:
TPoolAllocator();
Expand All @@ -52,29 +51,62 @@ class TPoolAllocator {
size_t reservedMem() const;
size_t freeMem() const;
void dumpAllocations(CString & allocs);
void resize(size_t growSize );

CPPCORE_NONE_COPYING(TPoolAllocator)

private:
size_t m_poolsize;
T *m_pool;
size_t m_currentIdx;
struct Pool {
size_t m_poolsize;
T *m_pool;
size_t m_currentIdx;
Pool *m_next;

Pool()
: m_poolsize(0u)
, m_pool(nullptr)
, m_currentIdx(0u)
, m_next(nullptr) {
// empty
}

Pool(size_t numItems, Pool *prev)
: m_poolsize(numItems)
, m_pool(nullptr)
, m_currentIdx(0u)
, m_next(prev) {
m_pool = new T[m_poolsize];
}

~Pool() {
delete[] m_pool;
m_pool = nullptr;
}

CPPCORE_NONE_COPYING(Pool)
};

Pool *m_first;
Pool *m_current;
size_t m_capacity;
};

template<class T>
inline
TPoolAllocator<T>::TPoolAllocator()
: m_poolsize(0)
, m_pool(nullptr)
, m_currentIdx(0) {
: m_first(nullptr)
, m_current(nullptr)
, m_capacity(0L) {
// empty
}

template<class T>
inline
TPoolAllocator<T>::TPoolAllocator(size_t numItems)
: m_poolsize(numItems)
, m_pool(nullptr)
, m_currentIdx( 0 ) {
m_pool = new T[m_poolsize];
: m_first(nullptr)
, m_current(nullptr) {
m_first = new Pool(numItems);
m_current = m_first;
}

template<class T>
Expand All @@ -86,66 +118,112 @@ TPoolAllocator<T>::~TPoolAllocator() {
template<class T>
inline
T *TPoolAllocator<T>::alloc() {
if ( m_currentIdx == m_poolsize ) {
if (nullptr == m_current) {
return nullptr;
}

if (m_current->m_currentIdx == m_current->m_poolsize) {
resize(m_current->m_poolsize);
}

T *ptr(&m_current->m_pool[m_current->m_currentIdx]);
m_current->m_currentIdx++;

++m_currentIdx;
return &m_pool[m_currentIdx-1];
return ptr;
}

template<class T>
inline
void TPoolAllocator<T>::release() {
m_currentIdx = 0;
if (nullptr == m_current) {
return;
}

m_current->m_currentIdx = 0;
}

template<class T>
inline
void TPoolAllocator<T>::reserve(size_t size) {
clear();
m_pool = new T[ size ];
m_poolsize = size;

m_first = new Pool(size, nullptr);
m_current = m_first;

m_current->m_pool = new T[size];
m_current->m_poolsize = size;

m_capacity = size;
}

template<class T>
inline
void TPoolAllocator<T>::clear() {
if (nullptr == m_pool) {
if (nullptr == m_current) {
return;
}

delete [] m_pool;
m_pool = nullptr;
m_currentIdx = 0;
m_poolsize = 0;
Pool *next(m_first);
while ( nullptr != next) {
Pool *current = next;
next = current->m_next;
delete current;
}
m_current = nullptr;
}

template<class T>
inline
size_t TPoolAllocator<T>::capacity() const {
return m_poolsize;
if (nullptr == m_current) {
return 0L;
}

return m_current->m_poolsize;
}

template<class T>
inline
size_t TPoolAllocator<T>::reservedMem() const {
return m_poolsize * sizeof(T);
return m_capacity;
}

template<class T>
inline
size_t TPoolAllocator<T>::freeMem() const {
return m_poolsize - m_currentIdx;
if (nullptr == m_current) {
return 0L;
}

return (m_current->m_poolsize - m_current->m_currentIdx);
}

template<class T>
inline
void TPoolAllocator<T>::dumpAllocations(CString & allocs) {
allocs.clear();
char buffer[512];
sprintf(buffer, "Number allocations = %d\n", (int)m_currentIdx);
::sprintf(buffer, "Number allocations = %zu\n", m_current->m_currentIdx);
allocs.set(buffer);
}

template<class T>
inline
void TPoolAllocator<T>::resize(size_t growSize) {
if (nullptr != m_current) {
if (growSize < m_current->m_poolsize) {
return;
}
}

if (nullptr == m_first) {
m_first = new Pool(growSize, nullptr);
m_current = m_first;
} else {
m_current->m_next = new Pool(growSize, nullptr);
m_current = m_current->m_next;
}
m_capacity += m_current->m_poolsize;
}

} // Namespace CPPCore
13 changes: 6 additions & 7 deletions include/cppcore/Memory/TStackAllocator.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*-----------------------------------------------------------------------------------------------
The MIT License (MIT)

Copyright (c) 2014-2016 Kim Kulling
Copyright (c) 2014-2019 Kim Kulling

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
Expand All @@ -22,9 +22,8 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-----------------------------------------------------------------------------------------------*/
#pragma once

#include <stddef.h>
#include <cppcore/CPPCoreCommon.h>
#include <cppcore/Common/CString.h>
#include <stdio.h>

namespace CPPCore {

Expand Down Expand Up @@ -52,11 +51,11 @@ class TStackAllocator {
size_t reservedMem() const;
size_t freeMem() const;
void dumpAllocations( CString & allocs );
TStackAllocator( const TStackAllocator<T> & ) = delete;
TStackAllocator<T> &operator = ( const TStackAllocator<T> & ) = delete;

CPPCORE_NONE_COPYING(TStackAllocator)

private:
typedef unsigned char byte_t;
using byte_t = unsigned char;
struct Header {
size_t m_size;
};
Expand Down Expand Up @@ -171,7 +170,7 @@ inline
void TStackAllocator<T>::dumpAllocations( CString & allocs ) {
allocs.clear();
char buffer[ 512 ];
sprintf( buffer, "Number allocations = %d\n", ( int ) m_numAllocs );
sprintf( buffer, "Number allocations = %zu\n", m_numAllocs );
allocs.set( buffer );
}

Expand Down
8 changes: 4 additions & 4 deletions test/container/TArrayTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,15 @@ TEST_F( TArrayTest, constructTest ) {

TEST_F( TArrayTest, constructWithSizeTest) {
TArray<float> arrayInstance( 4 );
EXPECT_EQ( 4, arrayInstance.size() );
EXPECT_EQ( 4u, arrayInstance.size() );
}

TEST_F( TArrayTest, addTest) {
TArray<float> arrayInstance;
arrayInstance.add( 0.0f );
arrayInstance.add( 1.0f );

EXPECT_EQ( 2, arrayInstance.size() );
EXPECT_EQ( 2u, arrayInstance.size() );
EXPECT_EQ( 0.0f, arrayInstance[ 0 ] );
EXPECT_EQ( 1.0f, arrayInstance[ 1 ] );
}
Expand All @@ -84,13 +84,13 @@ TEST_F( TArrayTest, addItemsTest ) {

float data[2] = { 0, 1 };
arrayInstance.add( data, 2 );
EXPECT_EQ( 3, arrayInstance.size() );
EXPECT_EQ( 3u, arrayInstance.size() );
EXPECT_EQ( 0.0f, arrayInstance[ 0 ] );
EXPECT_EQ( 0.0f, arrayInstance[ 1 ] );
EXPECT_EQ( 1.0f, arrayInstance[ 2 ] );

arrayInstance.add( nullptr, 0 );
EXPECT_EQ( 3, arrayInstance.size() );
EXPECT_EQ( 3u, arrayInstance.size() );
}

TEST_F( TArrayTest, accessTest) {
Expand Down
Loading