source: trunk/sys/libphoenix/stddefines.h @ 285

Last change on this file since 285 was 1, checked in by alain, 7 years ago

First import

File size: 3.9 KB
Line 
1/* Copyright (c) 2007-2009, Stanford University
2* All rights reserved.
3*
4* Redistribution and use in source and binary forms, with or without
5* modification, are permitted provided that the following conditions are met:
6*     * Redistributions of source code must retain the above copyright
7*       notice, this list of conditions and the following disclaimer.
8*     * Redistributions in binary form must reproduce the above copyright
9*       notice, this list of conditions and the following disclaimer in the
10*       documentation and/or other materials provided with the distribution.
11*     * Neither the name of Stanford University nor the
12*       names of its contributors may be used to endorse or promote products
13*       derived from this software without specific prior written permission.
14*
15* THIS SOFTWARE IS PROVIDED BY STANFORD UNIVERSITY ``AS IS'' AND ANY
16* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18* DISCLAIMED. IN NO EVENT SHALL STANFORD UNIVERSITY BE LIABLE FOR ANY
19* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25*/ 
26
27#ifndef STDDEFINES_H_
28#define STDDEFINES_H_
29
30#include <assert.h>
31#include <stdlib.h>
32#include <time.h>
33#include <stdint.h>
34#include <stdio.h>
35
36#define TIMING
37#define USE_EXPLICIT_PLACEMENT   0
38
39/* Debug printf */
40#define dprintf(...) fprintf(stdout, __VA_ARGS__)
41
42/* Wrapper to check for errors */
43#define CHECK_ERROR(a)                                                  \
44  do{{                                                                  \
45      int err = (a);                                                    \
46      if(err != 0){                                                     \
47        fprintf(stderr, "Error at line %d, file %s, exp\n\t" #a "\n\terr %d\n", __LINE__, __FUNCTION__,err); \
48        assert (err == 0);                                              \
49      }                                                                 \
50    }}while(0)
51
52static inline void *MALLOC(size_t size)
53{
54   void * temp = malloc(size);
55   assert(temp);
56   return temp;
57}
58
59static inline void *CALLOC(size_t num, size_t size)
60{
61   void * temp = calloc(num, size);
62   assert(temp);
63   return temp;
64}
65
66static inline void *REALLOC(void *ptr, size_t size)
67{
68   void * temp = realloc(ptr, size);
69   assert(temp);
70   return temp;
71}
72
73static inline char *GETENV(char *envstr)
74{
75   char *env = getenv(envstr);
76   if (env == NULL) return "0";
77   else return env;
78}
79
80#define GET_TIME(start, end, duration)                                     \
81  duration.tv_sec = (end.tv_sec - start.tv_sec);                           
82/*
83   if (end.tv_nsec >= start.tv_nsec) {                                     \
84      duration.tv_nsec = (end.tv_nsec - start.tv_nsec);                    \
85   }                                                                       \
86   else {                                                                  \
87      duration.tv_nsec = (1000000000L - (start.tv_nsec - end.tv_nsec));    \
88      duration.tv_sec--;                                                   \
89   }                                                                       \
90   if (duration.tv_nsec >= 1000000000L) {                                  \
91      duration.tv_sec++;                                                   \
92      duration.tv_nsec -= 1000000000L;                                     \
93   }
94*/
95
96static inline unsigned int time_diff (
97    struct timeval *end, struct timeval *begin)
98{
99#ifdef TIMING
100    uint64_t result;
101
102    result = end->tv_sec - begin->tv_sec;
103   // result *= 1000000;     /* usec */
104   // result += end->tv_usec - begin->tv_usec;
105
106    return result;
107#else
108    return 0;
109#endif
110}
111
112static inline void get_time (struct timeval *t)
113{
114#ifdef TIMING
115    uint64_t c;
116    c = clock();
117    t->tv_sec = c;
118    t->tv_usec = 0;
119//    gettimeofday (t, NULL);
120#endif
121}
122
123#endif // STDDEFINES_H_
Note: See TracBrowser for help on using the repository browser.