aboutsummaryrefslogtreecommitdiff
path: root/bfd/libbfd.c
diff options
context:
space:
mode:
authorAlan Modra <amodra@gmail.com>2023-05-03 13:00:11 +0930
committerAlan Modra <amodra@gmail.com>2023-05-03 14:56:01 +0930
commita41bd1c837f9b71b44dbb71c2d47814326cfaa8d (patch)
tree004dda95dfc323a4cc65050f0652a8c0f6274b28 /bfd/libbfd.c
parent37cfe371c4f2bb7aba2a684ed91749d1288ca187 (diff)
downloadfsf-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.
Diffstat (limited to 'bfd/libbfd.c')
-rw-r--r--bfd/libbfd.c82
1 files changed, 82 insertions, 0 deletions
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