Loading...
Searching...
No Matches
ptrtag.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2020 Otto-von-Guericke-Universität Magdeburg
3 *
4 * This file is subject to the terms and conditions of the GNU Lesser General
5 * Public License v2.1. See the file LICENSE in the top level directory for more
6 * details.
7 */
8
9#pragma once
10
62
63#include <assert.h>
64#include <inttypes.h>
65#include <stdint.h>
66
67#ifdef __cplusplus
68extern "C" {
69#endif
70
76#define PTRTAG __attribute__((aligned(4)))
77
89static inline void * ptrtag(void *ptr, uint8_t tag)
90{
91 uintptr_t tmp = (uintptr_t)ptr;
92 /* ensure ptr is aligned to four bytes and tag fits in two bits */
93 assert((tag < 4) && !(tmp & 0x3));
94 return (void *)(tmp | tag);
95}
96
102static inline void * ptrtag_ptr(void *tagged_ptr)
103{
104 uintptr_t tagged = (uintptr_t)tagged_ptr;
105 const uintptr_t mask = 0x3;
106 return (void *)(tagged & (~mask));
107}
108
114static inline uint8_t ptrtag_tag(void *tagged_ptr)
115{
116 uintptr_t tagged = (uintptr_t)tagged_ptr;
117 return tagged & 0x3;
118}
119
120#ifdef __cplusplus
121}
122#endif
123
POSIX.1-2008 compliant version of the assert macro.
#define assert(cond)
abort the program if assertion is false
Definition assert.h:135
static uint8_t ptrtag_tag(void *tagged_ptr)
Extract the tag from a tagged pointer.
Definition ptrtag.h:114
static void * ptrtag_ptr(void *tagged_ptr)
Extract the original pointer from a tagged pointer.
Definition ptrtag.h:102
static void * ptrtag(void *ptr, uint8_t tag)
Create a tagged pointer.
Definition ptrtag.h:89
Adds include for missing inttype definitions.