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