Loading...
Searching...
No Matches
ptrtag.h
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2020 Otto-von-Guericke-Universität Magdeburg
3 * SPDX-License-Identifier: LGPL-2.1-only
4 */
5
6#pragma once
7
59
60#include <assert.h>
61#include <inttypes.h>
62#include <stdint.h>
63
64#ifdef __cplusplus
65extern "C" {
66#endif
67
73#define PTRTAG __attribute__((aligned(4)))
74
86static inline void * ptrtag(void *ptr, uint8_t tag)
87{
88 uintptr_t tmp = (uintptr_t)ptr;
89 /* ensure ptr is aligned to four bytes and tag fits in two bits */
90 assert((tag < 4) && !(tmp & 0x3));
91 return (void *)(tmp | tag);
92}
93
99static inline void * ptrtag_ptr(void *tagged_ptr)
100{
101 uintptr_t tagged = (uintptr_t)tagged_ptr;
102 const uintptr_t mask = 0x3;
103 return (void *)(tagged & (~mask));
104}
105
111static inline uint8_t ptrtag_tag(void *tagged_ptr)
112{
113 uintptr_t tagged = (uintptr_t)tagged_ptr;
114 return tagged & 0x3;
115}
116
117#ifdef __cplusplus
118}
119#endif
120
POSIX.1-2008 compliant version of the assert macro.
#define assert(cond)
abort the program if assertion is false
Definition assert.h:143
static uint8_t ptrtag_tag(void *tagged_ptr)
Extract the tag from a tagged pointer.
Definition ptrtag.h:111
static void * ptrtag_ptr(void *tagged_ptr)
Extract the original pointer from a tagged pointer.
Definition ptrtag.h:99
static void * ptrtag(void *ptr, uint8_t tag)
Create a tagged pointer.
Definition ptrtag.h:86
Adds include for missing inttype definitions.