From 701925453d9a40b422e3f29fd13e30fe59a9b572 Mon Sep 17 00:00:00 2001
From: havoc <havoc@d7cf8633-e32d-0410-b094-e92efae38249>
Date: Tue, 5 Apr 2005 18:23:53 +0000
Subject: [PATCH] consolidated many mempools to make memlist more readable (and
 very slightly reduce memory usage) changed gltexture_t->identifier from a
 pointer to an array to reduce the number of allocations per texture (now 2-3
 rather than 3-4) made all MAX_EDICTS arrays in entityframe5_database_t
 adaptive size to reduce server memory usage

git-svn-id: svn://svn.icculus.org/twilight/trunk/darkplaces@5158 d7cf8633-e32d-0410-b094-e92efae38249
---
 cl_main.c      | 55 +++++++++++++++-------------
 cl_parse.c     |  8 ++---
 cl_particles.c |  9 +----
 cl_video.c     | 39 +++++++++-----------
 client.h       |  7 ++--
 common.c       | 19 ----------
 common.h       |  3 --
 console.c      |  6 +---
 fs.c           | 22 +++++-------
 gl_backend.c   |  4 ---
 gl_textures.c  | 52 ++++-----------------------
 host.c         | 11 +++---
 host_cmd.c     |  8 ++---
 image.c        | 36 ++++++++++---------
 menu.c         | 11 ++----
 meshqueue.c    |  9 ++---
 netconn.c      | 12 ++++---
 pr_cmds.c      |  9 ++---
 pr_edict.c     | 38 +++++++++++++-------
 progs.h        |  6 +++-
 progsvm.h      | 59 +++++++++++++++---------------
 protocol.c     | 43 ++++++++++++++++++++--
 protocol.h     | 16 +++++----
 prvm_edict.c   | 97 +++++++++++++++++++++++---------------------------
 r_shadow.c     |  2 +-
 server.h       |  3 +-
 sv_main.c      | 34 +++++++++---------
 ui.c           | 19 +++++-----
 wad.c          |  4 +--
 29 files changed, 288 insertions(+), 353 deletions(-)

diff --git a/cl_main.c b/cl_main.c
index 7ec57c22..f9897135 100644
--- a/cl_main.c
+++ b/cl_main.c
@@ -63,8 +63,7 @@ cvar_t cl_noplayershadow = {CVAR_SAVE, "cl_noplayershadow", "0"};
 
 cvar_t cl_prydoncursor = {0, "cl_prydoncursor", "0"};
 
-mempool_t *cl_refdef_mempool;
-mempool_t *cl_entities_mempool;
+mempool_t *cl_mempool;
 
 client_static_t	cls;
 client_state_t	cl;
