Image Interpolation with Contour Stencils
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros
basic.c
Go to the documentation of this file.
1 
29 #include <stdlib.h>
30 #include <stdarg.h>
31 #include "basic.h"
32 
33 
34 /* Autodetect whether to use Windows, POSIX,
35  or fallback implementation for Clock. */
36 #if !defined(USE_GETSYSTEMTIME) && !defined(USE_GETTIMEOFDAY) && !defined(USE_TIME)
37 # if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
38 # define USE_GETSYSTEMTIME
39 # elif defined(unix) || defined(__unix__) || defined(__unix)
40 # include <unistd.h>
41 # if (_POSIX_TIMERS) || (_POSIX_VERSION >= 200112L)
42 # define USE_GETTIMEOFDAY
43 # endif
44 # endif
45 #endif
46 
47 /* Define Clock(), get the system clock in milliseconds */
48 #if defined(USE_GETSYSTEMTIME)
49 #define WIN32_LEAN_AND_MEAN
50 #include <windows.h>
51 
52 unsigned long Clock() /* Windows implementation */
53 {
54  static SYSTEMTIME TimeVal;
55  GetSystemTime(&TimeVal);
56  return (unsigned long)((unsigned long)TimeVal.wMilliseconds
57  + 1000*((unsigned long)TimeVal.wSecond
58  + 60*((unsigned long)TimeVal.wMinute
59  + 60*((unsigned long)TimeVal.wHour
60  + 24*(unsigned long)TimeVal.wDay))));
61 }
62 #elif defined(USE_GETTIMEOFDAY)
63 #include <unistd.h>
64 #include <sys/time.h>
65 
66 unsigned long Clock() /* POSIX implementation */
67 {
68  struct timeval TimeVal;
69  gettimeofday(&TimeVal, NULL);
70  return (unsigned long)(TimeVal.tv_usec/1000 + TimeVal.tv_sec*1000);
71 }
72 #else
73 #include <time.h>
74 
75 unsigned long Clock() /* Fallback implementation */
76 {
77  time_t RawTime;
78  struct tm *TimeVal;
79  time(&RawTime);
80  TimeVal = localtime(&RawTime);
81  return (unsigned long)(1000*((unsigned long)TimeVal->tm_sec
82  + 60*((unsigned long)TimeVal->tm_min
83  + 60*((unsigned long)TimeVal->tm_hour
84  + 24*(unsigned long)TimeVal->tm_mday))));
85 }
86 #endif
87 
88 
90 void *MallocWithErrorMessage(size_t Size)
91 {
92  void *Ptr;
93 
94  if(!(Ptr = malloc(Size)))
95  ErrorMessage("Memory allocation of %u bytes failed.\n", Size);
96 
97  return Ptr;
98 }
99 
100 
102 void *ReallocWithErrorMessage(void *Ptr, size_t Size)
103 {
104  void *NewPtr;
105 
106  if(!(NewPtr = realloc(Ptr, Size)))
107  {
108  ErrorMessage("Memory reallocation of %u bytes failed.\n", Size);
109  Free(Ptr); /* Free the previous block on failure */
110  }
111 
112  return NewPtr;
113 }
114 
115 
117 void ErrorMessage(const char *Format, ...)
118 {
119  va_list Args;
120 
121  va_start(Args, Format);
122  /* Write a formatted error message to stderr */
123  vfprintf(stderr, Format, Args);
124  va_end(Args);
125 }