summaryrefslogtreecommitdiffstatshomepage
path: root/src/emu/netlist/pstring.c
blob: 6287fac8109a445dc166b9766553c967a19bca3d (plain) (blame)
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
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
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
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
/*
 * nl_string.c
 *
 */

#include "pstring.h"
#include <cstdio>


// The following will work on linux, however not on Windows ....

//pblockpool *pstring::m_pool = new pblockpool;
//pstring::str_t *pstring::m_zero = new(pstring::m_pool, 0) pstring::str_t(0);

pblockpool pstring::m_pool;

pstring::str_t *pstring::m_zero = NULL;

/*
 * Uncomment the following to override defaults
 */

//#define IMMEDIATE_MODE  (1)
//#define DEBUG_MODE      (0)

#ifdef MAME_DEBUG
	#ifndef IMMEDIATE_MODE
		#define IMMEDIATE_MODE  (0)
	#endif
	#ifndef DEBUG_MODE
		#define DEBUG_MODE      (1)
	#endif
#else
	#ifndef IMMEDIATE_MODE
		#define IMMEDIATE_MODE  (1)
	#endif
	#ifndef DEBUG_MODE
		#define DEBUG_MODE      (0)
	#endif
#endif

pstring::~pstring()
{
	sfree(m_ptr);
}

void pstring::pcat(const char *s)
{
	int slen = strlen(s);
	str_t *n = salloc(m_ptr->len() + slen);
	if (m_ptr->len() > 0)
		memcpy(n->str(), m_ptr->str(), m_ptr->len());
	if (slen > 0)
		memcpy(n->str() + m_ptr->len(), s, slen);
	*(n->str() + n->len()) = 0;
	sfree(m_ptr);
	m_ptr = n;
}

void pstring::pcopy(const char *from, int size)
{
	str_t *n = salloc(size);
	if (size > 0)
		memcpy(n->str(), from, size);
	*(n->str() + size) = 0;
	sfree(m_ptr);
	m_ptr = n;
}

pstring pstring::substr(unsigned int start, int count) const
{
	int alen = len();
	if (start >= alen)
		return pstring();
	if (count <0 || start + count > alen)
		count = alen - start;
	pstring ret;
	ret.pcopy(cstr() + start, count);
	return ret;
}

pstring pstring::ucase() const
{
	pstring ret = *this;
	ret.pcopy(cstr(), len());
	for (int i=0; i<ret.len(); i++)
		ret.m_ptr->m_str[i] = toupper((unsigned) ret.m_ptr->m_str[i]);
	return ret;
}

//-------------------------------------------------
//  pcmpi - compare a character array to an nstring
//-------------------------------------------------

int pstring::pcmpi(const char *lhs, const char *rhs, int count) const
{
	// loop while equal until we hit the end of strings
	int index;
	for (index = 0; index < count; index++)
		if (lhs[index] == 0 || tolower(lhs[index]) != tolower(rhs[index]))
			break;

	// determine the final result
	if (index < count)
		return tolower(lhs[index]) - tolower(rhs[index]);
	if (lhs[index] == 0)
		return 0;
	return 1;
}

double pstring::as_double(bool *error) const
{
	double ret;
	char *e = NULL;

	if (error != NULL)
		*error = false;
	ret = strtod(cstr(), &e);
	if (*e != 0)
		if (error != NULL)
			*error = true;
	return ret;
}

long pstring::as_long(bool *error) const
{
	double ret;
	char *e = NULL;

	if (error != NULL)
		*error = false;
	if (startsWith("0x"))
		ret = strtol(&(cstr()[2]), &e, 16);
	else
		ret = strtol(cstr(), &e, 10);
	if (*e != 0)
		if (error != NULL)
			*error = true;
	return ret;
}

pstring pstring::vprintf(va_list args) const
{
	// sprintf into the temporary buffer
	char tempbuf[4096];
	vsprintf(tempbuf, cstr(), args);

	return pstring(tempbuf);
}

