Codechange: Replace all usages of alloca/AllocaM with more modern/less discouraged alternatives

This commit is contained in:
Charles Pigott
2021-05-01 21:06:17 +01:00
committed by PeterN
parent b19f42ecd9
commit b282664242
18 changed files with 135 additions and 122 deletions

View File

@@ -124,22 +124,20 @@ struct DebugFileInfo {
SYSTEMTIME file_time;
};
static uint32 *_crc_table;
static uint32 _crc_table[256];
static void MakeCRCTable(uint32 *table)
static void MakeCRCTable()
{
uint32 crc, poly = 0xEDB88320L;
int i;
int j;
_crc_table = table;
for (i = 0; i != 256; i++) {
crc = i;
for (j = 8; j != 0; j--) {
crc = (crc & 1 ? (crc >> 1) ^ poly : crc >> 1);
}
table[i] = crc;
_crc_table[i] = crc;
}
}
@@ -206,7 +204,8 @@ static char *PrintModuleInfo(char *output, const char *last, HMODULE mod)
/* virtual */ char *CrashLogWindows::LogModules(char *output, const char *last) const
{
MakeCRCTable(AllocaM(uint32, 256));
MakeCRCTable();
output += seprintf(output, last, "Module information:\n");
HANDLE proc = OpenProcess(PROCESS_ALL_ACCESS, FALSE, GetCurrentProcessId());
@@ -426,7 +425,8 @@ char *CrashLogWindows::AppendDecodedStacktrace(char *buffer, const char *last) c
memcpy(&ctx, ep->ContextRecord, sizeof(ctx));
/* Allocate space for symbol info. */
IMAGEHLP_SYMBOL64 *sym_info = (IMAGEHLP_SYMBOL64*)alloca(sizeof(IMAGEHLP_SYMBOL64) + MAX_SYMBOL_LEN - 1);
char sym_info_raw[sizeof(IMAGEHLP_SYMBOL64) + MAX_SYMBOL_LEN - 1];
IMAGEHLP_SYMBOL64 *sym_info = (IMAGEHLP_SYMBOL64*)sym_info_raw;
sym_info->SizeOfStruct = sizeof(IMAGEHLP_SYMBOL64);
sym_info->MaxNameLength = MAX_SYMBOL_LEN;
@@ -698,7 +698,7 @@ static INT_PTR CALLBACK CrashDialogFunc(HWND wnd, UINT msg, WPARAM wParam, LPARA
len += wcslen(convert_to_fs(CrashLogWindows::current->crashdump_filename, filenamebuf, lengthof(filenamebuf))) + 2;
len += wcslen(convert_to_fs(CrashLogWindows::current->screenshot_filename, filenamebuf, lengthof(filenamebuf))) + 1;
wchar_t *text = AllocaM(wchar_t, len);
static wchar_t text[lengthof(_crash_desc) + 3 * MAX_PATH * 2 + 7];
int printed = _snwprintf(text, len, _crash_desc, convert_to_fs(CrashLogWindows::current->crashlog_filename, filenamebuf, lengthof(filenamebuf)));
if (printed < 0 || (size_t)printed > len) {
MessageBox(wnd, L"Catastrophic failure trying to display crash message. Could not perform text formatting.", L"OpenTTD", MB_ICONERROR);
@@ -729,7 +729,7 @@ static INT_PTR CALLBACK CrashDialogFunc(HWND wnd, UINT msg, WPARAM wParam, LPARA
if (CrashLogWindows::current->WriteSavegame(filename, lastof(filename))) {
convert_to_fs(filename, filenamebuf, lengthof(filenamebuf));
size_t len = lengthof(_save_succeeded) + wcslen(filenamebuf) + 1;
wchar_t *text = AllocaM(wchar_t, len);
static wchar_t text[lengthof(_save_succeeded) + MAX_PATH * 2 + 1];
_snwprintf(text, len, _save_succeeded, filenamebuf);
MessageBox(wnd, text, L"Save successful", MB_ICONINFORMATION);
} else {

View File

@@ -71,7 +71,6 @@ FT_Error GetFontByFaceName(const char *font_name, FT_Face *face)
HKEY hKey;
LONG ret;
wchar_t vbuffer[MAX_PATH], dbuffer[256];
wchar_t *pathbuf;
const char *font_path;
uint index;
size_t path_len;
@@ -122,12 +121,12 @@ FT_Error GetFontByFaceName(const char *font_name, FT_Face *face)
* contain multiple fonts inside this single file. GetFontData however
* returns the whole file, so we need to check each font inside to get the
* proper font. */
path_len = wcslen(vbuffer) + wcslen(dbuffer) + 2; // '\' and terminating nul.
pathbuf = AllocaM(wchar_t, path_len);
_snwprintf(pathbuf, path_len, L"%s\\%s", vbuffer, dbuffer);
std::wstring pathbuf(vbuffer);
pathbuf += L"\\";
pathbuf += dbuffer;
/* Convert the path into something that FreeType understands. */
font_path = GetShortPath(pathbuf);
font_path = GetShortPath(pathbuf.c_str());
index = 0;
do {
@@ -374,7 +373,7 @@ void Win32FontCache::SetFontSize(FontSize fs, int pixels)
HGDIOBJ old = SelectObject(this->dc, temp);
UINT size = GetOutlineTextMetrics(this->dc, 0, nullptr);
LPOUTLINETEXTMETRIC otm = (LPOUTLINETEXTMETRIC)AllocaM(BYTE, size);
LPOUTLINETEXTMETRIC otm = (LPOUTLINETEXTMETRIC)new BYTE[size];
GetOutlineTextMetrics(this->dc, size, otm);
/* Font height is minimum height plus the difference between the default
@@ -383,6 +382,7 @@ void Win32FontCache::SetFontSize(FontSize fs, int pixels)
/* Clamp() is not used as scaled_height could be greater than MAX_FONT_SIZE, which is not permitted in Clamp(). */
pixels = std::min(std::max(std::min<int>(otm->otmusMinimumPPEM, MAX_FONT_MIN_REC_SIZE) + diff, scaled_height), MAX_FONT_SIZE);
delete[] (BYTE*)otm;
SelectObject(dc, old);
DeleteObject(temp);
}
@@ -405,7 +405,7 @@ void Win32FontCache::SetFontSize(FontSize fs, int pixels)
/* Query the font metrics we needed. */
UINT otmSize = GetOutlineTextMetrics(this->dc, 0, nullptr);
POUTLINETEXTMETRIC otm = (POUTLINETEXTMETRIC)AllocaM(BYTE, otmSize);
POUTLINETEXTMETRIC otm = (POUTLINETEXTMETRIC)new BYTE[otmSize];
GetOutlineTextMetrics(this->dc, otmSize, otm);
this->units_per_em = otm->otmEMSquare;
@@ -418,6 +418,7 @@ void Win32FontCache::SetFontSize(FontSize fs, int pixels)
this->fontname = FS2OTTD((LPWSTR)((BYTE *)otm + (ptrdiff_t)otm->otmpFaceName));
Debug(fontcache, 2, "Loaded font '{}' with size {}", this->fontname, pixels);
delete[] (BYTE*)otm;
}
/**
@@ -449,7 +450,7 @@ void Win32FontCache::ClearFontCache()
if (width > MAX_GLYPH_DIM || height > MAX_GLYPH_DIM) usererror("Font glyph is too large");
/* Call GetGlyphOutline again with size to actually render the glyph. */
byte *bmp = AllocaM(byte, size);
byte *bmp = new byte[size];
GetGlyphOutline(this->dc, key, GGO_GLYPH_INDEX | (aa ? GGO_GRAY8_BITMAP : GGO_BITMAP), &gm, size, bmp, &mat);
/* GDI has rendered the glyph, now we allocate a sprite and copy the image into it. */
@@ -498,6 +499,8 @@ void Win32FontCache::ClearFontCache()
this->SetGlyphPtr(key, &new_glyph);
delete[] bmp;
return new_glyph.sprite;
}
@@ -589,10 +592,11 @@ void LoadWin32Font(FontSize fs)
/* Try to query an array of LOGFONTs that describe the file. */
DWORD len = 0;
if (GetFontResourceInfo(fontPath, &len, nullptr, 2) && len >= sizeof(LOGFONT)) {
LOGFONT *buf = (LOGFONT *)AllocaM(byte, len);
LOGFONT *buf = (LOGFONT *)new byte[len];
if (GetFontResourceInfo(fontPath, &len, buf, 2)) {
logfont = *buf; // Just use first entry.
}
delete[] (byte *)buf;
}
}

View File

@@ -544,10 +544,9 @@ std::string FS2OTTD(const std::wstring &name)
int name_len = (name.length() >= INT_MAX) ? INT_MAX : (int)name.length();
int len = WideCharToMultiByte(CP_UTF8, 0, name.c_str(), name_len, nullptr, 0, nullptr, nullptr);
if (len <= 0) return std::string();
char *utf8_buf = AllocaM(char, len + 1);
utf8_buf[len] = '\0';
WideCharToMultiByte(CP_UTF8, 0, name.c_str(), name_len, utf8_buf, len, nullptr, nullptr);
return std::string(utf8_buf, static_cast<size_t>(len));
std::string utf8_buf(len, '\0'); // len includes terminating null
WideCharToMultiByte(CP_UTF8, 0, name.c_str(), name_len, utf8_buf.data(), len, nullptr, nullptr);
return utf8_buf;
}
/**
@@ -562,10 +561,9 @@ std::wstring OTTD2FS(const std::string &name)
int name_len = (name.length() >= INT_MAX) ? INT_MAX : (int)name.length();
int len = MultiByteToWideChar(CP_UTF8, 0, name.c_str(), name_len, nullptr, 0);
if (len <= 0) return std::wstring();
wchar_t *system_buf = AllocaM(wchar_t, len + 1);
system_buf[len] = L'\0';
MultiByteToWideChar(CP_UTF8, 0, name.c_str(), name_len, system_buf, len);
return std::wstring(system_buf, static_cast<size_t>(len));
std::wstring system_buf(len, L'\0'); // len includes terminating null
MultiByteToWideChar(CP_UTF8, 0, name.c_str(), name_len, system_buf.data(), len);
return system_buf;
}
@@ -669,13 +667,13 @@ int OTTDStringCompare(const char *s1, const char *s2)
int len_s2 = MultiByteToWideChar(CP_UTF8, 0, s2, -1, nullptr, 0);
if (len_s1 != 0 && len_s2 != 0) {
LPWSTR str_s1 = AllocaM(WCHAR, len_s1);
LPWSTR str_s2 = AllocaM(WCHAR, len_s2);
std::wstring str_s1(len_s1, L'\0'); // len includes terminating null
std::wstring str_s2(len_s2, L'\0');
MultiByteToWideChar(CP_UTF8, 0, s1, -1, str_s1, len_s1);
MultiByteToWideChar(CP_UTF8, 0, s2, -1, str_s2, len_s2);
MultiByteToWideChar(CP_UTF8, 0, s1, -1, str_s1.data(), len_s1);
MultiByteToWideChar(CP_UTF8, 0, s2, -1, str_s2.data(), len_s2);
int result = _CompareStringEx(_cur_iso_locale, LINGUISTIC_IGNORECASE | SORT_DIGITSASNUMBERS, str_s1, -1, str_s2, -1, nullptr, nullptr, 0);
int result = _CompareStringEx(_cur_iso_locale, LINGUISTIC_IGNORECASE | SORT_DIGITSASNUMBERS, str_s1.c_str(), -1, str_s2.c_str(), -1, nullptr, nullptr, 0);
if (result != 0) return result;
}
}