Branch data Line data Source code
1 : : #include "mem.h"
2 : : #include <string.h>
3 : :
4 : : /**
5 : : * @brief Import an externally-owned byte buffer into a descriptor.
6 : : *
7 : : * This helper mirrors @ref memory_copy semantics for size checks and resize
8 : : * flow, but takes raw pointer + byte count instead of a source descriptor.
9 : : */
10 : 112 : Return memory_copy_buffer(
11 : : memory *destination,
12 : : const void *source_buffer,
13 : : size_t buffer_size)
14 : : {
15 : : /* Status returned by this function through provide()
16 : : Default value assumes successful completion */
17 : 112 : Return status = SUCCESS;
18 : :
19 [ - + ]: 112 : if(destination == NULL)
20 : : {
21 : 0 : report("Memory management; copy_buffer destination must be non-NULL");
22 : 0 : provide(FAILURE);
23 : : }
24 : :
25 [ - + ]: 112 : if(destination->element_size == 0)
26 : : {
27 : 0 : report("Memory management; Destination element size is zero (uninitialized)");
28 : 0 : provide(FAILURE);
29 : : }
30 : :
31 [ - + ]: 112 : if(source_buffer == NULL)
32 : : {
33 [ # # ]: 0 : if(buffer_size == 0)
34 : : {
35 [ # # # # ]: 0 : run(resize(destination,0));
36 : 0 : provide(status);
37 : : }
38 : :
39 : 0 : report("Memory management; copy_buffer source is NULL while size is %zu",buffer_size);
40 : 0 : provide(FAILURE);
41 : : }
42 : :
43 [ - + ]: 112 : if((buffer_size % destination->element_size) != 0)
44 : : {
45 : 0 : report("Memory management; copy_buffer size %zu is not divisible by element size %zu",buffer_size,destination->element_size);
46 : 0 : provide(FAILURE);
47 : : }
48 : :
49 : 112 : const size_t target_elements = buffer_size / destination->element_size;
50 : :
51 [ + - - + ]: 112 : run(resize(destination,target_elements));
52 : :
53 [ + - + - ]: 112 : if((TRIUMPH & status) && buffer_size > 0)
54 : : {
55 [ - + ]: 112 : if(destination->data == NULL)
56 : : {
57 : 0 : report("Memory management; Destination data pointer is NULL during copy_buffer");
58 : 0 : status = FAILURE;
59 : : } else {
60 : 112 : memcpy(destination->data,source_buffer,buffer_size);
61 : : }
62 : : }
63 : :
64 : 112 : provide(status);
65 : : }
|