1 /* These routines represent handling of struct memory_list. */
12 #include "util/error.h"
13 #include "util/memlist.h"
14 #include "util/memory.h"
18 * memory_list is used to track information about all allocated memory
19 * belonging to something. Then we can free it when we won't need it
20 * anymore, but the one who allocated it won't be able to get control
21 * back in order to free it himself.
24 #define ML_SIZE(n) (sizeof(struct memory_list) + (n) * sizeof(void *))
26 /* Create a memory list. If p is NULL or allocation fails, it will
28 * It always stops at first NULL element. */
31 debug_getml(unsigned char *file
, int line
, void *p
, ...)
37 struct memory_list
*ml
;
42 /* If first element is NULL, there's no need to allocate memory, so
46 /* How many elements ? */
48 while ((q
= va_arg(ap
, void *))) n
++;
51 /* Allocate space for memory list. */
52 ml
= mem_alloc(ML_SIZE(n
));
61 while ((q
= va_arg(ap
, void *))) ml
->p
[ml
->n
++] = q
;
68 /* Add elements to a memory list.
69 * If memory list exists, it enlarges it, else it creates it.
70 * if there's no elements or first element is NULL, it does nothing.
71 * It always stops at first NULL element. */
74 debug_add_to_ml(unsigned char *file
, int line
, struct memory_list
**ml
, ...)
77 add_to_ml(struct memory_list
**ml
, ...)
84 /* How many new elements ? */
86 while ((q
= va_arg(ap
, void *))) n
++;
89 /* None, so just return. */
92 errline
= line
, errfile
= file
;
93 elinks_error("add_to_ml(%p, NULL, ...)", ml
);
99 /* If getml() wasn't called before or returned NULL,
100 * then we create it. */
101 *ml
= mem_alloc(ML_SIZE(n
));
106 /* Enlarge existing ml. */
107 struct memory_list
*nml
;
109 nml
= mem_realloc(*ml
, ML_SIZE(n
+ (*ml
)->n
));
115 /* Set ml with new elements and update count. */
117 while ((q
= va_arg(ap
, void *))) (*ml
)->p
[(*ml
)->n
++] = q
;
123 debug_add_one_to_ml(unsigned char *file
, int line
, struct memory_list
**ml
, void *p
)
126 add_one_to_ml(struct memory_list
**ml
, void *p
)
129 /* None, so just return. */
132 errline
= line
, errfile
= file
;
133 elinks_error("add_one_to_ml(%p, NULL)", ml
);
139 /* If getml() wasn't called before or returned NULL,
140 * then we create it. */
141 *ml
= mem_alloc(ML_SIZE(1));
146 /* Enlarge existing ml. */
147 struct memory_list
*nml
;
149 nml
= mem_realloc(*ml
, ML_SIZE(1 + (*ml
)->n
));
155 /* Set ml with new element and update count. */
156 (*ml
)->p
[(*ml
)->n
++] = p
;
160 /* Free elements and memory list.
161 * It returns safely if passed a NULL pointer. */
163 freeml(struct memory_list
*ml
)
169 for (i
= 0; i
< ml
->n
; i
++)