Branch data Line data Source code
1 : : #include "mem.h"
2 : : #include "mem_internal.h"
3 : :
4 : : /**
5 : : * @brief Return a read-only raw pointer without checking the element type
6 : : *
7 : : * This helper is the read-only counterpart of @ref mem_raw_data_writable.
8 : : * It returns the descriptor's storage pointer as `const void *` and skips any
9 : : * element-type validation, so callers can use it for low-level inspection code
10 : : * that already trusts the stored representation.
11 : : *
12 : : * The function still protects callers from the roughest descriptor failures. It
13 : : * refuses to expose a buffer when the descriptor itself is `NULL`, when the
14 : : * element size was never initialized, when reserve metadata exists without a
15 : : * backing pointer, or when the logical length says the descriptor contains data
16 : : * but the data pointer is `NULL`
17 : : *
18 : : * On success the returned pointer is the descriptor's live @ref memory::data
19 : : * pointer, not a copy and not a detached view. A writable raw view obtained
20 : : * from the same descriptor exposes the same backing storage. The pointer stays
21 : : * valid only until an operation that may replace or release the descriptor
22 : : * storage, such as @ref mem_resize, @ref mem_delete, or a descriptor-copy or
23 : : * concat operation that targets this descriptor
24 : : *
25 : : * @param memory_object Pointer to a descriptor
26 : : * @return Read-only storage pointer on success. `NULL` when the descriptor is too broken to expose safely
27 : : */
28 : 38 : const void *mem_raw_data_readonly(const memory *memory_object)
29 : : {
30 [ + + ]: 38 : if(memory_object == NULL)
31 : : {
32 : 1 : report("Memory management; Descriptor is NULL");
33 : 1 : return(NULL);
34 : : }
35 : :
36 [ + + ]: 37 : if(memory_object->single_element_size == 0)
37 : : {
38 : 1 : report("Memory management; Descriptor element size is zero (uninitialized)");
39 : 1 : return(NULL);
40 : : }
41 : :
42 [ + + + + ]: 36 : if(memory_object->data == NULL && memory_object->actually_allocated_bytes > 0)
43 : : {
44 : 1 : report("Memory management; Descriptor has reserved bytes with NULL data pointer");
45 : 1 : return(NULL);
46 : : }
47 : :
48 [ + - + + ]: 35 : if(memory_object->length > 0 && memory_object->data == NULL)
49 : : {
50 : 1 : report("Memory management; Descriptor has non-zero length with NULL data pointer");
51 : 1 : return(NULL);
52 : : }
53 : :
54 : 34 : return(memory_object->data);
55 : : }
|