Files
openairinterface5g/common/utils/eq_check.h
T
Guido Casati 563a0cfa61 Refactor: unify equality-check macros across protocol libs
This commit centralizes equality-check behavior and removes duplicated
macro implementations across protocol stacks.

Major changes:
- add `common/utils/eq_check.h` with shared `_EQ_CHECK_*`,
  `EQ_CHECK_GENERIC`, and optional-IE pointer helpers
- replace E1AP/F1AP/NAS local equality macros and call sites with
  the shared helpers
- remove duplicated layer-specific error/equality macro definitions from
  common headers and switch includes to `eq_check.h`
- update CMake test flags to define `ENABLE_TESTS` for `e1ap_lib`,
  and 5GS NAS libraries; drop `E1AP_LOG_ERRORS` - equality-check/error
  printing is now consistently controlled by `ENABLE_TESTS`
2026-02-17 12:25:58 +01:00

82 lines
4.0 KiB
C

/*
* Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The OpenAirInterface Software Alliance licenses this file to You under
* the OAI Public License, Version 1.1 (the "License"); you may not use this file
* except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.openairinterface.org/?page_id=698
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*-------------------------------------------------------------------------------
* For more information about the OpenAirInterface (OAI) Software Alliance:
* contact@openairinterface.org
*/
#ifndef EQ_CHECK_H_
#define EQ_CHECK_H_
#include <stdio.h>
#include <string.h>
/*
* Layers may override PRINT_ERROR() before including this header to preserve
* custom logging style (for example by mapping it to a local PRINT_ERROR()).
*/
#ifndef PRINT_ERROR
#ifdef ENABLE_TESTS
#define PRINT_ERROR(fmt, ...) fprintf(stderr, "%s:%d: " fmt, __FILE__, __LINE__, ##__VA_ARGS__)
#else
#define PRINT_ERROR(...) \
do { \
} while (0)
#endif
#endif
#ifndef EQ_CHECK_FAIL
#define EQ_CHECK_FAIL() return false
#endif
#define EQ_CHECK_GENERIC(condition, fmt, ...) \
do { \
if (!(condition)) { \
PRINT_ERROR("Equality condition '%s' failed: " fmt " != " fmt "\n", #condition, ##__VA_ARGS__); \
EQ_CHECK_FAIL(); \
} \
} while (0)
#define _EQ_CHECK_LONG(A, B) EQ_CHECK_GENERIC((A) == (B), "%ld", (A), (B))
#define _EQ_CHECK_INT(A, B) EQ_CHECK_GENERIC((A) == (B), "%d", (A), (B))
#define _EQ_CHECK_UINT32(A, B) EQ_CHECK_GENERIC((A) == (B), "%u", (A), (B))
#define _EQ_CHECK_UINT64(A, B) EQ_CHECK_GENERIC((A) == (B), "%" PRIu64, (uint64_t)(A), (uint64_t)(B))
#define _EQ_CHECK_STR(A, B) EQ_CHECK_GENERIC(strcmp((A), (B)) == 0, "'%s'", (A), (B))
#define _EQ_CHECK_OPTIONAL_IE(A, B, FIELD, EQ_MACRO) \
do { \
EQ_CHECK_GENERIC((((A)->FIELD) != NULL) == (((B)->FIELD) != NULL), "%d", ((A)->FIELD) != NULL, ((B)->FIELD) != NULL); \
if ((A)->FIELD && (B)->FIELD) \
EQ_MACRO(*(A)->FIELD, *(B)->FIELD); \
} while (0)
#define _EQ_CHECK_OPTIONAL_PTR(A, B, FIELD) \
do { \
if ((((A)->FIELD) != NULL) != (((B)->FIELD) != NULL)) { \
PRINT_ERROR("%s:%d: optional IE '%s' not allocated: %p, %p\n", \
__FILE__, \
__LINE__, \
#FIELD, \
(void *)(A)->FIELD, \
(void *)(B)->FIELD); \
EQ_CHECK_FAIL(); \
} \
} while (0)
#endif /* EQ_CHECK_H_ */