I need to implement an optimized version of malloc / realloc / free (specifically for my specific application). The code currently runs on a specific platform, but I would like to write it in a portable way, if possible (the platform may change in the future), or at least I would like to concentrate the possible differences in the platform in one (probably .h ) I know about some issues:
(I will ignore the differences in the basic system services for memory allocation here, since on some embedded systems they may not be available at all. Imagine that we are working on a large pre-allocated memory block that will be used as a heap,).
Question (s):
C, C11. _Alignof, _Alignas aligned_alloc. ( ), , __STDC_VERSION__.
_Alignof
_Alignas
aligned_alloc
__STDC_VERSION__
, , , - :
union alloc_align { void *dummy1; long long dummy2; long double dummy3; };
... , , , sizeof (union alloc_align) , .
sizeof (union alloc_align)
, K & R.
( ), MSVC aligned_malloc, POSIX memalign Linux, _mm_alloc, ICC, MSVC GCC, IIRC, .
aligned_malloc
memalign
_mm_alloc
- , , - -, .
, (, SIMD), __attribute__((__aligned__(x))) __declspec(align(x)).
__attribute__((__aligned__(x)))
__declspec(align(x))
stdint.h/pstdint.h, , - UB uintptr_t ( , aren ' t :().
stdint.h
pstdint.h
uintptr_t
, malloc() , - . , , sizeof , n, , :
malloc()
p = malloc(sizeof(*p) * n);
, , s = 4 n = 10, s = 2 n = 20 s = 1 n = 40, 40 .
, , , . . , s n.
void *my_malloc (size_t s, size_t n)
, s, .
, malloc() (, 16), .
#pragma, , .
C , malloc , . C , C. , malloc - , , C, .
malloc
(C99, 7.20.3p1) " , , , ( ).