Skip to content

Commit

Permalink
feat: add HEDLEY_ALLOC_SIZE macro
Browse files Browse the repository at this point in the history
  • Loading branch information
nemequ committed Sep 20, 2024
1 parent 7f6a9ca commit 25973b8
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 0 deletions.
1 change: 1 addition & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ Version 16
==========

* New macros:
* HEDLEY_ALLOC_SIZE
* HEDLEY_DIAGNOSTIC_DISABLE_EXTRA_SEMI
* HEDLEY_DIAGNOSTIC_DISABLE_UNSAFE_BUFFER_USAGE
* Assorted bug fixes and enhancements.
Expand Down
9 changes: 9 additions & 0 deletions hedley.h
Original file line number Diff line number Diff line change
Expand Up @@ -1326,6 +1326,15 @@ HEDLEY_DIAGNOSTIC_PUSH
#endif
HEDLEY_DIAGNOSTIC_POP

#if defined(HEDLEY_ALLOC_SIZE)
# undef HEDLEY_ALLOC_SIZE
#endif
#if HEDLEY_GCC_VERSION_CHECK(4,3,0) || HEDLEY_HAS_ATTRIBUTE(alloc_size)
# define HEDLEY_ALLOC_SIZE(...) __alloc_size__((__VA_ARGS__))
#else
# define HEDLEY_ALLOC_SIZE(...)
#endif

#if defined(HEDLEY_PRINTF_FORMAT)
# undef HEDLEY_PRINTF_FORMAT
#endif
Expand Down
1 change: 1 addition & 0 deletions test/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ CXX_OUTPUT_FILE ?= --output=
NULL =

TESTS = \
alloc-size \
array-param \
assume \
cast \
Expand Down
18 changes: 18 additions & 0 deletions test/alloc-size.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include "common.h"

#include <stdlib.h>

HEDLEY_ALLOC_SIZE(1)
static void*
myalloc (size_t buffer_size) {
return malloc(buffer_size);
}

int main(void) {
void* buf = myalloc(42);
if (buf != NULL) {
free(buf);
}

return 0;
}

0 comments on commit 25973b8

Please sign in to comment.