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];
}
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
quit() // normal exit
{

View file

@ -100,7 +100,7 @@ spawnstate(dynent *d) // reset player state not persistent accross spawns
dynent *
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.y = 0;
d->o.z = 0;
@ -185,8 +185,7 @@ arenarespawn()
void
zapdynent(dynent *&d)
{
if (d)
free(d);
OFFreeMemory(d);
d = NULL;
}
@ -281,9 +280,9 @@ updateworld(int millis) // main game update loop
lastmillis = millis;
}
// brute force but effective way to find a free spawn spot in the map
void
entinmap(dynent *
d) // brute force but effective way to find a free spawn spot in the map
entinmap(dynent *d)
{
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);
if (t > maxremain)
free(undos.remove(i));
OFFreeMemory(undos.remove(i));
}
}
@ -268,7 +268,7 @@ editundo()
}
block *p = undos.pop();
blockpaste(*p);
free(p);
OFFreeMemory(p);
}
block *copybuf = NULL;
@ -278,7 +278,7 @@ copy()
{
EDITSELMP;
if (copybuf)
free(copybuf);
OFFreeMemory(copybuf);
copybuf = blockcopy(sel);
}

View file

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

View file

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

View file

@ -141,7 +141,7 @@ installtex(int tnum, OFIRI *IRI, int *xs, int *ys, bool clamp)
if (*xs != s->w) {
conoutf(@"warning: quality loss: scaling %@",
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,
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;
copysize = mapsize;
if (copydata)
free(copydata);
copydata = (uchar *)alloc(mapsize);
OFFreeMemory(copydata);
copydata = (uchar *)OFAllocMemory(1, mapsize);
memcpy(copydata, mapdata, mapsize);
}

View file

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

View file

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

View file

@ -238,7 +238,8 @@ dodynlight(
block *
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;
sqr *q = (sqr *)(b + 1);
for (int x = s.x; x < s.xs + s.x; x++)