#include <stdlib.h>
#include "exit_if_null.h"
+#include "sizeof.h"
-#define CALLOC(n, T) \
+#define CALLOC(n, T) CALLOC_(n, typeas(T))
+#define CALLOC_(n, T) \
( \
(T *) calloc(n, sizeof(T)) \
)
#include "attr.h"
#include "exit_if_null.h"
+#include "sizeof.h"
-#define MALLOC(n, T) \
+#define MALLOC(n, T) MALLOC_(n, typeas(T))
+#define MALLOC_(n, T) \
( \
(T *) mallocarray(n, sizeof(T)) \
)
#include <stdlib.h>
#include "exit_if_null.h"
+#include "sizeof.h"
-#define REALLOC(p, n, T) \
+#define REALLOC(p, n, T) REALLOC_(p, n, typeas(T))
+#define REALLOC_(p, n, T) \
( \
_Generic(p, T *: (T *) reallocarray(p, (n) ?: 1, sizeof(T))) \
)
-// SPDX-FileCopyrightText: 2023-2024, Alejandro Colomar <alx@kernel.org>
+// SPDX-FileCopyrightText: 2023-2025, Alejandro Colomar <alx@kernel.org>
// SPDX-License-Identifier: BSD-3-Clause
#include <stdlib.h>
#include "attr.h"
+#include "sizeof.h"
-#define REALLOCF(p, n, T) \
+#define REALLOCF(p, n, T) REALLOCF_(p, n, typeas(T))
+#define REALLOCF_(p, n, T) \
( \
_Generic(p, T *: (T *) reallocarrayf(p, (n) ?: 1, sizeof(T))) \
)
#include <stddef.h>
#include "search/cmp/cmp.h"
+#include "sizeof.h"
// lfind_T - linear find type-safe
-#define lfind_T(T, k, a, n, cmp) \
+#define lfind_T(T, ...) lfind_T_(typeas(T), __VA_ARGS__)
+#define lfind_T_(T, k, a, n, cmp) \
({ \
_Generic(k, T *: 0, const T *: 0); \
_Generic(a, T *: 0, const T *: 0); \
#include <search.h>
#include "search/cmp/cmp.h"
+#include "sizeof.h"
// lsearch_T - linear search-and-insert type-safe
-#define lsearch_T(T, k, a, n, cmp) \
+#define lsearch_T(T, ...) lsearch_T_(typeas(T), __VA_ARGS__)
+#define lsearch_T_(T, k, a, n, cmp) \
({ \
_Generic(k, T *: 0, const T *: 0); \
_Generic(a, T *: 0); \
#include <stdlib.h>
#include "search/cmp/cmp.h"
+#include "sizeof.h"
// qsort_T - sort type-safe
-#define qsort_T(T, a, n, cmp) do \
+#define qsort_T(T, ...) qsort_T_(typeas(T), __VA_ARGS__)
+#define qsort_T_(T, a, n, cmp) do \
{ \
_Generic(a, T *: 0); \
qsort(a, n, sizeof(T), cmp); \