Common.h revision 92a3b69286a757afff76402d8a0d5ec04d7f177c
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 */
16
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 <stdbool.h>
28#include <stdint.h>
29#include <stdio.h>
30#include <assert.h>
31#include <endian.h>
32#include "utils/Log.h"
33
34#if !defined(NDEBUG) && defined(WITH_DALVIK_ASSERT)
35# undef assert
36# define assert(x) \
37    ((x) ? ((void)0) : (LOGE("ASSERT FAILED (%s:%d): %s\n", \
38        __FILE__, __LINE__, #x), *(int*)39=39, (void)0) )
39#endif
40
41#define MIN(x,y) (((x) < (y)) ? (x) : (y))
42#define MAX(x,y) (((x) > (y)) ? (x) : (y))
43
44#define LIKELY(exp) (__builtin_expect((exp) != 0, true))
45#define UNLIKELY(exp) (__builtin_expect((exp) != 0, false))
46
47#define ALIGN_UP(x, n) (((size_t)(x) + (n) - 1) & ~((n) - 1))
48#define ALIGN_DOWN(x, n) ((size_t)(x) & -(n))
49#define ALIGN_UP_TO_PAGE_SIZE(p) ALIGN_UP(p, SYSTEM_PAGE_SIZE)
50#define ALIGN_DOWN_TO_PAGE_SIZE(p) ALIGN_DOWN(p, SYSTEM_PAGE_SIZE)
51
52#define CLZ(x) __builtin_clz(x)
53
54/*
55 * If "very verbose" logging is enabled, make it equivalent to LOGV.
56 * Otherwise, make it disappear.
57 *
58 * Define this above the #include "Dalvik.h" to enable for only a
59 * single file.
60 */
61/* #define VERY_VERBOSE_LOG */
62#if defined(VERY_VERBOSE_LOG)
63# define LOGVV      LOGV
64# define IF_LOGVV() IF_LOGV()
65#else
66# define LOGVV(...) ((void)0)
67# define IF_LOGVV() if (false)
68#endif
69
70
71/*
72 * These match the definitions in the VM specification.
73 */
74typedef uint8_t             u1;
75typedef uint16_t            u2;
76typedef uint32_t            u4;
77typedef uint64_t            u8;
78typedef int8_t              s1;
79typedef int16_t             s2;
80typedef int32_t             s4;
81typedef int64_t             s8;
82
83/*
84 * Storage for primitive types and object references.
85 *
86 * Some parts of the code (notably object field access) assume that values
87 * are "left aligned", i.e. given "JValue jv", "jv.i" and "*((s4*)&jv)"
88 * yield the same result.  This seems to be guaranteed by gcc on big- and
89 * little-endian systems.
90 */
91struct Object;
92
93union JValue {
94    u1      z;
95    s1      b;
96    u2      c;
97    s2      s;
98    s4      i;
99    s8      j;
100    float   f;
101    double  d;
102    Object* l;
103};
104
105#define NELEM(x) ((int) (sizeof(x) / sizeof((x)[0])))
106
107#endif /*_DALVIK_COMMON*/
108