]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
Define bfd_realloc(ptr,0) to return allocated memory, whilst bfd_realloc_or_free...
authorNick Clifton <nickc@redhat.com>
Tue, 4 May 2021 13:29:04 +0000 (14:29 +0100)
committerNick Clifton <nickc@redhat.com>
Tue, 4 May 2021 13:29:04 +0000 (14:29 +0100)
* libbfd.c (bfd_malloc): Provide some documenation.  Treat a size
of 0 as 1.
(bfd_realloc): Likewise.
(bfd_zmalloc): Likewise.
(bfd_realloc_or_free): Provide some documentation.  Treat a size
of 0 as a request to free.
* libbfd-in.h (bfd_realloc): Remove prototype.
(bfd_realloc_or_free): Remove prototype.
(bfd_zmalloc): Remove prototype.
* libbfd.h: Regenerate.

bfd/ChangeLog
bfd/libbfd-in.h
bfd/libbfd.c
bfd/libbfd.h

index 2659710787a9b97e8f475565cce8dfa3bdbaa7b5..0eb8618f6f99ac272b01acd8ad9037ef7045fcbc 100644 (file)
@@ -1,3 +1,16 @@
+2021-05-04  Nick Clifton  <nickc@redhat.com>
+
+       * libbfd.c (bfd_malloc): Provide some documenation.  Treat a size
+       of 0 as 1.
+       (bfd_realloc): Likewise.
+       (bfd_zmalloc): Likewise.
+       (bfd_realloc_or_free): Provide some documentation.  Treat a size
+       of 0 as a request to free.
+       * libbfd-in.h (bfd_realloc): Remove prototype.
+       (bfd_realloc_or_free): Remove prototype.
+       (bfd_zmalloc): Remove prototype.
+       * libbfd.h: Regenerate.
+
 2021-05-03  Jan Beulich  <jbeulich@suse.com>
 
        * elf32-i386.c (elf_howto_table): Switch all 32-bit relocs to
index 1ad1af8c002e4e239cb3008461c6eacf3073bbb8..d0abcc241f898786d15073fcc4ed136149a63549 100644 (file)
@@ -110,12 +110,6 @@ struct areltdata
 
 extern void *bfd_malloc
   (bfd_size_type) ATTRIBUTE_HIDDEN;
-extern void *bfd_realloc
-  (void *, bfd_size_type) ATTRIBUTE_HIDDEN;
-extern void *bfd_realloc_or_free
-  (void *, bfd_size_type) ATTRIBUTE_HIDDEN;
-extern void *bfd_zmalloc
-  (bfd_size_type) ATTRIBUTE_HIDDEN;
 
 static inline char *
 bfd_strdup (const char *str)
index 52c924560b2de80a8ccaa6fcf1874f57a090f05b..892f291dbd97d253f65ea3fc7414944accc94310 100644 (file)
@@ -258,6 +258,20 @@ _bfd_dummy_target (bfd *ignore_abfd ATTRIBUTE_UNUSED)
 #define SSIZE_MAX ((size_t) -1 >> 1)
 #endif
 
+/*
+INTERNAL_FUNCTION
+       bfd_malloc
+
+SYNOPSIS
+       extern void * bfd_malloc (bfd_size_type SIZE) ATTRIBUTE_HIDDEN;
+
+DESCRIPTION
+       Returns a pointer to an allocated block of memory that is at least
+       SIZE bytes long.  If SIZE is 0 then it will be treated as if it were
+       1.  If SIZE is too big then NULL will be returned.
+       
+       Returns NULL upon error and sets bfd_error.
+*/
 void *
 bfd_malloc (bfd_size_type size)
 {
@@ -272,13 +286,36 @@ bfd_malloc (bfd_size_type size)
       return NULL;
     }
 
-  ptr = malloc (sz);
-  if (ptr == NULL && sz != 0)
+  ptr = malloc (sz ? sz : 1);
+  if (ptr == NULL)
     bfd_set_error (bfd_error_no_memory);
 
   return ptr;
 }
 
+/*
+INTERNAL_FUNCTION
+       bfd_realloc
+
+SYNOPSIS
+       extern void * bfd_realloc (void * MEM, bfd_size_type SIZE) ATTRIBUTE_HIDDEN;
+
+DESCRIPTION
+       Returns a pointer to an allocated block of memory that is at least
+       SIZE bytes long.  If SIZE is 0 then it will be treated as if it were
+       1.  If SIZE is too big then NULL will be returned.
+       
+       If MEM is not NULL then it must point to an allocated block of memory.
+       If this block is large enough then MEM may be used as the return
+       value for this function, but this is not guaranteed.
+
+       If MEM is not returned then the first N bytes in the returned block
+       will be identical to the first N bytes in region pointed to by MEM,
+       where N is the lessor of SIZE and the length of the region of memory
+       currently addressed by MEM.
+
+       Returns NULL upon error and sets bfd_error.
+*/
 void *
 bfd_realloc (void *ptr, bfd_size_type size)
 {
@@ -296,37 +333,85 @@ bfd_realloc (void *ptr, bfd_size_type size)
       return NULL;
     }
 
