Use ObjFW functions for memory management

FossilOrigin-Name: 71ebb79f8f6a5c2920c91b004bf274f9133e3ee04caddaf0f5ba01f98db27762
This commit is contained in:
Jonathan Schleifer 2025-03-08 03:26:23 +00:00
parent a28209edcf
commit 3d8735818d
10 changed files with 23 additions and 32 deletions

View file

@ -345,15 +345,6 @@ fatal(OFString *s, OFString *o) // failure exit
[OFApplication terminateWithStatus:1]; [OFApplication terminateWithStatus:1];
} }
void *
alloc(int s) // for some big chunks... most other allocs use the memory pool
{
void *b = calloc(1, s);
if (!b)
fatal(@"out of memory!");
return b;
}
void void
quit() // normal exit quit() // normal exit
{ {

View file

@ -100,7 +100,7 @@ spawnstate(dynent *d) // reset player state not persistent accross spawns
dynent * dynent *
newdynent() // create a new blank player or monster newdynent() // create a new blank player or monster
{ {
dynent *d = (dynent *)malloc(sizeof(dynent)); dynent *d = (dynent *)OFAllocMemory(1, sizeof(dynent));
d->o.x = 0; d->o.x = 0;
d->o.y = 0; d->o.y = 0;
d->o.z = 0; d->o.z = 0;
@ -185,8 +185,7 @@ arenarespawn()
void void
zapdynent(dynent *&d) zapdynent(dynent *&d)
{ {
if (d) OFFreeMemory(d);
free(d);
d = NULL; d = NULL;
} }
@ -281,9 +280,9 @@ updateworld(int millis) // main game update loop
lastmillis = millis; lastmillis = millis;
} }
// brute force but effective way to find a free spawn spot in the map
void void
entinmap(dynent * entinmap(dynent *d)
d) // brute force but effective way to find a free spawn spot in the map
{ {
loopi(100) // try max 100 times loopi(100) // try max 100 times
{ {

View file

@ -247,7 +247,7 @@ pruneundos(int maxremain) // bound memory
{ {
t += undos[i]->xs * undos[i]->ys * sizeof(sqr); t += undos[i]->xs * undos[i]->ys * sizeof(sqr);
if (t > maxremain) if (t > maxremain)
free(undos.remove(i)); OFFreeMemory(undos.remove(i));
} }
} }
@ -268,7 +268,7 @@ editundo()
} }
block *p = undos.pop(); block *p = undos.pop();
blockpaste(*p); blockpaste(*p);
free(p); OFFreeMemory(p);
} }
block *copybuf = NULL; block *copybuf = NULL;
@ -278,7 +278,7 @@ copy()
{ {
EDITSELMP; EDITSELMP;
if (copybuf) if (copybuf)
free(copybuf); OFFreeMemory(copybuf);
copybuf = blockcopy(sel); copybuf = blockcopy(sel);
} }

View file

@ -140,7 +140,6 @@ extern int isoccluded(float vx, float vy, float cx, float cy, float csize);
// main // main
extern void fatal(OFString *s, OFString *o = @""); extern void fatal(OFString *s, OFString *o = @"");
extern void *alloc(int s);
// rendertext // rendertext
extern void draw_text(OFString *string, int left, int top, int gl_num); extern void draw_text(OFString *string, int left, int top, int gl_num);

View file

@ -18,10 +18,9 @@ setarraypointers()
void void
reallocv() reallocv()
{ {
verts = (vertex *)realloc(verts, (curmaxverts *= 2) * sizeof(vertex)); verts =
(vertex *)OFResizeMemory(verts, (curmaxverts *= 2), sizeof(vertex));
curmaxverts -= 10; curmaxverts -= 10;
if (!verts)
fatal(@"no vertex memory!");
setarraypointers(); setarraypointers();
} }

View file

