fix: avoid macro redefinition by namespacing helpers
Rename is_aligned and align_up to libtl_* variants and move header to
private include path. Prevents clashes with external projects that
define similar macros (e.g., TF-A).
Change-Id: I7e13efa55fe120dccc68d2a941dea7cdd1f005a6
Signed-off-by: Harrison Mutai <harrison.mutai@arm.com>
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 71b5b35..b69df14 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -21,7 +21,11 @@
SET(TARGET_GROUP release CACHE STRING "Specify the Build Target [\"release\" by default]")
add_library(tl STATIC "${CMAKE_CURRENT_SOURCE_DIR}/src/transfer_list.c")
-target_include_directories(tl PUBLIC include)
+target_include_directories(tl
+ PUBLIC
+ ${PROJECT_SOURCE_DIR}/include
+)
+
target_link_libraries(tl PUBLIC cxx_compiler_flags)
if(TARGET_GROUP STREQUAL test)
diff --git a/include/math_utils.h b/include/private/math_utils.h
similarity index 75%
rename from include/math_utils.h
rename to include/private/math_utils.h
index 6966287..ba1bb9c 100644
--- a/include/math_utils.h
+++ b/include/private/math_utils.h
@@ -16,7 +16,7 @@
* @param boundary The alignment boundary (must be a power of two).
* @return The smallest multiple of `boundary` that is greater than or equal to `value`.
*/
-static inline uintptr_t align_up(uintptr_t value, uintptr_t boundary)
+static inline uintptr_t libtl_align_up(uintptr_t value, uintptr_t boundary)
{
return (value + boundary - 1) & ~(boundary - 1);
}
@@ -28,7 +28,8 @@
* @param boundary The alignment boundary (should be a power of two for efficiency).
* @return `1` if `value` is aligned, `0` otherwise.
*/
-static inline int is_aligned(uintptr_t value, uintptr_t boundary) {
+static inline int libtl_is_aligned(uintptr_t value, uintptr_t boundary)
+{
return (value % boundary) == 0;
}
@@ -40,7 +41,7 @@
* @param res Pointer to store the result if no overflow occurs.
* @return `1` if overflow occurs, `0` otherwise.
*/
-#define add_overflow(a, b, res) __builtin_add_overflow((a), (b), (res))
+#define libtl_add_overflow(a, b, res) __builtin_add_overflow((a), (b), (res))
/**
* @brief Rounds up `v` to the nearest multiple of `size`, detecting overflow.
@@ -50,13 +51,13 @@
* @param res Pointer to store the rounded-up result.
* @return `1` if an overflow occurs, `0` otherwise.
*/
-#define round_up_overflow(v, size, res) \
+#define libtl_round_up_overflow(v, size, res) \
(__extension__({ \
typeof(res) __res = res; \
typeof(*(__res)) __roundup_tmp = 0; \
typeof(v) __roundup_mask = (typeof(v))(size) - 1; \
\
- add_overflow((v), __roundup_mask, &__roundup_tmp) ? \
+ libtl_add_overflow((v), __roundup_mask, &__roundup_tmp) ? \
1 : \
(void)(*(__res) = __roundup_tmp & ~__roundup_mask), \
0; \
@@ -72,14 +73,14 @@
* @param res Pointer to store the final rounded result.
* @return `1` if an overflow occurs during addition or rounding, `0` otherwise.
*/
-#define add_with_round_up_overflow(a, b, size, res) \
- (__extension__({ \
- typeof(a) __a = (a); \
- typeof(__a) __add_res = 0; \
- \
- add_overflow((__a), (b), &__add_res) ? 1 : \
- round_up_overflow(__add_res, (size), (res)) ? 1 : \
- 0; \
+#define libtl_add_with_round_up_overflow(a, b, size, res) \
+ (__extension__({ \
+ typeof(a) __a = (a); \
+ typeof(__a) __add_res = 0; \
+ \
+ libtl_add_overflow((__a), (b), &__add_res) ? 1 : \
+ libtl_round_up_overflow(__add_res, (size), (res)) ? 1 : \
+ 0; \
}))
#endif /* MATH_UTILS_H */
diff --git a/src/transfer_list.c b/src/transfer_list.c
index b330350..2aa584c 100644
--- a/src/transfer_list.c
+++ b/src/transfer_list.c
@@ -9,7 +9,7 @@
#include <stdio.h>
#include <string.h>
-#include <math_utils.h>
+#include <private/math_utils.h>
#include <transfer_list.h>
void transfer_list_dump(struct transfer_list_header *tl)
@@ -64,8 +64,9 @@
return NULL;
}
- if (!is_aligned((uintptr_t)addr, 1 << TRANSFER_LIST_INIT_MAX_ALIGN) ||
- !is_aligned(max_size, 1 << TRANSFER_LIST_INIT_MAX_ALIGN) ||
+ if (!libtl_is_aligned((uintptr_t)addr,
+ 1 << TRANSFER_LIST_INIT_MAX_ALIGN) ||
+ !libtl_is_aligned(max_size, 1 << TRANSFER_LIST_INIT_MAX_ALIGN) ||
max_size < sizeof(*tl)) {
return NULL;
}
@@ -203,12 +204,12 @@
if (last) {
va = (uintptr_t)last;
/* check if the total size overflow */
- if (add_overflow(last->hdr_size, last->data_size, &sz)) {
+ if (libtl_add_overflow(last->hdr_size, last->data_size, &sz)) {
return NULL;
}
/* roundup to the next entry */
- if (add_with_round_up_overflow(va, sz, TRANSFER_LIST_GRANULE,
- &va)) {
+ if (libtl_add_with_round_up_overflow(
+ va, sz, TRANSFER_LIST_GRANULE, &va)) {
return NULL;
}
} else {
@@ -218,8 +219,8 @@
te = (struct transfer_list_entry *)va;
if (va + sizeof(*te) > tl_ev || te->hdr_size < sizeof(*te) ||
- add_overflow(te->hdr_size, te->data_size, &sz) ||
- add_overflow(va, sz, &ev) || ev > tl_ev) {
+ libtl_add_overflow(te->hdr_size, te->data_size, &sz) ||
+ libtl_add_overflow(va, sz, &ev) || ev > tl_ev) {
return NULL;
}
@@ -324,14 +325,14 @@
* calculate the old and new end of TE
* both must be roundup to align with TRANSFER_LIST_GRANULE
*/
- if (add_overflow(te->hdr_size, te->data_size, &sz) ||
- add_with_round_up_overflow((uintptr_t)te, sz, TRANSFER_LIST_GRANULE,
- &old_ev)) {
+ if (libtl_add_overflow(te->hdr_size, te->data_size, &sz) ||
+ libtl_add_with_round_up_overflow((uintptr_t)te, sz,
+ TRANSFER_LIST_GRANULE, &old_ev)) {
return false;
}
- if (add_overflow(te->hdr_size, new_data_size, &sz) ||
- add_with_round_up_overflow((uintptr_t)te, sz, TRANSFER_LIST_GRANULE,
- &new_ev)) {
+ if (libtl_add_overflow(te->hdr_size, new_data_size, &sz) ||
+ libtl_add_with_round_up_overflow((uintptr_t)te, sz,
+ TRANSFER_LIST_GRANULE, &new_ev)) {
return false;
}
@@ -346,9 +347,9 @@
*/
dummy_te = transfer_list_next(tl, te);
if (dummy_te && (dummy_te->tag_id == TL_TAG_EMPTY)) {
- merge_ev = align_up(old_ev + dummy_te->hdr_size +
- dummy_te->data_size,
- TRANSFER_LIST_GRANULE);
+ merge_ev = libtl_align_up(old_ev + dummy_te->hdr_size +
+ dummy_te->data_size,
+ TRANSFER_LIST_GRANULE);
if (merge_ev >= new_ev) {
gap = merge_ev - new_ev;
goto set_dummy;
@@ -364,7 +365,8 @@
* the max size of TL
*/
mov_dis = new_ev - old_ev;
- if (round_up_overflow(mov_dis, 1 << tl->alignment, &mov_dis) ||
+ if (libtl_round_up_overflow(mov_dis, 1 << tl->alignment,
+ &mov_dis) ||
tl->size + mov_dis > tl->max_size) {
return false;
}
@@ -409,14 +411,15 @@
next = transfer_list_next(tl, te);
if (prev && prev->tag_id == TL_TAG_EMPTY) {
- prev->data_size += align_up(te->hdr_size + te->data_size,
- TRANSFER_LIST_GRANULE);
+ prev->data_size += libtl_align_up(te->hdr_size + te->data_size,
+ TRANSFER_LIST_GRANULE);
te = prev;
}
if (next && next->tag_id == TL_TAG_EMPTY) {
- te->data_size += align_up(next->hdr_size + next->data_size,
- TRANSFER_LIST_GRANULE);
+ te->data_size +=
+ libtl_align_up(next->hdr_size + next->data_size,
+ TRANSFER_LIST_GRANULE);
}
te->tag_id = TL_TAG_EMPTY;
@@ -448,11 +451,11 @@
* new TE will be added into the tail
*/
tl_ev = (uintptr_t)tl + tl->size;
- te = (struct transfer_list_entry *)align_up(tl_ev,
- TRANSFER_LIST_GRANULE);
+ te = (struct transfer_list_entry *)libtl_align_up(
+ tl_ev, TRANSFER_LIST_GRANULE);
- te_end = align_up((uintptr_t)te + sizeof(*te) + data_size,
- TRANSFER_LIST_GRANULE);
+ te_end = libtl_align_up((uintptr_t)te + sizeof(*te) + data_size,
+ TRANSFER_LIST_GRANULE);
if (te_end > (uintptr_t)tl + tl->max_size) {
return NULL;
@@ -499,13 +502,13 @@
tl_ev = (uintptr_t)tl + tl->size;
ev = tl_ev + sizeof(struct transfer_list_entry);
- if (!is_aligned(ev, 1 << alignment)) {
+ if (!libtl_is_aligned(ev, 1 << alignment)) {
/*
* TE data address is not aligned to the new alignment
* fill the gap with an empty TE as a placeholder before
* adding the desire TE
*/
- new_tl_ev = align_up(ev, 1 << alignment) -
+ new_tl_ev = libtl_align_up(ev, 1 << alignment) -
sizeof(struct transfer_list_entry);
dummy_te_data_sz =
new_tl_ev - tl_ev - sizeof(struct transfer_list_entry);