osi.h 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /******************************************************************************
  2. *
  3. * Copyright 2016 Google, Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at:
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. ******************************************************************************/
  18. #pragma once
  19. #include <stdbool.h>
  20. #include <stdint.h>
  21. #define UNUSED_ATTR __attribute__((unused))
  22. #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
  23. #define INVALID_FD (-1)
  24. #define CONCAT(a, b) a##b
  25. // Use during compile time to check conditional values
  26. // NOTE: The the failures will present as a generic error
  27. // "error: initialization makes pointer from integer without a cast"
  28. // but the file and line number will present the condition that
  29. // failed.
  30. #define DUMMY_COUNTER(c) CONCAT(__osi_dummy_, c)
  31. #define DUMMY_PTR DUMMY_COUNTER(__COUNTER__)
  32. // Macros for safe integer to pointer conversion. In the C language, data is
  33. // commonly cast to opaque pointer containers and back for generic parameter
  34. // passing in callbacks. These macros should be used sparingly in new code
  35. // (never in C++ code). Whenever integers need to be passed as a pointer, use
  36. // these macros.
  37. #define PTR_TO_UINT(p) ((unsigned int)((uintptr_t)(p)))
  38. #define UINT_TO_PTR(u) ((void*)((uintptr_t)(u)))
  39. #define PTR_TO_INT(p) ((int)((intptr_t)(p)))
  40. #define INT_TO_PTR(i) ((void*)((intptr_t)(i)))
  41. // Obtain a random number between 0 and INT_MAX inclusive.
  42. // Taken from a system random source such as /dev/random.
  43. // No guarantees of distribution are made.
  44. // Effort is made for this to come from a real random source.
  45. int osi_rand(void);
  46. // Re-run |fn| system call until the system call doesn't cause EINTR.
  47. #define OSI_NO_INTR(fn) \
  48. do { \
  49. } while ((fn) == -1 && errno == EINTR)