From e2908767e2ceb26f37425e5a41ed23257313ebfa Mon Sep 17 00:00:00 2001 From: dwarning Date: Tue, 6 Jun 2023 15:29:08 +0200 Subject: [PATCH] use ngspice memory allocation scheme --- src/maths/sparse/spAllocate.c | 58 +++++++++++++++++------------------ src/maths/sparse/spBuild.c | 26 ++++++++-------- src/maths/sparse/spDefs.h | 39 ++++++++++++++--------- src/maths/sparse/spFactor.c | 14 ++++----- src/maths/sparse/spOutput.c | 8 ++--- 5 files changed, 77 insertions(+), 68 deletions(-) diff --git a/src/maths/sparse/spAllocate.c b/src/maths/sparse/spAllocate.c index e525148db..d0d631c7e 100644 --- a/src/maths/sparse/spAllocate.c +++ b/src/maths/sparse/spAllocate.c @@ -150,7 +150,7 @@ int AllocatedSize; AllocatedSize = MAX( Size, MINIMUM_ALLOCATED_SIZE ); SizePlusOne = (unsigned)(AllocatedSize + 1); - if ((Matrix = ALLOC(struct MatrixFrame, 1)) == NULL) + if ((Matrix = SP_MALLOC(struct MatrixFrame, 1)) == NULL) { *pError = spNO_MEMORY; return NULL; } @@ -211,26 +211,26 @@ int AllocatedSize; #endif /* Allocate space in memory for Diag pointer vector. */ - CALLOC( Matrix->Diag, ElementPtr, SizePlusOne); + SP_CALLOC( Matrix->Diag, ElementPtr, SizePlusOne); if (Matrix->Diag == NULL) goto MemoryError; /* Allocate space in memory for FirstInCol pointer vector. */ - CALLOC( Matrix->FirstInCol, ElementPtr, SizePlusOne); + SP_CALLOC( Matrix->FirstInCol, ElementPtr, SizePlusOne); if (Matrix->FirstInCol == NULL) goto MemoryError; /* Allocate space in memory for FirstInRow pointer vector. */ - CALLOC( Matrix->FirstInRow, ElementPtr, SizePlusOne); + SP_CALLOC( Matrix->FirstInRow, ElementPtr, SizePlusOne); if (Matrix->FirstInRow == NULL) goto MemoryError; /* Allocate space in memory for IntToExtColMap vector. */ - if (( Matrix->IntToExtColMap = ALLOC(int, SizePlusOne)) == NULL) + if (( Matrix->IntToExtColMap = SP_MALLOC(int, SizePlusOne)) == NULL) goto MemoryError; /* Allocate space in memory for IntToExtRowMap vector. */ - if (( Matrix->IntToExtRowMap = ALLOC(int, SizePlusOne)) == NULL) + if (( Matrix->IntToExtRowMap = SP_MALLOC(int, SizePlusOne)) == NULL) goto MemoryError; /* Initialize MapIntToExt vectors. */ @@ -241,11 +241,11 @@ int AllocatedSize; #if TRANSLATE /* Allocate space in memory for ExtToIntColMap vector. */ - if (( Matrix->ExtToIntColMap = ALLOC(int, SizePlusOne)) == NULL) + if (( Matrix->ExtToIntColMap = SP_MALLOC(int, SizePlusOne)) == NULL) goto MemoryError; /* Allocate space in memory for ExtToIntRowMap vector. */ - if (( Matrix->ExtToIntRowMap = ALLOC(int, SizePlusOne)) == NULL) + if (( Matrix->ExtToIntRowMap = SP_MALLOC(int, SizePlusOne)) == NULL) goto MemoryError; /* Initialize MapExtToInt vectors. */ @@ -314,7 +314,7 @@ ElementPtr pElement; /* Allocate block of MatrixElements if necessary. */ if (Matrix->ElementsRemaining == 0) - { pElement = ALLOC(struct MatrixElement, ELEMENTS_PER_ALLOCATION); + { pElement = SP_MALLOC(struct MatrixElement, ELEMENTS_PER_ALLOCATION); RecordAllocation( Matrix, (void *)pElement ); if (Matrix->Error == spNO_MEMORY) return NULL; Matrix->ElementsRemaining = ELEMENTS_PER_ALLOCATION; @@ -375,21 +375,21 @@ ElementPtr pElement; /* Begin `InitializeElementBlocks'. */ /* Allocate block of MatrixElements for elements. */ - pElement = ALLOC(struct MatrixElement, InitialNumberOfElements); + pElement = SP_MALLOC(struct MatrixElement, InitialNumberOfElements); RecordAllocation( Matrix, (void *)pElement ); if (Matrix->Error == spNO_MEMORY) return; Matrix->ElementsRemaining = InitialNumberOfElements; Matrix->NextAvailElement = pElement; /* Allocate block of MatrixElements for fill-ins. */ - pElement = ALLOC(struct MatrixElement, NumberOfFillinsExpected); + pElement = SP_MALLOC(struct MatrixElement, NumberOfFillinsExpected); RecordAllocation( Matrix, (void *)pElement ); if (Matrix->Error == spNO_MEMORY) return; Matrix->FillinsRemaining = NumberOfFillinsExpected; Matrix->NextAvailFillin = pElement; /* Allocate a fill-in list structure. */ - Matrix->FirstFillinListNode = ALLOC(struct FillinListNodeStruct,1); + Matrix->FirstFillinListNode = SP_MALLOC(struct FillinListNodeStruct,1); RecordAllocation( Matrix, (void *)Matrix->FirstFillinListNode ); if (Matrix->Error == spNO_MEMORY) return; Matrix->LastFillinListNode = Matrix->FirstFillinListNode; @@ -457,14 +457,14 @@ ElementPtr pFillins; else { /* Allocate block of fill-ins. */ - pFillins = ALLOC(struct MatrixElement, ELEMENTS_PER_ALLOCATION); + pFillins = SP_MALLOC(struct MatrixElement, ELEMENTS_PER_ALLOCATION); RecordAllocation( Matrix, (void *)pFillins ); if (Matrix->Error == spNO_MEMORY) return NULL; Matrix->FillinsRemaining = ELEMENTS_PER_ALLOCATION; Matrix->NextAvailFillin = pFillins; /* Allocate a fill-in list structure. */ - pListNode->Next = ALLOC(struct FillinListNodeStruct,1); + pListNode->Next = SP_MALLOC(struct FillinListNodeStruct,1); RecordAllocation( Matrix, (void *)pListNode->Next ); if (Matrix->Error == spNO_MEMORY) return NULL; Matrix->LastFillinListNode = pListNode = pListNode->Next; @@ -526,7 +526,7 @@ RecordAllocation( if (Matrix->RecordsRemaining == 0) { AllocateBlockOfAllocationList( Matrix ); if (Matrix->Error == spNO_MEMORY) - { FREE(AllocatedPtr); + { SP_FREE(AllocatedPtr); return; } } @@ -571,7 +571,7 @@ register AllocationListPtr ListPtr; /* Begin `AllocateBlockOfAllocationList'. */ /* Allocate block of records for allocation list. */ - ListPtr = ALLOC(struct AllocationRecord, (ELEMENTS_PER_ALLOCATION+1)); + ListPtr = SP_MALLOC(struct AllocationRecord, (ELEMENTS_PER_ALLOCATION+1)); if (ListPtr == NULL) { Matrix->Error = spNO_MEMORY; return; @@ -629,19 +629,19 @@ register AllocationListPtr ListPtr, NextListPtr; ASSERT_IS_SPARSE( Matrix ); /* Deallocate the vectors that are located in the matrix frame. */ - FREE( Matrix->IntToExtColMap ); - FREE( Matrix->IntToExtRowMap ); - FREE( Matrix->ExtToIntColMap ); - FREE( Matrix->ExtToIntRowMap ); - FREE( Matrix->Diag ); - FREE( Matrix->FirstInRow ); - FREE( Matrix->FirstInCol ); - FREE( Matrix->MarkowitzRow ); - FREE( Matrix->MarkowitzCol ); - FREE( Matrix->MarkowitzProd ); - FREE( Matrix->DoCmplxDirect ); - FREE( Matrix->DoRealDirect ); - FREE( Matrix->Intermediate ); + SP_FREE( Matrix->IntToExtColMap ); + SP_FREE( Matrix->IntToExtRowMap ); + SP_FREE( Matrix->ExtToIntColMap ); + SP_FREE( Matrix->ExtToIntRowMap ); + SP_FREE( Matrix->Diag ); + SP_FREE( Matrix->FirstInRow ); + SP_FREE( Matrix->FirstInCol ); + SP_FREE( Matrix->MarkowitzRow ); + SP_FREE( Matrix->MarkowitzCol ); + SP_FREE( Matrix->MarkowitzProd ); + SP_FREE( Matrix->DoCmplxDirect ); + SP_FREE( Matrix->DoRealDirect ); + SP_FREE( Matrix->Intermediate ); /* Sequentially step through the list of allocated pointers freeing pointers * along the way. */ diff --git a/src/maths/sparse/spBuild.c b/src/maths/sparse/spBuild.c index 415626417..e2bf9a093 100644 --- a/src/maths/sparse/spBuild.c +++ b/src/maths/sparse/spBuild.c @@ -1008,23 +1008,23 @@ register int I, OldAllocatedSize = Matrix->AllocatedSize; NewSize = MAX( NewSize, (int)(EXPANSION_FACTOR * OldAllocatedSize) ); Matrix->AllocatedSize = NewSize; - if (( REALLOC(Matrix->IntToExtColMap, int, NewSize+1)) == NULL) + if (( SP_REALLOC(Matrix->IntToExtColMap, int, NewSize+1)) == NULL) { Matrix->Error = spNO_MEMORY; return; } - if (( REALLOC(Matrix->IntToExtRowMap, int, NewSize+1)) == NULL) + if (( SP_REALLOC(Matrix->IntToExtRowMap, int, NewSize+1)) == NULL) { Matrix->Error = spNO_MEMORY; return; } - if (( REALLOC(Matrix->Diag, ElementPtr, NewSize+1)) == NULL) + if (( SP_REALLOC(Matrix->Diag, ElementPtr, NewSize+1)) == NULL) { Matrix->Error = spNO_MEMORY; return; } - if (( REALLOC(Matrix->FirstInCol, ElementPtr, NewSize+1)) == NULL) + if (( SP_REALLOC(Matrix->FirstInCol, ElementPtr, NewSize+1)) == NULL) { Matrix->Error = spNO_MEMORY; return; } - if (( REALLOC(Matrix->FirstInRow, ElementPtr, NewSize+1)) == NULL) + if (( SP_REALLOC(Matrix->FirstInRow, ElementPtr, NewSize+1)) == NULL) { Matrix->Error = spNO_MEMORY; return; } @@ -1033,12 +1033,12 @@ register int I, OldAllocatedSize = Matrix->AllocatedSize; * Destroy the Markowitz and Intermediate vectors, they will be recreated * in spOrderAndFactor(). */ - FREE( Matrix->MarkowitzRow ); - FREE( Matrix->MarkowitzCol ); - FREE( Matrix->MarkowitzProd ); - FREE( Matrix->DoRealDirect ); - FREE( Matrix->DoCmplxDirect ); - FREE( Matrix->Intermediate ); + SP_FREE( Matrix->MarkowitzRow ); + SP_FREE( Matrix->MarkowitzCol ); + SP_FREE( Matrix->MarkowitzProd ); + SP_FREE( Matrix->DoRealDirect ); + SP_FREE( Matrix->DoCmplxDirect ); + SP_FREE( Matrix->Intermediate ); Matrix->InternalVectorsAllocated = NO; /* Initialize the new portion of the vectors. */ @@ -1097,11 +1097,11 @@ register int I, OldAllocatedSize = Matrix->AllocatedExtSize; NewSize = MAX( NewSize, (int)(EXPANSION_FACTOR * OldAllocatedSize) ); Matrix->AllocatedExtSize = NewSize; - if (( REALLOC(Matrix->ExtToIntRowMap, int, NewSize+1)) == NULL) + if (( SP_REALLOC(Matrix->ExtToIntRowMap, int, NewSize+1)) == NULL) { Matrix->Error = spNO_MEMORY; return; } - if (( REALLOC(Matrix->ExtToIntColMap, int, NewSize+1)) == NULL) + if (( SP_REALLOC(Matrix->ExtToIntColMap, int, NewSize+1)) == NULL) { Matrix->Error = spNO_MEMORY; return; } diff --git a/src/maths/sparse/spDefs.h b/src/maths/sparse/spDefs.h index 8bc762ad1..9b7a2b5a7 100644 --- a/src/maths/sparse/spDefs.h +++ b/src/maths/sparse/spDefs.h @@ -453,26 +453,35 @@ /* * MEMORY ALLOCATION */ -#include -spcEXTERN void *malloc(size_t size); -spcEXTERN void *calloc(size_t nmemb, size_t size); -spcEXTERN void *realloc(void *ptr, size_t size); -spcEXTERN void free(void *ptr); +//#include +#include +spcEXTERN void *tmalloc(size_t); +spcEXTERN void *trealloc(const void *, size_t); +spcEXTERN void txfree(const void *); spcEXTERN void abort(void); -#define ALLOC(type,number) ((type *)malloc((unsigned)(sizeof(type)*(number)))) -#define REALLOC(ptr,type,number) \ - ptr = (type *)realloc((char *)ptr,(unsigned)(sizeof(type)*(number))) -#define FREE(ptr) { if ((ptr) != NULL) free((char *)(ptr)); (ptr) = NULL; } +#define SP_MALLOC(type,number) (type *) tmalloc((size_t)(number) * sizeof(type)) +#define SP_REALLOC(ptr,type,number) \ + ptr = (type *) trealloc(ptr, (size_t)(number) * sizeof(type)) +#define SP_FREE(ptr) { if ((ptr) != NULL) txfree(ptr); (ptr) = NULL; } +#include "ngspice/config.h" -/* Calloc that properly handles allocating a cleared vector. */ -#define CALLOC(ptr,type,number) \ -{ int i; ptr = ALLOC(type, number); \ - if (ptr != (type *)NULL) \ - for(i=(number)-1;i>=0; i--) ptr[i] = (type) 0; \ +/* A new calloc */ +#ifndef HAVE_LIBGC +#define SP_CALLOC(ptr,type,number) \ +{ ptr = (type *) calloc((size_t)(number), sizeof(type)); \ } - +#else /* HAVE_LIBCG */ +#define SP_CALLOC(ptr,type,number) \ +{ ptr = (type *) tmalloc((size_t)(number) * sizeof(type)); \ +} +#include +#define tmalloc(m) GC_malloc(m) +#define trealloc(m, n) GC_realloc((m), (n)) +#define tfree(m) +#define txfree(m) +#endif diff --git a/src/maths/sparse/spFactor.c b/src/maths/sparse/spFactor.c index a1f67553e..45be9d52b 100644 --- a/src/maths/sparse/spFactor.c +++ b/src/maths/sparse/spFactor.c @@ -734,28 +734,28 @@ int Size; Size= Matrix->Size; if (Matrix->MarkowitzRow == NULL) - { if (( Matrix->MarkowitzRow = ALLOC(int, Size+1)) == NULL) + { if (( Matrix->MarkowitzRow = SP_MALLOC(int, Size+1)) == NULL) Matrix->Error = spNO_MEMORY; } if (Matrix->MarkowitzCol == NULL) - { if (( Matrix->MarkowitzCol = ALLOC(int, Size+1)) == NULL) + { if (( Matrix->MarkowitzCol = SP_MALLOC(int, Size+1)) == NULL) Matrix->Error = spNO_MEMORY; } if (Matrix->MarkowitzProd == NULL) - { if (( Matrix->MarkowitzProd = ALLOC(long, Size+2)) == NULL) + { if (( Matrix->MarkowitzProd = SP_MALLOC(long, Size+2)) == NULL) Matrix->Error = spNO_MEMORY; } /* Create DoDirect vectors for use in spFactor(). */ #if REAL if (Matrix->DoRealDirect == NULL) - { if (( Matrix->DoRealDirect = ALLOC(BOOLEAN, Size+1)) == NULL) + { if (( Matrix->DoRealDirect = SP_MALLOC(BOOLEAN, Size+1)) == NULL) Matrix->Error = spNO_MEMORY; } #endif #if spCOMPLEX if (Matrix->DoCmplxDirect == NULL) - { if (( Matrix->DoCmplxDirect = ALLOC(BOOLEAN, Size+1)) == NULL) + { if (( Matrix->DoCmplxDirect = SP_MALLOC(BOOLEAN, Size+1)) == NULL) Matrix->Error = spNO_MEMORY; } #endif @@ -763,12 +763,12 @@ int Size; /* Create Intermediate vectors for use in MatrixSolve. */ #if spCOMPLEX if (Matrix->Intermediate == NULL) - { if ((Matrix->Intermediate = ALLOC(RealNumber,2*(Size+1))) == NULL) + { if ((Matrix->Intermediate = SP_MALLOC(RealNumber,2*(Size+1))) == NULL) Matrix->Error = spNO_MEMORY; } #else if (Matrix->Intermediate == NULL) - { if ((Matrix->Intermediate = ALLOC(RealNumber, Size+1)) == NULL) + { if ((Matrix->Intermediate = SP_MALLOC(RealNumber, Size+1)) == NULL) Matrix->Error = spNO_MEMORY; } #endif diff --git a/src/maths/sparse/spOutput.c b/src/maths/sparse/spOutput.c index ca3a80490..88cee16a4 100644 --- a/src/maths/sparse/spOutput.c +++ b/src/maths/sparse/spOutput.c @@ -153,8 +153,8 @@ int *PrintOrdToIntRowMap, *PrintOrdToIntColMap; #else Top = Matrix->AllocatedSize; #endif - CALLOC( PrintOrdToIntRowMap, int, Top + 1 ); - CALLOC( PrintOrdToIntColMap, int, Top + 1 ); + SP_CALLOC( PrintOrdToIntRowMap, int, Top + 1 ); + SP_CALLOC( PrintOrdToIntColMap, int, Top + 1 ); if ( PrintOrdToIntRowMap == NULL OR PrintOrdToIntColMap == NULL) { Matrix->Error = spNO_MEMORY; return; @@ -341,8 +341,8 @@ int *PrintOrdToIntRowMap, *PrintOrdToIntColMap; putchar('\n'); (void)fflush(stdout); - FREE(PrintOrdToIntColMap); - FREE(PrintOrdToIntRowMap); + SP_FREE(PrintOrdToIntColMap); + SP_FREE(PrintOrdToIntRowMap); return; }