VPR-7.0
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros
util.h File Reference
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "TIO_PrintHandlerExtern.h"
+ Include dependency graph for util.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  s_linked_vptr
 
struct  s_linked_int
 
struct  s_ivec
 
struct  s_chunk
 

Macros

#define INP
 
#define OUTP
 
#define INOUTP
 
#define BUFSIZE   4096 /* Maximum line length for various parsing proc. */
 
#define nint(a)   ((int) floor (a + 0.5))
 
#define ERRTAG   "ERROR:\t"
 
#define WARNTAG   "WARNING:\t"
 

Typedefs

typedef struct s_linked_vptr t_linked_vptr
 
typedef struct s_linked_int t_linked_int
 
typedef struct s_ivec t_ivec
 
typedef struct s_chunk t_chunk
 
typedef unsigned char(* messagelogger )(TIO_MessageMode_t messageMode, char *pszMessage,...)
 

Enumerations

enum  boolean { FALSE, TRUE }
 

Functions

int limit_value (int cur, int max, const char *name)
 
void * my_malloc (size_t size)
 
void * my_calloc (size_t nelem, size_t size)
 
void * my_realloc (void *ptr, size_t size)
 
void * my_chunk_malloc (size_t size, t_chunk *chunk_info)
 
void free_chunk_memory (t_chunk *chunk_info)
 
void free_ivec_vector (struct s_ivec *ivec_vector, int nrmin, int nrmax)
 
void free_ivec_matrix (struct s_ivec **ivec_matrix, int nrmin, int nrmax, int ncmin, int ncmax)
 
void free_ivec_matrix3 (struct s_ivec ***ivec_matrix3, int nrmin, int nrmax, int ncmin, int ncmax, int ndmin, int ndmax)
 
void ** alloc_matrix (int nrmin, int nrmax, int ncmin, int ncmax, size_t elsize)
 
void *** alloc_matrix3 (int nrmin, int nrmax, int ncmin, int ncmax, int ndmin, int ndmax, size_t elsize)
 
void **** alloc_matrix4 (int nrmin, int nrmax, int ncmin, int ncmax, int ndmin, int ndmax, int nemin, int nemax, size_t elsize)
 
void free_matrix (void *vptr, int nrmin, int nrmax, int ncmin, size_t elsize)
 
void free_matrix3 (void *vptr, int nrmin, int nrmax, int ncmin, int ncmax, int ndmin, size_t elsize)
 
void free_matrix4 (void *vptr, int nrmin, int nrmax, int ncmin, int ncmax, int ndmin, int ndmax, int nemin, size_t elsize)
 
void print_int_matrix3 (int ***vptr, int nrmin, int nrmax, int ncmin, int ncmax, int ndmin, int ndmax, char *file)
 
struct s_linked_vptrinsert_in_vptr_list (struct s_linked_vptr *head, void *vptr_to_add)
 
struct s_linked_vptrdelete_in_vptr_list (struct s_linked_vptr *head)
 
t_linked_intinsert_in_int_list (t_linked_int *head, int data, t_linked_int **free_list_head_ptr)
 
void free_int_list (t_linked_int **int_list_head_ptr)
 
void alloc_ivector_and_copy_int_list (t_linked_int **list_head_ptr, int num_items, struct s_ivec *ivec, t_linked_int **free_list_head_ptr)
 
int my_atoi (const char *str)
 
char * my_strdup (const char *str)
 
char * my_strncpy (char *dest, const char *src, size_t size)
 
char * my_strtok (char *ptr, const char *tokens, FILE *fp, char *buf)
 
FILE * my_fopen (const char *fname, const char *flag, int prompt)
 
char * my_fgets (char *buf, int max_size, FILE *fp)
 
boolean file_exists (const char *filename)
 
void my_srandom (int seed)
 
int my_irand (int imax)
 
float my_frand (void)
 
int ipow (int base, int exp)
 

Variables

int file_line_number
 
char * out_file_prefix
 
messagelogger vpr_printf
 

Macro Definition Documentation

#define BUFSIZE   4096 /* Maximum line length for various parsing proc. */

Definition at line 23 of file util.h.

#define ERRTAG   "ERROR:\t"

Definition at line 26 of file util.h.

#define INOUTP

Definition at line 21 of file util.h.

#define INP

Definition at line 19 of file util.h.

#define nint (   a)    ((int) floor (a + 0.5))

Definition at line 24 of file util.h.

#define OUTP

Definition at line 20 of file util.h.

#define WARNTAG   "WARNING:\t"

Definition at line 27 of file util.h.

Typedef Documentation

typedef unsigned char(* messagelogger)(TIO_MessageMode_t messageMode, char *pszMessage,...)

Definition at line 133 of file util.h.

