/*
** memory.c
**
** Handy little functions to allocate memory for arrays etc.
**
**
** CVS: $Id: memory.c,v 1.1.1.1 2004-05-19 22:55:27 price Exp $
**
*/

#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include "memory.h"

/*
  Function to return a pointer to a generic object, such as a struct.
*/

void *papalloc (size_t size)
{
    void *newobject = malloc(size);
    if (! newobject)
    {
	fprintf (stderr, "Error allocating memory for array: %s\n",strerror(errno));
	abort();
    }
    
    return (newobject);
}
	


/*
  Subroutine to allocate memory for an array of doubles of the
  indicated size.  Array is zero-offset (pass size+1 for unit-offset
  array).
*/
double *darray (size_t size)
{
    double *thearray = malloc(size*sizeof(double));
    if (!thearray)
    {
	fprintf (stderr, "Error allocating memory for array: %s\n",strerror(errno));
	abort();
    }
    
    return (thearray);
}
			     

/*
  Subroutine to allocate memory for an array of floats of the
  indicated size.  Array is zero-offset (pass size+1 for unit-offset
  array).
*/
float *farray (size_t size)
{
    float *thearray = malloc(size*sizeof(float));
    if (!thearray)
    {
	fprintf (stderr, "Error allocating memory for array: %s\n",strerror(errno));
	abort();
    }

    return (thearray);
}


/*
  Subroutine to allocate memory for an array of integers of the
  indicated size.  Array is zero-offset (pass size+1 for unit-offset
  array).
*/
int *iarray (size_t size)
{
    int *thearray = malloc(size*sizeof(int));
    if (!thearray)
    {
	fprintf (stderr, "Error allocating memory for array: %s\n", strerror(errno));
	abort();
    }
    
    return (thearray);
}

/*
  Subroutine to allocate memory for an array of chars of the indicated
  size.  Array is zero-offset (pass size+1 for unit-offset array).
*/
char *carray (size_t size)
{
    char *thearray = malloc(size);   /* Since sizeof(char) == 1 by definition */
    if (!thearray)
    {
	fprintf (stderr, "Error allocating memory for array: %s\n", strerror(errno));
	abort();
    }
    
    return (thearray);
}

/*
  Subroutine to allocate memory for an array of short ints of the
  indicated size.  Array is zero-offset (pass size+1 for unit-offset
  array).
*/
short int *sarray (size_t size)
{
    short int *thearray = malloc(size*sizeof(short int));
    if (!thearray)
    {
	fprintf (stderr, "Error allocating memory for array: %s\n", strerror(errno));
	abort();
    }
    
    return (thearray);
}

/*
  Subroutine to allocate memory for an m x n rectangular matrix of
  floats.  Matrix is zero-offset (pass size+1 for unit-offset matrix).
*/
float **fmatrix (size_t m, size_t n)
{
    int i;
    float **thematrix = (float**)malloc(m*sizeof(float*));
    if (!thematrix)
    {
	fprintf (stderr, "Error allocating memory for matrix: %s\n", strerror(errno));
	abort();
    }

    for(i=0;i<m;i++)
    {
	thematrix[i] = malloc(n*sizeof(float));
	if (!thematrix[i])
	{
	    fprintf (stderr, "Error allocating memory for matrix: %s\n", strerror(errno));
	    abort();
	}
    }

    return thematrix;
}





/*
  Subroutine to allocate memory for a square matrix of doubles of the
  indicated size.  Matrix is zero-offset (pass size+1 for unit-offset
  matrix).
*/
double **squarematrix (size_t size)
{
    int i;
    double **thematrix = (double**)malloc(size*sizeof(double*));
    if (!thematrix)
    {
	fprintf (stderr, "Error allocating memory for matrix: %s\n", strerror(errno));
	abort();
    }
    
    for(i=0;i<size;i++)
    {
	thematrix[i] = malloc(size*sizeof(double));
	if (!thematrix[i])
	{
	    fprintf (stderr, "Error allocating memory for matrix: %s\n", strerror(errno));
	    abort();
	}
    }
  
    return thematrix;
}


/*
  Subroutine to allocate memory for an m x n rectangular matrix of
  doubles.  Matrix is zero-offset (pass size+1 for unit-offset matrix).
*/
double **rectmatrix (size_t m, size_t n)
{
    int i;
    double **thematrix = (double**)malloc(m*sizeof(double*));
    if (!thematrix)
    {
	fprintf (stderr, "Error allocating memory for matrix: %s\n", strerror(errno));
	abort();
    }

    for(i=0;i<m;i++)
    {
	thematrix[i] = malloc(n*sizeof(double));
	if (!thematrix[i])
	{
	    fprintf (stderr, "Error allocating memory for matrix: %s\n", strerror(errno));
	    abort();
	}
    }

    return thematrix;
}


/*
  Subroutine to allocate memory for an m x n rectangular matrix.
  Matrix is zero-offset (pass size+1 for unit-offset matrix).  This
  function adds a buffer of 1 pixel around the edge (i.e. it runs from
  -1 to m instead of 0 to m-1) --- provided by Nick Kaiser.
*/
double **padrectmatrix(size_t m, size_t n)
{

    int i;
    double **thematrix = (double **)malloc((m+2)*sizeof(double*)) + 1;
    if (!thematrix)
    {
	fprintf (stderr, "Error allocating memory for matrix: %s\n", strerror(errno));
	abort();
    }

    for(i=-1;i<=m;i++)
    {
	thematrix[i] = malloc((n+2)*sizeof(double)) + 1;
	if (!thematrix[i])
	{
	    fprintf (stderr, "Error allocating memory for matrix: %s\n", strerror(errno));
	    abort();
	}
    }

    return thematrix;
}

