Loading...
Searching...
No Matches
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
57#ifndef ZPTR_H
58#define ZPTR_H
59
60#include <assert.h>
61#include <stdint.h>
62#include <inttypes.h>
63
64#ifdef __cplusplus
65extern "C" {
66#endif
67
68#if ZPTR_BASE || defined(DOXYGEN)
69
73typedef uint16_t zptr_t;
74
78#define PRIzptr PRIu16
79
83#define ZPTR_MAX_ADDR ((uintptr_t)ZPTR_BASE + (1 << 18))
84
90static inline int zptr_check(void *pointer)
91{
92 uintptr_t int_ptr = (uintptr_t)pointer;
93 return ((!(int_ptr & 0x3)) \
94 && (int_ptr >= (uintptr_t)ZPTR_BASE) \
95 && (int_ptr < ZPTR_MAX_ADDR));
96}
97
106static inline zptr_t zptrc(void *pointer)
107{
108 assert(zptr_check(pointer));
109 return (uint16_t)(((uint32_t)pointer - (uint32_t)ZPTR_BASE) >> 2);
110}
111
120static inline void *zptrd(zptr_t zptr)
121{
122 return (void *)(ZPTR_BASE + ((uint32_t)zptr << 2));
123}
124
125#else /* ZPTR_BASE */
126/* fallback implementation */
127typedef void *zptr_t;
128#define PRIzptr "p"
129static inline int zptr_check(void *pointer) { (void)pointer; return 0; }
130static inline zptr_t zptrc(void *pointer) { return (zptr_t)pointer; }
131static inline void *zptrd(zptr_t zptr) { return (void *)zptr; }
132#endif
133
134#ifdef __cplusplus
135}
136#endif
137
138#endif /* ZPTR_H */
POSIX.1-2008 compliant version of the assert macro.
#define assert(cond)
abort the program if assertion is false
Definition assert.h:137
static zptr_t zptrc(void *pointer)
Compress a pointer (if possible)
Definition zptr.h:106
static int zptr_check(void *pointer)
Determine if a pointer is compressible by zptrc()
Definition zptr.h:90
#define ZPTR_MAX_ADDR
zptr highest compressible address
Definition zptr.h:83
static void * zptrd(zptr_t zptr)
Decompress a pointer.
Definition zptr.h:120
uint16_t zptr_t
zptr type definition
Definition zptr.h:73
Adds include for missing inttype definitions.