MemoryBlock

Declares structure describing a block of memory.

Memory management is not offered by this type.

Summary
MemoryBlockDeclares structure describing a block of memory.
CopyrightThis program is free software.
Files
C-kern/api/memory/memblock.hHeader file of MemoryBlock.
C-kern/memory/memblock.cImplementation file MemoryBlock impl.
Types
struct memblock_tExport memblock_t.
Functions
test
memblock_tDescribes memory block.
addrPoints to start (lowest) address of memory.
sizeSize of memory in bytes addr points to.
lifetime
memblock_FREEStatic initializer.
memblock_INITStatic initializer.
query
isfree_memblockReturns true if mblock equals memblock_FREE.
isvalid_memblockReturns true if mblock->addr != 0 and block->size != 0.
addr_memblockReturns start (lowest) address of memory block.
size_memblockReturns size of memory block.
fill
clear_memblockSets the content of the memory block to 0.
resize
shrinkleft_memblockShrinks the memory block by incr its addr and decr its size.
shrinkright_memblockShrinks the memory block by decrementing only its size.
growleft_memblockGrows the memory block by decr its addr and incr its size..
growright_memblockGrows the memory block by incrementing only its size.
generic
genericcast_memblockCasts a pointer to generic object into pointer to memblock_t.
inline implementation
Macros
addr_memblockImplements memblock_t.addr_memblock.
clear_memblockImplements memblock_t.clear_memblock.
genericcast_memblockImplements memblock_t.genericcast_memblock.
growleft_memblockImplements memblock_t.growleft_memblock.
growright_memblockImplements memblock_t.growright_memblock.
isfree_memblockImplements memblock_t.isfree_memblock.
isvalid_memblockImplements memblock_t.isvalid_memblock.
shrinkleft_memblockImplements memblock_t.shrinkleft_memblock.
shrinkright_memblockImplements memblock_t.shrinkright_memblock.
size_memblockImplements memblock_t.size_memblock.

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

© 2011 Jörg Seebohn

Files

C-kern/api/memory/memblock.h

Header file of MemoryBlock.

C-kern/memory/memblock.c

Implementation file MemoryBlock impl.

Types

struct memblock_t

typedef struct memblock_t memblock_t

Export memblock_t.

Functions

Summary

test

memblock_t

struct memblock_t

Describes memory block.  This object does not offers its own memory allocation / deallocation functionality.  Other modules like memory managers offer a such functionality and return allocated memory with help of <meblock_t>.

Summary
addrPoints to start (lowest) address of memory.
sizeSize of memory in bytes addr points to.
lifetime
memblock_FREEStatic initializer.
memblock_INITStatic initializer.
query
isfree_memblockReturns true if mblock equals memblock_FREE.
isvalid_memblockReturns true if mblock->addr != 0 and block->size != 0.
addr_memblockReturns start (lowest) address of memory block.
size_memblockReturns size of memory block.
fill
clear_memblockSets the content of the memory block to 0.
resize
shrinkleft_memblockShrinks the memory block by incr its addr and decr its size.
shrinkright_memblockShrinks the memory block by decrementing only its size.
growleft_memblockGrows the memory block by decr its addr and incr its size..
growright_memblockGrows the memory block by incrementing only its size.
generic
genericcast_memblockCasts a pointer to generic object into pointer to memblock_t.

addr

uint8_t * addr

Points to start (lowest) address of memory.  The value (0) indicates an invalid memory block (freed state).

size

size_t size

Size of memory in bytes addr points to.  The valid memory region is

addr[ 0 .. size - 1 ]

lifetime

memblock_FREE

#define memblock_FREE { 0, 0 }

Static initializer.

memblock_INIT

#define memblock_INIT(size,
addr) { (addr), (size) }

Static initializer.

Parameters

sizeSize of memory block in bytes (size_t).
addrStart address of memory block (uint8_t*).

query

isfree_memblock

bool isfree_memblock(const memblock_t *mblock)

Returns true if mblock equals memblock_FREE.

isvalid_memblock

bool isvalid_memblock(const memblock_t *mblock)

Returns true if mblock->addr != 0 and block->size != 0.  This functions returns true for a memblock_t which is initialized to memblock_FREE.

addr_memblock

uint8_t * addr_memblock(const memblock_t *mblock)

