/*
* The Spar Library - modular math parser
* Copyright (C) 2000,2001 Davide Angelocola <davide178@inwind.it>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*
*/
#include <stdlib.h>
#include <spar/sl_util.h>
#include <spar/sl_io.h>
#include <spar/sl_conf.h>
#include <spar/sl_error.h>
#ifdef DEBUG
static sl_size __allocated = 0;
static sl_size __de_allocated = 0;
static sl_size __free_counter = 0;
static sl_size __malloc_counter = 0;
#endif
#define BYTES_TO_KB 1024
void *
sl_malloc (sl_size bytes)
{
void *m = malloc (bytes);
if (m == NULL)
sl_error_throw (SL_ERROR_CLASS_FATAL, SL_ERROR_NO_MEM, "(%uK).",
bytes / BYTES_TO_KB);
#ifdef DEBUG
__allocated += bytes;
__malloc_counter++;
#endif
return m;
}
void *
sl_realloc (void *m, sl_size bytes)
{
sl_assert (m != NULL);
sl_assert (bytes > 0);
{
void *new = realloc (m, bytes);
if (m == NULL)
sl_error_throw (SL_ERROR_CLASS_FATAL, SL_ERROR_NO_MEM, "(%uK).",
bytes / BYTES_TO_KB);
#ifdef DEBUG
__allocated += bytes;
__malloc_counter++;
#endif
return new;
}
}
void *
sl_calloc (sl_size nmemb, sl_size bytes)
{
void *m = calloc (nmemb, bytes);
if (m == NULL)
sl_error_throw (SL_ERROR_CLASS_FATAL, SL_ERROR_NO_MEM, "(%uK).",
(nmemb * bytes) / BYTES_TO_KB);
#ifdef DEBUG
__allocated += nmemb * bytes;
__malloc_counter++;
#endif
return m;
}
void
sl_free (void *m, sl_size bytes)
{
#ifdef DEBUG
__de_allocated += bytes;
__free_counter++;
#endif
if (m == NULL)
sl_error_throw (SL_ERROR_CLASS_FATAL, SL_ERROR_FREE_TWICE,
"this memory localtion points to NULL. Are you sure that this is what you want?");
free (m);
}
void
sl_memory_stats (void)
{
#ifdef DEBUG
sl_writeln ("Memory Stats");
sl_writeln ("allocated : %d Kb (%d calls).", __allocated / BYTES_TO_KB,
__malloc_counter);
sl_writeln ("deallocated : %d Kb (%d calls).", __de_allocated / BYTES_TO_KB,
__free_counter);
#endif
}
syntax highlighted by Code2HTML, v. 0.9.1