Cube  Check-in [71ebb79f8f]

Overview
Comment:Use ObjFW functions for memory management
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 71ebb79f8f6a5c2920c91b004bf274f9133e3ee04caddaf0f5ba01f98db27762
User & Date: js on 2025-03-08 03:26:23
Other Links: manifest | tags
Context
2025-03-08
03:40
More string migration check-in: 4a87b39dfe user: js tags: trunk
03:26
Use ObjFW functions for memory management check-in: 71ebb79f8f user: js tags: trunk
03:05
More style cleanup check-in: 753ff34122 user: js tags: trunk
Changes

Modified src/Cube.mm from [b4e9c293d7] to [4d1b18c94e].

341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
	OFString *msg =
	    [OFString stringWithFormat:@"%@%@ (%s)\n", s, o, SDL_GetError()];

	[Cube.sharedInstance showMessage:msg];
	[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
{
	[Cube.sharedInstance quit];
}
COMMAND(quit, ARG_NONE)








<
<
<
<
<
<
<
<
<







341
342
343
344
345
346
347









348
349
350
351
352
353
354
	OFString *msg =
	    [OFString stringWithFormat:@"%@%@ (%s)\n", s, o, SDL_GetError()];

	[Cube.sharedInstance showMessage:msg];
	[OFApplication terminateWithStatus:1];
}










void
quit() // normal exit
{
	[Cube.sharedInstance quit];
}
COMMAND(quit, ARG_NONE)

Modified src/clientgame.mm from [1adcd0a4ff] to [1b8431e6e6].

96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
		d->ammo[GUN_SG] = 5;
	}
}

dynent *
newdynent() // create a new blank player or monster
{
	dynent *d = (dynent *)malloc(sizeof(dynent));
	d->o.x = 0;
	d->o.y = 0;
	d->o.z = 0;
	d->yaw = 270;
	d->pitch = 0;
	d->roll = 0;
	d->maxspeed = 22;







|







96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
		d->ammo[GUN_SG] = 5;
	}
}

dynent *
newdynent() // create a new blank player or monster
{
	dynent *d = (dynent *)OFAllocMemory(1, sizeof(dynent));
	d->o.x = 0;
	d->o.y = 0;
	d->o.z = 0;
	d->yaw = 270;
	d->pitch = 0;
	d->roll = 0;
	d->maxspeed = 22;
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
		}
	}
}

void
zapdynent(dynent *&d)
{
	if (d)
		free(d);
	d = NULL;
}

extern int democlientnum;

void
otherplayers()







|
<







181
182
183
184
185
186
187
188

189
190
191
192
193
194
195
		}
	}
}

void
zapdynent(dynent *&d)
{
	OFFreeMemory(d);

	d = NULL;
}

extern int democlientnum;

void
otherplayers()
277
278
279
280
281
282
283

284
285
286
287
288
289
290
291
292
293
			c2sinfo(player1); // do this last, to reduce the
			                  // effective frame lag
		}
	}
	lastmillis = millis;
}


void
entinmap(dynent *
        d) // brute force but effective way to find a free spawn spot in the map
{
	loopi(100) // try max 100 times
	{
		float dx = (rnd(21) - 10) / 10.0f * i; // increasing distance
		float dy = (rnd(21) - 10) / 10.0f * i;
		d->o.x += dx;
		d->o.y += dy;







>

|
<







276
277
278
279
280
281
282
283
284
285

286
287
288
289
290
291
292
			c2sinfo(player1); // do this last, to reduce the
			                  // effective frame lag
		}
	}
	lastmillis = millis;
}

// brute force but effective way to find a free spawn spot in the map
void
entinmap(dynent *d)

