VPR-7.0
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros
util.h
Go to the documentation of this file.
1 #ifndef UTIL_H
2 #define UTIL_H
3 
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <math.h>
7 
8 #include "TIO_PrintHandlerExtern.h"
9 
10 #ifndef TRUE /* Some compilers predefine TRUE, FALSE */
11 typedef enum {
13 } boolean;
14 #else
15 typedef int boolean;
16 #endif
17 
18 /* Parameter tags for preprocessor to strip */
19 #define INP
20 #define OUTP
21 #define INOUTP
22 
23 #define BUFSIZE 4096 /* Maximum line length for various parsing proc. */
24 #define nint(a) ((int) floor (a + 0.5))
25 
26 #define ERRTAG "ERROR:\t"
27 #define WARNTAG "WARNING:\t"
28 
29 
30 int limit_value(int cur, int max, const char *name);
31 
32 /* Linked lists of void pointers and integers, respectively. */
33 
34 typedef struct s_linked_vptr {
35  void *data_vptr;
38 
39 typedef struct s_linked_int {
40  int data;
41  struct s_linked_int *next;
42 } t_linked_int;
43 
44 /* Integer vector. nelem stores length, list[0..nelem-1] stores list of *
45  * integers. */
46 
47 typedef struct s_ivec {
48  int nelem;
49  int *list;
50 } t_ivec;
51 
52 /* This structure is to keep track of chunks of memory that is being *
53  * allocated to save overhead when allocating very small memory pieces. *
54  * For a complete description, please see the comment in my_chunk_malloc*
55  * in libarchfpga/utils.c. */
56 
57 typedef struct s_chunk {
59  /* chunk_ptr_head->data_vptr: head of the entire linked
60  * list of allocated "chunk" memory;
61  * chunk_ptr_head->next: pointer to the next chunk on the linked list*/
62  int mem_avail; /* number of bytes left in the current chunk */
63  char *next_mem_loc_ptr;/* pointer to the first available (free) *
64  * byte in the current chunk */
65 } t_chunk;
66 
67 #ifdef __cplusplus
68 extern "C" {
69 #endif
70 
71 extern int file_line_number; /* line in file being parsed */
72 extern char *out_file_prefix; /* Default prefix string for output files */
73 
74 /************************ Memory allocation routines *************************/
75 
76 void* my_malloc(size_t size);
77 void* my_calloc(size_t nelem, size_t size);
78 void *my_realloc(void *ptr, size_t size);
79 void *my_chunk_malloc(size_t size, t_chunk *chunk_info);
80 void free_chunk_memory(t_chunk *chunk_info);
81 
82 /******************* Linked list, matrix and vector utilities ****************/
83 
84 void free_ivec_vector(struct s_ivec *ivec_vector, int nrmin, int nrmax);
85 void free_ivec_matrix(struct s_ivec **ivec_matrix, int nrmin, int nrmax,
86  int ncmin, int ncmax);
87 void free_ivec_matrix3(struct s_ivec ***ivec_matrix3, int nrmin,
88  int nrmax, int ncmin, int ncmax, int ndmin, int ndmax);
89 
90 void** alloc_matrix(int nrmin, int nrmax, int ncmin, int ncmax,
91  size_t elsize);
92 void ***alloc_matrix3(int nrmin, int nrmax, int ncmin, int ncmax,
93  int ndmin, int ndmax, size_t elsize);
94 void ****alloc_matrix4(int nrmin, int nrmax, int ncmin, int ncmax,
95  int ndmin, int ndmax, int nemin, int nemax, size_t elsize);
96 
97 void free_matrix(void *vptr, int nrmin, int nrmax, int ncmin,
98  size_t elsize);
99 void free_matrix3(void *vptr, int nrmin, int nrmax, int ncmin, int ncmax,
100  int ndmin, size_t elsize);
101 void free_matrix4(void *vptr, int nrmin, int nrmax, int ncmin, int ncmax,
102  int ndmin, int ndmax, int nemin, size_t elsize);
103 
104 void print_int_matrix3(int ***vptr, int nrmin, int nrmax, int ncmin,
105  int ncmax, int ndmin, int ndmax, char *file);
106 
107 struct s_linked_vptr *insert_in_vptr_list(struct s_linked_vptr *head,
108  void *vptr_to_add);
109 struct s_linked_vptr *delete_in_vptr_list(struct s_linked_vptr *head);
111  t_linked_int ** free_list_head_ptr);
112 void free_int_list(t_linked_int ** int_list_head_ptr);
113 void alloc_ivector_and_copy_int_list(t_linked_int ** list_head_ptr,
114  int num_items, struct s_ivec *ivec, t_linked_int ** free_list_head_ptr);
115 
116 /****************** File and parsing utilities *******************************/
117 
118 int my_atoi(const char *str);
119 
120 char* my_strdup(const char *str);
121 char *my_strncpy(char *dest, const char *src, size_t size);
122 char *my_strtok(char *ptr, const char *tokens, FILE * fp, char *buf);
123 
124 FILE* my_fopen(const char *fname, const char *flag, int prompt);
125 char *my_fgets(char *buf, int max_size, FILE * fp);
126 boolean file_exists(const char * filename);
127 
128 /*********************** Portable random number generators *******************/
129 void my_srandom(int seed);
130 int my_irand(int imax);
131 float my_frand(void);
132 
133 typedef unsigned char (*messagelogger)( TIO_MessageMode_t messageMode,
134  char* pszMessage,
135  ... );
137 
138 #ifdef __cplusplus
139 }
140 #endif
141 
142 /*********************** Math operations *************************************/
143 int ipow(int base, int exp);
144 
145 #endif
146 
Definition: util.h:57
FILE * my_fopen(const char *fname, const char *flag, int prompt)
Definition: util.c:54
void ** alloc_matrix(int nrmin, int nrmax, int ncmin, int ncmax, size_t elsize)
Definition: util.c:551
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
char * my_strncpy(char *dest, const char *src, size_t size)
Definition: util.c:35
void free_matrix(void *vptr, int nrmin, int nrmax, int ncmin, size_t elsize)
Definition: util.c:573
int ipow(int base, int exp)
Definition: util.c:775
boolean
Definition: util.h:11
int data
Definition: util.h:40
void free_matrix3(void *vptr, int nrmin, int nrmax, int ncmin, int ncmax, int ndmin, size_t elsize)
Definition: util.c:663
int * list
Definition: util.h:49
t_linked_int * insert_in_int_list(t_linked_int *head, int data, t_linked_int **free_list_head_ptr)
Definition: util.c:319
void * my_realloc(void *ptr, size_t size)
Definition: util.c:163
void * my_chunk_malloc(size_t size, t_chunk *chunk_info)
Definition: util.c:184
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
struct s_linked_vptr * chunk_ptr_head
Definition: util.h:58
struct s_chunk t_chunk
Definition: util.h:12
char * out_file_prefix
Definition: util.c:16
struct s_linked_int t_linked_int
float my_frand(void)
Definition: util.c:738
#define max(a, b)
Definition: graphics.c:171
int nelem
Definition: util.h:48
void free_ivec_vector(struct s_ivec *ivec_vector, int nrmin, int nrmax)
Definition: util.c:498
struct s_linked_vptr * delete_in_vptr_list(struct s_linked_vptr *head)
Definition: util.c:308
void free_ivec_matrix(struct s_ivec **ivec_matrix, int nrmin, int nrmax, int ncmin, int ncmax)
Definition: util.c:511
void free_int_list(t_linked_int **int_list_head_ptr)
Definition: util.c:341
int limit_value(int cur, int max, const char *name)
Definition: util.c:23
char * my_strtok(char *ptr, const char *tokens, FILE *fp, char *buf)
Definition: util.c:472
struct s_ivec t_ivec
struct s_linked_vptr * next
Definition: util.h:36
struct s_linked_vptr * insert_in_vptr_list(struct s_linked_vptr *head, void *vptr_to_add)
Definition: util.c:290
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
Definition: util.h:47
void * data_vptr
Definition: util.h:35
int my_irand(int imax)
Definition: util.c:710
char * next_mem_loc_ptr
Definition: util.h:63
boolean file_exists(const char *filename)
Definition: util.c:760
char * my_fgets(char *buf, int max_size, FILE *fp)
Definition: util.c:412
int file_line_number
Definition: util.c:15
struct s_linked_int * next
Definition: util.h:41
int mem_avail
Definition: util.h:62
void free_chunk_memory(t_chunk *chunk_info)
Definition: util.c:270
unsigned char(* messagelogger)(TIO_MessageMode_t messageMode, char *pszMessage,...)
Definition: util.h:133
int my_atoi(const char *str)
Definition: util.c:116
char * my_strdup(const char *str)
Definition: util.c:101
void *** alloc_matrix3(int nrmin, int nrmax, int ncmin, int ncmax, int ndmin, int ndmax, size_t elsize)
Definition: util.c:585
messagelogger vpr_printf
Definition: util.c:17
void * my_malloc(size_t size)
Definition: util.c:147
void my_srandom(int seed)
Definition: util.c:706
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
Definition: util.h:12
void print_int_matrix3(int ***vptr, int nrmin, int nrmax, int ncmin, int ncmax, int ndmin, int ndmax, char *file)
Definition: util.c:642
struct s_linked_vptr t_linked_vptr