Cube  Check-in [9e3f7517ee]

Overview
Comment:tools.h: Some cleanups
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 9e3f7517ee9d4fe290eb8b0eba53bb9c077e3be3a952fd970469d4c94cc11f4e
User & Date: js on 2025-03-04 01:05:07
Other Links: manifest | tags
Context
2025-03-04
01:13
More string cleanups check-in: a401a49dd9 user: js tags: trunk
01:05
tools.h: Some cleanups check-in: 9e3f7517ee user: js tags: trunk
01:01
Remove hashtable check-in: 67c1421660 user: js tags: trunk
Changes

Modified src/tools.h from [1573940bd5] to [fecbc01816].

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
108
109
110
111
112
113
114
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
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
108
109
110
111
112
113
114
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







+
-
+




+
-
+





-
+






-
+



+

+








-
+
















-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
-















+




+
-
+







typedef char string[_MAXDEFSTR];

inline void
strn0cpy(char *d, const char *s, size_t m)
{
	strncpy(d, s, m);
	d[(m)-1] = 0;
}
};

inline void
strcpy_s(char *d, const char *s)
{
	strn0cpy(d, s, _MAXDEFSTR);
}
};

inline void
strcat_s(char *d, const char *s)
{
	size_t n = strlen(d);
	strn0cpy(d + n, s, _MAXDEFSTR - n);
};
}

inline void
formatstring(char *d, const char *fmt, va_list v)
{
	_vsnprintf(d, _MAXDEFSTR, fmt, v);
	d[_MAXDEFSTR - 1] = 0;
};
}

struct sprintf_s_f {
	char *d;

	sprintf_s_f(char *str) : d(str) {};

	void
	operator()(const char *fmt, ...)
	{
		va_list v;
		va_start(v, fmt);
		_vsnprintf(d, _MAXDEFSTR, fmt, v);
		va_end(v);
		d[_MAXDEFSTR - 1] = 0;
	};
	}
};

#define sprintf_s(d) sprintf_s_f((char *)d)
#define sprintf_sd(d)                                                          \
	string d;                                                              \
	sprintf_s(d)
#define sprintf_sdlv(d, last, fmt)                                             \
	string d;                                                              \
	{                                                                      \
		va_list ap;                                                    \
		va_start(ap, last);                                            \
		formatstring(d, fmt, ap);                                      \
		va_end(ap);                                                    \
	}
#define sprintf_sdv(d, fmt) sprintf_sdlv(d, fmt, fmt)

// fast pentium f2i

#ifdef _MSC_VER
inline int
fast_f2nat(float a)
{ // only for positive floats
	static const float fhalf = 0.5f;
	int retval;

	__asm fld a __asm fsub fhalf __asm fistp retval // perf regalloc?

	    return retval;
};
#else
# define fast_f2nat(val) ((int)(val))
#define fast_f2nat(val) ((int)(val))
#endif

extern char *path(char *s);
extern char *loadfile(char *fn, int *size);
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];

200
201
202
203
204
205
206
207

208
209
210
211
212
213

214
215
216
217
218
219
220
221
222
223
224
225

226
227
228
229
230
231
232
233
234

235
236
237
238
239

240

241
242
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

289
290
291
292
293
294
295
296
297
298

299
300
301
302
303
304
305
306
307
308

309
310
311
312
313
314
315
191
192
193
194
195
196
197

198
199
200
201
202
203

204
205
206
207
208
209
210
211
212
213
214
215

216
217
218
219
220
221
222
223
224

225
226
227
228
229
230
231

232
233
234
235
236
237

238
239
240
241
242

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
289
290
291
292
293

294
295
296
297
298
299
300
301
302
303

304
305
306
307
308
309
310
311







-
+





-
+











-
+








-
+





+
-
+




+
-
+




-
+





+
-
+





+
-
+





+
-
+




-
+






-
+







-
+









-
+









-
+








	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)
	{
		if (ulen == alen)
			realloc();
		new (&buf[ulen]) T(x);
		return buf[ulen++];
	};
	}

	T &
	add()
	{
		if (ulen == alen)
			realloc();
		new (&buf[ulen]) T;
		return buf[ulen++];
	};
	}

	T &
	pop()
	{
		return buf[--ulen];
	}
	};

	T &
	last()
	{
		return buf[ulen - 1];
	}
	};

	bool
	empty()
	{
		return ulen == 0;
	};
	}

	int
	length()
	{
		return ulen;
	}
	};

	T &
	operator[](int i)
	{
		assert(i >= 0 && i < ulen);
		return buf[i];
	}
	};

	void
	setsize(int i)
	{
		for (; ulen > i; ulen--)
			buf[ulen - 1].~T();
	}
	};

	T *
	getbuf()
	{
		return buf;
	};
	}

	void
	sort(void *cf)
	{
		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++)
			buf[p - 1] = buf[p];
		ulen--;
		return e;
	};
	}

	T &
	insert(int i, const T &e)
	{
		add(T());
		for (int p = ulen - 1; p > i; p--)
			buf[p] = buf[p - 1];
		buf[i] = e;
		return buf[i];
	};
	}
};

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