ngspice/src/misc/alloc.c

204 lines
3.3 KiB
C
Raw Normal View History

2000-04-27 22:03:57 +02:00
/**********
Copyright 1990 Regents of the University of California. All rights reserved.
$Id$
2000-04-27 22:03:57 +02:00
**********/
/*
* Memory alloction functions
*/
#include <ngspice/ngspice.h>
2000-04-27 22:03:57 +02:00
#ifndef HAVE_LIBGC
2000-04-27 22:03:57 +02:00
/*saj For Tcl module locking*/
#ifdef TCL_MODULE
#include <tcl.h>
#endif
2000-04-27 22:03:57 +02:00
2009-01-18 13:09:56 +01:00
#if defined(HAS_WINDOWS) || defined(HAS_TCLWIN)
2008-12-31 15:42:49 +01:00
#if defined(_MSC_VER) || defined(__MINGW32__)
#undef BOOLEAN
#include <windows.h>
extern HANDLE outheap;
#endif
2009-01-09 21:19:57 +01:00
#endif
2009-04-22 23:57:37 +02:00
2000-04-27 22:03:57 +02:00
/* Malloc num bytes and initialize to zero. Fatal error if the space can't
* be tmalloc'd. Return NULL for a request for 0 bytes.
2000-04-27 22:03:57 +02:00
*/
/* New implementation of tmalloc, it uses calloc and does not call bzero() */
void *
tmalloc(size_t num)
{
void *s;
/*saj*/
#ifdef TCL_MODULE
Tcl_Mutex *alloc;
alloc = Tcl_GetAllocMutex();
#endif
2000-04-27 22:03:57 +02:00
if (!num)
return NULL;
/*saj*/
#ifdef TCL_MODULE
Tcl_MutexLock(alloc);
#endif
2000-04-27 22:03:57 +02:00
s = calloc(num,1);
/*saj*/
#ifdef TCL_MODULE
Tcl_MutexUnlock(alloc);
#endif
2000-04-27 22:03:57 +02:00
if (!s){
fprintf(stderr,"malloc: Internal Error: can't allocate %ld bytes. \n",(long)num);
2010-01-30 15:04:59 +01:00
exit(EXIT_FAILURE);
2000-04-27 22:03:57 +02:00
}
return(s);
}
2009-01-09 21:19:57 +01:00
/* Original Berkeley Implementation */
/*
void *
tmalloc(size_t num)
{
void *s;
if (!num)
return NULL;
s = malloc((unsigned) num);
if (!s) {
fprintf(stderr,
"malloc: Internal Error: can't allocate %d bytes.\n", num);
exit(EXIT_BAD);
}
bzero(s, num);
return(s);
}
void *
trealloc(void *str, size_t num)
{
void *s;
if (!num) {
if (str)
free(str);
return NULL;
}
if (!str)
s = tmalloc(num);
else
s = realloc(str, (unsigned) num);
if (!s) {
fprintf(stderr,
"realloc: Internal Error: can't allocate %d bytes.\n", num);
exit(EXIT_BAD);
}
return(s);
}
*/
2000-04-27 22:03:57 +02:00
void *
trealloc(void *ptr, size_t num)
{
void *s;
/*saj*/
#ifdef TCL_MODULE
Tcl_Mutex *alloc;
alloc = Tcl_GetAllocMutex();
#endif
2000-04-27 22:03:57 +02:00
if (!num) {
if (ptr)
free(ptr);
return NULL;
}
if (!ptr)
s = tmalloc(num);
else {
/*saj*/
#ifdef TCL_MODULE
Tcl_MutexLock(alloc);
#endif
2000-04-27 22:03:57 +02:00
s = realloc(ptr, num);
/*saj*/
#ifdef TCL_MODULE
Tcl_MutexUnlock(alloc);
#endif
}
2000-04-27 22:03:57 +02:00
if (!s) {
2008-12-31 15:42:49 +01:00
fprintf(stderr,"realloc: Internal Error: can't allocate %ld bytes.\n", (long)num);
2010-01-30 15:04:59 +01:00
exit(EXIT_FAILURE);
2008-12-31 15:42:49 +01:00
}
return(s);
}
/* realloc using the output heap.
Function is used in outitf.c to prevent heap fragmentation
An additional heap outheap is used to store the plot output data.
*/
#if defined(HAS_TCLWIN)
2008-12-31 15:42:49 +01:00
#if defined(_MSC_VER) || defined(__MINGW32__)
void *
hrealloc(void *ptr, size_t num)
{
void *s;
/*saj*/
#ifdef TCL_MODULE
Tcl_Mutex *alloc;
alloc = Tcl_GetAllocMutex();
#endif
if (!num) {
if (ptr)
free(ptr);
return NULL;
}
if (!ptr)
s = HeapAlloc(outheap, HEAP_ZERO_MEMORY, num);
else {
/*saj*/
#ifdef TCL_MODULE
Tcl_MutexLock(alloc);
#endif
s = HeapReAlloc(outheap, HEAP_ZERO_MEMORY, ptr, num);
/*saj*/
#ifdef TCL_MODULE
Tcl_MutexUnlock(alloc);
#endif
}
if (!s) {
fprintf(stderr,"HeapReAlloc: Internal Error: can't allocate %ld bytes.\n", (long)num);
2010-01-30 15:04:59 +01:00
exit(EXIT_FAILURE);
2000-04-27 22:03:57 +02:00
}
return(s);
}
2008-12-31 15:42:49 +01:00
#endif
2009-01-09 21:19:57 +01:00
#endif
2000-04-27 22:03:57 +02:00
void
txfree(void *ptr)
{
/*saj*/
#ifdef TCL_MODULE
Tcl_Mutex *alloc;
alloc = Tcl_GetAllocMutex();
Tcl_MutexLock(alloc);
#endif
2000-04-27 22:03:57 +02:00
if (ptr)
free(ptr);
/*saj*/
#ifdef TCL_MODULE
Tcl_MutexUnlock(alloc);
#endif
2000-04-27 22:03:57 +02:00
}
#endif