X-Git-Url: http://4ch.mooo.com/gitweb/?a=blobdiff_plain;f=16%2Fv2%2Fsource%2FMAPED%2FA_MEMORY.C;fp=16%2Fv2%2Fsource%2FMAPED%2FA_MEMORY.C;h=c8f1598b65cd7e51b27a60051dc887061f82b69e;hb=47cdc66151d973d975d0e31fb8a786eb639bebdb;hp=0000000000000000000000000000000000000000;hpb=4b23f27092a9470a741e3a18261ad389fd1929db;p=16.git diff --git a/16/v2/source/MAPED/A_MEMORY.C b/16/v2/source/MAPED/A_MEMORY.C new file mode 100755 index 00000000..c8f1598b6 --- /dev/null +++ b/16/v2/source/MAPED/A_MEMORY.C @@ -0,0 +1,211 @@ +/* +Copyright (C) 1998 BJ Eirich (aka vecna) +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 Lic +See the GNU General Public License for more details. +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +*/ + +#include +#include +#include + +void err(char *str, ...); +void Log(char *str, ...); + +// ***** +// TODO: Move the chunk list from an array to a linked list? +// Would eliminate hardcoded chunk limit, but would make +// general operation slower. Probably not The Right Thing, +// sides the chunk limit can be interesting sometimes. If +// it becomes problematic, consider a Binary Tree. +// ***** + +// ***************************** Data ***************************** + +#define MAXCHUNKS 200 +#define PARANOID 1 +#define PADFILLVALUE 254 +#define PADSIZE 256 + +void MemReport(void); + +#define errm MemReport(), err + +typedef struct +{ + void *pointer; + int size; + int owner; + char desc[20]; +} memblockType; + +memblockType chunks[MAXCHUNKS+1]; +int numchunks=0; + +// ***************************** Code ***************************** + +void *valloc(int amount, char *desc, int owner) +{ + if (numchunks == MAXCHUNKS) + err("Failed allocated %d bytes (%s), reason: Out of chunks.", + amount, desc); + +#ifdef PARANOID + CheckCorruption(); + chunks[numchunks].pointer = (void *) ((int) malloc(amount + (PADSIZE * 2)) + PADSIZE); + chunks[numchunks].size = amount; + memset((char *) chunks[numchunks].pointer - PADSIZE, PADFILLVALUE, PADSIZE); + memset((char *) chunks[numchunks].pointer + + chunks[numchunks].size, PADFILLVALUE, PADSIZE); +#else + chunks[numchunks].pointer = malloc(amount); + chunks[numchunks].size = amount; +#endif + chunks[numchunks].owner = owner; + strncpy(chunks[numchunks].desc, desc, 19); + memset(chunks[numchunks].pointer, 0, chunks[numchunks].size); + return chunks[numchunks++].pointer; +} + +void *qvalloc(int amount) +{ + void *ptr; + + // Quick and dirty memory allocation. Should be used ONLY + // for temporary blocks in speed-critical loops. + + ptr = malloc(amount); + if (!ptr) errm("qvalloc: Failed allocating %d bytes.", amount); + return ptr; +} + +void qvfree(void *ptr) +{ + free(ptr); +} + +int TotalBytesAllocated(void) +{ + int i, tally=0; + + for (i=0; i