1 /* 2 * Copyright (C) 2008 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 /* 18 * Common defines for all Dalvik code. 19 */ 20 #ifndef _DALVIK_COMMON 21 #define _DALVIK_COMMON 22 23 #ifndef LOG_TAG 24 # define LOG_TAG "dalvikvm" 25 #endif 26 27 #include <stdio.h> 28 #include <assert.h> 29 30 #if !defined(NDEBUG) && defined(WITH_DALVIK_ASSERT) 31 # undef assert 32 # define assert(x) \ 33 ((x) ? ((void)0) : (LOGE("ASSERT FAILED (%s:%d): %s\n", \ 34 __FILE__, __LINE__, #x), *(int*)39=39, 0) ) 35 #endif 36 37 #define MIN(x,y) (((x) < (y)) ? (x) : (y)) 38 #define MAX(x,y) (((x) > (y)) ? (x) : (y)) 39 40 #define LIKELY(exp) (__builtin_expect((exp) != 0, true)) 41 #define UNLIKELY(exp) (__builtin_expect((exp) != 0, false)) 42 43 /* 44 * If "very verbose" logging is enabled, make it equivalent to LOGV. 45 * Otherwise, make it disappear. 46 * 47 * Define this above the #include "Dalvik.h" to enable for only a 48 * single file. 49 */ 50 /* #define VERY_VERBOSE_LOG */ 51 #if defined(VERY_VERBOSE_LOG) 52 # define LOGVV LOGV 53 # define IF_LOGVV() IF_LOGV() 54 #else 55 # define LOGVV(...) ((void)0) 56 # define IF_LOGVV() if (false) 57 #endif 58 59 60 /* 61 * These match the definitions in the VM specification. 62 */ 63 #ifdef HAVE_STDINT_H 64 # include <stdint.h> /* C99 */ 65 typedef uint8_t u1; 66 typedef uint16_t u2; 67 typedef uint32_t u4; 68 typedef uint64_t u8; 69 typedef int8_t s1; 70 typedef int16_t s2; 71 typedef int32_t s4; 72 typedef int64_t s8; 73 #else 74 typedef unsigned char u1; 75 typedef unsigned short u2; 76 typedef unsigned int u4; 77 typedef unsigned long long u8; 78 typedef signed char s1; 79 typedef signed short s2; 80 typedef signed int s4; 81 typedef signed long long s8; 82 #endif 83 84 /* 85 * Storage for primitive types and object references. 86 * 87 * Some parts of the code (notably object field access) assume that values 88 * are "left aligned", i.e. given "JValue jv", "jv.i" and "*((s4*)&jv)" 89 * yield the same result. This seems to be guaranteed by gcc on big- and 90 * little-endian systems. 91 */ 92 typedef union JValue { 93 u1 z; 94 s1 b; 95 u2 c; 96 s2 s; 97 s4 i; 98 s8 j; 99 float f; 100 double d; 101 void* l; 102 } JValue; 103 104 /* 105 * The <stdbool.h> definition uses _Bool, a type known to the compiler. 106 */ 107 #ifdef HAVE_STDBOOL_H 108 # include <stdbool.h> /* C99 */ 109 #else 110 # ifndef __bool_true_false_are_defined 111 typedef enum { false=0, true=!false } bool; 112 # define __bool_true_false_are_defined 1 113 # endif 114 #endif 115 116 #define NELEM(x) ((int) (sizeof(x) / sizeof((x)[0]))) 117 118 119 #if defined(HAVE_ENDIAN_H) 120 # include <endian.h> 121 #else /*not HAVE_ENDIAN_H*/ 122 # define __BIG_ENDIAN 4321 123 # define __LITTLE_ENDIAN 1234 124 # if defined(HAVE_LITTLE_ENDIAN) 125 # define __BYTE_ORDER __LITTLE_ENDIAN 126 # else 127 # define __BYTE_ORDER __BIG_ENDIAN 128 # endif 129 #endif /*not HAVE_ENDIAN_H*/ 130 131 132 #if 0 133 /* 134 * Pretend we have the Android logging macros. These are replaced by the 135 * Android logging implementation. 136 */ 137 #define ANDROID_LOG_DEBUG 3 138 #define LOGV(...) LOG_PRI(2, 0, __VA_ARGS__) 139 #define LOGD(...) LOG_PRI(3, 0, __VA_ARGS__) 140 #define LOGI(...) LOG_PRI(4, 0, __VA_ARGS__) 141 #define LOGW(...) LOG_PRI(5, 0, __VA_ARGS__) 142 #define LOGE(...) LOG_PRI(6, 0, __VA_ARGS__) 143 #define MIN_LOG_LEVEL 2 144 145 #define LOG_PRI(priority, tag, ...) do { \ 146 if (priority >= MIN_LOG_LEVEL) { \ 147 dvmFprintf(stdout, "%s:%-4d ", __FILE__, __LINE__); \ 148 dvmFprintf(stdout, __VA_ARGS__); \ 149 } \ 150 } while(0) 151 #else 152 # include "utils/Log.h" 153 #endif 154 155 #endif /*_DALVIK_COMMON*/ 156