-  ret = realloc (ptr, sz);
+  /* The behaviour of realloc(0) is implementation defined,
+     but for this function we always allocate memory.  */
+  ret = realloc (ptr, sz ? sz : 1);
 
-  if (ret == NULL && sz != 0)
+  if (ret == NULL)
     bfd_set_error (bfd_error_no_memory);
 
   return ret;
 }
 
-/* Reallocate memory using realloc.
-   If this fails the pointer is freed before returning.  */
+/*
+INTERNAL_FUNCTION
+       bfd_realloc_or_free
+
+SYNOPSIS
+       extern void * bfd_realloc_or_free (void * MEM, bfd_size_type SIZE) ATTRIBUTE_HIDDEN;
 
+DESCRIPTION
+       Returns a pointer to an allocated block of memory that is at least
+       SIZE bytes long.  If SIZE is 0 then no memory will be allocated,
+       MEM will be freed, and NULL will be returned.  This will not cause
+       bfd_error to be set.
+
+       If SIZE is too big then NULL will be returned and bfd_error will be
+       set. 
+       
+       If MEM is not NULL then it must point to an allocated block of memory.
+       If this block is large enough then MEM may be used as the return
+       value for this function, but this is not guaranteed.
+
+       If MEM is not returned then the first N bytes in the returned block
+       will be identical to the first N bytes in region pointed to by MEM,
+       where N is the lessor of SIZE and the length of the region of memory
+       currently addressed by MEM.
+*/
 void *
 bfd_realloc_or_free (void *ptr, bfd_size_type size)
 {
-  void *ret = bfd_realloc (ptr, size);
+  void *ret;
 
-  if (ret == NULL && size > 0)
+  /* The behaviour of realloc(0) is implementation defined, but
+     for this function we treat it is always freeing the memory.  */
+  if (size == 0)
+    {
+      free (ptr);
+      return NULL;
+    }
+      
+  ret = bfd_realloc (ptr, size);
+  if (ret == NULL)
     free (ptr);
 
   return ret;
 }
 
-/* Allocate memory using malloc and clear it.  */
+/*
+INTERNAL_FUNCTION
+       bfd_zmalloc
 
+SYNOPSIS
+       extern void * bfd_zmalloc (bfd_size_type SIZE) ATTRIBUTE_HIDDEN;
+
+DESCRIPTION
+       Returns a pointer to an allocated block of memory that is at least
+       SIZE bytes long.  If SIZE is 0 then it will be treated as if it were
+       1.  If SIZE is too big then NULL will be returned.
+       
+       Returns NULL upon error and sets bfd_error.
+
+       If NULL is not returned then the allocated block of memory will
+       have been cleared.
+*/
 void *
 bfd_zmalloc (bfd_size_type size)
 {
   void *ptr = bfd_malloc (size);
 
-  if (ptr != NULL && size > 0)
-    memset (ptr, 0, (size_t) size);
+  if (ptr != NULL)
+    memset (ptr, 0, size ? (size_t) size : 1);
 
   return ptr;
 }
index fba29998900214b9b787d9d67871d80c5f8ed3f7..f3a32fa428530d8e06abda06ec7f90bab27a80a7 100644 (file)
@@ -115,12 +115,6 @@ struct areltdata
 
 extern void *bfd_malloc
   (bfd_size_type) ATTRIBUTE_HIDDEN;
-extern void *bfd_realloc
-  (void *, bfd_size_type) ATTRIBUTE_HIDDEN;
-extern void *bfd_realloc_or_free
-  (void *, bfd_size_type) ATTRIBUTE_HIDDEN;
-extern void *bfd_zmalloc
-  (bfd_size_type) ATTRIBUTE_HIDDEN;
 
 static inline char *
 bfd_strdup (const char *str)
@@ -966,6 +960,14 @@ _bfd_malloc_and_read (bfd *abfd, bfd_size_type asize, bfd_size_type rsize)
   return NULL;
 }
 /* Extracted from libbfd.c.  */
+extern void * bfd_malloc (bfd_size_type SIZE) ATTRIBUTE_HIDDEN;
+
+extern void * bfd_realloc (void * MEM, bfd_size_type SIZE) ATTRIBUTE_HIDDEN;
+
+extern void * bfd_realloc_or_free (void * MEM, bfd_size_type SIZE) ATTRIBUTE_HIDDEN;
+
+extern void * bfd_zmalloc (bfd_size_type SIZE) ATTRIBUTE_HIDDEN;
+
 bool bfd_write_bigendian_4byte_int (bfd *, unsigned int);
 
 unsigned int bfd_log2 (bfd_vma x);