Skip to content

closes https://github.com/kimkulling/cppcore/issues/3: #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Aug 9, 2019
Merged
32 changes: 32 additions & 0 deletions .travis.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/bin/bash
function generate() {
OPTIONS=""

if [ "$ASAN" = "ON" ] ; then
OPTIONS="$OPTIONS -DCPPCORE_ASAN=ON"
else
OPTIONS="$OPTIONS -DCPPCORE_ASAN=OFF"
fi

if [ "$UBSAN" = "ON" ] ; then
OPTIONS="$OPTIONS -DCPPCORE_UBSAN=ON"
else
OPTIONS="$OPTIONS -DCPPCORE_UBSAN=OFF"
fi

cd build
cmake -G "Unix Makefiles" $OPTIONS
}

if [ "$TRAVIS_OS_NAME" = "linux" ]; then
if [ "$ANALYZE" = "ON" ] ; then
if [ "$CC" = "clang" ]; then
cd build
scan-build cmake -G "Unix Makefiles"
scan-build --status-bugs make -j4
fi
else
generate \
&& make -j4 && cd bin && ./cppcore_unittest
fi
fi
25 changes: 21 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ before_install:
- sudo apt-get install cmake

os:
# - windows
- linux

language:
Expand All @@ -13,8 +12,26 @@ language:
compiler:
- gcc
- clang
script:
- cd build && cmake -G "Unix Makefiles" && make -j4 && cd bin && ./cppcore_unittest


matrix:
include:
- os: linux
compiler: clang
env: ASAN=ON
- os: linux
compiler: clang
env: ANALYZE=ON
- os: linux
compiler: clang
env: UBSAN=ON
- os: linux
compiler: gcc
- os: linux
compiler: gcc
env: ANALYZE=ON

before_script:
- cd build && cmake -G "Unix Makefiles" && cd ..

script:
- . ./.travis.sh
25 changes: 24 additions & 1 deletion build/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,18 @@ if( CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_GNUCXX )
find_package(Threads)
endif()

option( BUILD_UNITTESTS "Build unit tests." ON )
option( BUILD_UNITTESTS
"Build unit tests."
ON
)
option( CPPCORE_ASAN
"Enable AddressSanitizer."
OFF
)
option( CPPCORE_UBSAN
"Enable Undefined Behavior sanitizer."
OFF
)

add_definitions( -DCPPCORE_BUILD )
add_definitions( -D_VARIADIC_MAX=10 )
Expand Down Expand Up @@ -45,6 +56,18 @@ elseif ( "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang" )
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wno-long-long -pedantic -std=c++11")
endif()

IF (ASSIMP_ASAN)
MESSAGE(STATUS "AddressSanitizer enabled")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address")
SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address")
ENDIF()

IF (ASSIMP_UBSAN)
MESSAGE(STATUS "Undefined Behavior sanitizer enabled")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=undefined -fno-sanitize-recover=all")
SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=undefined -fno-sanitize-recover=all")
ENDIF()

SET ( cppcore_src
../code/cppcore.cpp
../include/cppcore/CPPCoreCommon.h
Expand Down
49 changes: 35 additions & 14 deletions include/cppcore/Memory/TPoolAllocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,26 +86,39 @@ class TPoolAllocator {
CPPCORE_NONE_COPYING(Pool)
};

Pool *getFreePool() {
Pool *current(m_freeList);
if (nullptr != m_freeList) {
m_freeList = m_freeList->m_next;
}
return current;
}

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

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

template<class T>
inline
TPoolAllocator<T>::TPoolAllocator(size_t numItems)
: m_first(nullptr)
, m_current(nullptr) {
: m_first( nullptr )
, m_current( nullptr )
, m_freeList( nullptr )
, m_capacity(0L) {
m_first = new Pool(numItems);
m_capacity += numItems;
m_current = m_first;
}

Expand Down Expand Up @@ -139,7 +152,13 @@ void TPoolAllocator<T>::release() {
return;
}

m_current->m_currentIdx = 0;
Pool *current(m_first);
while(nullptr != current ) {
current->m_currentIdx = 0;
current = current->m_next;
}
m_freeList = m_first->m_next;
m_current = m_first;
}

template<class T>
Expand Down Expand Up @@ -170,22 +189,19 @@ void TPoolAllocator<T>::clear() {
delete current;
}
m_current = nullptr;
m_freeList = nullptr;
}

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

return m_current->m_poolsize;
return m_capacity;
}

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

