OpenImageIO
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
sysutil.h
Go to the documentation of this file.
1 /*
2  Copyright 2008 Larry Gritz and the other authors and contributors.
3  All Rights Reserved.
4 
5  Redistribution and use in source and binary forms, with or without
6  modification, are permitted provided that the following conditions are
7  met:
8  * Redistributions of source code must retain the above copyright
9  notice, this list of conditions and the following disclaimer.
10  * Redistributions in binary form must reproduce the above copyright
11  notice, this list of conditions and the following disclaimer in the
12  documentation and/or other materials provided with the distribution.
13  * Neither the name of the software's owners nor the names of its
14  contributors may be used to endorse or promote products derived from
15  this software without specific prior written permission.
16  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 
28  (This is the Modified BSD License)
29 */
30 
31 
38 
39 
40 #ifndef OPENIMAGEIO_SYSUTIL_H
41 #define OPENIMAGEIO_SYSUTIL_H
42 
43 #include <string>
44 
45 #ifdef __MINGW32__
46 #include <malloc.h> // for alloca
47 #endif
48 
49 #include "export.h"
50 #include "version.h"
51 
53 #define ALLOCA(type, size) ((type*)alloca((size) * sizeof (type)))
54 
55 
56 // Define a macro that can be used for memory alignment.
57 // I think that in a future world of C++1x compatibility, all these can
58 // be replaced with [[ align(size) ]].
59 #if defined (_MSC_VER)
60 # define OIIO_ALIGN(size) __declspec(align(size))
61 #elif defined (__GNUC__)
62 # define OIIO_ALIGN(size) __attribute__((aligned(size)))
63 #elif defined (__INTEL_COMPILER)
64 # define OIIO_ALIGN(size) __declspec(align((size)))
65 #else
66 # error "Don't know how to define OIIO_ALIGN"
67 #endif
68 
69 // Cache line size is 64 on all modern x86 CPUs. If this changes or we
70 // anticipate ports to other architectures, we'll need to change this.
71 #define OIIO_CACHE_LINE_SIZE 64
72 
73 // Align the next declaration to be on its own cache line
74 #define OIIO_CACHE_ALIGN OIIO_ALIGN(OIIO_CACHE_LINE_SIZE)
75 
76 
77 
78 // gcc defines a special intrinsic to use in conditionals that can speed
79 // up extremely performance-critical spots if the conditional usually
80 // (or rarely) is true. You use it by replacing
81 // if (x) ...
82 // with
83 // if (OIIO_LIKELY(x)) ... // if you think x will usually be true
84 // or
85 // if (OIIO_UNLIKELY(x)) ... // if you think x will rarely be true
86 // Caveat: Programmers are notoriously bad at guessing this, so it
87 // should be used only with thorough benchmarking.
88 #ifdef __GNUC__
89 #define OIIO_LIKELY(x) (__builtin_expect((x), 1))
90 #define OIIO_UNLIKELY(x) (__builtin_expect((x), 0))
91 #else
92 #define OIIO_LIKELY(x) (x)
93 #define OIIO_UNLIKELY(x) (x)
94 #endif
95 
96 
97 
98 OIIO_NAMESPACE_ENTER
99 {
100 
105 namespace Sysutil {
106 
113 OIIO_API size_t memory_used (bool resident=true);
114 
115 
118 OIIO_API void get_local_time (const time_t *time, struct tm *converted_time);
119 
122 OIIO_API std::string this_program_path ();
123 
126 OIIO_API void usleep (unsigned long useconds);
127 
131 OIIO_API int terminal_columns ();
132 
137 OIIO_API bool put_in_background (int argc, char* argv[]);
138 
139 
140 } // namespace Sysutils
141 
142 }
143 OIIO_NAMESPACE_EXIT
144 
145 #endif // OPENIMAGEIO_SYSUTIL_H