MathEngine
Home Page       Structures       File List       Functions and Macros      

ArraysBench.c

The Arrays memory manager's benchmark program:

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>

#include "MeMessage.h"
#include "MeMemory.h"
#include "MeAssert.h"

#define maxBLOCKS       2000

extern struct MeMemoryAPI MeMemoryAPIArrays;

struct MeMemoryOptions  opts;
struct MeMemoryOptions  opts2;
struct MeMemoryStats    stats;  

#define optsB           opts.block.fixed
#define statsB          stats.block.fixed

#define call(function)  (*MeMemoryAPIArrays.##function)

struct MeMemoryOptionsFixed optsF[] =
{
  /* align2Addr, minSize,defSize, minCount,maxCount */
  { 4,          12,16,          100,100 },
  { 4,          20,24,          200,200 },
  { 4,          28,40,          100,100 },
  { 4,          44,52,          300,300 },
  { 4,          56,64,          100,100 },
  { 4,          70,72,          150,150 },
  { 4,          80,96,          200,200 },
  { 4,          100,112,        100,100 }
};

int main(int argc,char *(argv[]))
{
  unsigned              arraysOrMalloc = 1;

  unsigned              loops;
  long unsigned         allocations = 0;
  const unsigned        optsFcount = sizeof optsF/sizeof *optsF;

  if (argc > 1)
    arraysOrMalloc = atoi(argv[1]);

  MeInfoLevel           = 0;
  MeWarningLevel        = 0;
  MeFatalErrorLevel     = 0;
  MeDebugLevel          = 0;

  if (arraysOrMalloc)
    {
      MeMemoryDoTrace       = 0;
      MeMemoryDoStats       = 0;

      opts.allocation       = MeMemoryArrays;
      opts.reclamation      = MeMemoryManual;

      opts.maxArena         = 128*1024;
      opts.minArena         = 0;

      opts.blockFixed       = optsFcount;

      optsB                 = &optsF[0];

      call(setOptions)(&opts);
      call(getStats)(&stats);
      call(init)(malloc(stats.maxArena),stats.maxArena);
    }

  for (loops = 0; loops < 8000; loops++)
  {
    unsigned l,m;
    register unsigned n = 0;
    void *(blocks)[maxBLOCKS];

    for (l = 0; n < maxBLOCKS && l < optsFcount; l++)
      {
        const struct MeMemoryOptionsFixed *const o = optsF+l;

        for (m = 0; n < maxBLOCKS && m < o->maxCount; m++)
          {
            register void *block;

            if (arraysOrMalloc)
              block = (*MeMemoryAPIArrays.create)(o->defSize);
            else
              block = malloc(o->defSize);

            *(char *) block = (char) 0xAA;

            blocks[n++] = block;
            allocations++;
          }
      }

    MEASSERT (n != 0);

    do
      {
        --n;

        if (arraysOrMalloc)
          (*MeMemoryAPIArrays.destroy)(blocks[n]);
        else
          free(blocks[n]);
      } 
    while (n != 0);
  }

  if (arraysOrMalloc)
    call(term)();

  printf("%lu create/destroy\n",allocations);

  return 0;
}

MathEngine Utilities: Memory Manager API - Version 0.0.5 Alpha - Reference Manual generated using doxygen at Wed Oct 11 00:34:56 2000

Copyright MathEngine PLC 2000, all rights reserved.