safestack_util.h 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. //===-- safestack_util.h --------------------------------------------------===//
  2. //
  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
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file contains utility code for SafeStack implementation.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #ifndef SAFESTACK_UTIL_H
  13. #define SAFESTACK_UTIL_H
  14. #include <pthread.h>
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. namespace safestack {
  18. #define SFS_CHECK(a) \
  19. do { \
  20. if (!(a)) { \
  21. fprintf(stderr, "safestack CHECK failed: %s:%d %s\n", __FILE__, \
  22. __LINE__, #a); \
  23. abort(); \
  24. }; \
  25. } while (false)
  26. inline size_t RoundUpTo(size_t size, size_t boundary) {
  27. SFS_CHECK((boundary & (boundary - 1)) == 0);
  28. return (size + boundary - 1) & ~(boundary - 1);
  29. }
  30. class MutexLock {
  31. public:
  32. explicit MutexLock(pthread_mutex_t &mutex) : mutex_(&mutex) {
  33. pthread_mutex_lock(mutex_);
  34. }
  35. ~MutexLock() { pthread_mutex_unlock(mutex_); }
  36. private:
  37. pthread_mutex_t *mutex_ = nullptr;
  38. };
  39. } // namespace safestack
  40. #endif // SAFESTACK_UTIL_H