zptr.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2017 Kaspar Schleiser <kaspar@schleiser.de>
3  * SPDX-License-Identifier: LGPL-2.1-only
4  */
5 
6 #pragma once
7 
56 #include <assert.h>
57 #include <stdint.h>
58 #include <inttypes.h>
59 
60 #ifdef __cplusplus
61 extern "C" {
62 #endif
63 
64 #if ZPTR_BASE || defined(DOXYGEN)
65 
69 typedef uint16_t zptr_t;
70 
74 #define PRIzptr PRIu16
75 
79 #define ZPTR_MAX_ADDR ((uintptr_t)ZPTR_BASE + (1 << 18))
80 
86 static inline int zptr_check(void *pointer)
87 {
88  uintptr_t int_ptr = (uintptr_t)pointer;
89  return ((!(int_ptr & 0x3)) \
90  && (int_ptr >= (uintptr_t)ZPTR_BASE) \
91  && (int_ptr < ZPTR_MAX_ADDR));
92 }
93 
102 static inline zptr_t zptrc(void *pointer)
103 {
104  assert(zptr_check(pointer));
105  return (uint16_t)(((uint32_t)pointer - (uint32_t)ZPTR_BASE) >> 2);
106 }
107 
116 static inline void *zptrd(zptr_t zptr)
117 {
118  return (void *)(ZPTR_BASE + ((uint32_t)zptr << 2));
119 }
120 
121 #else /* ZPTR_BASE */
122 /* fallback implementation */
123 typedef void *zptr_t;
124 #define PRIzptr "p"
125 static inline int zptr_check(void *pointer) { (void)pointer; return 0; }
126 static inline zptr_t zptrc(void *pointer) { return (zptr_t)pointer; }
127 static inline void *zptrd(zptr_t zptr) { return (void *)zptr; }
128 #endif
129 
130 #ifdef __cplusplus
131 }
132 #endif
133 
POSIX.1-2008 compliant version of the assert macro.
#define assert(cond)
abort the program if assertion is false
Definition: assert.h:143
static zptr_t zptrc(void *pointer)
Compress a pointer (if possible)
Definition: zptr.h:102
static int zptr_check(void *pointer)
Determine if a pointer is compressible by zptrc()
Definition: zptr.h:86
#define ZPTR_MAX_ADDR
zptr highest compressible address
Definition: zptr.h:79
static void * zptrd(zptr_t zptr)
Decompress a pointer.
Definition: zptr.h:116
uint16_t zptr_t
zptr type definition
Definition: zptr.h:69
Adds include for missing inttype definitions.