Changeset - r23623:2bc857f3bb00
[Not reviewed]
master
0 3 0
Michael Lutz - 5 years ago 2019-04-14 14:28:08
michi@icosahedron.de
Codechange: Replace SmallStackSafeStackAlloc with std::array.

The only port that ever used it to make heap allocations instead of stack ones was the NDS port, which got thrown out some time ago.
3 files changed with 49 insertions and 90 deletions:
0 comments (0 inline, 0 general)
src/core/alloc_type.hpp
Show inline comments
 
@@ -15,49 +15,6 @@
 
#include "alloc_func.hpp"
 

	
 
/**
 
 * A small 'wrapper' for allocations that can be done on most OSes on the
 
 * stack, but are just too large to fit in the stack on devices with a small
 
 * stack such as the NDS.
 
 * So when it is possible a stack allocation is made, otherwise a heap
 
 * allocation is made and this is freed once the struct goes out of scope.
 
 * @param T      the type to make the allocation for
 
 * @param length the amount of items to allocate
 
 */
 
template <typename T, size_t length>
 
struct SmallStackSafeStackAlloc {
 
	/** Storing the data on the stack */
 
	T data[length];
 

	
 
	/**
 
	 * Gets a pointer to the data stored in this wrapper.
 
	 * @return the pointer.
 
	 */
 
	inline operator T *()
 
	{
 
		return data;
 
	}
 

	
 
	/**
 
	 * Gets a pointer to the data stored in this wrapper.
 
	 * @return the pointer.
 
	 */
 
	inline T *operator -> ()
 
	{
 
		return data;
 
	}
 

	
 
	/**
 
	 * Gets a pointer to the last data element stored in this wrapper.
 
	 * @note needed because endof does not work properly for pointers.
 
	 * @return the 'endof' pointer.
 
	 */
 
	inline T *EndOf()
 
	{
 
		return endof(data);
 
	}
 
};
 

	
 
/**
 
 * A reusable buffer that can be used for places that temporary allocate
 
 * a bit of memory and do that very often, or for places where static
 
 * memory is allocated that might need to be reallocated sometimes.
src/saveload/map_sl.cpp
Show inline comments
 
@@ -13,6 +13,7 @@
 
#include "../map_func.h"
 
#include "../core/bitmath_func.hpp"
 
#include "../fios.h"
 
#include <array>
 

	
 
#include "saveload.h"
 

	
 
@@ -51,80 +52,80 @@ static const uint MAP_SL_BUF_SIZE = 4096
 

	
 
static void Load_MAPT()
 
{
 
	SmallStackSafeStackAlloc<byte, MAP_SL_BUF_SIZE> buf;
 
	std::array<byte, MAP_SL_BUF_SIZE> buf;
 
	TileIndex size = MapSize();
 

	
 
	for (TileIndex i = 0; i != size;) {
 
		SlArray(buf, MAP_SL_BUF_SIZE, SLE_UINT8);
 
		SlArray(buf.data(), MAP_SL_BUF_SIZE, SLE_UINT8);
 
		for (uint j = 0; j != MAP_SL_BUF_SIZE; j++) _m[i++].type = buf[j];
 
	}
 
}
 

	
 
static void Save_MAPT()
 
{
 
	SmallStackSafeStackAlloc<byte, MAP_SL_BUF_SIZE> buf;
 
	std::array<byte, MAP_SL_BUF_SIZE> buf;
 
	TileIndex size = MapSize();
 

	
 
	SlSetLength(size);
 
	for (TileIndex i = 0; i != size;) {
 
		for (uint j = 0; j != MAP_SL_BUF_SIZE; j++) buf[j] = _m[i++].type;
 
		SlArray(buf, MAP_SL_BUF_SIZE, SLE_UINT8);
 
		SlArray(buf.data(), MAP_SL_BUF_SIZE, SLE_UINT8);
 
	}
 
}
 

	
 
static void Load_MAPH()
 
{
 
	SmallStackSafeStackAlloc<byte, MAP_SL_BUF_SIZE> buf;
 
	std::array<byte, MAP_SL_BUF_SIZE> buf;
 
	TileIndex size = MapSize();
 

	
 
	for (TileIndex i = 0; i != size;) {
 
		SlArray(buf, MAP_SL_BUF_SIZE, SLE_UINT8);
 
		SlArray(buf.data(), MAP_SL_BUF_SIZE, SLE_UINT8);
 
		for (uint j = 0; j != MAP_SL_BUF_SIZE; j++) _m[i++].height = buf[j];
 
	}
 
}
 

	
 
static void Save_MAPH()
 
{
 
	SmallStackSafeStackAlloc<byte, MAP_SL_BUF_SIZE> buf;
 
	std::array<byte, MAP_SL_BUF_SIZE> buf;
 
	TileIndex size = MapSize();
 

	
 
	SlSetLength(size);
 
	for (TileIndex i = 0; i != size;) {
 
		for (uint j = 0; j != MAP_SL_BUF_SIZE; j++) buf[j] = _m[i++].height;
 
		SlArray(buf, MAP_SL_BUF_SIZE, SLE_UINT8);
 
		SlArray(buf.data(), MAP_SL_BUF_SIZE, SLE_UINT8);
 
	}
 
}
 

	
 
static void Load_MAP1()
 
{
 
	SmallStackSafeStackAlloc<byte, MAP_SL_BUF_SIZE> buf;
 
	std::array<byte, MAP_SL_BUF_SIZE> buf;
 
	TileIndex size = MapSize();
 

	
 
	for (TileIndex i = 0; i != size;) {
 
		SlArray(buf, MAP_SL_BUF_SIZE, SLE_UINT8);
 
		SlArray(buf.data(), MAP_SL_BUF_SIZE, SLE_UINT8);
 
		for (uint j = 0; j != MAP_SL_BUF_SIZE; j++) _m[i++].m1 = buf[j];
 
	}
 
}
 

	
 
static void Save_MAP1()
 
{
 
	SmallStackSafeStackAlloc<byte, MAP_SL_BUF_SIZE> buf;
 
	std::array<byte, MAP_SL_BUF_SIZE> buf;
 
	TileIndex size = MapSize();
 

	
 
	SlSetLength(size);
 
	for (TileIndex i = 0; i != size;) {
 
		for (uint j = 0; j != MAP_SL_BUF_SIZE; j++) buf[j] = _m[i++].m1;
 
		SlArray(buf, MAP_SL_BUF_SIZE, SLE_UINT8);
 
		SlArray(buf.data(), MAP_SL_BUF_SIZE, SLE_UINT8);
 
	}
 
}
 

	
 
static void Load_MAP2()
 
{
 
	SmallStackSafeStackAlloc<uint16, MAP_SL_BUF_SIZE> buf;
 
	std::array<uint16, MAP_SL_BUF_SIZE> buf;
 
	TileIndex size = MapSize();
 

	
 
	for (TileIndex i = 0; i != size;) {
 
		SlArray(buf, MAP_SL_BUF_SIZE,
 
		SlArray(buf.data(), MAP_SL_BUF_SIZE,
 
			/* In those versions the m2 was 8 bits */
 
			IsSavegameVersionBefore(SLV_5) ? SLE_FILE_U8 | SLE_VAR_U16 : SLE_UINT16
 
		);
 
