Version 7.0
Copyright © 2021 Lowell D. Thomas
APG
… an ABNF Parser Generator
|
Go to the documentation of this file.
60 #if defined APG_MEM_STATS
84 #define STATS_ALLOC(s, i) vStatsAlloc((s), (i))
88 #define STATS_FREE(s, i) vStatsFree((s), (i))
92 #define STATS_REALLOC(s, i, o) vStatsRealloc((s), (i), (o))
93 static void vStatsAlloc(
mem_stats* spStats, mem_cell* spIn);
94 static void vStatsFree(
mem_stats* spStats, mem_cell* spIn);
95 static void vStatsRealloc(
mem_stats* spStats, mem_cell* spIn, mem_cell* spOut);
98 const void* vpValidate;
100 aint uiActiveCellCount;
101 mem_cell* spActiveList;
103 #define STATS_ALLOC(s, i)
104 #define STATS_FREE(s, i)
105 #define STATS_REALLOC(s, i, o)
109 static const void *s_vpMagicNumber = (
void*)
"memory";
110 static const char* s_cpMemory =
"memory allocation error";
111 static void vActivePush(
mem* spCtx, mem_cell* spCellIn);
112 static void vActivePop(
mem* spCtx, mem_cell* spCellIn);
126 spCtx = (
mem*) malloc(
sizeof(
mem));
128 XTHROW(spException,
"malloc failure");
130 memset((
void*) spCtx, 0,
sizeof(
mem));
131 spCtx->spException = spException;
133 return (
void*) spCtx;
142 mem* spCtx = (
mem*) vpCtx;
147 memset(vpCtx, 0,
sizeof(
mem));
161 mem* spCtx = (
mem*) vpCtx;
162 if (vpCtx && (spCtx->
vpValidate == s_vpMagicNumber)) {
175 mem* spCtx = (
mem*) vpCtx;
176 if (vpCtx && (spCtx->
vpValidate == s_vpMagicNumber)) {
197 mem* spCtx = (
mem*) vpCtx;
198 if (!vpCtx || (spCtx->
vpValidate != s_vpMagicNumber)) {
203 spCell = (mem_cell*) malloc(uiBytes +
sizeof(mem_cell));
205 spCell->uiSize = uiBytes;
208 vActivePush(spCtx, spCell);
212 return (
void*) (spCell + 1);
227 mem* spCtx = (
mem*) vpCtx;
228 if (vpCtx && spCtx->
vpValidate == s_vpMagicNumber) {
230 mem* spCtx = (
mem*) vpCtx;
232 spCell = (mem_cell*) vpData;
239 if(spThis == spCell){
242 spThis = spThis->spNext;
249 vActivePop(spCtx, spCell);
269 mem* spCtx = (
mem*) vpCtx;
270 if (vpCtx && spCtx->
vpValidate == s_vpMagicNumber) {
282 spOldCell = (mem_cell*) vpData;
289 if(spThis == spOldCell){
292 spThis = spThis->spNext;
294 XTHROW(spCtx->
spException,
"attempt to re-allocate an unallocated memory address");
297 if(spOldCell->uiSize == uiBytes){
299 return (
void*)(spOldCell + 1);
303 spNewCell = (mem_cell*) malloc(uiBytes +
sizeof(mem_cell));
305 spNewCell->spNext = spOldCell->spNext;
306 spNewCell->spPrev = spOldCell->spPrev;
307 spOldCell->spNext->spPrev = (
struct mem_cell_tag*) spNewCell;
308 spOldCell->spPrev->spNext = (
struct mem_cell_tag*) spNewCell;
309 spNewCell->uiSeq = spOldCell->uiSeq;
310 spNewCell->uiSize = uiBytes;
313 uiCopy = uiBytes < spOldCell->uiSize ? uiBytes : spOldCell->uiSize;
314 vpDst = (
void*) (spNewCell + 1);
315 vpSrc = (
void*) (spOldCell + 1);
316 memcpy(vpDst, vpSrc, uiCopy);
324 free((
void*) spOldCell);
325 return (
void*) (spNewCell + 1);
343 mem* spCtx = (
mem*) vpCtx;
344 if (vpCtx && spCtx->
vpValidate == s_vpMagicNumber) {
348 return (spLast->uiSeq + 1);
362 mem* spCtx = (
mem*) vpCtx;
363 if (vpCtx && (spCtx->
vpValidate == s_vpMagicNumber)) {
368 vActivePop(spCtx, spLast);
380 static void vActivePush(
mem* spCtx, mem_cell* spCellIn) {
423 static void vActivePop(
mem* spCtx, mem_cell* spCellIn) {
445 free((
void*) spCell);
448 #if defined APG_MEM_STATS
457 mem* spCtx = (
mem*) vpCtx;
458 if (vpCtx && spCtx->
vpValidate == s_vpMagicNumber) {
460 mem* spMemCtx = (
mem*) vpCtx;
472 static void vStatsAlloc(
mem_stats* spStats, mem_cell* spIn) {
475 spStats->
uiHeapBytes += spIn->uiSize +
sizeof(mem_cell);
489 static void vStatsFree(
mem_stats* spStats, mem_cell* spIn) {
492 spStats->
uiHeapBytes -= spIn->uiSize +
sizeof(mem_cell);
500 static void vStatsRealloc(
mem_stats* spStats, mem_cell* spIn, mem_cell* spOut) {
502 if(spOut->uiSize > spIn->uiSize){
503 spStats->
uiHeapBytes += spOut->uiSize - spIn->uiSize;
505 spStats->
uiHeapBytes -= spIn->uiSize - spOut->uiSize;
520 memset((
void*) spStats, 0,
sizeof(
mem_stats));
This header "#include"s all publid lib headers and other standard headers needed by most objects.
mem_cell * spActiveList
pointer to the first (and last) cell in a circularly, doubly linked list
void vMemDtor(void *vpCtx)
Destroys a Memory component. Frees all memory allocated.
mem_stats sStats
memory statistics
const void * vpValidate
validation handle
For internal use only. The memory object's context. Opaque to user applications.
aint uiSeq
The sequence number of this cell.
void vMemClear(void *vpCtx)
Frees all memory allocations.
void vExContext()
Handles bad context pointers.
#define STATS_REALLOC(s, i, o)
Called by the memory object to count memory re-allocations.
aint uiMemCount(void *vpCtx)
Returns the number of memory allocations.
#define STATS_ALLOC(s, i)
Called by the memory object to count memory allocations.
#define STATS_FREE(s, i)
Called by the memory object to count memory frees.
Available to the user for display of memory statistics.
void * vpMemRealloc(void *vpCtx, const void *vpData, aint uiBytes)
Re-allocates memory previously allocated with vpMemAlloc().
#define XTHROW(ctx, msg)
Exception throw macro.
struct mem_cell_tag * spPrev
pointer to the previous cell
uint_fast32_t aint
The APG parser's unsigned integer type.
exception * spMemException(void *vpCtx)
Get a pointer to this memory objects's exception handler.
void * vpMemAlloc(void *vpCtx, aint uiBytes)
Allocates memory.
A structure to describe the type and location of a caught exception.
void vMemFree(void *vpCtx, const void *vpData)
Free memory previously allocated with vpMemAlloc().
aint uiActiveCellCount
number of cells on the active list
abool bExValidate(exception *spException)
Test an exception structure for validity.
const void * vpValidate
Used by the memory object to validate the exception structure.
void * vpMemCtor(exception *spException)
Construct a memory component.
exception * spException
Pointer to the exception struct. NULL if none.
abool bMemValidate(void *vpCtx)
Validates a memory context.
void vMemStats(void *vpCtx, mem_stats *spStats)
Returns a copy of the Memory component's current statistics.
uint8_t abool
abool is the APG bool type.
struct mem_cell_tag * spNext
pointer to the next cell
aint uiSize
The usable size, in bytes, of this memory allocation.
A circularly-linked list structure.
APG Version 7.0 is licensed under the
2-Clause BSD License,
an Open Source Initiative Approved License.