Branch data Line data Source code
1 : : #include "mem.h"
2 : : #include <string.h>
3 : :
4 : 0 : Return memory_append(
5 : : memory *destination,
6 : : const memory *source)
7 : : {
8 : : /* Status returned by this function through provide()
9 : : Default value assumes successful completion */
10 : 0 : Return status = SUCCESS;
11 : :
12 [ # # # # ]: 0 : if(destination == NULL || source == NULL)
13 : : {
14 : 0 : report("Memory management; append arguments must be non-NULL");
15 : 0 : provide(FAILURE);
16 : : }
17 : :
18 [ # # ]: 0 : if(destination->element_size == 0)
19 : : {
20 : 0 : report("Memory management; Destination element size is zero (uninitialized)");
21 : 0 : provide(FAILURE);
22 : : }
23 : :
24 [ # # ]: 0 : if(source->element_size == 0)
25 : : {
26 : 0 : report("Memory management; Source element size is zero (uninitialized)");
27 : 0 : provide(FAILURE);
28 : : }
29 : :
30 [ # # ]: 0 : if(destination->element_size != source->element_size)
31 : : {
32 : 0 : report("Memory management; Element size mismatch (%zu vs %zu)",destination->element_size,source->element_size);
33 : 0 : provide(FAILURE);
34 : : }
35 : :
36 : 0 : const size_t append_elements = source->length;
37 : :
38 [ # # ]: 0 : if(append_elements == 0)
39 : : {
40 : 0 : provide(status);
41 : : }
42 : :
43 [ # # ]: 0 : if(append_elements > SIZE_MAX - destination->length)
44 : : {
45 : 0 : report("Memory management; Append would overflow element count");
46 : 0 : provide(FAILURE);
47 : : }
48 : :
49 : 0 : size_t append_bytes = 0;
50 : :
51 [ # # # # ]: 0 : run(memory_guarded_size(source->element_size,append_elements,&append_bytes));
52 : :
53 [ # # ]: 0 : if(CRITICAL & status)
54 : : {
55 : 0 : report("Memory management; Overflow computing append bytes");
56 : : }
57 : :
58 : 0 : size_t offset_bytes = 0;
59 : :
60 [ # # # # ]: 0 : run(memory_guarded_size(destination->element_size,destination->length,&offset_bytes));
61 : :
62 [ # # ]: 0 : if(CRITICAL & status)
63 : : {
64 : 0 : report("Memory management; Overflow computing append offset");
65 : : }
66 : :
67 : : size_t new_total_elements;
68 : :
69 [ # # ]: 0 : if(TRIUMPH & status)
70 : : {
71 : 0 : new_total_elements = destination->length + append_elements;
72 : : }
73 : :
74 [ # # # # ]: 0 : run(resize(destination,new_total_elements));
75 : :
76 [ # # ]: 0 : if(TRIUMPH & status)
77 : : {
78 : 0 : unsigned char *destination_bytes = (unsigned char *)destination->data;
79 : :
80 [ # # # # ]: 0 : if(destination_bytes == NULL || source->data == NULL)
81 : : {
82 : 0 : report("Memory management; Data pointer is NULL during append");
83 : 0 : status = FAILURE;
84 : : } else {
85 : 0 : memcpy(destination_bytes + offset_bytes,source->data,append_bytes);
86 : : }
87 : : }
88 : :
89 : 0 : provide(status);
90 : : }
|