1 /*
2     Copyright (c) 2005-2021 Intel Corporation
3 
4     Licensed under the Apache License, Version 2.0 (the "License");
5     you may not use this file except in compliance with the License.
6     You may obtain a copy of the License at
7 
8         http://www.apache.org/licenses/LICENSE-2.0
9 
10     Unless required by applicable law or agreed to in writing, software
11     distributed under the License is distributed on an "AS IS" BASIS,
12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13     See the License for the specific language governing permissions and
14     limitations under the License.
15 */
16 
17 /*
18     The original source for this example is
19     Copyright (c) 1994-2008 John E. Stone
20     All rights reserved.
21 
22     Redistribution and use in source and binary forms, with or without
23     modification, are permitted provided that the following conditions
24     are met:
25     1. Redistributions of source code must retain the above copyright
26        notice, this list of conditions and the following disclaimer.
27     2. Redistributions in binary form must reproduce the above copyright
28        notice, this list of conditions and the following disclaimer in the
29        documentation and/or other materials provided with the distribution.
30     3. The name of the author may not be used to endorse or promote products
31        derived from this software without specific prior written permission.
32 
33     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
34     OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
35     WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
36     ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
37     DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
38     DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
39     OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
40     HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
41     LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
42     OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
43     SUCH DAMAGE.
44 */
45 
46 /*
47  * util.cpp - Contains all of the timing functions for various platforms.
48  */
49 
50 #include "machine.hpp"
51 #include "types.hpp"
52 #include "macros.hpp"
53 #include "util.hpp"
54 #include "light.hpp"
55 #include "global.hpp"
56 #include "ui.hpp"
57 
58 void rt_finalize(void);
59 
60 #if !defined(_WIN32)
61 #include <sys/time.h>
62 #include <unistd.h>
63 
64 void rt_sleep(int msec) {
65     usleep(msec * 1000);
66 }
67 
68 #else //_WIN32
69 
70 #undef OLDUNIXTIME
71 #undef STDTIME
72 
73 void rt_sleep(int msec) {
74 #if !WIN8UI_EXAMPLE
75     Sleep(msec);
76 #else
77     std::chrono::milliseconds sleep_time(msec);
78     std::this_thread::sleep_for(sleep_time);
79 #endif
80 }
81 
82 timer gettimer(void) {
83     return GetTickCount();
84 }
85 
86 flt timertime(timer st, timer fn) {
87     double ttime, start, end;
88 
89     start = ((double)st) / ((double)1000.00);
90     end = ((double)fn) / ((double)1000.00);
91     ttime = end - start;
92 
93     return ttime;
94 }
95 #endif /*  _WIN32  */
96 
97 /* if we're on a Unix with gettimeofday() we'll use newer timers */
98 #if defined(STDTIME)
99 struct timezone tz;
100 
101 timer gettimer(void) {
102     timer t;
103     gettimeofday(&t, &tz);
104     return t;
105 }
106 
107 flt timertime(timer st, timer fn) {
108     double ttime, start, end;
109 
110     start = (st.tv_sec + 1.0 * st.tv_usec / 1000000.0);
111     end = (fn.tv_sec + 1.0 * fn.tv_usec / 1000000.0);
112     ttime = end - start;
113 
114     return ttime;
115 }
116 #endif /*  STDTIME  */
117 
118 /* use the old fashioned Unix time functions */
119 #if defined(OLDUNIXTIME)
120 timer gettimer(void) {
121     return time(nullptr);
122 }
123 
124 flt timertime(timer st, timer fn) {
125     return difftime(fn, st);
126     ;
127 }
128 #endif /*  OLDUNIXTIME  */
129 
130 /* random other helper utility functions */
131 int rt_meminuse(void) {
132     return rt_mem_in_use;
133 }
134 
135 void* rt_getmem(unsigned int bytes) {
136     void* mem;
137 
138     mem = malloc(bytes);
139     if (mem != nullptr) {
140         rt_mem_in_use += bytes;
141     }
142     else {
143         rtbomb("No more memory!!!!");
144     }
145     return mem;
146 }
147 
148 unsigned int rt_freemem(void* addr) {
149     unsigned int bytes;
150 
151     free(addr);
152 
153     bytes = 0;
154     rt_mem_in_use -= bytes;
155     return bytes;
156 }
157 
158 void rtbomb(const char* msg) {
159     rt_ui_message(MSG_ERR, msg);
160     rt_ui_message(MSG_ABORT, "Rendering Aborted.");
161 
162     rt_finalize();
163     std::exit(-1);
164 }
165 
166 void rtmesg(const char* msg) {
167     rt_ui_message(MSG_0, msg);
168 }
169