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 bounderies. 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);
    ...
    free(ptr);

}

... would become

void func(zptr_t zptr);
    printf("%"PRIzptr"\n", zptr);
    ...
    free(zptrd(zptr));
}

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

Files

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

Macros

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

Typedefs

typedef uint16_t zptr_t
 zptr type definition
 

Functions

static int zptr_check (void *pointer)
 Determine if a pointer is compressable 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...
 

Function Documentation

◆ zptr_check()

static int zptr_check ( void *  pointer)
inlinestatic

Determine if a pointer is compressable by zptrc()

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

Definition at line 90 of file zptr.h.

◆ zptrc()

static zptr_t zptrc ( void *  pointer)
inlinestatic

Compress a pointer (if possible)

Substracts ZPTR_BASE, then right-shifts pointer by two.

Parameters
[in]pointerpointer to compress
Returns
compressed pointer

Definition at line 106 of file zptr.h.

◆ zptrd()

static void* zptrd ( zptr_t  zptr)
inlinestatic

Decompress a pointer.

Left-shifts zptr_t by two, then adds ZPTR_BASE.

Parameters
[in]zptrcompressed pointer
Returns
decompressed pointer

Definition at line 120 of file zptr.h.