blob: 19de943d4c1f62168589635ffdd53ed86492e50e [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
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080048/*
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 */
70typedef uint8_t u1;
71typedef uint16_t u2;
72typedef uint32_t u4;
73typedef uint64_t u8;
74typedef int8_t s1;
75typedef int16_t s2;
76typedef int32_t s4;
77typedef int64_t s8;
78#else
79typedef unsigned char u1;
80typedef unsigned short u2;
81typedef unsigned int u4;
82typedef unsigned long long u8;
83typedef signed char s1;
84typedef signed short s2;
85typedef signed int s4;
86typedef 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 */
97typedef 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 McFaddend51370f2009-08-05 15:20:27 -0700110 * The <stdbool.h> definition uses _Bool, a type known to the compiler.
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800111 */
Andy McFaddend51370f2009-08-05 15:20:27 -0700112#ifdef HAVE_STDBOOL_H
113# include <stdbool.h> /* C99 */
114#else
115# ifndef __bool_true_false_are_defined
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800116typedef enum { false=0, true=!false } bool;
Andy McFaddend51370f2009-08-05 15:20:27 -0700117# define __bool_true_false_are_defined 1
118# endif
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800119#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*/