@@ -134,94 +135,94 @@ static void Load_MAP2()
 

	
 
static void Save_MAP2()
 
{
 
	SmallStackSafeStackAlloc<uint16, MAP_SL_BUF_SIZE> buf;
 
	std::array<uint16, MAP_SL_BUF_SIZE> buf;
 
	TileIndex size = MapSize();
 

	
 
	SlSetLength(size * sizeof(uint16));
 
	for (TileIndex i = 0; i != size;) {
 
		for (uint j = 0; j != MAP_SL_BUF_SIZE; j++) buf[j] = _m[i++].m2;
 
		SlArray(buf, MAP_SL_BUF_SIZE, SLE_UINT16);
 
		SlArray(buf.data(), MAP_SL_BUF_SIZE, SLE_UINT16);
 
	}
 
}
 

	
 
static void Load_MAP3()
 
{
 
	SmallStackSafeStackAlloc<byte, MAP_SL_BUF_SIZE> buf;
 
	std::array<byte, MAP_SL_BUF_SIZE> buf;
 
	TileIndex size = MapSize();
 

	
 
	for (TileIndex i = 0; i != size;) {
 
		SlArray(buf, MAP_SL_BUF_SIZE, SLE_UINT8);
 
		SlArray(buf.data(), MAP_SL_BUF_SIZE, SLE_UINT8);
 
		for (uint j = 0; j != MAP_SL_BUF_SIZE; j++) _m[i++].m3 = buf[j];
 
	}
 
}
 

	
 
static void Save_MAP3()
 
{
 
	SmallStackSafeStackAlloc<byte, MAP_SL_BUF_SIZE> buf;
 
	std::array<byte, MAP_SL_BUF_SIZE> buf;
 
	TileIndex size = MapSize();
 

	
 
	SlSetLength(size);
 
	for (TileIndex i = 0; i != size;) {
 
		for (uint j = 0; j != MAP_SL_BUF_SIZE; j++) buf[j] = _m[i++].m3;
 
		SlArray(buf, MAP_SL_BUF_SIZE, SLE_UINT8);
 
		SlArray(buf.data(), MAP_SL_BUF_SIZE, SLE_UINT8);
 
	}
 
}
 

	
 