typedef struct s_chunk t_chunk
typedef struct s_ivec t_ivec
typedef struct s_linked_int t_linked_int
typedef struct s_linked_vptr t_linked_vptr

Enumeration Type Documentation

enum boolean
Enumerator
FALSE 
TRUE 

Definition at line 11 of file util.h.

11  {
12  FALSE, TRUE
13 } boolean;
boolean
Definition: util.h:11
Definition: util.h:12
Definition: util.h:12

Function Documentation

void alloc_ivector_and_copy_int_list ( t_linked_int **  list_head_ptr,
int  num_items,
struct s_ivec ivec,
t_linked_int **  free_list_head_ptr 
)

Definition at line 359 of file util.c.

360  {
361 
362  /* Allocates an integer vector with num_items elements and copies the *
363  * integers from the list pointed to by list_head (of which there must be *
364  * num_items) over to it. The int_list is then put on the free list, and *
365  * the list_head_ptr is set to NULL. */
366 
367  t_linked_int *linked_int, *list_head;
368  int i, *list;
369 
370  list_head = *list_head_ptr;
371 
372  if (num_items == 0) { /* Empty list. */
373  ivec->nelem = 0;
374  ivec->list = NULL;
375 
376  if (list_head != NULL ) {
377  vpr_printf(TIO_MESSAGE_ERROR,
378  "alloc_ivector_and_copy_int_list: Copied %d elements, "
379  "but list at %p contains more.\n", num_items,
380  (void *) list_head);
381  exit(1);
382  }
383  return;
384  }
385 
386  ivec->nelem = num_items;
387  list = (int *) my_malloc(num_items * sizeof(int));
388  ivec->list = list;
389  linked_int = list_head;
390 
391  for (i = 0; i < num_items - 1; i++) {
392  list[i] = linked_int->data;
393  linked_int = linked_int->next;
394  }
395 
396  list[num_items - 1] = linked_int->data;
397 
398  if (linked_int->next != NULL ) {
399  vpr_printf(TIO_MESSAGE_ERROR,
400  "Error in alloc_ivector_and_copy_int_list:\n Copied %d elements, "
401  "but list at %p contains more.\n", num_items,
402  (void *) list_head);
403  exit(1);
404  }
405 
406  linked_int->next = *free_list_head_ptr;
407  *free_list_head_ptr = list_head;
408  *list_head_ptr = NULL;
409 }
void * my_malloc(size_t size)
Definition: util.c:147
int data
Definition: util.h:40
int * list
Definition: util.h:49
int nelem
Definition: util.h:48
messagelogger vpr_printf
Definition: util.c:17
struct s_linked_int * next
Definition: util.h:41

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void** alloc_matrix ( int  nrmin,
int  nrmax,
int  ncmin,
int  ncmax,
size_t  elsize 
)

Definition at line 551 of file util.c.

551  {
552 
553  /* allocates an generic matrix with nrmax-nrmin + 1 rows and ncmax - *
554  * ncmin + 1 columns, with each element of size elsize. i.e. *
555  * returns a pointer to a storage block [nrmin..nrmax][ncmin..ncmax].*
556  * Simply cast the returned array pointer to the proper type. */
557 
558  int i;
559  char **cptr;
560 
561  cptr = (char **) my_malloc((nrmax - nrmin + 1) * sizeof(char *));
562  cptr -= nrmin;
563  for (i = nrmin; i <= nrmax; i++) {
564  cptr[i] = (char *) my_malloc((ncmax - ncmin + 1) * elsize);
565  cptr[i] -= ncmin * elsize / sizeof(char); /* sizeof(char) = 1 */
566  }
567  return ((void **) cptr);
568 }
void * my_malloc(size_t size)
Definition: util.c:147

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void*** alloc_matrix3 ( int  nrmin,
int  nrmax,
int  ncmin,
int  ncmax,
int  ndmin,
int  ndmax,
size_t  elsize 
)

Definition at line 585 of file util.c.

586  {
587 
588  /* allocates a 3D generic matrix with nrmax-nrmin + 1 rows, ncmax - *
589  * ncmin + 1 columns, and a depth of ndmax-ndmin + 1, with each *
590  * element of size elsize. i.e. returns a pointer to a storage block *
591  * [nrmin..nrmax][ncmin..ncmax][ndmin..ndmax]. Simply cast the *
592  * returned array pointer to the proper type. */
593 
594  int i, j;
595  char ***cptr;
596 
597  cptr = (char ***) my_malloc((nrmax - nrmin + 1) * sizeof(char **));
598  cptr -= nrmin;
599  for (i = nrmin; i <= nrmax; i++) {
600  cptr[i] = (char **) my_malloc((ncmax - ncmin + 1) * sizeof(char *));
601  cptr[i] -= ncmin;
602  for (j = ncmin; j <= ncmax; j++) {
603  cptr[i][j] = (char *) my_malloc((ndmax - ndmin + 1) * elsize);
604  cptr[i][j] -= ndmin * elsize / sizeof(char); /* sizeof(char) = 1) */
605  }
606  }
607  return ((void ***) cptr);
608 }
void * my_malloc(size_t size)
Definition: util.c:147

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void**** alloc_matrix4 ( int  nrmin,
int  nrmax,
int  ncmin,
int  ncmax,
int  ndmin,
int  ndmax,
int  nemin,
int  nemax,
size_t  elsize 
)

