diff options
author | Alan Modra <amodra@gmail.com> | 2023-05-03 13:00:11 +0930 |
---|---|---|
committer | Alan Modra <amodra@gmail.com> | 2023-05-03 14:56:01 +0930 |
commit | a41bd1c837f9b71b44dbb71c2d47814326cfaa8d (patch) | |
tree | 004dda95dfc323a4cc65050f0652a8c0f6274b28 | |
parent | 37cfe371c4f2bb7aba2a684ed91749d1288ca187 (diff) | |
download | fsf-binutils-gdb-a41bd1c837f9b71b44dbb71c2d47814326cfaa8d.zip fsf-binutils-gdb-a41bd1c837f9b71b44dbb71c2d47814326cfaa8d.tar.gz fsf-binutils-gdb-a41bd1c837f9b71b44dbb71c2d47814326cfaa8d.tar.bz2 |
Move bfd_alloc, bfd_zalloc and bfd_release to libbfd.c
These functions don't belong in opncls.c.
* libbfd-in.h (bfd_release): Delete prototype.
* opncls.c (bfd_alloc, bfd_zalloc, bfd_release): Move to..
* libbfd.c: ..here. Include objalloc.c and provide bfd_release
with a FUNCTION comment.
* bfd-in2.h: Regenerate.
* libbfd.h: Regenerate.
-rw-r--r-- | bfd/bfd-in2.h | 10 | ||||
-rw-r--r-- | bfd/libbfd-in.h | 4 | ||||
-rw-r--r-- | bfd/libbfd.c | 82 | ||||
-rw-r--r-- | bfd/libbfd.h | 4 | ||||
-rw-r--r-- | bfd/opncls.c | 73 |
5 files changed, 88 insertions, 85 deletions
diff --git a/bfd/bfd-in2.h b/bfd/bfd-in2.h index 470a3cc..0c1844d 100644 --- a/bfd/bfd-in2.h +++ b/bfd/bfd-in2.h @@ -495,10 +495,6 @@ bool bfd_make_writable (bfd *abfd); bool bfd_make_readable (bfd *abfd); -void *bfd_alloc (bfd *abfd, bfd_size_type wanted); - -void *bfd_zalloc (bfd *abfd, bfd_size_type wanted); - uint32_t bfd_calc_gnu_debuglink_crc32 (uint32_t crc, const bfd_byte *buf, bfd_size_type len); @@ -523,6 +519,12 @@ char *bfd_follow_build_id_debuglink (bfd *abfd, const char *dir); const char *bfd_set_filename (bfd *abfd, const char *filename); /* Extracted from libbfd.c. */ +void *bfd_alloc (bfd *abfd, bfd_size_type wanted); + +void *bfd_zalloc (bfd *abfd, bfd_size_type wanted); + +void bfd_release (bfd *, void *); + /* Byte swapping macros for user section data. */ diff --git a/bfd/libbfd-in.h b/bfd/libbfd-in.h index 4305b84..ca15b8b 100644 --- a/bfd/libbfd-in.h +++ b/bfd/libbfd-in.h @@ -119,10 +119,6 @@ bfd_strdup (const char *str) memcpy (buf, str, len); return buf; } -/* These routines allocate and free things on the BFD's objalloc. */ - -extern void bfd_release - (bfd *, void *) ATTRIBUTE_HIDDEN; extern bfd * _bfd_create_empty_archive_element_shell (bfd *) ATTRIBUTE_HIDDEN; diff --git a/bfd/libbfd.c b/bfd/libbfd.c index e21cd98..66ef324 100644 --- a/bfd/libbfd.c +++ b/bfd/libbfd.c @@ -22,6 +22,7 @@ #include "sysdep.h" #include "bfd.h" #include "libbfd.h" +#include "objalloc.h" #ifndef HAVE_GETPAGESIZE #define getpagesize() 2048 @@ -417,6 +418,87 @@ bfd_zmalloc (bfd_size_type size) } /* +FUNCTION + bfd_alloc + +SYNOPSIS + void *bfd_alloc (bfd *abfd, bfd_size_type wanted); + +DESCRIPTION + Allocate a block of @var{wanted} bytes of memory attached to + <<abfd>> and return a pointer to it. +*/ + +void * +bfd_alloc (bfd *abfd, bfd_size_type size) +{ + void *ret; + unsigned long ul_size = (unsigned long) size; + + if (size != ul_size + /* Note - although objalloc_alloc takes an unsigned long as its + argument, internally the size is treated as a signed long. This can + lead to problems where, for example, a request to allocate -1 bytes + can result in just 1 byte being allocated, rather than + ((unsigned long) -1) bytes. Also memory checkers will often + complain about attempts to allocate a negative amount of memory. + So to stop these problems we fail if the size is negative. */ + || ((signed long) ul_size) < 0) + { + bfd_set_error (bfd_error_no_memory); + return NULL; + } + + ret = objalloc_alloc ((struct objalloc *) abfd->memory, ul_size); + if (ret == NULL) + bfd_set_error (bfd_error_no_memory); + else + abfd->alloc_size += size; + return ret; +} + +/* +FUNCTION + bfd_zalloc + +SYNOPSIS + void *bfd_zalloc (bfd *abfd, bfd_size_type wanted); + +DESCRIPTION + Allocate a block of @var{wanted} bytes of zeroed memory + attached to <<abfd>> and return a pointer to it. +*/ + +void * +bfd_zalloc (bfd *abfd, bfd_size_type size) +{ + void *res; + + res = bfd_alloc (abfd, size); + if (res) + memset (res, 0, (size_t) size); + return res; +} + +/* +FUNCTION + bfd_release + +SYNOPSIS + void bfd_release (bfd *, void *); + +DESCRIPTION + Free a block allocated for a BFD. + Note: Also frees all more recently allocated blocks! +*/ + +void +bfd_release (bfd *abfd, void *block) +{ + objalloc_free_block ((struct objalloc *) abfd->memory, block); +} + +/* INTERNAL_FUNCTION bfd_write_bigendian_4byte_int diff --git a/bfd/libbfd.h b/bfd/libbfd.h index aceec4a..3bd79d2 100644 --- a/bfd/libbfd.h +++ b/bfd/libbfd.h @@ -125,10 +125,6 @@ bfd_strdup (const char *str) memcpy (buf, str, len); return buf; } -/* These routines allocate and free things on the BFD's objalloc. */ - -extern void bfd_release - (bfd *, void *) ATTRIBUTE_HIDDEN; extern bfd * _bfd_create_empty_archive_element_shell (bfd *) ATTRIBUTE_HIDDEN; diff --git a/bfd/opncls.c b/bfd/opncls.c index 602dc80..b7b9d8f 100644 --- a/bfd/opncls.c +++ b/bfd/opncls.c @@ -1043,79 +1043,6 @@ bfd_make_readable (bfd *abfd) } /* -FUNCTION - bfd_alloc - -SYNOPSIS - void *bfd_alloc (bfd *abfd, bfd_size_type wanted); - -DESCRIPTION - Allocate a block of @var{wanted} bytes of memory attached to - <<abfd>> and return a pointer to it. -*/ - -void * -bfd_alloc (bfd *abfd, bfd_size_type size) -{ - void *ret; - unsigned long ul_size = (unsigned long) size; - - if (size != ul_size - /* Note - although objalloc_alloc takes an unsigned long as its - argument, internally the size is treated as a signed long. This can - lead to problems where, for example, a request to allocate -1 bytes - can result in just 1 byte being allocated, rather than - ((unsigned long) -1) bytes. Also memory checkers will often - complain about attempts to allocate a negative amount of memory. - So to stop these problems we fail if the size is negative. */ - || ((signed long) ul_size) < 0) - { - bfd_set_error (bfd_error_no_memory); - return NULL; - } - - ret = objalloc_alloc ((struct objalloc *) abfd->memory, ul_size); - if (ret == NULL) - bfd_set_error (bfd_error_no_memory); - else - abfd->alloc_size += size; - return ret; -} - -/* -FUNCTION - bfd_zalloc - -SYNOPSIS - void *bfd_zalloc (bfd *abfd, bfd_size_type wanted); - -DESCRIPTION - Allocate a block of @var{wanted} bytes of zeroed memory - attached to <<abfd>> and return a pointer to it. -*/ - -void * -bfd_zalloc (bfd *abfd, bfd_size_type size) -{ - void *res; - - res = bfd_alloc (abfd, size); - if (res) - memset (res, 0, (size_t) size); - return res; -} - -/* Free a block allocated for a BFD. - Note: Also frees all more recently allocated blocks! */ - -void -bfd_release (bfd *abfd, void *block) -{ - objalloc_free_block ((struct objalloc *) abfd->memory, block); -} - - -/* GNU Extension: separate debug-info files The idea here is that a special section called .gnu_debuglink might be |