source: trunk/src/testing/app/FileViewer/ImageViewer/util.cpp @ 4

Revision 4, 5.2 KB checked in by ajaworski, 13 years ago (diff)

Added modified SAGE sources

Line 
1/******************************************************************************
2 * Fast DXT - a realtime DXT compression tool
3 *
4 * Author : Luc Renambot
5 *
6 * Copyright (C) 2007 Electronic Visualization Laboratory,
7 * University of Illinois at Chicago
8 *
9 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions are met:
13 *
14 *  * Redistributions of source code must retain the above copyright
15 *    notice, this list of conditions and the following disclaimer.
16 *  * Redistributions in binary form must reproduce the above
17 *    copyright notice, this list of conditions and the following disclaimer
18 *    in the documentation and/or other materials provided with the distribution.
19 *  * Neither the name of the University of Illinois at Chicago nor
20 *    the names of its contributors may be used to endorse or promote
21 *    products derived from this software without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
27 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 *
35 * Direct questions, comments etc about SAGE to http://www.evl.uic.edu/cavern/forum/
36 *
37 *****************************************************************************/
38
39#include "util.h"
40
41
42#if defined(WIN32)
43#include <io.h>
44LARGE_INTEGER perf_freq;
45LARGE_INTEGER perf_start;
46HANDLE win_err; // stderr in a console
47#elif defined(__APPLE__)
48#include <mach/mach_time.h>
49static double perf_conversion = 0.0;
50static uint64_t perf_start;
51#else
52struct timeval tv_start;
53#endif
54
55#if !defined(WIN32)
56#include <sys/stat.h>
57#include <unistd.h>
58#endif
59
60#include <fcntl.h>
61
62
63void aInitialize()
64{
65#if defined(WIN32)
66        QueryPerformanceCounter(&perf_start);
67        QueryPerformanceFrequency(&perf_freq);
68        AllocConsole();
69        win_err =  GetStdHandle(STD_ERROR_HANDLE);
70#elif defined(__APPLE__)
71        if( perf_conversion == 0.0 )
72        {
73                mach_timebase_info_data_t info;
74                kern_return_t err = mach_timebase_info( &info );
75
76                //Convert the timebase into seconds
77                if( err == 0  )
78                        perf_conversion = 1e-9 * (double) info.numer / (double) info.denom;
79        }
80                // Start of time
81        perf_start = mach_absolute_time();
82
83                // Initialize the random generator
84        srand(getpid());
85#else
86                // Start of time
87        gettimeofday(&tv_start,0);
88                // Initialize the random generator
89        srand(getpid());
90#endif
91}
92
93double aTime()
94// return time since start of process in seconds
95{
96#if defined(WIN32)
97    LARGE_INTEGER perf_counter;
98#else
99    struct timeval tv;
100#endif
101
102#if defined(WIN32)
103        // Windows: get performance counter and subtract starting mark
104        QueryPerformanceCounter(&perf_counter);
105        return (double)(perf_counter.QuadPart - perf_start.QuadPart) / (double)perf_freq.QuadPart;
106#elif defined(__APPLE__)
107    uint64_t difference = mach_absolute_time() - perf_start;
108    return perf_conversion * (double) difference;
109#else
110        // UNIX: gettimeofday
111        gettimeofday(&tv,0);
112        return (double)(tv.tv_sec - tv_start.tv_sec) + (double)(tv.tv_usec - tv_start.tv_usec) / 1000000.0;
113#endif
114}
115
116
117
118void aLog(char* format,...)
119{
120        va_list vl;
121        char line[2048];
122
123        va_start(vl,format);
124        vsprintf(line,format,vl);
125        va_end(vl);
126
127#if defined(WIN32)
128        DWORD res;
129        WriteFile(win_err, line, (DWORD)strlen(line), &res, NULL);
130#endif
131        fprintf(stderr,"%s",line);
132        fflush(stderr);
133}
134
135void aError(const char* format,...)
136{
137        va_list vl;
138        char line[2048];
139
140        va_start(vl,format);
141        vsprintf(line,format,vl);
142        va_end(vl);
143
144#if defined(WIN32)
145        DWORD res;
146        WriteFile(win_err, line, (DWORD)strlen(line), &res, NULL);
147#endif
148        fprintf(stderr,"%s",line);
149        fflush(stderr);
150
151    exit(1);
152}
153
154
155
156void* aAlloc(size_t const n)
157{
158        void* result;
159#if defined(WIN32)
160        result = LocalAlloc(0,n);
161#else
162        result = malloc(n);
163#endif
164        // Filling with zeros
165        memset(result, 0, n);
166
167        if(!result)
168                aError("Aura: not enough memory for %d bytes",n);
169        return result;
170}
171
172void aFree(void* const p)
173{
174#if defined(WIN32)
175        LocalFree(p);
176#else
177        if (p)
178        free(p);
179    else
180        aError("Alloc> Trying to free a NULL pointer\n");
181#endif
182}
183
184#if defined(WIN32)
185float
186drand48(void)
187{
188        return (((float) rand()) / RAND_MAX);
189}
190#endif
191
192
193void *aligned_malloc(size_t size, size_t align_size) {
194
195  char *ptr,*ptr2,*aligned_ptr;
196  int align_mask = (int)align_size - 1;
197
198  ptr=(char *)malloc(size + align_size + sizeof(int));
199  if(ptr==NULL) return(NULL);
200
201  ptr2 = ptr + sizeof(int);
202  aligned_ptr = ptr2 + (align_size - ((size_t)ptr2 & align_mask));
203
204
205  ptr2 = aligned_ptr - sizeof(int);
206  *((int *)ptr2)=(int)(aligned_ptr - ptr);
207
208  return(aligned_ptr);
209}
210
211void aligned_free(void *ptr)
212{
213        int *ptr2=(int *)ptr - 1;
214        ptr = (char*)ptr - *ptr2;
215        free(ptr);
216}
217
218
Note: See TracBrowser for help on using the repository browser.