summaryrefslogtreecommitdiffstats
path: root/base
diff options
context:
space:
mode:
authorElliott Hughes2018-04-06 19:45:22 -0500
committerElliott Hughes2018-04-11 14:29:50 -0500
commit1b86d41c7844729768b2ffd096e1cded3c145a93 (patch)
tree54017f8b404487b71258a3aa8b78a9979eb2eecd /base
parent1b2f2ee6215145d9d2af0bba6c0b847fec083107 (diff)
downloadplatform-system-core-1b86d41c7844729768b2ffd096e1cded3c145a93.tar.gz
platform-system-core-1b86d41c7844729768b2ffd096e1cded3c145a93.tar.xz
platform-system-core-1b86d41c7844729768b2ffd096e1cded3c145a93.zip
Add SIZEOF_MEMBER.
Bug: N/A Test: ran tests Change-Id: Icb7e8ae83d242c867b71a990b08eb1a62ed1482c
Diffstat (limited to 'base')
-rw-r--r--base/Android.bp1
-rw-r--r--base/include/android-base/macros.h4
-rw-r--r--base/macros_test.cpp30
3 files changed, 35 insertions, 0 deletions
diff --git a/base/Android.bp b/base/Android.bp
index 7b0ba11d0..12b396957 100644
--- a/base/Android.bp
+++ b/base/Android.bp
@@ -124,6 +124,7 @@ cc_test {
124 "errors_test.cpp", 124 "errors_test.cpp",
125 "file_test.cpp", 125 "file_test.cpp",
126 "logging_test.cpp", 126 "logging_test.cpp",
127 "macros_test.cpp",
127 "parsedouble_test.cpp", 128 "parsedouble_test.cpp",
128 "parseint_test.cpp", 129 "parseint_test.cpp",
129 "parsenetaddress_test.cpp", 130 "parsenetaddress_test.cpp",
diff --git a/base/include/android-base/macros.h b/base/include/android-base/macros.h
index 25f2ff4ed..fd6efb2a8 100644
--- a/base/include/android-base/macros.h
+++ b/base/include/android-base/macros.h
@@ -20,6 +20,8 @@
20#include <stddef.h> // for size_t 20#include <stddef.h> // for size_t
21#include <unistd.h> // for TEMP_FAILURE_RETRY 21#include <unistd.h> // for TEMP_FAILURE_RETRY
22 22
23#include <utility>
24
23// bionic and glibc both have TEMP_FAILURE_RETRY, but eg Mac OS' libc doesn't. 25// bionic and glibc both have TEMP_FAILURE_RETRY, but eg Mac OS' libc doesn't.
24#ifndef TEMP_FAILURE_RETRY 26#ifndef TEMP_FAILURE_RETRY
25#define TEMP_FAILURE_RETRY(exp) \ 27#define TEMP_FAILURE_RETRY(exp) \
@@ -114,6 +116,8 @@ char(&ArraySizeHelper(T(&array)[N]))[N]; // NOLINT(readability/casting)
114 ((sizeof(a) / sizeof(*(a))) / \ 116 ((sizeof(a) / sizeof(*(a))) / \
115 static_cast<size_t>(!(sizeof(a) % sizeof(*(a))))) 117 static_cast<size_t>(!(sizeof(a) % sizeof(*(a)))))
116 118
119#define SIZEOF_MEMBER(t, f) sizeof(std::declval<t>().f)
120
117// Changing this definition will cause you a lot of pain. A majority of 121// Changing this definition will cause you a lot of pain. A majority of
118// vendor code defines LIKELY and UNLIKELY this way, and includes 122// vendor code defines LIKELY and UNLIKELY this way, and includes
119// this header through an indirect path. 123// this header through an indirect path.
diff --git a/base/macros_test.cpp b/base/macros_test.cpp
new file mode 100644
index 000000000..2b522db5c
--- /dev/null
+++ b/base/macros_test.cpp
@@ -0,0 +1,30 @@
1/*
2 * Copyright (C) 2018 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#include "android-base/macros.h"
18
19#include <stdint.h>
20
21#include <gtest/gtest.h>
22
23TEST(macros, SIZEOF_MEMBER_macro) {
24 struct S {
25 int32_t i32;
26 double d;
27 };
28 ASSERT_EQ(4U, SIZEOF_MEMBER(S, i32));
29 ASSERT_EQ(8U, SIZEOF_MEMBER(S, d));
30}