Definition at line 611 of file util.c.

612  {
613 
614  /* allocates a 3D generic matrix with nrmax-nrmin + 1 rows, ncmax - *
615  * ncmin + 1 columns, and a depth of ndmax-ndmin + 1, with each *
616  * element of size elsize. i.e. returns a pointer to a storage block *
617  * [nrmin..nrmax][ncmin..ncmax][ndmin..ndmax]. Simply cast the *
618  * returned array pointer to the proper type. */
619 
620  int i, j, k;
621  char ****cptr;
622 
623  cptr = (char ****) my_malloc((nrmax - nrmin + 1) * sizeof(char ***));
624  cptr -= nrmin;
625  for (i = nrmin; i <= nrmax; i++) {
626  cptr[i] = (char ***) my_malloc((ncmax - ncmin + 1) * sizeof(char **));
627  cptr[i] -= ncmin;
628  for (j = ncmin; j <= ncmax; j++) {
629  cptr[i][j] = (char **) my_malloc(
630  (ndmax - ndmin + 1) * sizeof(char *));
631  cptr[i][j] -= ndmin;
632  for (k = ndmin; k <= ndmax; k++) {
633  cptr[i][j][k] = (char *) my_malloc(
634  (nemax - nemin + 1) * elsize);
635  cptr[i][j][k] -= nemin * elsize / sizeof(char); /* sizeof(char) = 1) */
636  }
637  }
638  }
639  return ((void ****) cptr);
640 }
void * my_malloc(size_t size)
Definition: util.c:147

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

struct s_linked_vptr* delete_in_vptr_list ( struct s_linked_vptr head)

Definition at line 308 of file util.c.

308  {
309  struct s_linked_vptr *linked_vptr;
310 
311  if (head == NULL )
312  return NULL ;
313  linked_vptr = head->next;
314  free(head);
315  return linked_vptr; /* New head of the list */
316 }
struct s_linked_vptr * next
Definition: util.h:36
boolean file_exists ( const char *  filename)

Definition at line 760 of file util.c.

760  {
761  FILE * file;
762 
763  if (filename == NULL ) {
764  return FALSE;
765  }
766 
767  file = fopen(filename, "r");
768  if (file) {
769  fclose(file);
770  return TRUE;
771  }
772  return FALSE;
773 }
Definition: util.h:12
Definition: util.h:12

+ Here is the caller graph for this function:

void free_chunk_memory ( t_chunk chunk_info)

Definition at line 270 of file util.c.

270  {
271 
272  /* Frees the memory allocated by a sequence of calls to my_chunk_malloc. */
273 
274  struct s_linked_vptr *curr_ptr, *prev_ptr;
275 
276  curr_ptr = chunk_info->chunk_ptr_head;
277 
278  while (curr_ptr != NULL ) {
279  free(curr_ptr->data_vptr); /* Free memory "chunk". */
280  prev_ptr = curr_ptr;
281  curr_ptr = curr_ptr->next;
282  free(prev_ptr); /* Free memory used to track "chunk". */
283  }
284  chunk_info->chunk_ptr_head = NULL;
285  chunk_info->mem_avail = 0;
286  chunk_info->next_mem_loc_ptr = NULL;
287 }
struct s_linked_vptr * chunk_ptr_head
Definition: util.h:58
struct s_linked_vptr * next
Definition: util.h:36
void * data_vptr
Definition: util.h:35
char * next_mem_loc_ptr
Definition: util.h:63
int mem_avail
Definition: util.h:62

+ Here is the caller graph for this function:

void free_int_list ( t_linked_int **  int_list_head_ptr)

Definition at line 341 of file util.c.

341  {
342 
343  /* This routine truly frees (calls free) all the integer list elements *
344  * on the linked list pointed to by *head, and sets head = NULL. */
345 
346  t_linked_int *linked_int, *next_linked_int;
347 
348  linked_int = *int_list_head_ptr;
349 
350  while (linked_int != NULL ) {
351  next_linked_int = linked_int->next;
352  free(linked_int);
353  linked_int = next_linked_int;
354  }
355 
356  *int_list_head_ptr = NULL;
357 }
struct s_linked_int * next
Definition: util.h:41

+ Here is the caller graph for this function:

void free_ivec_matrix ( struct s_ivec **  ivec_matrix,
int  nrmin,
int  nrmax,
int  ncmin,
int  ncmax 
)

Definition at line 511 of file util.c.

512  {
513 
514  /* Frees a 2D matrix of integer vectors (ivecs). */
515 
516  int i, j;
517 
518  for (i = nrmin; i <= nrmax; i++) {
519  for (j = ncmin; j <= ncmax; j++) {
520  if (ivec_matrix[i][j].nelem != 0) {
521  free(ivec_matrix[i][j].list);
522  }
523  }
524  }
525 
526  free_matrix(ivec_matrix, nrmin, nrmax, ncmin, sizeof(struct s_ivec));
527 }
void free_matrix(void *vptr, int nrmin, int nrmax, int ncmin, size_t elsize)
Definition: util.c:573
Definition: util.h:47

+ Here is the call graph for this function:

void free_ivec_matrix3 ( struct s_ivec ***  ivec_matrix3,
int  nrmin,
int  nrmax,
int  ncmin,
int  ncmax,
int  ndmin,
int  ndmax 
)

Definition at line 529 of file util.c.

530  {
531 
532  /* Frees a 3D matrix of integer vectors (ivecs). */
533 
534  int i, j, k;
535 
536  for (i = nrmin; i <= nrmax; i++) {
537  for (j = ncmin; j <= ncmax; j++) {
538  for (k = ndmin; k <= ndmax; k++) {
539  if (ivec_matrix3[i][j][k].nelem != 0) {
540  free(ivec_matrix3[i][j][k].list);
541  }
542  }
543  }
544  }
545 
546  free_matrix3(ivec_matrix3, nrmin, nrmax, ncmin, ncmax, ndmin,
547  sizeof(struct s_ivec));
548 }
void free_matrix3(void *vptr, int nrmin, int nrmax, int ncmin, int ncmax, int ndmin, size_t elsize)
Definition: util.c:663
Definition: util.h:47

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void free_ivec_vector ( struct s_ivec ivec_vector,
int  nrmin,
int  nrmax 
)

Definition at line 498 of file util.c.

498  {
499 
500  /* Frees a 1D array of integer vectors. */
501 
502  int i;
503 
504  for (i = nrmin; i <= nrmax; i++)
505  if (ivec_vector[i].nelem != 0)
506  free(ivec_vector[i].list);
507 
508  free(ivec_vector + nrmin);
509 }

+ Here is the caller graph for this function:

void free_matrix ( void *  vptr,
int  nrmin,
int  nrmax,
int  ncmin,
size_t  elsize 
)

Definition at line 573 of file util.c.

573  {
574  int i;
575  char **cptr;
576 
577  cptr = (char **) vptr;
578 
579  for (i = nrmin; i <= nrmax; i++)
580  free(cptr[i] + ncmin * elsize / sizeof(char));
581  free(cptr + nrmin);
582 }

+ Here is the caller graph for this function:

void free_matrix3 ( void *  vptr,
int  nrmin,
int  nrmax,
int  ncmin,
int  ncmax,
int  ndmin,
size_t  elsize 
)

Definition at line 663 of file util.c.

664  {
665  int i, j;
666  char ***cptr;
667 
668  cptr = (char ***) vptr;
669 
670  for (i = nrmin; i <= nrmax; i++) {
671  for (j = ncmin; j <= ncmax; j++)
672  free(cptr[i][j] + ndmin * elsize / sizeof(char));
673  free(cptr[i] + ncmin);
674  }
675  free(cptr + nrmin);
676 }

+ Here is the caller graph for this function:

void free_matrix4 ( void *  vptr,
int  nrmin,
int  nrmax,
int  ncmin,
int  ncmax,
int  ndmin,
int  ndmax,
int  nemin,
size_t  elsize 
)

Definition at line 678 of file util.c.

679  {
680  int i, j, k;
681  char ****cptr;
682 
683  cptr = (char ****) vptr;
684 
685  for (i = nrmin; i <= nrmax; i++) {
686  for (j = ncmin; j <= ncmax; j++) {
687  for (k = ndmin; k <= ndmax; k++)
688  free(cptr[i][j][k] + nemin * elsize / sizeof(char));
689  free(cptr[i][j] + ndmin * elsize / sizeof(char));
690  }
691  free(cptr[i] + ncmin);
692  }
693  free(cptr + nrmin);
694 }

+ Here is the caller graph for this function:

t_linked_int* insert_in_int_list ( t_linked_int head,
int  data,
t_linked_int **  free_list_head_ptr 
)

Definition at line 319 of file util.c.