template<class T>
Expand Down Expand Up @@ -219,11 +235,16 @@ void TPoolAllocator<T>::resize(size_t growSize) {
if (nullptr == m_first) {
m_first = new Pool(growSize, nullptr);
m_current = m_first;
m_capacity += m_current->m_poolsize;
} else {
m_current->m_next = new Pool(growSize, nullptr);
Pool *pool = getFreePool();
if (nullptr == pool) {
pool = new Pool(growSize, nullptr);
m_capacity += growSize;
}
m_current->m_next = pool;
m_current = m_current->m_next;
}
m_capacity += m_current->m_poolsize;
}

} // Namespace CPPCore
34 changes: 17 additions & 17 deletions test/container/TArrayTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,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 Down Expand Up @@ -158,29 +158,29 @@ TEST_F( TArrayTest, removeTest) {
TEST_F( TArrayTest, removeItTest) {
TArray<float> arrayInstance;
arrayInstance.add( 1.0f );
EXPECT_EQ( 1, arrayInstance.size() );
EXPECT_EQ( 1u, arrayInstance.size() );
TArray<float>::Iterator it = arrayInstance.find( 1.0f );
EXPECT_NE( arrayInstance.end(), it );

arrayInstance.remove( it );
EXPECT_EQ( 0, arrayInstance.size() );
EXPECT_EQ( 0u, arrayInstance.size() );
}

TEST_F( TArrayTest, removeBackTest) {
TArray<float> arrayInstance;
createArray( ArrayData, ArraySize, arrayInstance );

arrayInstance.removeBack();
EXPECT_EQ( 3, arrayInstance.size() );
EXPECT_EQ( 3u, arrayInstance.size() );
EXPECT_EQ( 2.0f, arrayInstance[ 2 ] );
}

TEST_F( TArrayTest, resizeTest ) {
TArray<float> arrayInstance;
EXPECT_EQ( 0, arrayInstance.size() );
EXPECT_EQ( 0u, arrayInstance.size() );

arrayInstance.resize( 5 );
EXPECT_EQ( 5, arrayInstance.size() );
EXPECT_EQ( 5u, arrayInstance.size() );
}

TEST_F( TArrayTest, moveTest ) {
Expand All @@ -193,22 +193,23 @@ TEST_F( TArrayTest, moveTest ) {

TEST_F( TArrayTest, reserveTest ) {
TArray<float> arrayInstance;
EXPECT_EQ( 0, arrayInstance.capacity() );
EXPECT_EQ( 0u, arrayInstance.capacity() );

arrayInstance.reserve( 5 );
EXPECT_EQ( 5, arrayInstance.capacity() );
EXPECT_EQ( 5u, arrayInstance.capacity() );

arrayInstance.reserve( 2000 );
EXPECT_EQ( 2000, arrayInstance.capacity() );
static const size_t NewSize = 2000;
arrayInstance.reserve(NewSize);
EXPECT_EQ( NewSize, arrayInstance.capacity() );
}

TEST_F( TArrayTest, resize_with_init_Test ) {
TArray<float> arrayInstance;
EXPECT_EQ( 0, arrayInstance.capacity() );
EXPECT_EQ( 0u, arrayInstance.capacity() );

arrayInstance.resize( 10, 1.0f );
EXPECT_EQ( 10, arrayInstance.size() );
for ( size_t i = 0; i < 10; i++ ) {
EXPECT_EQ( 10u, arrayInstance.size() );
for ( size_t i = 0; i < 10; ++i ) {
EXPECT_FLOAT_EQ( 1.0f, arrayInstance[ i ] );
}
}
Expand All @@ -230,7 +231,7 @@ TEST_F( TArrayTest, preIncIterateTest ) {

bool ok = true;
try {
size_t i=0;
size_t i( 0 );
for( TArray<float>::Iterator it = arrayInstance.begin( ); it != arrayInstance.end( ); ++it ) {
float tmp = *it;
EXPECT_EQ( tmp, ArrayData[ i ] );
Expand All @@ -248,7 +249,7 @@ TEST_F( TArrayTest, postIncIterateTest ) {

bool ok = true;
try {
size_t i=0;
size_t i(0);
for( TArray<float>::Iterator it = arrayInstance.begin( ); it != arrayInstance.end( ); it++ ) {
float tmp = *it;
EXPECT_EQ( tmp, ArrayData[ i ] );
Expand All @@ -266,7 +267,7 @@ TEST_F( TArrayTest, findTest ) {
arrayInstance.add( 1.0f );
arrayInstance.add( 2.0f );
arrayInstance.add( 3.0f );
EXPECT_EQ( 4, arrayInstance.size() );
EXPECT_EQ( 4u, arrayInstance.size() );

TArray<float>::Iterator it = arrayInstance.find( 1.0f );
EXPECT_NE( it, arrayInstance.end() );
Expand Down Expand Up @@ -311,4 +312,3 @@ TEST_F( TArrayTest, bug_AddHeapCorruptTest ) {
arrayInstance.add( ( float ) i );
}
}

Loading