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 a9229ef
Show file tree
Hide file tree
Showing 10 changed files with 89 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
10 changes: 10 additions & 0 deletions hedley.h
Original file line number Diff line number Diff line change
Expand Up @@ -1312,6 +1312,7 @@ HEDLEY_DIAGNOSTIC_PUSH
# pragma GCC diagnostic ignored "-Wvariadic-macros"
# endif
#endif

#if defined(HEDLEY_NON_NULL)
# undef HEDLEY_NON_NULL
#endif
Expand All @@ -1324,6 +1325,15 @@ HEDLEY_DIAGNOSTIC_PUSH
#else
# define HEDLEY_NON_NULL(...)
#endif

#if defined(HEDLEY_ALLOC_SIZE)
# undef HEDLEY_ALLOC_SIZE
#endif
#if HEDLEY_GCC_HAS_ATTRIBUTE(alloc_size,4,3,0) && !defined(HEDLEY_IBM_VERSION)
# define HEDLEY_ALLOC_SIZE(...) __attribute__((__alloc_size__(__VA_ARGS__)))
#else
# define HEDLEY_ALLOC_SIZE(...)
#endif
HEDLEY_DIAGNOSTIC_POP

#if defined(HEDLEY_PRINTF_FORMAT)
Expand Down
13 changes: 13 additions & 0 deletions test/.gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
# Generated by Makefile. Do not edit.
/alloc-size
/alloc-size.exe
/alloc-size.cpp
/alloc-size-cpp
/alloc-size-cpp.exe
/alloc-size-cpp.o
/alloc-size.obj
/alloc-size.lib
/alloc-size-cpp.obj
/alloc-size-cpp.lib
/alloc-size.o
/alloc-size.map
/alloc-size-cpp.map
/array-param
/array-param.exe
/array-param.cpp
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
9 changes: 9 additions & 0 deletions test/Makefile.dmc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ CFLAGS ?= -w -w17 -w18 -wx
CXXFLAGS ?= -wc $(CFLAGS)

TESTS = \
alloc-size.exe alloc-size-cpp.exe \
array-param.exe array-param-cpp.exe \
assume.exe assume-cpp.exe \
cast.exe cast-cpp.exe \
Expand Down Expand Up @@ -44,6 +45,7 @@ TESTS = \
$(NULL)

CLEANFILES = $(TESTS) \
alloc-size.cpp alloc-size.exe alloc-size-cpp.exe alloc-size.map alloc-size-cpp.map \
array-param.cpp array-param.exe array-param-cpp.exe array-param.map array-param-cpp.map \
assume.cpp assume.exe assume-cpp.exe assume.map assume-cpp.map \
cast.cpp cast.exe cast-cpp.exe cast.map cast-cpp.map \
Expand Down Expand Up @@ -86,6 +88,13 @@ all: $(TESTS)
clean:
del /Q $(CLEANFILES)

alloc-size.exe: alloc-size.c
$(CC) $(CFLAGS) -o $(@) $(?)
alloc-size.cpp: alloc-size.c
cp -f $(?) $(@)
alloc-size-cpp.exe: alloc-size.cpp
$(CXX) $(CXXFLAGS) -o $(@) $(?)

array-param.exe: array-param.c
$(CC) $(CFLAGS) -o $(@) $(?)
array-param.cpp: array-param.c
Expand Down
9 changes: 9 additions & 0 deletions test/Makefile.iar
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ CFLAGS = --silent --remarks --warnings_are_errors
CXXFLAGS = --c++ $(CFLAGS)

TESTS = \
alloc-size.o alloc-size-cpp.o \
array-param.o array-param-cpp.o \
assume.o assume-cpp.o \
cast.o cast-cpp.o \
Expand Down Expand Up @@ -44,6 +45,7 @@ TESTS = \
$(NULL)

CLEANFILES = $(TESTS) \
alloc-size.cpp alloc-size.o alloc-size-cpp.o \
array-param.cpp array-param.o array-param-cpp.o \
assume.cpp assume.o assume-cpp.o \
cast.cpp cast.o cast-cpp.o \
Expand Down Expand Up @@ -86,6 +88,13 @@ all: $(TESTS)
clean:
del /Q $(CLEANFILES)

alloc-size.o: alloc-size.c
$(CC) $(CFLAGS) --output $(@) $(?)
alloc-size.cpp: alloc-size.c
copy /Y $(?) $(@) >NUL
alloc-size-cpp.o: alloc-size.cpp
$(CXX) $(CXXFLAGS) --output $(@) $(?)

array-param.o: array-param.c
$(CC) $(CFLAGS) --output $(@) $(?)
array-param.cpp: array-param.c
Expand Down
10 changes: 10 additions & 0 deletions test/Makefile.icl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ CFLAGS = /nologo /Wall /wd4464 /wd4514
CXXFLAGS = $(CFLAGS)

