From 6939569362a8c3e2c1b5174962309d0d73152845 Mon Sep 17 00:00:00 2001 From: rubidium Date: Wed, 23 Apr 2008 20:56:08 +0000 Subject: [PATCH] (svn r12855) -Codechange: do not use autoptr's for testing whether certain objects can be build, but check it directly in the pool so we do not have to call destructors in the testing phase. Stations still use the autoptr though. --- src/engine.cpp | 9 ++----- src/group_cmd.cpp | 11 ++------- src/industry_cmd.cpp | 13 +++++----- src/oldpool.h | 12 ++++++++++ src/rail_cmd.cpp | 8 ++----- src/road_cmd.cpp | 7 ++---- src/ship_cmd.cpp | 1 - src/signs.cpp | 7 ++---- src/town_cmd.cpp | 7 ++---- src/water_cmd.cpp | 7 ++---- src/waypoint.cpp | 57 +++++++++++++++++++++----------------------- 11 files changed, 60 insertions(+), 79 deletions(-) diff --git a/src/engine.cpp b/src/engine.cpp index 71d56d5cb9..3bc0657c29 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -15,7 +15,6 @@ #include "aircraft.h" #include "newgrf_cargo.h" #include "group.h" -#include "misc/autoptr.hpp" #include "strings_func.h" #include "gfx_func.h" #include "functions.h" @@ -516,19 +515,15 @@ CommandCost AddEngineReplacement(EngineRenewList *erl, EngineID old_engine, Engi return CommandCost(); } - er = new EngineRenew(old_engine, new_engine); - if (er == NULL) return CMD_ERROR; - AutoPtrT er_auto_delete = er; - + if (!EngineRenew::CanAllocateItem()) return CMD_ERROR; if (flags & DC_EXEC) { + er = new EngineRenew(old_engine, new_engine); er->group_id = group; /* Insert before the first element */ er->next = (EngineRenew *)(*erl); *erl = (EngineRenewList)er; - - er_auto_delete.Detach(); } return CommandCost(); diff --git a/src/group_cmd.cpp b/src/group_cmd.cpp index 17d82129c0..685a9e53bf 100644 --- a/src/group_cmd.cpp +++ b/src/group_cmd.cpp @@ -12,7 +12,6 @@ #include "train.h" #include "aircraft.h" #include "vehicle_gui.h" -#include "misc/autoptr.hpp" #include "strings_func.h" #include "functions.h" #include "window_func.h" @@ -94,20 +93,14 @@ CommandCost CmdCreateGroup(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) VehicleType vt = (VehicleType)p1; if (!IsPlayerBuildableVehicleType(vt)) return CMD_ERROR; - AutoPtrT g_auto_delete; - - Group *g = new Group(_current_player); - if (g == NULL) return CMD_ERROR; - - g_auto_delete = g; + if (!Group::CanAllocateItem()) return CMD_ERROR; if (flags & DC_EXEC) { + Group *g = new Group(_current_player); g->replace_protection = false; g->vehicle_type = vt; InvalidateWindowData(GetWCForVT(vt), (vt << 11) | VLW_GROUP_LIST | _current_player); - - g_auto_delete.Detach(); } return CommandCost(); diff --git a/src/industry_cmd.cpp b/src/industry_cmd.cpp index 75b7c3ca02..8a33c20d1b 100644 --- a/src/industry_cmd.cpp +++ b/src/industry_cmd.cpp @@ -26,7 +26,6 @@ #include "newgrf_industries.h" #include "newgrf_industrytiles.h" #include "newgrf_callbacks.h" -#include "misc/autoptr.hpp" #include "autoslope.h" #include "transparency.h" #include "water.h" @@ -1592,17 +1591,19 @@ static Industry *CreateNewIndustryHelper(TileIndex tile, IndustryType type, uint if (!CheckIfIndustryIsAllowed(tile, type, t)) return NULL; if (!CheckSuitableIndustryPos(tile)) return NULL; - Industry *i = new Industry(tile); - if (i == NULL) return NULL; - AutoPtrT i_auto_delete = i; + if (!Industry::CanAllocateItem()) return NULL; if (flags & DC_EXEC) { + Industry *i = new Industry(tile); if (!custom_shape_check) CheckIfCanLevelIndustryPlatform(tile, DC_EXEC, it, type); DoCreateNewIndustry(i, tile, type, it, itspec_index, t, OWNER_NONE); - i_auto_delete.Detach(); + + return i; } - return i; + /* We need to return a non-NULL pointer to tell we have created an industry. + * However, we haven't created a real one (no DC_EXEC), so return a fake one. */ + return GetIndustry(0); } /** Build/Fund an industry diff --git a/src/oldpool.h b/src/oldpool.h index 46d31e9995..e8081fa823 100644 --- a/src/oldpool.h +++ b/src/oldpool.h @@ -292,6 +292,18 @@ protected: { return Tpool->CleaningPool(); } + +public: + /** + * Check whether we can allocate an item in this pool. This to prevent the + * need to actually construct the object and then destructing it again, + * which could be *very* costly. + * @return true if and only if at least ONE item can be allocated. + */ + static inline bool CanAllocateItem() + { + return AllocateRaw() != NULL; + } }; diff --git a/src/rail_cmd.cpp b/src/rail_cmd.cpp index 9caf2132f1..c08014d12a 100644 --- a/src/rail_cmd.cpp +++ b/src/rail_cmd.cpp @@ -30,7 +30,6 @@ #include "newgrf_callbacks.h" #include "newgrf_station.h" #include "train.h" -#include "misc/autoptr.hpp" #include "variables.h" #include "autoslope.h" #include "transparency.h" @@ -766,12 +765,10 @@ CommandCost CmdBuildTrainDepot(TileIndex tile, uint32 flags, uint32 p1, uint32 p if (MayHaveBridgeAbove(tile) && IsBridgeAbove(tile)) return_cmd_error(STR_5007_MUST_DEMOLISH_BRIDGE_FIRST); - Depot *d = new Depot(tile); - - if (d == NULL) return CMD_ERROR; - AutoPtrT d_auto_delete = d; + if (!Depot::CanAllocateItem()) return CMD_ERROR; if (flags & DC_EXEC) { + Depot *d = new Depot(tile); MakeRailDepot(tile, _current_player, dir, (RailType)p1); MarkTileDirtyByTile(tile); @@ -779,7 +776,6 @@ CommandCost CmdBuildTrainDepot(TileIndex tile, uint32 flags, uint32 p1, uint32 p AddSideToSignalBuffer(tile, INVALID_DIAGDIR, _current_player); YapfNotifyTrackLayoutChange(tile, TrackdirToTrack(DiagdirToDiagTrackdir(dir))); - d_auto_delete.Detach(); } return cost.AddCost(_price.build_train_depot); diff --git a/src/road_cmd.cpp b/src/road_cmd.cpp index a2a32d0230..c95506a725 100644 --- a/src/road_cmd.cpp +++ b/src/road_cmd.cpp @@ -23,7 +23,6 @@ #include "newgrf.h" #include "station_map.h" #include "tunnel_map.h" -#include "misc/autoptr.hpp" #include "variables.h" #include "autoslope.h" #include "transparency.h" @@ -817,16 +816,14 @@ CommandCost CmdBuildRoadDepot(TileIndex tile, uint32 flags, uint32 p1, uint32 p2 if (MayHaveBridgeAbove(tile) && IsBridgeAbove(tile)) return_cmd_error(STR_5007_MUST_DEMOLISH_BRIDGE_FIRST); - Depot *dep = new Depot(tile); - if (dep == NULL) return CMD_ERROR; - AutoPtrT d_auto_delete = dep; + if (!Depot::CanAllocateItem()) return CMD_ERROR; if (flags & DC_EXEC) { + Depot *dep = new Depot(tile); dep->town_index = ClosestTownFromTile(tile, (uint)-1)->index; MakeRoadDepot(tile, _current_player, dir, rt); MarkTileDirtyByTile(tile); - d_auto_delete.Detach(); } return cost.AddCost(_price.build_road_depot); } diff --git a/src/ship_cmd.cpp b/src/ship_cmd.cpp index de49269990..a6a4251504 100644 --- a/src/ship_cmd.cpp +++ b/src/ship_cmd.cpp @@ -28,7 +28,6 @@ #include "newgrf_text.h" #include "newgrf_sound.h" #include "spritecache.h" -#include "misc/autoptr.hpp" #include "strings_func.h" #include "functions.h" #include "window_func.h" diff --git a/src/signs.cpp b/src/signs.cpp index a1dce33049..93ab460f00 100644 --- a/src/signs.cpp +++ b/src/signs.cpp @@ -11,7 +11,6 @@ #include "saveload.h" #include "command_func.h" #include "variables.h" -#include "misc/autoptr.hpp" #include "strings_func.h" #include "viewport_func.h" #include "zoom_func.h" @@ -99,12 +98,11 @@ static void MarkSignDirty(Sign *si) CommandCost CmdPlaceSign(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) { /* Try to locate a new sign */ - Sign *si = new Sign(_current_player); - if (si == NULL) return_cmd_error(STR_2808_TOO_MANY_SIGNS); - AutoPtrT s_auto_delete = si; + if (!Sign::CanAllocateItem()) return_cmd_error(STR_2808_TOO_MANY_SIGNS); /* When we execute, really make the sign */ if (flags & DC_EXEC) { + Sign *si = new Sign(_current_player); int x = TileX(tile) * TILE_SIZE; int y = TileY(tile) * TILE_SIZE; @@ -117,7 +115,6 @@ CommandCost CmdPlaceSign(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) _sign_sort_dirty = true; _new_sign_id = si->index; _total_signs++; - s_auto_delete.Detach(); } return CommandCost(); diff --git a/src/town_cmd.cpp b/src/town_cmd.cpp index 8bbe053775..1f6c2bd61c 100644 --- a/src/town_cmd.cpp +++ b/src/town_cmd.cpp @@ -31,7 +31,6 @@ #include "newgrf_house.h" #include "newgrf_commons.h" #include "newgrf_townname.h" -#include "misc/autoptr.hpp" #include "autoslope.h" #include "waypoint.h" #include "transparency.h" @@ -1538,16 +1537,14 @@ CommandCost CmdBuildTown(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) return_cmd_error(STR_023A_TOO_MANY_TOWNS); /* Allocate town struct */ - Town *t = new Town(tile); - if (t == NULL) return_cmd_error(STR_023A_TOO_MANY_TOWNS); - AutoPtrT t_auto_delete = t; + if (!Town::CanAllocateItem()) return_cmd_error(STR_023A_TOO_MANY_TOWNS); /* Create the town */ if (flags & DC_EXEC) { + Town *t = new Town(tile); _generating_world = true; DoCreateTown(t, tile, townnameparts, (TownSizeMode)p2, p1); _generating_world = false; - t_auto_delete.Detach(); } return CommandCost(); } diff --git a/src/water_cmd.cpp b/src/water_cmd.cpp index 8e16b58568..6412a82b25 100644 --- a/src/water_cmd.cpp +++ b/src/water_cmd.cpp @@ -24,7 +24,6 @@ #include "industry_map.h" #include "newgrf.h" #include "newgrf_canal.h" -#include "misc/autoptr.hpp" #include "transparency.h" #include "strings_func.h" #include "functions.h" @@ -201,18 +200,16 @@ CommandCost CmdBuildShipDepot(TileIndex tile, uint32 flags, uint32 p1, uint32 p2 ret = DoCommand(tile2, 0, 0, flags, CMD_LANDSCAPE_CLEAR); if (CmdFailed(ret)) return CMD_ERROR; - Depot *depot = new Depot(tile); - if (depot == NULL) return CMD_ERROR; - AutoPtrT d_auto_delete = depot; + if (!Depot::CanAllocateItem()) return CMD_ERROR; if (flags & DC_EXEC) { + Depot *depot = new Depot(tile); depot->town_index = ClosestTownFromTile(tile, (uint)-1)->index; MakeShipDepot(tile, _current_player, DEPOT_NORTH, axis, wc1); MakeShipDepot(tile2, _current_player, DEPOT_SOUTH, axis, wc2); MarkTileDirtyByTile(tile); MarkTileDirtyByTile(tile2); - d_auto_delete.Detach(); } return CommandCost(EXPENSES_CONSTRUCTION, _price.build_ship_depot); diff --git a/src/waypoint.cpp b/src/waypoint.cpp index 781c67339d..2a0e98eb68 100644 --- a/src/waypoint.cpp +++ b/src/waypoint.cpp @@ -18,7 +18,6 @@ #include "variables.h" #include "yapf/yapf.h" #include "newgrf.h" -#include "misc/autoptr.hpp" #include "strings_func.h" #include "gfx_func.h" #include "functions.h" @@ -191,7 +190,6 @@ void AfterLoadWaypoints() CommandCost CmdBuildTrainWaypoint(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) { Waypoint *wp; - AutoPtrT wp_auto_delete; Slope tileh; Axis axis; @@ -219,35 +217,35 @@ CommandCost CmdBuildTrainWaypoint(TileIndex tile, uint32 flags, uint32 p1, uint3 /* Check if there is an already existing, deleted, waypoint close to us that we can reuse. */ wp = FindDeletedWaypointCloseTo(tile); - if (wp == NULL) { - wp = new Waypoint(tile); - if (wp == NULL) return CMD_ERROR; - - wp_auto_delete = wp; - - wp->town_index = INVALID_TOWN; - wp->name = NULL; - wp->town_cn = 0; - } else if (flags & DC_EXEC) { - /* Move existing (recently deleted) waypoint to the new location */ - - /* First we update the destination for all vehicles that - * have the old waypoint in their orders. */ - Vehicle *v; - FOR_ALL_VEHICLES(v) { - if (v->type == VEH_TRAIN && - v->First() == v && - v->current_order.IsType(OT_GOTO_WAYPOINT) && - v->dest_tile == wp->xy) { - v->dest_tile = tile; - } - } - - RedrawWaypointSign(wp); - wp->xy = tile; - } + if (wp == NULL && !Waypoint::CanAllocateItem()) return CMD_ERROR; if (flags & DC_EXEC) { + if (wp == NULL) { + wp = new Waypoint(tile); + if (wp == NULL) return CMD_ERROR; + + wp->town_index = INVALID_TOWN; + wp->name = NULL; + wp->town_cn = 0; + } else { + /* Move existing (recently deleted) waypoint to the new location */ + + /* First we update the destination for all vehicles that + * have the old waypoint in their orders. */ + Vehicle *v; + FOR_ALL_VEHICLES(v) { + if (v->type == VEH_TRAIN && + v->First() == v && + v->current_order.IsType(OT_GOTO_WAYPOINT) && + v->dest_tile == wp->xy) { + v->dest_tile = tile; + } + } + + RedrawWaypointSign(wp); + wp->xy = tile; + } + const StationSpec* statspec; MakeRailWaypoint(tile, GetTileOwner(tile), axis, GetRailType(tile), wp->index); @@ -274,7 +272,6 @@ CommandCost CmdBuildTrainWaypoint(TileIndex tile, uint32 flags, uint32 p1, uint3 UpdateWaypointSign(wp); RedrawWaypointSign(wp); YapfNotifyTrackLayoutChange(tile, AxisToTrack(axis)); - wp_auto_delete.Detach(); } return CommandCost(EXPENSES_CONSTRUCTION, _price.build_train_depot);