]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
contrib/mempattern: utilize modern attributes docs-mempattern-yfyu4u/deployments/9034 mempattern
authorVladimír Čunát <vladimir.cunat@nic.cz>
Wed, 6 May 2026 13:22:32 +0000 (15:22 +0200)
committerVladimír Čunát <vladimir.cunat@nic.cz>
Wed, 6 May 2026 13:43:52 +0000 (15:43 +0200)
They're mostly to aid diagnostics, but some of it may even
help performance, e.g. via aiding pointer aliasing analysis.

contrib/mempattern.c
contrib/mempattern.h

index 75c397a920f53be8a9a0551f35a0eb34c43be959..b6fcdf40aa2142cd2fd4daba3377ab0023902e13 100644 (file)
@@ -70,7 +70,13 @@ void *mm_realloc(knot_mm_t *mm, void *what, size_t size, size_t prev_size)
                                memcpy(p, what,
                                       prev_size < size ? prev_size : size);
                        }
+               #pragma GCC diagnostic push
+               #ifdef __clang__
+                       #pragma GCC diagnostic ignored "-Wunknown-warning-option"
+               #endif
+               #pragma GCC diagnostic ignored "-Wmismatched-dealloc"
                        mm_free(mm, what);
+               #pragma GCC diagnostic pop
                        return p;
                }
        } else {
@@ -80,9 +86,6 @@ void *mm_realloc(knot_mm_t *mm, void *what, size_t size, size_t prev_size)
 
 char *mm_strdup(knot_mm_t *mm, const char *s)
 {
-       if (s == NULL) {
-               return NULL;
-       }
        if (mm) {
                size_t len = strlen(s) + 1;
                void *mem = mm_alloc(mm, len);
index da299233fd30d1cd463788b385a5a0761424339e..fa84b550af6855414954061c87ab3bfd8feb2c6e 100644 (file)
 /*! \brief Default memory block size. */
 #define MM_DEFAULT_BLKSIZE 4096
 
+
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wignored-attributes"
+
+// TODO: also enable for clang at some version
+#if __GNUC__ >= 11
+       #define malloc_attr(...) malloc(__VA_ARGS__)
+#else
+       #define malloc_attr(...) malloc
+#endif
+
+/*! \brief Free using 'mm' if any, uses system free() otherwise. */
+KR_EXPORT
+void mm_free(knot_mm_t *mm, const void *what);
+
 /*! \brief Allocs using 'mm' if any, uses system malloc() otherwise. */
 KR_EXPORT
-void *mm_alloc(knot_mm_t *mm, size_t size);
+void *mm_alloc(knot_mm_t *mm, size_t size)
+__attribute__ ((malloc_attr(mm_free,2),alloc_size(2),warn_unused_result));
 
 /*! \brief Callocs using 'mm' if any, uses system calloc() otherwise. */
-void *mm_calloc(knot_mm_t *mm, size_t nmemb, size_t size);
+void *mm_calloc(knot_mm_t *mm, size_t nmemb, size_t size)
+__attribute__ ((malloc_attr(mm_free,2),alloc_size(2,3),warn_unused_result));
 
 /*! \brief Reallocs using 'mm' if any, uses system realloc() otherwise. */
 KR_EXPORT
-void *mm_realloc(knot_mm_t *mm, void *what, size_t size, size_t prev_size);
+void *mm_realloc(knot_mm_t *mm, void *what, size_t size, size_t prev_size)
+__attribute__ ((alloc_size(3),warn_unused_result));
 
 /*! \brief Strdups using 'mm' if any, uses system strdup() otherwise. */
-char *mm_strdup(knot_mm_t *mm, const char *s);
-
-/*! \brief Free using 'mm' if any, uses system free() otherwise. */
-KR_EXPORT
-void mm_free(knot_mm_t *mm, const void *what);
+char *mm_strdup(knot_mm_t *mm, const char *s)
+__attribute__ ((malloc_attr(mm_free,2),nonnull(2),warn_unused_result/*additionally to strdup()*/));
+// TODO: check in knot-dns that (s != NULL) is really assumed.
 
 /*! \brief Initialize default memory allocation context. */
 void mm_ctx_init(knot_mm_t *mm);
@@ -53,6 +69,8 @@ void mm_ctx_init(knot_mm_t *mm);
 /*! \brief Memory pool context. */
 void mm_ctx_mempool(knot_mm_t *mm, size_t chunk_size);
 
+#undef malloc_attr
+#pragma GCC diagnostic pop
 
 /* API in addition to Knot's mempattern. */