#include <datadog/heap/sample_flag.h>
#if defined(__x86_64__)
bool dd_sample_flag_thread_init(void) {
return true;
}
void *dd_sample_flag_apply(void *raw, size_t alignment) {
return x86_apply(raw, alignment);
}
bool dd_sample_flag_peek(void *user, void **raw_out, size_t *offset_out) {
if (((uintptr_t)user & (DD_PAGE_SIZE - 1)) < DD_HEADER_BYTES) {
return false;
}
x86_header_t hdr = x86_header_read(user);
if (hdr.magic != DD_MAGIC) {
return false;
}
if (hdr.offset < DD_HEADER_BYTES || hdr.offset > 2 * DD_SAMPLE_ALIGNMENT_CAP) {
return false;
}
*raw_out = x86_raw_from_user(user, hdr.offset);
*offset_out = (size_t)hdr.offset;
return true;
}
#elif defined(__aarch64__)
void *dd_sample_flag_apply(void *raw, size_t alignment) {
(void)alignment;
return (void *)((uintptr_t)raw | DD_TBI_TAGGED);
}
bool dd_sample_flag_peek(void *user, void **raw_out, size_t *offset_out) {
if (!dd_sample_flag_check_and_clear(user, raw_out)) {
return false;
}
*offset_out = 0;
return true;
}
#if defined(__linux__)
#include <sys/prctl.h>
#ifndef PR_SET_TAGGED_ADDR_CTRL
# define PR_SET_TAGGED_ADDR_CTRL 55
#endif
#ifndef PR_TAGGED_ADDR_ENABLE
# define PR_TAGGED_ADDR_ENABLE (1UL << 0)
#endif
bool dd_sample_flag_thread_init(void) {
#if DD_HEAP_LIVE_TRACKING
return prctl(PR_SET_TAGGED_ADDR_CTRL, PR_TAGGED_ADDR_ENABLE, 0, 0, 0) == 0;
#else
return true;
#endif
}
#else
bool dd_sample_flag_thread_init(void) {
return true;
}
#endif
#else
# error "dd_sample_flag: unsupported architecture (expected __x86_64__ or __aarch64__)"
#endif