From c7d8df0fb9971a35acddf15c55992449947d7f50 Mon Sep 17 00:00:00 2001 From: kougyokugentou <41278462+kougyokugentou@users.noreply.github.com> Date: Sun, 24 Oct 2021 16:13:48 -0700 Subject: [PATCH] A header file to allocate some memory (2006) --- demos/alloc_perm.h | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 demos/alloc_perm.h diff --git a/demos/alloc_perm.h b/demos/alloc_perm.h new file mode 100644 index 0000000..0553333 --- /dev/null +++ b/demos/alloc_perm.h @@ -0,0 +1,38 @@ +#define MAX_PERM_BLOCK 131072 + +/* + * Allocate some permanent memory. + * Permanent memory is never freed, + * pointers into it may be copied safely. + */ +void *alloc_perm( int sMem ) +{ + static char *pMemPerm; + static int iMemPerm; + void *pMem; + + while ( sMem % sizeof(long) != 0 ) + sMem++; + if ( sMem > MAX_PERM_BLOCK ) + { + printf("\n\rAlloc_perm: %d too large.", sMem ); + exit( 1 ); + } + + if ( pMemPerm == NULL || iMemPerm + sMem > MAX_PERM_BLOCK ) + { + iMemPerm = 0; + if ( ( pMemPerm = calloc( 1, MAX_PERM_BLOCK ) ) == NULL ) + { + perror( "Alloc_perm" ); + exit( 1 ); + } + } + + pMem = pMemPerm + iMemPerm; + iMemPerm += sMem; +// nAllocPerm += 1; +// sAllocPerm += sMem; + return pMem; +} +