dylib_std.h
Go to the documentation of this file.
1 #ifndef _DYLIB_STD_H
2 #define _DYLIB_STD_H
3 
4 /*
5  This file is part of the support library for the Dylp LP distribution.
6 
7  Copyright (C) 2005 -- 2007 Lou Hafer
8 
9  School of Computing Science
10  Simon Fraser University
11  Burnaby, B.C., V5A 1S6, Canada
12  lou@cs.sfu.ca
13 
14  This code is licensed under the terms of the Common Public License (CPL).
15 */
16 
17 /*
18  @(#)dylib_std.h 1.5 09/25/04
19  svn/cvs: $Id: dylib_std.h 148 2007-06-09 03:15:30Z lou $
20 */
21 
22 /*
23  This file contains common definitions.
24 
25  First thing to do is haul in the Ansi C standard definitions. Takes care of
26  NULL plus a few more obscure definitions. Also haul in the standard library
27  declarations.
28 */
29 
30 #include <stddef.h>
31 #include <stdlib.h>
32 
33 #include "DylpConfig.h"
34 
35 /*
36  A utility definition which allows for easy suppression of unused variable
37  warnings from GCC. Useful when a variable is used only for assert()
38  statements, and for sccsid/cvsid strings.
39 */
40 #ifndef UNUSED
41 # if defined(_GNU_SOURCE) || defined(__GNUC__)
42 # define UNUSED __attribute__((unused))
43 # else
44 # define UNUSED
45 # endif
46 #endif
47 
48 /*
49  Memory copy functions --- memcpy, memset, and other less common ones.
50 */
51 
52 #include <string.h>
53 
54 /*
55  We need a boolean type. Never could understand why C doesn't have this.
56 
57  [Aug 10, 01] For compatibility with C++, TRUE and FALSE are defined to be
58  the corresponding C++ values. BOOL should be set in the compiler command
59  line to be the storage type (char/short/int/long) that matches the size of
60  a C++ "bool". All these are necessary to link with and be called by C++
61  code in osi-bonsai.
62 */
63 
64 #ifndef __cplusplus
65 #define FALSE 0
66 #define TRUE 1
67 # ifdef BOOL
68  typedef BOOL bool ;
69 # else
70 /*
71  You're in trouble. The likely source of the problem is that this file is
72  being included in the course of a build controlled by a makefile that
73  doesn't know about the booltype utility in the dylp distribution. See the
74  Utils subdirectory, and also check the makefile for dylp to see how this is
75  used. If you don't want to fiddle with your build control files, just
76  build booltype, run it, and edit in the appropriate definition. If you're
77  not worried about C++ compatibility, int is a good as anything.
78 */
79 # warning The compile-time symbol BOOL is not defined (dylib_std.h)
80  typedef int bool ;
81 # endif
82 #endif
83 
84 #ifdef __cplusplus
85 #ifndef FALSE
86 # define FALSE false
87 #endif
88 #ifndef TRUE
89 # define TRUE true
90 #endif
91 #endif
92 
93 /*
94  flags is used to indicate a data type composed of one-bit flags. Manipulated
95  with the set of flag manipulation macros defined below.
96 */
97 
98 typedef unsigned int flags ;
99 
100 #define setflg(zz_flgs,zz_flg) ((zz_flgs) |= (zz_flg))
101 #define clrflg(zz_flgs,zz_flg) ((zz_flgs) &= ~(zz_flg))
102 #define comflg(zz_flgs,zz_flg) ((zz_flgs) ^= (zz_flg))
103 #define getflg(zz_flgs,zz_flg) ((zz_flgs)&(zz_flg))
104 #define flgon(zz_flgs,zz_flg) ((zz_flgs)&(zz_flg)?TRUE:FALSE)
105 #define flgoff(zz_flgs,zz_flg) ((zz_flgs)&(zz_flg)?FALSE:TRUE)
106 #define flgall(zz_flgs,zz_flg) ((((zz_flgs)&(zz_flg)) == (zz_flg))?TRUE:FALSE)
107 
108 
109 /*
110  lnk_struct is a general-purpose linked list structure.
111 
112  Field Description
113  ----- -----------
114  llnxt pointer to the next list element
115  llval pointer to the associated value
116 */
117 
118 typedef struct lnk_struct_tag
120  void *llval ; } lnk_struct ;
121 
122 #define lnk_in(qqlnk,qqval) ((qqlnk)->llval = (void *) (qqval))
123 #define lnk_out(qqlnk,qqtype) ((qqtype) (qqlnk)->llval)
124 
125 
126 /* Max and min macros */
127 
128 #define minn(qa,qb) (((qa) > (qb))?(qb):(qa))
129 #define maxx(qa,qb) (((qa) > (qb))?(qa):(qb))
130 
131 
132 /*
133  Some macros to hide the memory allocation functions.
134 
135  The serious debugging versions of these macros (MALLOC_DEBUG = 2) use
136  outfmt from the io library and assume the existence of a string, rtnnme
137  (typically the name of the current subroutine) that's used to identify the
138  origin of the message. There's enough information in the messages to track
139  the allocation and deallocation of blocks, should you not have access to an
140  interactive debugger with this capability.
141 
142  The casual debugging versions (MALLOC_DEBUG = 1) only check for a return
143  value of 0 and print a message to stderr with the file and line number.
144  This at least tells you when your code has core dumped because it ran out
145  of space (as opposed to a bug you can actually fix).
146 */
147 
148 #if (MALLOC_DEBUG == 2)
149 
150 #include "dylib_io.h"
151 
152 void *zz_ptr_zz ;
153 ioid zz_chn_zz ;
154 
155 #define MALLOC_DBG_INIT(chn) ( zz_chn_zz = chn )
156 
157 #define MALLOC(zz_sze_zz) \
158  ( zz_ptr_zz = (void *) malloc(zz_sze_zz), \
159  outfmt(zz_chn_zz,FALSE,":malloc: %d bytes at %#08x in %s.\n", \
160  zz_sze_zz,zz_ptr_zz,rtnnme), \
161  zz_ptr_zz )
162 
163 #define CALLOC(zz_cnt_zz,zz_sze_zz) \
164  ( zz_ptr_zz = (void *) calloc(zz_cnt_zz,zz_sze_zz), \
165  outfmt(zz_chn_zz,FALSE,":calloc: %d (%d*%d) bytes at %#08x in %s.\n", \
166  zz_cnt_zz*zz_sze_zz,zz_cnt_zz,zz_sze_zz,zz_ptr_zz,rtnnme), \
167  zz_ptr_zz )
168 
169 #define REALLOC(zz_rptr_zz,zz_sze_zz) \
170  ( zz_ptr_zz = (void *) realloc(zz_rptr_zz,zz_sze_zz), \
171  outfmt(zz_chn_zz,FALSE, \
172  ":realloc: %#08x changed to %d bytes at %#08x in %s.\n", \
173  zz_rptr_zz,zz_sze_zz,zz_ptr_zz,rtnnme), \
174  zz_ptr_zz )
175 
176 #define FREE(zz_fptr_zz) \
177  ( outfmt(zz_chn_zz,FALSE,":free: %#08x in %s.\n",zz_fptr_zz,rtnnme), \
178  free((void *) zz_fptr_zz) )
179 
180 #elif (MALLOC_DEBUG == 1)
181 
182 #include <stdio.h>
183 void *zz_ptr_zz ;
184 
185 #define MALLOC(zz_sze_zz) \
186  ( zz_ptr_zz = (void *) malloc(zz_sze_zz), \
187  (zz_ptr_zz != 0)?0:\
188  fprintf(stderr,":malloc: failed to get %d bytes at %s:%d.\n", \
189  zz_sze_zz,__FILE__,__LINE__), \
190  zz_ptr_zz )
191 
192 #define CALLOC(zz_cnt_zz,zz_sze_zz) \
193  ( zz_ptr_zz = (void *) calloc(zz_cnt_zz,zz_sze_zz), \
194  (zz_ptr_zz != 0)?0:\
195  fprintf(stderr,":calloc: failed to get %d bytes at %s:%d.\n", \
196  zz_sze_zz*zz_cnt_zz,__FILE__,__LINE__), \
197  zz_ptr_zz )
198 
199 #define REALLOC(zz_rptr_zz,zz_sze_zz) \
200  ( zz_ptr_zz = (void *) realloc(zz_rptr_zz,zz_sze_zz), \
201  (zz_ptr_zz != 0)?0:\
202  fprintf(stderr,":realloc: failed to get %d bytes at %s:%d.\n", \
203  zz_sze_zz,__FILE__,__LINE__), \
204  zz_ptr_zz )
205 
206 #define FREE(zz_fptr_zz) free((void *) zz_fptr_zz)
207 
208 #else
209 
210 #define MALLOC_DBG_INIT(chn)
211 
212 #define MALLOC(zz_sze_zz) malloc(zz_sze_zz)
213 
214 #define CALLOC(zz_cnt_zz,zz_sze_zz) calloc(zz_cnt_zz,zz_sze_zz)
215 
216 #define REALLOC(zz_rptr_zz,zz_sze_zz) realloc(zz_rptr_zz,zz_sze_zz)
217 
218 #define FREE(zz_fptr_zz) free((void *) zz_fptr_zz)
219 
220 #endif
221 
222 
223 #endif /* _DYLIB_STD_H */