Coda Distributed File System
odytypes.h
Go to the documentation of this file.
1/*
2** odytypes.h
3**
4** the every-odyssey-file-should-have-them definitions
5**
6** Types: bool boolean variables
7** magic_t magic number tags for structures
8** CMPFN comparison function of two anonymous objects
9** TIMEVAL a time value; currently a struct timeval
10** might change with better machines. Full precision.
11**
12** Macros: CODA_ASSERT use instead of CODA_ASSERT.h
13** ALLOC use instead of malloc for objects
14** NALLOC use instead of malloc for arrays
15** FREE use instead of free
16** TIME get current time
17** TIMEDIFF difference in time, in TIMEVAL units.
18*/
19
20#ifndef _ODYTYPES_H_
21#define _ODYTYPES_H_
22
23#include <stdlib.h>
24#include <stdio.h>
25#include <sys/time.h>
26
27/**************************************** prototypes */
28#ifndef __P
29#ifdef __STDC__
30#define __P(args) args
31#else /* __STDC__ */
32#define __P(args) ()
33#endif /* __STDC__ */
34#endif /* __P */
35
36/**************************************** booleans */
37typedef enum
38{
39 TRUE = 1,
40 FALSE = 0
42
43/*************************************** magic number tags */
44typedef unsigned long magic_t;
45
46/*************************************** Common function pointer types */
47
48#ifdef __STDC__
49
50typedef long (*COMPFN)(void *, void *); /* used by data structure routines */
51
52#else /* __STDC__ */
53
54typdef int (*COMPFN)();
55
56#endif /* __STDC__ */
57
58/*************************************** TIMEVAL */
59
60typedef struct timeval TIMEVAL;
61
62/*************************************** assertions */
63#ifndef __FILE__
64#define __FILE__ "()"
65#endif
66
67#ifndef __LINE__
68#define __LINE__ 0
69#endif
70
71#ifdef __STDC__
72
73#define CODA_ASSERT(cond) \
74 do { \
75 if (!(cond)) { \
76 int *j = 0; \
77 fprintf(stderr, "Assert (%s) in %s:%d\n", #cond, __FILE__, \
78 __LINE__); \
79 fflush(stderr); \
80 *j = 1; /* cause a real SIGTRAP to happen: can be caught */ \
81 } \
82 } while (0)
83
84#else /* __STDC__ */
85
86#define CODA_ASSERT(cond) \
87 do { \
88 if (!(cond)) { \
89 int *j = 0; \
90 fprintf(stderr, "Assert failed in %s:%d\n", __FILE__, __LINE__); \
91 fflush(stderr); \
92 *j = 1; /* cause a real SIGTRAP to happen: can be caught */ \
93 } \
94 } while (0)
95
96#endif /* __STDC__ */
97
98/*************************************** Allocation, deallocation, validity */
99
100#define ALLOC(X, T) \
101 do { \
102 if (((X) = (T *)malloc(sizeof(T))) == NULL) \
103 CODA_ASSERT(0); \
104 } while (0)
105
106#define NALLOC(X, T, S) \
107 do { \
108 if (((X) = (T *)malloc(sizeof(T) * (S))) == NULL) { \
109 CODA_ASSERT(0); \
110 } \
111 } while (0)
112
113#define FREE(X) \
114 do { \
115 if ((X) != NULL) { \
116 free((X)); \
117 (X) = NULL; \
118 } \
119 } while (0)
120
121/*************************************** time operations */
122
123/* tvp should be of type TIMEVAL */
124#define TIME(tv) (gettimeofday(&(tv), NULL))
125
126/* tvb, tve, tvr should be of type TIMEVAL */
127/* tve should be >= tvb. */
128#define TIMEDIFF(tvb, tve, tvr) \
129 do { \
130 CODA_ASSERT(((tve).sec > (tvb).sec) || \
131 (((tve).sec == (tvb).sec) && ((tve).usec > (tvb).usec))); \
132 if ((tve).usec < (tvb).usec) { \
133 (tvr).usec = 1000000 + (tve).usec - (tvb).usec; \
134 (tvr).sec = (tve).sec - (tvb).sec - 1; \
135 } else { \
136 (tvr).usec = (tve).usec - (tvb).usec; \
137 (tvr).sec = (tve).sec - (tvb).sec; \
138 } \
139 } while (0)
140
141#endif /* _ODYTYPES_H_ */
struct timeval TIMEVAL
Definition: odytypes.h:60
unsigned long magic_t
Definition: odytypes.h:44
typdef int(* COMPFN)()
Definition: odytypes.h:54
bool
Definition: odytypes.h:38
@ FALSE
Definition: odytypes.h:40
@ TRUE
Definition: odytypes.h:39