Branch data Line data Source code
1 : : #include "mem.h"
2 : :
3 : : /**
4 : : * @brief Delete every inline descriptor in a descriptor-backed array and then delete the root descriptor
5 : : *
6 : : * The root descriptor must stay in data mode and must store elements of type
7 : : * `memory`. This helper walks every inline descriptor, deletes each child
8 : : * descriptor through @ref m_del, and finally deletes the root descriptor itself
9 : : * so the whole array can be torn down through one call
10 : : *
11 : : * Small example:
12 : : * @code
13 : : * m_create(memory,string_array);
14 : : *
15 : : * if((TRIUMPH & m_string_array_append(string_array,char,"delta")) == 0) { return FAILURE; }
16 : : * if((TRIUMPH & m_string_array_append(string_array,char,sizeof("epsilon"),"epsilon")) == 0) { return FAILURE; }
17 : : * if((TRIUMPH & m_array_del(string_array)) == 0) { return FAILURE; }
18 : : * @endcode
19 : : *
20 : : * @param descriptor_array Root data-mode descriptor that stores `memory` elements
21 : : * @return `SUCCESS` on success; `FAILURE` otherwise
22 : : */
23 : 1111 : Return mem_array_delete(memory *descriptor_array)
24 : : {
25 : : /* Status returned by this function through provide()
26 : : Default value assumes successful completion */
27 : 1111 : Return status = SUCCESS;
28 : 1111 : memory *inline_descriptors_data_rewritable = NULL;
29 : 1111 : size_t descriptor_count = 0;
30 : :
31 [ - + ]: 1111 : if(descriptor_array == NULL)
32 : : {
33 : 0 : report("Memory management; Array delete descriptor is NULL");
34 : 0 : provide(FAILURE);
35 : : }
36 : :
37 [ + + - + ]: 1111 : if(descriptor_array->length > 0 && descriptor_array->data == NULL)
38 : : {
39 : 0 : report("Memory management; Array delete descriptor has non-zero length with NULL data pointer");
40 : 0 : provide(FAILURE);
41 : : }
42 : :
43 [ - + ]: 1111 : if(descriptor_array->single_element_size != sizeof(memory))
44 : : {
45 : 0 : report("Memory management; Array delete requires a root descriptor with memory-sized elements");
46 : 0 : provide(FAILURE);
47 : : }
48 : :
49 [ - + ]: 1111 : if(descriptor_array->is_string == true)
50 : : {
51 : 0 : report("Memory management; Array delete requires a data-mode root descriptor");
52 : 0 : provide(FAILURE);
53 : : }
54 : :
55 [ - + ]: 1111 : if(descriptor_array->string_length != 0)
56 : : {
57 : 0 : report("Memory management; Data-mode root descriptor has stale string metadata during array delete");
58 : 0 : provide(FAILURE);
59 : : }
60 : :
61 : 1111 : descriptor_count = descriptor_array->length;
62 : :
63 [ + + ]: 1111 : if(descriptor_count > 0)
64 : : {
65 : 522 : inline_descriptors_data_rewritable = m_data(memory,descriptor_array);
66 : :
67 [ - + ]: 522 : if(inline_descriptors_data_rewritable == NULL)
68 : : {
69 : 0 : report("Memory management; Array delete could not map the root descriptor data");
70 : 0 : status = FAILURE;
71 : : }
72 : : }
73 : :
74 [ + - + + ]: 1111 : if((TRIUMPH & status) && inline_descriptors_data_rewritable != NULL)
75 : : {
76 [ + + ]: 1184 : for(size_t descriptor_index = 0; descriptor_index < descriptor_count; ++descriptor_index)
77 : : {
78 : 662 : call(m_del(&inline_descriptors_data_rewritable[descriptor_index]));
79 : : }
80 : : }
81 : :
82 : 1111 : call(m_del(descriptor_array));
83 : :
84 : 1111 : provide(status);
85 : : }
|