Skip to content

Commit

Permalink
Merged pull request "Sync with Q2PRO: Memory allocation": NVIDIA#327
Browse files Browse the repository at this point in the history
  • Loading branch information
apanteleev committed Oct 3, 2023
2 parents 30d959e + 9dda3e7 commit c6fca4b
Show file tree
Hide file tree
Showing 28 changed files with 166 additions and 226 deletions.
4 changes: 3 additions & 1 deletion inc/common/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,9 @@ void Com_AddConfigFile(const char *name, unsigned flags);
#define Com_DDDDPrintf(...) ((void)0)
#endif

extern cvar_t *z_perturb;
#if USE_TESTS
extern cvar_t *z_perturb;
#endif

#if USE_DEBUG
extern cvar_t *developer;
Expand Down
12 changes: 4 additions & 8 deletions inc/common/zone.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#ifndef ZONE_H
#define ZONE_H

#define Z_Malloc(size) Z_TagMalloc(size, TAG_GENERAL)
#define Z_Mallocz(size) Z_TagMallocz(size, TAG_GENERAL)
#define Z_Reserve(size) Z_TagReserve(size, TAG_GENERAL)
#define Z_CopyString(string) Z_TagCopyString(string, TAG_GENERAL)
#define Z_CopyStruct(ptr) memcpy(Z_Malloc(sizeof(*ptr)), ptr, sizeof(*ptr))

Expand All @@ -47,19 +44,18 @@ typedef enum {

void Z_Init(void);
void Z_Free(void *ptr);
// Frees the memory block pointed at by (*ptr), if that's nonzero, and sets (*ptr) to zero.
void Z_Freep(void **ptr);
void *Z_Realloc(void *ptr, size_t size);
void *Z_Malloc(size_t size) q_malloc;
void *Z_Mallocz(size_t size) q_malloc;
void *Z_TagMalloc(size_t size, memtag_t tag) q_malloc;
void *Z_TagMallocz(size_t size, memtag_t tag) q_malloc;
char *Z_TagCopyString(const char *in, memtag_t tag) q_malloc;
void Z_FreeTags(memtag_t tag);
void Z_LeakTest(memtag_t tag);
void Z_Stats_f(void);

void Z_TagReserve(size_t size, memtag_t tag);
void *Z_ReservedAlloc(size_t size) q_malloc;
void *Z_ReservedAllocz(size_t size) q_malloc;
char *Z_ReservedCopyString(const char *in) q_malloc;

// may return pointer to static memory
char *Z_CvarCopyString(const char *in);

Expand Down
2 changes: 1 addition & 1 deletion inc/refresh/models.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "system/hunk.h"
#include "common/error.h"

#define MOD_Malloc(size) Hunk_Alloc(&model->hunk, size)
#define MOD_Malloc(size) Hunk_TryAlloc(&model->hunk, size)

#define CHECK(x) if (!(x)) { ret = Q_ERR(ENOMEM); goto fail; }

Expand Down
6 changes: 6 additions & 0 deletions inc/shared/list.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ static inline void List_Unlink(list_t *prev, list_t *next)
next->prev = prev;
}

static inline void List_Relink(list_t *elem)
{
elem->prev->next = elem;
elem->next->prev = elem;
}

static inline void List_Init(list_t *list)
{
list->prev = list->next = list;
Expand Down
1 change: 1 addition & 0 deletions inc/system/hunk.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ typedef struct {

void Hunk_Init(void);
void Hunk_Begin(memhunk_t *hunk, size_t maxsize);
void *Hunk_TryAlloc(memhunk_t *hunk, size_t size);
void *Hunk_Alloc(memhunk_t *hunk, size_t size);
void Hunk_End(memhunk_t *hunk);
void Hunk_Free(memhunk_t *hunk);
Expand Down
13 changes: 3 additions & 10 deletions src/client/http.c
Original file line number Diff line number Diff line change
Expand Up @@ -405,10 +405,7 @@ void HTTP_CleanupDownloads(void)
dl->file = NULL;
}

if (dl->buffer) {
Z_Free(dl->buffer);
dl->buffer = NULL;
}
Z_Freep((void**)&dl->buffer);

if (dl->curl) {
if (curl_multi && dl->multi_added)
Expand Down Expand Up @@ -682,8 +679,7 @@ static void parse_file_list(dlhandle_t *dl)
}
}