320  {
321 
322  /* Inserts a new element at the head of a linked list of integers. Returns *
323  * the new head of the list. One argument is the address of the head of *
324  * a list of free ilist elements. If there are any elements on this free *
325  * list, the new element is taken from it. Otherwise a new one is malloced. */
326 
327  t_linked_int *linked_int;
328 
329  if (*free_list_head_ptr != NULL ) {
330  linked_int = *free_list_head_ptr;
331  *free_list_head_ptr = linked_int->next;
332  } else {
333  linked_int = (t_linked_int *) my_malloc(sizeof(t_linked_int));
334  }
335 
336  linked_int->data = data;
337  linked_int->next = head;
338  return (linked_int);
339 }
void * my_malloc(size_t size)
Definition: util.c:147
int data
Definition: util.h:40
struct s_linked_int * next
Definition: util.h:41

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

struct s_linked_vptr* insert_in_vptr_list ( struct s_linked_vptr head,
void *  vptr_to_add 
)

Definition at line 290 of file util.c.

290  {
291 
292  /* Inserts a new element at the head of a linked list of void pointers. *
293  * Returns the new head of the list. */
294 
295  struct s_linked_vptr *linked_vptr;
296 
297  linked_vptr = (struct s_linked_vptr *) my_malloc(
298  sizeof(struct s_linked_vptr));
299 
300  linked_vptr->data_vptr = vptr_to_add;
301  linked_vptr->next = head;
302  return (linked_vptr); /* New head of the list */
303 }
void * my_malloc(size_t size)
Definition: util.c:147
struct s_linked_vptr * next
Definition: util.h:36
void * data_vptr
Definition: util.h:35

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

int ipow ( int  base,
int  exp 
)

Definition at line 775 of file util.c.

775  {
776  int result = 1;
777 
778  assert(exp >= 0);
779 
780  while (exp) {
781  if (exp & 1)
782  result *= base;
783  exp >>= 1;
784  base *= base;
785  }
786 
787  return result;
788 }

+ Here is the caller graph for this function:

int limit_value ( int  cur,
int  max,
const char *  name 
)

Definition at line 23 of file util.c.

23  {
24  if (cur > max) {
25  vpr_printf(TIO_MESSAGE_WARNING,
26  "%s is being limited from [%d] to [%d]\n", name, cur, max);
27  return max;
28  }
29  return cur;
30 }
#define max(a, b)
Definition: graphics.c:171
messagelogger vpr_printf
Definition: util.c:17
int my_atoi ( const char *  str)

Definition at line 116 of file util.c.

116  {
117 
118  /* Returns the integer represented by the first part of the character *
119  * string. */
120 
121  if (str[0] < '0' || str[0] > '9') {
122  if (!(str[0] == '-' && str[1] >= '0' && str[1] <= '9')) {
123  vpr_printf(TIO_MESSAGE_ERROR, "expected number instead of '%s'.\n",
124  str);
125  exit(1);
126  }
127  }
128  return (atoi(str));
129 }
messagelogger vpr_printf
Definition: util.c:17

+ Here is the caller graph for this function:

void* my_calloc ( size_t  nelem,
size_t  size 
)

Definition at line 132 of file util.c.

132  {
133  void *ret;
134  if (nelem == 0) {
135  return NULL ;
136  }
137 
138  if ((ret = calloc(nelem, size)) == NULL ) {
139  vpr_printf(TIO_MESSAGE_ERROR,
140  "Error: Unable to calloc memory. Aborting.\n");
141  exit(1);
142  }
143  return (ret);
144 }
messagelogger vpr_printf
Definition: util.c:17
void* my_chunk_malloc ( size_t  size,
t_chunk chunk_info 
)

Definition at line 184 of file util.c.

