DyLP 1.10.4
Loading...
Searching...
No Matches
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 Eclipse Public License (EPL).
15*/
16
17/*
18 @(#)dylib_std.h 1.5 09/25/04
19 svn/cvs: $Id: dylib_std.h 407 2010-12-31 20:48:48Z 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".
61*/
62
63#ifndef __cplusplus
64#define FALSE 0
65#define TRUE 1
66# if __STDC_VERSION__ < 202311
67# ifdef BOOL
68 typedef BOOL bool ;
69# else
70/*
71 You're in trouble. Normally a definition for BOOL is determined by the
72 configure script; apparently you're outside of whatever framework should
73 do this. If you're not worried about C++ compatibility, int is a good a
74 choice as anything. If you're concerned about C++ compatibility, write a
75 small C++ program that prints out sizeof(bool) and add the definition here.
76*/
77# warning The compile-time symbol BOOL is not defined (dylib_std.h)
78 typedef int bool ;
79# endif
80#endif
81#endif
82
83#ifdef __cplusplus
84#ifndef FALSE
85# define FALSE false
86#endif
87#ifndef TRUE
88# define TRUE true
89#endif
90#endif
91
92/*
93 flags is used to indicate a data type composed of one-bit flags. Manipulated
94 with the set of flag manipulation macros defined below.
95*/
96
97typedef unsigned int flags ;
98
99#define setflg(zz_flgs,zz_flg) ((zz_flgs) |= (zz_flg))
100#define clrflg(zz_flgs,zz_flg) ((zz_flgs) &= ~(zz_flg))
101#define comflg(zz_flgs,zz_flg) ((zz_flgs) ^= (zz_flg))
102#define getflg(zz_flgs,zz_flg) ((zz_flgs)&(zz_flg))
103#define flgon(zz_flgs,zz_flg) ((zz_flgs)&(zz_flg)?TRUE:FALSE)
104#define flgoff(zz_flgs,zz_flg) ((zz_flgs)&(zz_flg)?FALSE:TRUE)
105#define flgall(zz_flgs,zz_flg) ((((zz_flgs)&(zz_flg)) == (zz_flg))?TRUE:FALSE)
106
107
108/*
109 lnk_struct is a general-purpose linked list structure.
110
111 Field Description
112 ----- -----------
113 llnxt pointer to the next list element
114 llval pointer to the associated value
115*/
116
117typedef struct lnk_struct_tag
119 void *llval ; } lnk_struct ;
120
121#define lnk_in(qqlnk,qqval) ((qqlnk)->llval = (void *) (qqval))
122#define lnk_out(qqlnk,qqtype) ((qqtype) (qqlnk)->llval)
123
124
125/* Max and min macros */
126
127#define minn(qa,qb) (((qa) > (qb))?(qb):(qa))
128#define maxx(qa,qb) (((qa) > (qb))?(qa):(qb))
129
130
131/*
132 Some macros to hide the memory allocation functions.
133
134 The serious debugging versions of these macros (MALLOC_DEBUG = 2) use
135 outfmt from the io library and assume the existence of a string, rtnnme
136 (typically the name of the current subroutine) that's used to identify the
137 origin of the message. There's enough information in the messages to track
138 the allocation and deallocation of blocks, should you not have access to an
139 interactive debugger with this capability.
140
141 The casual debugging versions (MALLOC_DEBUG = 1) only check for a return
142 value of 0 and print a message to stderr with the file and line number.
143 This at least tells you when your code has core dumped because it ran out
144 of space (as opposed to a bug you can actually fix).
145*/
146
147#if (MALLOC_DEBUG == 2)
148
149#include "dylib_io.h"
150
151void *zz_ptr_zz ;
152ioid zz_chn_zz ;
153
154#define MALLOC_DBG_INIT(chn) ( zz_chn_zz = chn )
155
156#define MALLOC(zz_sze_zz) \
157 ( zz_ptr_zz = (void *) malloc(zz_sze_zz), \
158 dyio_outfmt(zz_chn_zz,FALSE,":malloc: %d bytes at %#08x in %s.\n", \
159 zz_sze_zz,zz_ptr_zz,rtnnme), \
160 zz_ptr_zz )
161
162#define CALLOC(zz_cnt_zz,zz_sze_zz) \
163 ( zz_ptr_zz = (void *) calloc(zz_cnt_zz,zz_sze_zz), \
164 dyio_outfmt(zz_chn_zz,FALSE,":calloc: %d (%d*%d) bytes at %#08x in %s.\n", \
165 zz_cnt_zz*zz_sze_zz,zz_cnt_zz,zz_sze_zz,zz_ptr_zz,rtnnme), \
166 zz_ptr_zz )
167
168#define REALLOC(zz_rptr_zz,zz_sze_zz) \
169 ( zz_ptr_zz = (void *) realloc(zz_rptr_zz,zz_sze_zz), \
170 dyio_outfmt(zz_chn_zz,FALSE, \
171 ":realloc: %#08x changed to %d bytes at %#08x in %s.\n", \
172 zz_rptr_zz,zz_sze_zz,zz_ptr_zz,rtnnme), \
173 zz_ptr_zz )
174
175#define FREE(zz_fptr_zz) \
176 ( dyio_outfmt(zz_chn_zz,FALSE,":free: %#08x in %s.\n",zz_fptr_zz,rtnnme), \
177 free((void *) zz_fptr_zz) )
178
179#elif (MALLOC_DEBUG == 1)
180
181#include <stdio.h>
182void *zz_ptr_zz ;
183
184#define MALLOC(zz_sze_zz) \
185 ( zz_ptr_zz = (void *) malloc(zz_sze_zz), \
186 (zz_ptr_zz != 0)?0:\
187 fprintf(stderr,":malloc: failed to get %d bytes at %s:%d.\n", \
188 zz_sze_zz,__FILE__,__LINE__), \
189 zz_ptr_zz )
190
191#define CALLOC(zz_cnt_zz,zz_sze_zz) \
192 ( zz_ptr_zz = (void *) calloc(zz_cnt_zz,zz_sze_zz), \
193 (zz_ptr_zz != 0)?0:\
194 fprintf(stderr,":calloc: failed to get %d bytes at %s:%d.\n", \
195 zz_sze_zz*zz_cnt_zz,__FILE__,__LINE__), \
196 zz_ptr_zz )
197
198#define REALLOC(zz_rptr_zz,zz_sze_zz) \
199 ( zz_ptr_zz = (void *) realloc(zz_rptr_zz,zz_sze_zz), \
200 (zz_ptr_zz != 0)?0:\
201 fprintf(stderr,":realloc: failed to get %d bytes at %s:%d.\n", \
202 zz_sze_zz,__FILE__,__LINE__), \
203 zz_ptr_zz )
204
205#define FREE(zz_fptr_zz) free((void *) zz_fptr_zz)
206
207#else
208
209#define MALLOC_DBG_INIT(chn)
210
211#define MALLOC(zz_sze_zz) malloc(zz_sze_zz)
212
213#define CALLOC(zz_cnt_zz,zz_sze_zz) calloc(zz_cnt_zz,zz_sze_zz)
214
215#define REALLOC(zz_rptr_zz,zz_sze_zz) realloc(zz_rptr_zz,zz_sze_zz)
216
217#define FREE(zz_fptr_zz) free((void *) zz_fptr_zz)
218
219#endif
220
221
222#endif /* _DYLIB_STD_H */
#define BOOL
Definition config.h:6
int ioid
Definition dylib_io.h:39
unsigned int flags
Definition dylib_std.h:97
BOOL bool
Definition dylib_std.h:68
struct lnk_struct_tag lnk_struct
struct lnk_struct_tag * llnxt
Definition dylib_std.h:118