| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 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 | */ |
| Andy McFadden | 734155e | 2009-07-16 18:11:22 -0700 | [diff] [blame] | 16 | |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 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) \ |
| Andy McFadden | 734155e | 2009-07-16 18:11:22 -0700 | [diff] [blame] | 33 | ((x) ? ((void)0) : (LOGE("ASSERT FAILED (%s:%d): %s\n", \ |
| Carl Shapiro | 4a1ef7d | 2010-12-01 19:51:36 -0800 | [diff] [blame] | 34 | __FILE__, __LINE__, #x), *(int*)39=39, (void)0) ) |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 35 | #endif |
| 36 | |
| Carl Shapiro | 99fc456 | 2010-07-09 17:35:15 -0700 | [diff] [blame] | 37 | #define MIN(x,y) (((x) < (y)) ? (x) : (y)) |
| 38 | #define MAX(x,y) (((x) > (y)) ? (x) : (y)) |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 39 | |
| Carl Shapiro | a20a992 | 2010-07-29 21:22:12 -0700 | [diff] [blame] | 40 | #define LIKELY(exp) (__builtin_expect((exp) != 0, true)) |
| 41 | #define UNLIKELY(exp) (__builtin_expect((exp) != 0, false)) |
| 42 | |
| Carl Shapiro | cc3f8b4 | 2011-02-08 20:10:56 -0800 | [diff] [blame^] | 43 | #define ALIGN_UP(x, n) (((size_t)(x) + (n) - 1) & ~((n) - 1)) |
| 44 | #define ALIGN_DOWN(x, n) ((size_t)(x) & -(n)) |
| 45 | #define ALIGN_UP_TO_PAGE_SIZE(p) ALIGN_UP(p, SYSTEM_PAGE_SIZE) |
| 46 | #define ALIGN_DOWN_TO_PAGE_SIZE(p) ALIGN_DOWN(p, SYSTEM_PAGE_SIZE) |
| 47 | |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 48 | /* |
| 49 | * If "very verbose" logging is enabled, make it equivalent to LOGV. |
| 50 | * Otherwise, make it disappear. |
| 51 | * |
| 52 | * Define this above the #include "Dalvik.h" to enable for only a |
| 53 | * single file. |
| 54 | */ |
| 55 | /* #define VERY_VERBOSE_LOG */ |
| 56 | #if defined(VERY_VERBOSE_LOG) |
| 57 | # define LOGVV LOGV |
| 58 | # define IF_LOGVV() IF_LOGV() |
| 59 | #else |
| 60 | # define LOGVV(...) ((void)0) |
| 61 | # define IF_LOGVV() if (false) |
| 62 | #endif |
| 63 | |
| 64 | |
| 65 | /* |
| 66 | * These match the definitions in the VM specification. |
| 67 | */ |
| 68 | #ifdef HAVE_STDINT_H |
| 69 | # include <stdint.h> /* C99 */ |
| 70 | typedef uint8_t u1; |
| 71 | typedef uint16_t u2; |
| 72 | typedef uint32_t u4; |
| 73 | typedef uint64_t u8; |
| 74 | typedef int8_t s1; |
| 75 | typedef int16_t s2; |
| 76 | typedef int32_t s4; |
| 77 | typedef int64_t s8; |
| 78 | #else |
| 79 | typedef unsigned char u1; |
| 80 | typedef unsigned short u2; |
| 81 | typedef unsigned int u4; |
| 82 | typedef unsigned long long u8; |
| 83 | typedef signed char s1; |
| 84 | typedef signed short s2; |
| 85 | typedef signed int s4; |
| 86 | typedef signed long long s8; |
| 87 | #endif |
| 88 | |
| 89 | /* |
| 90 | * Storage for primitive types and object references. |
| 91 | * |
| 92 | * Some parts of the code (notably object field access) assume that values |
| 93 | * are "left aligned", i.e. given "JValue jv", "jv.i" and "*((s4*)&jv)" |
| 94 | * yield the same result. This seems to be guaranteed by gcc on big- and |
| 95 | * little-endian systems. |
| 96 | */ |
| 97 | typedef union JValue { |
| 98 | u1 z; |
| 99 | s1 b; |
| 100 | u2 c; |
| 101 | s2 s; |
| 102 | s4 i; |
| 103 | s8 j; |
| 104 | float f; |
| 105 | double d; |
| 106 | void* l; |
| 107 | } JValue; |
| 108 | |
| 109 | /* |
| Andy McFadden | d51370f | 2009-08-05 15:20:27 -0700 | [diff] [blame] | 110 | * The <stdbool.h> definition uses _Bool, a type known to the compiler. |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 111 | */ |
| Andy McFadden | d51370f | 2009-08-05 15:20:27 -0700 | [diff] [blame] | 112 | #ifdef HAVE_STDBOOL_H |
| 113 | # include <stdbool.h> /* C99 */ |
| 114 | #else |
| 115 | # ifndef __bool_true_false_are_defined |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 116 | typedef enum { false=0, true=!false } bool; |
| Andy McFadden | d51370f | 2009-08-05 15:20:27 -0700 | [diff] [blame] | 117 | # define __bool_true_false_are_defined 1 |
| 118 | # endif |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 119 | #endif |
| 120 | |
| 121 | #define NELEM(x) ((int) (sizeof(x) / sizeof((x)[0]))) |
| 122 | |
| 123 | |
| 124 | #if defined(HAVE_ENDIAN_H) |
| 125 | # include <endian.h> |
| 126 | #else /*not HAVE_ENDIAN_H*/ |
| 127 | # define __BIG_ENDIAN 4321 |
| 128 | # define __LITTLE_ENDIAN 1234 |
| 129 | # if defined(HAVE_LITTLE_ENDIAN) |
| 130 | # define __BYTE_ORDER __LITTLE_ENDIAN |
| 131 | # else |
| 132 | # define __BYTE_ORDER __BIG_ENDIAN |
| 133 | # endif |
| 134 | #endif /*not HAVE_ENDIAN_H*/ |
| 135 | |
| 136 | |
| 137 | #if 0 |
| 138 | /* |
| 139 | * Pretend we have the Android logging macros. These are replaced by the |
| 140 | * Android logging implementation. |
| 141 | */ |
| 142 | #define ANDROID_LOG_DEBUG 3 |
| 143 | #define LOGV(...) LOG_PRI(2, 0, __VA_ARGS__) |
| 144 | #define LOGD(...) LOG_PRI(3, 0, __VA_ARGS__) |
| 145 | #define LOGI(...) LOG_PRI(4, 0, __VA_ARGS__) |
| 146 | #define LOGW(...) LOG_PRI(5, 0, __VA_ARGS__) |
| 147 | #define LOGE(...) LOG_PRI(6, 0, __VA_ARGS__) |
| 148 | #define MIN_LOG_LEVEL 2 |
| 149 | |
| 150 | #define LOG_PRI(priority, tag, ...) do { \ |
| 151 | if (priority >= MIN_LOG_LEVEL) { \ |
| 152 | dvmFprintf(stdout, "%s:%-4d ", __FILE__, __LINE__); \ |
| 153 | dvmFprintf(stdout, __VA_ARGS__); \ |
| 154 | } \ |
| 155 | } while(0) |
| 156 | #else |
| 157 | # include "utils/Log.h" |
| 158 | #endif |
| 159 | |
| 160 | #endif /*_DALVIK_COMMON*/ |