← Back to C-Kernel-Engine Docs Doxygen Source Documentation
Loading...
Searching...
No Matches
copy_i32_span_checked.c
Go to the documentation of this file.
2
3#include <string.h>
4
5static int valid_region(const int32_t *pointer, size_t elements) {
6 return pointer && (uintptr_t)pointer % _Alignof(int32_t) == 0 &&
7 elements <= SIZE_MAX / sizeof(int32_t) &&
8 (uintptr_t)pointer <= UINTPTR_MAX - elements * sizeof(int32_t);
9}
10
11int ck_copy_i32_span_checked(const int32_t *source, size_t source_elements,
12 size_t source_offset, int32_t *destination,
13 size_t destination_elements,
14 size_t destination_offset, size_t count) {
15 if (!count || source_offset > source_elements ||
16 count > source_elements - source_offset ||
17 destination_offset > destination_elements ||
18 count > destination_elements - destination_offset)
19 return -2;
20 if (!valid_region(source, source_elements) ||
21 !valid_region(destination, destination_elements))
22 return -1;
23 const uintptr_t src = (uintptr_t)(source + source_offset);
24 const uintptr_t dst = (uintptr_t)(destination + destination_offset);
25 const size_t bytes = count * sizeof(int32_t);
26 if (src < dst + bytes && dst < src + bytes)
27 return -1;
28 memcpy(destination + destination_offset, source + source_offset, bytes);
29 return 0;
30}
int ck_copy_i32_span_checked(const int32_t *source, size_t source_elements, size_t source_offset, int32_t *destination, size_t destination_elements, size_t destination_offset, size_t count)
static int valid_region(const int32_t *pointer, size_t elements)
const uint8_t size_t count
Definition true_bpe.h:211