Z_Free(dl->buffer);
dl->buffer = NULL;
Z_Freep((void**)&dl->buffer);
}

// A pak file just downloaded, let's see if we can remove some stuff from
Expand Down Expand Up @@ -830,10 +826,7 @@ static bool finish_download(void)
remove(dl->path);
dl->path[0] = 0;
}
if (dl->buffer) {
Z_Free(dl->buffer);
dl->buffer = NULL;
}
Z_Freep((void**)&dl->buffer);
if (dl->multi_added) {
//remove the handle and mark it as such
curl_multi_remove_handle(curl_multi, curl);
Expand Down
6 changes: 2 additions & 4 deletions src/client/sound/ogg.c
Original file line number Diff line number Diff line change
Expand Up @@ -379,8 +379,7 @@ void
OGG_LoadTrackList(void)
{
tracklist_free();
Z_Free(ogg.music_dir);
ogg.music_dir = NULL;
Z_Freep((void**)&ogg.music_dir);

const char* potMusicDirs[4] = {0};
char fullMusicDir[MAX_OSPATH] = {0};
Expand Down Expand Up @@ -796,8 +795,7 @@ OGG_Shutdown(void)
// Free file lsit.
tracklist_free();

Z_Free(ogg.music_dir);
ogg.music_dir = NULL;
Z_Freep((void**)&ogg.music_dir);

// Remove console commands
Cmd_RemoveCommand("ogg");
Expand Down
3 changes: 1 addition & 2 deletions src/client/ui/demos.c
Original file line number Diff line number Diff line change
Expand Up @@ -366,8 +366,7 @@ static void FreeList(void)
for (i = 0; i < m_demos.list.numItems; i++) {
Z_Free(m_demos.list.items[i]);
}
Z_Free(m_demos.list.items);
m_demos.list.items = NULL;
Z_Freep((void**)&m_demos.list.items);
m_demos.list.numItems = 0;
}
}
Expand Down
5 changes: 1 addition & 4 deletions src/client/ui/servers.c
Original file line number Diff line number Diff line change
Expand Up @@ -1052,10 +1052,7 @@ static bool Push(menuFrameWork_t *self)
static void Pop(menuFrameWork_t *self)
{
ClearServers();
if (m_servers.args) {
Z_Free(m_servers.args);
m_servers.args = NULL;
}
Z_Freep((void**)&m_servers.args);
}

static void Expose(menuFrameWork_t *self)
Expand Down
4 changes: 4 additions & 0 deletions src/common/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ static bool com_logNewline;
static char **com_argv;
static int com_argc;

#if USE_TESTS
cvar_t *z_perturb;
#endif

#if USE_DEBUG
cvar_t *developer;
Expand Down Expand Up @@ -887,7 +889,9 @@ void Qcommon_Init(int argc, char **argv)
//
// init commands and vars
//
#if USE_TESTS
z_perturb = Cvar_Get("z_perturb", "0", 0);
#endif
#if USE_CLIENT
host_speeds = Cvar_Get("host_speeds", "0", 0);
#endif
Expand Down
10 changes: 2 additions & 8 deletions src/common/cvar.c
Original file line number Diff line number Diff line change
Expand Up @@ -328,10 +328,7 @@ static void set_back_cvar(cvar_t *var)
{
if (var->flags & CVAR_LATCH) {
// set back to current value
if (var->latched_string) {
Z_Free(var->latched_string);
var->latched_string = NULL;
}
Z_Freep((void**)&var->latched_string);
}
}

Expand Down Expand Up @@ -390,10 +387,7 @@ void Cvar_SetByVar(cvar_t *var, const char *value, from_t from)
}

