(svn r6975) Use the pool macros for the Vehicle pool
authortron
Sat, 28 Oct 2006 10:55:59 +0000
changeset 4972 af023b864bad
parent 4971 0a3653c45b5a
child 4973 415bf67206dd
(svn r6975) Use the pool macros for the Vehicle pool
date.c
oldloader.c
openttd.c
saveload.c
vehicle.c
vehicle.h
--- a/date.c	Sat Oct 28 10:55:26 2006 +0000
+++ b/date.c	Sat Oct 28 10:55:59 2006 +0000
@@ -196,7 +196,7 @@
  */
 static void RunVehicleDayProc(uint daytick)
 {
-	uint total = _vehicle_pool.total_items;
+	uint total = GetVehiclePoolSize();
 	uint i;
 
 	for (i = daytick; i < total; i += DAY_TICKS) {
--- a/oldloader.c	Sat Oct 28 10:55:26 2006 +0000
+++ b/oldloader.c	Sat Oct 28 10:55:59 2006 +0000
@@ -1196,7 +1196,7 @@
 
 		_current_vehicle_id = num * _old_vehicle_multiplier + i;
 
-		if (!AddBlockIfNeeded(&_vehicle_pool, _current_vehicle_id))
+		if (!AddBlockIfNeeded(&_Vehicle_pool, _current_vehicle_id))
 			error("Vehicles: failed loading savegame: too many vehicles");
 
 		v = GetVehicle(_current_vehicle_id);
--- a/openttd.c	Sat Oct 28 10:55:26 2006 +0000
+++ b/openttd.c	Sat Oct 28 10:55:59 2006 +0000
@@ -257,7 +257,7 @@
 	CleanPool(&_town_pool);
 	CleanPool(&_industry_pool);
 	CleanPool(&_station_pool);
-	CleanPool(&_vehicle_pool);
+	CleanPool(&_Vehicle_pool);
 	CleanPool(&_sign_pool);
 	CleanPool(&_order_pool);
 
--- a/saveload.c	Sat Oct 28 10:55:26 2006 +0000
+++ b/saveload.c	Sat Oct 28 10:55:59 2006 +0000
@@ -1253,7 +1253,7 @@
 			return GetOrder(index);
 		}
 		case REF_VEHICLE: {
-			if (!AddBlockIfNeeded(&_vehicle_pool, index))
+			if (!AddBlockIfNeeded(&_Vehicle_pool, index))
 				error("Vehicles: failed loading savegame: too many vehicles");
 			return GetVehicle(index);
 		}
@@ -1286,7 +1286,7 @@
 			if (index == INVALID_VEHICLE)
 				return NULL;
 
-			if (!AddBlockIfNeeded(&_vehicle_pool, index))
+			if (!AddBlockIfNeeded(&_Vehicle_pool, index))
 				error("Vehicles: failed loading savegame: too many vehicles");
 			return GetVehicle(index);
 		}
--- a/vehicle.c	Sat Oct 28 10:55:26 2006 +0000
+++ b/vehicle.c	Sat Oct 28 10:55:59 2006 +0000
@@ -79,10 +79,6 @@
 
 
 enum {
-	/* Max vehicles: 64000 (512 * 125) */
-	VEHICLES_POOL_BLOCK_SIZE_BITS = 9,       /* In bits, so (1 << 9) == 512 */
-	VEHICLES_POOL_MAX_BLOCKS      = 125,
-
 	BLOCKS_FOR_SPECIAL_VEHICLES   = 2, ///< Blocks needed for special vehicles
 };
 
@@ -95,11 +91,11 @@
 
 	/* 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 (v = GetVehicle(start_item); v != NULL; v = (v->index + 1 < GetVehiclePoolSize()) ? GetVehicle(v->index + 1) : NULL) v->index = start_item++;
+	for (v = GetVehicle(start_item); v != NULL; v = (v->index + 1U < GetVehiclePoolSize()) ? GetVehicle(v->index + 1) : NULL) v->index = start_item++;
 }
 
 /* Initialize the vehicle-pool */
