bump product version to 5.0.4.1
[LibreOffice.git] / sd / source / ui / remotecontrol / mDNSResponder / CommonServices.h
blob261828f98a5ceb709072a4ad18fec36988089554
1 /* -*- Mode: C; tab-width: 4 -*-
3 * Copyright (c) 1997-2004 Apple Computer, Inc. All rights reserved.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
19 /*! @header CommonServices
21 Common Services for Mac OS X, Linux, Palm, VxWorks, Windows, and Windows CE.
24 #ifndef __COMMON_SERVICES__
25 #define __COMMON_SERVICES__
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
31 #if 0
32 #pragma mark == Target ==
33 #endif
36 // Target
37 // Macintosh
38 #if ( !defined( TARGET_OS_MAC ) )
39 #if ( ( macintosh || __MACH__ ) && !KERNEL )
40 // ConditionalMacros.h in CoreServices will define this TARGET_* flag.
41 #else
42 #define TARGET_OS_MAC 0
43 #endif
44 #endif
46 #if ( !defined( TARGET_API_MAC_OSX_KERNEL ) )
47 #if ( __MACH__ && KERNEL )
48 #define TARGET_API_MAC_OSX_KERNEL 1
49 #else
50 #define TARGET_API_MAC_OSX_KERNEL 0
51 #endif
52 #endif
54 // FreeBSD
55 #if ( !defined( TARGET_OS_FREEBSD ) )
56 #if ( defined( __FreeBSD__ ) )
57 #define TARGET_OS_FREEBSD 1
58 #else
59 #define TARGET_OS_FREEBSD 0
60 #endif
61 #endif
63 // Linux
64 #if ( !defined( TARGET_OS_LINUX ) )
65 #if ( defined( __linux__ ) )
66 #define TARGET_OS_LINUX 1
67 #else
68 #define TARGET_OS_LINUX 0
69 #endif
70 #endif
72 // Solaris
73 #if ( !defined( TARGET_OS_SOLARIS ) )
74 #if ( defined(solaris) || (defined(__SVR4) && defined(sun)) )
75 #define TARGET_OS_SOLARIS 1
76 #else
77 #define TARGET_OS_SOLARIS 0
78 #endif
79 #endif
81 // Palm
82 #if ( !defined( TARGET_OS_PALM ) )
83 #if ( defined( __PALMOS_TRAPS__ ) || defined( __PALMOS_ARMLET__ ) )
84 #define TARGET_OS_PALM 1
85 #else
86 #define TARGET_OS_PALM 0
87 #endif
88 #endif
90 // VxWorks
91 #if ( !defined( TARGET_OS_VXWORKS ) )
93 // No predefined macro for VxWorks so just assume VxWorks if nothing else is set.
95 #if ( !macintosh && !__MACH__ && !defined( __FreeBSD__ ) && !defined( __linux__ ) && !defined ( __SVR4 ) && !defined ( __sun ) && !defined( __PALMOS_TRAPS__ ) && !defined( __PALMOS_ARMLET__ ) && !defined( _WIN32 ) )
96 #define TARGET_OS_VXWORKS 1
97 #else
98 #define TARGET_OS_VXWORKS 0
99 #endif
100 #endif
102 // Windows
103 #if ( !defined( TARGET_OS_WIN32 ) )
104 #if ( macintosh || __MACH__ )
105 // ConditionalMacros.h in CoreServices will define this TARGET_* flag.
106 #else
107 #if ( defined( _WIN32 ) )
108 #define TARGET_OS_WIN32 1
109 #else
110 #define TARGET_OS_WIN32 0
111 #endif
112 #endif
113 #endif
115 // Windows CE
116 #if ( !defined( TARGET_OS_WINDOWS_CE ) )
117 #if ( defined( _WIN32_WCE ) )
118 #define TARGET_OS_WINDOWS_CE 1
119 #else
120 #define TARGET_OS_WINDOWS_CE 0
121 #endif
122 #endif
124 #if 0
125 #pragma mark == Includes ==
126 #endif
129 // Includes
130 #if ( !KERNEL )
131 #if defined(WIN32) && !defined(_WSPIAPI_COUNTOF)
132 #define _WSPIAPI_COUNTOF(_Array) (sizeof(_Array) / sizeof(_Array[0]))
133 #endif
134 #include <stddef.h>
135 #endif
137 #if ( ( macintosh || __MACH__ ) && !KERNEL )
139 #if ( defined( __MWERKS__ ) )
140 #if ( __option( c9x ) )
141 #include <stdbool.h>
142 #endif
143 #else
144 #include <stdbool.h>
145 #endif
147 #include <stdint.h>
149 #if ( __MACH__ )
151 // Mac OS X
152 #include <sys/types.h>
153 #include <netinet/in.h>
154 #include <arpa/inet.h>
155 #include <fcntl.h>
156 #include <pthread.h>
157 #include <sys/ioctl.h>
158 #include <sys/socket.h>
159 #include <unistd.h>
161 #else
163 // Classic Mac OS
164 #include <ConditionalMacros.h>
165 #include <MacTypes.h>
167 #endif
169 #elif ( KERNEL )
171 // Mac OS X Kernel
172 #include <stdint.h>
174 #include <libkern/OSTypes.h>
175 #include <sys/types.h>
177 #elif ( TARGET_OS_FREEBSD )
179 // FreeBSD
180 #include <stdint.h>
181 #include <pthread.h>
182 #include <netinet/in.h>
183 #include <arpa/inet.h>
184 #include <sys/socket.h>
186 #elif ( TARGET_OS_LINUX )
188 // Linux
189 #include <stdint.h>
190 #include <arpa/inet.h>
192 #elif ( TARGET_OS_SOLARIS )
194 // Solaris
195 #include <stdint.h>
197 #include <arpa/inet.h>
198 #include <arpa/nameser.h>
200 #if ( defined( BYTE_ORDER ) && defined( LITTLE_ENDIAN ) && ( BYTE_ORDER == LITTLE_ENDIAN ) )
201 #define TARGET_RT_LITTLE_ENDIAN 1
202 #endif
203 #if ( defined( BYTE_ORDER ) && defined( BIG_ENDIAN ) && ( BYTE_ORDER == BIG_ENDIAN ) )
204 #define TARGET_RT_BIG_ENDIAN 1
205 #endif
207 #elif ( TARGET_OS_PALM )
209 // Palm (no special includes yet).
211 #elif ( TARGET_OS_VXWORKS )
213 // VxWorks
214 #include "vxWorks.h"
216 #elif ( TARGET_OS_WIN32 )
218 // Windows
219 #if ( !defined( WIN32_WINDOWS ) )
220 #define WIN32_WINDOWS 0x0401
221 #endif
223 #if ( !defined( _WIN32_WINDOWS ) )
224 #define _WIN32_WINDOWS 0x0401
225 #endif
227 #if ( !defined( WIN32_LEAN_AND_MEAN ) )
228 #define WIN32_LEAN_AND_MEAN // Needed to avoid redefinitions by Windows interfaces.
229 #endif
231 #if ( defined( __MWERKS__ ) )
233 #if ( __option( c9x ) )
234 #include <stdbool.h>
235 #endif
237 #include <stdint.h>
239 #elif ( defined( _MSC_VER ) )
241 #pragma warning( disable:4127 ) // Disable "conditional expression is constant" warning for debug macros.
243 #endif
245 #include <windows.h>
246 #include <winsock2.h>
247 #include <ws2tcpip.h>
249 #else
250 #error unknown OS - update this file to support your OS
251 #endif
253 #if ( !defined( TARGET_BUILD_MAIN ) )
254 #if ( !TARGET_OS_VXWORKS )
255 #define TARGET_BUILD_MAIN 1
256 #endif
257 #endif
259 #if ( __GNUC__ || !TARGET_OS_VXWORKS )
260 #define TARGET_LANGUAGE_C_LIKE 1
261 #else
262 #define TARGET_LANGUAGE_C_LIKE 0
263 #endif
265 #if 0
266 #pragma mark == CPU ==
267 #endif
270 // CPU
272 // PowerPC
273 #if ( !defined( TARGET_CPU_PPC ) )
274 #if ( defined( __ppc__ ) || defined( __PPC__ ) || defined( powerpc ) || defined( ppc ) || defined( _M_MPPC ) )
275 #define TARGET_CPU_PPC 1
276 #else
277 #define TARGET_CPU_PPC 0
278 #endif
279 #endif
281 // x86
282 #if ( !defined( TARGET_CPU_X86 ) )
283 #if ( __INTEL__ || defined( __i386__ ) || defined( i386 ) || defined( intel ) || defined( _M_IX86 ) )
284 #define TARGET_CPU_X86 1
285 #else
286 #define TARGET_CPU_X86 0
287 #endif
288 #endif
290 // MIPS
291 #if ( !defined( TARGET_CPU_MIPS ) )
292 #if ( __MIPS__ || defined( MIPS32 ) || defined( R3000 ) || defined( R4000 ) || defined( R4650 ) || defined( _M_MRX000 ) )
293 #define TARGET_CPU_MIPS 1
294 #else
295 #define TARGET_CPU_MIPS 0
296 #endif
297 #endif
299 #if ( !defined( TARGET_CPU_PPC ) && !defined( TARGET_CPU_X86 ) && !defined( TARGET_CPU_MIPS ) )
300 #error unknown CPU - update this file to support your CPU
301 #endif
303 #if 0
304 #pragma mark == Byte Order ==
305 #endif
308 // Byte Order
310 // TARGET_RT_LITTLE_ENDIAN
311 #if ( !defined( TARGET_RT_LITTLE_ENDIAN ) )
312 #if ( MIPSEL || IL_LITTLE_ENDIAN || defined( __LITTLE_ENDIAN__ ) || \
313 ( defined( BYTE_ORDER ) && defined( LITTLE_ENDIAN ) && ( BYTE_ORDER == LITTLE_ENDIAN ) ) || \
314 ( defined( _BYTE_ORDER ) && defined( _LITTLE_ENDIAN ) && ( _BYTE_ORDER == _LITTLE_ENDIAN ) ) || \
315 ( defined( __BYTE_ORDER ) && defined( __LITTLE_ENDIAN ) && ( __BYTE_ORDER == __LITTLE_ENDIAN ) ) || \
316 TARGET_CPU_X86 || ( defined( TARGET_RT_BIG_ENDIAN ) && !TARGET_RT_BIG_ENDIAN ) )
317 #define TARGET_RT_LITTLE_ENDIAN 1
318 #else
319 #define TARGET_RT_LITTLE_ENDIAN 0
320 #endif
321 #endif
323 // TARGET_RT_BIG_ENDIAN
324 #if ( !defined( TARGET_RT_BIG_ENDIAN ) )
325 #if ( MIPSEB || IL_BIG_ENDIAN || defined( __BIG_ENDIAN__ ) || \
326 ( defined( BYTE_ORDER ) && defined( BIG_ENDIAN ) && ( BYTE_ORDER == BIG_ENDIAN ) ) || \
327 ( defined( _BYTE_ORDER ) && defined( _BIG_ENDIAN ) && ( _BYTE_ORDER == _BIG_ENDIAN ) ) || \
328 ( defined( __BYTE_ORDER ) && defined( __BIG_ENDIAN ) && ( __BYTE_ORDER == __BIG_ENDIAN ) ) || \
329 ( defined( TARGET_RT_LITTLE_ENDIAN ) && !TARGET_RT_LITTLE_ENDIAN ) )
330 #define TARGET_RT_BIG_ENDIAN 1
331 #else
332 #define TARGET_RT_BIG_ENDIAN 0
333 #endif
334 #endif
336 #if ( defined( TARGET_RT_LITTLE_ENDIAN ) && !defined( TARGET_RT_BIG_ENDIAN ) )
337 #if ( TARGET_RT_LITTLE_ENDIAN )
338 #define TARGET_RT_BIG_ENDIAN 0
339 #else
340 #define TARGET_RT_BIG_ENDIAN 1
341 #endif
342 #endif
344 #if ( defined( TARGET_RT_BIG_ENDIAN ) && !defined( TARGET_RT_LITTLE_ENDIAN ) )
345 #if ( TARGET_RT_BIG_ENDIAN )
346 #define TARGET_RT_LITTLE_ENDIAN 0
347 #else
348 #define TARGET_RT_LITTLE_ENDIAN 1
349 #endif
350 #endif
352 #if ( !defined( TARGET_RT_LITTLE_ENDIAN ) || !defined( TARGET_RT_BIG_ENDIAN ) )
353 #error unknown byte order - update this file to support your byte order
354 #endif
356 // TARGET_RT_BYTE_ORDER
357 #if ( !defined( TARGET_RT_BYTE_ORDER_BIG_ENDIAN ) )
358 #define TARGET_RT_BYTE_ORDER_BIG_ENDIAN 1234
359 #endif
361 #if ( !defined( TARGET_RT_BYTE_ORDER_LITTLE_ENDIAN ) )
362 #define TARGET_RT_BYTE_ORDER_LITTLE_ENDIAN 4321
363 #endif
365 #if ( !defined( TARGET_RT_BYTE_ORDER ) )
366 #if ( TARGET_RT_LITTLE_ENDIAN )
367 #define TARGET_RT_BYTE_ORDER TARGET_RT_BYTE_ORDER_LITTLE_ENDIAN
368 #else
369 #define TARGET_RT_BYTE_ORDER TARGET_RT_BYTE_ORDER_BIG_ENDIAN
370 #endif
371 #endif
373 #if 0
374 #pragma mark == Constants ==
375 #endif
378 // Constants
379 #if ( !TARGET_OS_MAC )
380 #define CR '\r'
381 #endif
383 #define LF '\n'
384 #define CRLF "\r\n"
386 #if 0
387 #pragma mark == Compatibility ==
388 #endif
391 // Compatibility
393 // Macros to allow the same code to work on Windows and other sockets API-compatible platforms.
394 #if ( TARGET_OS_WIN32 )
395 #define close_compat( X ) closesocket( X )
396 #define errno_compat() (int) GetLastError()
397 #define set_errno_compat( X ) SetLastError( X )
398 #define EWOULDBLOCK_compat WSAEWOULDBLOCK
399 #define ETIMEDOUT_compat WSAETIMEDOUT
400 #define ENOTCONN_compat WSAENOTCONN
401 #define IsValidSocket( X ) ( ( X ) != INVALID_SOCKET )
402 #define kInvalidSocketRef INVALID_SOCKET
403 #if ( TARGET_LANGUAGE_C_LIKE )
404 typedef SOCKET SocketRef;
405 #endif
406 #else
407 #define close_compat( X ) close( X )
408 #define errno_compat() errno
409 #define set_errno_compat( X ) do { errno = ( X ); } while( 0 )
410 #define EWOULDBLOCK_compat EWOULDBLOCK
411 #define ETIMEDOUT_compat ETIMEDOUT
412 #define ENOTCONN_compat ENOTCONN
413 #define IsValidSocket( X ) ( ( X ) >= 0 )
414 #define kInvalidSocketRef -1
415 #if ( TARGET_LANGUAGE_C_LIKE )
416 typedef int SocketRef;
417 #endif
418 #endif
420 // socklen_t is not defined on the following platforms so emulate it if not defined:
422 // - Pre-Panther Mac OS X. Panther defines SO_NOADDRERR so trigger off that.
423 // - Windows SDK prior to 2003. 2003+ SDK's define EAI_AGAIN so trigger off that.
424 // - VxWorks
426 #if ( TARGET_LANGUAGE_C_LIKE )
427 #if ( ( TARGET_OS_MAC && !defined( SO_NOADDRERR ) ) || ( TARGET_OS_WIN32 && !defined( EAI_AGAIN ) ) || TARGET_OS_VXWORKS )
428 typedef int socklen_t;
429 #endif
430 #endif
432 // ssize_t is not defined on the following platforms so emulate it if not defined:
434 // - Mac OS X when not building with BSD headers
435 // - Windows
437 #if ( TARGET_LANGUAGE_C_LIKE )
438 #if ( !defined(_SSIZE_T) && ( TARGET_OS_WIN32 || !defined( _BSD_SSIZE_T_DEFINED_ ) ) && !TARGET_OS_FREEBSD && !TARGET_OS_LINUX && !TARGET_OS_VXWORKS && !TARGET_OS_MAC && !defined (__MINGW32__))
439 typedef int ssize_t;
440 #endif
441 #endif
443 // sockaddr_storage is not supported on non-IPv6 machines so alias it to an IPv4-compatible structure.
445 #if ( TARGET_LANGUAGE_C_LIKE )
446 #if ( !defined( AF_INET6 ) )
447 #define sockaddr_storage sockaddr_in
448 #define ss_family sin_family
449 #endif
450 #endif
453 /*! @defined SOCKADDR_IS_IP_LOOPBACK
455 @abstract Determines if a sockaddr is an IPv4 or IPv6 loopback address (if IPv6 is supported).
457 #if ( defined( AF_INET6 ) )
458 #define SOCKADDR_IS_IP_LOOPBACK( SA ) \
459 ( ( (const struct sockaddr *)( SA ) )->sa_family == AF_INET ) \
460 ? ( ( (const struct sockaddr_in *)( SA ) )->sin_addr.s_addr == htonl( INADDR_LOOPBACK ) ) \
461 : ( ( (const struct sockaddr *)( SA ) )->sa_family == AF_INET6 ) \
462 ? IN6_IS_ADDR_LOOPBACK( &( (const struct sockaddr_in6 *)( SA ) )->sin6_addr ) \
464 #else
465 #define SOCKADDR_IS_IP_LOOPBACK( SA ) \
466 ( ( (const struct sockaddr *)( SA ) )->sa_family == AF_INET ) \
467 ? ( ( (const struct sockaddr_in *)( SA ) )->sin_addr.s_addr == htonl( INADDR_LOOPBACK ) ) \
469 #endif
472 /*! @defined SOCKADDR_IS_IP_LINK_LOCAL
474 @abstract Determines if a sockaddr is an IPv4 or IPv6 link-local address (if IPv6 is supported).
476 #if ( defined( AF_INET6 ) )
477 #define SOCKADDR_IS_IP_LINK_LOCAL( SA ) \
478 ( ( ( (const struct sockaddr *)( SA ) )->sa_family == AF_INET ) \
479 ? ( ( ( (uint8_t *)( &( (const struct sockaddr_in *)( SA ) )->sin_addr ) )[ 0 ] == 169 ) && \
480 ( ( (uint8_t *)( &( (const struct sockaddr_in *)( SA ) )->sin_addr ) )[ 1 ] == 254 ) ) \
481 : IN6_IS_ADDR_LOOPBACK( &( (const struct sockaddr_in6 *)( SA ) )->sin6_addr ) )
482 #else
483 #define SOCKADDR_IS_IP_LINK_LOCAL( SA ) \
484 ( ( ( (const struct sockaddr *)( SA ) )->sa_family == AF_INET ) \
485 ? ( ( ( (uint8_t *)( &( (const struct sockaddr_in *)( SA ) )->sin_addr ) )[ 0 ] == 169 ) && \
486 ( ( (uint8_t *)( &( (const struct sockaddr_in *)( SA ) )->sin_addr ) )[ 1 ] == 254 ) ) \
487 : 0 )
488 #endif
490 // _beginthreadex and _endthreadex are not supported on Windows CE 2.1 or later (the C runtime issues with leaking
491 // resources have apparently been resolved and they seem to have just ripped out support for the API) so map it to
492 // CreateThread on Windows CE.
493 #if ( TARGET_OS_WINDOWS_CE )
494 #define _beginthreadex_compat( SECURITY_PTR, STACK_SIZE, START_ADDRESS, ARG_LIST, FLAGS, THREAD_ID_PTR ) \
495 (uintptr_t) CreateThread( SECURITY_PTR, STACK_SIZE, (LPTHREAD_START_ROUTINE) START_ADDRESS, ARG_LIST, FLAGS, \
496 (LPDWORD) THREAD_ID_PTR )
498 #define _endthreadex_compat( RESULT ) ExitThread( (DWORD) RESULT )
499 #elif ( TARGET_OS_WIN32 )
500 #define _beginthreadex_compat _beginthreadex
501 #define _endthreadex_compat _endthreadex
502 #endif
504 // The C99 "inline" keyword is not supported by Microsoft compilers, but they do support __inline so map it when needed.
506 #if ( defined( _MSC_VER ) )
507 #define inline_compat __inline
508 #else
509 #define inline_compat inline
510 #endif
512 // Calling conventions
514 #if ( !defined( CALLBACK_COMPAT ) )
515 #if ( TARGET_OS_WIN32 || TARGET_OS_WINDOWS_CE )
516 #define CALLBACK_COMPAT CALLBACK
517 #else
518 #define CALLBACK_COMPAT
519 #endif
520 #endif
522 #if 0
523 #pragma mark == Macros ==
524 #endif
527 /*! @defined kSizeCString
529 @abstract A meta-value to pass to supported routines to indicate the size should be calculated with strlen.
532 #define kSizeCString ( (size_t) -1 )
535 /*! @defined sizeof_array
537 @abstract Determines the number of elements in an array.
540 #define sizeof_array( X ) ( sizeof( X ) / sizeof( X[ 0 ] ) )
543 /*! @defined sizeof_element
545 @abstract Determines the size of an array element.
548 #define sizeof_element( X ) sizeof( X[ 0 ] )
551 /*! @defined sizeof_string
553 @abstract Determines the size of a constant C string, excluding the null terminator.
556 #define sizeof_string( X ) ( sizeof( ( X ) ) - 1 )
559 /*! @defined sizeof_field
561 @abstract Determines the size of a field of a type.
564 #define sizeof_field( TYPE, FIELD ) sizeof( ( ( (TYPE *) 0 )->FIELD ) )
567 /*! @function RoundUp
569 @abstract Rounds X up to a multiple of Y.
572 #define RoundUp( X, Y ) ( ( X ) + ( ( Y ) -( ( X ) % ( Y ) ) ) )
575 /*! @function IsAligned
577 @abstract Returns non-zero if X is aligned to a Y byte boundary and 0 if not. Y must be a power of 2.
580 #define IsAligned( X, Y ) ( ( ( X ) &( ( Y ) -1 ) ) == 0 )
583 /*! @function IsFieldAligned
585 @abstract Returns non-zero if FIELD of type TYPE is aligned to a Y byte boundary and 0 if not. Y must be a power of 2.
588 #define IsFieldAligned( X, TYPE, FIELD, Y ) IsAligned( ( (uintptr_t)( X ) ) + offsetof( TYPE, FIELD ), ( Y ) )
591 /*! @function AlignDown
593 @abstract Aligns X down to a Y byte boundary. Y must be a power of 2.
596 #define AlignDown( X, Y ) ( ( X ) &~( ( Y ) -1 ) )
599 /*! @function AlignUp
601 @abstract Aligns X up to a Y byte boundary. Y must be a power of 2.
604 #define AlignUp( X, Y ) ( ( ( X ) + ( ( Y ) -1 ) ) & ~( ( Y ) -1 ) )
607 /*! @function Min
609 @abstract Returns the lesser of X and Y.
612 #if ( !defined( Min ) )
613 #define Min( X, Y ) ( ( ( X ) < ( Y ) ) ? ( X ) : ( Y ) )
614 #endif
617 /*! @function Max
619 @abstract Returns the greater of X and Y.
622 #if ( !defined( Max ) )
623 #define Max( X, Y ) ( ( ( X ) > ( Y ) ) ? ( X ) : ( Y ) )
624 #endif
627 /*! @function InsertBits
629 @abstract Inserts BITS (both 0 and 1 bits) into X, controlled by MASK and SHIFT, and returns the result.
631 @discussion
633 MASK is the bitmask of the bits in the final position.
634 SHIFT is the number of bits to shift left for 1 to reach the first bit position of MASK.
636 For example, if you wanted to insert 0x3 into the leftmost 4 bits of a 32-bit value:
638 InsertBits( 0, 0x3, 0xF0000000U, 28 ) == 0x30000000
641 #define InsertBits( X, BITS, MASK, SHIFT ) ( ( ( X ) &~( MASK ) ) | ( ( ( BITS ) << ( SHIFT ) ) & ( MASK ) ) )
644 /*! @function ExtractBits
646 @abstract Extracts bits from X, controlled by MASK and SHIFT, and returns the result.
648 @discussion
650 MASK is the bitmask of the bits in the final position.
651 SHIFT is the number of bits to shift right to right justify MASK.
653 For example, if you had a 32-bit value (e.g. 0x30000000) wanted the left-most 4 bits (e.g. 3 in this example):
655 ExtractBits( 0x30000000U, 0xF0000000U, 28 ) == 0x3
658 #define ExtractBits( X, MASK, SHIFT ) ( ( ( X ) >> ( SHIFT ) ) & ( ( MASK ) >> ( SHIFT ) ) )
661 /*! @function Stringify
663 @abstract Stringify's an expression.
665 @discussion
667 Stringify macros to process raw text passed via -D options to C string constants. The double-wrapping is necessary
668 because the C preprocessor doesn't perform its normal argument expansion pre-scan with stringified macros so the
669 -D macro needs to be expanded once via the wrapper macro then stringified so the raw text is stringified. Otherwise,
670 the replacement value would be used instead of the symbolic name (only for preprocessor symbols like #defines).
672 For example:
674 #define kMyConstant 1
676 printf( "%s", Stringify( kMyConstant ) ); // Prints "kMyConstant"
677 printf( "%s", StringifyExpansion( kMyConstant ) ); // Prints "1"
679 Non-preprocessor symbols do not have this issue. For example:
681 enum
683 kMyConstant = 1
686 printf( "%s", Stringify( kMyConstant ) ); // Prints "kMyConstant"
687 printf( "%s", StringifyExpansion( kMyConstant ) ); // Prints "kMyConstant"
689 See <http://gcc.gnu.org/onlinedocs/cpp/Argument-Prescan.html> for more info on C preprocessor pre-scanning.
692 #define Stringify( X ) # X
693 #define StringifyExpansion( X ) Stringify( X )
695 #if 0
696 #pragma mark == Types ==
697 #endif
699 #if ( TARGET_LANGUAGE_C_LIKE )
701 // Standard Types
704 #if ( !defined( INT8_MIN ) )
706 #define INT8_MIN SCHAR_MIN
708 #if ( defined( _MSC_VER ) )
710 // C99 stdint.h not supported in VC++/VS.NET yet.
712 typedef INT8 int8_t;
713 typedef UINT8 uint8_t;
714 typedef INT16 int16_t;
715 typedef UINT16 uint16_t;
716 typedef INT32 int32_t;
717 typedef UINT32 uint32_t;
718 typedef __int64 int64_t;
719 typedef unsigned __int64 uint64_t;
720 #elif defined (__MINGW32__)
721 # include <stdint.h>
722 #elif ( TARGET_OS_VXWORKS && ( TORNADO_VERSION < 220 ) )
723 typedef long long int64_t;
724 typedef unsigned long long uint64_t;
725 #endif
727 typedef int32_t int_least32_t;
729 typedef uint8_t uint_least8_t;
730 typedef uint16_t uint_least16_t;
731 typedef uint32_t uint_least32_t;
732 typedef uint64_t uint_least64_t;
735 typedef uint8_t uint_fast8_t;
736 typedef uint16_t uint_fast16_t;
737 typedef uint32_t uint_fast32_t;
738 typedef uint64_t uint_fast64_t;
740 #if ( !defined( _WIN32 ) || TARGET_OS_WINDOWS_CE )
741 typedef long int intptr_t;
742 typedef unsigned long int uintptr_t;
743 #endif
745 #endif
747 // Macros for minimum-width integer constants
749 #if ( !defined( INT8_C ) )
750 #define INT8_C( value ) value
751 #endif
753 #if ( !defined( INT16_C ) )
754 #define INT16_C( value ) value
755 #endif
757 #if ( !defined( INT32_C ) )
758 #define INT32_C( value ) value ## L
759 #endif
761 #if ( !defined( INT64_C ) )
762 #if ( defined( _MSC_VER ) )
763 #define INT64_C( value ) value ## i64
764 #else
765 #define INT64_C( value ) value ## LL
766 #endif
767 #endif
769 #if ( !defined( UINT8_C ) )
770 #define UINT8_C( value ) value ## U
771 #endif
773 #if ( !defined( UINT16_C ) )
774 #define UINT16_C( value ) value ## U
775 #endif
777 #if ( !defined( UINT32_C ) )
778 #define UINT32_C( value ) value ## UL
779 #endif
781 #if ( !defined( UINT64_C ) )
782 #if ( defined( _MSC_VER ) )
783 #define UINT64_C( value ) value ## UI64
784 #else
785 #define UINT64_C( value ) value ## ULL
786 #endif
787 #endif
789 #if 0
790 #pragma mark == bool ==
791 #endif
794 // Boolean Constants and Types
797 // C++ defines bool, true, and false. Metrowerks allows this to be controlled by the "bool" option though.
798 // C99 defines __bool_true_false_are_defined when bool, true, and false are defined.
799 // MacTypes.h defines true and false (Mac builds only).
801 // Note: The Metrowerks has to be in its own block because Microsoft Visual Studio .NET does not completely
802 // short-circuit and gets confused by the option( bool ) portion of the conditional.
804 #if ( defined( __MWERKS__ ) )
806 // Note: The following test is done on separate lines because CodeWarrior doesn't like it all on one line.
808 #if ( !__bool_true_false_are_defined && ( !defined( __cplusplus ) || !__option( bool ) ) )
809 #define COMMON_SERVICES_NEEDS_BOOL 1
810 #else
811 #define COMMON_SERVICES_NEEDS_BOOL 0
812 #endif
814 // Workaround when building with CodeWarrior, but using the Apple stdbool.h header, which uses _Bool.
816 #if ( __bool_true_false_are_defined && !defined( __cplusplus ) && !__option( c9x ) )
817 #define _Bool int
818 #endif
820 // Workaround when building with CodeWarrior for C++ with bool disabled and using the Apple stdbool.h header,
821 // which defines true and false to map to C++ true and false (which are not enabled). Serenity Now!
823 #if ( __bool_true_false_are_defined && defined( __cplusplus ) && !__option( bool ) )
824 #define true 1
825 #define false 0
826 #endif
827 #else
828 #define COMMON_SERVICES_NEEDS_BOOL ( !defined( __cplusplus ) && !__bool_true_false_are_defined )
829 #endif
831 #if ( COMMON_SERVICES_NEEDS_BOOL )
833 typedef int bool;
835 #define bool bool
837 #if ( !defined( __MACTYPES__ ) && !defined( true ) && !defined( false ) )
838 #define true 1
839 #define false 0
840 #endif
842 #define __bool_true_false_are_defined 1
843 #endif
845 // IOKit IOTypes.h typedef's bool if TYPE_BOOL is not defined so define it here to prevent redefinition by IOTypes.h.
847 #if ( TARGET_API_MAC_OSX_KERNEL )
848 #define TYPE_BOOL 1
849 #endif
852 /*! @typedef CStr255
854 @abstract 255 character null-terminated (C-style) string.
857 #if ( TARGET_LANGUAGE_C_LIKE )
858 typedef char CStr255[ 256 ];
859 #endif
861 #endif // TARGET_LANGUAGE_C_LIKE
864 /*! @defined TYPE_LONGLONG_NATIVE
866 @abstract Defines whether long long (or its equivalent) is natively supported or requires special libraries.
869 #if ( !defined( TYPE_LONGLONG_NATIVE ) )
870 #if ( !TARGET_OS_VXWORKS )
871 #define TYPE_LONGLONG_NATIVE 1
872 #else
873 #define TYPE_LONGLONG_NATIVE 0
874 #endif
875 #endif
878 /*! @defined long_long_compat
880 @abstract Compatibility type to map to the closest thing to long long and unsigned long long.
882 @discussion
884 Neither long long nor unsigned long long are supported by Microsoft compilers, but they do support proprietary
885 "__int64" and "unsigned __int64" equivalents so map to those types if the real long long is not supported.
888 #if ( TARGET_LANGUAGE_C_LIKE )
889 #if ( TARGET_OS_WIN32 )
890 typedef __int64 long_long_compat;
891 typedef unsigned __int64 unsigned_long_long_compat;
892 #else
893 typedef signed long long long_long_compat;
894 typedef unsigned long long unsigned_long_long_compat;
895 #endif
896 #endif
898 #if 0
899 #pragma mark == Errors ==
900 #endif
903 /*! @enum OSStatus
905 @abstract Status Code
907 @constant kNoErr 0 No error occurred.
908 @constant kInProgressErr 1 Operation in progress.
909 @constant kUnknownErr -6700 Unknown error occurred.
910 @constant kOptionErr -6701 Option was not acceptable.
911 @constant kSelectorErr -6702 Selector passed in is invalid or unknown.
912 @constant kExecutionStateErr -6703 Call made in the wrong execution state (e.g. called at interrupt time).
913 @constant kPathErr -6704 Path is invalid, too long, or otherwise not usable.
914 @constant kParamErr -6705 Parameter is incorrect, missing, or not appropriate.
915 @constant kParamCountErr -6706 Incorrect or unsupported number of parameters.
916 @constant kCommandErr -6707 Command invalid or not supported.
917 @constant kIDErr -6708 Unknown, invalid, or inappropriate identifier.
918 @constant kStateErr -6709 Not in appropriate state to perform operation.
919 @constant kRangeErr -6710 Index is out of range or not valid.
920 @constant kRequestErr -6711 Request was improperly formed or not appropriate.
921 @constant kResponseErr -6712 Response was incorrect or out of sequence.
922 @constant kChecksumErr -6713 Checksum does not match the actual data.
923 @constant kNotHandledErr -6714 Operation was not handled (or not handled completely).
924 @constant kVersionErr -6715 Version is not incorrect or not compatibile.
925 @constant kSignatureErr -6716 Signature did not match what was expected.
926 @constant kFormatErr -6717 Unknown, invalid, or inappropriate file/data format.
927 @constant kNotInitializedErr -6718 Action request before needed services were initialized.
928 @constant kAlreadyInitializedErr -6719 Attempt made to initialize when already initialized.
929 @constant kNotInUseErr -6720 Object not in use (e.g. cannot abort if not already in use).
930 @constant kInUseErr -6721 Object is in use (e.g. cannot reuse active param blocks).
931 @constant kTimeoutErr -6722 Timeout occurred.
932 @constant kCanceledErr -6723 Operation canceled (successful cancel).
933 @constant kAlreadyCanceledErr -6724 Operation has already been canceled.
934 @constant kCannotCancelErr -6725 Operation could not be canceled (maybe already done or invalid).
935 @constant kDeletedErr -6726 Object has already been deleted.
936 @constant kNotFoundErr -6727 Something was not found.
937 @constant kNoMemoryErr -6728 Not enough memory was available to perform the operation.
938 @constant kNoResourcesErr -6729 Resources unavailable to perform the operation.
939 @constant kDuplicateErr -6730 Duplicate found or something is a duplicate.
940 @constant kImmutableErr -6731 Entity is not changeable.
941 @constant kUnsupportedDataErr -6732 Data is unknown or not supported.
942 @constant kIntegrityErr -6733 Data is corrupt.
943 @constant kIncompatibleErr -6734 Data is not compatible or it is in an incompatible format.
944 @constant kUnsupportedErr -6735 Feature or option is not supported.
945 @constant kUnexpectedErr -6736 Error occurred that was not expected.
946 @constant kValueErr -6737 Value is not appropriate.
947 @constant kNotReadableErr -6738 Could not read or reading is not allowed.
948 @constant kNotWritableErr -6739 Could not write or writing is not allowed.
949 @constant kBadReferenceErr -6740 An invalid or inappropriate reference was specified.
950 @constant kFlagErr -6741 An invalid, inappropriate, or unsupported flag was specified.
951 @constant kMalformedErr -6742 Something was not formed correctly.
952 @constant kSizeErr -6743 Size was too big, too small, or not appropriate.
953 @constant kNameErr -6744 Name was not correct, allowed, or appropriate.
954 @constant kNotReadyErr -6745 Device or service is not ready.
955 @constant kReadErr -6746 Could not read.
956 @constant kWriteErr -6747 Could not write.
957 @constant kMismatchErr -6748 Something does not match.
958 @constant kDateErr -6749 Date is invalid or out-of-range.
959 @constant kUnderrunErr -6750 Less data than expected.
960 @constant kOverrunErr -6751 More data than expected.
961 @constant kEndingErr -6752 Connection, session, or something is ending.
962 @constant kConnectionErr -6753 Connection failed or could not be established.
963 @constant kAuthenticationErr -6754 Authentication failed or is not supported.
964 @constant kOpenErr -6755 Could not open file, pipe, device, etc.
965 @constant kTypeErr -6756 Incorrect or incompatible type (e.g. file, data, etc.).
966 @constant kSkipErr -6757 Items should be or was skipped.
967 @constant kNoAckErr -6758 No acknowledge.
968 @constant kCollisionErr -6759 Collision occurred (e.g. two on bus at same time).
969 @constant kBackoffErr -6760 Backoff in progress and operation intentionally failed.
970 @constant kNoAddressAckErr -6761 No acknowledge of address.
971 @constant kBusyErr -6762 Cannot perform because something is busy.
972 @constant kNoSpaceErr -6763 Not enough space to perform operation.
975 #if ( TARGET_LANGUAGE_C_LIKE )
976 #if ( !TARGET_OS_MAC && !TARGET_API_MAC_OSX_KERNEL )
977 typedef int32_t OSStatus;
978 #endif
979 #endif
981 #define kNoErr 0
982 #define kInProgressErr 1
984 // Generic error codes are in the range -6700 to -6779.
986 #define kGenericErrorBase -6700 // Starting error code for all generic errors.
988 #define kUnknownErr -6700
989 #define kOptionErr -6701
990 #define kSelectorErr -6702
991 #define kExecutionStateErr -6703
992 #define kPathErr -6704
993 #define kParamErr -6705
994 #define kParamCountErr -6706
995 #define kCommandErr -6707
996 #define kIDErr -6708
997 #define kStateErr -6709
998 #define kRangeErr -6710
999 #define kRequestErr -6711
1000 #define kResponseErr -6712
1001 #define kChecksumErr -6713
1002 #define kNotHandledErr -6714
1003 #define kVersionErr -6715
1004 #define kSignatureErr -6716
1005 #define kFormatErr -6717
1006 #define kNotInitializedErr -6718
1007 #define kAlreadyInitializedErr -6719
1008 #define kNotInUseErr -6720
1009 #define kInUseErr -6721
1010 #define kTimeoutErr -6722
1011 #define kCanceledErr -6723
1012 #define kAlreadyCanceledErr -6724
1013 #define kCannotCancelErr -6725
1014 #define kDeletedErr -6726
1015 #define kNotFoundErr -6727
1016 #define kNoMemoryErr -6728
1017 #define kNoResourcesErr -6729
1018 #define kDuplicateErr -6730
1019 #define kImmutableErr -6731
1020 #define kUnsupportedDataErr -6732
1021 #define kIntegrityErr -6733
1022 #define kIncompatibleErr -6734
1023 #define kUnsupportedErr -6735
1024 #define kUnexpectedErr -6736
1025 #define kValueErr -6737
1026 #define kNotReadableErr -6738
1027 #define kNotWritableErr -6739
1028 #define kBadReferenceErr -6740
1029 #define kFlagErr -6741
1030 #define kMalformedErr -6742
1031 #define kSizeErr -6743
1032 #define kNameErr -6744
1033 #define kNotReadyErr -6745
1034 #define kReadErr -6746
1035 #define kWriteErr -6747
1036 #define kMismatchErr -6748
1037 #define kDateErr -6749
1038 #define kUnderrunErr -6750
1039 #define kOverrunErr -6751
1040 #define kEndingErr -6752
1041 #define kConnectionErr -6753
1042 #define kAuthenticationErr -6754
1043 #define kOpenErr -6755
1044 #define kTypeErr -6756
1045 #define kSkipErr -6757
1046 #define kNoAckErr -6758
1047 #define kCollisionErr -6759
1048 #define kBackoffErr -6760
1049 #define kNoAddressAckErr -6761
1050 #define kBusyErr -6762
1051 #define kNoSpaceErr -6763
1053 #define kGenericErrorEnd -6779 // Last generic error code (inclusive)
1055 #if 0
1056 #pragma mark == Mac Compatibility ==
1057 #endif
1060 // Mac Compatibility
1064 /*! @enum Duration
1066 @abstract Type used to specify a duration of time.
1068 @constant kDurationImmediate Indicates no delay/wait time.
1069 @constant kDurationMicrosecond Microsecond units.
1070 @constant kDurationMillisecond Millisecond units.
1071 @constant kDurationSecond Second units.
1072 @constant kDurationMinute Minute units.
1073 @constant kDurationHour Hour units.
1074 @constant kDurationDay Day units.
1075 @constant kDurationForever Infinite period of time (no timeout).
1077 @discussion
1079 Duration values are intended to be multiplied by the specific interval to achieve an actual duration. For example,
1080 to wait for 5 seconds you would use "5 * kDurationSecond".
1083 #if ( TARGET_LANGUAGE_C_LIKE )
1084 #if ( !TARGET_OS_MAC )
1085 typedef int32_t Duration;
1086 #endif
1087 #endif
1089 #define kDurationImmediate 0L
1090 #define kDurationMicrosecond -1L
1091 #define kDurationMillisecond 1L
1092 #define kDurationSecond ( 1000L * kDurationMillisecond )
1093 #define kDurationMinute ( 60L * kDurationSecond )
1094 #define kDurationHour ( 60L * kDurationMinute )
1095 #define kDurationDay ( 24L * kDurationHour )
1096 #define kDurationForever 0x7FFFFFFFL
1098 // Seconds <-> Minutes <-> Hours <-> Days <-> Weeks <-> Months <-> Years conversions
1100 #define kSecondsPerHour ( 60 * 60 ) // 3600
1101 #define kSecondsPerDay ( 60 * 60 * 24 ) // 86400
1102 #define kSecondsPerWeek ( 60 * 60 * 24 * 7 ) // 604800
1103 #define kMinutesPerDay ( 60 * 24 ) // 1440
1106 /*! @defined VersionStages
1108 @abstract NumVersion-style version stages.
1111 #define kVersionStageDevelopment 0x20
1112 #define kVersionStageAlpha 0x40
1113 #define kVersionStageBeta 0x60
1114 #define kVersionStageFinal 0x80
1117 /*! @function NumVersionBuild
1119 @abstract Builds a 32-bit Mac-style NumVersion value (e.g. NumVersionBuild( 1, 2, 3, kVersionStageBeta, 4 ) -> 1.2.3b4).
1122 #define NumVersionBuild( MAJOR, MINOR, BUGFIX, STAGE, REV ) \
1123 ( ( ( ( MAJOR ) & 0xFF ) << 24 ) | \
1124 ( ( ( MINOR ) & 0x0F ) << 20 ) | \
1125 ( ( ( BUGFIX ) & 0x0F ) << 16 ) | \
1126 ( ( ( STAGE ) & 0xFF ) << 8 ) | \
1127 ( ( ( REV ) & 0xFF ) ) )
1129 #define NumVersionExtractMajor( VERSION ) ( (uint8_t)( ( ( VERSION ) >> 24 ) & 0xFF ) )
1130 #define NumVersionExtractMinorAndBugFix( VERSION ) ( (uint8_t)( ( ( VERSION ) >> 16 ) & 0xFF ) )
1131 #define NumVersionExtractMinor( VERSION ) ( (uint8_t)( ( ( VERSION ) >> 20 ) & 0x0F ) )
1132 #define NumVersionExtractBugFix( VERSION ) ( (uint8_t)( ( ( VERSION ) >> 16 ) & 0x0F ) )
1133 #define NumVersionExtractStage( VERSION ) ( (uint8_t)( ( ( VERSION ) >> 8 ) & 0xFF ) )
1134 #define NumVersionExtractRevision( VERSION ) ( (uint8_t)( ( VERSION ) & 0xFF ) )
1137 /*! @function NumVersionCompare
1139 @abstract Compares two NumVersion values and returns the following values:
1141 left < right -> -1
1142 left > right -> 1
1143 left = right -> 0
1146 #if ( TARGET_LANGUAGE_C_LIKE )
1147 int NumVersionCompare( uint32_t inLeft, uint32_t inRight );
1148 #endif
1150 #if 0
1151 #pragma mark == Binary Constants ==
1152 #endif
1155 /*! @defined binary_4
1157 @abstract Macro to generate an 4-bit constant using binary notation (e.g. binary_4( 1010 ) == 0xA).
1160 #define binary_4( a ) binary_4_hex_wrap( hex_digit4( a ) )
1161 #define binary_4_hex_wrap( a ) binary_4_hex( a )
1162 #define binary_4_hex( a ) ( 0x ## a )
1165 /*! @defined binary_8
1167 @abstract Macro to generate an 8-bit constant using binary notation (e.g. binary_8( 01111011 ) == 0x7B).
1170 #define binary_8( a ) binary_8_hex_wrap( hex_digit8( a ) )
1171 #define binary_8_hex_wrap( a ) binary_8_hex( a )
1172 #define binary_8_hex( a ) ( 0x ## a )
1175 /*! @defined binary_16
1177 @abstract Macro to generate an 16-bit constant using binary notation (e.g. binary_16( 01111011, 01111011 ) == 0x7B7B).
1180 #define binary_16( a, b ) binary_16_hex_wrap( hex_digit8( a ), hex_digit8( b ) )
1181 #define binary_16_hex_wrap( a, b ) binary_16_hex( a, b )
1182 #define binary_16_hex( a, b ) ( 0x ## a ## b )
1185 /*! @defined binary_32
1187 @abstract Macro to generate an 32-bit constant using binary notation
1188 (e.g. binary_32( 01111011, 01111011, 01111011, 01111011 ) == 0x7B7B7B7B).
1191 #define binary_32( a, b, c, d ) binary_32_hex_wrap( hex_digit8( a ), hex_digit8( b ), hex_digit8( c ), hex_digit8( d ) )
1192 #define binary_32_hex_wrap( a, b, c, d ) binary_32_hex( a, b, c, d )
1193 #define binary_32_hex( a, b, c, d ) ( 0x ## a ## b ## c ## d )
1195 // Binary Constant Helpers
1197 #define hex_digit8( a ) HEX_DIGIT_ ## a
1198 #define hex_digit4( a ) HEX_DIGIT_ ## 0000 ## a
1201 #if 0
1202 #pragma mark == Debugging ==
1203 #endif
1206 /*! @function CommonServicesTest
1208 @abstract Unit test.
1211 #if ( DEBUG )
1212 #if ( TARGET_LANGUAGE_C_LIKE )
1213 OSStatus CommonServicesTest( void );
1214 #endif
1215 #endif
1217 #ifdef __cplusplus
1219 #endif
1221 #endif // __COMMON_SERVICES__