diff --git a/config.lib b/config.lib
index 8d88eb4145..fd21c00b9a 100644
--- a/config.lib
+++ b/config.lib
@@ -3109,24 +3109,40 @@ detect_nforenum() {
log 1 "checking nforenum... found"
}
-detect_cputype() {
- if [ -n "$cpu_type" ] && [ "$cpu_type" != "DETECT" ]; then
- log 1 "forcing cpu-type... $cpu_type bits"
- return;
- fi
- echo "#define _SQ64 1" > tmp.64bit.cpp
- echo "#include \"src/stdafx.h\"" >> tmp.64bit.cpp
- echo "assert_compile(sizeof(size_t) == 8);" >> tmp.64bit.cpp
- echo "int main() { return 0; }" >> tmp.64bit.cpp
- execute="$cxx_host $CFLAGS tmp.64bit.cpp -o tmp.64bit -DTESTING 2>&1"
+_detect_cputype_width() {
+ echo "#define _SQ64 1" > $1.cpp
+ echo "#include \"src/stdafx.h\"" >> $1.cpp
+ echo "assert_compile(sizeof(size_t) == $2);" >> $1.cpp
+ echo "int main() { return 0; }" >> $1.cpp
+ execute="$cxx_host $CFLAGS -std=c++11 $1.cpp -o $1 -DTESTING 2>&1"
cpu_type="`eval $execute 2>/dev/null`"
ret=$?
log 2 "executing $execute"
log 2 " returned $cpu_type"
log 2 " exit code $ret"
- if [ "$ret" = "0" ]; then cpu_type="64"; else cpu_type="32"; fi
+ rm -f $1 $1.cpp
+ return $ret
+}
+
+detect_cputype() {
+ if [ -n "$cpu_type" ] && [ "$cpu_type" != "DETECT" ]; then
+ log 1 "forcing cpu-type... $cpu_type bits"
+ return;
+ fi
+ _detect_cputype_width tmp.32bit 4
+ result32=$?
+ _detect_cputype_width tmp.64bit 8
+ result64=$?
+
+ if [ "$result32" = 0 ] && [ "$result64" != 0 ]; then
+ cpu_type="32"
+ elif [ "$result32" != 0 ] && [ "$result64" = 0 ]; then
+ cpu_type="64"
+ else
+ log 1 "configure: unable to determine cpu-type (pointer width)"
+ exit 1
+ fi
log 1 "detecting cpu-type... $cpu_type bits"
- rm -f tmp.64bit tmp.64bit.cpp
}
detect_sse_capable_architecture() {
diff --git a/projects/openttd_vs100.vcxproj b/projects/openttd_vs100.vcxproj
index f01954ab5e..2e97986270 100644
--- a/projects/openttd_vs100.vcxproj
+++ b/projects/openttd_vs100.vcxproj
@@ -679,6 +679,8 @@
+
+
diff --git a/projects/openttd_vs100.vcxproj.filters b/projects/openttd_vs100.vcxproj.filters
index 16848eb7b8..37d40d30e8 100644
--- a/projects/openttd_vs100.vcxproj.filters
+++ b/projects/openttd_vs100.vcxproj.filters
@@ -1230,6 +1230,12 @@
Core Source Code
+
+ Core Source Code
+
+
+ Core Source Code
+
Core Source Code
diff --git a/projects/openttd_vs140.vcxproj b/projects/openttd_vs140.vcxproj
index 03b88451fa..f09a83712d 100644
--- a/projects/openttd_vs140.vcxproj
+++ b/projects/openttd_vs140.vcxproj
@@ -696,6 +696,8 @@
+
+
diff --git a/projects/openttd_vs140.vcxproj.filters b/projects/openttd_vs140.vcxproj.filters
index 16848eb7b8..37d40d30e8 100644
--- a/projects/openttd_vs140.vcxproj.filters
+++ b/projects/openttd_vs140.vcxproj.filters
@@ -1230,6 +1230,12 @@
Core Source Code
+
+ Core Source Code
+
+
+ Core Source Code
+
Core Source Code
diff --git a/projects/openttd_vs80.vcproj b/projects/openttd_vs80.vcproj
index 9f4e2bbf3e..634b37f0ce 100644
--- a/projects/openttd_vs80.vcproj
+++ b/projects/openttd_vs80.vcproj
@@ -1946,6 +1946,14 @@
RelativePath=".\..\src\core\bitmath_func.hpp"
>
+
+
+
+
diff --git a/projects/openttd_vs90.vcproj b/projects/openttd_vs90.vcproj
index 930522942c..49a07b5cd6 100644
--- a/projects/openttd_vs90.vcproj
+++ b/projects/openttd_vs90.vcproj
@@ -1943,6 +1943,14 @@
RelativePath=".\..\src\core\bitmath_func.hpp"
>
+
+
+
+
diff --git a/source.list b/source.list
index 2b177a0889..26a7a0aa24 100644
--- a/source.list
+++ b/source.list
@@ -424,6 +424,7 @@ core/backup_type.hpp
core/bitmath_func.cpp
core/bitmath_func.hpp
core/container_func.hpp
+core/dyn_arena_alloc.hpp
core/endian_func.hpp
core/endian_type.hpp
core/enum_type.hpp
diff --git a/src/core/container_func.hpp b/src/core/container_func.hpp
index 1b8e8ad847..276c1f7f54 100644
--- a/src/core/container_func.hpp
+++ b/src/core/container_func.hpp
@@ -9,6 +9,11 @@
/** @file container_func.hpp Functions related to use of containers. */
+#ifndef CONTAINER_FUNC_HPP
+#define CONTAINER_FUNC_HPP
+
+#include
+
template unsigned int container_unordered_remove_if (C &container, UP predicate) {
unsigned int removecount = 0;
for (auto it = container.begin(); it != container.end();) {
@@ -33,3 +38,5 @@ template unsigned int container_unordered_remove(C &con
return v == value;
});
}
+
+#endif /* CONTAINER_FUNC_HPP */
diff --git a/src/core/dyn_arena_alloc.hpp b/src/core/dyn_arena_alloc.hpp
new file mode 100644
index 0000000000..2a074d6aa1
--- /dev/null
+++ b/src/core/dyn_arena_alloc.hpp
@@ -0,0 +1,102 @@
+/* $Id$ */
+
+/*
+ * This file is part of OpenTTD.
+ * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
+ * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+ * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see .
+ */
+
+/** @file dyn_arena_alloc.hpp Dynamic chunk-size arena allocator. */
+
+#ifndef DYN_ARENA_ALLOC_HPP
+#define DYN_ARENA_ALLOC_HPP
+
+#include
+
+/**
+ * Custom arena allocator for uniform-size allocations of a variable size.
+ * The allocation and chunk sizes may only be changed when the arena is empty.
+ */
+class DynUniformArenaAllocator {
+ std::vector used_blocks;
+
+ void *current_block = nullptr;
+ void *last_freed = nullptr;
+ size_t next_position = 0;
+
+ size_t item_size = 0;
+ size_t items_per_chunk = 0;
+
+ void NewBlock()
+ {
+ current_block = malloc(item_size * items_per_chunk);
+ assert(current_block != nullptr);
+ next_position = 0;
+ used_blocks.push_back(current_block);
+ }
+
+ public:
+ DynUniformArenaAllocator() = default;
+ DynUniformArenaAllocator(const DynUniformArenaAllocator &other) = delete;
+ DynUniformArenaAllocator& operator=(const DynUniformArenaAllocator &other) = delete;
+
+ ~DynUniformArenaAllocator()
+ {
+ EmptyArena();
+ }
+
+ void EmptyArena()
+ {
+ current_block = nullptr;
+ last_freed = nullptr;
+ next_position = 0;
+ for (void *block : used_blocks) {
+ free(block);
+ }
+ used_blocks.clear();
+ }
+
+ void ResetArena()
+ {
+ EmptyArena();
+ item_size = 0;
+ items_per_chunk = 0;
+ }
+
+ void *Allocate() {
+ assert(item_size != 0);
+ if (last_freed) {
+ void *ptr = last_freed;
+ last_freed = *reinterpret_cast(ptr);
+ return ptr;
+ } else {
+ if (current_block == nullptr || next_position == items_per_chunk) {
+ NewBlock();
+ }
+ void *out = reinterpret_cast(current_block) + (item_size * next_position);
+ next_position++;
+ return out;
+ }
+ }
+
+ void Free(void *ptr) {
+ if (!ptr) return;
+ assert(current_block != nullptr);
+
+ *reinterpret_cast(ptr) = last_freed;
+ last_freed = ptr;
+ }
+
+ void SetParameters(size_t item_size, size_t items_per_chunk)
+ {
+ if (item_size < sizeof(void *)) item_size = sizeof(void *);
+ if (this->item_size == item_size && this->items_per_chunk == items_per_chunk) return;
+
+ assert(current_block == nullptr);
+ this->item_size = item_size;
+ this->items_per_chunk = items_per_chunk;
+ }
+};
+
+#endif /* DYN_ARENA_ALLOC_HPP */
diff --git a/src/core/pool_func.hpp b/src/core/pool_func.hpp
index 5569addbd7..a5375e13fc 100644
--- a/src/core/pool_func.hpp
+++ b/src/core/pool_func.hpp
@@ -196,6 +196,7 @@ DEFINE_POOL_METHOD(void)::FreeItem(size_t index)
DEFINE_POOL_METHOD(void)::CleanPool()
{
this->cleaning = true;
+ Titem::PreCleanPool();
for (size_t i = 0; i < this->first_unused; i++) {
delete this->Get(i); // 'delete NULL;' is very valid
}
diff --git a/src/core/pool_type.hpp b/src/core/pool_type.hpp
index 4d20ed1abb..70f6480103 100644
--- a/src/core/pool_type.hpp
+++ b/src/core/pool_type.hpp
@@ -286,6 +286,13 @@ struct Pool : PoolBase {
* @note it's called only when !CleaningPool()
*/
static inline void PostDestructor(size_t index) { }
+
+ /**
+ * Dummy function called before a pool is about to be cleaned.
+ * If you want to use it, override it in PoolItem's subclass.
+ * @note it's called only when CleaningPool()
+ */
+ static inline void PreCleanPool() { }
};
private: