blob: ae0938aeae1e196291daa24bf5e6817f54efb658 [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", \
34 __FILE__, __LINE__, #x), *(int*)39=39, 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
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080043/*
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 */
65typedef uint8_t u1;
66typedef uint16_t u2;
67typedef uint32_t u4;
68typedef uint64_t u8;
69typedef int8_t s1;
70typedef int16_t s2;
71typedef int32_t s4;
72typedef int64_t s8;
73#else
74typedef unsigned char u1;
75typedef unsigned short u2;
76typedef unsigned int u4;
77typedef unsigned long long u8;
78typedef signed char s1;
79typedef signed short s2;
80typedef signed int s4;
81typedef 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 */
92typedef 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/*
Andy McFaddend51370f2009-08-05 15:20:27 -0700105 * The <stdbool.h> definition uses _Bool, a type known to the compiler.
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800106 */
Andy McFaddend51370f2009-08-05 15:20:27 -0700107#ifdef HAVE_STDBOOL_H
108# include <stdbool.h> /* C99 */
109#else
110# ifndef __bool_true_false_are_defined
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800111typedef enum { false=0, true=!false } bool;
Andy McFaddend51370f2009-08-05 15:20:27 -0700112# define __bool_true_false_are_defined 1
113# endif
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800114#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*/