TESTS = \
alloc-size.exe alloc-size-cpp.exe \
array-param.exe array-param-cpp.exe \
assume.exe assume-cpp.exe \
cast.exe cast-cpp.exe \
Expand Down Expand Up @@ -44,6 +45,7 @@ TESTS = \
$(NULL)

TESTS = \
alloc-size.exe alloc-size-cpp.exe \
array-param.exe array-param-cpp.exe \
assume.exe assume-cpp.exe \
cast.exe cast-cpp.exe \
Expand Down Expand Up @@ -82,6 +84,7 @@ TESTS = \
$(NULL)

CLEANFILES = $(TESTS) \
alloc-size.cpp alloc-size.obj alloc-size.lib alloc-size-cpp.exp alloc-size-cpp.lib \
array-param.cpp array-param.obj array-param.lib array-param-cpp.exp array-param-cpp.lib \
assume.cpp assume.obj assume.lib assume-cpp.exp assume-cpp.lib \
cast.cpp cast.obj cast.lib cast-cpp.exp cast-cpp.lib \
Expand Down Expand Up @@ -124,6 +127,13 @@ all: $(TESTS)
clean:
del /Q $(CLEANFILES)

alloc-size.exe: alloc-size.c
$(CC) $(CFLAGS) /Fe$(@) $(?)
alloc-size.cpp: alloc-size.c
copy /Y $(?) $(@) >NUL
alloc-size-cpp.exe: alloc-size.cpp
$(CXX) $(CXXFLAGS) /Fe$(@) $(?)

array-param.exe: array-param.c
$(CC) $(CFLAGS) /Fe$(@) $(?)
array-param.cpp: array-param.c
Expand Down
10 changes: 10 additions & 0 deletions test/Makefile.msvc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ CFLAGS = /nologo /Wall /wd4464 /wd4514
CXXFLAGS = $(CFLAGS)

TESTS = \
alloc-size.exe alloc-size-cpp.exe \
array-param.exe array-param-cpp.exe \
assume.exe assume-cpp.exe \
cast.exe cast-cpp.exe \
Expand Down Expand Up @@ -44,6 +45,7 @@ TESTS = \
$(NULL)

TESTS = \
alloc-size.exe alloc-size-cpp.exe \
array-param.exe array-param-cpp.exe \
assume.exe assume-cpp.exe \
cast.exe cast-cpp.exe \
Expand Down Expand Up @@ -82,6 +84,7 @@ TESTS = \
$(NULL)

CLEANFILES = $(TESTS) \
alloc-size.cpp alloc-size.obj alloc-size.lib alloc-size-cpp.exp alloc-size-cpp.lib \
array-param.cpp array-param.obj array-param.lib array-param-cpp.exp array-param-cpp.lib \
assume.cpp assume.obj assume.lib assume-cpp.exp assume-cpp.lib \
cast.cpp cast.obj cast.lib cast-cpp.exp cast-cpp.lib \
Expand Down Expand Up @@ -124,6 +127,13 @@ all: $(TESTS)
clean:
del /Q $(CLEANFILES)

alloc-size.exe: alloc-size.c
$(CC) $(CFLAGS) /Fe$(@) $(?)
alloc-size.cpp: alloc-size.c
copy /Y $(?) $(@) >NUL
alloc-size-cpp.exe: alloc-size.cpp
$(CXX) $(CXXFLAGS) /Fe$(@) $(?)

array-param.exe: array-param.c
$(CC) $(CFLAGS) /Fe$(@) $(?)
array-param.cpp: array-param.c
Expand Down
8 changes: 8 additions & 0 deletions test/Makefile.pelles
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ CFLAGS ?=
LDFLAGS ?=

TESTS = \
alloc-size.exe \
array-param.exe \
assume.exe \
cast.exe \
Expand Down Expand Up @@ -44,6 +45,7 @@ TESTS = \
$(NULL)

CLEANFILES = $(TESTS) \
alloc-size.obj alloc-size.exe \
array-param.obj array-param.exe \
assume.obj assume.exe \
cast.obj cast.exe \
Expand Down Expand Up @@ -86,6 +88,12 @@ all: $(TESTS)
clean:
del /Q $(CLEANFILES)

alloc-size.obj: alloc-size.c
$(CC) $(CFLAGS) alloc-size.c

alloc-size.exe: alloc-size.obj
$(LD) $(LDFLAGS) /OUT:alloc-size.exe alloc-size.obj

array-param.obj: array-param.c
$(CC) $(CFLAGS) array-param.c

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 a9229ef

Please sign in to comment.