File: ebuffer.c

package info (click to toggle)
entity 1.0.1-8
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 5,604 kB
  • ctags: 5,394
  • sloc: ansic: 64,242; sh: 7,377; makefile: 776; perl: 319
file content (644 lines) | stat: -rw-r--r-- 12,636 bytes parent folder | download | duplicates (2)
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
#include <string.h>
#include <ctype.h>
#include <glib.h>

#include "entity.h"

#define EBUF_STRUCT_ALLOC 1000
#define EBUF_CHUNK_SMALL_ALLOC 100
#define EBUF_CHUNK_MEDIUM_ALLOC 30

#define EBUF_CHUNK_SMALL_SIZE 20
#define EBUF_CHUNK_MEDIUM_SIZE 50



/* define inline functions here if they weren't inlined in .h */
#ifndef G_CAN_INLINE
G_INLINE_FUNC gint
ebuf_empty (EBuf * buf)
{
    return (!buf || (buf->len == 0));
}
#endif				/* no G_CAN_INLINE */


#ifndef G_CAN_INLINE
G_INLINE_FUNC gint
ebuf_not_empty (EBuf * buf)
{
    return (buf && (buf->len > 0));
}
#endif				/* no G_CAN_INLINE */


/* #define EBUF_EXTENDED_STATS 1 */

#ifdef EBUF_EXTENDED_STATS
static gint ebuf_num_moved_to_large = 0;
static gint ebuf_num_moved_to_medium = 0;
#endif


static EMemChunk *ebuf_struct_chunk_admin = NULL;
static EMemChunk *ebuf_small_chunk_admin = NULL;
static EMemChunk *ebuf_medium_chunk_admin = NULL;

static gchar *
ebuf_small_chunk_alloc (void)
{
    gchar *chunk;

    if (!ebuf_small_chunk_admin)
	ebuf_small_chunk_admin =
	    eutils_memchunk_admin_new (EBUF_CHUNK_SMALL_SIZE,
				       EBUF_CHUNK_SMALL_ALLOC);
    chunk = eutils_memchunk_alloc (ebuf_small_chunk_admin);

    return (chunk);
}

static void
ebuf_small_chunk_free (gchar * chunk)
{
    eutils_memchunk_free (ebuf_small_chunk_admin, chunk);
}

static gchar *
ebuf_medium_chunk_alloc (void)
{
    gchar *chunk;

    if (!ebuf_medium_chunk_admin)
	ebuf_medium_chunk_admin =
	    eutils_memchunk_admin_new (EBUF_CHUNK_MEDIUM_SIZE,
				       EBUF_CHUNK_MEDIUM_ALLOC);
    chunk = eutils_memchunk_alloc (ebuf_medium_chunk_admin);

    return (chunk);
}

static void
ebuf_medium_chunk_free (gchar * chunk)
{
    eutils_memchunk_free (ebuf_medium_chunk_admin, chunk);
}

/* Free chunk associated with buf */
static void
ebuf_chunk_free (EBuf * buf)
{
    g_return_if_fail (buf != NULL);

    switch (buf->type) {
    case EBUF_CHUNK_SMALL:
	ebuf_small_chunk_free (buf->str);
	break;

    case EBUF_CHUNK_MEDIUM:
	ebuf_medium_chunk_free (buf->str);
	break;

    case EBUF_CHUNK_LARGE:
	g_free (buf->str);
	break;

    default:
	break;
    }
}

static gint
nearest_power (gint num)
{
    gint n = 1;

    while (n < num)
	n <<= 1;

    return n;
}