@@ -103,12 +102,22 @@ void CL_ClearState(void)
 {
 	int i;
 
+	if (cl_entities) Mem_Free(cl_entities);cl_entities = NULL;
+	if (cl_entities_active) Mem_Free(cl_entities_active);cl_entities_active = NULL;
+	if (cl_static_entities) Mem_Free(cl_static_entities);cl_static_entities = NULL;
+	if (cl_temp_entities) Mem_Free(cl_temp_entities);cl_temp_entities = NULL;
+	if (cl_effects) Mem_Free(cl_effects);cl_effects = NULL;
+	if (cl_beams) Mem_Free(cl_beams);cl_beams = NULL;
+	if (cl_dlights) Mem_Free(cl_dlights);cl_dlights = NULL;
+	if (cl_lightstyle) Mem_Free(cl_lightstyle);cl_lightstyle = NULL;
+	if (cl_brushmodel_entities) Mem_Free(cl_brushmodel_entities);cl_brushmodel_entities = NULL;
+	if (cl.entitydatabase) EntityFrame_FreeDatabase(cl.entitydatabase);cl.entitydatabase = NULL;
+	if (cl.entitydatabase4) EntityFrame4_FreeDatabase(cl.entitydatabase4);cl.entitydatabase4 = NULL;
+	if (cl.scores) Mem_Free(cl.scores);cl.scores = NULL;
+
 	if (!sv.active)
 		Host_ClearMemory ();
 
-	// note: this also gets rid of the entity database
-	Mem_EmptyPool(cl_entities_mempool);
-
 // wipe the entire cl structure
 	memset (&cl, 0, sizeof(cl));
 	// reset the view zoom interpolation
@@ -131,15 +140,15 @@ void CL_ClearState(void)
 	cl_max_lightstyle = MAX_LIGHTSTYLES;
 	cl_max_brushmodel_entities = MAX_EDICTS;
 
-	cl_entities = Mem_Alloc(cl_entities_mempool, cl_max_entities * sizeof(entity_t));
-	cl_entities_active = Mem_Alloc(cl_entities_mempool, cl_max_entities * sizeof(qbyte));
-	cl_static_entities = Mem_Alloc(cl_entities_mempool, cl_max_static_entities * sizeof(entity_t));
-	cl_temp_entities = Mem_Alloc(cl_entities_mempool, cl_max_temp_entities * sizeof(entity_t));
-	cl_effects = Mem_Alloc(cl_entities_mempool, cl_max_effects * sizeof(cl_effect_t));
-	cl_beams = Mem_Alloc(cl_entities_mempool, cl_max_beams * sizeof(beam_t));
-	cl_dlights = Mem_Alloc(cl_entities_mempool, cl_max_dlights * sizeof(dlight_t));
-	cl_lightstyle = Mem_Alloc(cl_entities_mempool, cl_max_lightstyle * sizeof(lightstyle_t));
-	cl_brushmodel_entities = Mem_Alloc(cl_entities_mempool, cl_max_brushmodel_entities * sizeof(entity_render_t *));
+	cl_entities = Mem_Alloc(cl_mempool, cl_max_entities * sizeof(entity_t));
+	cl_entities_active = Mem_Alloc(cl_mempool, cl_max_brushmodel_entities * sizeof(qbyte));
+	cl_static_entities = Mem_Alloc(cl_mempool, cl_max_static_entities * sizeof(entity_t));
+	cl_temp_entities = Mem_Alloc(cl_mempool, cl_max_temp_entities * sizeof(entity_t));
+	cl_effects = Mem_Alloc(cl_mempool, cl_max_effects * sizeof(cl_effect_t));
+	cl_beams = Mem_Alloc(cl_mempool, cl_max_beams * sizeof(beam_t));
+	cl_dlights = Mem_Alloc(cl_mempool, cl_max_dlights * sizeof(dlight_t));
+	cl_lightstyle = Mem_Alloc(cl_mempool, cl_max_lightstyle * sizeof(lightstyle_t));
+	cl_brushmodel_entities = Mem_Alloc(cl_mempool, cl_max_brushmodel_entities * sizeof(entity_render_t *));
 
 	// LordHavoc: have to set up the baseline info for alpha and other stuff
 	for (i = 0;i < cl_max_entities;i++)
@@ -165,7 +174,7 @@ void CL_ExpandEntities(int num)
 		oldmaxentities = cl_max_entities;
 		oldentities = cl_entities;
 		cl_max_entities = (num & ~255) + 256;
-		cl_entities = Mem_Alloc(cl_entities_mempool, cl_max_entities * sizeof(entity_t));
+		cl_entities = Mem_Alloc(cl_mempool, cl_max_entities * sizeof(entity_t));
 		memcpy(cl_entities, oldentities, oldmaxentities * sizeof(entity_t));
 		Mem_Free(oldentities);
 		for (i = oldmaxentities;i < cl_max_entities;i++)
@@ -1335,10 +1344,7 @@ void CL_Shutdown (void)
 	CL_Particles_Shutdown();
 	CL_Parse_Shutdown();
 
-	SZ_Free (&cls.message);
-
-	Mem_FreePool (&cl_entities_mempool);
-	Mem_FreePool (&cl_refdef_mempool);
+	Mem_FreePool (&cl_mempool);
 }
 
 /*
@@ -1348,18 +1354,19 @@ CL_Init
 */
 void CL_Init (void)
 {
-	cl_entities_mempool = Mem_AllocPool("client entities", 0, NULL);
-	cl_refdef_mempool = Mem_AllocPool("refdef", 0, NULL);
+	cl_mempool = Mem_AllocPool("client", 0, NULL);
 
 	memset(&r_refdef, 0, sizeof(r_refdef));
 	// max entities sent to renderer per frame
 	r_refdef.maxentities = MAX_EDICTS + 256 + 512;
-	r_refdef.entities = Mem_Alloc(cl_refdef_mempool, sizeof(entity_render_t *) * r_refdef.maxentities);
+	r_refdef.entities = Mem_Alloc(cl_mempool, sizeof(entity_render_t *) * r_refdef.maxentities);
 	// 256k drawqueue buffer
 	r_refdef.maxdrawqueuesize = 256 * 1024;
-	r_refdef.drawqueue = Mem_Alloc(cl_refdef_mempool, r_refdef.maxdrawqueuesize);
+	r_refdef.drawqueue = Mem_Alloc(cl_mempool, r_refdef.maxdrawqueuesize);
 
-	SZ_Alloc (&cls.message, 1024, "cls.message");
+	cls.message.data = cls.message_buf;
+	cls.message.maxsize = sizeof(cls.message_buf);
+	cls.message.cursize = 0;
 
 	CL_InitInput ();
 
diff --git a/cl_parse.c b/cl_parse.c
index 3587de0f..43b82068 100644
--- a/cl_parse.c
+++ b/cl_parse.c
@@ -99,8 +99,6 @@ char *svc_strings[128] =
 cvar_t demo_nehahra = {0, "demo_nehahra", "0"};
 cvar_t developer_networkentities = {0, "developer_networkentities", "0"};
 
-mempool_t *cl_scores_mempool;
-
 /*
 ==================
 CL_ParseStartSoundPacket
@@ -337,6 +335,7 @@ void CL_ParseServerInfo (void)
 	entity_t *ent;
 
 	Con_DPrint("Serverinfo packet received.\n");
+
 //
 // wipe the client_state_t struct
 //
@@ -362,8 +361,7 @@ void CL_ParseServerInfo (void)
 		Host_Error("Bad maxclients (%u) from server\n", cl.maxclients);
 		return;
 	}
-	Mem_EmptyPool(cl_scores_mempool);
-	cl.scores = Mem_Alloc(cl_scores_mempool, cl.maxclients*sizeof(*cl.scores));
+	cl.scores = Mem_Alloc(cl_mempool, cl.maxclients*sizeof(*cl.scores));
 
 // parse gametype
 	cl.gametype = MSG_ReadByte ();
@@ -1775,7 +1773,6 @@ void CL_Parse_DumpPacket(void)
 void CL_Parse_Init(void)
 {
 	// LordHavoc: added demo_nehahra cvar
-	cl_scores_mempool = Mem_AllocPool("client player info", 0, NULL);
 	Cvar_RegisterVariable (&demo_nehahra);
 	if (gamemode == GAME_NEHAHRA)
 		Cvar_SetValue("demo_nehahra", 1);
@@ -1784,5 +1781,4 @@ void CL_Parse_Init(void)
 
 void CL_Parse_Shutdown(void)
 {
-	Mem_FreePool (&cl_scores_mempool);
 }
diff --git a/cl_particles.c b/cl_particles.c
index 3dba711f..f125cf2c 100644
--- a/cl_particles.c
+++ b/cl_particles.c
@@ -324,10 +324,6 @@ cvar_t cl_decals = {CVAR_SAVE, "cl_decals", "0"};
 cvar_t cl_decals_time = {CVAR_SAVE, "cl_decals_time", "0"};
 cvar_t cl_decals_fadetime = {CVAR_SAVE, "cl_decals_fadetime", "20"};
 
-#ifndef WORKINGLQUAKE
-static mempool_t *cl_part_mempool;
-#endif
-
 void CL_Particles_Clear(void)
 {
 	cl_numparticles = 0;
@@ -383,8 +379,7 @@ void CL_Particles_Init (void)
 #ifdef WORKINGLQUAKE
 	particles = (particle_t *) Hunk_AllocName(cl_maxparticles * sizeof(particle_t), "particles");
 #else
-	cl_part_mempool = Mem_AllocPool("CL_Part", 0, NULL);
-	particles = (particle_t *) Mem_Alloc(cl_part_mempool, cl_maxparticles * sizeof(particle_t));
+	particles = (particle_t *) Mem_Alloc(cl_mempool, cl_maxparticles * sizeof(particle_t));
 #endif
 	CL_Particles_Clear();
 }
@@ -393,8 +388,6 @@ void CL_Particles_Shutdown (void)
 {
 #ifdef WORKINGLQUAKE
 	// No clue what to do here...
-#else
-	Mem_FreePool (&cl_part_mempool);
 #endif
 }
 
diff --git a/cl_video.c b/cl_video.c
index cd34f6f0..51fccbb6 100644
--- a/cl_video.c
+++ b/cl_video.c
@@ -10,7 +10,6 @@ static int  cl_videogmask;
 static int	cl_videobytesperpixel;
 
 static clvideo_t videoarray[ MAXCLVIDEOS ];
-static mempool_t *cl_videomempool;
 static rtexturepool_t *cl_videotexturepool;
 
 static clvideo_t *FindUnusedVid( void )
@@ -44,8 +43,8 @@ static void SuspendVideo( clvideo_t * video )
 	// free the image data
 	Mem_Free( video->imagedata );
 	// if we are in firstframe mode, also close the stream
-	if( video->state == CLVIDEO_FIRSTFRAME ) 
-		dpvsimpledecode_close( video->stream );	
+	if( video->state == CLVIDEO_FIRSTFRAME )
+		dpvsimpledecode_close( video->stream );
 }
 
 static qboolean WakeVideo( clvideo_t * video )
@@ -59,10 +58,10 @@ static qboolean WakeVideo( clvideo_t * video )
 			video->state = CLVIDEO_UNUSED;
 			return false;
 		}
-		
-	video->imagedata = Mem_Alloc( cl_videomempool, video->cpif.width * video->cpif.height * cl_videobytesperpixel );
-	video->cpif.tex = R_LoadTexture2D( cl_videotexturepool, video->cpif.name, 
-		video->cpif.width, video->cpif.height, NULL, TEXTYPE_RGBA, 0, NULL );    
+
+	video->imagedata = Mem_Alloc( cl_mempool, video->cpif.width * video->cpif.height * cl_videobytesperpixel );
+	video->cpif.tex = R_LoadTexture2D( cl_videotexturepool, video->cpif.name,
+		video->cpif.width, video->cpif.height, NULL, TEXTYPE_RGBA, 0, NULL );
 
 	// update starttime
 	video->starttime += realtime - video->lasttime;
@@ -88,10 +87,10 @@ static clvideo_t* OpenVideo( clvideo_t *video, char *filename, char *name, int o
 
 	video->cpif.width = dpvsimpledecode_getwidth( video->stream );
 	video->cpif.height = dpvsimpledecode_getheight( video->stream );
-	video->cpif.tex = R_LoadTexture2D( cl_videotexturepool, video->cpif.name, 
+	video->cpif.tex = R_LoadTexture2D( cl_videotexturepool, video->cpif.name,
 		video->cpif.width, video->cpif.height, NULL, TEXTYPE_RGBA, 0, NULL );
 
-    video->imagedata = Mem_Alloc( cl_videomempool, video->cpif.width * video->cpif.height * cl_videobytesperpixel );
+    video->imagedata = Mem_Alloc( cl_mempool, video->cpif.width * video->cpif.height * cl_videobytesperpixel );
 
 	return video;
 }
@@ -114,7 +113,7 @@ clvideo_t* CL_GetVideo( char *name )
 	clvideo_t *video;
 
 	for( i = 0 ; i < MAXCLVIDEOS ; i++ )
-		if( videoarray[ i ].state != CLVIDEO_UNUSED 
+		if( videoarray[ i ].state != CLVIDEO_UNUSED
 			&&	!strcmp( videoarray[ i ].cpif.name , name ) )
 			break;
 	if( i == MAXCLVIDEOS )
@@ -125,7 +124,7 @@ clvideo_t* CL_GetVideo( char *name )
 	{
 		if( !WakeVideo( video ) )
 			return NULL;
-		else if( video->state == CLVIDEO_RESETONWAKEUP ) 
+		else if( video->state == CLVIDEO_RESETONWAKEUP )
 			video->framenum = -1;
 	}
 
@@ -149,7 +148,7 @@ void CL_RestartVideo( clvideo_t *video )
 {
 	if( !video )
 		return;
-    
+
 	video->starttime = video->lasttime = realtime;
 	video->framenum = -1;
 
@@ -186,9 +185,9 @@ static void VideoFrame( clvideo_t *video )
 	if( video->framenum < destframe ) {
 		do {
 			video->framenum++;
-			if( dpvsimpledecode_video( video->stream, video->imagedata, cl_videormask, 
-				cl_videogmask, cl_videobmask, cl_videobytesperpixel, 
-				cl_videobytesperpixel * video->cpif.width ) 
+			if( dpvsimpledecode_video( video->stream, video->imagedata, cl_videormask,
+				cl_videogmask, cl_videobmask, cl_videobytesperpixel,
+				cl_videobytesperpixel * video->cpif.width )
 				) { // finished?
 				CL_RestartVideo( video );
 				if( video->state == CLVIDEO_PLAY )
@@ -197,7 +196,7 @@ static void VideoFrame( clvideo_t *video )
 			}
 		} while( video->framenum < destframe );
 		R_UpdateTexture( video->cpif.tex, video->imagedata );
-	}					
+	}
 }
 
 void CL_VideoFrame( void ) // update all videos
@@ -212,7 +211,7 @@ void CL_VideoFrame( void ) // update all videos
 				SuspendVideo( video );
 			else if( video->state == CLVIDEO_PAUSE )
 				video->starttime = realtime - video->framenum * video->framerate;
-			else 
+			else
 				VideoFrame( video );
 		}
 
@@ -225,8 +224,6 @@ void CL_Video_Shutdown( void )
 	int i;
 	for( i = 0 ; i < MAXCLVIDEOS ; i++ )
 		CL_CloseVideo( &videoarray[ i ] );
-
-	Mem_FreePool( &cl_videomempool );
 }
 
 void CL_PurgeOwner( int owner )
@@ -293,7 +290,7 @@ static void cl_video_start( void )
 
 	for( video = videoarray, i = 0 ; i < MAXCLVIDEOS ; i++, video++ )
 		if( video->state != CLVIDEO_UNUSED && !video->suspended )
-			video->cpif.tex = R_LoadTexture2D( cl_videotexturepool, video->cpif.name, 
+			video->cpif.tex = R_LoadTexture2D( cl_videotexturepool, video->cpif.name,
 				video->cpif.width, video->cpif.height, NULL, TEXTYPE_RGBA, 0, NULL );
 }
 
@@ -315,8 +312,6 @@ void CL_Video_Init( void )
 
 	Cmd_AddCommand( "playvideo", CL_PlayVideo_f );
 	Cmd_AddCommand( "stopvideo", CL_StopVideo_f );
-	
-	cl_videomempool = Mem_AllocPool( "CL_Video", 0, NULL );
 
 	R_RegisterModule( "CL_Video", cl_video_start, cl_video_shutdown, cl_video_newmap );
 }
diff --git a/client.h b/client.h
index 708c8f69..a1cb0b22 100644
--- a/client.h
+++ b/client.h
@@ -446,6 +446,7 @@ typedef struct
 	netconn_t *netcon;
 	// writing buffer to send to server
 	sizebuf_t message;
+	qbyte message_buf[1024];
 }
 client_static_t;
 
@@ -581,8 +582,6 @@ typedef struct
 }
 client_state_t;
 
-extern mempool_t *cl_scores_mempool;
-
 //
 // cvars
 //
@@ -640,7 +639,7 @@ extern int cl_num_static_entities;
 extern int cl_num_temp_entities;
 extern int cl_num_brushmodel_entities;
 
-extern mempool_t *cl_entities_mempool;
+extern mempool_t *cl_mempool;
 extern entity_t *cl_entities;
 extern qbyte *cl_entities_active;
 extern entity_t *cl_static_entities;
@@ -861,8 +860,6 @@ refdef_t;
 
 refdef_t r_refdef;
 
-extern mempool_t *cl_refdef_mempool;
-
 #include "cgamevm.h"
 
 #endif
diff --git a/common.c b/common.c
index 572c2b11..74d26067 100644
--- a/common.c
+++ b/common.c
@@ -491,25 +491,6 @@ float MSG_ReadAngle (int protocol)
 
 //===========================================================================
 
-void SZ_Alloc (sizebuf_t *buf, int startsize, const char *name)
-{
-	if (startsize < 256)
-		startsize = 256;
-	buf->mempool = Mem_AllocPool(name, 0, NULL);
-	buf->data = Mem_Alloc(buf->mempool, startsize);
-	buf->maxsize = startsize;
-	buf->cursize = 0;
-}
-
-
-void SZ_Free (sizebuf_t *buf)
-{
-	Mem_FreePool(&buf->mempool);
-	buf->data = NULL;
-	buf->maxsize = 0;
-	buf->cursize = 0;
-}
-
 void SZ_Clear (sizebuf_t *buf)
 {
 	buf->cursize = 0;
diff --git a/common.h b/common.h
index 990ab6de..3a73d7ca 100644
--- a/common.h
+++ b/common.h
@@ -41,13 +41,10 @@ typedef struct sizebuf_s
 	qboolean	allowoverflow;	// if false, do a Sys_Error
 	qboolean	overflowed;		// set to true if the buffer size failed
 	qbyte		*data;
-	mempool_t	*mempool;
 	int			maxsize;
 	int			cursize;
 } sizebuf_t;
 
-void SZ_Alloc (sizebuf_t *buf, int startsize, const char *name);
-void SZ_Free (sizebuf_t *buf);
 void SZ_Clear (sizebuf_t *buf);
 void *SZ_GetSpace (sizebuf_t *buf, int length);
 void SZ_Write (sizebuf_t *buf, const void *data, int length);
diff --git a/console.c b/console.c
index 28229e2d..e6127a24 100644
--- a/console.c
+++ b/console.c
@@ -39,7 +39,7 @@ int con_backscroll;
 int con_current;
 // offset in current line for next print
 int con_x;
-char *con_text = 0;
+char con_text[CON_TEXTSIZE];
 
 //seconds
 cvar_t con_notifytime = {CVAR_SAVE, "con_notifytime","3"};
@@ -60,8 +60,6 @@ extern int key_insert;
 
 qboolean con_initialized;
 
-mempool_t *console_mempool;
-
 
 /*
 ==============================================================================
@@ -403,8 +401,6 @@ Con_Init
 */
 void Con_Init (void)
 {
-	console_mempool = Mem_AllocPool("console", 0, NULL);
-	con_text = Mem_Alloc(console_mempool, CON_TEXTSIZE);
 	memset (con_text, ' ', CON_TEXTSIZE);
 	con_linewidth = -1;
 	Con_CheckResize ();
diff --git a/fs.c b/fs.c
index 03ac2a98..39a5aeca 100644
--- a/fs.c
+++ b/fs.c
@@ -218,7 +218,6 @@ typedef struct pack_s
 	int ignorecase;  // PK3 ignores case
 	int numfiles;
 	packfile_t *files;
-	mempool_t *mempool;
 	struct pack_s *next;
 } pack_t;
 
@@ -258,7 +257,6 @@ VARIABLES
 */
 
 mempool_t *fs_mempool;
-mempool_t *pak_mempool;
 
 int fs_filesize;
 
@@ -555,13 +553,12 @@ pack_t *FS_LoadPackPK3 (const char *packfile)
 #endif
 
 	// Create a package structure in memory
-	pack = Mem_Alloc (pak_mempool, sizeof (pack_t));
+	pack = Mem_Alloc(fs_mempool, sizeof (pack_t));
 	pack->ignorecase = true; // PK3 ignores case
 	strlcpy (pack->filename, packfile, sizeof (pack->filename));
 	pack->handle = packhandle;
 	pack->numfiles = eocd.nbentries;
-	pack->mempool = Mem_AllocPool (packfile, 0, NULL);
-	pack->files = Mem_Alloc (pack->mempool, eocd.nbentries * sizeof(packfile_t));
+	pack->files = Mem_Alloc(fs_mempool, eocd.nbentries * sizeof(packfile_t));
 	pack->next = packlist;
 	packlist = pack;
 
@@ -749,13 +746,12 @@ pack_t *FS_LoadPackPAK (const char *packfile)
 	if (numpackfiles > MAX_FILES_IN_PACK)
 		Sys_Error ("%s has %i files", packfile, numpackfiles);
 
-	pack = Mem_Alloc(pak_mempool, sizeof (pack_t));
+	pack = Mem_Alloc(fs_mempool, sizeof (pack_t));
 	pack->ignorecase = false; // PAK is case sensitive
 	strlcpy (pack->filename, packfile, sizeof (pack->filename));
 	pack->handle = packhandle;
 	pack->numfiles = 0;
-	pack->mempool = Mem_AllocPool(packfile, 0, NULL);
-	pack->files = Mem_Alloc(pack->mempool, numpackfiles * sizeof(packfile_t));
+	pack->files = Mem_Alloc(fs_mempool, numpackfiles * sizeof(packfile_t));
 	pack->next = packlist;
 	packlist = pack;
 
@@ -807,7 +803,7 @@ void FS_AddGameDirectory (const char *dir)
 			pak = FS_LoadPackPAK (pakfile);
 			if (pak)
 			{
-				search = Mem_Alloc(pak_mempool, sizeof(searchpath_t));
+				search = Mem_Alloc(fs_mempool, sizeof(searchpath_t));
 				search->pack = pak;
 				search->next = fs_searchpaths;
 				fs_searchpaths = search;
@@ -826,7 +822,7 @@ void FS_AddGameDirectory (const char *dir)
 			pak = FS_LoadPackPK3 (pakfile);
 			if (pak)
 			{
-				search = Mem_Alloc(pak_mempool, sizeof(searchpath_t));
+				search = Mem_Alloc(fs_mempool, sizeof(searchpath_t));
 				search->pack = pak;
 				search->next = fs_searchpaths;
 				fs_searchpaths = search;
@@ -839,7 +835,7 @@ void FS_AddGameDirectory (const char *dir)
 
 	// Add the directory to the search path
 	// (unpacked files have the priority over packed files)
-	search = Mem_Alloc(pak_mempool, sizeof(searchpath_t));
+	search = Mem_Alloc(fs_mempool, sizeof(searchpath_t));
 	strlcpy (search->filename, dir, sizeof (search->filename));
 	search->next = fs_searchpaths;
 	fs_searchpaths = search;
@@ -907,7 +903,6 @@ void FS_Init (void)
 	searchpath_t *search;
 
 	fs_mempool = Mem_AllocPool("file management", 0, NULL);
-	pak_mempool = Mem_AllocPool("paks", 0, NULL);
 
 	Cvar_RegisterVariable (&scr_screenshot_name);
 
@@ -944,7 +939,7 @@ void FS_Init (void)
 			if (!com_argv[i] || com_argv[i][0] == '+' || com_argv[i][0] == '-')
 				break;
 
-			search = Mem_Alloc(pak_mempool, sizeof(searchpath_t));
+			search = Mem_Alloc(fs_mempool, sizeof(searchpath_t));
 			if (!strcasecmp (FS_FileExtension(com_argv[i]), "pak"))
 			{
 				search->pack = FS_LoadPackPAK (com_argv[i]);
@@ -1005,7 +1000,6 @@ FS_Shutdown
 */
 void FS_Shutdown (void)
 {
-	Mem_FreePool (&pak_mempool);
 	Mem_FreePool (&fs_mempool);
 }
 
diff --git a/gl_backend.c b/gl_backend.c
index 782329d1..15bf23f9 100644
--- a/gl_backend.c
+++ b/gl_backend.c
@@ -84,7 +84,6 @@ static matrix4x4_t backend_glmodelviewmatrix;
 static matrix4x4_t backend_projectmatrix;
 
 static unsigned int backendunits, backendimageunits, backendarrayunits, backendactive;
-static mempool_t *gl_backend_mempool;
 
 /*
 note: here's strip order for a terrain row:
@@ -146,15 +145,12 @@ static void R_Mesh_CacheArray_Startup(void);
 static void R_Mesh_CacheArray_Shutdown(void);
 void GL_Backend_AllocArrays(void)
 {
-	if (!gl_backend_mempool)
-		gl_backend_mempool = Mem_AllocPool("GL_Backend", 0, NULL);
 	R_Mesh_CacheArray_Startup();
 }
 
 void GL_Backend_FreeArrays(void)
 {
 	R_Mesh_CacheArray_Shutdown();
-	Mem_FreePool(&gl_backend_mempool);
 }
 
 static void gl_backend_start(void)
diff --git a/gl_textures.c b/gl_textures.c
index e2a49dd3..d79c17a5 100644
--- a/gl_textures.c
+++ b/gl_textures.c
@@ -15,8 +15,6 @@ int		gl_filter_mag = GL_LINEAR;
 
 
 static mempool_t *texturemempool;
-static mempool_t *texturedatamempool;
-static mempool_t *textureprocessingmempool;
 
 // note: this must not conflict with TEXF_ flags in r_textures.h
 // cleared when a texture is uploaded
@@ -100,7 +98,7 @@ typedef struct gltexture_s
 	// pointer into gltextureimage array
 	gltextureimage_t *image;
 	// name of the texture (this might be removed someday), no duplicates
-	char *identifier;
+	char identifier[32];
 	// location in the image, and size
 	int x, y, z, width, height, depth;
 	// copy of the original texture(s) supplied to the upload function, for
@@ -247,29 +245,11 @@ void R_FreeTexture(rtexture_t *rt)
 		}
 	}
 
-	if (glt->identifier)
-		Mem_Free(glt->identifier);
 	if (glt->inputtexels)
 		Mem_Free(glt->inputtexels);
 	Mem_Free(glt);
 }
 
-/*
-static gltexture_t *R_FindTexture (gltexturepool_t *pool, char *identifier)
-{
-	gltexture_t	*glt;
-
-	if (!identifier)
-		return NULL;
-
-	for (glt = pool->gltchain;glt;glt = glt->chain)
-		if (glt->identifier && !strcmp (identifier, glt->identifier))
-			return glt;
-
-	return NULL;
-}
-*/
-
 rtexturepool_t *R_AllocTexturePool(void)
 {
 	gltexturepool_t *pool;
@@ -458,7 +438,7 @@ void R_TextureStats_Print(qboolean printeach, qboolean printpool, qboolean print
 				poolloadedp += glt->inputdatasize;
 			}
 			if (printeach)
-				Con_Printf("%c%4i%c%c%4i%c %s %s %s %s\n", isloaded ? '[' : ' ', (glsize + 1023) / 1024, isloaded ? ']' : ' ', glt->inputtexels ? '[' : ' ', (glt->inputdatasize + 1023) / 1024, glt->inputtexels ? ']' : ' ', isloaded ? "loaded" : "      ", (glt->flags & TEXF_MIPMAP) ? "mip" : "   ", (glt->flags & TEXF_ALPHA) ? "alpha" : "     ", glt->identifier ? glt->identifier : "<unnamed>");
+				Con_Printf("%c%4i%c%c%4i%c %s %s %s %s\n", isloaded ? '[' : ' ', (glsize + 1023) / 1024, isloaded ? ']' : ' ', glt->inputtexels ? '[' : ' ', (glt->inputdatasize + 1023) / 1024, glt->inputtexels ? ']' : ' ', isloaded ? "loaded" : "      ", (glt->flags & TEXF_MIPMAP) ? "mip" : "   ", (glt->flags & TEXF_ALPHA) ? "alpha" : "     ", glt->identifier);
 		}
 		if (printpool)
 			Con_Printf("texturepool %10p total: %i (%.3fMB, %.3fMB original), uploaded %i (%.3fMB, %.3fMB original), upload on demand %i (%.3fMB, %.3fMB original)\n", pool, pooltotal, pooltotalt / 1048576.0, pooltotalp / 1048576.0, poolloaded, poolloadedt / 1048576.0, poolloadedp / 1048576.0, pooltotal - poolloaded, (pooltotalt - poolloadedt) / 1048576.0, (pooltotalp - poolloadedp) / 1048576.0);
@@ -492,9 +472,7 @@ static void r_textures_start(void)
 	// use the largest scrap texture size we can (not sure if this is really a good idea)
 	for (block_size = 1;block_size < realmaxsize && block_size < gl_max_scrapsize.integer;block_size <<= 1);
 
-	texturemempool = Mem_AllocPool("Texture Info", 0, NULL);
-	texturedatamempool = Mem_AllocPool("Texture Storage (not yet uploaded)", 0, NULL);
-	textureprocessingmempool = Mem_AllocPool("Texture Processing Buffers", 0, NULL);
+	texturemempool = Mem_AllocPool("texture management", 0, NULL);
 
 	// Disable JPEG screenshots if the DLL isn't loaded
 	if (! JPEG_OpenLibrary ())
@@ -519,8 +497,6 @@ static void r_textures_shutdown(void)
 	colorconvertbuffer = NULL;
 	texturebuffer = NULL;
 	Mem_FreePool(&texturemempool);
-	Mem_FreePool(&texturedatamempool);
-	Mem_FreePool(&textureprocessingmempool);
 }
 
 static void r_textures_newmap(void)
@@ -599,8 +575,8 @@ void R_MakeResizeBufferBigger(int size)
 			Mem_Free(resizebuffer);
 		if (colorconvertbuffer)
 			Mem_Free(colorconvertbuffer);
-		resizebuffer = Mem_Alloc(textureprocessingmempool, resizebuffersize);
-		colorconvertbuffer = Mem_Alloc(textureprocessingmempool, resizebuffersize);
+		resizebuffer = Mem_Alloc(texturemempool, resizebuffersize);
+		colorconvertbuffer = Mem_Alloc(texturemempool, resizebuffersize);
 		if (!resizebuffer || !colorconvertbuffer)
 			Host_Error("R_Upload: out of memory\n");
 	}
@@ -1048,15 +1024,6 @@ static rtexture_t *R_SetupTexture(rtexturepool_t *rtexturepool, const char *iden
 	if (texturetype == GLTEXTURETYPE_3D && !gl_texture3d)
 		Sys_Error("R_LoadTexture: 3d texture not supported by driver\n");
 
-	/*
-	glt = R_FindTexture (pool, identifier);
-	if (glt)
-	{
-		Con_Printf("R_LoadTexture: replacing existing texture %s\n", identifier);
-		R_FreeTexture((rtexture_t *)glt);
-	}
-	*/
-
 	texinfo = R_GetTexTypeInfo(textype, flags);
 	size = width * height * depth * sides * texinfo->inputbytesperpixel;
 	if (size < 1)
@@ -1111,12 +1078,7 @@ static rtexture_t *R_SetupTexture(rtexturepool_t *rtexturepool, const char *iden
 
 	glt = Mem_Alloc(texturemempool, sizeof(gltexture_t));
 	if (identifier)
-	{
-		glt->identifier = Mem_Alloc(texturemempool, strlen(identifier)+1);
-		strcpy (glt->identifier, identifier);
-	}
-	else
-		glt->identifier = NULL;
+		strlcpy (glt->identifier, identifier, sizeof(glt->identifier));
 	glt->pool = pool;
 	glt->chain = pool->gltchain;
 	pool->gltchain = glt;
@@ -1131,7 +1093,7 @@ static rtexture_t *R_SetupTexture(rtexturepool_t *rtexturepool, const char *iden
 
 	if (data)
 	{
-		glt->inputtexels = Mem_Alloc(texturedatamempool, size);
+		glt->inputtexels = Mem_Alloc(texturemempool, size);
 		if (glt->inputtexels == NULL)
 			Sys_Error("R_LoadTexture: out of memory\n");
 		memcpy(glt->inputtexels, data, size);
diff --git a/host.c b/host.c
index 58b423e1..46ca83d0 100644
--- a/host.c
+++ b/host.c
@@ -152,8 +152,6 @@ void Host_Error (const char *error, ...)
 	longjmp (host_abortserver, 1);
 }
 
-mempool_t *sv_clients_mempool = NULL;
-
 void Host_ServerOptions (void)
 {
 	int i, numplayers;
@@ -216,8 +214,7 @@ void Host_ServerOptions (void)
 		Cvar_SetValueQuick(&deathmatch, 1);
 
 	svs.maxclients = numplayers;
-	sv_clients_mempool = Mem_AllocPool("server clients", 0, NULL);
-	svs.clients = Mem_Alloc(sv_clients_mempool, sizeof(client_t) * svs.maxclients);
+	svs.clients = Mem_Alloc(sv_mempool, sizeof(client_t) * svs.maxclients);
 }
 
 /*
@@ -899,18 +896,18 @@ void Host_Init (void)
 
 	Cmd_Init();
 	Memory_Init_Commands();
-	R_Modules_Init();
+	Con_Init();
 	Cbuf_Init();
+	R_Modules_Init();
 	V_Init();
 	COM_Init();
-	Host_InitLocal();
 	Key_Init();
-	Con_Init();
 	PR_Init();
 	PRVM_Init();
 	Mod_Init();
 	NetConn_Init();
 	SV_Init();
+	Host_InitLocal();
 
 	Con_Printf("Builddate: %s\n", buildstring);
 
diff --git a/host_cmd.c b/host_cmd.c
index 86668a20..21ad53e6 100644
--- a/host_cmd.c
+++ b/host_cmd.c
@@ -533,8 +533,6 @@ void Host_Savegame_f (void)
 }
 
 
-extern mempool_t *edictstring_mempool;
-
 /*
 ===============
 Host_Loadgame_f
@@ -629,8 +627,8 @@ void Host_Loadgame_f (void)
 	{
 		// light style
 		COM_ParseToken(&t, false);
-		sv.lightstyles[i] = Mem_Alloc(edictstring_mempool, strlen(com_token)+1);
-		strcpy (sv.lightstyles[i], com_token);
+		sv.lightstyles[i] = PR_Alloc(strlen(com_token)+1);
+		strcpy(sv.lightstyles[i], com_token);
 	}
 
 // load the edicts out of the savegame file
@@ -1846,7 +1844,7 @@ static void MaxPlayers_f(void)
 	if (svs.clients)
 		Mem_Free(svs.clients);
 	svs.maxclients = n;
-	svs.clients = Mem_Alloc(sv_clients_mempool, sizeof(client_t) * svs.maxclients);
+	svs.clients = Mem_Alloc(sv_mempool, sizeof(client_t) * svs.maxclients);
 	if (n == 1)
 		Cvar_Set ("deathmatch", "0");
 	else
diff --git a/image.c b/image.c
index 38497c79..654747e0 100644
--- a/image.c
+++ b/image.c
@@ -998,20 +998,20 @@ static void Image_Resample24LerpLine (const qbyte *in, qbyte *out, int inwidth,
 	}
 }
 
-int resamplerowsize = 0;
-qbyte *resamplerow1 = NULL;
-qbyte *resamplerow2 = NULL;
-mempool_t *resamplemempool = NULL;
-
 #define LERPBYTE(i) r = resamplerow1[i];out[i] = (qbyte) ((((resamplerow2[i] - r) * lerp) >> 16) + r)
 void Image_Resample32Lerp(const void *indata, int inwidth, int inheight, void *outdata, int outwidth, int outheight)
 {
 	int i, j, r, yi, oldy, f, fstep, lerp, endy = (inheight-1), inwidth4 = inwidth*4, outwidth4 = outwidth*4;
 	qbyte *out;
 	const qbyte *inrow;
+	qbyte *resamplerow1;
+	qbyte *resamplerow2;
 	out = outdata;
 	fstep = (int) (inheight*65536.0f/outheight);
 
+	resamplerow1 = Mem_Alloc(tempmempool, outwidth*4*2);
+	resamplerow2 = resamplerow1 + outwidth*4;
+
 	inrow = indata;
 	oldy = 0;
 	Image_Resample32LerpLine (inrow, resamplerow1, inwidth, outwidth);
@@ -1097,9 +1097,13 @@ void Image_Resample32Lerp(const void *indata, int inwidth, int inheight, void *o
 			memcpy(out, resamplerow1, outwidth4);
 		}
 	}
+
+	Mem_Free(resamplerow1);
+	resamplerow1 = NULL;
+	resamplerow2 = NULL;
 }
 
-void Image_Resample32Nearest(const void *indata, int inwidth, int inheight, void *outdata, int outwidth, int outheight)
+void Image_Resample32Nolerp(const void *indata, int inwidth, int inheight, void *outdata, int outwidth, int outheight)
 {
 	int i, j;
 	unsigned frac, fracstep;
@@ -1141,9 +1145,14 @@ void Image_Resample24Lerp(const void *indata, int inwidth, int inheight, void *o
 	int i, j, r, yi, oldy, f, fstep, lerp, endy = (inheight-1), inwidth3 = inwidth * 3, outwidth3 = outwidth * 3;
 	qbyte *out;
 	const qbyte *inrow;
+	qbyte *resamplerow1;
+	qbyte *resamplerow2;
 	out = outdata;
 	fstep = (int) (inheight*65536.0f/outheight);
 
+	resamplerow1 = Mem_Alloc(tempmempool, outwidth*3*2);
+	resamplerow2 = resamplerow1 + outwidth*3;
+
 	inrow = indata;
 	oldy = 0;
 	Image_Resample24LerpLine (inrow, resamplerow1, inwidth, outwidth);
@@ -1222,6 +1231,9 @@ void Image_Resample24Lerp(const void *indata, int inwidth, int inheight, void *o
 			memcpy(out, resamplerow1, outwidth3);
 		}
 	}
+	Mem_Free(resamplerow1);
+	resamplerow1 = NULL;
+	resamplerow2 = NULL;
 }
 
 void Image_Resample24Nolerp(const void *indata, int inwidth, int inheight, void *outdata, int outwidth, int outheight)
@@ -1268,22 +1280,12 @@ void Image_Resample (const void *indata, int inwidth, int inheight, int indepth,
 {
 	if (indepth != 1 || outdepth != 1)
 		Sys_Error("Image_Resample: 3D resampling not supported\n");
-	if (resamplerowsize < outwidth*4)
-	{
-		if (resamplerow1)
-			Mem_Free(resamplerow1);
-		resamplerowsize = outwidth*4;
-		if (!resamplemempool)
-			resamplemempool = Mem_AllocPool("Image Scaling Buffer", 0, NULL);
-		resamplerow1 = Mem_Alloc(resamplemempool, resamplerowsize*2);
-		resamplerow2 = resamplerow1 + resamplerowsize;
-	}
 	if (bytesperpixel == 4)
 	{
 		if (quality)
 			Image_Resample32Lerp(indata, inwidth, inheight, outdata, outwidth, outheight);
 		else
-			Image_Resample32Nearest(indata, inwidth, inheight, outdata, outwidth, outheight);
+			Image_Resample32Nolerp(indata, inwidth, inheight, outdata, outwidth, outheight);
 	}
 	else if (bytesperpixel == 3)
 	{
diff --git a/menu.c b/menu.c
index 100e4f2e..3c84478b 100644
--- a/menu.c
+++ b/menu.c
@@ -28,8 +28,6 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 #define TYPE_GAME 2
 #define TYPE_BOTH 3
 
-mempool_t *menu_mempool;
-
 int NehGameType;
 
 enum m_state_e m_state;
@@ -1332,8 +1330,8 @@ void M_Setup_Draw (void)
 			menuplyr_width = image_width;
 			menuplyr_height = image_height;
 			Mem_Free(f);
-			menuplyr_pixels = Mem_Alloc(menu_mempool, menuplyr_width * menuplyr_height);
-			menuplyr_translated = Mem_Alloc(menu_mempool, menuplyr_width * menuplyr_height * 4);
+			menuplyr_pixels = Mem_Alloc(cl_mempool, menuplyr_width * menuplyr_height);
+			menuplyr_translated = Mem_Alloc(cl_mempool, menuplyr_width * menuplyr_height * 4);
 			memcpy(menuplyr_pixels, data, menuplyr_width * menuplyr_height);
 			Mem_Free(data);
 		}
@@ -4253,7 +4251,6 @@ void M_Shutdown(void);
 
 void M_Init (void)
 {
-	menu_mempool = Mem_AllocPool("Menu", 0, NULL);
 	menuplyr_load = true;
 	menuplyr_pixels = NULL;
 
@@ -4579,8 +4576,6 @@ void M_Shutdown(void)
 {
 	// reset key_dest
 	key_dest = key_game;
-
-	Mem_FreePool (&menu_mempool);
 }
 
 void M_Restart(void)
@@ -4717,8 +4712,6 @@ void MP_Init (void)
 	prog->error_cmd = MP_Error;
 
 	// allocate the mempools
-	prog->edicts_mempool = Mem_AllocPool(M_NAME " edicts mempool", 0, NULL);
-	prog->edictstring_mempool = Mem_AllocPool( M_NAME " edict string mempool", 0, NULL);
 	prog->progs_mempool = Mem_AllocPool(M_PROG_FILENAME, 0, NULL);
 
 	PRVM_LoadProgs(M_PROG_FILENAME, m_numrequiredfunc, m_required_func);
diff --git a/meshqueue.c b/meshqueue.c
index 24744f31..f3131544 100644
--- a/meshqueue.c
+++ b/meshqueue.c
@@ -21,15 +21,12 @@ meshqueue_t *mq_array, *mqt_array, *mq_listhead;
 int mq_count, mqt_count;
 int mq_total, mqt_total;
 
-mempool_t *meshqueuemempool;
-
 void R_MeshQueue_Init(void)
 {
 	Cvar_RegisterVariable(&r_meshqueue_entries);
 	Cvar_RegisterVariable(&r_meshqueue_immediaterender);
 	Cvar_RegisterVariable(&r_meshqueue_sort);
 
-	meshqueuemempool = Mem_AllocPool("R_MeshQueue", 0, NULL);
 	mq_total = 0;
 	mqt_total = 0;
 	mq_array = NULL;
@@ -50,7 +47,7 @@ void R_MeshQueue_Render(void)
 static void R_MeshQueue_EnlargeTransparentArray(int newtotal)
 {
 	meshqueue_t *newarray;
-	newarray = Mem_Alloc(meshqueuemempool, newtotal * sizeof(meshqueue_t));
+	newarray = Mem_Alloc(cl_mempool, newtotal * sizeof(meshqueue_t));
 	if (mqt_array)
 	{
 		memcpy(newarray, mqt_array, mqt_total * sizeof(meshqueue_t));
@@ -158,11 +155,11 @@ void R_MeshQueue_BeginScene(void)
 		mq_total = r_meshqueue_entries.integer;
 		if (mq_array)
 			Mem_Free(mq_array);
-		mq_array = Mem_Alloc(meshqueuemempool, mq_total * sizeof(meshqueue_t));
+		mq_array = Mem_Alloc(cl_mempool, mq_total * sizeof(meshqueue_t));
 	}
 
 	if (mqt_array == NULL)
-		mqt_array = Mem_Alloc(meshqueuemempool, mqt_total * sizeof(meshqueue_t));
+		mqt_array = Mem_Alloc(cl_mempool, mqt_total * sizeof(meshqueue_t));
 
 	mq_count = 0;
 	mqt_count = 0;
diff --git a/netconn.c b/netconn.c
index acbc3a81..b1f69921 100755
--- a/netconn.c
+++ b/netconn.c
@@ -44,6 +44,7 @@ static cvar_t sv_masters [] =
 static double nextheartbeattime = 0;
 
 sizebuf_t net_message;
+static qbyte net_message_buf[NET_MAXMESSAGE];
 
 cvar_t net_messagetimeout = {0, "net_messagetimeout","300"};
 cvar_t net_messagerejointimeout = {0, "net_messagerejointimeout","10"};
@@ -193,7 +194,7 @@ static qboolean _ServerList_CompareInt( int A, serverlist_maskop_t op, int B )
 }
 
 static qboolean _ServerList_CompareStr( const char *A, serverlist_maskop_t op, const char *B )
-{ 
+{
 	// Same here, also using an intermediate & final return would be more appropriate
 	// A info B mask
 	switch( op ) {
@@ -879,7 +880,6 @@ void NetConn_ConnectionEstablished(lhnetsocket_t *mysocket, lhnetaddress_t *peer
 	cls.demonum = -1;			// not in the demo loop now
 	cls.state = ca_connected;
 	cls.signon = 0;				// need all the signon messages before playing
-	CL_ClearState();
 }
 
 int NetConn_IsLocalGame(void)
@@ -1023,7 +1023,7 @@ int NetConn_ClientParsePacket(lhnetsocket_t *mysocket, qbyte *data, int length,
 				if( serverlist_cachecount == SERVERLIST_TOTALSIZE )
 					break;
 				// also ignore it if we have already queried it (other master server response)
-				for( n = 0 ; n < serverlist_cachecount ; n++ ) 
+				for( n = 0 ; n < serverlist_cachecount ; n++ )
 					if( !strcmp( ipstring, serverlist_cache[ n ].info.cname ) )
 						break;
 				if( n >= serverlist_cachecount )
@@ -1794,7 +1794,7 @@ void NetConn_Init(void)
 {
 	int i;
 	lhnetaddress_t tempaddress;
-	netconn_mempool = Mem_AllocPool("Networking", 0, NULL);
+	netconn_mempool = Mem_AllocPool("network connections", 0, NULL);
 	Cmd_AddCommand("net_stats", Net_Stats_f);
 	Cmd_AddCommand("net_slist", Net_Slist_f);
 	Cmd_AddCommand("heartbeat", Net_Heartbeat_f);
@@ -1838,7 +1838,9 @@ void NetConn_Init(void)
 	}
 	cl_numsockets = 0;
 	sv_numsockets = 0;
-	SZ_Alloc(&net_message, NET_MAXMESSAGE, "net_message");
+	net_message.data = net_message_buf;
+	net_message.maxsize = sizeof(net_message_buf);
+	net_message.cursize = 0;
 	LHNET_Init();
 }
 
diff --git a/pr_cmds.c b/pr_cmds.c
index dd9ca8ef..6abd53b6 100644
--- a/pr_cmds.c
+++ b/pr_cmds.c
@@ -23,8 +23,6 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 cvar_t sv_aim = {CVAR_SAVE, "sv_aim", "2"}; //"0.93"}; // LordHavoc: disabled autoaim by default
 cvar_t pr_zone_min_strings = {0, "pr_zone_min_strings", "64"};
 
-mempool_t *pr_strings_mempool;
-
 // LordHavoc: added this to semi-fix the problem of using many ftos calls in a print
 #define STRINGTEMP_BUFFERS 16
 #define STRINGTEMP_LENGTH 4096
@@ -2984,7 +2982,7 @@ void PF_strzone(void)
 {
 	char *in, *out;
 	in = G_STRING(OFS_PARM0);
-	out = Mem_Alloc(pr_strings_mempool, strlen(in) + 1);
+	out = PR_Alloc(strlen(in) + 1);
 	strcpy(out, in);
 	G_INT(OFS_RETURN) = PR_SetString(out);
 }
@@ -2992,7 +2990,7 @@ void PF_strzone(void)
 //void(string s) strunzone = #119; // removes a copy of a string from the string zone (you can not use that string again or it may crash!!!)
 void PF_strunzone(void)
 {
-	Mem_Free(G_STRING(OFS_PARM0));
+	PR_Free(G_STRING(OFS_PARM0));
 }
 
 //void(entity e, string s) clientcommand = #440; // executes a command string as if it came from the specified client
@@ -3718,19 +3716,16 @@ int pr_numbuiltins = sizeof(pr_builtin)/sizeof(pr_builtin[0]);
 
 void PR_Cmd_Init(void)
 {
-	pr_strings_mempool = Mem_AllocPool("pr_stringszone", 0, NULL);
 	PR_Files_Init();
 	PR_Search_Init();
 }
 
 void PR_Cmd_Shutdown(void)
 {
-	Mem_FreePool (&pr_strings_mempool);
 }
 
 void PR_Cmd_Reset(void)
 {
-	Mem_EmptyPool(pr_strings_mempool);
 	PR_Search_Reset();
 	PR_Files_CloseAll();
 }
diff --git a/pr_edict.c b/pr_edict.c
index 0176661c..8b41df06 100644
--- a/pr_edict.c
+++ b/pr_edict.c
@@ -34,8 +34,7 @@ int				pr_edictareasize;		// LordHavoc: in bytes
 
 unsigned short	pr_crc;
 
-mempool_t		*progs_mempool;
-mempool_t		*edictstring_mempool;
+mempool_t		*serverprogs_mempool;
 
 int		type_size[8] = {1,sizeof(string_t)/4,1,3,1,1,sizeof(func_t)/4,sizeof(void *)/4};
 
@@ -921,7 +920,7 @@ char *ED_NewString (const char *string)
 	int i,l;
 
 	l = strlen(string) + 1;
-	new = Mem_Alloc(edictstring_mempool, l);
+	new = PR_Alloc(l);
 	new_p = new;
 
 	for (i=0 ; i< l ; i++)
@@ -1307,10 +1306,9 @@ void PR_LoadProgs (const char *progsname)
 	for (i=0 ; i<GEFV_CACHESIZE ; i++)
 		gefvCache[i].field[0] = 0;
 
-	Mem_EmptyPool(progs_mempool);
-	Mem_EmptyPool(edictstring_mempool);
+	PR_FreeAll();
 
-	progs = (dprograms_t *)FS_LoadFile (progsname, progs_mempool, false);
+	progs = (dprograms_t *)FS_LoadFile (progsname, serverprogs_mempool, false);
 	if (!progs)
 		Host_Error ("PR_LoadProgs: couldn't load %s", progsname);
 
@@ -1335,7 +1333,8 @@ void PR_LoadProgs (const char *progsname)
 	// we need to expand the fielddefs list to include all the engine fields,
 	// so allocate a new place for it
 	infielddefs = (ddef_t *)((qbyte *)progs + progs->ofs_fielddefs);
-	pr_fielddefs = Mem_Alloc(progs_mempool, (progs->numfielddefs + DPFIELDS) * sizeof(ddef_t));
+	pr_fielddefs = PR_Alloc((progs->numfielddefs + DPFIELDS) * sizeof(ddef_t));
+	pr_functions = PR_Alloc(sizeof(mfunction_t) * progs->numfunctions);
 
 	pr_statements = (dstatement_t *)((qbyte *)progs + progs->ofs_statements);
 
@@ -1353,7 +1352,6 @@ void PR_LoadProgs (const char *progsname)
 		pr_statements[i].c = LittleShort(pr_statements[i].c);
 	}
 
-	pr_functions = Mem_Alloc(progs_mempool, sizeof(mfunction_t) * progs->numfunctions);
 	for (i = 0;i < progs->numfunctions;i++)
 	{
 		pr_functions[i].first_statement = LittleLong (dfunctions[i].first_statement);
@@ -1674,8 +1672,7 @@ void PR_Init (void)
 	Cvar_RegisterVariable (&pr_boundscheck);
 	Cvar_RegisterVariable (&pr_traceqc);
 
-	progs_mempool = Mem_AllocPool("progs.dat", 0, NULL);
-	edictstring_mempool = Mem_AllocPool("edict strings", 0, NULL);
+	serverprogs_mempool = Mem_AllocPool("server progs", 0, NULL);
 
 	PR_Cmd_Init();
 }
@@ -1690,8 +1687,25 @@ void PR_Shutdown (void)
 {
 	PR_Cmd_Shutdown();
 
-	Mem_FreePool(&edictstring_mempool);
-	Mem_FreePool(&progs_mempool);
+	Mem_FreePool(&serverprogs_mempool);
+}
+
+void *PR_Alloc(size_t buffersize)
+{
+	return Mem_Alloc(serverprogs_mempool, buffersize);
+}
+
+void PR_Free(void *buffer)
+{
+	Mem_Free(buffer);
+}
+
+void PR_FreeAll(void)
+{
+	progs = NULL;
+	pr_fielddefs = NULL;
+	pr_functions = NULL;
+	Mem_EmptyPool(serverprogs_mempool);
 }
 
 // LordHavoc: turned EDICT_NUM into a #define for speed reasons
diff --git a/progs.h b/progs.h
index 4fb9f4f3..cd692f51 100644
--- a/progs.h
+++ b/progs.h
@@ -164,6 +164,10 @@ void PR_Shutdown (void);
 void PR_ExecuteProgram (func_t fnum, const char *errormessage);
 void PR_LoadProgs (const char *progsname);
 
+void *PR_Alloc (size_t buffersize);
+void PR_Free (void *buffer);
+void PR_FreeAll (void);
+
 void PR_Profile_f (void);
 
 void PR_PrintState(void);
@@ -237,7 +241,7 @@ void PR_Execute_ProgsLoaded(void);
 void ED_PrintEdicts (void);
 void ED_PrintNum (int ent);
 
-#define PR_GetString(num) (pr_strings + num) 
+#define PR_GetString(num) (pr_strings + num)
 #define PR_SetString(s)   ((s) != NULL ? (int) (s - pr_strings) : 0)
 
 #endif
diff --git a/progsvm.h b/progsvm.h
index 56b832d6..744c8f57 100644
--- a/progsvm.h
+++ b/progsvm.h
@@ -170,7 +170,7 @@ typedef struct prvm_edict_s
 {
 	// engine-private fields (stored in dynamically resized array)
 	//edict_engineprivate_t *e;
-	union 
+	union
 	{
 		prvm_edict_private_t *e;
 		void				 *vp;
@@ -195,7 +195,7 @@ typedef struct prvm_edict_s
 	//entvars_t *v;
 	void *v;
 } prvm_edict_t;
-	
+
 #define PRVM_GETEDICTFIELDVALUE(ed, fieldoffset) (fieldoffset ? (prvm_eval_t *)((qbyte *)ed->v + fieldoffset) : NULL)
 
 /*// this struct is the basic requirement for a qc prog
@@ -245,19 +245,19 @@ typedef struct vm_prog_s
 	ddef_t				*fielddefs;
 	ddef_t				*globaldefs;
 	dstatement_t		*statements;
-	//prvm_pr_globalvars_t*pr_global_struct; 
+	//prvm_pr_globalvars_t*pr_global_struct;
 	float				*globals;			// same as pr_global_struct
 	int					edict_size;			// in bytes
 	int					edictareasize;		// LordHavoc: in bytes (for bound checking)
-	
+
+	// all memory allocations related to this vm_prog (code, edicts, strings)
 	mempool_t			*progs_mempool;
-	mempool_t			*edictstring_mempool;
 
 	prvm_builtin_t		*builtins;
 	int					numbuiltins;
-	
+
 	int					argc;
-	
+
 	int					trace;
 	mfunction_t			*xfunction;
 	int					xstatement;
@@ -266,11 +266,11 @@ typedef struct vm_prog_s
 	// thus increase the array, so depth wont be overwritten
 	prvm_stack_t		stack[PRVM_MAX_STACK_DEPTH+1];
 	int					depth;
-	
+
 	int					localstack[PRVM_LOCALSTACK_SIZE];
 	int					localstack_used;
 
-	unsigned short		crc;	
+	unsigned short		crc;
 
 	//============================================================================
 	// until this point everything also exists (with the pr_ prefix) in the old vm
@@ -286,9 +286,6 @@ typedef struct vm_prog_s
 	// size of the engine private struct
 	int					edictprivate_size;
 
-	// instead of sv_editcts_mempool
-	mempool_t			*edicts_mempool;
-	
 	// has to be updated every frame - so the vm time is up-to-date
 	// AK changed so time will point to the time field (if there is one) else it points to _time
 	// actually should be double, but qc doesnt support it
@@ -312,19 +309,19 @@ typedef struct vm_prog_s
 //	prvm_builtin_mem_t  *mem_list;
 
 // now passes as parameter of PRVM_LoadProgs
-//	char				**required_func; 
+//	char				**required_func;
 //	int					numrequiredfunc;
 
 	//============================================================================
-	
+
 	ddef_t				*self; // if self != 0 then there is a global self
-	
+
 	//============================================================================
 	// function pointers
-	
+
 	void				(*begin_increase_edicts)(void);  // used by PRVM_MEM_Increase_Edicts
-	void				(*end_increase_edicts)(void); 
-	
+	void				(*end_increase_edicts)(void);
+
 	void				(*init_edict)(int num);		// used by PRVM_ED_ClearEdict
 	void				(*free_edict)(prvm_edict_t *ed); // used by PRVM_ED_Free
 
@@ -352,25 +349,25 @@ extern prvm_prog_t prvm_prog_list[PRVM_MAXPROGS];
 //============================================================================
 // prvm_cmds part
 
-extern prvm_builtin_t vm_sv_builtins[];  
-extern prvm_builtin_t vm_cl_builtins[];  
-extern prvm_builtin_t vm_m_builtins[];  
+extern prvm_builtin_t vm_sv_builtins[];
+extern prvm_builtin_t vm_cl_builtins[];
+extern prvm_builtin_t vm_m_builtins[];
 
 extern const int vm_sv_numbuiltins;
 extern const int vm_cl_numbuiltins;
 extern const int vm_m_numbuiltins;
 
-extern char * vm_sv_extensions; 
-extern char * vm_cl_extensions; 
-extern char * vm_m_extensions; 
+extern char * vm_sv_extensions;
+extern char * vm_cl_extensions;
+extern char * vm_m_extensions;
 
-void VM_SV_Cmd_Init(void); 
-void VM_SV_Cmd_Reset(void); 
+void VM_SV_Cmd_Init(void);
+void VM_SV_Cmd_Reset(void);
 
-void VM_CL_Cmd_Init(void); 
-void VM_CL_Cmd_Reset(void); 
+void VM_CL_Cmd_Init(void);
+void VM_CL_Cmd_Reset(void);
 
-void VM_M_Cmd_Init(void); 
+void VM_M_Cmd_Init(void);
 void VM_M_Cmd_Reset(void);
 
 void VM_Cmd_Init(void);
@@ -454,7 +451,7 @@ void PRVM_ED_PrintNum (int ent);
 #define PRVM_Begin  if(prog != 0) Con_Printf("prog not 0(prog = %i) in file: %s line: %i!\n", PRVM_GetProgNr(), __FILE__, __LINE__)
 #define PRVM_End	prog = 0
 #else
-#define PRVM_Begin  
+#define PRVM_Begin
 #define PRVM_End	prog = 0
 #endif
 
@@ -481,7 +478,7 @@ void	 PRVM_ResetProg(void);
 qboolean PRVM_ProgLoaded(int prognr);
 
 int		 PRVM_GetProgNr(void);
-	
+
 
 // TODO: fill in the params
 //void PRVM_Create();
diff --git a/protocol.c b/protocol.c
index 176ae44e..d0254aff 100644
--- a/protocol.c
+++ b/protocol.c
@@ -860,7 +860,7 @@ void EntityFrame_CL_ReadFrame(void)
 	entity_t *ent;
 	entityframe_database_t *d;
 	if (!cl.entitydatabase)
-		cl.entitydatabase = EntityFrame_AllocDatabase(cl_entities_mempool);
+		cl.entitydatabase = EntityFrame_AllocDatabase(cl_mempool);
 	d = cl.entitydatabase;
 
 	EntityFrame_Clear(f, NULL, -1);
@@ -1130,7 +1130,7 @@ void EntityFrame4_CL_ReadFrame(void)
 	entity_state_t *s;
 	entityframe4_database_t *d;
 	if (!cl.entitydatabase4)
-		cl.entitydatabase4 = EntityFrame4_AllocDatabase(cl_entities_mempool);
+		cl.entitydatabase4 = EntityFrame4_AllocDatabase(cl_mempool);
 	d = cl.entitydatabase4;
 	// read the number of the frame this refers to
 	referenceframenum = MSG_ReadLong();
@@ -1373,6 +1373,10 @@ entityframe5_database_t *EntityFrame5_AllocDatabase(mempool_t *pool)
 
 void EntityFrame5_FreeDatabase(entityframe5_database_t *d)
 {
+	// all the [maxedicts] memory is allocated at once, so there's only one
+	// thing to free
+	if (d->maxedicts)
+		Mem_Free(d->deltabits);
 	Mem_Free(d);
 }
 
@@ -1381,10 +1385,40 @@ void EntityFrame5_ResetDatabase(entityframe5_database_t *d)
 	int i;
 	memset(d, 0, sizeof(*d));
 	d->latestframenum = 0;
-	for (i = 0;i < MAX_EDICTS;i++)
+	for (i = 0;i < d->maxedicts;i++)
 		d->states[i] = defaultstate;
 }
 
+void EntityFrame5_ExpandEdicts(entityframe5_database_t *d, mempool_t *mempool, int newmax)
+{
+	if (d->maxedicts < newmax)
+	{
+		qbyte *data;
+		int oldmaxedicts = d->maxedicts;
+		int *olddeltabits = d->deltabits;
+		qbyte *oldpriorities = d->priorities;
+		int *oldupdateframenum = d->updateframenum;
+		entity_state_t *oldstates = d->states;
+		qbyte *oldvisiblebits = d->visiblebits;
+		d->maxedicts = newmax;
+		data = Mem_Alloc(mempool, d->maxedicts * sizeof(int) + d->maxedicts * sizeof(qbyte) + d->maxedicts * sizeof(int) + d->maxedicts * sizeof(entity_state_t) + (d->maxedicts+7)/8 * sizeof(qbyte));
+		d->deltabits = (void *)data;data += d->maxedicts * sizeof(int);
+		d->priorities = (void *)data;data += d->maxedicts * sizeof(qbyte);
+		d->updateframenum = (void *)data;data += d->maxedicts * sizeof(int);
+		d->states = (void *)data;data += d->maxedicts * sizeof(entity_state_t);
+		d->visiblebits = (void *)data;data += (d->maxedicts+7)/8 * sizeof(qbyte);
+		if (oldmaxedicts)
+		{
+			memcpy(d->deltabits, olddeltabits, d->maxedicts * sizeof(int));
+			memcpy(d->priorities, oldpriorities, d->maxedicts * sizeof(qbyte));
+			memcpy(d->updateframenum, oldupdateframenum, d->maxedicts * sizeof(int));
+			memcpy(d->states, oldstates, d->maxedicts * sizeof(entity_state_t));
+			memcpy(d->visiblebits, oldvisiblebits, (d->maxedicts+7)/8 * sizeof(qbyte));
+			// the previous buffers were a single allocation, so just one free
+			Mem_Free(d->deltabits);
+		}
+	}
+}
 
 int EntityState5_Priority(entityframe5_database_t *d, entity_state_t *view, entity_state_t *s, int changedbits, int age)
 {
@@ -1898,6 +1932,9 @@ void EntityFrame5_WriteFrame(sizebuf_t *msg, entityframe5_database_t *d, int num
 	qbyte data[128];
 	entityframe5_packetlog_t *packetlog;
 
+	if (sv.num_edicts > d->maxedicts)
+		EntityFrame5_ExpandEdicts(d, sv_mempool, (sv.num_edicts + 255) & ~255);
+
 	framenum = d->latestframenum + 1;
 
 	// if packet log is full, mark all frames as lost, this will cause
diff --git a/protocol.h b/protocol.h
index 398cf838..96e46ef8 100644
--- a/protocol.h
+++ b/protocol.h
@@ -645,7 +645,7 @@ void EntityFrame4_CL_ReadFrame(void);
 // byte = bound(0, s->scale * 16, 255)
 #define E5_SCALE (1<<10)
 // flag
-#define E5_ORIGIN32 (1<<11) 
+#define E5_ORIGIN32 (1<<11)
 // flag
 #define E5_ANGLES16 (1<<12)
 // flag
@@ -720,21 +720,23 @@ typedef struct entityframe5_database_s
 	// logs of all recently sent messages (between acked and latest)
 	entityframe5_packetlog_t packetlog[ENTITYFRAME5_MAXPACKETLOGS];
 
+	// this goes up as needed and causes all the arrays to be reallocated
+	int maxedicts;
+
 	// which properties of each entity have changed since last send
-	int deltabits[MAX_EDICTS];
+	int *deltabits; // [maxedicts]
 	// priorities of entities (updated whenever deltabits change)
 	// (derived from deltabits)
-	qbyte priorities[MAX_EDICTS];
+	qbyte *priorities; // [maxedicts]
 	// last frame this entity was sent on, for prioritzation
-	int updateframenum[MAX_EDICTS];
+	int *updateframenum; // [maxedicts]
 
 	// database of current status of all entities
-	// (FIXME: this is 2.5mb per client even if most is unused!)
-	entity_state_t states[MAX_EDICTS];
+	entity_state_t *states; // [maxedicts]
 	// which entities are currently active
 	// (duplicate of the active bit of every state in states[])
 	// (derived from states)
-	qbyte visiblebits[(MAX_EDICTS+7)/8];
+	qbyte *visiblebits; // [(maxedicts+7)/8]
 
 	// delta compression of stats
 	qbyte statsdeltabits[(MAX_CL_STATS+7)/8];
diff --git a/prvm_edict.c b/prvm_edict.c
index 0dceb690..3f54cbeb 100644
--- a/prvm_edict.c
+++ b/prvm_edict.c
@@ -17,7 +17,7 @@ along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
 */
-// AK new vm 
+// AK new vm
 
 #include "quakedef.h"
 #include "progsvm.h"
@@ -54,19 +54,19 @@ void PRVM_MEM_Alloc()
 	// reserve space for the null entity aka world
 	// check bound of max_edicts
 	prog->max_edicts = bound(1, prog->max_edicts, prog->limit_edicts);
-	prog->num_edicts = bound(1, prog->num_edicts, prog->max_edicts);	
+	prog->num_edicts = bound(1, prog->num_edicts, prog->max_edicts);
 
 	// edictprivate_size has to be min as big prvm_edict_private_t
-	prog->edictprivate_size = max(prog->edictprivate_size,(int)sizeof(prvm_edict_private_t)); 
+	prog->edictprivate_size = max(prog->edictprivate_size,(int)sizeof(prvm_edict_private_t));
 
 	// alloc edicts
-	prog->edicts = Mem_Alloc(prog->edicts_mempool,prog->limit_edicts * sizeof(prvm_edict_t));
-	
+	prog->edicts = Mem_Alloc(prog->progs_mempool,prog->limit_edicts * sizeof(prvm_edict_t));
+
 	// alloc edict private space
-	prog->edictprivate = Mem_Alloc(prog->edicts_mempool, prog->max_edicts * prog->edictprivate_size);
-	
+	prog->edictprivate = Mem_Alloc(prog->progs_mempool, prog->max_edicts * prog->edictprivate_size);
+
 	// alloc edict fields
-	prog->edictsfields = Mem_Alloc(prog->edicts_mempool, prog->max_edicts * prog->edict_size);
+	prog->edictsfields = Mem_Alloc(prog->progs_mempool, prog->max_edicts * prog->edict_size);
 
 	// set edict pointers
 	for(i = 0; i < prog->max_edicts; i++)
@@ -84,20 +84,20 @@ PRVM_MEM_IncreaseEdicts
 void PRVM_MEM_IncreaseEdicts()
 {
 	int		i;
-	int		oldmaxedicts = prog->max_edicts; 
+	int		oldmaxedicts = prog->max_edicts;
 	void	*oldedictsfields = prog->edictsfields;
 	void	*oldedictprivate = prog->edictprivate;
-	
+
 	if(prog->max_edicts >= prog->limit_edicts)
 		return;
-	
+
 	PRVM_GCALL(begin_increase_edicts)();
 
 	// increase edicts
 	prog->max_edicts = min(prog->max_edicts + 256, prog->limit_edicts);
 
-	prog->edictsfields = Mem_Alloc(prog->edicts_mempool, prog->max_edicts * prog->edict_size);
-	prog->edictprivate = Mem_Alloc(prog->edicts_mempool, prog->max_edicts * prog->edictprivate_size);
+	prog->edictsfields = Mem_Alloc(prog->progs_mempool, prog->max_edicts * prog->edict_size);
+	prog->edictprivate = Mem_Alloc(prog->progs_mempool, prog->max_edicts * prog->edictprivate_size);
 
 	memcpy(prog->edictsfields, oldedictsfields, oldmaxedicts * prog->edict_size);
 	memcpy(prog->edictprivate, oldedictprivate, oldmaxedicts * prog->edictprivate_size);
@@ -171,7 +171,7 @@ PRVM_SetProg
 void PRVM_SetProg(int prognr)
 {
 	if(prognr && prognr < PRVM_MAXPROGS)
-	{	
+	{
 		if(prog_list[prognr].loaded)
 			prog = &prog_list[prognr];
 		else
@@ -197,7 +197,7 @@ void PRVM_ED_ClearEdict (prvm_edict_t *e)
 	num = PRVM_NUM_FOR_EDICT(e) - 1;
 
 	// AK: Let the init_edict function determine if something needs to be initialized
-	PRVM_GCALL(init_edict)(num);	
+	PRVM_GCALL(init_edict)(num);
 }
 
 /*
@@ -438,9 +438,9 @@ char *PRVM_UglyValueString (etype_t type, prvm_eval_t *val)
 	char *s;
 	ddef_t *def;
 	mfunction_t *f;
-	
+
 	type &= ~DEF_SAVEGLOBAL;
-	
+
 	switch (type)
 	{
 	case ev_string:
@@ -490,7 +490,7 @@ char *PRVM_UglyValueString (etype_t type, prvm_eval_t *val)
 		dpsnprintf (line, sizeof (line), "bad type %i", type);
 		break;
 	}
-	
+
 	return line;
 }
 
@@ -694,7 +694,7 @@ void PRVM_ED_PrintEdicts_f (void)
 		Con_Print("prvm_edicts <program name>\n");
 		return;
 	}
-	
+
 	PRVM_Begin;
 	if(!PRVM_SetProgFromString(Cmd_Argv(1)))
 		return;
@@ -776,7 +776,7 @@ void PRVM_ED_Count_f (void)
 				continue;
 			active++;
 		}
-		
+
 		Con_Printf("num_edicts:%3i\n", prog->num_edicts);
 		Con_Printf("active    :%3i\n", active);
 	}
@@ -877,7 +877,7 @@ char *PRVM_ED_NewString (const char *string)
 	int i,l;
 
 	l = strlen(string) + 1;
-	new = Mem_Alloc(prog->edictstring_mempool, l);
+	new = Mem_Alloc(prog->progs_mempool, l);
 	new_p = new;
 
 	for (i=0 ; i< l ; i++)
@@ -1148,9 +1148,9 @@ void PRVM_ED_LoadFromFile (const char *data)
 			PRVM_ERROR ("PRVM_ED_LoadFromFile: %s: found %s when expecting {", PRVM_NAME, com_token);
 
 		// CHANGED: this is not conform to ED_LoadFromFile
-		if(!prog->num_edicts) 
+		if(!prog->num_edicts)
 			ent = PRVM_EDICT_NUM(0);
-		else 
+		else
 			ent = PRVM_ED_Alloc();
 
 		// clear it
@@ -1181,10 +1181,10 @@ void PRVM_ED_LoadFromFile (const char *data)
 				PRVM_ED_Free (ent);
 				continue;
 			}
-			
+
 			// look for the spawn function
 			func = PRVM_ED_FindFunction (PRVM_GetString(handle));
-			
+
 			if (!func)
 			{
 				if (developer.integer) // don't confuse non-developers with errors
@@ -1195,19 +1195,19 @@ void PRVM_ED_LoadFromFile (const char *data)
 				PRVM_ED_Free (ent);
 				continue;
 			}
-			
+
 			// self = ent
 			PRVM_G_INT(prog->self->ofs) = PRVM_EDICT_TO_PROG(ent);
 			PRVM_ExecuteProgram (func - prog->functions, "");
 		}
-	
+
 		spawned++;
 		if (ent->p.e->free)
 			died++;
 	}
 
 	Con_DPrintf("%s: %i new entities parsed, %i new inhibited, %i (%i new) spawned (whereas %i removed self, %i stayed)\n", PRVM_NAME, parsed, inhibited, prog->num_edicts, spawned, died, spawned - died);
-}	
+}
 
 // not used
 /*
@@ -1233,26 +1233,18 @@ PRVM_ResetProg
 
 void PRVM_ResetProg()
 {
-	/*mempool_t *t1, *t2, *t3;
+	/*mempool_t *t1;
 
 	t1 = prog->progs_mempool;
-	t2 = prog->edictstring_mempool;
-	t3 = prog->edicts_mempool;
-	
-	Mem_EmptyPool(prog->progs_mempool);
-	Mem_EmptyPool(prog->edictstring_mempool);
-	Mem_EmptyPool(prog->edicts_mempool);*/
+
+	Mem_EmptyPool(prog->progs_mempool);*/
 	Mem_FreePool(&prog->progs_mempool);
-	Mem_FreePool(&prog->edictstring_mempool);
-	Mem_FreePool(&prog->edicts_mempool);
-	
+
 	memset(prog,0,sizeof(prvm_prog_t));
-	
+
 	/*prog->time = &prog->_time;
-	
-	prog->progs_mempool = t1;
-	prog->edictstring_mempool = t2;
-	prog->edicts_mempool = t3;*/
+
+	prog->progs_mempool = t1;*/
 
 	PRVM_GCALL(reset_cmd)();
 }
@@ -1270,7 +1262,6 @@ void PRVM_LoadProgs (const char * filename, int numrequiredfunc, char **required
 	dfunction_t *dfunctions;
 
 	Mem_EmptyPool(prog->progs_mempool);
-	Mem_EmptyPool(prog->edictstring_mempool);
 
 	prog->progs = (dprograms_t *)FS_LoadFile (filename, prog->progs_mempool, false);
 	if (prog->progs == NULL)
@@ -1473,11 +1464,11 @@ void PRVM_LoadProgs (const char * filename, int numrequiredfunc, char **required
 	PRVM_Init_Exec();
 
 	prog->loaded = TRUE;
-	
+
 	// set flags & ddef_ts in prog
-	
+
 	prog->flag = 0;
-	
+
 	prog->self = PRVM_ED_FindGlobal("self");
 
 	if( PRVM_ED_FindGlobal("time") && PRVM_ED_FindGlobal("time")->type & ev_float )
@@ -1487,12 +1478,12 @@ void PRVM_LoadProgs (const char * filename, int numrequiredfunc, char **required
 		prog->flag |= PRVM_FE_CHAIN;
 
 	if(PRVM_ED_FindField ("classname"))
-		prog->flag |= PRVM_FE_CLASSNAME; 
+		prog->flag |= PRVM_FE_CLASSNAME;
 
-	if(PRVM_ED_FindField ("nextthink") && PRVM_ED_FindField ("frame") && PRVM_ED_FindField ("think") 
-		&& prog->flag && prog->self) 
+	if(PRVM_ED_FindField ("nextthink") && PRVM_ED_FindField ("frame") && PRVM_ED_FindField ("think")
+		&& prog->flag && prog->self)
 		prog->flag |= PRVM_OP_STATE;
-	
+
 	PRVM_GCALL(reset_cmd)();
 	PRVM_GCALL(init_cmd)();
 
@@ -1672,7 +1663,7 @@ void PRVM_Global_f(void)
 	if( !global )
 		Con_Printf( "No global '%s' in %s!\n", Cmd_Argv(2), Cmd_Argv(1) );
 	else
-		Con_Printf( "%s: %s\n", Cmd_Argv(2), PRVM_ValueString( global->type, (prvm_eval_t *) &prog->globals[ global->ofs ] ) ); 
+		Con_Printf( "%s: %s\n", Cmd_Argv(2), PRVM_ValueString( global->type, (prvm_eval_t *) &prog->globals[ global->ofs ] ) );
 	PRVM_End;
 }
 
@@ -1696,7 +1687,7 @@ void PRVM_GlobalSet_f(void)
 	global = PRVM_ED_FindGlobal( Cmd_Argv(2) );
 	if( !global )
 		Con_Printf( "No global '%s' in %s!\n", Cmd_Argv(2), Cmd_Argv(1) );
-	else 
+	else
 		PRVM_ED_ParseEpair( NULL, global, Cmd_Argv(3) );
 	PRVM_End;
 }
diff --git a/r_shadow.c b/r_shadow.c
index b0e4e6b9..840783a9 100644
--- a/r_shadow.c
+++ b/r_shadow.c
@@ -3248,7 +3248,7 @@ void R_Shadow_SaveWorldLights(void)
 		{
 			bufmaxchars = bufchars + strlen(line) + 2048;
 			oldbuf = buf;
-			buf = Mem_Alloc(r_shadow_mempool, bufmaxchars);
+			buf = Mem_Alloc(tempmempool, bufmaxchars);
 			if (oldbuf)
 			{
 				if (bufchars)
diff --git a/server.h b/server.h
index 70a8348b..3fcc597b 100644
--- a/server.h
+++ b/server.h
@@ -278,8 +278,7 @@ extern cvar_t sv_gameplayfix_stepwhilejumping;
 extern cvar_t sv_gameplayfix_swiminbmodels;
 extern cvar_t sv_gameplayfix_setmodelrealbox;
 
-extern mempool_t *sv_clients_mempool;
-extern mempool_t *sv_edicts_mempool;
+extern mempool_t *sv_mempool;
 
 // persistant server info
 extern server_static_t svs;
diff --git a/sv_main.c b/sv_main.c
index d85aba28..f90512d5 100644
--- a/sv_main.c
+++ b/sv_main.c
@@ -46,7 +46,7 @@ cvar_t sv_progs = {0, "sv_progs", "progs.dat" };
 server_t sv;
 server_static_t svs;
 
-mempool_t *sv_edicts_mempool = NULL;
+mempool_t *sv_mempool = NULL;
 
 //============================================================================
 
@@ -91,7 +91,7 @@ void SV_Init (void)
 	SV_Phys_Init();
 	SV_World_Init();
 
-	sv_edicts_mempool = Mem_AllocPool("server edicts", 0, NULL);
+	sv_mempool = Mem_AllocPool("server", 0, NULL);
 }
 
 static void SV_SaveEntFile_f(void)
@@ -307,11 +307,11 @@ void SV_SendServerinfo (client_t *client)
 		EntityFrame5_FreeDatabase(client->entitydatabase5);
 
 	if (sv.protocol == PROTOCOL_DARKPLACES1 || sv.protocol == PROTOCOL_DARKPLACES2 || sv.protocol == PROTOCOL_DARKPLACES3)
-		client->entitydatabase = EntityFrame_AllocDatabase(sv_clients_mempool);
+		client->entitydatabase = EntityFrame_AllocDatabase(sv_mempool);
 	if (sv.protocol == PROTOCOL_DARKPLACES4)
-		client->entitydatabase4 = EntityFrame4_AllocDatabase(sv_clients_mempool);
+		client->entitydatabase4 = EntityFrame4_AllocDatabase(sv_mempool);
 	if (sv.protocol == PROTOCOL_DARKPLACES5 || sv.protocol == PROTOCOL_DARKPLACES6)
-		client->entitydatabase5 = EntityFrame5_AllocDatabase(sv_clients_mempool);
+		client->entitydatabase5 = EntityFrame5_AllocDatabase(sv_mempool);
 
 	MSG_WriteByte (&client->message, svc_print);
 	dpsnprintf (message, sizeof (message), "\002\nServer: %s build %s (progs %i crc)", gamename, buildstring, pr_crc);
@@ -756,7 +756,7 @@ void SV_MarkWriteEntityStateToClient(entity_state_t *s)
 	sententities[s->number] = sententitiesmark;
 }
 
-entity_state_t sendstates[MAX_EDICTS]; 
+entity_state_t sendstates[MAX_EDICTS];
 
 void SV_WriteEntitiesToClient(client_t *client, edict_t *clent, sizebuf_t *msg, int *stats)
 {
@@ -1567,9 +1567,9 @@ void SV_IncreaseEdicts(void)
 	SV_ClearWorld();
 
 	sv.max_edicts   = min(sv.max_edicts + 256, MAX_EDICTS);
-	sv.edictsengineprivate = Mem_Alloc(sv_edicts_mempool, sv.max_edicts * sizeof(edict_engineprivate_t));
-	sv.edictsfields = Mem_Alloc(sv_edicts_mempool, sv.max_edicts * pr_edict_size);
-	sv.moved_edicts = Mem_Alloc(sv_edicts_mempool, sv.max_edicts * sizeof(edict_t *));
+	sv.edictsengineprivate = PR_Alloc(sv.max_edicts * sizeof(edict_engineprivate_t));
+	sv.edictsfields = PR_Alloc(sv.max_edicts * pr_edict_size);
+	sv.moved_edicts = PR_Alloc(sv.max_edicts * sizeof(edict_t *));
 
 	memcpy(sv.edictsengineprivate, oldedictsengineprivate, oldmax_edicts * sizeof(edict_engineprivate_t));
 	memcpy(sv.edictsfields, oldedictsfields, oldmax_edicts * pr_edict_size);
@@ -1583,9 +1583,9 @@ void SV_IncreaseEdicts(void)
 			SV_LinkEdict(ent, false);
 	}
 
-	Mem_Free(oldedictsengineprivate);
-	Mem_Free(oldedictsfields);
-	Mem_Free(oldmoved_edicts);
+	PR_Free(oldedictsengineprivate);
+	PR_Free(oldedictsfields);
+	PR_Free(oldmoved_edicts);
 }
 
 /*
@@ -1690,16 +1690,14 @@ void SV_SpawnServer (const char *server)
 	sv.max_edicts = max(svs.maxclients + 1, 512);
 	sv.max_edicts = min(sv.max_edicts, MAX_EDICTS);
 
-	// clear the edict memory pool
-	Mem_EmptyPool(sv_edicts_mempool);
 	// edict_t structures (hidden from progs)
-	sv.edicts = Mem_Alloc(sv_edicts_mempool, MAX_EDICTS * sizeof(edict_t));
+	sv.edicts = PR_Alloc(MAX_EDICTS * sizeof(edict_t));
 	// engine private structures (hidden from progs)
-	sv.edictsengineprivate = Mem_Alloc(sv_edicts_mempool, sv.max_edicts * sizeof(edict_engineprivate_t));
+	sv.edictsengineprivate = PR_Alloc(sv.max_edicts * sizeof(edict_engineprivate_t));
 	// progs fields, often accessed by server
-	sv.edictsfields = Mem_Alloc(sv_edicts_mempool, sv.max_edicts * pr_edict_size);
+	sv.edictsfields = PR_Alloc(sv.max_edicts * pr_edict_size);
 	// used by PushMove to move back pushed entities
-	sv.moved_edicts = Mem_Alloc(sv_edicts_mempool, sv.max_edicts * sizeof(edict_t *));
+	sv.moved_edicts = PR_Alloc(sv.max_edicts * sizeof(edict_t *));
 	for (i = 0;i < sv.max_edicts;i++)
 	{
 		ent = sv.edicts + i;
diff --git a/ui.c b/ui.c
index 0d1b67fd..1e4ed791 100644
--- a/ui.c
+++ b/ui.c
@@ -1,7 +1,7 @@
 
 #include "quakedef.h"
 
-// here is the real ui drawing engine 
+// here is the real ui drawing engine
 
 /*
 #define FRAME_THICKNESS	2
@@ -22,14 +22,14 @@ static void UIG_DrawFrame(float x, float y, float w, float h)
 	DrawQ_Fill(x + w, y, FRAME_THICKNESS, h, FRAME_COLOR1);
 	// area
 	DrawQ_Fill(x, y, w, h, FRAME_COLOR2);
-} 
+}
 
 static void UIG_DrawText(const char *text, float x, float y, float w, float h, float r, float g, float b, float a, float f)
 {
 	if(w != 0 && h != 0)
 		DrawQ_SetClipArea(x, y, w, h);
 	DrawQ_String(x, y, text, 0, TEXT_FONTSIZE_X, TEXT_FONTSIZE_Y, r, g, b, a, f);
-	if(w != 0 && h != 0)	
+	if(w != 0 && h != 0)
 		DrawQ_ResetClipArea();
 }
 
@@ -42,17 +42,14 @@ static void UIG_DrawCursor(float x, float y, float r, float g, float b, float a,
 }
 */
 
-static mempool_t *ui_mem;
-
 //#define UI_MEM_SIZE (1 << 10) << 9 // 512 KByte
 #define UI_MEM_SIZE 1
 
 void UI_Init(void)
 {
-	ui_mem = Mem_AllocPool("Intern UI Memory", 0, NULL);
 }
 
-#define UI_Alloc(size)	Mem_Alloc(ui_mem, size)
+#define UI_Alloc(size)	Mem_Alloc(cl_mempool, size)
 #define UI_Free(ptr)	Mem_Free(ptr)
 
 void UI_Event(ui_itemlist_t list, ui_message_t *in)
@@ -65,9 +62,9 @@ void UI_Event(ui_itemlist_t list, ui_message_t *in)
 		for(item = list->list; item != 0 && !processed; item = item->next)
 		{
 			unsigned int i;
-		
+
 			processed = item->eventhandler(list, item, in, &out);
-		
+
 			// process posted messages
 			for(i = 0; i < out.used; i++)
 				list->eventhandler(list, &out.queue[i]);
@@ -77,7 +74,7 @@ void UI_Event(ui_itemlist_t list, ui_message_t *in)
 		}
 
 	if(!processed)
-		list->eventhandler(list, in);		
+		list->eventhandler(list, in);
 }
 
 void UI_Draw(ui_itemlist_t list)
@@ -95,7 +92,7 @@ void UI_Draw(ui_itemlist_t list)
 	{
 		unsigned int depth = 0, nextdepth = ~0;
 
-		while(depth != nextdepth) 
+		while(depth != nextdepth)
 		{
 			for(item = list->list; item != 0; item = item->next)
 			{
diff --git a/wad.c b/wad.c
index 2183b8e3..35671d6e 100644
--- a/wad.c
+++ b/wad.c
@@ -70,7 +70,6 @@ void *W_GetLumpName(char *name)
 	static int wad_numlumps = 0;
 	static lumpinfo_t *wad_lumps = NULL;
 	static qbyte *wad_base = NULL;
-	static mempool_t *wad_mempool = NULL;
 
 	W_CleanupName (name, clean);
 
@@ -83,8 +82,7 @@ void *W_GetLumpName(char *name)
 				Con_Print("gfx.wad doesn't have WAD2 id\n");
 			else
 			{
-				wad_mempool = Mem_AllocPool("gfx.wad", 0, NULL);
-				wad_base = Mem_Alloc(wad_mempool, fs_filesize);
+				wad_base = Mem_Alloc(cl_mempool, fs_filesize);
 
 				memcpy(wad_base, temp, fs_filesize);
 				Mem_Free(temp);
-- 
2.39.5