184  {
185 
186  /* This routine should be used for allocating fairly small data *
187  * structures where memory-efficiency is crucial. This routine allocates *
188  * large "chunks" of data, and parcels them out as requested. Whenever *
189  * it mallocs a new chunk it adds it to the linked list pointed to by *
190  * chunk_info->chunk_ptr_head. This list can be used to free the *
191  * chunked memory. *
192  * Information about the currently open "chunk" must be stored by the *
193  * user program. chunk_info->mem_avail_ptr points to an int storing *
194  * how many bytes are left in the current chunk, while *
195  * chunk_info->next_mem_loc_ptr is the address of a pointer to the *
196  * next free bytes in the chunk. To start a new chunk, simply set *
197  * chunk_info->mem_avail_ptr = 0. Each independent set of data *
198  * structures should use a new chunk. */
199 
200  /* To make sure the memory passed back is properly aligned, I must *
201  * only send back chunks in multiples of the worst-case alignment *
202  * restriction of the machine. On most machines this should be *
203  * a long, but on 64-bit machines it might be a long long or a *
204  * double. Change the typedef below if this is the case. */
205 
206  typedef long Align;
207 
208 #define CHUNK_SIZE 32768
209 #define FRAGMENT_THRESHOLD 100
210 
211  char *tmp_ptr;
212  int aligned_size;
213 
214  assert(chunk_info->mem_avail >= 0);
215 
216  if ((size_t) (chunk_info->mem_avail) < size) { /* Need to malloc more memory. */
217  if (size > CHUNK_SIZE) { /* Too big, use standard routine. */
218  tmp_ptr = (char *) my_malloc(size);
219 
220  /* When debugging, uncomment the code below to see if memory allocation size */
221  /* makes sense */
222  /*#ifdef DEBUG
223  vpr_printf("NB: my_chunk_malloc got a request for %d bytes.\n",
224  size);
225  vpr_printf("You should consider using my_malloc for such big requests.\n");
226  #endif */
227 
228  assert(chunk_info != NULL);
229  chunk_info->chunk_ptr_head = insert_in_vptr_list(
230  chunk_info->chunk_ptr_head, tmp_ptr);
231  return (tmp_ptr);
232  }
233 
234  if (chunk_info->mem_avail < FRAGMENT_THRESHOLD) { /* Only a small scrap left. */
235  chunk_info->next_mem_loc_ptr = (char *) my_malloc(CHUNK_SIZE);
236  chunk_info->mem_avail = CHUNK_SIZE;
237  assert(chunk_info != NULL);
238  chunk_info->chunk_ptr_head = insert_in_vptr_list(
239  chunk_info->chunk_ptr_head, chunk_info->next_mem_loc_ptr);
240  }
241 
242  /* Execute else clause only when the chunk we want is pretty big, *
243  * and would leave too big an unused fragment. Then we use malloc *
244  * to allocate normally. */
245 
246  else {
247  tmp_ptr = (char *) my_malloc(size);
248  assert(chunk_info != NULL);
249  chunk_info->chunk_ptr_head = insert_in_vptr_list(
250  chunk_info->chunk_ptr_head, tmp_ptr);
251  return (tmp_ptr);
252  }
253  }
254 
255  /* Find the smallest distance to advance the memory pointer and keep *
256  * everything aligned. */
257 
258  if (size % sizeof(Align) == 0) {
259  aligned_size = size;
260  } else {
261  aligned_size = size + sizeof(Align) - size % sizeof(Align);
262  }
263 
264  tmp_ptr = chunk_info->next_mem_loc_ptr;
265  chunk_info->next_mem_loc_ptr += aligned_size;
266  chunk_info->mem_avail -= aligned_size;
267  return (tmp_ptr);
268 }
void * my_malloc(size_t size)
Definition: util.c:147
struct s_linked_vptr * insert_in_vptr_list(struct s_linked_vptr *head, void *vptr_to_add)
Definition: util.c:290
struct s_linked_vptr * chunk_ptr_head
Definition: util.h:58
#define CHUNK_SIZE
char * next_mem_loc_ptr
Definition: util.h:63
int mem_avail
Definition: util.h:62
#define FRAGMENT_THRESHOLD

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

char* my_fgets ( char *  buf,
int  max_size,
FILE *  fp 
)

Definition at line 412 of file util.c.

