unofficial mirror of libc-alpha@sourceware.org
 help / color / mirror / code / Atom feed
* [PATCH] tests: use xmalloc to allocate implementation array
@ 2021-07-28  5:28 Siddhesh Poyarekar via Libc-alpha
  2021-07-28  7:25 ` Florian Weimer via Libc-alpha
  0 siblings, 1 reply; 3+ messages in thread
From: Siddhesh Poyarekar via Libc-alpha @ 2021-07-28  5:28 UTC (permalink / raw)
  To: libc-alpha

The benchmark and tests must fail in case of allocation failure in the
implementation array.  Also annotate the x* allocators in support.h so
that the compiler has more information about them.
---
 benchtests/bench-string.h |  5 +++--
 string/test-string.h      |  5 +++--
 support/support.h         | 25 ++++++++++++++++++-------
 3 files changed, 24 insertions(+), 11 deletions(-)

diff --git a/benchtests/bench-string.h b/benchtests/bench-string.h
index fd25264417..03de372cff 100644
--- a/benchtests/bench-string.h
+++ b/benchtests/bench-string.h
@@ -18,6 +18,7 @@
 
 #include <getopt.h>
 #include <sys/cdefs.h>
+#include <support/support.h>
 
 /* We are compiled under _ISOMAC, so libc-symbols.h does not do this
    for us.  */
@@ -200,8 +201,8 @@ static impl_t *impl_array;
 		 skip = impl;						      \
 	       else							      \
 		 impl_count++;						      \
-	     a = impl_array = malloc ((impl_count + func_count) *	      \
-				   sizeof (impl_t));			      \
+	     a = impl_array = xmalloc ((impl_count + func_count) *	      \
+				       sizeof (impl_t));		      \
 	     for (impl = __start_impls; impl < __stop_impls; ++impl)	      \
 	       if (impl != skip)					      \
 		 *a++ = *impl;						      \
diff --git a/string/test-string.h b/string/test-string.h
index febde61040..78b66efe36 100644
--- a/string/test-string.h
+++ b/string/test-string.h
@@ -18,6 +18,7 @@
    <https://www.gnu.org/licenses/>.  */
 
 #include <sys/cdefs.h>
+#include <support/support.h>
 
 typedef struct
 {
@@ -146,8 +147,8 @@ static impl_t *impl_array;
 	      skip = impl;						\
 	    else							\
 	      impl_count++;						\
-	  a = impl_array = malloc ((impl_count + func_count) *		\
-				   sizeof (impl_t));			\
+	  a = impl_array = xmalloc ((impl_count + func_count) *		\
+				    sizeof (impl_t));			\
 	  for (impl = __start_impls; impl < __stop_impls; ++impl)	\
 	    if (impl != skip)						\
 	      *a++ = *impl;						\
diff --git a/support/support.h b/support/support.h
index dbd270c78d..dce1ea3509 100644
--- a/support/support.h
+++ b/support/support.h
@@ -87,14 +87,25 @@ int support_descriptor_supports_holes (int fd);
 /* Error-checking wrapper functions which terminate the process on
    error.  */
 
-void *xmalloc (size_t) __attribute__ ((malloc));
-void *xcalloc (size_t n, size_t s) __attribute__ ((malloc));
-void *xrealloc (void *p, size_t n);
-void *xposix_memalign (size_t alignment, size_t n);
+extern void *xmalloc (size_t n)
+  __attribute_malloc__ __attribute_alloc_size__ ((1)) __attr_dealloc_free
+  __returns_nonnull;
+extern void *xcalloc (size_t n, size_t s)
+  __attribute_malloc__ __attribute_alloc_size__ ((1, 2)) __attr_dealloc_free
+  __returns_nonnull;
+extern void *xrealloc (void *o, size_t n)
+  __attribute_malloc__ __attribute_alloc_size__ ((2)) __attr_dealloc_free
+  __returns_nonnull;
+extern char *xstrdup (const char *) __attribute_malloc__ __attr_dealloc_free
+  __returns_nonnull;
+void *xposix_memalign (size_t alignment, size_t n)
+  __attribute_malloc__ __attribute_alloc_size__ ((2)) __attr_dealloc_free
+  __returns_nonnull;
 char *xasprintf (const char *format, ...)
-  __attribute__ ((format (printf, 1, 2), malloc));
-char *xstrdup (const char *);
-char *xstrndup (const char *, size_t);
+  __attribute__ ((format (printf, 1, 2), malloc)) __attr_dealloc_free
+  __returns_nonnull;
+char *xstrdup (const char *) __attr_dealloc_free __returns_nonnull;
+char *xstrndup (const char *, size_t) __attr_dealloc_free __returns_nonnull;
 char *xsetlocale (int category, const char *locale);
 locale_t xnewlocale (int category_mask, const char *locale, locale_t base);
 char *xuselocale (locale_t newloc);
-- 
2.31.1


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] tests: use xmalloc to allocate implementation array
  2021-07-28  5:28 [PATCH] tests: use xmalloc to allocate implementation array Siddhesh Poyarekar via Libc-alpha
@ 2021-07-28  7:25 ` Florian Weimer via Libc-alpha
  2021-07-28  7:31   ` Siddhesh Poyarekar via Libc-alpha
  0 siblings, 1 reply; 3+ messages in thread
From: Florian Weimer via Libc-alpha @ 2021-07-28  7:25 UTC (permalink / raw)
  To: Siddhesh Poyarekar via Libc-alpha; +Cc: Siddhesh Poyarekar

* Siddhesh Poyarekar via Libc-alpha:

> diff --git a/benchtests/bench-string.h b/benchtests/bench-string.h
> index fd25264417..03de372cff 100644
> --- a/benchtests/bench-string.h
> +++ b/benchtests/bench-string.h
> @@ -18,6 +18,7 @@
>  
>  #include <getopt.h>
>  #include <sys/cdefs.h>
> +#include <support/support.h>
>  
>  /* We are compiled under _ISOMAC, so libc-symbols.h does not do this
>     for us.  */
> @@ -200,8 +201,8 @@ static impl_t *impl_array;
>  		 skip = impl;						      \
>  	       else							      \
>  		 impl_count++;						      \
> -	     a = impl_array = malloc ((impl_count + func_count) *	      \
> -				   sizeof (impl_t));			      \
> +	     a = impl_array = xmalloc ((impl_count + func_count) *	      \
> +				       sizeof (impl_t));		      \
>  	     for (impl = __start_impls; impl < __stop_impls; ++impl)	      \
>  	       if (impl != skip)					      \
>  		 *a++ = *impl;						      \

I'm surprised the benchtests link against libsupport.  There's some
non-trivial setup code involved in libsupport for failure handling, and
that gets linked into xmalloc.  It might affect the benchmarks.

> diff --git a/string/test-string.h b/string/test-string.h
> index febde61040..78b66efe36 100644
> --- a/string/test-string.h
> +++ b/string/test-string.h
> @@ -18,6 +18,7 @@
>     <https://www.gnu.org/licenses/>.  */
>  
>  #include <sys/cdefs.h>
> +#include <support/support.h>
>  
>  typedef struct
>  {
> @@ -146,8 +147,8 @@ static impl_t *impl_array;
>  	      skip = impl;						\
>  	    else							\
>  	      impl_count++;						\
> -	  a = impl_array = malloc ((impl_count + func_count) *		\
> -				   sizeof (impl_t));			\
> +	  a = impl_array = xmalloc ((impl_count + func_count) *		\
> +				    sizeof (impl_t));			\
>  	  for (impl = __start_impls; impl < __stop_impls; ++impl)	\
>  	    if (impl != skip)						\
>  	      *a++ = *impl;						\
> diff --git a/support/support.h b/support/support.h
> index dbd270c78d..dce1ea3509 100644
> --- a/support/support.h
> +++ b/support/support.h
> @@ -87,14 +87,25 @@ int support_descriptor_supports_holes (int fd);
>  /* Error-checking wrapper functions which terminate the process on
>     error.  */
>  
> -void *xmalloc (size_t) __attribute__ ((malloc));
> -void *xcalloc (size_t n, size_t s) __attribute__ ((malloc));
> -void *xrealloc (void *p, size_t n);
> -void *xposix_memalign (size_t alignment, size_t n);
> +extern void *xmalloc (size_t n)
> +  __attribute_malloc__ __attribute_alloc_size__ ((1)) __attr_dealloc_free
> +  __returns_nonnull;
> +extern void *xcalloc (size_t n, size_t s)
> +  __attribute_malloc__ __attribute_alloc_size__ ((1, 2)) __attr_dealloc_free
> +  __returns_nonnull;
> +extern void *xrealloc (void *o, size_t n)
> +  __attribute_malloc__ __attribute_alloc_size__ ((2)) __attr_dealloc_free
> +  __returns_nonnull;

realloc will return NULL if the size is zero and the original pointer is
not.  See its implementation in support/.

Rest looks okay.

Thanks,
Florian


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] tests: use xmalloc to allocate implementation array
  2021-07-28  7:25 ` Florian Weimer via Libc-alpha
