From 840283d6e50e1b1dd2a25fe3195bb363d493be85 Mon Sep 17 00:00:00 2001 From: SNMetamorph <25657591+SNMetamorph@users.noreply.github.com> Date: Fri, 9 Dec 2022 17:33:39 +0400 Subject: [PATCH] engine: netchan: fixed fragbufs very high memory usage --- engine/common/net_chan.c | 16 +++++++++------- engine/common/netchan.h | 2 +- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/engine/common/net_chan.c b/engine/common/net_chan.c index 5662fe49..ba000172 100644 --- a/engine/common/net_chan.c +++ b/engine/common/net_chan.c @@ -414,6 +414,7 @@ void Netchan_ClearFragbufs( fragbuf_t **ppbuf ) while( buf ) { n = buf->next; + Mem_Free( buf->frag_message_buf ); Mem_Free( buf ); buf = n; } @@ -535,12 +536,13 @@ Netchan_AllocFragbuf ============================== */ -fragbuf_t *Netchan_AllocFragbuf( void ) +fragbuf_t *Netchan_AllocFragbuf( int fragment_size ) { fragbuf_t *buf; buf = (fragbuf_t *)Mem_Calloc( net_mempool, sizeof( fragbuf_t )); - MSG_Init( &buf->frag_message, "Frag Message", buf->frag_message_buf, sizeof( buf->frag_message_buf )); + buf->frag_message_buf = (byte *)Mem_Calloc( net_mempool, fragment_size ); + MSG_Init( &buf->frag_message, "Frag Message", buf->frag_message_buf, fragment_size ); return buf; } @@ -736,7 +738,7 @@ static void Netchan_CreateFragments_( netchan_t *chan, sizebuf_t *msg ) bytes = Q_min( remaining, chunksize ); remaining -= bytes; - buf = Netchan_AllocFragbuf(); + buf = Netchan_AllocFragbuf( bytes ); buf->bufferid = bufferid++; // Copy in data @@ -803,7 +805,7 @@ fragbuf_t *Netchan_FindBufferById( fragbuf_t **pplist, int id, qboolean allocate return NULL; // create new entry - pnewbuf = Netchan_AllocFragbuf(); + pnewbuf = Netchan_AllocFragbuf( NET_MAX_FRAGMENT ); pnewbuf->bufferid = id; Netchan_AddBufferToList( pplist, pnewbuf ); @@ -894,7 +896,7 @@ void Netchan_CreateFileFragmentsFromBuffer( netchan_t *chan, const char *filenam { send = Q_min( remaining, chunksize ); - buf = Netchan_AllocFragbuf(); + buf = Netchan_AllocFragbuf( send ); buf->bufferid = bufferid++; // copy in data @@ -959,7 +961,7 @@ int Netchan_CreateFileFragments( netchan_t *chan, const char *filename ) qboolean bCompressed = false; fragbufwaiting_t *wait, *p; fragbuf_t *buf; - + if(( filesize = FS_FileSize( filename, false )) <= 0 ) { Con_Printf( S_WARN "Unable to open %s for transfer\n", filename ); @@ -1013,7 +1015,7 @@ int Netchan_CreateFileFragments( netchan_t *chan, const char *filename ) { send = Q_min( remaining, chunksize ); - buf = Netchan_AllocFragbuf(); + buf = Netchan_AllocFragbuf( send ); buf->bufferid = bufferid++; // copy in data diff --git a/engine/common/netchan.h b/engine/common/netchan.h index daed9f31..3acc28bb 100644 --- a/engine/common/netchan.h +++ b/engine/common/netchan.h @@ -185,7 +185,7 @@ typedef struct fragbuf_s struct fragbuf_s *next; // next buffer in chain int bufferid; // id of this buffer sizebuf_t frag_message; // message buffer where raw data is stored - byte frag_message_buf[NET_MAX_FRAGMENT]; // the actual data sits here + byte *frag_message_buf; // the actual data sits here qboolean isfile; // is this a file buffer? qboolean isbuffer; // is this file buffer from memory ( custom decal, etc. ). qboolean iscompressed; // is compressed file, we should using filename.ztmp