MemoryPointer

Supports extracting and adding bits to pointers.  Aligned pointers do not need all bits in their integer representation.  This module allows to modify unused bits to encode additional information.  For example the type of memory the pointer points to could be encoded.

Summary
MemoryPointerSupports extracting and adding bits to pointers.
CopyrightThis program is free software.
Files
C-kern/api/memory/ptr.hHeader file MemoryPointer.
C-kern/memory/ptr.cImplementation file MemoryPointer impl.
Types
struct ptr_tExport ptr_t into global namespace.
Functions
test
unittest_memory_ptrTest ptr_t and <ptrf_t> functionality.
ptr_tDefines generic pointer type which points to data in memory.
lifetime
ptr_FREEStatic initializer.
query
isaligned_ptrReturns true if all nrbits least significant bits of ptr are zero.
lsbits_ptrReturns the value of all nrbits least significant bits of ptr.
update
clearlsbits_ptrSets all nrbits least significant bits of ptr to zero.
orlsbits_ptrThe value of all nrbits least significant bits of value are ored into ptr.
inline implementation
Macros
clearlsbits_ptrImplements ptr_t.clearlsbits_ptr.
isaligned_ptrImplements ptr_t.isaligned_ptr.
lsbits_ptrImplements ptr_t.lsbits_ptr.
orlsbits_ptrImplements ptr_t.orlsbits_ptr.

Copyright

This program is free software.  You can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.

Author

© 2014 Jörg Seebohn

Files

C-kern/api/memory/ptr.h

Header file MemoryPointer.

C-kern/memory/ptr.c

Implementation file MemoryPointer impl.

Types

struct ptr_t

Export ptr_t into global namespace.

Functions

Summary
test
unittest_memory_ptrTest ptr_t and <ptrf_t> functionality.

test

unittest_memory_ptr

int unittest_memory_ptr(void)

Test ptr_t and <ptrf_t> functionality.

ptr_t

typedef void * ptr_t

Defines generic pointer type which points to data in memory.

Summary
lifetime
ptr_FREEStatic initializer.
query
isaligned_ptrReturns true if all nrbits least significant bits of ptr are zero.
lsbits_ptrReturns the value of all nrbits least significant bits of ptr.
update
clearlsbits_ptrSets all nrbits least significant bits of ptr to zero.
orlsbits_ptrThe value of all nrbits least significant bits of value are ored into ptr.

lifetime

ptr_FREE

#define ptr_FREE { 0 }

Static initializer.

query

isaligned_ptr

int isaligned_ptr(const ptr_t ptr,
unsigned nrbits)

Returns true if all nrbits least significant bits of ptr are zero.

Unchecked Precondition

nrbits < bitsof(void*)

lsbits_ptr

int lsbits_ptr(const ptr_t ptr,
unsigned nrbits)

Returns the value of all nrbits least significant bits of ptr.

Unchecked Precondition

nrbits < bitsof(void*)

update

clearlsbits_ptr

ptr_t clearlsbits_ptr(const ptr_t ptr,
unsigned nrbits)

Sets all nrbits least significant bits of ptr to zero.

Unchecked Precondition

nrbits < bitsof(void*)

orlsbits_ptr

ptr_t orlsbits_ptr(const ptr_t ptr,
unsigned nrbits,
uintptr_t value)

The value of all nrbits least significant bits of value are ored into ptr.  This function assumes that <isaligned_ptr>(ptr, nrbits) returns true.

Unchecked Precondition

nrbits < bitsof(void*)

Macros

clearlsbits_ptr

isaligned_ptr

#define isaligned_ptr(ptr,
nrbits) (0 == lsbits_ptr(ptr, nrbits))

Implements ptr_t.isaligned_ptr.

lsbits_ptr

Implements ptr_t.lsbits_ptr.

orlsbits_ptr

Implements ptr_t.orlsbits_ptr.

Supports extracting and adding bits to pointers.
Implements MemoryPointer.
typedef void * ptr_t
Defines generic pointer type which points to data in memory.
int unittest_memory_ptr(void)
Test ptr_t and ptrf_t functionality.
#define ptr_FREE { 0 }
Static initializer.
int isaligned_ptr(const ptr_t ptr,
unsigned nrbits)
Returns true if all nrbits least significant bits of ptr are zero.
int lsbits_ptr(const ptr_t ptr,
unsigned nrbits)
Returns the value of all nrbits least significant bits of ptr.
ptr_t clearlsbits_ptr(const ptr_t ptr,
unsigned nrbits)
Sets all nrbits least significant bits of ptr to zero.
ptr_t orlsbits_ptr(const ptr_t ptr,
unsigned nrbits,
uintptr_t value)
The value of all nrbits least significant bits of value are ored into ptr.
#define isaligned_ptr(ptr,
nrbits) (0 == lsbits_ptr(ptr, nrbits))
Implements ptr_t.isaligned_ptr.
Close