Skip to content

Commit 98c92c1

Browse files
authored
Merge pull request #4 from kimkulling/issue_3
closes #3:
2 parents 4c9f99d + 6a8a811 commit 98c92c1

File tree

7 files changed

+154
-58
lines changed

7 files changed

+154
-58
lines changed

.travis.sh

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/bin/bash
2+
function generate() {
3+
OPTIONS=""
4+
5+
if [ "$ASAN" = "ON" ] ; then
6+
OPTIONS="$OPTIONS -DCPPCORE_ASAN=ON"
7+
else
8+
OPTIONS="$OPTIONS -DCPPCORE_ASAN=OFF"
9+
fi
10+
11+
if [ "$UBSAN" = "ON" ] ; then
12+
OPTIONS="$OPTIONS -DCPPCORE_UBSAN=ON"
13+
else
14+
OPTIONS="$OPTIONS -DCPPCORE_UBSAN=OFF"
15+
fi
16+
17+
cd build
18+
cmake -G "Unix Makefiles" $OPTIONS
19+
}
20+
21+
if [ "$TRAVIS_OS_NAME" = "linux" ]; then
22+
if [ "$ANALYZE" = "ON" ] ; then
23+
if [ "$CC" = "clang" ]; then
24+
cd build
25+
scan-build cmake -G "Unix Makefiles"
26+
scan-build --status-bugs make -j4
27+
fi
28+
else
29+
generate \
30+
&& make -j4 && cd bin && ./cppcore_unittest
31+
fi
32+
fi

.travis.yml

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ before_install:
44
- sudo apt-get install cmake
55

66
os:
7-
# - windows
87
- linux
98

109
language:
@@ -13,8 +12,26 @@ language:
1312
compiler:
1413
- gcc
1514
- clang
16-
script:
17-
- cd build && cmake -G "Unix Makefiles" && make -j4 && cd bin && ./cppcore_unittest
18-
15+
16+
matrix:
17+
include:
18+
- os: linux
19+
compiler: clang
20+
env: ASAN=ON
21+
- os: linux
22+
compiler: clang
23+
env: ANALYZE=ON
24+
- os: linux
25+
compiler: clang
26+
env: UBSAN=ON
27+
- os: linux
28+
compiler: gcc
29+
- os: linux
30+
compiler: gcc
31+
env: ANALYZE=ON
1932

33+
before_script:
34+
- cd build && cmake -G "Unix Makefiles" && cd ..
2035

36+
script:
37+
- . ./.travis.sh

build/CMakeLists.txt

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,18 @@ if( CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_GNUCXX )
1010
find_package(Threads)
1111
endif()
1212

13-
option( BUILD_UNITTESTS "Build unit tests." ON )
13+
option( BUILD_UNITTESTS
14+
"Build unit tests."
15+
ON
16+
)
17+
option( CPPCORE_ASAN
18+
"Enable AddressSanitizer."
19+
OFF
20+
)
21+
option( CPPCORE_UBSAN
22+
"Enable Undefined Behavior sanitizer."
23+
OFF
24+
)
1425

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

59+
IF (ASSIMP_ASAN)
60+
MESSAGE(STATUS "AddressSanitizer enabled")
61+
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address")
62+
SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address")
63+
ENDIF()
64+
65+
IF (ASSIMP_UBSAN)
66+
MESSAGE(STATUS "Undefined Behavior sanitizer enabled")
67+
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=undefined -fno-sanitize-recover=all")
68+
SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=undefined -fno-sanitize-recover=all")
69+
ENDIF()
70+
4871
SET ( cppcore_src
4972
../code/cppcore.cpp
5073
../include/cppcore/CPPCoreCommon.h

include/cppcore/Memory/TPoolAllocator.h

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -86,26 +86,39 @@ class TPoolAllocator {
8686
CPPCORE_NONE_COPYING(Pool)
8787
};
8888

89+
Pool *getFreePool() {
90+
Pool *current(m_freeList);
91+
if (nullptr != m_freeList) {
92+
m_freeList = m_freeList->m_next;
93+
}
94+
return current;
95+
}
96+
8997
Pool *m_first;
9098
Pool *m_current;
99+
Pool *m_freeList;
91100
size_t m_capacity;
92101
};
93102

