summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNicolas Trangez <ikke@nicolast.be>2022-10-30 22:56:24 +0100
committerMarge Bot <ben+marge-bot@smart-cactus.org>2022-11-02 12:06:48 -0400
commit99a1d896bca1c2e1068646eed50b02314673236c (patch)
tree27d6477b740341086cbcbae4b34df547fdd26e47
parent81c0c7c94a58d7ed5634abbb002ef88ef903e218 (diff)
downloadhaskell-99a1d896bca1c2e1068646eed50b02314673236c.tar.gz
rts: add and use `STG_RETURNS_NONNULL`
See: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-returns_005fnonnull-function-attribute See: https://gitlab.haskell.org/ghc/ghc/-/issues/22381
-rw-r--r--rts/RtsUtils.h12
-rw-r--r--rts/include/Stg.h9
2 files changed, 19 insertions, 2 deletions
diff --git a/rts/RtsUtils.h b/rts/RtsUtils.h
index f2b8bdd1d8..7f7e925cd4 100644
--- a/rts/RtsUtils.h
+++ b/rts/RtsUtils.h
@@ -22,10 +22,17 @@ void stgFree(void* p);
void *stgMallocBytes(size_t n, char *msg)
STG_MALLOC STG_MALLOC1(stgFree)
STG_ALLOC_SIZE1(1);
+/* Note: unlike `stgReallocBytes` and `stgCallocBytes`, `stgMallocBytes` is
+ * *not* `STG_RETURNS_NONNULL`, since it will return `NULL` when the requested
+ * allocation size is zero.
+ *
+ * See: https://gitlab.haskell.org/ghc/ghc/-/issues/22380
+ */
void *stgReallocBytes(void *p, size_t n, char *msg)
STG_MALLOC1(stgFree)
- STG_ALLOC_SIZE1(2);
+ STG_ALLOC_SIZE1(2)
+ STG_RETURNS_NONNULL;
/* Note: `stgRallocBytes` can *not* be tagged as `STG_MALLOC`
* since its return value *can* alias an existing pointer (i.e.,
* the given pointer `p`).
@@ -35,7 +42,8 @@ void *stgReallocBytes(void *p, size_t n, char *msg)
void *stgCallocBytes(size_t count, size_t size, char *msg)
STG_MALLOC STG_MALLOC1(stgFree)
- STG_ALLOC_SIZE2(1, 2);
+ STG_ALLOC_SIZE2(1, 2)
+ STG_RETURNS_NONNULL;
char *stgStrndup(const char *s, size_t n);
diff --git a/rts/include/Stg.h b/rts/include/Stg.h
index 5b5ba3ddb7..bd15e73cda 100644
--- a/rts/include/Stg.h
+++ b/rts/include/Stg.h
@@ -294,6 +294,15 @@
# define STG_ALLOC_SIZE2(position1, position2)
#endif
+/*
+ * https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-returns_005fnonnull-function-attribute
+ */
+#if stg__has_attribute(__returns_nonnull__)
+# define STG_RETURNS_NONNULL __attribute__((__returns_nonnull__))
+#else
+# define STG_RETURNS_NONNULL
+#endif
+
/* -----------------------------------------------------------------------------
Global type definitions
-------------------------------------------------------------------------- */