@ -141,7 +141,7 @@ installtex(int tnum, OFIRI *IRI, int *xs, int *ys, bool clamp)
if (*xs != s->w) { if (*xs != s->w) {
conoutf(@"warning: quality loss: scaling %@", conoutf(@"warning: quality loss: scaling %@",
IRI.string); // for voodoo cards under linux IRI.string); // for voodoo cards under linux
scaledimg = alloc(*xs * *ys * 3); scaledimg = OFAllocMemory(1, *xs * *ys * 3);
gluScaleImage(GL_RGB, s->w, s->h, GL_UNSIGNED_BYTE, gluScaleImage(GL_RGB, s->w, s->h, GL_UNSIGNED_BYTE,
s->pixels, *xs, *ys, GL_UNSIGNED_BYTE, scaledimg); s->pixels, *xs, *ys, GL_UNSIGNED_BYTE, scaledimg);
} }

View file

@ -108,8 +108,8 @@ sendmaps(int n, OFString *mapname, int mapsize, uchar *mapdata)
copyname = mapname; copyname = mapname;
copysize = mapsize; copysize = mapsize;
if (copydata) if (copydata)
free(copydata); OFFreeMemory(copydata);
copydata = (uchar *)alloc(mapsize); copydata = (uchar *)OFAllocMemory(1, mapsize);
memcpy(copydata, mapdata, mapsize); memcpy(copydata, mapdata, mapsize);
} }

View file

@ -25,6 +25,8 @@
# include <new.h> # include <new.h>
#endif #endif
#import <ObjFW/ObjFW.h>
#ifdef NULL #ifdef NULL
# undef NULL # undef NULL
#endif #endif
@ -127,7 +129,7 @@ template <class T> struct vector {
vector() vector()
{ {
alen = 8; alen = 8;
buf = (T *)malloc(alen * sizeof(T)); buf = (T *)OFAllocMemory(alen, sizeof(T));
ulen = 0; ulen = 0;
} }
@ -212,7 +214,7 @@ template <class T> struct vector {
void void
realloc() realloc()
{ {
buf = (T *)::realloc(buf, (alen *= 2) * sizeof(T)); buf = (T *)OFResizeMemory(buf, (alen *= 2), sizeof(T));
} }
T T

View file

@ -423,7 +423,7 @@ setupworld(int factor)
ssize = 1 << (sfactor = factor); ssize = 1 << (sfactor = factor);
cubicsize = ssize * ssize; cubicsize = ssize * ssize;
mipsize = cubicsize * 134 / 100; mipsize = cubicsize * 134 / 100;
sqr *w = world = (sqr *)alloc(mipsize * sizeof(sqr)); sqr *w = world = (sqr *)OFAllocZeroedMemory(mipsize, sizeof(sqr));
loopi(LARGEST_FACTOR * 2) loopi(LARGEST_FACTOR * 2)
{ {
wmip[i] = w; wmip[i] = w;
@ -431,10 +431,10 @@ setupworld(int factor)
} }
} }
// main empty world creation routine, if passed factor -1 will enlarge old
// world by 1
void void
empty_world( empty_world(int factor, bool force)
int factor, bool force) // main empty world creation routine, if passed
// factor -1 will enlarge old world by 1
{ {
if (!force && noteditmode()) if (!force && noteditmode())
return; return;
@ -495,7 +495,7 @@ empty_world(
calclight(); calclight();
startmap(@"base/unnamed"); startmap(@"base/unnamed");
if (oldworld) { if (oldworld) {
free(oldworld); OFFreeMemory(oldworld);
toggleedit(); toggleedit();
execute(@"fullbright 1"); execute(@"fullbright 1");
} }

View file

@ -238,7 +238,8 @@ dodynlight(
block * block *
blockcopy(block &s) blockcopy(block &s)
{ {
block *b = (block *)alloc(sizeof(block) + s.xs * s.ys * sizeof(sqr)); block *b = (block *)OFAllocZeroedMemory(
1, sizeof(block) + s.xs * s.ys * sizeof(sqr));
*b = s; *b = s;
sqr *q = (sqr *)(b + 1); sqr *q = (sqr *)(b + 1);
for (int x = s.x; x < s.xs + s.x; x++) for (int x = s.x; x < s.xs + s.x; x++)