]> andersk Git - libyaml.git/blob - src/yaml_private.h
Fix some leaks, segfaults and warnings.
[libyaml.git] / src / yaml_private.h
1
2 #if HAVE_CONFIG_H
3 #include <config.h>
4 #endif
5
6 #include <yaml.h>
7
8 #include <assert.h>
9
10 /*
11  * Memory management.
12  */
13
14 YAML_DECLARE(void *)
15 yaml_malloc(size_t size);
16
17 YAML_DECLARE(void *)
18 yaml_realloc(void *ptr, size_t size);
19
20 YAML_DECLARE(void)
21 yaml_free(void *ptr);
22
23 YAML_DECLARE(char *)
24 yaml_strdup(const char *);
25
26 /*
27  * Reader: Ensure that the buffer contains at least `length` characters.
28  */
29
30 YAML_DECLARE(int)
31 yaml_parser_update_buffer(yaml_parser_t *parser, size_t length);
32
33 /*
34  * Scanner: Ensure that the token stack contains at least one token ready.
35  */
36
37 YAML_DECLARE(int)
38 yaml_parser_fetch_more_tokens(yaml_parser_t *parser);
39
40 /*
41  * The size of the raw buffer.
42  */
43
44 #define RAW_BUFFER_SIZE 16384
45
46 /*
47  * The size of the buffer.
48  *
49  * It should be possible to decode the whole raw buffer.
50  */
51
52 #define BUFFER_SIZE     (RAW_BUFFER_SIZE*3)
53
54 /*
55  * The size of other stacks and queues.
56  */
57
58 #define INITIAL_STACK_SIZE  16
59 #define INITIAL_QUEUE_SIZE  16
60 #define INITIAL_STRING_SIZE 16
61
62 /*
63  * Buffer management.
64  */
65
66 #define BUFFER_INIT(context,buffer,size)                                        \
67     (((buffer).start = yaml_malloc(size)) ?                                     \
68         ((buffer).last = (buffer).pointer = (buffer).start,                     \
69          (buffer).end = (buffer).start+(size),                                  \
70          1) :                                                                   \
71         ((context)->error = YAML_MEMORY_ERROR,                                  \
72          0))
73
74 #define BUFFER_DEL(context,buffer)                                              \
75     (yaml_free((buffer).start),                                                 \
76      (buffer).start = (buffer).pointer = (buffer).end = 0)
77
78 /*
79  * String management.
80  */
81
82 typedef struct {
83     yaml_char_t *start;
84     yaml_char_t *end;
85     yaml_char_t *pointer;
86 } yaml_string_t;
87
88 YAML_DECLARE(int)
89 yaml_string_extend(yaml_char_t **start,
90         yaml_char_t **pointer, yaml_char_t **end);
91
92 YAML_DECLARE(int)
93 yaml_string_join(
94         yaml_char_t **a_start, yaml_char_t **a_pointer, yaml_char_t **a_end,
95         yaml_char_t **b_start, yaml_char_t **b_pointer, yaml_char_t **b_end);
96
97 #define NULL_STRING { NULL, NULL, NULL }
98
99 #define STRING_INIT(context,string,size)                                        \
100     (((string).start = yaml_malloc(size)) ?                                     \
101         ((string).pointer = (string).start,                                     \
102          (string).end = (string).start+(size),                                  \
103          memset((string).start, 0, (size)),                                     \
104          1) :                                                                   \
105         ((context)->error = YAML_MEMORY_ERROR,                                  \
106          0))
107
108 #define STRING_DEL(context,string)                                              \
109     (yaml_free((string).start),                                                 \
110      (string).start = (string).pointer = (string).end = 0)
111
112 #define STRING_EXTEND(context,string)                                           \
113     (((string).pointer+5 < (string).end)                                        \
114         || yaml_string_extend(&(string).start,                                  \
115             &(string).pointer, &(string).end))
116
117 #define CLEAR(context,string)                                                   \
118     ((string).pointer = (string).start,                                         \
119      memset((string).start, 0, (string).end-(string).start))
120
121 #define JOIN(context,string_a,string_b)                                         \
122     ((yaml_string_join(&(string_a).start, &(string_a).pointer,                  \
123                        &(string_a).end, &(string_b).start,                      \
124                        &(string_b).pointer, &(string_b).end)) ?                 \
125         ((string_b).pointer = (string_b).start,                                 \
126          1) :                                                                   \
127         ((context)->error = YAML_MEMORY_ERROR,                                  \
128          0))
129
130 /*
131  * Stack and queue management.
132  */
133
134 YAML_DECLARE(int)
135 yaml_stack_extend(void **start, void **top, void **end);
136
137 YAML_DECLARE(int)
138 yaml_queue_extend(void **start, void **head, void **tail, void **end);
139
140 #define STACK_INIT(context,stack,size)                                          \
141     (((stack).start = yaml_malloc((size)*sizeof(*(stack).start))) ?             \
142         ((stack).top = (stack).start,                                           \
143          (stack).end = (stack).start+(size),                                    \
144          1) :                                                                   \
145         ((context)->error = YAML_MEMORY_ERROR,                                  \
146          0))
147
148 #define STACK_DEL(context,stack)                                                \
149     (yaml_free((stack).start),                                                  \
150      (stack).start = (stack).top = (stack).end = 0)
151
152 #define STACK_EMPTY(context,stack)                                              \
153     ((stack).start == (stack).top)
154
155 #define PUSH(context,stack,value)                                               \
156     (((stack).top != (stack).end                                                \
157       || yaml_stack_extend((void **)&(stack).start,                             \
158               (void **)&(stack).top, (void **)&(stack).end)) ?                  \
159         (*((stack).top++) = value,                                              \
160          1) :                                                                   \
161         ((context)->error = YAML_MEMORY_ERROR,                                  \
162          0))
163
164 #define POP(context,stack)                                                      \
165     (*(--(stack).top))
166
167 #define QUEUE_INIT(context,queue,size)                                          \
168     (((queue).start = yaml_malloc((size)*sizeof(*(queue).start))) ?             \
169         ((queue).head = (queue).tail = (queue).start,                           \
170          (queue).end = (queue).start+(size),                                    \
171          1) :                                                                   \
172         ((context)->error = YAML_MEMORY_ERROR,                                  \
173          0))
174
175 #define QUEUE_DEL(context,queue)                                                \
176     (yaml_free((queue).start),                                                  \
177      (queue).start = (queue).head = (queue).tail = (queue).end = 0)
178
179 #define QUEUE_EMPTY(context,queue)                                              \
180     ((queue).head == (queue).tail)
181
182 #define ENQUEUE(context,queue,value)                                            \
183     (((queue).tail != (queue).end                                               \
184       || yaml_queue_extend((void **)&(queue).start, (void **)&(queue).head,     \
185             (void **)&(queue).tail, (void **)&(queue).end)) ?                   \
186         (*((queue).tail++) = value,                                             \
187          1) :                                                                   \
188         ((context)->error = YAML_MEMORY_ERROR,                                  \
189          0))
190
191 #define DEQUEUE(context,queue)                                                  \
192     (*((queue).head++))
193
194 #define QUEUE_INSERT(context,queue,index,value)                                 \
195     (((queue).tail != (queue).end                                               \
196       || yaml_queue_extend((void **)&(queue).start, (void **)&(queue).head,     \
197             (void **)&(queue).tail, (void **)&(queue).end)) ?                   \
198         (memmove((queue).head+(index)+1,(queue).head+(index),                   \
199             ((queue).tail-(queue).head-(index))*sizeof(*(queue).start)),        \
200          *((queue).head+(index)) = value,                                       \
201          (queue).tail++,                                                        \
202          1) :                                                                   \
203         ((context)->error = YAML_MEMORY_ERROR,                                  \
204          0))
205
This page took 0.04688 seconds and 5 git commands to generate.