static void
ebuf_maybe_expand (EBuf * buf, gint size)
{
    gint total;
    gchar *tmpbuf;

    total = buf->len + size;

    if (total >= EBUF_CHUNK_MEDIUM_SIZE - 2) {
	if (total >= buf->alloc) {
	    gint new_size;

	    /* Special case to allow resizing via realloc on large chunks */
	    if (buf->type == EBUF_CHUNK_LARGE) {
		new_size = nearest_power (buf->len + size + 1);
		buf->str = g_realloc (buf->str, new_size);
		buf->alloc = new_size;
	    } else {
#ifdef EBUF_EXTENDED_STATS
		ebuf_num_moved_to_large++;
#endif
		new_size = nearest_power (buf->len + size + 1);
		tmpbuf = g_malloc (new_size);
		memcpy (tmpbuf, buf->str, buf->len);
		ebuf_chunk_free (buf);

		buf->str = tmpbuf;
		buf->type = EBUF_CHUNK_LARGE;
		buf->alloc = new_size;
	    }
	}
    } else if (total >= EBUF_CHUNK_SMALL_SIZE - 2) {
	/* Make sure we're not just growing in the same pool, or one larger */
	if ((buf->type != EBUF_CHUNK_MEDIUM) && (buf->type != EBUF_CHUNK_LARGE)) {
#ifdef EBUF_EXTENDED_STATS
	    ebuf_num_moved_to_medium++;
#endif
	    tmpbuf = ebuf_medium_chunk_alloc ();
	    memcpy (tmpbuf, buf->str, buf->len);
	    ebuf_chunk_free (buf);

	    buf->str = tmpbuf;
	    buf->type = EBUF_CHUNK_MEDIUM;
	    buf->alloc = EBUF_CHUNK_MEDIUM_SIZE;
	}
    } else {
	if (buf->type == EBUF_CHUNK_NONE) {
	    tmpbuf = ebuf_small_chunk_alloc ();
	    memcpy (tmpbuf, buf->str, buf->len);
	    ebuf_chunk_free (buf);

	    buf->str = tmpbuf;
	    buf->type = EBUF_CHUNK_SMALL;
	    buf->alloc = EBUF_CHUNK_SMALL_SIZE;
	}
    }
}

#ifdef EBUF_EXTENDED_STATS
static GSList *allocated_list = NULL;
#endif				/* EBUF_EXTENDED_STATS */

void
ebuf_free (EBuf * buf)
{
    ECHECK_RET (buf != NULL);

#ifdef EBUF_EXTENDED_STATS
    allocated_list = g_slist_remove (allocated_list, buf);
#endif				/* EBUF_EXTENDED_STATS */

    ebuf_chunk_free (buf);
    eutils_memchunk_free (ebuf_struct_chunk_admin, buf);
}

EBuf *
ebuf_alloc (void)
{
    EBuf *newbuf;

    if (!ebuf_struct_chunk_admin)
	ebuf_struct_chunk_admin =
	    eutils_memchunk_admin_new (sizeof (EBuf), EBUF_STRUCT_ALLOC);

    newbuf = eutils_memchunk_alloc (ebuf_struct_chunk_admin);
    newbuf->type = EBUF_CHUNK_NONE;
    newbuf->len = 0;

#ifdef EBUF_EXTENDED_STATS
    allocated_list = g_slist_append (allocated_list, newbuf);
#endif				/* EBUF_EXTENDED_STATS */

    return (newbuf);
}


/* Allocate a new SMALL EBuf */
EBuf *
ebuf_new (void)
{
    EBuf *newbuf = ebuf_alloc ();

    ebuf_maybe_expand (newbuf, 2);
    return (newbuf);
}

EBuf *
ebuf_new_sized (guint size)
{
    EBuf *newbuf = ebuf_alloc ();

    ebuf_maybe_expand (newbuf, size);
    return (newbuf);
}

EBuf *
ebuf_new_with_int (gint val)
{
    EBuf *newbuf = ebuf_alloc ();

    ebuf_sprintf (newbuf, "%d", val);
    return (newbuf);
}

EBuf *
ebuf_new_with_str (gchar * str)
{
    EBuf *newbuf = ebuf_alloc ();

    ebuf_append_str (newbuf, str);
    return (newbuf);
}

EBuf *
ebuf_new_with_data (gchar * str, gint len)
{
    EBuf *newbuf = ebuf_alloc ();

    ebuf_append_data (newbuf, str, len);
    return (newbuf);
}

EBuf *
ebuf_new_with_ebuf (EBuf * buf)
{
    EBuf *newbuf = ebuf_alloc ();

    ebuf_append_ebuf (newbuf, buf);
    return (newbuf);
}

EBuf *
ebuf_new_with_true (void)
{
    EBuf *newbuf = ebuf_alloc ();

    ebuf_append_str (newbuf, "true");
    return (newbuf);
}

EBuf *
ebuf_new_with_false (void)
{
    EBuf *newbuf = ebuf_alloc ();

    ebuf_append_str (newbuf, "false");
    return (newbuf);
}


void
ebuf_set_to_str (EBuf * buf, gchar * str)
{
    gint len;
    gint total;

    g_return_if_fail (buf != NULL);
    g_return_if_fail (str != NULL);

    len = strlen (str);

    total = len - buf->len;
    ebuf_maybe_expand (buf, total);

    strcpy (buf->str, str);
    buf->len = len;
}