94103
template<class T>
95104
inline
96105
TPoolAllocator<T>::TPoolAllocator()
97-
: m_first(nullptr)
98-
, m_current(nullptr)
99-
, m_capacity(0L) {
106+
: m_first( nullptr )
107+
, m_current( nullptr )
108+
, m_freeList( nullptr )
109+
, m_capacity( 0L ) {
100110
// empty
101111
}
102112

103113
template<class T>
104114
inline
105115
TPoolAllocator<T>::TPoolAllocator(size_t numItems)
106-
: m_first(nullptr)
107-
, m_current(nullptr) {
116+
: m_first( nullptr )
117+
, m_current( nullptr )
118+
, m_freeList( nullptr )
119+
, m_capacity(0L) {
108120
m_first = new Pool(numItems);
121+
m_capacity += numItems;
109122
m_current = m_first;
110123
}
111124

@@ -139,7 +152,13 @@ void TPoolAllocator<T>::release() {
139152
return;
140153
}
141154

142-
m_current->m_currentIdx = 0;
155+
Pool *current(m_first);
156+
while(nullptr != current ) {
157+
current->m_currentIdx = 0;
158+
current = current->m_next;
159+
}
160+
m_freeList = m_first->m_next;
161+
m_current = m_first;
143162
}
144163

145164
template<class T>
@@ -170,22 +189,19 @@ void TPoolAllocator<T>::clear() {
170189
delete current;
171190
}
172191
m_current = nullptr;
192+
m_freeList = nullptr;
173193
}
174194

175195
template<class T>
176196
inline
177197
size_t TPoolAllocator<T>::capacity() const {
178-
if (nullptr == m_current) {
179-
return 0L;
180-
}
181-
182-
return m_current->m_poolsize;
198+
return m_capacity;
183199
}
184200

185201
template<class T>
186202
inline
187203
size_t TPoolAllocator<T>::reservedMem() const {
188-
return m_capacity;
204+
return m_capacity * sizeof(T);
189205
}
190206

191207
template<class T>
@@ -219,11 +235,16 @@ void TPoolAllocator<T>::resize(size_t growSize) {
219235
if (nullptr == m_first) {
220236
m_first = new Pool(growSize, nullptr);
221237
m_current = m_first;
238+
m_capacity += m_current->m_poolsize;
222239
} else {
223-
m_current->m_next = new Pool(growSize, nullptr);
240+
Pool *pool = getFreePool();
241+
if (nullptr == pool) {
242+
pool = new Pool(growSize, nullptr);
243+
m_capacity += growSize;
244+
}
245+
m_current->m_next = pool;
224246
m_current = m_current->m_next;
225247
}
226-
m_capacity += m_current->m_poolsize;
227248
}
228249

229250
} // Namespace CPPCore

test/container/TArrayTest.cpp

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
-------------------------------------------------------------------------------------------------
33
The MIT License (MIT)
44
5-
Copyright (c) 2014-2016 Kim Kulling
5+
Copyright (c) 2014-2019 Kim Kulling
66
77
Permission is hereby granted, free of charge, to any person obtaining a copy of
88
this software and associated documentation files (the "Software"), to deal in
@@ -158,29 +158,29 @@ TEST_F( TArrayTest, removeTest) {
158158
TEST_F( TArrayTest, removeItTest) {
159159
TArray<float> arrayInstance;
160160
arrayInstance.add( 1.0f );
161-
EXPECT_EQ( 1, arrayInstance.size() );
161+
EXPECT_EQ( 1u, arrayInstance.size() );
162162
TArray<float>::Iterator it = arrayInstance.find( 1.0f );
163163
EXPECT_NE( arrayInstance.end(), it );
164164

165165
arrayInstance.remove( it );
166-
EXPECT_EQ( 0, arrayInstance.size() );
166+
EXPECT_EQ( 0u, arrayInstance.size() );
167167
}
168168

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

173173
arrayInstance.removeBack();
174-
EXPECT_EQ( 3, arrayInstance.size() );
174+
EXPECT_EQ( 3u, arrayInstance.size() );
175175
EXPECT_EQ( 2.0f, arrayInstance[ 2 ] );
176176
}
177177

