UTMisc.h

Go to the documentation of this file.
00001 //==================================================================================================
00002 // Copyright (C) 2010  Brian Tietz    sdbtietz at yahoo dot com
00003 //
00004 // This program is free software; you can redistribute it and/or modify it under the terms of the
00005 // GNU General Public License as published by the Free Software Foundation, version 2.0 of the
00006 // License.
00007 //
00008 // This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
00009 // even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00010 // General Public License for more details.
00011 //
00012 // You should have received a copy of the GNU General Public License along with this program; if
00013 // not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00014 // 02110-1301, USA.
00015 //
00016 // For commercial software, the copyright holder (Brian Tietz, email sdbtietz at yahoo dot com)
00017 // reserves the right and is willing to waive the proprietary source code disclosure aspects of that
00018 // license as applied to the UT library in exchange for either substantial contributions to the
00019 // development of the UT library or other forms of compensation.  Any such waiver must be
00020 // established in writing between the copyright holder and the commercial entity obtaining such a
00021 // waiver.
00022 //==================================================================================================
00023 
00024 
00025 #ifndef _UT_H_
00026 #error "DO NOT INCLUDE THIS FILE - INCLUDE UT.h INSTEAD"
00027 #endif
00028 
00029 
00030 EXTERN_C_BEGIN
00031 
00032 
00033 //==================================================================================================
00034 //=== Basic types
00035 //==================================================================================================
00036 
00038 typedef unsigned int        uint;
00039 
00041 typedef unsigned char       uint8;
00042 
00044 typedef signed char         int8;
00045 
00047 typedef unsigned short int  uint16;
00048 
00050 typedef signed short int    int16;
00051 
00053 typedef unsigned int        uint32;     // Note: long int is 64-bits with gcc for x86_64
00054 
00056 typedef signed int          int32;      // Note: long int is 64-bits with gcc for x86_64
00057 
00059 typedef unsigned long long  uint64;
00060 
00062 typedef signed long long    int64;
00063 
00065 typedef uint8               byte;
00066 
00068 typedef uint8               ascii7;
00069 
00072 typedef uint8               utf8;
00073 
00077 typedef const char          stringliteral;
00078 
00079 #if POINTER_IS_32_BIT
00081 typedef uint32              ptr_as_uint_t;
00082 #elif POINTER_IS_64_BIT
00083 typedef uint64              ptr_as_uint_t;
00084 #else
00085 #error "UT target platform header must define POINTER_IS_32_BIT or POINTER_IS_64_BIT"
00086 #endif
00087 
00088 
00089 //==================================================================================================
00090 //=== Simple value manipulation macros
00091 //==================================================================================================
00092 
00093 #ifndef min
00095 #define min( a, b )                                     ( ( (a) < (b) ) ? (a) : (b) )
00096 #endif
00097 
00098 #ifndef max
00100 #define max( a, b )                                     ( ( (a) > (b) ) ? (a) : (b) )
00101 #endif
00102 
00103 #ifndef abs
00105 #define abs(value)                                      ( ( (value) > 0 ) ? (value) : -(value) )
00106 #endif
00107 
00109 #define div_round( numer, denom )                       ( ( (numer) + (denom)/2 ) / (denom) )
00110 
00112 #define div_round_up( numer, denom )                    ( ( (numer) + (denom) - 1 ) / (denom) )
00113 
00115 #define four_cc( a, b, c, d )                           ( (a<<24) | (b<<16) | (c<<8) | (d) )
00116 
00119 #define sizeof_array_element(object)                    (sizeof(object[2]) / 2)
00120 
00121 
00122 //==================================================================================================
00123 //=== Self documentation aids
00124 //==================================================================================================
00125 
00129 #define takes
00130 
00134 #define gives
00135 
00138 #define out
00139 
00140 
00141 //==================================================================================================
00142 //=== Convenience macros for typical mode_t uses
00143 //==================================================================================================
00144 
00146 #define S_IRWUSR (S_IRUSR | S_IWUSR)
00147 
00149 #define S_IRWGRP (S_IRGRP | S_IWGRP)
00150 
00152 #define S_IRWOTH (S_IROTH | S_IWOTH)
00153 
00154 
00155 //==================================================================================================
00156 //=== Cleaner way to distinguish prefix and postfix (++i or i++) with operator overloading
00157 //==================================================================================================
00158 
00167 #define postfix int
00168 
00169 
00170 //==================================================================================================
00171 //=== System performance information
00172 //==================================================================================================
00173 
00217 extern UT_EXPORT void get_memory_info(  out uint32* total_phys_kb,
00218                                         out uint32* total_virt_kb,
00219                                         out uint32* free_phys_kb,
00220                                         out uint32* free_virt_kb );
00221 
00235 extern UT_EXPORT void get_cpu_utilization( out int* highest_of_any_one_core, out int* aggregate );
00236 
00237 
00238 //==================================================================================================
00239 //=== Improved time functions
00240 //==================================================================================================
00241 
00245 typedef struct
00246 {
00247     uint16 year;            
00248     uint8  month;           
00249     uint8  day_of_week;     
00250     uint8  day;             
00251     uint8  hour;            
00252     uint8  minute;          
00253     uint8  second;          
00254     uint32 microsecond;     
00255 } DateTime_t;
00256 
00257 
00260 extern UT_EXPORT void get_date_time(out DateTime_t* date_time);
00261 
00264 typedef int64 ustime_t;
00265 
00268 extern UT_EXPORT ustime_t   system_time_us();
00269 
00273 typedef int32 mstime_t;
00274 
00277 extern UT_EXPORT mstime_t   system_time_ms();
00278 
00283 typedef int32 roughtime_t;
00284 
00289 extern UT_EXPORT roughtime_t    rough_time_ms();
00290 
00291 
00292 //==================================================================================================
00293 //=== Synchronization utilities
00294 //==================================================================================================
00295 
00304 extern UT_EXPORT int32 atomic_add( volatile int32* value_pointer, int32 add );
00305 
00315 extern UT_EXPORT bool atomic_add_if_greater(    volatile int32* value_pointer,
00316                                                 int32 add_value,
00317                                                 int32 if_greater_than );
00318 
00326 extern UT_EXPORT bool atomic_add_if_less(   volatile int32* value_pointer,
00327                                             int32 add_value,
00328                                             int32 if_less_than );
00329 
00330 
00331 //==================================================================================================
00332 //=== Automatically seeded random number generator
00333 //==================================================================================================
00334 
00337 extern UT_EXPORT int seeded_rand15();
00338 
00339 
00343 extern UT_EXPORT int32 seeded_rand(int bits);
00344 
00345 
00348 extern UT_EXPORT int32 seeded_rand_range( int32 from_val, int32 to_val );
00349 
00350 
00353 extern UT_EXPORT int64 seeded_rand_range64( int64 from_val, int64 to_val );
00354 
00355 
00356 //==================================================================================================
00357 //=== Case-insensitive string functions, but which are safe with international characters
00358 //==================================================================================================
00359 
00366 extern UT_EXPORT int stricmp_utf8( stringliteral* a, stringliteral* b );
00367 
00368 
00375 extern UT_EXPORT int strnicmp_utf8( stringliteral* a, stringliteral* b, int len );
00376 
00377 
00385 extern UT_EXPORT stringliteral* stristr_utf8( stringliteral* haystack, stringliteral* needle );
00386 
00387 
00396 extern UT_EXPORT stringliteral* strnistr_utf8(  stringliteral* haystack,
00397                                                 int haystack_length,
00398                                                 stringliteral* needle,
00399                                                 int needle_length );
00400 
00401 
00402 //==================================================================================================
00403 //=== Bit masking, counting, and flipping functions
00404 //==================================================================================================
00405 
00408 extern UT_EXPORT uint8  mask_ls_bits_on8(uint on_bit_count);
00409 
00412 extern UT_EXPORT uint16 mask_ls_bits_on16(uint on_bit_count);
00413 
00416 extern UT_EXPORT uint32 mask_ls_bits_on32(uint on_bit_count);
00417 
00420 extern UT_EXPORT uint64 mask_ls_bits_on64(uint on_bit_count);
00421 
00424 extern UT_EXPORT uint8  mask_ms_bits_on8(uint on_bit_count);
00425 
00428 extern UT_EXPORT uint16 mask_ms_bits_on16(uint on_bit_count);
00429 
00432 extern UT_EXPORT uint32 mask_ms_bits_on32(uint on_bit_count);
00433 
00436 extern UT_EXPORT uint64 mask_ms_bits_on64(uint on_bit_count);
00437 
00439 extern UT_EXPORT uint   count_bits_on8(uint8 value);
00440 
00442 extern UT_EXPORT uint   count_bits_on16(uint16 value);
00443 
00445 extern UT_EXPORT uint   count_bits_on32(uint32 value);
00446 
00448 extern UT_EXPORT uint   count_bits_on64(uint64 value);
00449 
00452 extern UT_EXPORT int    ls_on_bit_index8(uint8 value);
00453 
00456 extern UT_EXPORT int    ls_on_bit_index16(uint16 value);
00457 
00460 extern UT_EXPORT int    ls_on_bit_index32(uint32 value);
00461 
00464 extern UT_EXPORT int    ls_on_bit_index64(uint64 value);
00465 
00468 extern UT_EXPORT int    ms_on_bit_index8(uint8 value);
00469 
00472 extern UT_EXPORT int    ms_on_bit_index16(uint16 value);
00473 
00476 extern UT_EXPORT int    ms_on_bit_index32(uint32 value);
00477 
00480 extern UT_EXPORT int    ms_on_bit_index64(uint64 value);
00481 
00484 extern UT_EXPORT uint8  flip_bit_order8(uint8 value);
00485 
00488 extern UT_EXPORT uint16 flip_bit_order16(uint16 value);
00489 
00492 extern UT_EXPORT uint32 flip_bit_order32(uint32 value);
00493 
00496 extern UT_EXPORT uint64 flip_bit_order64(uint64 value);
00497 
00498 
00499 EXTERN_C_END

Generated on Tue Dec 14 22:35:05 2010 for UT library by  doxygen 1.6.1