util.h 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2022 The Apache Software Foundation (ASF)
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * SPDX-FileContributor: 2019-2022 Espressif Systems (Shanghai) CO LTD
  7. */
  8. /*
  9. * Licensed to the Apache Software Foundation (ASF) under one
  10. * or more contributor license agreements. See the NOTICE file
  11. * distributed with this work for additional information
  12. * regarding copyright ownership. The ASF licenses this file
  13. * to you under the Apache License, Version 2.0 (the
  14. * "License"); you may not use this file except in compliance
  15. * with the License. You may obtain a copy of the License at
  16. *
  17. * http://www.apache.org/licenses/LICENSE-2.0
  18. *
  19. * Unless required by applicable law or agreed to in writing,
  20. * software distributed under the License is distributed on an
  21. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  22. * KIND, either express or implied. See the License for the
  23. * specific language governing permissions and limitations
  24. * under the License.
  25. */
  26. #ifndef H_OS_UTIL_
  27. #define H_OS_UTIL_
  28. /* Helpers to pass integers as pointers and vice-versa */
  29. #define POINTER_TO_UINT(p) ((unsigned int) ((uintptr_t) (p)))
  30. #define UINT_TO_POINTER(u) ((void *) ((uintptr_t) (u)))
  31. #define POINTER_TO_INT(p) ((int) ((intptr_t) (p)))
  32. #define INT_TO_POINTER(u) ((void *) ((intptr_t) (u)))
  33. /* Helper to retrieve pointer to "parent" object in structure */
  34. #define CONTAINER_OF(ptr, type, field) \
  35. ((type *)(((char *)(ptr)) - offsetof(type, field)))
  36. /* Helper to calculate number of elements in array */
  37. #ifndef ARRAY_SIZE
  38. #define ARRAY_SIZE(array) \
  39. (sizeof(array) / sizeof((array)[0]))
  40. #endif
  41. #endif