Skip to content

Commit

Permalink
Replace memset by ranges::fill (or by value initialization).
Browse files Browse the repository at this point in the history
  • Loading branch information
Jarod42 committed May 20, 2024
1 parent 27e9a2b commit b5d144a
Show file tree
Hide file tree
Showing 20 changed files with 53 additions and 61 deletions.
4 changes: 1 addition & 3 deletions src/ai/ai_resource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -490,9 +490,7 @@ static bool AiRequestSupply()

// Count the already made build requests.
const auto counter = AiGetBuildRequestsCount(*AiPlayer);
struct cnode cache[16];

memset(cache, 0, sizeof(cache));
struct cnode cache[16]{};

//
// Check if we can build this?
Expand Down
14 changes: 6 additions & 8 deletions src/include/ui/statusline.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,7 @@ class CFont;
class CStatusLine
{
public:
CStatusLine() : Width(0), TextX(0), TextY(0), Font(0) {
memset(Costs, 0, (ManaResCost + 1) * sizeof(int));
}
CStatusLine() = default;

void Draw();
void DrawCosts();
Expand All @@ -54,11 +52,11 @@ class CStatusLine
void ClearCosts();

public:
int Width;
int TextX;
int TextY;
CFont *Font;
int Costs[ManaResCost + 1];
int Width = 0;
int TextX = 0;
int TextY = 0;
CFont *Font = nullptr;
int Costs[ManaResCost + 1]{};

private:
std::string StatusLine;
Expand Down
7 changes: 4 additions & 3 deletions src/include/upgrade_structs.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
----------------------------------------------------------------------------*/

#include "settings.h"
#include "util.h"

#include <cstring>
#include <memory>
Expand Down Expand Up @@ -198,8 +199,8 @@ class CAllow

void Clear()
{
memset(Units, 0, sizeof(Units));
memset(Upgrades, 0, sizeof(Upgrades));
ranges::fill(Units, 0);
ranges::fill(Upgrades, '\0');
}

int Units[UnitTypeMax]{}; /// maximum amount of units allowed
Expand All @@ -215,7 +216,7 @@ class CUpgradeTimers
public:
CUpgradeTimers() = default;

void Clear() { memset(Upgrades, 0, sizeof(Upgrades)); }
void Clear() { ranges::fill(Upgrades, 0); }

/**
** all 0 at the beginning, all upgrade actions do increment values in
Expand Down
1 change: 1 addition & 0 deletions src/include/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
//@{

#include "filesystem.h"
#include "stratagus.h"

#include <cstdlib>
#include <cstdint>
Expand Down
4 changes: 2 additions & 2 deletions src/map/map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -313,8 +313,8 @@ void CMapInfo::Clear()
this->Description.clear();
this->Filename.clear();
this->MapWidth = this->MapHeight = 0;
memset(this->PlayerSide, 0, sizeof(this->PlayerSide));
memset(this->PlayerType, 0, sizeof(this->PlayerType));
ranges::fill(this->PlayerSide, 0);
ranges::fill(this->PlayerType, PlayerTypes::PlayerUnset);
this->MapUID = 0;

this->HighgroundsEnabled = false;
Expand Down
8 changes: 4 additions & 4 deletions src/map/tileset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -214,15 +214,15 @@ void CTileset::clear()
midOneTreeTile = 0;
botOneTreeTile = 0;
removedTreeTile = 0;
memset(woodTable, 0, sizeof(woodTable));
ranges::fill(woodTable, 0);
mixedLookupTable.clear();
topOneRockTile = 0;
midOneRockTile = 0;
botOneRockTile = 0;
removedRockTile = 0;
memset(rockTable, 0, sizeof(rockTable));
memset(humanWallTable, 0, sizeof(humanWallTable));
memset(orcWallTable, 0, sizeof(orcWallTable));
ranges::fill(rockTable, 0);
ranges::fill(humanWallTable, 0);
ranges::fill(orcWallTable, 0);
}

bool CTileset::setTileCount(const size_t newCount)
Expand Down
5 changes: 2 additions & 3 deletions src/network/mdns_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,8 @@ static int service_callback(int sock, const struct sockaddr* from, size_t addrle

void MDNS::AnswerServerQueries() {
if (serviceSocket == -1) {
// When recieving, a socket can recieve data from all network interfaces
struct sockaddr_in sock_addr;
memset(&sock_addr, 0, sizeof(struct sockaddr_in));
// When receiving, a socket can receive data from all network interfaces
struct sockaddr_in sock_addr{};
sock_addr.sin_family = AF_INET;
#ifdef _WIN32
sock_addr.sin_addr = in4addr_any;
Expand Down
9 changes: 3 additions & 6 deletions src/network/net_lowlevel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -307,9 +307,8 @@ Socket NetOpenUDP(unsigned long ip, int port)
}
// bind local port
if (port) {
struct sockaddr_in sock_addr;
struct sockaddr_in sock_addr{};

memset(&sock_addr, 0, sizeof(sock_addr));
sock_addr.sin_family = AF_INET;
sock_addr.sin_addr.s_addr = ip;
sock_addr.sin_port = htons(port);
Expand Down Expand Up @@ -339,9 +338,8 @@ Socket NetOpenTCP(const char *addr, int port)
}
// bind local port
if (port) {
struct sockaddr_in sock_addr;
struct sockaddr_in sock_addr{};

memset(&sock_addr, 0, sizeof(sock_addr));
sock_addr.sin_family = AF_INET;
if (addr) {
sock_addr.sin_addr.s_addr = inet_addr(addr);
Expand Down Expand Up @@ -377,7 +375,6 @@ Socket NetOpenTCP(const char *addr, int port)
*/
int NetConnectTCP(Socket sockfd, unsigned long addr, int port)
{
struct sockaddr_in sa;
#ifndef __BEOS__
int opt = 1;
setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, (setsockopttype)&opt, sizeof(opt));
Expand All @@ -389,7 +386,7 @@ int NetConnectTCP(Socket sockfd, unsigned long addr, int port)
return -1;
}