Returns start (lowest) address of memory block. in case of the value null the block is in a freed state.

size_memblock

size_t size_memblock(const memblock_t *mblock)

Returns size of memory block.  The size can also be 0.

fill

clear_memblock

void clear_memblock(memblock_t *mblock)

Sets the content of the memory block to 0.

resize

shrinkleft_memblock

int shrinkleft_memblock(memblock_t *mblock,
size_t addr_increment)

Shrinks the memory block by incr its addr and decr its size.  The start address memblock_t.addr is incremented by addr_increment and the size of the block memblock_t.size is decremented by the same amount.

╭───────────────╮        ╭┈┈┈┈┬──────────╮
│<--- size ---->│    ==> │    │<- size'->│ addr' == addr + addr_increment
├───────────────┤        ╰┈┈┈┈├──────────┤ size' == size - addr_increment
└- addr         └- addr+size  └- addr'   └- addr+size == addr'+size'

shrinkright_memblock

int shrinkright_memblock(memblock_t *mblock,
size_t size_decrement)

Shrinks the memory block by decrementing only its size.  The start address memblock_t.addr is not changed but the size of the block memblock_t.size is decremented by size_decrement.

╭───────────────╮            ╭─────────┬┈┈┈┈┈╮
│<--- size ---->│    ==>     │<-size'->│<-D->│ D     == size_decrement
├───────────────┤            ╰─────────├┈┈┈┈┈┤ size' == size - D
└- addr         └- addr+size └- addr   └- addr+size'

growleft_memblock

int growleft_memblock(memblock_t *mblock,
size_t addr_decrement)

Grows the memory block by decr its addr and incr its size..  The start address memblock_t.addr is decremented by addr_decrement and the size of the block memblock_t.size is incremented by the same amount.

╭────────────╮            ╭┈┈┈┈┈──────────╮
│<-- size -->│        ==> │<--- size' --->│ addr' == addr - addr_decrement
├────────────┤            ├┈┈┈┈┈──────────┤ size' == size + addr_decrement
└- addr      └- addr+size └- addr'        └- addr'+size'

growright_memblock

int growright_memblock(memblock_t *mblock,
size_t size_increment)

Grows the memory block by incrementing only its size.  The start address memblock_t.addr is not changed but the size of the block memblock_t.size is incremented by size_increment.

╭────────────╮            ╭┈┈┈┈┈──────────╮
│<-- size -->│        ==> │<--- size' --->│
├────────────┤            ├┈┈┈┈┈──────────┤ size' == size + size_increment
└- addr      └- addr+size └- addr         └- addr+size'

generic

genericcast_memblock

memblock_t * genericcast_memblock(void *obj,
IDNAME nameprefix)

Casts a pointer to generic object into pointer to memblock_t.  The object must have two members nameprefix##addr and nameprefix##size of the same type as memblock_t and in the same order.

Macros

addr_memblock

#define addr_memblock(mblock) ((mblock)->addr)

Implements memblock_t.addr_memblock.

clear_memblock

#define clear_memblock(mblock) (memset((mblock)->addr, 0, (mblock)->size))

Implements memblock_t.clear_memblock.

genericcast_memblock

