Replace Global* memory functions

Replace Global* memory functions with mem_* equivalents from mem.h, remove compatibility functions on Linux / macOS platforms.
This commit is contained in:
Azamat H. Hackimov
2024-06-12 23:37:55 +03:00
parent c8fa74550a
commit c41c3a7cb1
8 changed files with 33 additions and 59 deletions

View File

@@ -1872,11 +1872,11 @@ void FlushDataCache() {
soundsfreed++;
int index = Sounds[i].sample_index;
if (SoundFiles[index].sample_16bit) {
GlobalFree(SoundFiles[index].sample_16bit);
mem_free(SoundFiles[index].sample_16bit);
SoundFiles[index].sample_16bit = NULL;
}
if (SoundFiles[index].sample_8bit) {
GlobalFree(SoundFiles[index].sample_8bit);
mem_free(SoundFiles[index].sample_8bit);
SoundFiles[index].sample_8bit = NULL;
}
}

View File

@@ -188,15 +188,16 @@
* $NoKeywords: $
*/
#include "d3_version.h"
#include "Debug.h"
#include "mono.h"
#include <windows.h>
#include <cstdarg>
#include <cstdlib>
#include <cstdint>
#include "d3_version.h"
#include "debug.h"
#include "mem.h"
#include "mono.h"
///////////////////////////////////////////////////////////////////////////////
bool Debug_break = false;
@@ -247,8 +248,8 @@ int Debug_ErrorBox(int type, const char *title, const char *topstring, const cha
else
debug_break();
char *tmpbuf = (char *)GlobalAlloc(
GMEM_FIXED, strlen(dumptext) + strlen(topstring) + strlen(bottomstring) +
char *tmpbuf = (char *)mem_malloc(
strlen(dumptext) + strlen(topstring) + strlen(bottomstring) +
10); // malloc(strlen(dumptext) + strlen(topstring) + strlen(bottomstring) + 10);
strcpy(tmpbuf, topstring);
@@ -268,7 +269,7 @@ int Debug_ErrorBox(int type, const char *title, const char *topstring, const cha
ShowCursor(FALSE);
// free(tmpbuf);
GlobalFree(tmpbuf);
mem_free(tmpbuf);
// ShowWindow(wnd, SW_SHOWMAXIMIZED);
@@ -936,10 +937,9 @@ void DumpTextToClipboard(char *text) {
// Length of string with CRs added
int len = strlen(text) + extra + 1;
HGLOBAL h_text = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, len);
char *h_text = (char *)mem_malloc(len);
if (!h_text)
return;
ptr = (char *)GlobalLock(h_text);
if (!ptr)
return;

View File

@@ -25,9 +25,6 @@
#define HGLOBAL void *
void GlobalFree(void *);
void *GlobalAlloc(int flags, int size);
void *GlobalLock(HGLOBAL hMem);
void Sleep(int millis);
char *strupr(char *string);

View File

@@ -90,19 +90,6 @@ bool con_Create(int flags);
void con_Destroy();
void con_Defer();
void GlobalFree(void *mptr) {
if (mptr)
free(mptr);
}
void *GlobalAlloc(int flags, int size) {
if (size <= 0)
return nullptr;
return malloc(size);
}
void *GlobalLock(HGLOBAL hMem) { return hMem; }
void Sleep(int millis) {
struct timeval tv{};
tv.tv_sec = 0;

View File

@@ -86,21 +86,13 @@
* $NoKeywords: $
*/
// yes this is a low-level library for Windows, but no use duplicating code
// when we can fake the OS-specific functions (GlobalAlloc, etc)
#if defined(WIN32)
#include "windows.h"
#include "winbase.h"
#elif defined(__LINUX__)
#include "linux_fix.h"
#endif
#include <cstdlib>
#include <cstring>
#include "ssl_lib.h"
#include <cstdlib>
#include "cfile.h"
#include "mem.h"
#include "pserror.h"
#include <string.h>
#include "byteswap.h"
#include "gamesequence.h"
@@ -378,8 +370,7 @@ char SoundLoadWaveFile(const char *filename, float percent_volume, int sound_fil
SoundFiles[sound_file_index].sample_length = aligned_size;
SoundFiles[sound_file_index].np_sample_length = cksize;
SoundFiles[sound_file_index].sample_8bit = (uint8_t *)GlobalAlloc(0, aligned_size);
GlobalLock(SoundFiles[sound_file_index].sample_8bit);
SoundFiles[sound_file_index].sample_8bit = (uint8_t *)mem_malloc(aligned_size);
cf_ReadBytes((uint8_t *)SoundFiles[sound_file_index].sample_8bit, cksize, cfptr);
@@ -398,8 +389,7 @@ char SoundLoadWaveFile(const char *filename, float percent_volume, int sound_fil
SoundFiles[sound_file_index].sample_length = cksize / 2 + num_needed / 2;
}
SoundFiles[sound_file_index].sample_16bit = (int16_t *)GlobalAlloc(0, cksize + num_needed);
GlobalLock(SoundFiles[sound_file_index].sample_16bit);
SoundFiles[sound_file_index].sample_16bit = (int16_t *)mem_malloc(cksize + num_needed);
cf_ReadBytes((uint8_t *)SoundFiles[sound_file_index].sample_16bit, cksize, cfptr);
for (count = 0; count < (int)cksize / 2; count++) {
SoundFiles[sound_file_index].sample_16bit[count] =
@@ -447,8 +437,7 @@ char SoundLoadWaveFile(const char *filename, float percent_volume, int sound_fil
} else if (SoundFiles[sound_file_index].sample_8bit == NULL && !f_high_quality) {
SoundFiles[sound_file_index].sample_8bit =
(uint8_t *)GlobalAlloc(0, SoundFiles[sound_file_index].sample_length);
GlobalLock(SoundFiles[sound_file_index].sample_8bit);
(uint8_t *)mem_malloc(SoundFiles[sound_file_index].sample_length);
// Do the volume clipping with the high quality sound
for (count = 0; count < (int)SoundFiles[sound_file_index].sample_length; count++) {
@@ -462,13 +451,12 @@ char SoundLoadWaveFile(const char *filename, float percent_volume, int sound_fil
(uint8_t)((((int)SoundFiles[sound_file_index].sample_16bit[count]) + 32767) >> 8);
}
GlobalFree(SoundFiles[sound_file_index].sample_16bit);
mem_free(SoundFiles[sound_file_index].sample_16bit);
SoundFiles[sound_file_index].sample_16bit = NULL;
} else if (SoundFiles[sound_file_index].sample_16bit == NULL && f_high_quality) {
SoundFiles[sound_file_index].sample_16bit =
(int16_t *)GlobalAlloc(0, SoundFiles[sound_file_index].sample_length * sizeof(int16_t));
GlobalLock(SoundFiles[sound_file_index].sample_16bit);
(int16_t *)mem_malloc(SoundFiles[sound_file_index].sample_length * sizeof(int16_t));
// NOTE: Interesting note on sound conversion: 16 bit sounds are signed (0 biase). 8 bit sounds are unsigned
// (+128 biase).
@@ -481,7 +469,7 @@ char SoundLoadWaveFile(const char *filename, float percent_volume, int sound_fil
SoundFiles[sound_file_index].sample_16bit[count] *= CLIP_ATTENUATION * percent_volume;
}
GlobalFree(SoundFiles[sound_file_index].sample_8bit);
mem_free(SoundFiles[sound_file_index].sample_8bit);
SoundFiles[sound_file_index].sample_8bit = NULL;
}
@@ -498,12 +486,12 @@ error_state:
cfclose(cfptr);
if (SoundFiles[sound_file_index].sample_8bit) {
GlobalFree(SoundFiles[sound_file_index].sample_8bit);
mem_free(SoundFiles[sound_file_index].sample_8bit);
SoundFiles[sound_file_index].sample_8bit = NULL;
}
if (SoundFiles[sound_file_index].sample_16bit) {
GlobalFree(SoundFiles[sound_file_index].sample_16bit);
mem_free(SoundFiles[sound_file_index].sample_16bit);
SoundFiles[sound_file_index].sample_16bit = NULL;
}
@@ -515,10 +503,10 @@ void SoundLoadFree(int sound_file_index) {
if (SoundFiles[i].used != 0) {
if (SoundFiles[i].sample_8bit)
GlobalFree(SoundFiles[i].sample_8bit);
mem_free(SoundFiles[i].sample_8bit);
if (SoundFiles[i].sample_16bit)
GlobalFree(SoundFiles[i].sample_16bit);
mem_free(SoundFiles[i].sample_16bit);
}
SoundFiles[i].sample_8bit = NULL;

View File

@@ -2648,7 +2648,7 @@ bool win_llsSystem::SetSoundQuality(char quality) {
int j = Sounds[i].sample_index;
if (SoundFiles[j].sample_8bit && m_sound_quality == SQT_HIGH) {
GlobalFree(SoundFiles[j].sample_8bit);
mem_free(SoundFiles[j].sample_8bit);
SoundFiles[j].sample_8bit = NULL;
CheckAndForceSoundDataAlloc(i);
@@ -2657,7 +2657,7 @@ bool win_llsSystem::SetSoundQuality(char quality) {
int count;
ASSERT(SoundFiles[j].sample_8bit == NULL);
SoundFiles[j].sample_8bit = (uint8_t *)GlobalAlloc(0, SoundFiles[j].sample_length);
SoundFiles[j].sample_8bit = (uint8_t *)mem_malloc(SoundFiles[j].sample_length);
// NOTE: Interesting note on sound conversion: 16 bit sounds are signed (0 biase). 8 bit sounds are unsigned
// (+128 biase).
@@ -2665,7 +2665,7 @@ bool win_llsSystem::SetSoundQuality(char quality) {
SoundFiles[j].sample_8bit[count] = (uint8_t)((((int)SoundFiles[j].sample_16bit[count]) + 32767) >> 8);
}
GlobalFree(SoundFiles[j].sample_16bit);
mem_free(SoundFiles[j].sample_16bit);
SoundFiles[j].sample_16bit = NULL;
}
}

View File

@@ -24,6 +24,7 @@
#include <SDL.h>
#include "pserror.h"
#include "mem.h"
#include "mono.h"
#include "ssl_lib.h"
#include "application.h"
@@ -164,7 +165,7 @@ bool lnxsound::SetSoundQuality(char quality) {
int j = Sounds[i].sample_index;
if (SoundFiles[j].sample_8bit && m_sound_quality == SQT_HIGH) {
GlobalFree(SoundFiles[j].sample_8bit);
mem_free(SoundFiles[j].sample_8bit);
SoundFiles[j].sample_8bit = nullptr;
CheckAndForceSoundDataAlloc(i);
@@ -173,7 +174,7 @@ bool lnxsound::SetSoundQuality(char quality) {
int count;
ASSERT(SoundFiles[j].sample_8bit == nullptr);
SoundFiles[j].sample_8bit = (uint8_t *)GlobalAlloc(0, SoundFiles[j].sample_length);
SoundFiles[j].sample_8bit = (uint8_t *)mem_malloc(SoundFiles[j].sample_length);
// NOTE: Interesting note on sound conversion: 16 bit sounds are signed (0 biase). 8 bit sounds are unsigned
// (+128 biase).
@@ -181,7 +182,7 @@ bool lnxsound::SetSoundQuality(char quality) {
SoundFiles[j].sample_8bit[count] = (uint8_t)((((int)SoundFiles[j].sample_16bit[count]) + 32767) >> 8);
}
GlobalFree(SoundFiles[j].sample_16bit);
mem_free(SoundFiles[j].sample_16bit);
SoundFiles[j].sample_16bit = nullptr;
}
}

View File

@@ -233,6 +233,7 @@
#include "ssl_lib.h"
#include "object.h"
#include "ddio.h"
#include "mem.h"
#include "soundload.h"
#include "weapon.h"
#include "ship.h"
@@ -323,11 +324,11 @@ void FreeSoundFile(int n) {
SoundFiles[n].used = 0;
SoundFiles[n].name[0] = 0;
if (SoundFiles[n].sample_8bit) {
GlobalFree(SoundFiles[n].sample_8bit);
mem_free(SoundFiles[n].sample_8bit);
SoundFiles[n].sample_8bit = NULL;
}
if (SoundFiles[n].sample_16bit) {
GlobalFree(SoundFiles[n].sample_16bit);
mem_free(SoundFiles[n].sample_16bit);
SoundFiles[n].sample_16bit = NULL;
}
Num_sound_files--;