memset(&sa, 0, sizeof(sa));
struct sockaddr_in sa{};
memcpy(&sa.sin_addr, &addr, sizeof(addr));
sa.sin_family = AF_INET;
sa.sin_port = htons(port);
Expand Down
2 changes: 1 addition & 1 deletion src/network/net_message.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ void CNetworkHost::Clear()
this->Host = 0;
this->Port = 0;
this->PlyNr = 0;
memset(this->PlyName, 0, sizeof(this->PlyName));
ranges::fill(this->PlyName, '\0');
}

void CNetworkHost::SetName(const char *name)
Expand Down
3 changes: 1 addition & 2 deletions src/network/netsockets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,7 @@ class CTCPSocket_Impl

bool CTCPSocket_Impl::Open(const CHost &host)
{
char ip[24]; // 127.255.255.255:65555
memset(&ip, 0, sizeof(ip));
char ip[24]{}; // 127.255.255.255:65555
sprintf(ip, "%d.%d.%d.%d", NIPQUAD(ntohl(host.getIp())));
this->socket = NetOpenTCP(ip, host.getPort());
return this->socket != INVALID_SOCKET;
Expand Down
10 changes: 5 additions & 5 deletions src/network/network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -512,11 +512,11 @@ void NetworkOnStartGame()
ncqs[1].Type = MessageNone;
}
}
memset(NetworkSyncSeeds, 0, sizeof(NetworkSyncSeeds));
memset(NetworkSyncHashs, 0, sizeof(NetworkSyncHashs));
memset(PlayerQuit, 0, sizeof(PlayerQuit));
memset(NetworkLastFrame, 0, sizeof(NetworkLastFrame));
memset(NetworkLastCycle, 0, sizeof(NetworkLastCycle));
ranges::fill(NetworkSyncSeeds, 0);
ranges::fill(NetworkSyncHashs, 0);
ranges::fill(PlayerQuit, 0);
ranges::fill(NetworkLastFrame, 0);
ranges::fill(NetworkLastCycle, 0);
}

//----------------------------------------------------------------------------
Expand Down
22 changes: 11 additions & 11 deletions src/stratagus/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -751,8 +751,8 @@ void CPlayer::Init(PlayerTypes type)
this->MaxResources[i] = DefaultResourceMaxAmounts[i];
}

memset(this->UnitTypesCount, 0, sizeof(this->UnitTypesCount));
memset(this->UnitTypesAiActiveCount, 0, sizeof(this->UnitTypesAiActiveCount));
ranges::fill(this->UnitTypesCount, 0);
ranges::fill(this->UnitTypesAiActiveCount, 0);