#define genericcast_memblock(
   obj,
   nameprefix
) ( __extension__ ({ typeof(obj) _obj = (obj) ; static_assert( sizeof(_obj->nameprefix##addr) == sizeof(((memblock_t*)0)->addr) && offsetof(memblock_t, addr) == 0 && (typeof(_obj->nameprefix##addr))10 == (uint8_t*)10 && sizeof(_obj->nameprefix##size) == sizeof(((memblock_t*)0)->size) && offsetof(memblock_t, size) == ((uintptr_t)&_obj->nameprefix##size) -((uintptr_t)&_obj->nameprefix##addr) && (typeof(_obj->nameprefix##size)*)10 == (size_t*)10, "obj is compatible") ; (memblock_t *)(&_obj->nameprefix##addr) ; }))

Implements memblock_t.genericcast_memblock.

growleft_memblock

growright_memblock

#define growright_memblock(
   mblock,
   size_increment
) ( __extension__ ({ int _err ; typeof(mblock) _mblock = (mblock) ; size_t _size = _mblock->size + (size_increment) ; if ( _size < _mblock->size || ((uintptr_t)_mblock->addr + _size < (uintptr_t)_mblock->addr)) { _err = ENOMEM ; } else { _mblock->size = _size ; _err = 0 ; } _err ; }))

Implements memblock_t.growright_memblock.

isfree_memblock

#define isfree_memblock(mblock) (0 == (mblock)->addr && 0 == (mblock)->size)

Implements memblock_t.isfree_memblock.

isvalid_memblock

#define isvalid_memblock(mblock) (0 != (mblock)->size && 0 != (mblock)->addr)

Implements memblock_t.isvalid_memblock.

shrinkleft_memblock

shrinkright_memblock

size_memblock

#define size_memblock(mblock) ((mblock)->size)

Implements memblock_t.size_memblock.

Declares structure describing a block of memory.
Implements MemoryBlock.
typedef struct memblock_t memblock_t
Export memblock_t.
struct memblock_t
Describes memory block.
uint8_t * addr
Points to start (lowest) address of memory.
size_t size
Size of memory in bytes addr points to.
#define memblock_FREE { 0, 0 }
Static initializer.
#define memblock_INIT(size,
addr) { (addr), (size) }
Static initializer.
bool isfree_memblock(const memblock_t *mblock)
Returns true if mblock equals memblock_FREE.
bool isvalid_memblock(const memblock_t *mblock)
Returns true if mblock->addr != 0 and block->size != 0.
uint8_t * addr_memblock(const memblock_t *mblock)
Returns start (lowest) address of memory block.
size_t size_memblock(const memblock_t *mblock)
Returns size of memory block.
void clear_memblock(memblock_t *mblock)
Sets the content of the memory block to 0.
int shrinkleft_memblock(memblock_t *mblock,
size_t addr_increment)
Shrinks the memory block by incr its addr and decr its size.
int shrinkright_memblock(memblock_t *mblock,
size_t size_decrement)
Shrinks the memory block by decrementing only its size.
int growleft_memblock(memblock_t *mblock,
size_t addr_decrement)
Grows the memory block by decr its addr and incr its size..
int growright_memblock(memblock_t *mblock,
size_t size_increment)
Grows the memory block by incrementing only its size.
memblock_t * genericcast_memblock(void *obj,
IDNAME nameprefix)
Casts a pointer to generic object into pointer to memblock_t.
#define addr_memblock(mblock) ((mblock)->addr)
Implements memblock_t.addr_memblock.
#define clear_memblock(mblock) (memset((mblock)->addr, 0, (mblock)->size))
Implements memblock_t.clear_memblock.
#define genericcast_memblock(
   obj,
   nameprefix
) ( __extension__ ({ typeof(obj) _obj = (obj) ; static_assert( sizeof(_obj->nameprefix##addr) == sizeof(((memblock_t*)0)->addr) && offsetof(memblock_t, addr) == 0 && (typeof(_obj->nameprefix##addr))10 == (uint8_t*)10 && sizeof(_obj->nameprefix##size) == sizeof(((memblock_t*)0)->size) && offsetof(memblock_t, size) == ((uintptr_t)&_obj->nameprefix##size) -((uintptr_t)&_obj->nameprefix##addr) && (typeof(_obj->nameprefix##size)*)10 == (size_t*)10, "obj is compatible") ; (memblock_t *)(&_obj->nameprefix##addr) ; }))
Implements memblock_t.genericcast_memblock.
#define growright_memblock(
   mblock,
   size_increment
) ( __extension__ ({ int _err ; typeof(mblock) _mblock = (mblock) ; size_t _size = _mblock->size + (size_increment) ; if ( _size < _mblock->size || ((uintptr_t)_mblock->addr + _size < (uintptr_t)_mblock->addr)) { _err = ENOMEM ; } else { _mblock->size = _size ; _err = 0 ; } _err ; }))
Implements memblock_t.growright_memblock.
#define isfree_memblock(mblock) (0 == (mblock)->addr && 0 == (mblock)->size)
Implements memblock_t.isfree_memblock.
#define isvalid_memblock(mblock) (0 != (mblock)->size && 0 != (mblock)->addr)
Implements memblock_t.isvalid_memblock.
#define size_memblock(mblock) ((mblock)->size)
Implements memblock_t.size_memblock.
Close