{
	loopi(100) // try max 100 times
	{
		float dx = (rnd(21) - 10) / 10.0f * i; // increasing distance
		float dy = (rnd(21) - 10) / 10.0f * i;
		d->o.x += dx;
		d->o.y += dy;

Modified src/editing.mm from [46292a408f] to [0154fefe79].

243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
pruneundos(int maxremain) // bound memory
{
	int t = 0;
	loopvrev(undos)
	{
		t += undos[i]->xs * undos[i]->ys * sizeof(sqr);
		if (t > maxremain)
			free(undos.remove(i));
	}
}

void
makeundo()
{
	undos.add(blockcopy(sel));
	pruneundos(undomegs << 20);
}

void
editundo()
{
	EDITMP;
	if (undos.empty()) {
		conoutf(@"nothing more to undo");
		return;
	}
	block *p = undos.pop();
	blockpaste(*p);
	free(p);
}

block *copybuf = NULL;

void
copy()
{
	EDITSELMP;
	if (copybuf)
		free(copybuf);
	copybuf = blockcopy(sel);
}

void
paste()
{
	EDITMP;







|




















|









|







243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
pruneundos(int maxremain) // bound memory
{
	int t = 0;
	loopvrev(undos)
	{
		t += undos[i]->xs * undos[i]->ys * sizeof(sqr);
		if (t > maxremain)
			OFFreeMemory(undos.remove(i));
	}
}

void
makeundo()
{
	undos.add(blockcopy(sel));
	pruneundos(undomegs << 20);
}

void
editundo()
{
	EDITMP;
	if (undos.empty()) {
		conoutf(@"nothing more to undo");
		return;
	}
	block *p = undos.pop();
	blockpaste(*p);
	OFFreeMemory(p);
}

block *copybuf = NULL;

void
copy()
{
	EDITSELMP;
	if (copybuf)
		OFFreeMemory(copybuf);
	copybuf = blockcopy(sel);
}

void
paste()
{
	EDITMP;

Modified src/protos.h from [dbbc22637c] to [10883ae4ba].

136
137
138
139
140
141
142
143
144
145
146
147
148
149
150

// worldocull
extern void computeraytable(float vx, float vy);
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);
extern void draw_textf(
    OFConstantString *format, int left, int top, int gl_num, ...);
extern int text_width(OFString *string);
extern void draw_envbox(int t, int fogdist);







<







136
137
138
139
140
141
142

143
144
145
146
147
148
149

// worldocull
extern void computeraytable(float vx, float vy);
extern int isoccluded(float vx, float vy, float cx, float cy, float csize);

// main
extern void fatal(OFString *s, OFString *o = @"");


// rendertext
extern void draw_text(OFString *string, int left, int top, int gl_num);
extern void draw_textf(
    OFConstantString *format, int left, int top, int gl_num, ...);
extern int text_width(OFString *string);
extern void draw_envbox(int t, int fogdist);

Modified src/rendercubes.mm from [50158effb9] to [a098c5d67b].

14
15
16
17
18
19
20

21
22
23
24
25
26
27
28
29
30
31
	glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(vertex), &verts[0].r);
	glTexCoordPointer(2, GL_FLOAT, sizeof(vertex), &verts[0].u);
}

void
reallocv()
{

	verts = (vertex *)realloc(verts, (curmaxverts *= 2) * sizeof(vertex));
	curmaxverts -= 10;
	if (!verts)
		fatal(@"no vertex memory!");
	setarraypointers();
}

// generating the actual vertices is done dynamically every frame and sits at
// the leaves of all these functions, and are part of the cpu bottleneck on
// really slow machines, hence the macros.








>
|

<
<







14
15
16
17
18
19
20
21
22
23


24
25
26
27
28
29
30
	glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(vertex), &verts[0].r);
	glTexCoordPointer(2, GL_FLOAT, sizeof(vertex), &verts[0].u);
}

void
reallocv()
{
	verts =
	    (vertex *)OFResizeMemory(verts, (curmaxverts *= 2), sizeof(vertex));
	curmaxverts -= 10;


	setarraypointers();
}

// generating the actual vertices is done dynamically every frame and sits at
// the leaves of all these functions, and are part of the cpu bottleneck on
// really slow machines, hence the macros.

Modified src/rendergl.mm from [9007a349cc] to [ed4ed00e36].

137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
		}

		void *scaledimg = s->pixels;

		if (*xs != s->w) {
			conoutf(@"warning: quality loss: scaling %@",
			    IRI.string); // for voodoo cards under linux
			scaledimg = alloc(*xs * *ys * 3);
			gluScaleImage(GL_RGB, s->w, s->h, GL_UNSIGNED_BYTE,
			    s->pixels, *xs, *ys, GL_UNSIGNED_BYTE, scaledimg);
		}

		if (gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, *xs, *ys, GL_RGB,
		        GL_UNSIGNED_BYTE, scaledimg))
			fatal(@"could not build mipmaps");







|







137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
		}

		void *scaledimg = s->pixels;

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

		if (gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, *xs, *ys, GL_RGB,
		        GL_UNSIGNED_BYTE, scaledimg))
			fatal(@"could not build mipmaps");

Modified src/serverutil.mm from [34361918f4] to [89ca7e5bbf].

104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
sendmaps(int n, OFString *mapname, int mapsize, uchar *mapdata)
{
	if (mapsize <= 0 || mapsize > 256 * 256)
		return;
	copyname = mapname;
	copysize = mapsize;
	if (copydata)
		free(copydata);
	copydata = (uchar *)alloc(mapsize);
	memcpy(copydata, mapdata, mapsize);
}

