Cube  Check-in [0097baa3a7]

Overview
Comment:Remove memory pool
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 0097baa3a78b36f6358e9877ad0c14ed4225486be33b821a408adf2e0213e0c0
User & Date: js on 2025-03-08 01:09:36
Other Links: manifest | tags
Context
2025-03-08
02:38
Improve clang-format check-in: 6f5dd50626 user: js tags: trunk
01:09
Remove memory pool check-in: 0097baa3a7 user: js tags: trunk
00:58
More string migration check-in: 245efe8045 user: js tags: trunk
Changes

Modified src/clientgame.mm from [ce2ec26dd6] to [2ddec801b6].

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 *)gp()->alloc(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 *)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;
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
	};
};

void
zapdynent(dynent *&d)
{
	if (d)
		gp()->dealloc(d, sizeof(dynent));
	d = NULL;
};

extern int democlientnum;

void
otherplayers()







|







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()

Modified src/commands.mm from [ac6583caf8] to [2917ffb85d].

15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
{
	sprintf_s(s)("%d", i);
}

char *
exchangestr(char *o, const char *n)
{
	gp()->deallocstr(o);
	return newstring(n);
}

// contains ALL vars/commands/aliases
static OFMutableDictionary<OFString *, __kindof Identifier *> *identifiers;

void
alias(OFString *name, OFString *action)







|
|







15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
{
	sprintf_s(s)("%d", i);
}

char *
exchangestr(char *o, const char *n)
{
	free(o);
	return strdup(n);
}

// contains ALL vars/commands/aliases
static OFMutableDictionary<OFString *, __kindof Identifier *> *identifiers;

void
alias(OFString *name, OFString *action)
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
			brak--;
		else if (!c) {
			p--;
			conoutf(@"missing \"%c\"", right);
			return NULL;
		}
	}
	char *s = newstring(word, p - word - 1);
	if (left == '(') {
		string t;
		// evaluate () exps directly, and substitute result
		@autoreleasepool {
			itoa(t, execute(@(s)));
		}
		s = exchangestr(s, t);







|







128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
			brak--;
		else if (!c) {
			p--;
			conoutf(@"missing \"%c\"", right);
			return NULL;
		}
	}
	char *s = strndup(word, p - word - 1);
	if (left == '(') {
		string t;
		// evaluate () exps directly, and substitute result
		@autoreleasepool {
			itoa(t, execute(@(s)));
		}
		s = exchangestr(s, t);
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
	p += strspn(p, " \t\r");
	if (p[0] == '/' && p[1] == '/')
		p += strcspn(p, "\n\0");
	if (*p == '\"') {
		p++;
		char *word = p;
		p += strcspn(p, "\"\r\n\0");
		char *s = newstring(word, p - word);
		if (*p == '\"')
			p++;
		return s;
	}
	if (*p == '(')
		return parseexp(p, ')');
	if (*p == '[')
		return parseexp(p, ']');
	char *word = p;
	p += strcspn(p, "; \t\r\n\0");
	if (p - word == 0)
		return NULL;
	return newstring(word, p - word);
}