void
ebuf_set_to_ebuf (EBuf * buf, EBuf * val)
{
    gint total;

    g_return_if_fail (buf != NULL);
    g_return_if_fail (val != NULL);

    total = val->len - buf->len;
    ebuf_maybe_expand (buf, total);

    memcpy (buf->str, val->str, val->len);
    buf->len = val->len;
    /* This should just be buf->len nor buf->len +1. MW */
    buf->str[buf->len] = '\0';
}

void
ebuf_set_to_data (EBuf * buf, gchar * str, gint len)
{
    gint total;

    g_return_if_fail (buf != NULL);
    g_return_if_fail (str != NULL);

    total = len - buf->len;
    ebuf_maybe_expand (buf, total);

    memcpy (buf->str, str, len);
    buf->len = len;
    /* This should just be buf->len nor buf->len +1. MW */
    buf->str[buf->len] = '\0';
}


void
ebuf_append_str (EBuf * buf, gchar * str)
{
    gint len;

    g_return_if_fail (buf != NULL);
    g_return_if_fail (str != NULL);

    len = strlen (str);
    ebuf_maybe_expand (buf, len);

    strcpy (buf->str + buf->len, str);
    buf->len += len;
    buf->str[buf->len] = '\0';
}

void
ebuf_append_char (EBuf * buf, gchar c)
{
    g_return_if_fail (buf != NULL);

    ebuf_maybe_expand (buf, 1);

    buf->str[buf->len] = c;
    buf->len += 1;
    buf->str[buf->len] = '\0';
}

void
ebuf_prepend_str (EBuf * buf, gchar * str)
{
    gint len;

    g_return_if_fail (buf != NULL);
    g_return_if_fail (str != NULL);

    len = strlen (str);
    ebuf_maybe_expand (buf, len);

    g_memmove (buf->str + len, buf->str, buf->len);
    strncpy (buf->str, str, len);
    buf->len += len;
    /* insurance */
    buf->str[buf->len + 1] = '\0';
}

void
ebuf_prepend_char (EBuf * buf, gchar c)
{
    g_return_if_fail (buf != NULL);

    ebuf_maybe_expand (buf, 1);

    g_memmove (buf->str + 1, buf->str, buf->len);
    buf->str[0] = c;
    buf->len += 1;
    /* insurance */
    buf->str[buf->len] = '\0';
}

void
ebuf_append_data (EBuf * buf, gchar * str, gint len)
{
    g_return_if_fail (buf != NULL);
    g_return_if_fail (str != NULL);

    ebuf_maybe_expand (buf, len);

    memcpy (&buf->str[buf->len], str, len);
    buf->len += len;

    buf->str[buf->len] = '\0';
}

void
ebuf_append_ebuf (EBuf * buf, EBuf * newbuf)
{
    g_return_if_fail (buf != NULL);
    g_return_if_fail (newbuf != NULL);

    ebuf_maybe_expand (buf, newbuf->len);

    memcpy (&buf->str[buf->len], newbuf->str, newbuf->len);
    buf->len += newbuf->len;

    buf->str[buf->len] = '\0';
}

void
ebuf_insert_str (EBuf * buf, gint pos, gchar * val)
{
    gint len;

    g_return_if_fail (buf != NULL);
    g_return_if_fail (val != NULL);
    g_return_if_fail (pos >= 0);
    g_return_if_fail (pos <= buf->len);

    len = strlen (val);
    ebuf_maybe_expand (buf, len);

    g_memmove (buf->str + pos + len, buf->str + pos, buf->len - pos);
    strncpy (buf->str + pos, val, len);
    buf->len += len;
    buf->str[buf->len] = 0;
}

void
ebuf_insert_data (EBuf * buf, gint pos, gchar * val, gint len)
{
    g_return_if_fail (buf != NULL);
    g_return_if_fail (val != NULL);
    g_return_if_fail (pos >= 0);
    g_return_if_fail (pos <= buf->len);

    ebuf_maybe_expand (buf, len);

    g_memmove (buf->str + pos + len, buf->str + pos, buf->len - pos);
    memcpy (buf->str + pos, val, len);
    buf->len += len;
    buf->str[buf->len] = 0;
}

