-
Notifications
You must be signed in to change notification settings - Fork 0
/
basic.c
125 lines (107 loc) · 3.6 KB
/
basic.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/**
* @file basic.c
* @brief Memory management, portable types, math constants, and timing
* @author Pascal Getreuer <[email protected]>
*
* This file implements a function Clock, a timer with millisecond
* precision. In order to obtain timing at high resolution, platform-
* specific functions are needed:
*
* - On Windows systems, the GetSystemTime function is used.
* - On POSIX systems, the gettimeofday function is used.
*
* Otherwise as a fallback, time.h time is used, and in this case Clock has
* only second accuracy. This file attempts to detect whether the platform
* is POSIX or Windows and defines Clock accordingly. A particular
* implementation can be forced by defining USE_GETSYSTEMTIME,
* USE_GETTIMEOFDAY, or USE_TIME.
*
*
* Copyright (c) 2010-2011, Pascal Getreuer
* All rights reserved.
*
* This program is free software: you can use, modify and/or
* redistribute it under the terms of the simplified BSD License. You
* should have received a copy of this license along this program. If
* not, see <http://www.opensource.org/licenses/bsd-license.html>.
*/
#include <stdlib.h>
#include <stdarg.h>
#include "basic.h"
/* Autodetect whether to use Windows, POSIX,
or fallback implementation for Clock. */
#if !defined(USE_GETSYSTEMTIME) && !defined(USE_GETTIMEOFDAY) && !defined(USE_TIME)
# if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
# define USE_GETSYSTEMTIME
# elif defined(unix) || defined(__unix__) || defined(__unix)
# include <unistd.h>
# if (_POSIX_TIMERS) || (_POSIX_VERSION >= 200112L)
# define USE_GETTIMEOFDAY
# endif
# endif
#endif
/* Define Clock(), get the system clock in milliseconds */
#if defined(USE_GETSYSTEMTIME)
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
unsigned long Clock() /* Windows implementation */
{
static SYSTEMTIME TimeVal;
GetSystemTime(&TimeVal);
return (unsigned long)((unsigned long)TimeVal.wMilliseconds
+ 1000*((unsigned long)TimeVal.wSecond
+ 60*((unsigned long)TimeVal.wMinute
+ 60*((unsigned long)TimeVal.wHour
+ 24*(unsigned long)TimeVal.wDay))));
}
#elif defined(USE_GETTIMEOFDAY)
#include <unistd.h>
#include <sys/time.h>
unsigned long Clock() /* POSIX implementation */
{
struct timeval TimeVal;
gettimeofday(&TimeVal, NULL);
return (unsigned long)(TimeVal.tv_usec/1000 + TimeVal.tv_sec*1000);
}
#else
#include <time.h>
unsigned long Clock() /* Fallback implementation */
{
time_t RawTime;
struct tm *TimeVal;
time(&RawTime);
TimeVal = localtime(&RawTime);
return (unsigned long)(1000*((unsigned long)TimeVal->tm_sec
+ 60*((unsigned long)TimeVal->tm_min
+ 60*((unsigned long)TimeVal->tm_hour
+ 24*(unsigned long)TimeVal->tm_mday))));
}
#endif
/** @brief malloc with an error message on failure. */
void *MallocWithErrorMessage(size_t Size)
{
void *Ptr;
if(!(Ptr = malloc(Size)))
ErrorMessage("Memory allocation of %u bytes failed.\n", Size);
return Ptr;
}
/** @brief realloc with an error message and free on failure. */
void *ReallocWithErrorMessage(void *Ptr, size_t Size)
{
void *NewPtr;
if(!(NewPtr = realloc(Ptr, Size)))
{
ErrorMessage("Memory reallocation of %u bytes failed.\n", Size);
Free(Ptr); /* Free the previous block on failure */
}
return NewPtr;
}
/** @brief Redefine this function to customize error messages. */
void ErrorMessage(const char *Format, ...)
{
va_list Args;
va_start(Args, Format);
/* Write a formatted error message to stderr */
vfprintf(stderr, Format, Args);
va_end(Args);
}