| 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 | |
| Carl Shapiro | ae188c6 | 2011-04-08 13:11:58 -0700 | [diff] [blame] | 27 | #include <stdbool.h> |
| 28 | #include <stdint.h> |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 29 | #include <stdio.h> |
| 30 | #include <assert.h> |
| Carl Shapiro | ae188c6 | 2011-04-08 13:11:58 -0700 | [diff] [blame] | 31 | #include "utils/Log.h" |
| 32 | |
| Brian Carlstrom | 38e191b | 2011-05-05 15:26:41 -0700 | [diff] [blame] | 33 | #if defined(HAVE_ENDIAN_H) |
| 34 | # include <endian.h> |
| 35 | #else /*not HAVE_ENDIAN_H*/ |
| 36 | # define __BIG_ENDIAN 4321 |
| 37 | # define __LITTLE_ENDIAN 1234 |
| 38 | # if defined(HAVE_LITTLE_ENDIAN) |
| 39 | # define __BYTE_ORDER __LITTLE_ENDIAN |
| 40 | # else |
| 41 | # define __BYTE_ORDER __BIG_ENDIAN |
| 42 | # endif |
| 43 | #endif /*not HAVE_ENDIAN_H*/ |
| 44 | |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 45 | #if !defined(NDEBUG) && defined(WITH_DALVIK_ASSERT) |
| 46 | # undef assert |
| 47 | # define assert(x) \ |
| Andy McFadden | 734155e | 2009-07-16 18:11:22 -0700 | [diff] [blame] | 48 | ((x) ? ((void)0) : (LOGE("ASSERT FAILED (%s:%d): %s\n", \ |
| Carl Shapiro | 4a1ef7d | 2010-12-01 19:51:36 -0800 | [diff] [blame] | 49 | __FILE__, __LINE__, #x), *(int*)39=39, (void)0) ) |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 50 | #endif |
| 51 | |
| Carl Shapiro | 99fc456 | 2010-07-09 17:35:15 -0700 | [diff] [blame] | 52 | #define MIN(x,y) (((x) < (y)) ? (x) : (y)) |
| 53 | #define MAX(x,y) (((x) > (y)) ? (x) : (y)) |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 54 | |
| Carl Shapiro | a20a992 | 2010-07-29 21:22:12 -0700 | [diff] [blame] | 55 | #define LIKELY(exp) (__builtin_expect((exp) != 0, true)) |
| 56 | #define UNLIKELY(exp) (__builtin_expect((exp) != 0, false)) |
| 57 | |
| Carl Shapiro | cc3f8b4 | 2011-02-08 20:10:56 -0800 | [diff] [blame] | 58 | #define ALIGN_UP(x, n) (((size_t)(x) + (n) - 1) & ~((n) - 1)) |
| 59 | #define ALIGN_DOWN(x, n) ((size_t)(x) & -(n)) |
| 60 | #define ALIGN_UP_TO_PAGE_SIZE(p) ALIGN_UP(p, SYSTEM_PAGE_SIZE) |
| 61 | #define ALIGN_DOWN_TO_PAGE_SIZE(p) ALIGN_DOWN(p, SYSTEM_PAGE_SIZE) |
| 62 | |
| Carl Shapiro | 67f9be7 | 2011-02-08 19:54:55 -0800 | [diff] [blame] | 63 | #define CLZ(x) __builtin_clz(x) |
| 64 | |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 65 | /* |
| 66 | * If "very verbose" logging is enabled, make it equivalent to LOGV. |
| 67 | * Otherwise, make it disappear. |
| 68 | * |
| 69 | * Define this above the #include "Dalvik.h" to enable for only a |
| 70 | * single file. |
| 71 | */ |
| 72 | /* #define VERY_VERBOSE_LOG */ |
| 73 | #if defined(VERY_VERBOSE_LOG) |
| 74 | # define LOGVV LOGV |
| 75 | # define IF_LOGVV() IF_LOGV() |
| 76 | #else |
| 77 | # define LOGVV(...) ((void)0) |
| 78 | # define IF_LOGVV() if (false) |
| 79 | #endif |
| 80 | |
| 81 | |
| 82 | /* |
| 83 | * These match the definitions in the VM specification. |
| 84 | */ |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 85 | typedef uint8_t u1; |
| 86 | typedef uint16_t u2; |
| 87 | typedef uint32_t u4; |
| 88 | typedef uint64_t u8; |
| 89 | typedef int8_t s1; |
| 90 | typedef int16_t s2; |
| 91 | typedef int32_t s4; |
| 92 | typedef int64_t s8; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 93 | |
| 94 | /* |
| 95 | * Storage for primitive types and object references. |
| 96 | * |
| 97 | * Some parts of the code (notably object field access) assume that values |
| 98 | * are "left aligned", i.e. given "JValue jv", "jv.i" and "*((s4*)&jv)" |
| 99 | * yield the same result. This seems to be guaranteed by gcc on big- and |
| 100 | * little-endian systems. |
| 101 | */ |
| Carl Shapiro | 92a3b69 | 2011-04-29 19:19:46 -0700 | [diff] [blame] | 102 | struct Object; |
| 103 | |
| 104 | union JValue { |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 105 | u1 z; |
| 106 | s1 b; |
| 107 | u2 c; |
| 108 | s2 s; |
| 109 | s4 i; |
| 110 | s8 j; |
| 111 | float f; |
| 112 | double d; |
| Carl Shapiro | 92a3b69 | 2011-04-29 19:19:46 -0700 | [diff] [blame] | 113 | Object* l; |
| 114 | }; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 115 | |
| Carl Shapiro | 7cc095f | 2011-05-02 20:01:42 -0700 | [diff] [blame] | 116 | #define OFFSETOF_MEMBER(t, f) \ |
| 117 | (reinterpret_cast<char*>( \ |
| 118 | &reinterpret_cast<t*>(16)->f) - \ |
| 119 | reinterpret_cast<char*>(16)) |
| 120 | |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 121 | #define NELEM(x) ((int) (sizeof(x) / sizeof((x)[0]))) |
| 122 | |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 123 | #endif /*_DALVIK_COMMON*/ |