OFString *
lookup(OFString *n) // find value of ident referenced with $ in exp
{
	@autoreleasepool {
		__kindof Identifier *identifier =







|












|







150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
	p += strspn(p, " \t\r");
	if (p[0] == '/' && p[1] == '/')
		p += strcspn(p, "\n\0");
	if (*p == '\"') {
		p++;
		char *word = p;
		p += strcspn(p, "\"\r\n\0");
		char *s = strndup(word, p - word);
		if (*p == '\"')
			p++;
		return s;
	}
	if (*p == '(')
		return parseexp(p, ')');
	if (*p == '[')
		return parseexp(p, ']');
	char *word = p;
	p += strcspn(p, "; \t\r\n\0");
	if (p - word == 0)
		return NULL;
	return strndup(word, p - word);
}

OFString *
lookup(OFString *n) // find value of ident referenced with $ in exp
{
	@autoreleasepool {
		__kindof Identifier *identifier =

Modified src/console.mm from [2969d1193c] to [de4b6af5c1].

28
29
30
31
32
33
34

35

36
37
38
39
40
41
42
43
44
45
}
COMMANDN(conskip, setconskip, ARG_1INT)

static void
conline(OFString *sf, bool highlight) // add a line to the console buffer
{
	cline cl;

	cl.cref = conlines.length() > 100

	              ? conlines.pop().cref
	              : newstringbuf(""); // constrain the buffer size
	cl.outtime = lastmillis;          // for how long to keep line on screen
	conlines.insert(0, cl);
	if (highlight) // show line in a different colour, for chat etc.
	{
		cl.cref[0] = '\f';
		cl.cref[1] = 0;
		strcat_s(cl.cref, sf.UTF8String);
	} else {







>
|
>
|
<
|







28
29
30
31
32
33
34
35
36
37
38

39
40
41
42
43
44
45
46
}
COMMANDN(conskip, setconskip, ARG_1INT)

static void
conline(OFString *sf, bool highlight) // add a line to the console buffer
{
	cline cl;
	// constrain the buffer size
	cl.cref = conlines.length() > 100 ? conlines.pop().cref
	                                  : (char *)calloc(_MAXDEFSTR, 1);
	// for how long to keep line on screen

	cl.outtime = lastmillis;
	conlines.insert(0, cl);
	if (highlight) // show line in a different colour, for chat etc.
	{
		cl.cref[0] = '\f';
		cl.cref[1] = 0;
		strcat_s(cl.cref, sf.UTF8String);
	} else {

Modified src/monster.mm from [34205145ec] to [6306bf3f2c].

98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
	basicmonster(type, rnd(360), M_SEARCH, 1000, 1);
};

void
monsterclear() // called after map start of when toggling edit mode to
               // reset/spawn all monsters to initial state
{
	loopv(monsters) gp()->dealloc(monsters[i], sizeof(dynent));
	monsters.setsize(0);
	numkilled = 0;
	monstertotal = 0;
	spawnremain = 0;
	if (m_dmsp) {
		nextmonster = mtimestart = lastmillis + 10000;
		monstertotal = spawnremain = gamemode < 0 ? skill * 10 : 0;







|







98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
	basicmonster(type, rnd(360), M_SEARCH, 1000, 1);
};

void
monsterclear() // called after map start of when toggling edit mode to
               // reset/spawn all monsters to initial state
{
	loopv(monsters) free(monsters[i]);
	monsters.setsize(0);
	numkilled = 0;
	monstertotal = 0;
	spawnremain = 0;
	if (m_dmsp) {
		nextmonster = mtimestart = lastmillis + 10000;
		monstertotal = spawnremain = gamemode < 0 ? skill * 10 : 0;

Modified src/tools.h from [217eb7d86e] to [a99286d792].

115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
	}
#define sprintf_sdv(d, fmt) sprintf_sdlv(d, fmt, fmt)

#define fast_f2nat(val) ((int)(val))

extern void endianswap(void *, int, int);

// memory pool that uses buckets and linear allocation for small objects
// VERY fast, and reasonably good memory reuse

struct pool {
	enum { POOLSIZE = 4096 }; // can be absolutely anything
	enum { PTRSIZE = sizeof(char *) };
	enum {
		MAXBUCKETS = 65
	}; // meaning up to size 256 on 32bit pointer systems
	enum { MAXREUSESIZE = MAXBUCKETS * PTRSIZE - PTRSIZE };

	inline size_t
	bucket(size_t s)
	{
		return (s + PTRSIZE - 1) >> PTRBITS;
	}

	enum { PTRBITS = PTRSIZE == 2 ? 1 : PTRSIZE == 4 ? 2 : 3 };

	char *p;
	size_t left;
	char *blocks;
	void *reuse[MAXBUCKETS];

	pool();
	~pool() { dealloc_block(blocks); };

	void *alloc(size_t size);
	void dealloc(void *p, size_t size);
	void *realloc(void *p, size_t oldsize, size_t newsize);

	char *string(const char *s, size_t l);

	char *
	string(const char *s)
	{
		return string(s, strlen(s));
	}

	void
	deallocstr(char *s)
	{
		dealloc(s, strlen(s) + 1);
	}

	char *
	stringbuf(const char *s)
	{
		return string(s, _MAXDEFSTR - 1);
	}

	void dealloc_block(void *b);
	void allocnext(size_t allocsize);
};

pool *gp();

template <class T> struct vector {
	T *buf;
	int alen;
	int ulen;
	pool *p;

	vector()
	{
		this->p = gp();
		alen = 8;
		buf = (T *)p->alloc(alen * sizeof(T));
		ulen = 0;
	}

	~vector()
	{
		setsize(0);
		p->dealloc(buf, alen * sizeof(T));
	}

	vector(vector<T> &v);
	void operator=(vector<T> &v);

	T &
	add(const T &x)







<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<




<



<

|






|







115
116
117
118
119
120
121

























































122
123
124
125

126
127
128

129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
	}
#define sprintf_sdv(d, fmt) sprintf_sdlv(d, fmt, fmt)

#define fast_f2nat(val) ((int)(val))

extern void endianswap(void *, int, int);


























































template <class T> struct vector {
	T *buf;
	int alen;
	int ulen;


	vector()
	{

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

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

	vector(vector<T> &v);
	void operator=(vector<T> &v);

	T &
	add(const T &x)
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
		qsort(buf, ulen, sizeof(T),
		    (int(__cdecl *)(const void *, const void *))cf);
	}

	void
	realloc()
	{
		int olen = alen;
		buf = (T *)p->realloc(
		    buf, olen * sizeof(T), (alen *= 2) * sizeof(T));
	}

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







<
<
|







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++)
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
	} else                                                                 \
		for (int i = 0; i < (v).length(); i++)
#define loopvrev(v)                                                            \
	if (false) {                                                           \
	} else                                                                 \
		for (int i = (v).length() - 1; i >= 0; i--)

inline char *
newstring(const char *s)
{
	return gp()->string(s);
}

inline char *
newstring(const char *s, size_t l)
{
	return gp()->string(s, l);
}

inline char *
newstringbuf(const char *s)
{
	return gp()->stringbuf(s);
}

#endif







<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<

241
242
243
244
245
246
247


















248
	} else                                                                 \
		for (int i = 0; i < (v).length(); i++)
#define loopvrev(v)                                                            \
	if (false) {                                                           \
	} else                                                                 \
		for (int i = (v).length() - 1; i >= 0; i--)



















#endif

Modified src/tools.mm from [06a94d32b1] to [d7e6269b15].

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// implementation of generic tools

#include "tools.h"
#include <new>

//////////////////////////// pool ///////////////////////////

pool::pool()
{
	blocks = 0;
	allocnext(POOLSIZE);
	for (int i = 0; i < MAXBUCKETS; i++)
		reuse[i] = NULL;
};

void *
pool::alloc(size_t size)
{
	if (size > MAXREUSESIZE) {
		return malloc(size);
	} else {
		size = bucket(size);
		void **r = (void **)reuse[size];
		if (r) {
			reuse[size] = *r;
			return (void *)r;
		} else {
			size <<= PTRBITS;
			if (left < size)
				allocnext(POOLSIZE);
			char *r = p;
			p += size;
			left -= size;
			return r;
		};
	};
};

void
pool::dealloc(void *p, size_t size)
{
	if (size > MAXREUSESIZE) {
		free(p);
	} else {
		size = bucket(size);
		if (size) // only needed for 0-size free, are there any?
		{
			*((void **)p) = reuse[size];
			reuse[size] = p;
		};
	};
};

void *
pool::realloc(void *p, size_t oldsize, size_t newsize)
{
	void *np = alloc(newsize);
	if (!oldsize)
		return np;
	memcpy(np, p, newsize > oldsize ? oldsize : newsize);
	dealloc(p, oldsize);
	return np;
};

void
pool::dealloc_block(void *b)
{
	if (b) {
		dealloc_block(*((char **)b));
		free(b);
	};
}

void
pool::allocnext(size_t allocsize)
{
	char *b = (char *)malloc(allocsize + PTRSIZE);
	*((char **)b) = blocks;
	blocks = b;
	p = b + PTRSIZE;
	left = allocsize;
};

char *
pool::string(const char *s, size_t l)
{
	char *b = (char *)alloc(l + 1);
	strncpy(b, s, l);
	b[l] = 0;
	return b;
}

pool *
gp() // useful for global buffers that need to be initialisation order
     // independant
{
	static pool *p = NULL;
	return p ? p : (p = new pool());
};

///////////////////////// misc tools ///////////////////////

void
endianswap(
    void *memory, int stride, int length) // little indians as storage format
{
	if (*((char *)&stride))





<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<







1
2
3
4
5































































































6
7
8
9
10
11
12
// implementation of generic tools

#include "tools.h"
#include <new>
































































































///////////////////////// misc tools ///////////////////////

void
endianswap(
    void *memory, int stride, int length) // little indians as storage format
{
	if (*((char *)&stride))