Merge branch 'cpp-11' into crashlog_improvements

# Conflicts:
#	config.lib
This commit is contained in:
Jonathan G Rennison
2017-09-04 01:22:54 +01:00
301 changed files with 6351 additions and 4178 deletions

View File

@@ -0,0 +1,42 @@
/* $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 <http://www.gnu.org/licenses/>.
*/
/** @file container_func.hpp Functions related to use of containers. */
#ifndef CONTAINER_FUNC_HPP
#define CONTAINER_FUNC_HPP
#include <iterator>
template <typename C, typename UP> unsigned int container_unordered_remove_if (C &container, UP predicate) {
unsigned int removecount = 0;
for (auto it = container.begin(); it != container.end();) {
if (predicate(*it)) {
removecount++;
if (std::next(it) != container.end()) {
*it = std::move(container.back());
container.pop_back();
} else {
container.pop_back();
break;
}
} else {
++it;
}
}
return removecount;
}
template <typename C, typename V> unsigned int container_unordered_remove(C &container, const V &value) {
return container_unordered_remove_if (container, [&](const typename C::value_type &v) {
return v == value;
});
}
#endif /* CONTAINER_FUNC_HPP */

View File

@@ -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 <http://www.gnu.org/licenses/>.
*/
/** @file dyn_arena_alloc.hpp Dynamic chunk-size arena allocator. */
#ifndef DYN_ARENA_ALLOC_HPP
#define DYN_ARENA_ALLOC_HPP
#include <vector>
/**
* 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<void *> 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<void**>(ptr);
return ptr;
} else {
if (current_block == nullptr || next_position == items_per_chunk) {
NewBlock();
}
void *out = reinterpret_cast<char *>(current_block) + (item_size * next_position);
next_position++;
return out;
}
}
void Free(void *ptr) {
if (!ptr) return;
assert(current_block != nullptr);
*reinterpret_cast<void**>(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 */

View File