// free latched string, if any
if (var->latched_string) {
Z_Free(var->latched_string);
var->latched_string = NULL;
}
Z_Freep((void**)&var->latched_string);

change_string_value(var, value, from);
}
Expand Down
21 changes: 12 additions & 9 deletions src/common/net/chan.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ static cvar_t *showdrop;
#define SHOWDROP(...)
#endif

#define SOCK_TAG(sock) ((sock) == NS_SERVER ? TAG_SERVER : TAG_GENERAL)

cvar_t *net_qport;
cvar_t *net_maxmsglen;
cvar_t *net_chantype;
Expand Down Expand Up @@ -398,11 +400,7 @@ static netchan_t *NetchanOld_Setup(netsrc_t sock, const netadr_t *adr,
netchan_old_t *chan;
netchan_t *netchan;

Z_TagReserve(sizeof(*chan) + maxpacketlen * 2,
sock == NS_SERVER ? TAG_SERVER : TAG_GENERAL);

chan = Z_ReservedAlloc(sizeof(*chan));
memset(chan, 0, sizeof(*chan));
chan = Z_TagMallocz(sizeof(*chan), SOCK_TAG(sock));
netchan = (netchan_t *)chan;
netchan->sock = sock;
netchan->remote_address = *adr;
Expand All @@ -418,10 +416,10 @@ static netchan_t *NetchanOld_Setup(netsrc_t sock, const netadr_t *adr,
netchan->TransmitNextFragment = NetchanOld_TransmitNextFragment;
netchan->ShouldUpdate = NetchanOld_ShouldUpdate;

chan->message_buf = Z_ReservedAlloc(maxpacketlen);
chan->message_buf = Z_TagMalloc(maxpacketlen, SOCK_TAG(sock));
SZ_Init(&netchan->message, chan->message_buf, maxpacketlen);

chan->reliable_buf = Z_ReservedAlloc(maxpacketlen);
chan->reliable_buf = Z_TagMalloc(maxpacketlen, SOCK_TAG(sock));

return netchan;
}
Expand Down Expand Up @@ -800,8 +798,7 @@ static netchan_t *NetchanNew_Setup(netsrc_t sock, const netadr_t *adr,
netchan_new_t *chan;
netchan_t *netchan;

chan = Z_TagMallocz(sizeof(*chan),
sock == NS_SERVER ? TAG_SERVER : TAG_GENERAL);
chan = Z_TagMallocz(sizeof(*chan), SOCK_TAG(sock));
netchan = (netchan_t *)chan;
netchan->sock = sock;
netchan->remote_address = *adr;
Expand Down Expand Up @@ -865,6 +862,12 @@ Netchan_Close
*/
void Netchan_Close(netchan_t *netchan)
{
if (netchan->type == NETCHAN_OLD) {
netchan_old_t *chan = (netchan_old_t *)netchan;

Z_Free(chan->message_buf);
Z_Free(chan->reliable_buf);
}
Z_Free(netchan);
}

10 changes: 2 additions & 8 deletions src/common/prompt.c
Original file line number Diff line number Diff line change
Expand Up @@ -415,10 +415,7 @@ void Prompt_CompleteHistory(commandPrompt_t *prompt, bool forward)
void Prompt_ClearState(commandPrompt_t *prompt)
{
prompt->tooMany = false;
if (prompt->search) {
Z_Free(prompt->search);
prompt->search = NULL;
}
Z_Freep((void**)&prompt->search);
}

/*
Expand Down Expand Up @@ -522,10 +519,7 @@ void Prompt_Clear(commandPrompt_t *prompt)
Prompt_ClearState(prompt);

for (i = 0; i < HISTORY_SIZE; i++) {
if (prompt->history[i]) {
Z_Free(prompt->history[i]);
prompt->history[i] = NULL;
}
Z_Freep((void**)&prompt->history[i]);
}

prompt->historyLineNum = 0;
Expand Down
Loading

0 comments on commit c6fca4b

Please sign in to comment.