kusano 7d535a
/*! @file memory.c
kusano 7d535a
 * \brief Precision-independent memory-related routines
kusano 7d535a
 *
kusano 7d535a
 * 
kusano 7d535a
 * -- SuperLU routine (version 2.0) --
kusano 7d535a
 * Univ. of California Berkeley, Xerox Palo Alto Research Center,
kusano 7d535a
 * and Lawrence Berkeley National Lab.
kusano 7d535a
 * November 15, 1997
kusano 7d535a
 * 
kusano 7d535a
 */
kusano 7d535a
/** Precision-independent memory-related routines.
kusano 7d535a
    (Shared by [sdcz]memory.c) **/
kusano 7d535a
kusano 7d535a
#include "slu_ddefs.h"
kusano 7d535a
kusano 7d535a
kusano 7d535a
#if ( DEBUGlevel>=1 )           /* Debug malloc/free. */
kusano 7d535a
int superlu_malloc_total = 0;
kusano 7d535a
kusano 7d535a
#define PAD_FACTOR  2
kusano 7d535a
#define DWORD  (sizeof(double)) /* Be sure it's no smaller than double. */
kusano 7d535a
/* size_t is usually defined as 'unsigned long' */
kusano 7d535a
kusano 7d535a
void *superlu_malloc(size_t size)
kusano 7d535a
{
kusano 7d535a
    char *buf;
kusano 7d535a
kusano 7d535a
    buf = (char *) malloc(size + DWORD);
kusano 7d535a
    if ( !buf ) {
kusano 7d535a
	printf("superlu_malloc fails: malloc_total %.0f MB, size %ld\n",
kusano 7d535a
	       superlu_malloc_total*1e-6, size);
kusano 7d535a
	ABORT("superlu_malloc: out of memory");
kusano 7d535a
    }
kusano 7d535a
kusano 7d535a
    ((int_t *) buf)[0] = size;
kusano 7d535a
#if 0
kusano 7d535a
    superlu_malloc_total += size + DWORD;
kusano 7d535a
#else
kusano 7d535a
    superlu_malloc_total += size;
kusano 7d535a
#endif
kusano 7d535a
    return (void *) (buf + DWORD);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
void superlu_free(void *addr)
kusano 7d535a
{
kusano 7d535a
    char *p = ((char *) addr) - DWORD;
kusano 7d535a
kusano 7d535a
    if ( !addr )
kusano 7d535a
	ABORT("superlu_free: tried to free NULL pointer");
kusano 7d535a
kusano 7d535a
    if ( !p )
kusano 7d535a
	ABORT("superlu_free: tried to free NULL+DWORD pointer");
kusano 7d535a
kusano 7d535a
    { 
kusano 7d535a
	int_t n = ((int_t *) p)[0];
kusano 7d535a
	
kusano 7d535a
	if ( !n )
kusano 7d535a
	    ABORT("superlu_free: tried to free a freed pointer");
kusano 7d535a
	*((int_t *) p) = 0; /* Set to zero to detect duplicate free's. */
kusano 7d535a
#if 0	
kusano 7d535a
	superlu_malloc_total -= (n + DWORD);
kusano 7d535a
#else
kusano 7d535a
	superlu_malloc_total -= n;
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
	if ( superlu_malloc_total < 0 )
kusano 7d535a
	    ABORT("superlu_malloc_total went negative!");
kusano 7d535a
	
kusano 7d535a
	/*free (addr);*/
kusano 7d535a
	free (p);
kusano 7d535a
    }
kusano 7d535a
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
#else   /* production mode */
kusano 7d535a
kusano 7d535a
void *superlu_malloc(size_t size)
kusano 7d535a
{
kusano 7d535a
    void *buf;
kusano 7d535a
    buf = (void *) malloc(size);
kusano 7d535a
    return (buf);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
void superlu_free(void *addr)
kusano 7d535a
{
kusano 7d535a
    free (addr);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*! \brief Set up pointers for integer working arrays.
kusano 7d535a
 */
kusano 7d535a
void
kusano 7d535a
SetIWork(int m, int n, int panel_size, int *iworkptr, int **segrep,
kusano 7d535a
	 int **parent, int **xplore, int **repfnz, int **panel_lsub,
kusano 7d535a
	 int **xprune, int **marker)
kusano 7d535a
{
kusano 7d535a
    *segrep = iworkptr;
kusano 7d535a
    *parent = iworkptr + m;
kusano 7d535a
    *xplore = *parent + m;
kusano 7d535a
    *repfnz = *xplore + m;
kusano 7d535a
    *panel_lsub = *repfnz + panel_size * m;
kusano 7d535a
    *xprune = *panel_lsub + panel_size * m;
kusano 7d535a
    *marker = *xprune + n;
kusano 7d535a
    ifill (*repfnz, m * panel_size, EMPTY);
kusano 7d535a
    ifill (*panel_lsub, m * panel_size, EMPTY);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
void
kusano 7d535a
copy_mem_int(int howmany, void *old, void *new)
kusano 7d535a
{
kusano 7d535a
    register int i;
kusano 7d535a
    int *iold = old;
kusano 7d535a
    int *inew = new;
kusano 7d535a
    for (i = 0; i < howmany; i++) inew[i] = iold[i];
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
void
kusano 7d535a
user_bcopy(char *src, char *dest, int bytes)
kusano 7d535a
{
kusano 7d535a
    char *s_ptr, *d_ptr;
kusano 7d535a
kusano 7d535a
    s_ptr = src + bytes - 1;
kusano 7d535a
    d_ptr = dest + bytes - 1;
kusano 7d535a
    for (; d_ptr >= dest; --s_ptr, --d_ptr ) *d_ptr = *s_ptr;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
kusano 7d535a
int *intMalloc(int n)
kusano 7d535a
{
kusano 7d535a
    int *buf;
kusano 7d535a
    buf = (int *) SUPERLU_MALLOC((size_t) n * sizeof(int));
kusano 7d535a
    if ( !buf ) {
kusano 7d535a
	ABORT("SUPERLU_MALLOC fails for buf in intMalloc()");
kusano 7d535a
    }
kusano 7d535a
    return (buf);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
int *intCalloc(int n)
kusano 7d535a
{
kusano 7d535a
    int *buf;
kusano 7d535a
    register int i;
kusano 7d535a
    buf = (int *) SUPERLU_MALLOC(n * sizeof(int));
kusano 7d535a
    if ( !buf ) {
kusano 7d535a
	ABORT("SUPERLU_MALLOC fails for buf in intCalloc()");
kusano 7d535a
    }
kusano 7d535a
    for (i = 0; i < n; ++i) buf[i] = 0;
kusano 7d535a
    return (buf);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
kusano 7d535a
#if 0
kusano 7d535a
check_expanders()
kusano 7d535a
{
kusano 7d535a
    int p;
kusano 7d535a
    printf("Check expanders:\n");
kusano 7d535a
    for (p = 0; p < NO_MEMTYPE; p++) {
kusano 7d535a
	printf("type %d, size %d, mem %d\n",
kusano 7d535a
	       p, expanders[p].size, (int)expanders[p].mem);
kusano 7d535a
    }
kusano 7d535a
kusano 7d535a
    return 0;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
StackInfo()
kusano 7d535a
{
kusano 7d535a
    printf("Stack: size %d, used %d, top1 %d, top2 %d\n",
kusano 7d535a
	   stack.size, stack.used, stack.top1, stack.top2);
kusano 7d535a
    return 0;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
kusano 7d535a
PrintStack(char *msg, GlobalLU_t *Glu)
kusano 7d535a
{
kusano 7d535a
    int i;
kusano 7d535a
    int *xlsub, *lsub, *xusub, *usub;
kusano 7d535a
kusano 7d535a
    xlsub = Glu->xlsub;
kusano 7d535a
    lsub  = Glu->lsub;
kusano 7d535a
    xusub = Glu->xusub;
kusano 7d535a
    usub  = Glu->usub;
kusano 7d535a
kusano 7d535a
    printf("%s\n", msg);
kusano 7d535a
    
kusano 7d535a
/*    printf("\nUCOL: ");
kusano 7d535a
    for (i = 0; i < xusub[ndim]; ++i)
kusano 7d535a
	printf("%f  ", ucol[i]);
kusano 7d535a
kusano 7d535a
    printf("\nLSUB: ");
kusano 7d535a
    for (i = 0; i < xlsub[ndim]; ++i)
kusano 7d535a
	printf("%d  ", lsub[i]);
kusano 7d535a
kusano 7d535a
    printf("\nUSUB: ");
kusano 7d535a
    for (i = 0; i < xusub[ndim]; ++i)
kusano 7d535a
	printf("%d  ", usub[i]);
kusano 7d535a
kusano 7d535a
    printf("\n");*/
kusano 7d535a
    return 0;
kusano 7d535a
}   
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
kusano 7d535a