/********************************************************************
This file is part of the abs 0.907 distribution. abs is a spreadsheet
with graphical user interface.
Copyright (C) 1998-2001 André Bertin (Andre.Bertin@ping.be)
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 if in the same spirit as version 2.
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 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., 675 Mass Ave, Cambridge, MA 02139, USA.
Concact: abs@pi.be
http://home.pi.be/bertin/abs.shtml
*********************************************************************/
#include <stdlib.h>
#include <stdio.h>
#define MEMSIZE 1
#ifdef MEMDEBUG
#include "object.h"
#include "cell.h"
#endif
static void *memmap[MEMSIZE];
static size_t memsize[MEMSIZE];
static char *allocmsg[MEMSIZE];
static long allocation = 0;
static int nptr = 0;
static int startmap = 0;
int
absstartmap ()
{
#ifdef MEMDEBUG
startmap = nptr - 1;
printf ("\n\n\n\n==============NEW WORKBOOK=====nptr %d======\n\n\n", nptr);
#endif
return 0;
}
int
absmemmap ()
{
#ifdef MEMDEBUG
int i;
printf ("\n\n\n\n==============WORKBOOK CLOSED=====nptr %d======\n\n\n", nptr);
for (i = startmap; i < nptr; i++)
{
if (!strncmp (allocmsg[i], "mkderef:base", 12))
{
printf ("ptr %d:%d len %d msg %s cont", i, memmap[i], memsize[i], allocmsg[i], (char *) memmap[i]);
printobj (*(obj *) memmap[i]);
printf ("\n");
}
else if (!strstr (allocmsg[i], "familly"))
{
Cell tmp;
tmp.familly = (Familly *) memmap[i];
printf ("ptr %d:%d len %d msg %s cont", i, memmap[i], memsize[i], allocmsg[i], (char *) memmap[i]);
printf ("\n");
}
else
printf ("ptr %d:%d len %d msg %s cont %10s\n", i, memmap[i], memsize[i], allocmsg[i], (char *) memmap[i]);
}
printf ("\n\n\n\n===============================================\n\n\n", nptr);
#endif
return 0;
}
void *
absmalloc (Size, message)
size_t Size;
char *message;
{
void *ptr;
ptr = malloc (Size);
if (ptr == NULL)
fprintf (stderr, "abs: cannot allocate memory size %d for %s!!!\n", Size, message);
#ifdef MEMDEBUG
memmap[nptr] = ptr;
memsize[nptr] = Size;
allocmsg[nptr] = message;
allocation += Size;
nptr++;
if (nptr > MEMSIZE - 1)
nptr = MEMSIZE - 1;
printf ("(%d/%d:%d) %35s: malloc size %d at %d\n", allocation, nptr, nptr - 1, message, Size, ptr);
return ptr;
#endif
return ptr;
}
void
absfree (Pointer, message)
void *Pointer;
char *message;
{
#ifdef MEMDEBUG
int i, j;
size_t Size = 0;
if (Pointer != NULL)
{
i = nptr - 1;
while (i >= 0 && memmap[i] != Pointer)
i--;
if (i >= 0)
{
Size = memsize[i];
allocation = allocation - Size;
for (j = i; j < nptr - 1; j++)
{
memsize[j] = memsize[j + 1];
memmap[j] = memmap[j + 1];
allocmsg[j] = allocmsg[j + 1];
}
nptr--;
}
else
printf ("WARNING free of ptr %d not found!!! (%s)\n", Pointer, message);
}
else
printf ("WARNING free of NULL ptr %d !!! (%s)\n", Pointer, message);
printf ("(%d/%d:%d) %35s: free size %d at %d\n", allocation, nptr, i, message, Size, Pointer);
if (Size <= 0)
{
printf ("WARNING free of Size %d ptr %d !!! (%s)\n", Size, Pointer, message);
return;
}
#endif
if (Pointer == NULL)
fprintf (stderr, "abs: free of NULL pointer for %s!!!\n", message);
free (Pointer);
}
void *
absrealloc (Pointer, Size, message)
void *Pointer;
size_t Size;
char *message;
{
void *ptr;
#ifdef MEMDEBUG
int i = -1;
int j;
size_t oldSize = 0;
if (Pointer != NULL)
{
i = nptr - 1;
while (i >= 0 && memmap[i] != Pointer)
i--;
if (i >= 0)
{
oldSize = memsize[i];
allocation = allocation - memsize[i];
allocation = allocation + Size;
memsize[i] = Size;
allocmsg[i] = message;
}
else
{
printf ("WARNING realloc to non NULL ptr %d not found!!! (%s)\n", Pointer, message);
}
}
else
{
i = nptr;
nptr++;
if (nptr > MEMSIZE - 1)
{
nptr = MEMSIZE - 1;
fprintf (stderr, "NO MORE SPACE TO HOLD POINTER TABLE!!!\n");
}
allocation += Size;
memsize[i] = Size;
allocmsg[i] = message;
}
ptr = realloc (Pointer, Size);
memmap[i] = ptr;
printf ("(%d/%d:%d) %35s: realloc size %d from %d to ", allocation, nptr, i, message, oldSize, Pointer);
printf ("size %d at %d\n", Size, ptr);
#endif
#ifndef MEMDEBUG
ptr = realloc (Pointer, Size);
#endif
if (ptr == NULL)
fprintf (stderr, "abs: cannot reallocate memory size %d for %s!!!\n", Size, message);
return ptr;
}
syntax highlighted by Code2HTML, v. 0.9.1