@ 2021-07-28  7:31   ` Siddhesh Poyarekar via Libc-alpha
  0 siblings, 0 replies; 3+ messages in thread
From: Siddhesh Poyarekar via Libc-alpha @ 2021-07-28  7:31 UTC (permalink / raw)
  To: Florian Weimer, Siddhesh Poyarekar via Libc-alpha

On 7/28/21 12:55 PM, Florian Weimer wrote:
> * Siddhesh Poyarekar via Libc-alpha:
> 
>> diff --git a/benchtests/bench-string.h b/benchtests/bench-string.h
>> index fd25264417..03de372cff 100644
>> --- a/benchtests/bench-string.h
>> +++ b/benchtests/bench-string.h
>> @@ -18,6 +18,7 @@
>>   
>>   #include <getopt.h>
>>   #include <sys/cdefs.h>
>> +#include <support/support.h>
>>   
>>   /* We are compiled under _ISOMAC, so libc-symbols.h does not do this
>>      for us.  */
>> @@ -200,8 +201,8 @@ static impl_t *impl_array;
>>   		 skip = impl;						      \
>>   	       else							      \
>>   		 impl_count++;						      \
>> -	     a = impl_array = malloc ((impl_count + func_count) *	      \
>> -				   sizeof (impl_t));			      \
>> +	     a = impl_array = xmalloc ((impl_count + func_count) *	      \
>> +				       sizeof (impl_t));		      \
>>   	     for (impl = __start_impls; impl < __stop_impls; ++impl)	      \
>>   	       if (impl != skip)					      \
>>   		 *a++ = *impl;						      \
> 
> I'm surprised the benchtests link against libsupport.  There's some
> non-trivial setup code involved in libsupport for failure handling, and
> that gets linked into xmalloc.  It might affect the benchmarks.

Yeah, it may not hurt to wean them away; it's really just the string 
benchmarks that were copied over from tests and adapted.  I'll use 
programs/xmalloc.h here then.

Siddhesh

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2021-07-28  8:18 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-07-28  5:28 [PATCH] tests: use xmalloc to allocate implementation array Siddhesh Poyarekar via Libc-alpha
2021-07-28  7:25 ` Florian Weimer via Libc-alpha
2021-07-28  7:31   ` Siddhesh Poyarekar via Libc-alpha

Code repositories for project(s) associated with this inbox:

	https://80x24.org/mirrors/glibc.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).