static void Load_MAP4()
 
{
 
	SmallStackSafeStackAlloc<byte, MAP_SL_BUF_SIZE> buf;
 
	std::array<byte, MAP_SL_BUF_SIZE> buf;
 
	TileIndex size = MapSize();
 

	
 
	for (TileIndex i = 0; i != size;) {
 
		SlArray(buf, MAP_SL_BUF_SIZE, SLE_UINT8);
 
		SlArray(buf.data(), MAP_SL_BUF_SIZE, SLE_UINT8);
 
		for (uint j = 0; j != MAP_SL_BUF_SIZE; j++) _m[i++].m4 = buf[j];
 
	}
 
}
 

	
 
static void Save_MAP4()
 
{
 
	SmallStackSafeStackAlloc<byte, MAP_SL_BUF_SIZE> buf;
 
	std::array<byte, MAP_SL_BUF_SIZE> buf;
 
	TileIndex size = MapSize();
 

	
 
	SlSetLength(size);
 
	for (TileIndex i = 0; i != size;) {
 
		for (uint j = 0; j != MAP_SL_BUF_SIZE; j++) buf[j] = _m[i++].m4;
 
		SlArray(buf, MAP_SL_BUF_SIZE, SLE_UINT8);
 
		SlArray(buf.data(), MAP_SL_BUF_SIZE, SLE_UINT8);
 
	}
 
}
 

	
 
static void Load_MAP5()
 
{
 
	SmallStackSafeStackAlloc<byte, MAP_SL_BUF_SIZE> buf;
 
	std::array<byte, MAP_SL_BUF_SIZE> buf;
 
	TileIndex size = MapSize();
 

	
 
	for (TileIndex i = 0; i != size;) {
 
		SlArray(buf, MAP_SL_BUF_SIZE, SLE_UINT8);
 
		SlArray(buf.data(), MAP_SL_BUF_SIZE, SLE_UINT8);
 
		for (uint j = 0; j != MAP_SL_BUF_SIZE; j++) _m[i++].m5 = buf[j];
 
	}
 
}
 

	
 
static void Save_MAP5()
 
{
 
	SmallStackSafeStackAlloc<byte, MAP_SL_BUF_SIZE> buf;
 
	std::array<byte, MAP_SL_BUF_SIZE> buf;
 
	TileIndex size = MapSize();
 

	
 
	SlSetLength(size);
 
	for (TileIndex i = 0; i != size;) {
 
		for (uint j = 0; j != MAP_SL_BUF_SIZE; j++) buf[j] = _m[i++].m5;
 
		SlArray(buf, MAP_SL_BUF_SIZE, SLE_UINT8);
 
		SlArray(buf.data(), MAP_SL_BUF_SIZE, SLE_UINT8);
 
	}
 
}
 

	
 
static void Load_MAP6()
 
