VPR-7.0
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros
util.c
Go to the documentation of this file.
1 #include <string.h>
2 #include <assert.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <stdarg.h>
6 #include <errno.h>
7 #include "util.h"
8 
9 /* This file contains utility functions widely used in *
10  * my programs. Many are simply versions of file and *
11  * memory grabbing routines that take the same *
12  * arguments as the standard library ones, but exit *
13  * the program if they find an error condition. */
14 
15 int file_line_number; /* file in line number being parsed */
16 char *out_file_prefix = NULL;
17 messagelogger vpr_printf = PrintHandlerMessage;
18 
19 static int cont; /* line continued? */
20 
21 /* Returns the min of cur and max. If cur > max, a warning
22  * is emitted. */
23 int limit_value(int cur, int max, const char *name) {
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 }
31 
32 /* An alternate for strncpy since strncpy doesn't work as most
33  * people would expect. This ensures null termination */
34 char *
35 my_strncpy(char *dest, const char *src, size_t size) {
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 }
51 
52 /* Uses global var 'out_file_prefix' */
53 FILE *
54 my_fopen(const char *fname, const char *flag, int prompt) {
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 }
99 
100 char *
101 my_strdup(const char *str) {
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 }
115 
116 int my_atoi(const char *str) {
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 }
130 
131 void *
132 my_calloc(size_t nelem, size_t size) {
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 }
145 
146 void *
147 my_malloc(size_t size) {
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 }
161 
162 void *
163 my_realloc(void *ptr, size_t size) {
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 }
182 
183 void *
184 my_chunk_malloc(size_t size, t_chunk *chunk_info) {
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 }
269 
270 void free_chunk_memory(t_chunk *chunk_info) {
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 }
288 
289 struct s_linked_vptr *
290 insert_in_vptr_list(struct s_linked_vptr *head, void *vptr_to_add) {
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 }
304 
305 /* Deletes the element at the head of a linked list of void pointers. *
306  * Returns the new head of the list. */
307 struct s_linked_vptr *
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 }
317 
318 t_linked_int *
320  t_linked_int ** free_list_head_ptr) {
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 }
340 
341 void free_int_list(t_linked_int ** int_list_head_ptr) {
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 }
358 
360  int num_items, struct s_ivec *ivec, t_linked_int ** free_list_head_ptr) {
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 }
410 
411 char *
412 my_fgets(char *buf, int max_size, FILE * fp) {
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 }
470 
471 char *
472 my_strtok(char *ptr, const char *tokens, FILE * fp, char *buf) {
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 }
497 
498 void free_ivec_vector(struct s_ivec *ivec_vector, int nrmin, int nrmax) {
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 }
510 
511 void free_ivec_matrix(struct s_ivec **ivec_matrix, int nrmin, int nrmax,
512  int ncmin, int ncmax) {
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 }
528 
529 void free_ivec_matrix3(struct s_ivec ***ivec_matrix3, int nrmin, int nrmax,
530  int ncmin, int ncmax, int ndmin, int ndmax) {
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 }
549 
550 void **
551 alloc_matrix(int nrmin, int nrmax, int ncmin, int ncmax, size_t elsize) {
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 }
569 
570 /* NB: need to make the pointer type void * instead of void ** to allow *
571  * any pointer to be passed in without a cast. */
572 
573 void free_matrix(void *vptr, int nrmin, int nrmax, int ncmin, size_t elsize) {
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 }
583 
584 void ***
585 alloc_matrix3(int nrmin, int nrmax, int ncmin, int ncmax, int ndmin, int ndmax,
586  size_t elsize) {
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 }
609 
610 void ****
611 alloc_matrix4(int nrmin, int nrmax, int ncmin, int ncmax, int ndmin, int ndmax,
612  int nemin, int nemax, size_t elsize) {
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 }
641 
642 void print_int_matrix3(int ***vptr, int nrmin, int nrmax, int ncmin, int ncmax,
643  int ndmin, int ndmax, char *file) {
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 }
662 
663 void free_matrix3(void *vptr, int nrmin, int nrmax, int ncmin, int ncmax,
664  int ndmin, size_t elsize) {
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 }
677 
678 void free_matrix4(void *vptr, int nrmin, int nrmax, int ncmin, int ncmax,
679  int ndmin, int ndmax, int nemin, size_t elsize) {
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 }
695 
696 /* Portable random number generator defined below. Taken from ANSI C by *
697  * K & R. Not a great generator, but fast, and good enough for my needs. */
698 
699 #define IA 1103515245u
700 #define IC 12345u
701 #define IM 2147483648u
702 #define CHECK_RAND
703 
704 static unsigned int current_random = 0;
705 
706 void my_srandom(int seed) {
707  current_random = (unsigned int) seed;
708 }
709 
710 int my_irand(int imax) {
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 }
737 
738 float my_frand(void) {
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 }
759 
760 boolean file_exists(const char * filename) {
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 }
774 
775 int ipow(int base, int exp) {
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 }
void free_matrix(void *vptr, int nrmin, int nrmax, int ncmin, size_t elsize)
Definition: util.c:573
void free_ivec_matrix3(struct s_ivec ***ivec_matrix3, int nrmin, int nrmax, int ncmin, int ncmax, int ndmin, int ndmax)
Definition: util.c:529
int ipow(int base, int exp)
Definition: util.c:775
boolean file_exists(const char *filename)
Definition: util.c:760
Definition: util.h:57
int file_line_number
Definition: util.c:15
#define IM
Definition: util.c:701
int my_atoi(const char *str)
Definition: util.c:116
void * my_malloc(size_t size)
Definition: util.c:147
void * my_realloc(void *ptr, size_t size)
Definition: util.c:163
char * out_file_prefix
Definition: util.c:16
int data
Definition: util.h:40
int * list
Definition: util.h:49
void ** alloc_matrix(int nrmin, int nrmax, int ncmin, int ncmax, size_t elsize)
Definition: util.c:551
struct s_linked_vptr * insert_in_vptr_list(struct s_linked_vptr *head, void *vptr_to_add)
Definition: util.c:290
void free_chunk_memory(t_chunk *chunk_info)
Definition: util.c:270
struct s_linked_vptr * chunk_ptr_head
Definition: util.h:58
void free_int_list(t_linked_int **int_list_head_ptr)
Definition: util.c:341
#define BUFSIZE
Definition: graphics.c:184
Definition: util.h:12
char * my_strtok(char *ptr, const char *tokens, FILE *fp, char *buf)
Definition: util.c:472
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: util.c:359
void * my_calloc(size_t nelem, size_t size)
Definition: util.c:132
#define CHUNK_SIZE
void free_ivec_matrix(struct s_ivec **ivec_matrix, int nrmin, int nrmax, int ncmin, int ncmax)
Definition: util.c:511
float my_frand(void)
Definition: util.c:738
static int cont
Definition: util.c:19
int my_irand(int imax)
Definition: util.c:710
char * my_strdup(const char *str)
Definition: util.c:101
void free_matrix4(void *vptr, int nrmin, int nrmax, int ncmin, int ncmax, int ndmin, int ndmax, int nemin, size_t elsize)
Definition: util.c:678
#define max(a, b)
Definition: graphics.c:171
int nelem
Definition: util.h:48
int limit_value(int cur, int max, const char *name)
Definition: util.c:23
void free_matrix3(void *vptr, int nrmin, int nrmax, int ncmin, int ncmax, int ndmin, size_t elsize)
Definition: util.c:663
t_linked_int * insert_in_int_list(t_linked_int *head, int data, t_linked_int **free_list_head_ptr)
Definition: util.c:319
struct s_linked_vptr * next
Definition: util.h:36
FILE * my_fopen(const char *fname, const char *flag, int prompt)
Definition: util.c:54
Definition: util.h:47
void * data_vptr
Definition: util.h:35
#define IC
Definition: util.c:700
struct s_linked_vptr * delete_in_vptr_list(struct s_linked_vptr *head)
Definition: util.c:308
messagelogger vpr_printf
Definition: util.c:17
char * my_strncpy(char *dest, const char *src, size_t size)
Definition: util.c:35
static unsigned int current_random
Definition: util.c:704
char * my_fgets(char *buf, int max_size, FILE *fp)
Definition: util.c:412
char * next_mem_loc_ptr
Definition: util.h:63
void **** alloc_matrix4(int nrmin, int nrmax, int ncmin, int ncmax, int ndmin, int ndmax, int nemin, int nemax, size_t elsize)
Definition: util.c:611
void my_srandom(int seed)
Definition: util.c:706
struct s_linked_int * next
Definition: util.h:41
int mem_avail
Definition: util.h:62
void print_int_matrix3(int ***vptr, int nrmin, int nrmax, int ncmin, int ncmax, int ndmin, int ndmax, char *file)
Definition: util.c:642
void *** alloc_matrix3(int nrmin, int nrmax, int ncmin, int ncmax, int ndmin, int ndmax, size_t elsize)
Definition: util.c:585
#define IA
Definition: util.c:699
unsigned char(* messagelogger)(TIO_MessageMode_t messageMode, char *pszMessage,...)
Definition: util.h:133
void free_ivec_vector(struct s_ivec *ivec_vector, int nrmin, int nrmax)
Definition: util.c:498
#define FRAGMENT_THRESHOLD
Definition: util.h:12
void * my_chunk_malloc(size_t size, t_chunk *chunk_info)
Definition: util.c:184