Pointer Compression

Provides 32bit -> 16bit pointer compression. More...

Detailed Description

Provides 32bit -> 16bit pointer compression.

On many platforms, some pointers may have to be aligned, e.g., to 4 byte boundaries. On 32bit platforms, that makes it possible to store all possible aligned 32bit pointers in a 16bit value as long as the total memory is small (e.g., with 4 byte alignment, all pointers within 256kb RAM can be represented by a 16bit value). This can save memory, at the cost of some instructions for compression/decompression.

In order to use pointer compression, ZPTR_BASE needs to be defined to a (4 byte aligned) base address.

A printf format macro (PRIzptr) is provided.

You can then use zptr_t instead of a pointer type, using the supplied functions to compress / decompress, e.g.,

void func(void *ptr) {
    printf("%"PRIzptr"\n", ptr);


... would become

void func(zptr_t zptr);
    printf("%"PRIzptr"\n", zptr);

If ZPTR_BASE is unset, zptr_t / zptrc() / zptrd() will transparently and without overhead compile to normal (uncompressed) pointer operations.


file  zptr.h
 32bit -> 16bit pointer compression implementation


#define PRIzptr   PRIu16
 zptr printf format definition More...
#define ZPTR_MAX_ADDR   ((uintptr_t)ZPTR_BASE + (1 << 18))
 zptr highest compressible address More...


typedef uint16_t zptr_t
 zptr type definition More...


static int zptr_check (void *pointer)
 Determine if a pointer is compressible by zptrc() More...
static zptr_t zptrc (void *pointer)
 Compress a pointer (if possible) More...
static void * zptrd (zptr_t zptr)
 Decompress a pointer. More...

Macro Definition Documentation

◆ PRIzptr

#define PRIzptr   PRIu16

zptr printf format definition

Definition at line 78 of file zptr.h.


#define ZPTR_MAX_ADDR   ((uintptr_t)ZPTR_BASE + (1 << 18))

zptr highest compressible address

Definition at line 83 of file zptr.h.

Typedef Documentation

◆ zptr_t

typedef uint16_t zptr_t

zptr type definition

Definition at line 73 of file zptr.h.

Function Documentation

◆ zptr_check()

static int zptr_check ( void *  pointer)

Determine if a pointer is compressible by zptrc()

[in]pointerpointer to check
1 if pointer can be compressed, 0 if not

Definition at line 90 of file zptr.h.

◆ zptrc()

static zptr_t zptrc ( void *  pointer)

Compress a pointer (if possible)

Subtracts ZPTR_BASE, then right-shifts pointer by two.

[in]pointerpointer to compress
compressed pointer

Definition at line 106 of file zptr.h.

◆ zptrd()

static void * zptrd ( zptr_t  zptr)

Decompress a pointer.

Left-shifts zptr_t by two, then adds ZPTR_BASE.

[in]zptrcompressed pointer
decompressed pointer

Definition at line 120 of file zptr.h.