{
 
	SmallStackSafeStackAlloc<byte, MAP_SL_BUF_SIZE> buf;
 
	std::array<byte, MAP_SL_BUF_SIZE> buf;
 
	TileIndex size = MapSize();
 

	
 
	if (IsSavegameVersionBefore(SLV_42)) {
 
		for (TileIndex i = 0; i != size;) {
 
			/* 1024, otherwise we overflow on 64x64 maps! */
 
			SlArray(buf, 1024, SLE_UINT8);
 
			SlArray(buf.data(), 1024, SLE_UINT8);
 
			for (uint j = 0; j != 1024; j++) {
 
				_me[i++].m6 = GB(buf[j], 0, 2);
 
				_me[i++].m6 = GB(buf[j], 2, 2);
 
@@ -231,7 +232,7 @@ static void Load_MAP6()
 
		}
 
	} else {
 
		for (TileIndex i = 0; i != size;) {
 
			SlArray(buf, MAP_SL_BUF_SIZE, SLE_UINT8);
 
			SlArray(buf.data(), MAP_SL_BUF_SIZE, SLE_UINT8);
 
			for (uint j = 0; j != MAP_SL_BUF_SIZE; j++) _me[i++].m6 = buf[j];
 
		}
 
	}
 
@@ -239,59 +240,59 @@ static void Load_MAP6()
 

	
 
static void Save_MAP6()
 
{
 
	SmallStackSafeStackAlloc<byte, MAP_SL_BUF_SIZE> buf;
 
	std::array<byte, MAP_SL_BUF_SIZE> buf;
 
	TileIndex size = MapSize();
 

	
 
	SlSetLength(size);
 
	for (TileIndex i = 0; i != size;) {
 
		for (uint j = 0; j != MAP_SL_BUF_SIZE; j++) buf[j] = _me[i++].m6;
 
		SlArray(buf, MAP_SL_BUF_SIZE, SLE_UINT8);
 
		SlArray(buf.data(), MAP_SL_BUF_SIZE, SLE_UINT8);
 
	}
 
}
 

	
 
static void Load_MAP7()
 
{
 
	SmallStackSafeStackAlloc<byte, MAP_SL_BUF_SIZE> buf;
 
	std::array<byte, MAP_SL_BUF_SIZE> buf;
 
	TileIndex size = MapSize();
 

	
 
	for (TileIndex i = 0; i != size;) {
 
		SlArray(buf, MAP_SL_BUF_SIZE, SLE_UINT8);
 
		SlArray(buf.data(), MAP_SL_BUF_SIZE, SLE_UINT8);
 
		for (uint j = 0; j != MAP_SL_BUF_SIZE; j++) _me[i++].m7 = buf[j];
 
	}
 
}
 

	
 
static void Save_MAP7()
 
{
 
	SmallStackSafeStackAlloc<byte, MAP_SL_BUF_SIZE> buf;
 
	std::array<byte, MAP_SL_BUF_SIZE> buf;
 
	TileIndex size = MapSize();
 

	
 
	SlSetLength(size);
 
	for (TileIndex i = 0; i != size;) {
 
		for (uint j = 0; j != MAP_SL_BUF_SIZE; j++) buf[j] = _me[i++].m7;
 
		SlArray(buf, MAP_SL_BUF_SIZE, SLE_UINT8);
 
		SlArray(buf.data(), MAP_SL_BUF_SIZE, SLE_UINT8);
 
	}
 
}
 

	
 
static void Load_MAP8()
 
{
 
	SmallStackSafeStackAlloc<uint16, MAP_SL_BUF_SIZE> buf;
 
	std::array<uint16, MAP_SL_BUF_SIZE> buf;
 
	TileIndex size = MapSize();
 

	
 
	for (TileIndex i = 0; i != size;) {
 
		SlArray(buf, MAP_SL_BUF_SIZE, SLE_UINT16);
 
		SlArray(buf.data(), MAP_SL_BUF_SIZE, SLE_UINT16);
 
		for (uint j = 0; j != MAP_SL_BUF_SIZE; j++) _me[i++].m8 = buf[j];
 
	}
 
}
 

	
 
static void Save_MAP8()
 
{
 
	SmallStackSafeStackAlloc<uint16, MAP_SL_BUF_SIZE> buf;
 
	std::array<uint16, MAP_SL_BUF_SIZE> buf;
 
	TileIndex size = MapSize();
 

	
 
	SlSetLength(size * sizeof(uint16));
 
	for (TileIndex i = 0; i != size;) {
 
		for (uint j = 0; j != MAP_SL_BUF_SIZE; j++) buf[j] = _me[i++].m8;
 
		SlArray(buf, MAP_SL_BUF_SIZE, SLE_UINT16);
 
		SlArray(buf.data(), MAP_SL_BUF_SIZE, SLE_UINT16);
 
	}
 
}
 

	
src/saveload/oldloader_sl.cpp
Show inline comments
 
@@ -31,6 +31,7 @@
 
#include "../core/smallvec_type.hpp"
 
#include "saveload_internal.h"
 
#include "oldloader.h"
 
#include <array>
 

	
 
#include "table/strings.h"
 
#include "../table/engines.h"
 
@@ -1754,8 +1755,8 @@ bool LoadTTDMain(LoadgameState *ls)
 
	_read_ttdpatch_flags = false;
 

	
 
	/* Load the biggest chunk */
 
	SmallStackSafeStackAlloc<byte, OLD_MAP_SIZE * 2> map3;
 
	_old_map3 = map3.data;
 
	std::array<byte, OLD_MAP_SIZE * 2> map3;
 
	_old_map3 = map3.data();
 
	_old_vehicle_names = nullptr;
 
	try {
 
		if (!LoadChunk(ls, nullptr, main_chunk)) {
 
@@ -1797,10 +1798,10 @@ bool LoadTTOMain(LoadgameState *ls)
 

	
 
	_read_ttdpatch_flags = false;
 

	
 
	SmallStackSafeStackAlloc<byte, 103 * sizeof(Engine)> engines; // we don't want to call Engine constructor here
 
	_old_engines = (Engine *)engines.data;
 
	SmallStackSafeStackAlloc<StringID, 800> vehnames;
 
	_old_vehicle_names = vehnames.data;
 
	std::array<byte, 103 * sizeof(Engine)> engines; // we don't want to call Engine constructor here
 
	_old_engines = (Engine *)engines.data();
 
	std::array<StringID, 800> vehnames;
 
	_old_vehicle_names = vehnames.data();
 

	
 
	/* Load the biggest chunk */
 
	if (!LoadChunk(ls, nullptr, main_chunk)) {
0 comments (0 inline, 0 general)