1 //===-- safestack_util.h --------------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file contains utility code for SafeStack implementation.
11 //===----------------------------------------------------------------------===//
13 #ifndef SAFESTACK_UTIL_H
14 #define SAFESTACK_UTIL_H
22 #define SFS_CHECK(a) \
25 fprintf(stderr, "safestack CHECK failed: %s:%d %s\n", __FILE__, \
31 inline size_t RoundUpTo(size_t size
, size_t boundary
) {
32 SFS_CHECK((boundary
& (boundary
- 1)) == 0);
33 return (size
+ boundary
- 1) & ~(boundary
- 1);
36 inline constexpr bool IsAligned(size_t a
, size_t alignment
) {
37 return (a
& (alignment
- 1)) == 0;
42 explicit MutexLock(pthread_mutex_t
&mutex
) : mutex_(&mutex
) {
43 pthread_mutex_lock(mutex_
);
45 ~MutexLock() { pthread_mutex_unlock(mutex_
); }
48 pthread_mutex_t
*mutex_
= nullptr;
51 } // namespace safestack
53 #endif // SAFESTACK_UTIL_H