Changeset - r9488:2873382f8f4a
[Not reviewed]
master
0 6 0
smatz - 16 years ago 2008-06-10 21:59:22
smatz@openttd.org
(svn r13456) -Codechange: use AllocaM() macro instead of alloca() at most places
6 files changed with 15 insertions and 12 deletions:
0 comments (0 inline, 0 general)
src/core/alloc_func.hpp
Show inline comments
 
@@ -81,13 +81,16 @@ template <typename T> FORCEINLINE T* Rea
 
	 * to behave the same on all OSes.
 
	 */
 
	if (num_elements == 0) {
 
		free(t_ptr);
 
		return NULL;
 
	}
 

	
 
	t_ptr = (T*)realloc(t_ptr, num_elements * sizeof(T));
 
	if (t_ptr == NULL) ReallocError(num_elements * sizeof(T));
 
	return t_ptr;
 
}
 

	
 
/** alloca() has to be called in the parent function, so define AllocaM() as a macro */
 
#define AllocaM(T, num_elements) ((T*)alloca((num_elements) * sizeof(T)))
 

	
 
#endif /* ALLOC_FUNC_HPP */
src/newgrf.cpp
Show inline comments
 
@@ -2937,25 +2937,25 @@ static void VehicleMapSpriteGroup(byte *
 
			return;
 
		}
 

	
 
		grfmsg(6, "VehicleMapSpriteGroup: WagonOverride: %u engines, %u wagons",
 
				last_engines_count, idcount);
 
	} else {
 
		if (last_engines_count != idcount) {
 
			last_engines = ReallocT(last_engines, idcount);
 
			last_engines_count = idcount;
 
		}
 
	}
 

	
 
	EngineID *engines = (EngineID*)alloca(idcount * sizeof(*engines));
 
	EngineID *engines = AllocaM(EngineID, idcount);
 
	for (uint i = 0; i < idcount; i++) {
 
		engines[i] = GetNewEngine(_cur_grffile, (VehicleType)feature, grf_load_byte(&buf))->index;
 
		if (!wagover) last_engines[i] = engines[i];
 
	}
 

	
 
	uint8 cidcount = grf_load_byte(&buf);
 
	for (uint c = 0; c < cidcount; c++) {
 
		uint8 ctype = grf_load_byte(&buf);
 
		uint16 groupid = grf_load_word(&buf);
 
		if (!IsValidGroupID(groupid, "VehicleMapSpriteGroup")) continue;
 

	
 
		grfmsg(8, "VehicleMapSpriteGroup: * [%d] Cargo type 0x%X, group id 0x%02X", c, ctype, groupid);
 
@@ -2987,25 +2987,25 @@ static void VehicleMapSpriteGroup(byte *
 
		if (wagover) {
 
			SetWagonOverrideSprites(engine, CT_DEFAULT, _cur_grffile->spritegroups[groupid], last_engines, last_engines_count);
 
		} else {
 
			SetCustomEngineSprites(engine, CT_DEFAULT, _cur_grffile->spritegroups[groupid]);
 
			SetEngineGRF(engine, _cur_grffile);
 
		}
 
	}
 
}
 

	
 

	
 
static void CanalMapSpriteGroup(byte *buf, uint8 idcount)
 
