From 6871fd2c6fd6e359e7284827bd7d162ab13041d3 Mon Sep 17 00:00:00 2001 From: butterkeks Date: Sun, 2 Jun 2024 13:09:23 +0200 Subject: [PATCH] Add deallocator attribute --- include/database.h | 2 +- include/deinflector.h | 2 +- include/util.h | 10 ++++++++-- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/include/database.h b/include/database.h index 8ca72c1..448f718 100644 --- a/include/database.h +++ b/include/database.h @@ -15,7 +15,7 @@ void jdb_close(database *); void jdb_add_headword_with_file(database *db, s8 headword, s8 filepath); void jdb_add_file_with_fileinfo(database *db, s8 filepath, fileinfo fi); -s8 *jdb_get_files(database *db, s8 key); +_deallocator_(frees8buffer) s8 *jdb_get_files(database *db, s8 key); fileinfo jdb_get_fileinfo(database *db, s8 fullpath); i32 jdb_check_exists(s8 dbpath); diff --git a/include/deinflector.h b/include/deinflector.h index 7789c1c..2def618 100644 --- a/include/deinflector.h +++ b/include/deinflector.h @@ -9,7 +9,7 @@ * Returns: An s8* buffer (see buf.h) containing the deinflections. * The caller of the function takes ownership of the data, and is responsible for freeing it. */ -s8 *deinflect(s8 word); +_deallocator_(frees8buffer) s8 *deinflect(s8 word); /* * Tries to give a hiragana conversion of @input using MeCab. diff --git a/include/util.h b/include/util.h index b6db538..54c0df6 100644 --- a/include/util.h +++ b/include/util.h @@ -18,6 +18,11 @@ typedef __PTRDIFF_TYPE__ isize; #define _drop_(x) __attribute__((__cleanup__(drop_##x))) #define _nonnull_ __attribute__((nonnull)) #define _nonnull_n_(...) __attribute__((nonnull(__VA_ARGS__))) +#if defined(__clang__) + #define _deallocator_(x) +#else + #define _deallocator_(x) __attribute__((malloc(x))) +#endif #define _printf_(a, b) __attribute__((__format__(printf, a, b))) #define arrlen(x) \ @@ -35,8 +40,9 @@ typedef __PTRDIFF_TYPE__ isize; /** * Memory allocation wrapper which abort on failure */ -__attribute__((malloc, returns_nonnull)) void *xcalloc(size_t nmemb, size_t size); -__attribute__((malloc, returns_nonnull)) void *xrealloc(void *ptr, size_t size); +__attribute__((malloc, returns_nonnull)) +_deallocator_(free) void *xcalloc(size_t nmemb, size_t size); +__attribute__((malloc, returns_nonnull)) _deallocator_(free) void *xrealloc(void *ptr, size_t size); // clang-format off #define new(type, num) xcalloc(num, sizeof(type)) // clang-format on