• Main Page
  • Classes
  • Files
  • File List
  • File Members

ath_array.c

Go to the documentation of this file.
00001 #include "copyright.h"
00002 /*============================================================================*/
00003 /*! \file ath_array.c
00004  *  \brief Functions that construct and destruct 1D, 2D and 3D arrays of any
00005  *   type.
00006  *
00007  * PURPOSE: Functions that construct and destruct 1D, 2D and 3D arrays of any
00008  *   type. Elements in these arrays can be accessed in the standard fashion of
00009  *   array[in][im] (where in = 0 -> nr-1 and im = 0 -> nc-1) as if it
00010  *   were an array of fixed size at compile time with the statement:
00011  *   -  "Type" array[nr][nc];
00012  *
00013  *  Equally so for 3D arrys:  array[nt][nr][nc]       
00014  *
00015  *  TAG -- 8/2/2001
00016  *
00017  * EXAMPLE usage of 3D construct/destruct functions for arrays of type Real:
00018  * - array = (Real ***)calloc_3d_array(nt,nr,nc,sizeof(Real));
00019  * - free_3d_array(array);
00020  *
00021  * CONTAINS PUBLIC FUNCTIONS: 
00022  *   - calloc_1d_array() - creates 1D array
00023  *   - calloc_2d_array() - creates 2D array
00024  *   - calloc_3d_array() - creates 3D array
00025  *   - free_1d_array()   - destroys 1D array
00026  *   - free_2d_array()   - destroys 2D array
00027  *   - free_3d_array()   - destroys 3D array                                  */
00028 /*============================================================================*/
00029 
00030 #include <stdlib.h>
00031 #include "prototypes.h"
00032 
00033 /*----------------------------------------------------------------------------*/
00034 /*! \fn void* calloc_1d_array(size_t nc, size_t size)
00035  *  \brief Construct 1D array = array[nc]  */
00036 void* calloc_1d_array(size_t nc, size_t size)
00037 {
00038   void *array;
00039   
00040   if ((array = (void *)calloc(nc,size)) == NULL) {
00041     ath_error("[calloc_1d] failed to allocate memory (%d of size %d)\n",
00042               (int)nc,(int)size);
00043     return NULL;
00044   }
00045   return array;
00046 }
00047 
00048 /*----------------------------------------------------------------------------*/
00049 /*! \fn void** calloc_2d_array(size_t nr, size_t nc, size_t size)
00050  *  \brief Construct 2D array = array[nr][nc]  */
00051 void** calloc_2d_array(size_t nr, size_t nc, size_t size)
00052 {
00053   void **array;
00054   size_t i;
00055 
00056   if((array = (void **)calloc(nr,sizeof(void*))) == NULL){
00057     ath_error("[calloc_2d] failed to allocate mem for %d pointers\n",(int)nr);
00058     return NULL;
00059   }
00060 
00061   if((array[0] = (void *)calloc(nr*nc,size)) == NULL){
00062     ath_error("[calloc_2d] failed to allocate memory (%d X %d of size %d)\n",
00063               (int)nr,(int)nc,(int)size);
00064     free((void *)array);
00065     return NULL;
00066   }
00067 
00068   for(i=1; i<nr; i++){
00069     array[i] = (void *)((unsigned char *)array[0] + i*nc*size);
00070   }
00071 
00072   return array;
00073 }
00074 
00075 /*----------------------------------------------------------------------------*/
00076 /*! \fn void*** calloc_3d_array(size_t nt, size_t nr, size_t nc, size_t size)
00077  *  \brief Construct 3D array = array[nt][nr][nc]  */
00078 void*** calloc_3d_array(size_t nt, size_t nr, size_t nc, size_t size)
00079 {
00080   void ***array;
00081   size_t i,j;
00082 
00083   if((array = (void ***)calloc(nt,sizeof(void**))) == NULL){
00084     ath_error("[calloc_3d] failed to allocate memory for %d 1st-pointers\n",
00085               (int)nt);
00086     return NULL;
00087   }
00088 
00089   if((array[0] = (void **)calloc(nt*nr,sizeof(void*))) == NULL){
00090     ath_error("[calloc_3d] failed to allocate memory for %d 2nd-pointers\n",
00091               (int)(nt*nr));
00092     free((void *)array);
00093     return NULL;
00094   }
00095 
00096   for(i=1; i<nt; i++){
00097     array[i] = (void **)((unsigned char *)array[0] + i*nr*sizeof(void*));
00098   }
00099 
00100   if((array[0][0] = (void *)calloc(nt*nr*nc,size)) == NULL){
00101     ath_error("[calloc_3d] failed to alloc. memory (%d X %d X %d of size %d)\n",
00102               (int)nt,(int)nr,(int)nc,(int)size);
00103     free((void *)array[0]);
00104     free((void *)array);
00105     return NULL;
00106   }
00107 
00108   for(j=1; j<nr; j++){
00109     array[0][j] = (void **)((unsigned char *)array[0][j-1] + nc*size);
00110   }
00111 
00112   for(i=1; i<nt; i++){
00113     array[i][0] = (void **)((unsigned char *)array[i-1][0] + nr*nc*size);
00114     for(j=1; j<nr; j++){
00115       array[i][j] = (void **)((unsigned char *)array[i][j-1] + nc*size);
00116     }
00117   }
00118 
00119   return array;
00120 }
00121 
00122 /*----------------------------------------------------------------------------*/
00123 /*! \fn void free_1d_array(void *array)
00124  *  \brief Free memory used by 1D array  */
00125 void free_1d_array(void *array)
00126 {
00127   free(array);
00128 }
00129 
00130 /*----------------------------------------------------------------------------*/
00131 /*! \fn void free_2d_array(void *array)
00132  *  \brief Free memory used by 2D array  */
00133 void free_2d_array(void *array)
00134 {
00135   void **ta = (void **)array;
00136 
00137   free(ta[0]);
00138   free(array);
00139 }
00140 
00141 /*----------------------------------------------------------------------------*/
00142 /*! \fn void free_3d_array(void *array)
00143  *  \brief Free memory used by 3D array  */
00144 void free_3d_array(void *array)
00145 {
00146   void ***ta = (void ***)array;
00147 
00148   free(ta[0][0]);
00149   free(ta[0]);
00150   free(array);
00151 }

Generated on Mon Sep 27 2010 23:03:06 for Athena by  doxygen 1.7.1