MemoryStream

Wraps a memory block which points to start and end address.  Use this structure to stream memory, i.e. read bytes and increment the start address to point to the next unread byte.

Summary
MemoryStreamWraps a memory block which points to start and end address.
CopyrightThis program is free software.
Files
C-kern/api/memory/memstream.hHeader file MemoryStream.
C-kern/memory/memstream.cImplementation file MemoryStream impl.
Types
struct memstream_tExport memstream_t into global namespace.
Functions
test
unittest_memory_memstreamTest memstream_t functionality.
memstream_tWraps a memory block which points to start and end address.
nextPoints to next unread memory byte.
endPoints one after the last memory byte.
lifetime
memstream_FREEStatic initializer.
memstream_INITInitializes memstream_t.
query
size_memstreamReturns the nuber of unread bytes.
generic
genericcast_memstreamCasts pointer obj into pointer to memstream_t.
inline implementation
Macros
genericcast_memstreamImplements memstream_t.genericcast_memstream.
size_memstreamImplements memstream_t.size_memstream.

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

© 2013 Jörg Seebohn

Files

C-kern/api/memory/memstream.h

Header file MemoryStream.

C-kern/memory/memstream.c

Implementation file MemoryStream impl.

Types

struct memstream_t

typedef struct memstream_t memstream_t

Export memstream_t into global namespace.

Functions

Summary

test

unittest_memory_memstream

int unittest_memory_memstream(void)

Test memstream_t functionality.

memstream_t

struct memstream_t

Wraps a memory block which points to start and end address.  The start address is the lowest address of an allocated memory block.  The end address points one after the highest address of the allocated memory block.

Summary
nextPoints to next unread memory byte.
endPoints one after the last memory byte.
lifetime
memstream_FREEStatic initializer.
memstream_INITInitializes memstream_t.
query
size_memstreamReturns the nuber of unread bytes.
generic
genericcast_memstreamCasts pointer obj into pointer to memstream_t.

next

uint8_t * next

Points to next unread memory byte. next is always lower or equal to end.

end

uint8_t * end

Points one after the last memory byte.  The number of unread bytes can be determined by “end - next”.  If next equals end all bytes are read.

lifetime

memstream_FREE

#define memstream_FREE { 0, 0 }

Static initializer.

memstream_INIT

#define memstream_INIT(start,
end) { start, end }

Initializes memstream_t.

Parameter

startPoints to first unread byte.
endPoints one after the last unread byte .
The difference (endstart) is the positive number of the memory block size.

query

size_memstream

size_t size_memstream(const memstream_t *memstr)

Returns the nuber of unread bytes.

generic

genericcast_memstream

memstream_t * genericcast_memstream(void *obj,
IDNAME nameprefix)

Casts pointer obj into pointer to memstream_t. obj must point a generic object with two members nameprefix##next and nameprefix##end of the same type as the members of memstream_t and in the same order.

Macros

genericcast_memstream

#define genericcast_memstream(
   obj,
   nameprefix
) ( __extension__ ({ typeof(obj) _obj = (obj) ; static_assert( &((memstream_t*)((uintptr_t) &_obj->nameprefix##next))->next == &_obj->nameprefix##next && &((memstream_t*)((uintptr_t) &_obj->nameprefix##next))->end == &_obj->nameprefix##end, "obj is compatible") ; (memstream_t *)(&_obj->nameprefix##next) ; }))

Implements memstream_t.genericcast_memstream.

size_memstream

#define size_memstream(
   memstr
) ( __extension__ ({ const memstream_t * _m ; _m = (memstr) ; (size_t) (_m->end - _m->next) ; }))

Implements memstream_t.size_memstream.

Wraps a memory block which points to start and end address.
Implements MemoryStream.
typedef struct memstream_t memstream_t
Export memstream_t into global namespace.
struct memstream_t
Wraps a memory block which points to start and end address.
int unittest_memory_memstream(void)
Test memstream_t functionality.
uint8_t * next
Points to next unread memory byte.
uint8_t * end
Points one after the last memory byte.
#define memstream_FREE { 0, 0 }
Static initializer.
#define memstream_INIT(start,
end) { start, end }
Initializes memstream_t.
size_t size_memstream(const memstream_t *memstr)
Returns the nuber of unread bytes.
memstream_t * genericcast_memstream(void *obj,
IDNAME nameprefix)
Casts pointer obj into pointer to memstream_t.
#define genericcast_memstream(
   obj,
   nameprefix
) ( __extension__ ({ typeof(obj) _obj = (obj) ; static_assert( &((memstream_t*)((uintptr_t) &_obj->nameprefix##next))->next == &_obj->nameprefix##next && &((memstream_t*)((uintptr_t) &_obj->nameprefix##next))->end == &_obj->nameprefix##end, "obj is compatible") ; (memstream_t *)(&_obj->nameprefix##next) ; }))
Implements memstream_t.genericcast_memstream.
#define size_memstream(
   memstr
) ( __extension__ ({ const memstream_t * _m ; _m = (memstr) ; (size_t) (_m->end - _m->next) ; }))
Implements memstream_t.size_memstream.
Close