1 /*****************************************************************************
2
3 NAME:
4 textblock.c -- implementation of textblock linked lists.
5
6 ******************************************************************************/
7
8 #include "common.h"
9
10 #include "textblock.h"
11 #include "xmalloc.h"
12
13 /* Global Variables */
14
15 static textblock_t *textblocks = NULL;
16
17 static size_t cur_mem, max_mem, tot_mem;
18
19 /* Function Definitions */
20
textblock_head(void)21 textdata_t *textblock_head(void)
22 {
23 return textblocks->head;
24 }
25
textblock_init(void)26 void textblock_init(void)
27 {
28 textblock_t *t = (textblock_t *) xcalloc(1, sizeof(*t));
29 size_t mem = sizeof(*t)+sizeof(textdata_t);
30 t->head = (textdata_t *) xcalloc(1, sizeof(textdata_t));
31 t->tail = t->head;
32 cur_mem += mem;
33 tot_mem += mem;
34 max_mem = max(max_mem, cur_mem);
35 if (DEBUG_TEXT(2))
36 fprintf(dbgout, "%s:%d %p %p %3lu *ini* cur: %lu, max: %lu, tot: %lu\n", __FILE__,__LINE__,
37 (void *)t, (void *)t->head,
38 (unsigned long)mem, (unsigned long)cur_mem,
39 (unsigned long)max_mem, (unsigned long)tot_mem);
40 textblocks = t;
41 }
42
textblock_add(const byte * text,size_t size)43 void textblock_add(const byte *text, size_t size)
44 {
45 textblock_t *t = textblocks;
46 size_t mem = size+sizeof(textdata_t);
47 textdata_t *cur = t->tail;
48
49 cur->size = size;
50 if (size == 0)
51 cur->data = NULL;
52 else {
53 cur->data = (byte *)xmalloc(size+D);
54 memcpy((char *)cur->data, (const char *)text, size+D);
55 Z(((char *)cur->data)[size]); /* for easier debugging - removable */
56 }
57 cur_mem += mem;
58 tot_mem += mem;
59 max_mem = max(max_mem, cur_mem);
60 if (DEBUG_TEXT(2))
61 fprintf(dbgout, "%s:%d %p %p %3lu *add* cur: %lu, max: %lu, tot: %lu\n",
62 __FILE__,__LINE__,
63 (void *)cur, (void *)cur->data,
64 (unsigned long)cur->size,
65 (unsigned long)cur_mem,
66 (unsigned long)max_mem,
67 (unsigned long)tot_mem );
68 cur = cur->next = (textdata_t *) xcalloc(1, sizeof(textdata_t));
69 t->tail = cur;
70 }
71
textblock_free(void)72 void textblock_free(void)
73 {
74 size_t mem;
75 textdata_t *cur, *nxt;
76 textblock_t *t = textblocks;
77
78 for (cur = t->head; cur ; cur = nxt) {
79 nxt = cur->next;
80 mem = cur->size + sizeof(*cur);
81 cur_mem -= mem;
82 if (DEBUG_TEXT(2)) fprintf(dbgout, "%s:%d %p %p %3lu *rel* cur: %lu, max: %lu, tot: %lu\n",
83 __FILE__,__LINE__, (void *)cur, cur->data,
84 (unsigned long)cur->size,
85 (unsigned long)cur_mem,
86 (unsigned long)max_mem,
87 (unsigned long)tot_mem);
88 xfree((void*)cur->data);
89 xfree((void*)cur);
90 }
91
92 mem = sizeof(*t->head);
93 cur_mem -= mem;
94
95 if (DEBUG_TEXT(2)) fprintf(dbgout, "%s:%d %p %p *rel* cur: %lu, max: %lu, tot: %lu\n",
96 __FILE__,__LINE__,
97 (void *)t, (void *)t->head,
98 (unsigned long)cur_mem, (unsigned long)max_mem,
99 (unsigned long)tot_mem);
100 xfree(t);
101 cur_mem -= sizeof(t->head) + sizeof(t);
102 if (DEBUG_TEXT(1)) fprintf(dbgout, "cur: %lu, max: %lu, tot: %lu\n", (unsigned long)cur_mem, (unsigned long)max_mem, (unsigned long)tot_mem );
103 }
104