-MemoryPool _vehicle_pool = { "Vehicle", VEHICLES_POOL_MAX_BLOCKS, VEHICLES_POOL_BLOCK_SIZE_BITS, sizeof(Vehicle), &VehiclePoolNewBlock, NULL, 0, 0, NULL };
+DEFINE_POOL(Vehicle, Vehicle, VehiclePoolNewBlock, NULL)
 
 void VehicleServiceInDepot(Vehicle *v)
 {
@@ -297,9 +293,9 @@
 
 	/* 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 (v = GetVehicle(0); v != NULL; v = (v->index + 1 < GetVehiclePoolSize()) ? GetVehicle(v->index + 1) : NULL) {
+	for (v = GetVehicle(0); v != NULL; v = (v->index + 1U < GetVehiclePoolSize()) ? GetVehicle(v->index + 1) : NULL) {
 		/* No more room for the special vehicles, return NULL */
-		if (v->index >= (1 << _vehicle_pool.block_size_bits) * BLOCKS_FOR_SPECIAL_VEHICLES)
+		if (v->index >= (1 << Vehicle_POOL_BLOCK_SIZE_BITS) * BLOCKS_FOR_SPECIAL_VEHICLES)
 			return NULL;
 
 		if (!IsValidVehicle(v)) return InitializeVehicle(v);
@@ -314,26 +310,26 @@
  * *skip_vehicles is an offset to where in the array we should begin looking
  * this is to avoid looping though the same vehicles more than once after we learned that they are not free
  * this feature is used by AllocateVehicles() since it need to allocate more than one and when
- * another block is added to _vehicle_pool, since we only do that when we know it's already full
+ * another block is added to _Vehicle_pool, since we only do that when we know it's already full
  */
 static Vehicle *AllocateSingleVehicle(VehicleID *skip_vehicles)
 {
 	/* See note by ForceAllocateSpecialVehicle() why we skip the
 	 * first blocks */
 	Vehicle *v;
-	const int offset = (1 << VEHICLES_POOL_BLOCK_SIZE_BITS) * BLOCKS_FOR_SPECIAL_VEHICLES;
+	const int offset = (1 << Vehicle_POOL_BLOCK_SIZE_BITS) * BLOCKS_FOR_SPECIAL_VEHICLES;
 
 	/* We don't use FOR_ALL here, because FOR_ALL skips invalid items.
 	 * TODO - This is just a temporary stage, this will be removed. */
-	if (*skip_vehicles < (_vehicle_pool.total_items - offset)) { // make sure the offset in the array is not larger than the array itself
-		for (v = GetVehicle(offset + *skip_vehicles); v != NULL; v = (v->index + 1 < GetVehiclePoolSize()) ? GetVehicle(v->index + 1) : NULL) {
+	if (*skip_vehicles < (_Vehicle_pool.total_items - offset)) { // make sure the offset in the array is not larger than the array itself
+		for (v = GetVehicle(offset + *skip_vehicles); v != NULL; v = (v->index + 1U < GetVehiclePoolSize()) ? GetVehicle(v->index + 1) : NULL) {
 			(*skip_vehicles)++;
 			if (!IsValidVehicle(v)) return InitializeVehicle(v);
 		}
 	}
 
 	/* Check if we can add a block to the pool */
-	if (AddBlockToPool(&_vehicle_pool))
+	if (AddBlockToPool(&_Vehicle_pool))
 		return AllocateSingleVehicle(skip_vehicles);
 
 	return NULL;
@@ -451,10 +447,10 @@
 	/* Clean the vehicle pool, and reserve enough blocks
 	 *  for the special vehicles, plus one for all the other
 	 *  vehicles (which is increased on-the-fly) */
-	CleanPool(&_vehicle_pool);
-	AddBlockToPool(&_vehicle_pool);
+	CleanPool(&_Vehicle_pool);
+	AddBlockToPool(&_Vehicle_pool);
 	for (i = 0; i < BLOCKS_FOR_SPECIAL_VEHICLES; i++)
-		AddBlockToPool(&_vehicle_pool);
+		AddBlockToPool(&_Vehicle_pool);
 
 	for (i = 0; i < lengthof(_vehicle_position_hash); i++) {
 		_vehicle_position_hash[i] = INVALID_VEHICLE;
@@ -3177,7 +3173,7 @@
 	while ((index = SlIterateArray()) != -1) {
 		Vehicle *v;
 
-		if (!AddBlockIfNeeded(&_vehicle_pool, index))
+		if (!AddBlockIfNeeded(&_Vehicle_pool, index))
 			error("Vehicles: failed loading savegame: too many vehicles");
 
 		v = GetVehicle(index);
--- a/vehicle.h	Sat Oct 28 10:55:26 2006 +0000
+++ b/vehicle.h	Sat Oct 28 10:55:59 2006 +0000
@@ -359,23 +359,7 @@
 #define BEGIN_ENUM_WAGONS(v) do {
 #define END_ENUM_WAGONS(v) } while ( (v=v->next) != NULL);
 
-extern MemoryPool _vehicle_pool;
-
-/**
- * Get the pointer to the vehicle with index 'index'
- */
-static inline Vehicle *GetVehicle(VehicleID index)
-{
-	return (Vehicle*)GetItemFromPool(&_vehicle_pool, index);
-}
-
-/**
- * Get the current size of the VehiclePool
- */
-static inline uint16 GetVehiclePoolSize(void)
-{
-	return _vehicle_pool.total_items;
-}
+DECLARE_POOL(Vehicle, Vehicle, 9, 125)
 
 static inline VehicleID GetVehicleArraySize(void)
 {
@@ -403,7 +387,7 @@
 	v->type = 0;
 }
 
-#define FOR_ALL_VEHICLES_FROM(v, start) for (v = GetVehicle(start); v != NULL; v = (v->index + 1 < GetVehiclePoolSize()) ? GetVehicle(v->index + 1) : NULL) if (IsValidVehicle(v))
+#define FOR_ALL_VEHICLES_FROM(v, start) for (v = GetVehicle(start); v != NULL; v = (v->index + 1U < GetVehiclePoolSize()) ? GetVehicle(v->index + 1) : NULL) if (IsValidVehicle(v))
 #define FOR_ALL_VEHICLES(v) FOR_ALL_VEHICLES_FROM(v, 0)
 
 /**