this->Supply = 0;
this->Demand = 0;
Expand Down Expand Up @@ -801,14 +801,14 @@ void CPlayer::Clear()
GaveVisionTo.clear();
StartPos.x = 0;
StartPos.y = 0;
memset(Resources, 0, sizeof(Resources));
memset(StoredResources, 0, sizeof(StoredResources));
memset(MaxResources, 0, sizeof(MaxResources));
memset(LastResources, 0, sizeof(LastResources));
memset(Incomes, 0, sizeof(Incomes));
memset(Revenue, 0, sizeof(Revenue));
memset(UnitTypesCount, 0, sizeof(UnitTypesCount));
memset(UnitTypesAiActiveCount, 0, sizeof(UnitTypesAiActiveCount));
ranges::fill(Resources, 0);
ranges::fill(StoredResources, 0);
ranges::fill(MaxResources, 0);
ranges::fill(LastResources, 0);
ranges::fill(Incomes, 0);
ranges::fill(Revenue, 0);
ranges::fill(UnitTypesCount, 0);
ranges::fill(UnitTypesAiActiveCount, 0);
AiEnabled = false;
Ai = nullptr;
this->Units.resize(0);
Expand All @@ -823,7 +823,7 @@ void CPlayer::Clear()
Score = 0;
TotalUnits = 0;
TotalBuildings = 0;
memset(TotalResources, 0, sizeof(TotalResources));
ranges::fill(TotalResources, 0);
TotalRazings = 0;
TotalKills = 0;
this->LostMainFacilityTimer = 0;
Expand Down
3 changes: 1 addition & 2 deletions src/stratagus/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -676,8 +676,7 @@ void aligned_free(void *block)
fs::path GetExecutablePath()
{
#ifdef WIN32
TCHAR executable_path[MAX_PATH];
memset(executable_path, 0, sizeof(executable_path));
TCHAR executable_path[MAX_PATH]{};
GetModuleFileName(nullptr, executable_path, sizeof(executable_path)-1);
#else
const auto& executable_path = OriginalArgv[0];
Expand Down
2 changes: 1 addition & 1 deletion src/ui/interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ static void ShowInput()
static void UiBeginInput()
{
KeyState = EKeyState::Input;
memset(Input, 0, sizeof(Input));
ranges::fill(Input, '\0');
InputIndex = 0;
addCursorToInput();
UI.StatusLine.ClearCosts();
Expand Down
2 changes: 1 addition & 1 deletion src/ui/statusline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ void CStatusLine::SetCosts(int mana, int food, const int *costs)
if (costs) {
memcpy(Costs, costs, MaxCosts * sizeof(*costs));
} else {
memset(Costs, 0, sizeof(Costs));
ranges::fill(Costs, 0);
}
Costs[ManaResCost] = mana;
Costs[FoodCost] = food;
Expand Down
4 changes: 2 additions & 2 deletions src/unit/unit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ void CUnit::Init()

Frame = 0;
Colors = -1;
memset(IndividualUpgrades, 0, sizeof(IndividualUpgrades));
ranges::fill(IndividualUpgrades, false);
IX = 0;
IY = 0;
Direction = 0;
Expand All @@ -453,7 +453,7 @@ void CUnit::Init()
JustMoved = 0;
TeamSelected = 0;
RescuedFrom = nullptr;
memset(VisCount, 0, sizeof(VisCount));
ranges::fill(VisCount, 0);
memset(&Seen, 0, sizeof(Seen));
Variable.clear();
TTL = 0;
Expand Down
4 changes: 2 additions & 2 deletions src/unit/upgrade.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,8 @@ static int CclDefineModifier(lua_State *l)

auto um = std::make_unique<CUpgradeModifier>();

memset(um->ChangeUpgrades, '?', sizeof(um->ChangeUpgrades));
memset(um->ApplyTo, '?', sizeof(um->ApplyTo));
ranges::fill(um->ChangeUpgrades, '?');
ranges::fill(um->ApplyTo, '?');
um->Modifier.Variables.resize(UnitTypeVar.GetNumberVariable());
um->ModifyPercent.resize(UnitTypeVar.GetNumberVariable());

Expand Down
2 changes: 1 addition & 1 deletion src/video/shaders.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ static bool RenderWithShaderInternal(SDL_Renderer *renderer, SDL_Window* win, SD

lazyGlGetFloatv(GL_MODELVIEW_MATRIX, modelview);
lazyGlGetFloatv(GL_PROJECTION_MATRIX, projection);
memset(matrix, 0, sizeof(matrix));
ranges::fill(matrix, 0);
for (int i = 0; i < 4; i++) {
for(int j = 0; j < 4; j++) {
for (int k = 0; k < 4; k++) {
Expand Down
4 changes: 2 additions & 2 deletions tests/network/test_net_lowlevel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ class ClientTCP
class Foo
{
public:
Foo() { memset(&data, 0, sizeof(data)); }
Foo() = default;

void Fill()
{
Expand All @@ -153,7 +153,7 @@ class Foo
return true;
}
public:
char data[42];
char data[42]{};
};

class ReceiverTCPJob : public Job
Expand Down
4 changes: 2 additions & 2 deletions tests/network/test_udpsocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ TEST_CASE_FIXTURE(AutoNetwork, "CHost")
class Foo
{
public:
Foo() { memset(&data, 0, sizeof(data)); }
Foo() = default;

void Fill()
{
Expand All @@ -74,7 +74,7 @@ class Foo
return true;
}
public:
char data[42];
char data[42]{};
};

TEST_CASE_FIXTURE(AutoNetwork, "CUDPSocket")
Expand Down

0 comments on commit b5d144a

Please sign in to comment.