// ----------------------------------------------------------------------------------------
// static stuff ...
// ----------------------------------------------------------------------------------------

void pstring::sfree(str_t *s)
{
	s->m_ref_count--;
	if (s->m_ref_count == 0)
		m_pool.dealloc(s);
}

pstring::str_t *pstring::salloc(int n)
{
	str_t *ret = new(m_pool, n+1) str_t(n);
	return ret;
}

pstring pstring::sprintf(const char *format, ...)
{
	va_list ap;
	va_start(ap, format);
	pstring ret = pstring(format).vprintf(ap);
	va_end(ap);
	return ret;
}


void pstring::resetmem()
{
	// Release the 0 string
	if (m_zero != NULL)
		sfree(m_zero);
	m_zero = NULL;
	m_pool.m_shutdown = true;
	m_pool.resetmem();
}

// ----------------------------------------------------------------------------------------
// block allocation pool
// ----------------------------------------------------------------------------------------


pblockpool::pblockpool()
	: m_shutdown(false)
	, m_first(NULL)
	, m_blocksize((DEBUG_MODE) ? 16384 : 16384)
	, m_align(8)
{
}

pblockpool::~pblockpool()
{
}


void *pblockpool::alloc(const std::size_t n)
{
	if (IMMEDIATE_MODE)
		return (char *) malloc(n);
	else
	{
		int memsize = ((n + m_align - 1) / m_align) * m_align;
		int min_alloc = MAX(m_blocksize, memsize+sizeof(memblock)-MINDATASIZE);
		char *ret = NULL;
		//std::printf("m_first %p\n", m_first);
		for (memblock *p = m_first; p != NULL && ret == NULL; p = p->next)
		{
			if (p->remaining > memsize)
			{
				ret = p->cur;
				p->cur += memsize;
				p->allocated += 1;
				p->remaining -= memsize;
			}
		}

		if (ret == NULL)
		{
			// need to allocate a new block
			memblock *p = (memblock *) malloc(min_alloc); //new char[min_alloc];
			p->allocated = 0;
			p->cur = &p->data[0];
			p->size = p->remaining = min_alloc - (sizeof(memblock)-MINDATASIZE);
			p->next = m_first;
			//std::printf("allocated block size %d\n", sizeof(p->data));

			ret = p->cur;
			p->cur += memsize;
			p->allocated += 1;
			p->remaining -= memsize;

			m_first = p;
		}

		return ret;
	}
}

void pblockpool::dealloc(void *ptr)
{
	if (IMMEDIATE_MODE)
		free(ptr);
	else
	{
		for (memblock *p = m_first; p != NULL; p = p->next)
		{
			if (ptr >= &p->data[0] && ptr < &p->data[p->size])
			{
				p->allocated -= 1;
				if (p->allocated < 0)
					std::fprintf(stderr, "nstring: memory corruption - crash likely\n");
				if (p->allocated == 0)
				{
					//std::printf("Block entirely freed\n");
					p->remaining = p->size;
					p->cur = &p->data[0];
				}
				// shutting down ?
				if (m_shutdown)
					resetmem(); // try to free blocks
				return;
			}
		}
		std::fprintf(stderr, "nstring: string <%p> not found on free\n", ptr);
	}
}

void pblockpool::resetmem()
{
	if (!IMMEDIATE_MODE)
	{
		memblock **p = &m_first;
		int totalblocks = 0;
		int freedblocks = 0;

		while (*p != NULL)
		{
			totalblocks++;
			memblock **next = &((*p)->next);
			if ((*p)->allocated == 0)
			{
				//std::printf("freeing block %p\n", *p);
				memblock *freeme = *p;
				*p = *next;
				free(freeme); //delete[] *p;
				freedblocks++;
			}
			else
			{
				//if (DEBUG_MODE)
				//    std::printf("Allocated: <%s>\n", ((str_t *)(&(*p)->data[0]))->str());

				p = next;
			}
		}
		if (DEBUG_MODE)
			std::printf("Freed %d out of total %d blocks\n", freedblocks, totalblocks);
	}
}