ENetPacket *
recvmap(int n)
{
	if (!copydata)







|
|







104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
sendmaps(int n, OFString *mapname, int mapsize, uchar *mapdata)
{
	if (mapsize <= 0 || mapsize > 256 * 256)
		return;
	copyname = mapname;
	copysize = mapsize;
	if (copydata)
		OFFreeMemory(copydata);
	copydata = (uchar *)OFAllocMemory(1, mapsize);
	memcpy(copydata, mapdata, mapsize);
}

ENetPacket *
recvmap(int n)
{
	if (!copydata)

Modified src/tools.h from [fd4e5832e2] to [c2f36ac343].

21
22
23
24
25
26
27


28
29
30
31
32
33
34
#include <string.h>
#ifdef __GNUC__
# include <new>
#else
# include <new.h>
#endif



#ifdef NULL
# undef NULL
#endif
#define NULL 0

typedef unsigned char uchar;
typedef unsigned short ushort;







>
>







21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <string.h>
#ifdef __GNUC__
# include <new>
#else
# include <new.h>
#endif

#import <ObjFW/ObjFW.h>

#ifdef NULL
# undef NULL
#endif
#define NULL 0

typedef unsigned char uchar;
typedef unsigned short ushort;
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
	T *buf;
	int alen;
	int ulen;

	vector()
	{
		alen = 8;
		buf = (T *)malloc(alen * sizeof(T));
		ulen = 0;
	}

	~vector()
	{
		setsize(0);
		free(buf);







|







125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
	T *buf;
	int alen;
	int ulen;

	vector()
	{
		alen = 8;
		buf = (T *)OFAllocMemory(alen, sizeof(T));
		ulen = 0;
	}

	~vector()
	{
		setsize(0);
		free(buf);
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
		qsort(buf, ulen, sizeof(T),
		    (int(__cdecl *)(const void *, const void *))cf);
	}

	void
	realloc()
	{
		buf = (T *)::realloc(buf, (alen *= 2) * sizeof(T));
	}

	T
	remove(int i)
	{
		T e = buf[i];
		for (int p = i + 1; p < ulen; p++)







|







210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
		qsort(buf, ulen, sizeof(T),
		    (int(__cdecl *)(const void *, const void *))cf);
	}

	void
	realloc()
	{
		buf = (T *)OFResizeMemory(buf, (alen *= 2), sizeof(T));
	}

	T
	remove(int i)
	{
		T e = buf[i];
		for (int p = i + 1; p < ulen; p++)

Modified src/world.mm from [e205b06d1f] to [2ffb0794b1].

419
420
421
422
423
424
425
426
427
428
429
430
431
432
433


434
435
436
437
438
439
440
441
442
443
444

void
setupworld(int factor)
{
	ssize = 1 << (sfactor = factor);
	cubicsize = ssize * ssize;
	mipsize = cubicsize * 134 / 100;
	sqr *w = world = (sqr *)alloc(mipsize * sizeof(sqr));
	loopi(LARGEST_FACTOR * 2)
	{
		wmip[i] = w;
		w += cubicsize >> (i * 2);
	}
}



void
empty_world(
    int factor, bool force) // main empty world creation routine, if passed
                            // factor -1 will enlarge old world by 1
{
	if (!force && noteditmode())
		return;
	cleardlights();
	pruneundos();
	sqr *oldworld = world;
	bool copy = false;







|







>
>

|
<
<







419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437


438
439
440
441
442
443
444

void
setupworld(int factor)
{
	ssize = 1 << (sfactor = factor);
	cubicsize = ssize * ssize;
	mipsize = cubicsize * 134 / 100;
	sqr *w = world = (sqr *)OFAllocZeroedMemory(mipsize, sizeof(sqr));
	loopi(LARGEST_FACTOR * 2)
	{
		wmip[i] = w;
		w += cubicsize >> (i * 2);
	}
}

// main empty world creation routine, if passed factor -1 will enlarge old
// world by 1
void
empty_world(int factor, bool force)


{
	if (!force && noteditmode())
		return;
	cleardlights();
	pruneundos();
	sqr *oldworld = world;
	bool copy = false;
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
		block b = { 8, 8, ssize - 16, ssize - 16 };
		edittypexy(SPACE, b);
	}

	calclight();
	startmap(@"base/unnamed");
	if (oldworld) {
		free(oldworld);
		toggleedit();
		execute(@"fullbright 1");
	}
}

void
mapenlarge()







|







491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
		block b = { 8, 8, ssize - 16, ssize - 16 };
		edittypexy(SPACE, b);
	}

	calclight();
	startmap(@"base/unnamed");
	if (oldworld) {
		OFFreeMemory(oldworld);
		toggleedit();
		execute(@"fullbright 1");
	}
}

void
mapenlarge()

Modified src/worldlight.mm from [2af1475366] to [6a518301f9].

234
235
236
237
238
239
240

241
242
243
244
245
246
247
248
}

// utility functions also used by editing code

block *
blockcopy(block &s)
{

	block *b = (block *)alloc(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++)
		for (int y = s.y; y < s.ys + s.y; y++)
			*q++ = *S(x, y);
	return b;
}







>
|







234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
}

// utility functions also used by editing code

block *
blockcopy(block &s)
{
	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++)
		for (int y = s.y; y < s.ys + s.y; y++)
			*q++ = *S(x, y);
	return b;
}