178178
TEST_F( TArrayTest, resizeTest ) {
179179
TArray<float> arrayInstance;
180-
EXPECT_EQ( 0, arrayInstance.size() );
180+
EXPECT_EQ( 0u, arrayInstance.size() );
181181

182182
arrayInstance.resize( 5 );
183-
EXPECT_EQ( 5, arrayInstance.size() );
183+
EXPECT_EQ( 5u, arrayInstance.size() );
184184
}
185185

186186
TEST_F( TArrayTest, moveTest ) {
@@ -193,22 +193,23 @@ TEST_F( TArrayTest, moveTest ) {
193193

194194
TEST_F( TArrayTest, reserveTest ) {
195195
TArray<float> arrayInstance;
196-
EXPECT_EQ( 0, arrayInstance.capacity() );
196+
EXPECT_EQ( 0u, arrayInstance.capacity() );
197197

198198
arrayInstance.reserve( 5 );
199-
EXPECT_EQ( 5, arrayInstance.capacity() );
199+
EXPECT_EQ( 5u, arrayInstance.capacity() );
200200

201-
arrayInstance.reserve( 2000 );
202-
EXPECT_EQ( 2000, arrayInstance.capacity() );
201+
static const size_t NewSize = 2000;
202+
arrayInstance.reserve(NewSize);
203+
EXPECT_EQ( NewSize, arrayInstance.capacity() );
203204
}
204205

205206
TEST_F( TArrayTest, resize_with_init_Test ) {
206207
TArray<float> arrayInstance;
207-
EXPECT_EQ( 0, arrayInstance.capacity() );
208+
EXPECT_EQ( 0u, arrayInstance.capacity() );
208209

209210
arrayInstance.resize( 10, 1.0f );
210-
EXPECT_EQ( 10, arrayInstance.size() );
211-
for ( size_t i = 0; i < 10; i++ ) {
211+
EXPECT_EQ( 10u, arrayInstance.size() );
212+
for ( size_t i = 0; i < 10; ++i ) {
212213
EXPECT_FLOAT_EQ( 1.0f, arrayInstance[ i ] );
213214
}
214215
}
@@ -230,7 +231,7 @@ TEST_F( TArrayTest, preIncIterateTest ) {
230231

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

249250
bool ok = true;
250251
try {
251-
size_t i=0;
252+
size_t i(0);
252253
for( TArray<float>::Iterator it = arrayInstance.begin( ); it != arrayInstance.end( ); it++ ) {
253254
float tmp = *it;
254255
EXPECT_EQ( tmp, ArrayData[ i ] );
@@ -266,7 +267,7 @@ TEST_F( TArrayTest, findTest ) {
266267
arrayInstance.add( 1.0f );
267268
arrayInstance.add( 2.0f );
268269
arrayInstance.add( 3.0f );
269-
EXPECT_EQ( 4, arrayInstance.size() );
270+
EXPECT_EQ( 4u, arrayInstance.size() );
270271

271272
TArray<float>::Iterator it = arrayInstance.find( 1.0f );
272273
EXPECT_NE( it, arrayInstance.end() );
@@ -311,4 +312,3 @@ TEST_F( TArrayTest, bug_AddHeapCorruptTest ) {
311312
arrayInstance.add( ( float ) i );
312313
}
313314
}
314-

0 commit comments

Comments
 (0)