412  {
413  /* Get an input line, update the line number and cut off *
414  * any comment part. A \ at the end of a line with no *
415  * comment part (#) means continue. my_fgets should give *
416  * identical results for Windows (\r\n) and Linux (\n) *
417  * newlines, since it replaces each carriage return \r *
418  * by a newline character \n. Returns NULL after EOF. */
419 
420  char ch;
421  int i;
422 
423  cont = 0; /* line continued? */
424  file_line_number++; /* global variable */
425 
426  for (i = 0; i < max_size - 1; i++) { /* Keep going until the line finishes or the buffer is full */
427 
428  ch = fgetc(fp);
429 
430  if (feof(fp)) { /* end of file */
431  if (i == 0) {
432  return NULL ; /* required so we can write while (my_fgets(...) != NULL) */
433  } else { /* no newline before end of file - last line must be returned */
434  buf[i] = '\0';
435  return buf;
436  }
437  }
438 
439  if (ch == '#') { /* comment */
440  buf[i] = '\0';
441  while ((ch = fgetc(fp)) != '\n' && !feof(fp))
442  ; /* skip the rest of the line */
443  return buf;
444  }
445 
446  if (ch == '\r' || ch == '\n') { /* newline (cross-platform) */
447  if (i != 0 && buf[i - 1] == '\\') { /* if \ at end of line, line continued */
448  cont = 1;
449  buf[i - 1] = '\n'; /* May need this for tokens */
450  buf[i] = '\0';
451  } else {
452  buf[i] = '\n';
453  buf[i + 1] = '\0';
454  }
455  return buf;
456  }
457 
458  buf[i] = ch; /* copy character into the buffer */
459 
460  }
461 
462  /* Buffer is full but line has not terminated, so error */
463  vpr_printf(TIO_MESSAGE_ERROR,
464  "Error on line %d -- line is too long for input buffer.\n",
466  vpr_printf(TIO_MESSAGE_ERROR,
467  "All lines must be at most %d characters long.\n", BUFSIZE - 2);
468  exit(1);
469 }
int file_line_number
Definition: util.c:15
#define BUFSIZE
Definition: graphics.c:184
static int cont
Definition: util.c:19
messagelogger vpr_printf
Definition: util.c:17

+ Here is the caller graph for this function:

FILE* my_fopen ( const char *  fname,
const char *  flag,
int  prompt 
)

Definition at line 54 of file util.c.

54  {
55  FILE *fp;
56  int Len;
57  char *new_fname = NULL;
58  char prompt_filename[256];
59 
60  /* Appends a prefix string for output files */
61  if (out_file_prefix) {
62  if (strchr(flag, 'w')) {
63  Len = 1; /* NULL char */
64  Len += strlen(out_file_prefix);
65  Len += strlen(fname);
66  new_fname = (char *) my_malloc(Len * sizeof(char));
67  strcpy(new_fname, out_file_prefix);
68  strcat(new_fname, fname);
69  fname = new_fname;
70  }
71  }
72 
73  if (prompt) {
74  int check_num_of_entered_values = scanf("%s", prompt_filename);
75  while (getchar() != '\n')
76  ;
77 
78  while (check_num_of_entered_values != 1) {
79  vpr_printf(TIO_MESSAGE_ERROR,
80  "Was expecting one file name to be entered, with no spaces. You have entered %d parameters. Please try again: \n",
81  check_num_of_entered_values);
82  check_num_of_entered_values = scanf("%s", prompt_filename);
83  }
84  fname = prompt_filename;
85  }
86 
87  if (NULL == (fp = fopen(fname, flag))) {
88  vpr_printf(TIO_MESSAGE_ERROR,
89  "Error opening file %s for %s access: %s.\n", fname, flag,
90  strerror(errno));
91  exit(1);
92  }
93 
94  if (new_fname)
95  free(new_fname);
96 
97  return (fp);
98 }
void * my_malloc(size_t size)
Definition: util.c:147
char * out_file_prefix
Definition: util.c:16
messagelogger vpr_printf
Definition: util.c:17

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

float my_frand ( void  )

Definition at line 738 of file util.c.

738  {
739 
740  /* Creates a random float between 0 and 1. i.e. [0..1). */
741 
742  float fval;
743  int ival;
744 
745  current_random = current_random * IA + IC; /* Use overflow to wrap */
746  ival = current_random & (IM - 1); /* Modulus */
747  fval = (float) ival / (float) IM;
748 
749 #ifdef CHECK_RAND
750  if ((fval < 0) || (fval > 1.)) {
751  vpr_printf(TIO_MESSAGE_ERROR, "Bad value in my_frand, fval = %g\n",
752  fval);
753  exit(1);
754  }
755 #endif
756 
757  return (fval);
758 }
#define IM
Definition: util.c:701
#define IC
Definition: util.c:700
messagelogger vpr_printf
Definition: util.c:17
static unsigned int current_random
Definition: util.c:704
#define IA
Definition: util.c:699

+ Here is the caller graph for this function:

int my_irand ( int  imax)

Definition at line 710 of file util.c.

710  {
711 
712  /* Creates a random integer between 0 and imax, inclusive. i.e. [0..imax] */
713 
714  int ival;
715 
716  /* current_random = (current_random * IA + IC) % IM; */
717  current_random = current_random * IA + IC; /* Use overflow to wrap */
718  ival = current_random & (IM - 1); /* Modulus */
719  ival = (int) ((float) ival * (float) (imax + 0.999) / (float) IM);
720 
721 #ifdef CHECK_RAND
722  if ((ival < 0) || (ival > imax)) {
723  if (ival == imax + 1) {
724  /* Due to random floating point rounding, sometimes above calculation gives number greater than ival by 1 */
725  ival = imax;
726  } else {
727  vpr_printf(TIO_MESSAGE_ERROR,
728  "Bad value in my_irand, imax = %d ival = %d\n", imax,
729  ival);
730  exit(1);
731  }
732  }
733 #endif
734 
735  return (ival);
736 }
#define IM
Definition: util.c:701
#define IC
Definition: util.c:700
messagelogger vpr_printf
Definition: util.c:17
static unsigned int current_random
Definition: util.c:704
#define IA
Definition: util.c:699

+ Here is the caller graph for this function:

void* my_malloc ( size_t  size)

Definition at line 147 of file util.c.

147  {
148  void *ret;
149  if (size == 0) {
150  return NULL ;
151  }
152 
153  if ((ret = malloc(size)) == NULL ) {
154  vpr_printf(TIO_MESSAGE_ERROR,
155  "Error: Unable to malloc memory. Aborting.\n");
156  abort();
157  exit(1);
158  }
159  return (ret);
160 }
messagelogger vpr_printf
Definition: util.c:17

+ Here is the caller graph for this function:

void* my_realloc ( void *  ptr,
size_t  size 
)

Definition at line 163 of file util.c.

163  {
164  void *ret;
165 
166  if (size <= 0) {
167  vpr_printf(TIO_MESSAGE_WARNING, "reallocating of size <= 0.\n");
168  }
169 
170  ret = realloc(ptr, size);
171  if (NULL == ret) {
172  vpr_printf(TIO_MESSAGE_ERROR, "Unable to realloc memory. Aborting. "
173  "ptr=%p, Size=%d.\n", ptr, (int) size);
174  if (ptr == NULL ) {
175  vpr_printf(TIO_MESSAGE_ERROR,
176  "my_realloc: ptr == NULL. Aborting.\n");
177  }
178  exit(1);
179  }
180  return (ret);
181 }
messagelogger vpr_printf
Definition: util.c:17
void my_srandom ( int  seed)

Definition at line 706 of file util.c.

706  {
707  current_random = (unsigned int) seed;
708 }
static unsigned int current_random
Definition: util.c:704

+ Here is the caller graph for this function:

char* my_strdup ( const char *  str)

Definition at line 101 of file util.c.

101  {
102  int Len;
103  char *Dst;
104 
105  if (str == NULL ) {
106  return NULL ;
107  }
108 
109  Len = 1 + strlen(str);
110  Dst = (char *) my_malloc(Len * sizeof(char));
111  memcpy(Dst, str, Len);
112 
113  return Dst;
114 }
void * my_malloc(size_t size)
Definition: util.c:147

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

char* my_strncpy ( char *  dest,
const char *  src,
size_t  size 
)

Definition at line 35 of file util.c.

35  {
36  /* Find string's length */
37  size_t len = strlen(src);
38 
39  /* Cap length at (num - 1) to leave room for \0 */
40  if (size <= len)
41  len = (size - 1);
42 
43  /* Copy as much of string as we can fit */
44  memcpy(dest, src, len);
45 
46  /* explicit null termination */
47  dest[len] = '\0';
48 
49  return dest;
50 }

+ Here is the caller graph for this function:

char* my_strtok ( char *  ptr,
const char *  tokens,
FILE *  fp,
char *  buf 
)

Definition at line 472 of file util.c.

472  {
473 
474  /* Get next token, and wrap to next line if \ at end of line. *
475  * There is a bit of a "gotcha" in strtok. It does not make a *
476  * copy of the character array which you pass by pointer on the *
477  * first call. Thus, you must make sure this array exists for *
478  * as long as you are using strtok to parse that line. Don't *
479  * use local buffers in a bunch of subroutines calling each *
480  * other; the local buffer may be overwritten when the stack is *
481  * restored after return from the subroutine. */
482 
483  char *val;
484 
485  val = strtok(ptr, tokens);
486  for (;;) {
487  if (val != NULL || cont == 0)
488  return (val);
489 
490  /* return unless we have a null value and a continuation line */
491  if (my_fgets(buf, BUFSIZE, fp) == NULL )
492  return (NULL );
493 
494  val = strtok(buf, tokens);
495  }
496 }
#define BUFSIZE
Definition: graphics.c:184
static int cont
Definition: util.c:19
char * my_fgets(char *buf, int max_size, FILE *fp)
Definition: util.c:412

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void print_int_matrix3 ( int ***  vptr,
int  nrmin,
int  nrmax,
int  ncmin,
int  ncmax,
int  ndmin,
int  ndmax,
char *  file 
)

Definition at line 642 of file util.c.

643  {
644  FILE *outfile;
645  int i, j, k;
646 
647  outfile = my_fopen(file, "w", 0);
648 
649  for (k = nrmin; k <= nrmax; ++k) {
650  fprintf(outfile, "Plane %d\n", k);
651  for (j = ncmin; j <= ncmax; ++j) {
652  for (i = ndmin; i <= ndmax; ++i) {
653  fprintf(outfile, "%d ", vptr[k][j][i]);
654  }
655  fprintf(outfile, "\n");
656  }
657  fprintf(outfile, "\n");
658  }
659 
660  fclose(outfile);
661 }
FILE * my_fopen(const char *fname, const char *flag, int prompt)
Definition: util.c:54

+ Here is the call graph for this function:

Variable Documentation

int file_line_number

Definition at line 15 of file util.c.

char* out_file_prefix

Definition at line 16 of file util.c.

messagelogger vpr_printf

Definition at line 17 of file util.c.