summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/cargopacket.cpp81
-rw-r--r--src/cargopacket.h34
2 files changed, 13 insertions, 102 deletions
diff --git a/src/cargopacket.cpp b/src/cargopacket.cpp
index dca6103e2..94adb4eaf 100644
--- a/src/cargopacket.cpp
+++ b/src/cargopacket.cpp
@@ -8,36 +8,14 @@
#include "cargopacket.h"
#include "saveload.h"
-/** Cache for speeding up lookups in AllocateRaw */
-static uint _first_free_cargo_packet_index;
-
-/**
- * Called if a new block is added to the station-pool
- */
-static void CargoPacketPoolNewBlock(uint cpart_item)
-{
- /* We don't use FOR_ALL here, because FOR_ALL skips invalid items.
- * TODO - This is just a temporary stage, this will be removed. */
- for (CargoPacket *cp = GetCargoPacket(cpart_item); cp != NULL; cp = (cp->index + 1U < GetCargoPacketPoolSize()) ? GetCargoPacket(cp->index + 1U) : NULL) cp->index = cpart_item++;
-}
-
-static void CargoPacketPoolCleanBlock(uint start_item, uint end_item)
-{
- for (uint i = start_item; i <= end_item; i++) {
- CargoPacket *cp = GetCargoPacket(i);
- if (cp->IsValid()) cp->~CargoPacket();
- }
-}
-
/* Initialize the cargopacket-pool */
-DEFINE_OLD_POOL(CargoPacket, CargoPacket, CargoPacketPoolNewBlock, CargoPacketPoolCleanBlock)
+DEFINE_OLD_POOL_GENERIC(CargoPacket, CargoPacket)
void InitializeCargoPackets()
{
- _first_free_cargo_packet_index = 0;
/* Clean the cargo packet pool and create 1 block in it */
- CleanPool(&_CargoPacket_pool);
- AddBlockToPool(&_CargoPacket_pool);
+ _CargoPacket_pool.CleanPool();
+ _CargoPacket_pool.AddBlockToPool();
/* Check whether our &cargolist == &cargolist.packets "hack" works */
CargoList::AssertOnWrongPacketOffset();
@@ -59,7 +37,6 @@ CargoPacket::CargoPacket(StationID source, uint16 count)
CargoPacket::~CargoPacket()
{
- if (this->index < _first_free_cargo_packet_index) _first_free_cargo_packet_index = this->index;
this->count = 0;
}
@@ -68,52 +45,6 @@ bool CargoPacket::SameSource(CargoPacket *cp)
return this->source_xy == cp->source_xy && this->days_in_transit == cp->days_in_transit && this->paid_for == cp->paid_for;
}
-void *CargoPacket::operator new(size_t size)
-{
- CargoPacket *cp = AllocateRaw();
- return cp;
-}
-
-void *CargoPacket::operator new(size_t size, CargoPacket::ID cp_idx)
-{
- if (!AddBlockIfNeeded(&_CargoPacket_pool, cp_idx))
- error("CargoPackets: failed loading savegame: too many cargo packets");
-
- CargoPacket *cp = GetCargoPacket(cp_idx);
- return cp;
-}
-
-void CargoPacket::operator delete(void *p)
-{
-}
-
-void CargoPacket::operator delete(void *p, CargoPacket::ID cp_idx)
-{
-}
-
-/*static*/ CargoPacket *CargoPacket::AllocateRaw()
-{
- CargoPacket *cp = NULL;
-
- /* We don't use FOR_ALL here, because FOR_ALL skips invalid items.
- * TODO - This is just a temporary stage, this will be removed. */
- for (cp = GetCargoPacket(_first_free_cargo_packet_index); cp != NULL; cp = (cp->index + 1U < GetCargoPacketPoolSize()) ? GetCargoPacket(cp->index + 1U) : NULL) {
- if (!cp->IsValid()) {
- CargoPacket::ID index = cp->index;
-
- memset(cp, 0, sizeof(CargoPacket));
- cp->index = index;
- _first_free_cargo_packet_index = cp->index;
- return cp;
- }
- }
-
- /* Check if we can add a block to the pool */
- if (AddBlockToPool(&_CargoPacket_pool)) return AllocateRaw();
-
- error("CargoPackets: too many cargo packets");
-}
-
static const SaveLoad _cargopacket_desc[] = {
SLE_VAR(CargoPacket, source, SLE_UINT16),
SLE_VAR(CargoPacket, source_xy, SLE_UINT32),
@@ -141,11 +72,7 @@ static void Load_CAPA()
int index;
while ((index = SlIterateArray()) != -1) {
- if (!AddBlockIfNeeded(&_CargoPacket_pool, index)) {
- error("CargoPackets: failed loading savegame: too many cargo packets");
- }
-
- CargoPacket *cp = GetCargoPacket(index);
+ CargoPacket *cp = new (index) CargoPacket();
SlObject(cp, _cargopacket_desc);
}
}
diff --git a/src/cargopacket.h b/src/cargopacket.h
index 4f653e225..c7948b579 100644
--- a/src/cargopacket.h
+++ b/src/cargopacket.h
@@ -7,14 +7,17 @@
#include <list>
+typedef uint32 CargoPacketID;
+struct CargoPacket;
+
+/** We want to use a pool */
+DECLARE_OLD_POOL(CargoPacket, CargoPacket, 10, 1000)
+
+
/**
* Container for cargo from the same location and time
*/
-struct CargoPacket {
- typedef uint32 ID; ///< Type for cargopacket identifiers
-
- ID index; ///< The unique index of this packet
-
+struct CargoPacket : PoolItem<CargoPacket, CargoPacketID, &_CargoPacket_pool> {
StationID source; ///< The station where the cargo came from first
TileIndex source_xy; ///< The origin of the cargo (first station in feeder chain)
TileIndex loaded_at_xy; ///< Location where this cargo has been loaded into the vehicle
@@ -33,7 +36,7 @@ struct CargoPacket {
CargoPacket(StationID source = INVALID_STATION, uint16 count = 0);
/** Destroy the packet */
- ~CargoPacket();
+ virtual ~CargoPacket();
/**
@@ -49,27 +52,8 @@ struct CargoPacket {
* @return true if and only if days_in_transit and source_xy are equal
*/
bool SameSource(CargoPacket *cp);
-
-
- /* normal new/delete operators. Used when building/removing cargo packet */
- void *operator new (size_t size);
- void operator delete(void *p);
-
- /* new/delete operators accepting cargo packet index. Used when loading cargo packets from savegame. */
- void *operator new (size_t size, CargoPacket::ID cp_idx);
- void operator delete(void *p, CargoPacket::ID cp_idx);
-
-private:
- /**
- * Allocate the raw memory for this cargo packet
- * @return the allocated memory
- */
- static CargoPacket *AllocateRaw();
};
-/** We want to use a pool */
-DECLARE_OLD_POOL(CargoPacket, CargoPacket, 10, 1000)
-
/**
* Iterate over all _valid_ cargo packets from the given start
* @param cp the variable used as "iterator"