Branch data Line data Source code
1 : : #include "mem.h"
2 : : #include "mem_internal.h"
3 : :
4 : : /**
5 : : * @brief Return a writable raw pointer without checking the element type
6 : : *
7 : : * This helper returns a writable pointer without performing any element-type
8 : : * verification. It is meant for low-level code that already knows what lives in
9 : : * the descriptor and only needs the library to reject the most obviously broken
10 : : * descriptor states before exposing the buffer.
11 : : *
12 : : * In practice, that means the function still checks for a `NULL` descriptor, a
13 : : * zero element size, reserved bytes with no backing pointer, and non-zero
14 : : * logical length with a `NULL` data pointer. It does not compare the descriptor
15 : : * against an expected element type, and it does not rewrite `is_string` or
16 : : * `string_length`
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 read-only 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 Writable storage pointer on success. `NULL` when the descriptor is too broken to expose safely
27 : : */
28 : 2553 : void *mem_raw_data_writable(memory *memory_object)
29 : : {
30 [ + + ]: 2553 : if(memory_object == NULL)
31 : : {
32 : 1 : report("Memory management; Descriptor is NULL");
33 : 1 : return(NULL);
34 : : }
35 : :
36 [ + + ]: 2552 : 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 [ + + + + ]: 2551 : 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 [ + - + + ]: 2550 : 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 : 2549 : return(memory_object->data);
55 : : }
|