Malloc wrapper's test/example program:
#include <stdio.h> #include "MeMessage.h" #include "MeMemory.h" #define testINCREASING_BLOCKS 20 #define testINCREASING_MIN 5 #define testINCREASING_NUM 3 #define testINCREASING_DEN 2 #define testRANDOM_MIN 7 #define testRANDOM_MAX 1340 #define testRANDOM_BLOCKS 20 #define testRANDOM_REQUESTS 350 extern struct MeMemoryAPI MeMemoryAPIMalloc; struct MeMemoryOptions opts; struct MeMemoryOptions opts2; struct MeMemoryStats stats; #define optsB opts.block.flex #define statsB stats.block.flex int main(int argc,char *(argv[])) { MeMemoryDoTrace = 1; opts.allocation = MeMemoryAny; opts.reclamation = MeMemoryManual; opts.minArena = 128*1024; opts.maxArena = 1024*1024; opts.blockFixed = 0; optsB.align2Addr = 3; optsB.align2Size = 6; optsB.minSize = 16; optsB.maxSize = 300000; optsB.minTotal = 256*1024; optsB.maxTotal = 1024*1024; MeInfo(4,"Options: before 'setOptions':\n"); if (MeInfoLevel >= 4) MeMemoryPrintOptions(&opts); (*MeMemoryAPIMalloc.setOptions)(&opts); MeInfo(9,"Options: after 'setOptions'\n"); (*MeMemoryAPIMalloc.getOptions)(&opts2); if (MeInfoLevel >= 9) MeMemoryPrintOptions(&opts2); (*MeMemoryAPIMalloc.init)(0,0); MeInfo(9,"Options: after 'init':\n"); (*MeMemoryAPIMalloc.getOptions)(&opts2); if (MeInfoLevel >= 9) MeMemoryPrintOptions(&opts2); MeInfo(9,"Stats: after 'init'\n"); (*MeMemoryAPIMalloc.getStats)(&stats); if (MeInfoLevel >= 9) MeMemoryPrintStats(&stats); /* test increasing block sizes */ { unsigned n; unsigned size; void *(blocks)[testINCREASING_BLOCKS]; for (n = 0, size = testINCREASING_MIN; n < testINCREASING_BLOCKS; n++, size = (size*testINCREASING_NUM)/testINCREASING_DEN) { blocks[n] = (*MeMemoryAPIMalloc.create)(size); MeInfo(2,"Create: in increasing n %u size %u b 0x%0lx\n", n,size,(long unsigned) blocks[n]); MeInfo(2,"Stats: after each 'create' in increasing\n"); if (MeInfoLevel >= 2) { (*MeMemoryAPIMalloc.getStats)(&stats); MeMemoryPrintStats(&stats); } } MeInfo(9,"Stats: after 'create's in increasing\n"); (*MeMemoryAPIMalloc.getStats)(&stats); if (MeInfoLevel >= 9) MeMemoryPrintStats(&stats); for (n = 0; n < testINCREASING_BLOCKS; n++) { MeInfo(2,"Destroy: 0x%0lx\n",(long unsigned) blocks[n]); (*MeMemoryAPIMalloc.destroy)(blocks[n]); MeInfo(2,"Stats: after each 'destroy' in increasing\n"); if (MeInfoLevel >= 2) { (*MeMemoryAPIMalloc.getStats)(&stats); MeMemoryPrintStats(&stats); } } MeInfo(9,"Stats: after 'destroys's in increasing\n"); (*MeMemoryAPIMalloc.getStats)(&stats); if (MeInfoLevel >= 9) MeMemoryPrintStats(&stats); (*MeMemoryAPIMalloc.term)(); MeInfo(9,"Stats: after 'term' in increasing\n"); (*MeMemoryAPIMalloc.getStats)(&stats); if (MeInfoLevel >= 9) MeMemoryPrintStats(&stats); } return 0; }
Copyright MathEngine PLC 2000, all rights reserved.