zptr.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2017 Kaspar Schleiser <kaspar@schleiser.de>
3  *
4  * This file is subject to the terms and conditions of the GNU Lesser
5  * General Public License v2.1. See the file LICENSE in the top level
6  * directory for more details.
7  */
8 
9 #pragma once
10 
59 #include <assert.h>
60 #include <stdint.h>
61 #include <inttypes.h>
62 
63 #ifdef __cplusplus
64 extern "C" {
65 #endif
66 
67 #if ZPTR_BASE || defined(DOXYGEN)
68 
72 typedef uint16_t zptr_t;
73 
77 #define PRIzptr PRIu16
78 
82 #define ZPTR_MAX_ADDR ((uintptr_t)ZPTR_BASE + (1 << 18))
83 
89 static inline int zptr_check(void *pointer)
90 {
91  uintptr_t int_ptr = (uintptr_t)pointer;
92  return ((!(int_ptr & 0x3)) \
93  && (int_ptr >= (uintptr_t)ZPTR_BASE) \
94  && (int_ptr < ZPTR_MAX_ADDR));
95 }
96 
105 static inline zptr_t zptrc(void *pointer)
106 {
107  assert(zptr_check(pointer));
108  return (uint16_t)(((uint32_t)pointer - (uint32_t)ZPTR_BASE) >> 2);
109 }
110 
119 static inline void *zptrd(zptr_t zptr)
120 {
121  return (void *)(ZPTR_BASE + ((uint32_t)zptr << 2));
122 }
123 
124 #else /* ZPTR_BASE */
125 /* fallback implementation */
126 typedef void *zptr_t;
127 #define PRIzptr "p"
128 static inline int zptr_check(void *pointer) { (void)pointer; return 0; }
129 static inline zptr_t zptrc(void *pointer) { return (zptr_t)pointer; }
130 static inline void *zptrd(zptr_t zptr) { return (void *)zptr; }
131 #endif
132 
133 #ifdef __cplusplus
134 }
135 #endif
136 
POSIX.1-2008 compliant version of the assert macro.
#define assert(cond)
abort the program if assertion is false
Definition: assert.h:135
static zptr_t zptrc(void *pointer)
Compress a pointer (if possible)
Definition: zptr.h:105
static int zptr_check(void *pointer)
Determine if a pointer is compressible by zptrc()
Definition: zptr.h:89
#define ZPTR_MAX_ADDR
zptr highest compressible address
Definition: zptr.h:82
static void * zptrd(zptr_t zptr)
Decompress a pointer.
Definition: zptr.h:119
uint16_t zptr_t
zptr type definition
Definition: zptr.h:72
Adds include for missing inttype definitions.