Created
June 23, 2024 20:52
-
-
Save matu3ba/2f15b94f31191bdedbc8519d90e99551 to your computer and use it in GitHub Desktop.
cerberus iso compat pointer works
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <assert.h> | |
#include <inttypes.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
static void memset_16aligned(void * ptr, char byte, size_t size_bytes, uint16_t alignment) { | |
assert((size_bytes & (alignment-1)) == 0); // Size aligned | |
assert(((uintptr_t)ptr & (alignment-1)) == 0); // Pointer aligned | |
memset(ptr, byte, size_bytes); | |
} | |
// 1. Careful with segmented address spaces: lookup uintptr_t semantics | |
// 2. Careful with long standing existing optimization compiler bugs pointer to | |
// integer and back optimizations in for example clang and gcc | |
// 3. Careful with LTO potentially creating problem 2. | |
// 4. Consider C11 aligned_alloc or posix_memalign | |
void ptrtointtoptr() { | |
const uint16_t alignment = 16; | |
const uint16_t align_min_1 = alignment - 1; | |
void * mem = malloc(1024+align_min_1); | |
// C89: void *ptr = (void *)(((INT_WITH_PTR_SIZE)mem+align_min_1) & ~(INT_WITH_PTR_SIZE)align_min_1); | |
void *ptr = (void *)(((uint64_t)mem+align_min_1) & ~(uint64_t)align_min_1); | |
// offset ptr to next alignment byte boundary | |
// void * ptr = (void *)(((uintptr_t)mem+align_min_1) & ~(uintptr_t)align_min_1); | |
// void * ptr = (void *)(((uintptr_t)mem+align_min_1) & ~(uintptr_t)align_min_1); | |
printf("0x%08" PRIXPTR ", 0x%08" PRIXPTR "\n", (uintptr_t)mem, (uintptr_t)ptr); | |
memset_16aligned(ptr, 0, 1024, alignment); | |
free(mem); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
misterspoon@pc ~/d/t/tryc> cerberus ptrtoint_inttoptr.c | |
misterspoon@pc ~/d/t/tryc> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment