Path: blob/main/contrib/llvm-project/compiler-rt/lib/safestack/safestack_util.h
35260 views
//===-- safestack_util.h --------------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This file contains utility code for SafeStack implementation.9//10//===----------------------------------------------------------------------===//1112#ifndef SAFESTACK_UTIL_H13#define SAFESTACK_UTIL_H1415#include <pthread.h>16#include <stdio.h>17#include <stdlib.h>1819namespace safestack {2021#define SFS_CHECK(a) \22do { \23if (!(a)) { \24fprintf(stderr, "safestack CHECK failed: %s:%d %s\n", __FILE__, \25__LINE__, #a); \26abort(); \27}; \28} while (false)2930inline size_t RoundUpTo(size_t size, size_t boundary) {31SFS_CHECK((boundary & (boundary - 1)) == 0);32return (size + boundary - 1) & ~(boundary - 1);33}3435inline constexpr bool IsAligned(size_t a, size_t alignment) {36return (a & (alignment - 1)) == 0;37}3839class MutexLock {40public:41explicit MutexLock(pthread_mutex_t &mutex) : mutex_(&mutex) {42pthread_mutex_lock(mutex_);43}44~MutexLock() { pthread_mutex_unlock(mutex_); }4546private:47pthread_mutex_t *mutex_ = nullptr;48};4950} // namespace safestack5152#endif // SAFESTACK_UTIL_H535455