shithub: qk1

Download patch

ref: 1bf25dcef002723ee963d081cb90f57b4ae77e16
parent: 8a9976018f1a147abc3441d98e428592f5c82cfc
author: Sigrid Solveig Haflínudóttir <sigrid@ftrv.se>
date: Wed Oct 11 20:57:27 EDT 2023

memset now-hunk-allocated arrays correctly

--- a/cl_main.c
+++ b/cl_main.c
@@ -26,7 +26,6 @@
 
 client_static_t	cls;
 client_state_t	cl;
-// FIXME: put these on hunk?
 entity_t		**cl_visedicts;
 efrag_t			*cl_efrags;
 entity_t		*cl_entities;
@@ -54,11 +53,12 @@
 	SZ_Clear (&cls.message);
 
 // clear other arrays	
-	memset(cl_efrags, 0, sizeof cl_efrags);
-	memset(cl_entities, 0, sizeof cl_entities);
+	Hunk_Memset0(cl_efrags);
+	Hunk_Memset0(cl_entities);
+	Hunk_Memset0(cl_temp_entities);
+
 	memset(cl_dlights, 0, sizeof cl_dlights);
 	memset(cl_lightstyle, 0, sizeof cl_lightstyle);
-	memset(cl_temp_entities, 0, sizeof cl_temp_entities);
 	memset(cl_beams, 0, sizeof cl_beams);
 
 //
--- a/snd.c
+++ b/snd.c
@@ -583,7 +583,7 @@
 {
 	if(afd < 0)
 		return;
-	memset(chans, 0, sizeof chans);
+	memset(chans, 0, sizeof(*chans)*nchan);
 	che = chans + Sstat;
 }
 
--- a/zone.c
+++ b/zone.c
@@ -99,6 +99,16 @@
 	return m+1;
 }
 
+void
+Hunk_Memset0(void *p)
+{
+	mem_t *m;
+
+	m = p;
+	m--;
+	memset(p, 0, m->size);
+}
+
 void *
 Hunk_Mark(void)
 {
@@ -123,12 +133,13 @@
 	static void	*buf;
 	static int bufsz;
 
-	if(bufsz > size)
-		return buf;
-	buf = realloc(buf, size);
-	if(buf == nil)
-		sysfatal("Hunk_TempAlloc: %r");
-	bufsz = size;
+	if(size > bufsz){
+		buf = realloc(buf, size);
+		if(buf == nil)
+			sysfatal("Hunk_TempAlloc: %r");
+		bufsz = size;
+	}
+	memset(buf, 0, size);
 
 	return buf;
 }
@@ -146,7 +157,6 @@
 		
 	while(cache_head != nil){
 		s = cache_head->next;
-		cache_head->user->data = nil;
 		free(cache_head);
 		cache_head = s;
 	}
@@ -160,12 +170,13 @@
 	if(!c->data)
 		fatal("Cache_Free: not allocated");
 
-	cs = ((mem_t *)c->data) - 1;
+	cs = (mem_t *)c->data - 1;
 	if(cs == cache_head)
 		cache_head = cs->next;
-	cs->prev->next = cs->next;
-	cs->next->prev = cs->prev;
-	cs->next = cs->prev = nil;
+	if(cs->prev != nil)
+		cs->prev->next = cs->next;
+	if(cs->next != nil)
+		cs->next->prev = cs->prev;
 
 	c->data = nil;
 	free(cs);
--- a/zone.h
+++ b/zone.h
@@ -10,6 +10,7 @@
 void Hunk_CacheFrom(mem_user_t *c, void *p);
 void *Hunk_Alloc(int size);
 void *Hunk_Double(void *p);
+void Hunk_Memset0(void *p);
 void *Hunk_Mark(void);
 void Hunk_FreeToMark(void *mark);
 void *Hunk_TempAlloc(int size);