From a41bd1c837f9b71b44dbb71c2d47814326cfaa8d Mon Sep 17 00:00:00 2001 From: Alan Modra Date: Wed, 3 May 2023 13:00:11 +0930 Subject: 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. --- bfd/bfd-in2.h | 10 ++++--- bfd/libbfd-in.h | 4 --- bfd/libbfd.c | 82 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ bfd/libbfd.h | 4 --- bfd/opncls.c | 73 -------------------------------------------------- 5 files changed, 88 insertions(+), 85 deletions(-) (limited to 'bfd') diff --git a/bfd/bfd-in2.h b/bfd/bfd-in2.h index 470a3cc9d3b..0c1844d5ca4 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 4305b8416ea..ca15b8b0a23 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 e21cd988208..66ef32469fb 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 @@ -416,6 +417,87 @@ bfd_zmalloc (bfd_size_type size) return ptr; } +/* +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 + <> 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 <> 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 aceec4ab9c0..3bd79d2d3bd 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 602dc80a6c4..b7b9d8f2d0b 100644 --- a/bfd/opncls.c +++ b/bfd/opncls.c @@ -1042,79 +1042,6 @@ bfd_make_readable (bfd *abfd) return true; } -/* -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 - <> 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 <> 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 -- cgit v1.2.1