Utah Raster Toolkit  9999-git
URT Development version (post-3.1b)
Functions
mallocNd.c File Reference
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
Include dependency graph for mallocNd.c:

Go to the source code of this file.

Functions

char * BuildIndirectionTable ()
 
char * mallocNd (unsigned int nDim, Dims, unsigned int sizeofelt)
 
char * BuildIndirectionTable (unsigned nDim, Dims, char ***nextFreeTable, char **nextFreeArray, unsigned sizeofelt)
 
char * malloc_2d (int n, int m, int sizeofelt)
 

Function Documentation

char* BuildIndirectionTable ( )
char* BuildIndirectionTable ( unsigned  nDim,
Dims  ,
char ***  nextFreeTable,
char **  nextFreeArray,
unsigned  sizeofelt 
)

Definition at line 184 of file mallocNd.c.

190 {
191  int i;
192  char *ReturnValue, **table;
193 
194  if (nDim == 1) { /* Base case, indirection table unneeded */
195  ReturnValue = *nextFreeArray;
196  *nextFreeArray += sizeofelt * Dims[0];
197  } else {
198  /* Allocate the 1'st level indirection table and fill it in
199  ** calling ourselves recursively for the others.
200  */
201  table = *nextFreeTable;
202  *nextFreeTable += Dims[0];
203  for(i=0; i<Dims[0]; i++)
204  {
205  table[i] = BuildIndirectionTable( nDim-1, Dims+1, nextFreeTable,
206  nextFreeArray, sizeofelt );
207  }
208  ReturnValue = (char *) table;
209  }
210  return ReturnValue;
211 }
char * BuildIndirectionTable()
int i
Definition: rletorla.c:82
char* malloc_2d ( int  n,
int  m,
int  sizeofelt 
)

Definition at line 218 of file mallocNd.c.

220 {
221  unsigned Dims[2];
222 
223  Dims[0] = n;
224  Dims[1] = m;
225  return mallocNd(2,Dims,sizeofelt);
226 }
char * mallocNd(unsigned int nDim, Dims, unsigned int sizeofelt)
Definition: mallocNd.c:125
char* mallocNd ( unsigned int  nDim,
Dims  ,
unsigned int  sizeofelt 
)

Definition at line 125 of file mallocNd.c.

129 {
130  int i;
131  char *ptr, *tableBase, *arrayBase, *nextFreeArray, *nextFreeTable;
132  unsigned int arrayBytesNeeded, tableBytesNeeded, dimProduct, slop;
133 
134  /* Count up the space needed for the indirection tables.
135  ** It's sizeof(pointer) * Dim[0] * ( 1 + Dim[1] * ( 1 + Dim[2] ... ))
136  */
137  tableBytesNeeded = 0;
138  dimProduct = 1;
139  for(i=0; i<nDim-1; i++)
140  {
141  dimProduct *= Dims[i];
142  tableBytesNeeded += dimProduct*sizeof(char *);
143  }
144 
145  /* Add in extra space to force alignment of the array */
146  slop = (tableBytesNeeded % sizeofelt);
147  if (slop != 0)
148  slop = sizeofelt - slop;
149 
150  /* Add in the space for the array itself */
151  dimProduct *= Dims[i];
152  arrayBytesNeeded = dimProduct*sizeofelt;
153 
154  /* Get the memory for the whole thing */
155  ptr = malloc( tableBytesNeeded + slop + arrayBytesNeeded );
156  if (ptr == NULL) return (char *) NULL;
157 
158  /* Set up the base address of the actual array and the indirection table */
159  tableBase = ptr;
160  arrayBase = ptr + slop + tableBytesNeeded;
161 
162  /* Set up the next free pointers for the table space and the array space */
163  nextFreeTable = tableBase;
164  nextFreeArray = arrayBase;
165 
166  /* Build the indirection tables recursively */
167  (void) BuildIndirectionTable( nDim, Dims, (char ***)&nextFreeTable,
168  &nextFreeArray, sizeofelt );
169 
170  /* Do a sanity check to be sure we haven't run off the end of the allocated
171  ** memory. Not strictly needed but here for robustness and debugging.
172  */
173  if (nextFreeTable != arrayBase - slop ||
174  nextFreeArray != ptr + tableBytesNeeded + slop + arrayBytesNeeded)
175  {
176  fprintf( stderr, "Error in mallocNd! Memory overwrite." );
177  abort();
178  }
179  return(char *) tableBase;
180 }
char * BuildIndirectionTable()
int * ptr
Definition: scanargs.c:57
void * malloc()
int i
Definition: rletorla.c:82