1/* 2 * Copyright (c) 2008 - 2010 Apple Inc. All rights reserved. 3 * 4 * @APPLE_LICENSE_HEADER_START@ 5 * 6 * This file contains Original Code and/or Modifications of Original Code 7 * as defined in and that are subject to the Apple Public Source License 8 * Version 2.0 (the 'License'). You may not use this file except in 9 * compliance with the License. Please obtain a copy of the License at 10 * http://www.opensource.apple.com/apsl/ and read it before using this 11 * file. 12 * 13 * The Original Code and all software distributed under the License are 14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 18 * Please see the License for the specific language governing rights and 19 * limitations under the License. 20 * 21 * @APPLE_LICENSE_HEADER_END@ 22 */ 23 24#ifndef COMPILER_H_09EABBF7_1566_4BAB_B665_CBD0D0831CFB 25#define COMPILER_H_09EABBF7_1566_4BAB_B665_CBD0D0831CFB 26 27#include "platform.h" 28 29#include <stdint.h> // 30#if PLATFORM(UNIX) 31#include <sys/types.h> // ssize_t 32#endif 33 34 35 36/*! 37 * @define UNLIKELY 38 * Marks an expression as likely to evaluate to FALSE. 39 */ 40#ifndef UNLIKELY 41#if COMPILER(GCC) 42#define UNLIKELY(x) __builtin_expect(!!(x), 0) 43#else 44#define UNLIKELY(x) (x) 45#endif 46#endif 47 48/*! 49 * @define LIKELY 50 * Marks an expression as likely to evaluate to TRUE. 51 */ 52#ifndef LIKELY 53#if COMPILER(GCC) 54#define LIKELY(x) __builtin_expect(!!(x), 1) 55#else 56#define LIKELY(x) (x) 57#endif 58#endif 59 60/*! 61 * @define REFERENCE_SYMBOL 62 * Mark a symbol as referenced so that the static linker won't strip it. 63 */ 64#if PLATFORM(DARWIN) 65/* Force strip(1) to preserve SYM by setting the REFERENCED_DYNAMICALLY bit. */ 66#define REFERENCE_SYMBOL(sym) asm(".desc " #sym ", 0x10"); 67#else 68#define REFERENCE_SYMBOL(sym) (sym) 69#endif 70 71/*! 72 * @define PRINTFLIKE 73 * @abstract Mark a function as a candidate for printf(3) format string checking. 74 * @param fmtarg The number of the format string argument. 75 * @param firstvararg The number of the first argument used in the format string. 76 */ 77#ifndef PRINTFLIKE 78#if COMPILER(GCC) 79#define PRINTFLIKE(fmtarg, firstvararg) \ 80 __attribute__((__format__ (__printf__, fmtarg, firstvararg))) 81#else 82#define PRINTFLIKE(fmtarg, firstvararg) 83#endif /* COMPILER(GCC) */ 84#endif 85 86#ifdef __cplusplus 87/*! 88 * @define CXX_NONCOPYABLE 89 * @abstract Disable copy construction and assignent for this class. 90 */ 91#define CXX_NONCOPYABLE(CLASSNAME) \ 92 CLASSNAME(const CLASSNAME&); const CLASSNAME& operator=(const CLASSNAME&) 93#endif 94 95#if COMPILER(GCC) 96// Return the required alignment of the given type. 97#define alignof(x) ((uintptr_t)__alignof__(x)) 98#define __hidden __attribute__((visibility("hidden"))) 99 100// XXX We might have to do some funkier preprocessor checks when we get the 101// C++0x alignof keyword. 102#else 103#define alignof(t) ((uintptr_t)offsetof(struct { char c; t x; }, x)) 104#define __hidden 105#endif 106 107// Define explicit UTF project types, and convenience casts 108#if !defined(_utf32_t_DEFINED) 109#define _utf32_t_DEFINED 110typedef uint32_t utf32_t; 111#endif 112 113#if !defined(_utf16_t_DEFINED) 114#define _utf16_t_DEFINED 115typedef uint16_t utf16_t; 116#endif 117 118#if !defined(_utf8_t_DEFINED) 119#define _utf8_t_DEFINED 120typedef uint8_t utf8_t; 121#endif 122 123// Cosmetic conversion fro utf8_t * to char *. 124static inline 125char * utf8_cast(utf8_t * c) { 126 return reinterpret_cast<char *>(c); 127} 128 129// Cosmetic conversion from const utf8_t * to const char *. 130static inline 131const char * utf8_cast(const utf8_t * c) { 132 return reinterpret_cast<const char *>(c); 133} 134 135// Return the size of an array literal. 136template <typename T, unsigned N> 137unsigned array_size(const T (&)[N]) { 138 return N; 139} 140 141// Return an iterator to the end of an array literal. 142template <typename T, unsigned N> 143T * array_end(T (&a)[N]) { 144 return a + N; 145} 146 147// Return an iterator to the end of a const array literal. 148template <typename T, unsigned N> 149const T * array_end(const T (&a)[N]) { 150 return a + N; 151} 152 153// Copy an array literal. 154template <typename T, unsigned N> 155void array_copy(T (&dst)[N], const T (&src)[N]) { 156 memcpy(dst, src, N * sizeof(T)); 157} 158 159#endif /* COMPILER_H_09EABBF7_1566_4BAB_B665_CBD0D0831CFB */ 160 161/* vim: set cindent et ts=4 sw=4 tw=79 : */ 162