blob: 303218e283deb0c90ec82f993bf0a800c50345dd [file] [log] [blame]
Elliott Hughes8eac9af2014-05-09 19:12:08 -07001/*
2 * Copyright (C) 2010 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#ifndef _BIONIC_MACROS_H_
18#define _BIONIC_MACROS_H_
19
Evgenii Stepanovd13e9a62016-07-15 16:31:42 -070020#include <stdint.h>
21
Andreas Gampe00bbc7f2014-11-10 22:04:08 -080022// Frameworks OpenGL code currently leaks this header and allows
23// collisions with other declarations, e.g., from libnativehelper.
24// TODO: Remove once cleaned up. b/18334516
25#if !defined(DISALLOW_COPY_AND_ASSIGN)
Elliott Hughes8eac9af2014-05-09 19:12:08 -070026// DISALLOW_COPY_AND_ASSIGN disallows the copy and operator= functions.
27// It goes in the private: declarations in a class.
28#define DISALLOW_COPY_AND_ASSIGN(TypeName) \
Dmitriy Ivanovd9ff7222014-09-08 16:22:22 -070029 TypeName(const TypeName&) = delete; \
30 void operator=(const TypeName&) = delete
Andreas Gampe00bbc7f2014-11-10 22:04:08 -080031#endif // !defined(DISALLOW_COPY_AND_ASSIGN)
Elliott Hughes8eac9af2014-05-09 19:12:08 -070032
33// A macro to disallow all the implicit constructors, namely the
34// default constructor, copy constructor and operator= functions.
35//
36// This should be used in the private: declarations for a class
37// that wants to prevent anyone from instantiating it. This is
38// especially useful for classes containing only static methods.
39#define DISALLOW_IMPLICIT_CONSTRUCTORS(TypeName) \
Dmitriy Ivanovd9ff7222014-09-08 16:22:22 -070040 TypeName() = delete; \
Elliott Hughes8eac9af2014-05-09 19:12:08 -070041 DISALLOW_COPY_AND_ASSIGN(TypeName)
42
Christopher Ferris03eebcb2014-06-13 13:57:51 -070043#define BIONIC_ALIGN(value, alignment) \
44 (((value) + (alignment) - 1) & ~((alignment) - 1))
45
46#define BIONIC_ROUND_UP_POWER_OF_2(value) \
Christopher Ferris0b13f292015-12-16 16:11:04 -080047 ((sizeof(value) == 8) \
Christopher Ferris27047fa2014-07-14 18:47:23 -070048 ? (1UL << (64 - __builtin_clzl(static_cast<unsigned long>(value)))) \
Christopher Ferris0b13f292015-12-16 16:11:04 -080049 : (1UL << (32 - __builtin_clz(static_cast<unsigned int>(value)))))
Christopher Ferris03eebcb2014-06-13 13:57:51 -070050
Evgenii Stepanov0a3637d2016-07-06 13:20:59 -070051static constexpr uintptr_t align_down(uintptr_t p, size_t align) {
Evgenii Stepanovd13e9a62016-07-15 16:31:42 -070052 return p & ~(align - 1);
53}
54
Evgenii Stepanov0a3637d2016-07-06 13:20:59 -070055static constexpr uintptr_t align_up(uintptr_t p, size_t align) {
Evgenii Stepanovd13e9a62016-07-15 16:31:42 -070056 return (p + align - 1) & ~(align - 1);
57}
58
59template <typename T>
60static inline T* align_down(T* p, size_t align) {
61 return reinterpret_cast<T*>(align_down(reinterpret_cast<uintptr_t>(p), align));
62}
63
64template <typename T>
65static inline T* align_up(T* p, size_t align) {
66 return reinterpret_cast<T*>(align_up(reinterpret_cast<uintptr_t>(p), align));
67}
68
Elliott Hughes8eac9af2014-05-09 19:12:08 -070069#endif // _BIONIC_MACROS_H_