]> git.ozlabs.org Git - ccan/commitdiff
tal, tal/str: allow annotation with returns_nonnull if they opt in.
authorRusty Russell <rusty@rustcorp.com.au>
Thu, 21 Mar 2024 23:18:10 +0000 (09:48 +1030)
committerRusty Russell <rusty@rustcorp.com.au>
Mon, 24 Jun 2024 02:46:26 +0000 (12:16 +0930)
This is true if you don't override the error callback, and helps with
optimization (and warnings!).

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
ccan/tal/str/str.h
ccan/tal/tal.h

index f939484400f57609302eade19c6d4d8599c9e649..a2424cbedb7e2b1dd70ad516e07335ee2e6be501 100644 (file)
@@ -17,7 +17,8 @@
  * The returned string will have tal_count() == strlen() + 1.
  */
 #define tal_strdup(ctx, p) tal_strdup_(ctx, p, TAL_LABEL(char, "[]"))
-char *tal_strdup_(const tal_t *ctx, const char *p TAKES, const char *label);
+char *tal_strdup_(const tal_t *ctx, const char *p TAKES, const char *label)
+       TAL_RETURN_PTR;
 
 /**
  * tal_strndup - duplicate a limited amount of a string.
@@ -30,7 +31,8 @@ char *tal_strdup_(const tal_t *ctx, const char *p TAKES, const char *label);
  */
 #define tal_strndup(ctx, p, n) tal_strndup_(ctx, p, n, TAL_LABEL(char, "[]"))
 char *tal_strndup_(const tal_t *ctx, const char *p TAKES, size_t n,
-                  const char *label);
+                  const char *label)
+       TAL_RETURN_PTR;
 
 /**
  * tal_fmt - allocate a formatted string
@@ -42,7 +44,7 @@ char *tal_strndup_(const tal_t *ctx, const char *p TAKES, size_t n,
 #define tal_fmt(ctx, ...)                               \
        tal_fmt_(ctx, TAL_LABEL(char, "[]"), __VA_ARGS__)
 char *tal_fmt_(const tal_t *ctx, const char *label, const char *fmt TAKES,
-               ...) PRINTF_FMT(3,4);
+               ...) PRINTF_FMT(3,4) TAL_RETURN_PTR;
 
 /**
  * tal_vfmt - allocate a formatted string (va_list version)
@@ -56,7 +58,7 @@ char *tal_fmt_(const tal_t *ctx, const char *label, const char *fmt TAKES,
        tal_vfmt_(ctx, fmt, va, TAL_LABEL(char, "[]"))
 char *tal_vfmt_(const tal_t *ctx, const char *fmt TAKES, va_list ap,
                const char *label)
-       PRINTF_FMT(2,0);
+       PRINTF_FMT(2,0) TAL_RETURN_PTR;
 
 /**
  * tal_append_fmt - append a formatted string to a talloc string.
@@ -89,7 +91,7 @@ bool tal_append_vfmt(char **baseptr, const char *fmt TAKES, va_list ap);
  */
 #define tal_strcat(ctx, s1, s2) tal_strcat_(ctx, s1, s2, TAL_LABEL(char, "[]"))
 char *tal_strcat_(const tal_t *ctx, const char *s1 TAKES, const char *s2 TAKES,
-                 const char *label);
+                 const char *label) TAL_RETURN_PTR;
 
 enum strsplit {
        STR_EMPTY_OK,
@@ -137,7 +139,8 @@ char **tal_strsplit_(const tal_t *ctx,
                     const char *string TAKES,
                     const char *delims TAKES,
                     enum strsplit flag,
-                    const char *label);
+                    const char *label)
+       TAL_RETURN_PTR;
 
 enum strjoin {
        STR_TRAIL,
@@ -175,7 +178,8 @@ char *tal_strjoin_(const void *ctx,
                   char *strings[] TAKES,
                   const char *delim TAKES,
                   enum strjoin flags,
-                  const char *label);
+                  const char *label)
+       TAL_RETURN_PTR;
 
 /**
  * tal_strreg - match/extract from a string via (extended) regular expressions.
index c486f9e8e194a99b51ffdb8117788bbae86a89ed..347a5e8c801aab5061b265b415e8b32b6156ad5e 100644 (file)
 #include <stdbool.h>
 #include <stdarg.h>
 
+/* Define this for better optimization if you never override errfn
+ * to something tat returns */
+#ifdef CCAN_TAL_NEVER_RETURN_NULL
+#define TAL_RETURN_PTR RETURNS_NONNULL
+#else
+#define TAL_RETURN_PTR
+#endif /* CCAN_TAL_NEVER_RETURN_NULL */
+
 /**
  * tal_t - convenient alias for void to mark tal pointers.
  *
@@ -417,7 +425,8 @@ tal_t *tal_parent(const tal_t *ctx);
  * @error_fn: called on errors or NULL (default is abort)
  *
  * The defaults are set up so tal functions never return NULL, but you
- * can override erorr_fn to change that.  error_fn can return, and is
+ * can override error_fn to change that.  error_fn can return (only if
+ * you haven't defined CCAN_TAL_NEVER_RETURN_NULL!), and is
  * called if alloc_fn or resize_fn fail.
  *
  * If any parameter is NULL, that function is unchanged.
@@ -521,9 +530,11 @@ bool tal_set_name_(tal_t *ctx, const char *name, bool literal);
 #define tal_typechk_(ptr, ptype) (ptr)
 #endif
 
-void *tal_alloc_(const tal_t *ctx, size_t bytes, bool clear, const char *label);
+void *tal_alloc_(const tal_t *ctx, size_t bytes, bool clear, const char *label)
+       TAL_RETURN_PTR;
 void *tal_alloc_arr_(const tal_t *ctx, size_t bytes, size_t count, bool clear,
-                    const char *label);
+                    const char *label)
+       TAL_RETURN_PTR;
 
 void *tal_dup_(const tal_t *ctx, const void *p TAKES, size_t size,
               size_t n, size_t extra, bool nullok, const char *label);