2000-04-27 22:03:57 +02:00
|
|
|
/**********
|
|
|
|
|
Copyright 1990 Regents of the University of California. All rights reserved.
|
2005-05-21 17:56:20 +02:00
|
|
|
$Id$
|
2000-04-27 22:03:57 +02:00
|
|
|
**********/
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Memory alloction functions
|
|
|
|
|
*/
|
2000-10-14 23:49:25 +02:00
|
|
|
#include <config.h>
|
2000-04-27 22:03:57 +02:00
|
|
|
|
2000-10-14 23:49:25 +02:00
|
|
|
#ifndef HAVE_LIBGC
|
|
|
|
|
#include <ngspice.h>
|
2000-04-27 22:03:57 +02:00
|
|
|
#include <stdio.h>
|
2000-10-14 23:49:25 +02:00
|
|
|
#include <memory.h>
|
2000-04-27 22:03:57 +02:00
|
|
|
|
2008-08-27 15:39:05 +02:00
|
|
|
/*saj For Tcl module locking*/
|
|
|
|
|
#ifdef TCL_MODULE
|
|
|
|
|
#include <tcl.h>
|
|
|
|
|
//#include <tclDecls.h>
|
|
|
|
|
#endif
|
2000-04-27 22:03:57 +02:00
|
|
|
|
|
|
|
|
/* Malloc num bytes and initialize to zero. Fatal error if the space can't
|
* src/main.c, src/multidec.c, src/proc2mod.c,
src/frontend/display.c, src/frontend/outitf.c,
src/frontend/help/readhelp.c, src/frontend/help/x11disp.c,
src/frontend/parser/complete.c, src/frontend/parser/glob.c,
src/frontend/plotting/graf.c,
src/frontend/plotting/graphdb.c,
src/frontend/plotting/x11.c, src/include/graph.h,
src/include/iferrmsg.h, src/include/ifsim.h,
src/include/macros.h, src/maths/poly/polyfit.c,
src/maths/sparse/spalloc.c, src/maths/sparse/spconfig.h,
src/misc/alloc.c, src/misc/mktemp.c,
src/spicelib/analysis/cktpzstr.c,
src/spicelib/devices/bsim2/b2temp.c,
src/spicelib/devices/bsim3/b3temp.c,
src/spicelib/devices/bsim3v1/b3v1temp.c,
src/spicelib/devices/bsim3v2/b3v2temp.c,
src/spicelib/devices/bsim4/b4temp.c: replaced malloc
realloc and free calls to use tmalloc trealloc and txfree.
* tests/diffpair.out, tests/fourbitadder.out,
tests/resistance/res_partition.out: Updated.
2000-10-14 15:16:53 +02:00
|
|
|
* 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;
|
2008-08-27 15:39:05 +02:00
|
|
|
/*saj*/
|
|
|
|
|
#ifdef TCL_MODULE
|
|
|
|
|
Tcl_Mutex *alloc;
|
|
|
|
|
alloc = Tcl_GetAllocMutex();
|
|
|
|
|
#endif
|
2000-04-27 22:03:57 +02:00
|
|
|
if (!num)
|
|
|
|
|
return NULL;
|
2008-08-27 15:39:05 +02:00
|
|
|
/*saj*/
|
|
|
|
|
#ifdef TCL_MODULE
|
|
|
|
|
Tcl_MutexLock(alloc);
|
|
|
|
|
#endif
|
2000-04-27 22:03:57 +02:00
|
|
|
s = calloc(num,1);
|
2008-08-27 15:39:05 +02:00
|
|
|
/*saj*/
|
|
|
|
|
#ifdef TCL_MODULE
|
|
|
|
|
Tcl_MutexUnlock(alloc);
|
|
|
|
|
#endif
|
2000-04-27 22:03:57 +02:00
|
|
|
if (!s){
|
2005-05-21 17:56:20 +02:00
|
|
|
fprintf(stderr,"malloc: Internal Error: can't allocate %ld bytes. \n",(long)num);
|
2000-04-27 22:03:57 +02:00
|
|
|
exit(EXIT_BAD);
|
|
|
|
|
}
|
|
|
|
|
return(s);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void *
|
|
|
|
|
trealloc(void *ptr, size_t num)
|
|
|
|
|
{
|
|
|
|
|
void *s;
|
2008-08-27 15:39:05 +02:00
|
|
|
/*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);
|
2008-08-27 15:39:05 +02:00
|
|
|
else {
|
|
|
|
|
/*saj*/
|
|
|
|
|
#ifdef TCL_MODULE
|
|
|
|
|
Tcl_MutexLock(alloc);
|
|
|
|
|
#endif
|
2000-04-27 22:03:57 +02:00
|
|
|
s = realloc(ptr, num);
|
2008-08-27 15:39:05 +02:00
|
|
|
/*saj*/
|
|
|
|
|
#ifdef TCL_MODULE
|
|
|
|
|
Tcl_MutexUnlock(alloc);
|
|
|
|
|
#endif
|
|
|
|
|
}
|
2000-04-27 22:03:57 +02:00
|
|
|
if (!s) {
|
2005-05-21 17:56:20 +02:00
|
|
|
fprintf(stderr,"realloc: Internal Error: can't allocate %ld bytes.\n",(long)num);
|
2000-04-27 22:03:57 +02:00
|
|
|
exit(EXIT_BAD);
|
|
|
|
|
}
|
|
|
|
|
return(s);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* 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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
txfree(void *ptr)
|
|
|
|
|
{
|
2008-08-27 15:39:05 +02:00
|
|
|
/*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);
|
2008-08-27 15:39:05 +02:00
|
|
|
/*saj*/
|
|
|
|
|
#ifdef TCL_MODULE
|
|
|
|
|
Tcl_MutexUnlock(alloc);
|
|
|
|
|
#endif
|
2000-04-27 22:03:57 +02:00
|
|
|
}
|
|
|
|
|
|
2000-10-14 23:49:25 +02:00
|
|
|
#endif
|