{
 
	CanalFeature *cfs = (CanalFeature*)alloca(idcount * sizeof(*cfs));
 
	CanalFeature *cfs = AllocaM(CanalFeature, idcount);
 
	for (uint i = 0; i < idcount; i++) {
 
		cfs[i] = (CanalFeature)grf_load_byte(&buf);
 
	}
 

	
 
	uint8 cidcount = grf_load_byte(&buf);
 
	buf += cidcount * 3;
 

	
 
	uint16 groupid = grf_load_word(&buf);
 
	if (!IsValidGroupID(groupid, "CanalMapSpriteGroup")) return;
 

	
 
	for (uint i = 0; i < idcount; i++) {
 
		CanalFeature cf = cfs[i];
 
@@ -3013,25 +3013,25 @@ static void CanalMapSpriteGroup(byte *bu
 
		if (cf >= CF_END) {
 
			grfmsg(1, "CanalMapSpriteGroup: Canal subset %d out of range, skipping", cf);
 
			continue;
 
		}
 

	
 
		_water_feature[cf].group = _cur_grffile->spritegroups[groupid];
 
	}
 
}
 

	
 

	
 
static void StationMapSpriteGroup(byte *buf, uint8 idcount)
 
{
 
	uint8 *stations = (uint8*)alloca(idcount * sizeof(*stations));
 
	uint8 *stations = AllocaM(uint8, idcount);
 
	for (uint i = 0; i < idcount; i++) {
 
		stations[i] = grf_load_byte(&buf);
 
	}
 

	
 
	uint8 cidcount = grf_load_byte(&buf);
 
	for (uint c = 0; c < cidcount; c++) {
 
		uint8 ctype = grf_load_byte(&buf);
 
		uint16 groupid = grf_load_word(&buf);
 
		if (!IsValidGroupID(groupid, "StationMapSpriteGroup")) continue;
 

	
 
		ctype = TranslateCargo(GSF_STATION, ctype);
 
		if (ctype == CT_INVALID) continue;
 
@@ -3060,25 +3060,25 @@ static void StationMapSpriteGroup(byte *
 
		}
 

	
 
		statspec->spritegroup[CT_DEFAULT] = _cur_grffile->spritegroups[groupid];
 
		statspec->grffile = _cur_grffile;
 
		statspec->localidx = stations[i];
 
		SetCustomStationSpec(statspec);
 
	}
 
}
 

	
 

	
 
static void TownHouseMapSpriteGroup(byte *buf, uint8 idcount)
 
{
 
	uint8 *houses = (uint8*)alloca(idcount * sizeof(*houses));
 
	uint8 *houses = AllocaM(uint8, idcount);
 
	for (uint i = 0; i < idcount; i++) {
 
		houses[i] = grf_load_byte(&buf);
 
	}
 

	
 
	/* Skip the cargo type section, we only care about the default group */
 
	uint8 cidcount = grf_load_byte(&buf);
 
	buf += cidcount * 3;
 

	
 
	uint16 groupid = grf_load_word(&buf);
 
	if (!IsValidGroupID(groupid, "TownHouseMapSpriteGroup")) return;
 

	
 
	for (uint i = 0; i < idcount; i++) {
 
@@ -3086,25 +3086,25 @@ static void TownHouseMapSpriteGroup(byte
 

	
 
		if (hs == NULL) {
 
			grfmsg(1, "TownHouseMapSpriteGroup: Too many houses defined, skipping");
 
			return;
 
		}
 

	
 
		hs->spritegroup = _cur_grffile->spritegroups[groupid];
 
	}
 
}
 

	
 
static void IndustryMapSpriteGroup(byte *buf, uint8 idcount)
 
{
 
	uint8 *industries = (uint8*)alloca(idcount * sizeof(*industries));
 
	uint8 *industries = AllocaM(uint8, idcount);
 
	for (uint i = 0; i < idcount; i++) {
 
		industries[i] = grf_load_byte(&buf);
 
	}
 

	
 
	/* Skip the cargo type section, we only care about the default group */
 
	uint8 cidcount = grf_load_byte(&buf);
 
	buf += cidcount * 3;
 

	
 
	uint16 groupid = grf_load_word(&buf);
 
	if (!IsValidGroupID(groupid, "IndustryMapSpriteGroup")) return;
 

	
 
	for (uint i = 0; i < idcount; i++) {
 
@@ -3112,25 +3112,25 @@ static void IndustryMapSpriteGroup(byte 
 

	
 
		if (indsp == NULL) {
 
			grfmsg(1, "IndustryMapSpriteGroup: Too many industries defined, skipping");
 
			return;
 
		}
 

	
 
		indsp->grf_prop.spritegroup = _cur_grffile->spritegroups[groupid];
 
	}
 
}
 

	
 
static void IndustrytileMapSpriteGroup(byte *buf, uint8 idcount)
 
{
 
	uint8 *indtiles = (uint8*)alloca(idcount * sizeof(*indtiles));
 
	uint8 *indtiles = AllocaM(uint8, idcount);
 
	for (uint i = 0; i < idcount; i++) {
 
		indtiles[i] = grf_load_byte(&buf);
 
	}
 

	
 
	/* Skip the cargo type section, we only care about the default group */
 
	uint8 cidcount = grf_load_byte(&buf);
 
	buf += cidcount * 3;
 

	
 
	uint16 groupid = grf_load_word(&buf);
 
	if (!IsValidGroupID(groupid, "IndustrytileMapSpriteGroup")) return;
 

	
 
	for (uint i = 0; i < idcount; i++) {
 
@@ -3138,25 +3138,25 @@ static void IndustrytileMapSpriteGroup(b
 

	
 
		if (indtsp == NULL) {
 
			grfmsg(1, "IndustrytileMapSpriteGroup: Too many industry tiles defined, skipping");
 
			return;
 
		}
 

	
 
		indtsp->grf_prop.spritegroup = _cur_grffile->spritegroups[groupid];
 
	}
 
}
 

	
 
static void CargoMapSpriteGroup(byte *buf, uint8 idcount)
 
{
 
	CargoID *cargos = (CargoID*)alloca(idcount * sizeof(*cargos));
 
	CargoID *cargos = AllocaM(CargoID, idcount);
 
	for (uint i = 0; i < idcount; i++) {
 
		cargos[i] = grf_load_byte(&buf);
 
	}
 

	
 
	/* Skip the cargo type section, we only care about the default group */
 
	uint8 cidcount = grf_load_byte(&buf);
 
	buf += cidcount * 3;
 

	
 
	uint16 groupid = grf_load_word(&buf);
 
	if (!IsValidGroupID(groupid, "CargoMapSpriteGroup")) return;
 

	
 
	for (uint i = 0; i < idcount; i++) {
src/roadveh_cmd.cpp
Show inline comments
 
@@ -185,25 +185,25 @@ CommandCost CmdBuildRoadVeh(TileIndex ti
 
	if (flags & DC_QUERY_COST) return cost;
 

	
 
	/* The ai_new queries the vehicle cost before building the route,
 
	 * so we must check against cheaters no sooner than now. --pasky */
 
	if (!IsRoadDepotTile(tile)) return CMD_ERROR;
 
	if (!IsTileOwner(tile, _current_player)) return CMD_ERROR;
 

	
 
	if (HasTileRoadType(tile, ROADTYPE_TRAM) != HasBit(EngInfo(p1)->misc_flags, EF_ROAD_TRAM)) return_cmd_error(STR_DEPOT_WRONG_DEPOT_TYPE);
 

	
 
	uint num_vehicles = 1 + CountArticulatedParts(p1, false);
 

	
 
	/* Allow for the front and the articulated parts, plus one to "terminate" the list. */
 
	Vehicle **vl = (Vehicle**)alloca(sizeof(*vl) * (num_vehicles + 1));
 
	Vehicle **vl = AllocaM(Vehicle*, num_vehicles + 1);
 
	memset(vl, 0, sizeof(*vl) * (num_vehicles + 1));
 

	
 
	if (!Vehicle::AllocateList(vl, num_vehicles)) {
 
		return_cmd_error(STR_00E1_TOO_MANY_VEHICLES_IN_GAME);
 
	}
 

	
 
	v = vl[0];
 

	
 
	/* find the first free roadveh id */
 
	unit_num = HasBit(p2, 0) ? 0 : GetFreeUnitNumber(VEH_ROAD);
 
	if (unit_num > _settings_game.vehicle.max_roadveh)
 
		return_cmd_error(STR_00E1_TOO_MANY_VEHICLES_IN_GAME);
src/station_cmd.cpp
Show inline comments
 
@@ -1004,25 +1004,25 @@ CommandCost CmdBuildRailroadStation(Tile
 

	
 
		st->rect.BeforeAddRect(tile_org, w_org, h_org, StationRect::ADD_TRY);
 

	
 
		if (statspec != NULL) {
 
			/* Include this station spec's animation trigger bitmask
 
			 * in the station's cached copy. */
 
			st->cached_anim_triggers |= statspec->anim_triggers;
 
		}
 

	
 
		tile_delta = (axis == AXIS_X ? TileDiffXY(1, 0) : TileDiffXY(0, 1));
 
		track = AxisToTrack(axis);
 

	
 
		layout_ptr = (byte*)alloca(numtracks * plat_len);
 
		layout_ptr = AllocaM(byte, numtracks * plat_len);
 
		GetStationLayout(layout_ptr, numtracks, plat_len, statspec);
 

	
 
		numtracks_orig = numtracks;
 

	
 
		do {
 
			TileIndex tile = tile_org;
 
			int w = plat_len;
 
			do {
 
				byte layout = *layout_ptr++;
 
				MakeRailStation(tile, st->owner, st->index, axis, layout & ~1, (RailType)GB(p2, 0, 4));
 
				SetCustomStationSpecIndex(tile, specindex);
 
				SetStationTileRandomBits(tile, GB(Random(), 0, 4));
src/train_cmd.cpp
Show inline comments
 
@@ -540,25 +540,25 @@ void DrawTrainEngine(int x, int y, Engin
 
static CommandCost CmdBuildRailWagon(EngineID engine, TileIndex tile, uint32 flags)
 
{
 
	const RailVehicleInfo *rvi = RailVehInfo(engine);
 
	CommandCost value(EXPENSES_NEW_VEHICLES, (GetEngineProperty(engine, 0x17, rvi->base_cost) * _price.build_railwagon) >> 8);
 

	
 
	uint num_vehicles = 1 + CountArticulatedParts(engine, false);
 

	
 
	if (!(flags & DC_QUERY_COST)) {
 
		/* Check that the wagon can drive on the track in question */
 
		if (!IsCompatibleRail(rvi->railtype, GetRailType(tile))) return CMD_ERROR;
 

	
 
		/* Allow for the wagon and the articulated parts, plus one to "terminate" the list. */
 
		Vehicle **vl = (Vehicle**)alloca(sizeof(*vl) * (num_vehicles + 1));
 
		Vehicle **vl = AllocaM(Vehicle*, num_vehicles + 1);
 
		memset(vl, 0, sizeof(*vl) * (num_vehicles + 1));
 

	
 
		if (!Vehicle::AllocateList(vl, num_vehicles))
 
			return_cmd_error(STR_00E1_TOO_MANY_VEHICLES_IN_GAME);
 

	
 
		if (flags & DC_EXEC) {
 
			Vehicle *v = vl[0];
 
			v->spritenum = rvi->image_index;
 

	
 
			Vehicle *u = NULL;
 

	
 
			Vehicle *w;
 
@@ -707,25 +707,25 @@ CommandCost CmdBuildRailVehicle(TileInde
 
	CommandCost value = EstimateTrainCost(p1, rvi);
 

	
 
	uint num_vehicles =
 
		(rvi->railveh_type == RAILVEH_MULTIHEAD ? 2 : 1) +
 
		CountArticulatedParts(p1, false);
 

	
 
	if (!(flags & DC_QUERY_COST)) {
 
		/* Check if depot and new engine uses the same kind of tracks *
 
		 * We need to see if the engine got power on the tile to avoid eletric engines in non-electric depots */
 
		if (!HasPowerOnRail(rvi->railtype, GetRailType(tile))) return CMD_ERROR;
 

	
 
		/* Allow for the dual-heads and the articulated parts, plus one to "terminate" the list. */
 
		Vehicle **vl = (Vehicle**)alloca(sizeof(*vl) * (num_vehicles + 1));
 
		Vehicle **vl = AllocaM(Vehicle*, num_vehicles + 1);
 
		memset(vl, 0, sizeof(*vl) * (num_vehicles + 1));
 

	
 
		if (!Vehicle::AllocateList(vl, num_vehicles)) {
 
			return_cmd_error(STR_00E1_TOO_MANY_VEHICLES_IN_GAME);
 
		}
 

	
 
		Vehicle *v = vl[0];
 

	
 
		UnitID unit_num = HasBit(p2, 0) ? 0 : GetFreeUnitNumber(VEH_TRAIN);
 
		if (unit_num > _settings_game.vehicle.max_trains)
 
			return_cmd_error(STR_00E1_TOO_MANY_VEHICLES_IN_GAME);
 

	
src/win32.cpp
Show inline comments
 
@@ -465,25 +465,25 @@ static void GamelogPrintCrashLogProc(con
 
}
 

	
 
static LONG WINAPI ExceptionHandler(EXCEPTION_POINTERS *ep)
 
{
 
	char *output;
 
	static bool had_exception = false;
 

	
 
	if (had_exception) ExitProcess(0);
 
	had_exception = true;
 

	
 
	_ident = GetTickCount(); // something pretty unique
 

	
 
	MakeCRCTable((uint32*)alloca(256 * sizeof(uint32)));
 
	MakeCRCTable(AllocaM(uint32, 256));
 
	_crash_msg = output = (char*)LocalAlloc(LMEM_FIXED, 8192);
 

	
 
	{
 
		SYSTEMTIME time;
 
		GetLocalTime(&time);
 
		output += sprintf(output,
 
			"*** OpenTTD Crash Report ***\r\n"
 
			"Date: %d-%.2d-%.2d %.2d:%.2d:%.2d\r\n"
 
			"Build: %s built on " __DATE__ " " __TIME__ "\r\n",
 
			time.wYear,
 
			time.wMonth,
 
			time.wDay,
0 comments (0 inline, 0 general)