void
ebuf_insert_ebuf (EBuf * buf, gint pos, EBuf * val)
{
    g_return_if_fail (buf != NULL);
    g_return_if_fail (val != NULL);
    g_return_if_fail (pos >= 0);
    g_return_if_fail (pos <= buf->len);

    ebuf_maybe_expand (buf, val->len);

    g_memmove (buf->str + pos + val->len, buf->str + pos, buf->len - pos);
    memcpy (buf->str + pos, val, val->len);
    buf->len += val->len;
    buf->str[buf->len] = 0;
}


void
ebuf_erase (EBuf * buf, gint pos, gint len)
{
    g_return_if_fail (buf != NULL);
    g_return_if_fail (len >= 0);
    g_return_if_fail (pos >= 0);
    g_return_if_fail (pos <= buf->len);
    g_return_if_fail (pos + len <= buf->len);

    if (pos + len < buf->len)
	g_memmove (buf->str + pos, buf->str + pos + len,
		   buf->len - (pos + len));
    buf->len -= len;
    buf->str[buf->len] = 0;
}

void
ebuf_truncate (EBuf * buf, gint len)
{
    g_return_if_fail (buf != NULL);

    buf->len = len;
    buf->str[len] = 0;
}

/* Lifted from GString.c */
static void
ebuf_sprintfa_int (EBuf * buf, gchar * fmt, va_list args)
{
    gchar *tmp;

    tmp = g_strdup_vprintf (fmt, args);
    ebuf_append_str (buf, tmp);
    g_free (tmp);
}

void
ebuf_sprintf (EBuf * buf, gchar * fmt, ...)
{
    va_list args;

    va_start (args, fmt);
    ebuf_sprintfa_int (buf, fmt, args);
    va_end (args);
}


void
ebuf_stats (void)
{
#ifdef EBUF_EXTENDED_STATS
    GSList *tmp;

    g_print ("For EBuf structs:\n");
    eutils_memchunk_stats (ebuf_struct_chunk_admin);
    g_print ("For small buffers:\n");
    eutils_memchunk_stats (ebuf_small_chunk_admin);
    g_print ("For medium buffers:\n");
    eutils_memchunk_stats (ebuf_medium_chunk_admin);

    g_print ("Number of chunks upgraded to medium: %d, large %d\n",
	     ebuf_num_moved_to_medium, ebuf_num_moved_to_large);
    tmp = allocated_list;
    while (tmp) {
	EBuf *buf = tmp->data;
	g_print ("Buf: '%s'\n", buf->str);
	tmp = tmp->next;
    }
#endif
}

void
ebuf_down (EBuf * buf)
{
    guchar *c;

    g_return_if_fail (buf != NULL);

    c = buf->str;
    while (*c) {
	*c = tolower (*c);
	c++;
    }
}

void
ebuf_up (EBuf * buf)
{
    guchar *c;

    g_return_if_fail (buf != NULL);

    c = buf->str;
    while (*c) {
	*c = toupper (*c);
	c++;
    }
}

gint
ebuf_is_whitespace (EBuf * string)
{
    gchar *str;
    gint i;

    if (!string)
	return (TRUE);

    g_return_val_if_fail (string != NULL, 1);

    str = string->str;

    for (i = 0; i < string->len; i++) {
	if ((str[i] != ' ') &&
	    (str[i] != '\t') &&
	    (str[i] != '\r') && (str[i] != '\n') && (str[i] != '\0'))
	    return (FALSE);
    }
    return (TRUE);
}

gint
ebuf_equal_ebuf (EBuf * buf1, EBuf * buf2)
{
    g_return_val_if_fail (buf1 != NULL, 0);
    g_return_val_if_fail (buf2 != NULL, 0);
    return strcmp ((const gchar *) buf1->str, (const gchar *) buf2->str) == 0;
}

gint
ebuf_equal_str (EBuf * buf1, gchar * str)
{
    g_return_val_if_fail (buf1 != NULL, 0);
    return strcmp (buf1->str, str) == 0;
}

gint
ebuf_equal_strcase (EBuf * buf1, gchar * str)
{
    g_return_val_if_fail (buf1 != NULL, 0);
    return g_strcasecmp (buf1->str, str) == 0;
}

gint
ebuf_equal_ebufcase (EBuf * buf1, EBuf * buf2)
{
    g_return_val_if_fail (buf1 != NULL, 0);
    g_return_val_if_fail (buf2 != NULL, 0);
    return g_strcasecmp (buf1->str, buf2->str) == 0;
}

guint
ebuf_hash (EBuf * buf)
{
    if (!buf || !buf->str)
	return (0);
    return (x31_hash (buf->str));
}