@@ -15,7 +15,7 @@
#include <map>
#include <list>
template<typename Tkey, typename Tvalue, typename Tcompare>
template<typename Tkey, typename Tvalue, typename Tcontainer, typename Tcompare>
class MultiMap;
/**
@@ -23,14 +23,15 @@ class MultiMap;
* @tparam Tmap_iter Iterator type for the map in the MultiMap.
* @tparam Tlist_iter Iterator type for the lists in the MultiMap.
* @tparam Tkey Key type of the MultiMap.
* @tparam Tvalue Value type of the MultMap.
* @tparam Tvalue Value type of the MultiMap.
* @tparam Tcontainer Container type for the values of the MultiMap.
* @tparam Tcompare Comparator type for keys of the MultiMap.
*/
template<class Tmap_iter, class Tlist_iter, class Tkey, class Tvalue, class Tcompare>
template<class Tmap_iter, class Tlist_iter, class Tkey, class Tvalue, class Tcontainer, class Tcompare>
class MultiMapIterator {
protected:
friend class MultiMap<Tkey, Tvalue, Tcompare>;
typedef MultiMapIterator<Tmap_iter, Tlist_iter, Tkey, Tvalue, Tcompare> Self;
friend class MultiMap<Tkey, Tvalue, Tcontainer, Tcompare>;
typedef MultiMapIterator<Tmap_iter, Tlist_iter, Tkey, Tvalue, Tcontainer, Tcompare> Self;
Tlist_iter list_iter; ///< Iterator pointing to current position in the current list of items with equal keys.
Tmap_iter map_iter; ///< Iterator pointing to the position of the current list of items with equal keys in the map.
@@ -201,8 +202,8 @@ public:
* @param iter2 Second iterator to compare.
* @return If iter1 and iter2 are equal.
*/
template<class Tmap_iter1, class Tlist_iter1, class Tmap_iter2, class Tlist_iter2, class Tkey, class Tvalue1, class Tvalue2, class Tcompare>
bool operator==(const MultiMapIterator<Tmap_iter1, Tlist_iter1, Tkey, Tvalue1, Tcompare> &iter1, const MultiMapIterator<Tmap_iter2, Tlist_iter2, Tkey, Tvalue2, Tcompare> &iter2)
template<class Tmap_iter1, class Tlist_iter1, class Tmap_iter2, class Tlist_iter2, class Tkey, class Tvalue1, class Tvalue2, class Tcontainer1, class Tcontainer2, class Tcompare>
bool operator==(const MultiMapIterator<Tmap_iter1, Tlist_iter1, Tkey, Tvalue1, Tcontainer1, Tcompare> &iter1, const MultiMapIterator<Tmap_iter2, Tlist_iter2, Tkey, Tvalue2, Tcontainer2, Tcompare> &iter2)
{
if (iter1.GetMapIter() != iter2.GetMapIter()) return false;
if (!iter1.ListValid()) return !iter2.ListValid();
@@ -218,8 +219,8 @@ bool operator==(const MultiMapIterator<Tmap_iter1, Tlist_iter1, Tkey, Tvalue1, T
* @param iter2 Second iterator to compare.
* @return If iter1 and iter2 are not equal.
*/
template<class Tmap_iter1, class Tlist_iter1, class Tmap_iter2, class Tlist_iter2, class Tkey, class Tvalue1, class Tvalue2, class Tcompare>
bool operator!=(const MultiMapIterator<Tmap_iter1, Tlist_iter1, Tkey, Tvalue1, Tcompare> &iter1, const MultiMapIterator<Tmap_iter2, Tlist_iter2, Tkey, Tvalue2, Tcompare> &iter2)
template<class Tmap_iter1, class Tlist_iter1, class Tmap_iter2, class Tlist_iter2, class Tkey, class Tvalue1, class Tvalue2, class Tcontainer1, class Tcontainer2, class Tcompare>
bool operator!=(const MultiMapIterator<Tmap_iter1, Tlist_iter1, Tkey, Tvalue1, Tcontainer1, Tcompare> &iter1, const MultiMapIterator<Tmap_iter2, Tlist_iter2, Tkey, Tvalue2, Tcontainer2, Tcompare> &iter2)
{
return !(iter1 == iter2);
}
@@ -232,8 +233,8 @@ bool operator!=(const MultiMapIterator<Tmap_iter1, Tlist_iter1, Tkey, Tvalue1, T
* @param iter2 Map iterator.
* @return If iter1 points to the begin of the list pointed to by iter2.
*/
template<class Tmap_iter1, class Tlist_iter1, class Tmap_iter2, class Tkey, class Tvalue, class Tcompare >
bool operator==(const MultiMapIterator<Tmap_iter1, Tlist_iter1, Tkey, Tvalue, Tcompare> &iter1, const Tmap_iter2 &iter2)
template<class Tmap_iter1, class Tlist_iter1, class Tmap_iter2, class Tkey, class Tvalue, class Tcontainer, class Tcompare >
bool operator==(const MultiMapIterator<Tmap_iter1, Tlist_iter1, Tkey, Tvalue, Tcontainer, Tcompare> &iter1, const Tmap_iter2 &iter2)
{
return !iter1.ListValid() && iter1.GetMapIter() == iter2;
}
@@ -244,8 +245,8 @@ bool operator==(const MultiMapIterator<Tmap_iter1, Tlist_iter1, Tkey, Tvalue, Tc
* @param iter2 Map iterator.
* @return If iter1 doesn't point to the begin of the list pointed to by iter2.
*/
template<class Tmap_iter1, class Tlist_iter1, class Tmap_iter2, class Tkey, class Tvalue, class Tcompare >
bool operator!=(const MultiMapIterator<Tmap_iter1, Tlist_iter1, Tkey, Tvalue, Tcompare> &iter1, const Tmap_iter2 &iter2)
template<class Tmap_iter1, class Tlist_iter1, class Tmap_iter2, class Tkey, class Tvalue, class Tcontainer, class Tcompare >
bool operator!=(const MultiMapIterator<Tmap_iter1, Tlist_iter1, Tkey, Tvalue, Tcontainer, Tcompare> &iter1, const Tmap_iter2 &iter2)
{
return iter1.ListValid() || iter1.GetMapIter() != iter2;
}
@@ -256,8 +257,8 @@ bool operator!=(const MultiMapIterator<Tmap_iter1, Tlist_iter1, Tkey, Tvalue, Tc
* @param iter1 MultiMap iterator.
* @return If iter1 points to the begin of the list pointed to by iter2.
*/
template<class Tmap_iter1, class Tlist_iter1, class Tmap_iter2, class Tkey, class Tvalue, class Tcompare >
bool operator==(const Tmap_iter2 &iter2, const MultiMapIterator<Tmap_iter1, Tlist_iter1, Tkey, Tvalue, Tcompare> &iter1)
template<class Tmap_iter1, class Tlist_iter1, class Tmap_iter2, class Tkey, class Tvalue, class Tcontainer, class Tcompare >
bool operator==(const Tmap_iter2 &iter2, const MultiMapIterator<Tmap_iter1, Tlist_iter1, Tkey, Tvalue, Tcontainer, Tcompare> &iter1)
{
return !iter1.ListValid() && iter1.GetMapIter() == iter2;
}
@@ -268,8 +269,8 @@ bool operator==(const Tmap_iter2 &iter2, const MultiMapIterator<Tmap_iter1, Tlis
* @param iter1 MultiMap iterator.
* @return If iter1 doesn't point to the begin of the list pointed to by iter2.
*/
template<class Tmap_iter1, class Tlist_iter1, class Tmap_iter2, class Tkey, class Tvalue, class Tcompare >
bool operator!=(const Tmap_iter2 &iter2, const MultiMapIterator<Tmap_iter1, Tlist_iter1, Tkey, Tvalue, Tcompare> &iter1)
template<class Tmap_iter1, class Tlist_iter1, class Tmap_iter2, class Tkey, class Tvalue, class Tcontainer, class Tcompare >
bool operator!=(const Tmap_iter2 &iter2, const MultiMapIterator<Tmap_iter1, Tlist_iter1, Tkey, Tvalue, Tcontainer, Tcompare> &iter1)
{
return iter1.ListValid() || iter1.GetMapIter() != iter2;
}
@@ -282,10 +283,10 @@ bool operator!=(const Tmap_iter2 &iter2, const MultiMapIterator<Tmap_iter1, Tlis
* STL-compatible members are named in STL style, all others are named in OpenTTD
* style.
*/
template<typename Tkey, typename Tvalue, typename Tcompare = std::less<Tkey> >
class MultiMap : public std::map<Tkey, std::list<Tvalue>, Tcompare > {
template<typename Tkey, typename Tvalue, typename Tcontainer = std::list<Tvalue>, typename Tcompare = std::less<Tkey> >
class MultiMap : public std::map<Tkey, Tcontainer, Tcompare > {
public:
typedef typename std::list<Tvalue> List;
typedef Tcontainer List;
typedef typename List::iterator ListIterator;
typedef typename List::const_iterator ConstListIterator;
@@ -293,8 +294,8 @@ public:
typedef typename Map::iterator MapIterator;
typedef typename Map::const_iterator ConstMapIterator;
typedef MultiMapIterator<MapIterator, ListIterator, Tkey, Tvalue, Tcompare> iterator;
typedef MultiMapIterator<ConstMapIterator, ConstListIterator, Tkey, const Tvalue, Tcompare> const_iterator;
typedef MultiMapIterator<MapIterator, ListIterator, Tkey, Tvalue, Tcontainer, Tcompare> iterator;
typedef MultiMapIterator<ConstMapIterator, ConstListIterator, Tkey, const Tvalue, Tcontainer, Tcompare> const_iterator;
/**
* Erase the value pointed to by an iterator. The iterator may be invalid afterwards.

View File

@@ -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
}

View File

@@ -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: