* libbfd-in.h (bfd_malloc2, bfd_realloc2, bfd_zmalloc2, bfd_alloc2,
[deliverable/binutils-gdb.git] / bfd / opncls.c
index 74bee5777aea9eea4c5206973a233c1a169bfacd..c5cc252c66bdc4dc2cf8f9f44934374b5500f1b1 100644 (file)
@@ -144,6 +144,8 @@ DESCRIPTION
        Calls <<bfd_find_target>>, so @var{target} is interpreted as by
        that function.
 
+       The new BFD is marked as cacheable iff @var{fd} is -1.
+
        If <<NULL>> is returned then an error has occured.   Possible errors
        are <<bfd_error_no_memory>>, <<bfd_error_invalid_target>> or
        <<system_call>> error.
@@ -155,8 +157,6 @@ bfd_fopen (const char *filename, const char *target, const char *mode, int fd)
   bfd *nbfd;
   const bfd_target *target_vec;
 
-  bfd_set_error (bfd_error_system_call);
-
   nbfd = _bfd_new_bfd ();
   if (nbfd == NULL)
     return NULL;
@@ -176,6 +176,7 @@ bfd_fopen (const char *filename, const char *target, const char *mode, int fd)
     nbfd->iostream = fopen (filename, mode);
   if (nbfd->iostream == NULL)
     {
+      bfd_set_error (bfd_error_system_call);
       _bfd_delete_bfd (nbfd);
       return NULL;
     }
@@ -199,6 +200,12 @@ bfd_fopen (const char *filename, const char *target, const char *mode, int fd)
       return NULL;
     }
   nbfd->opened_once = TRUE;
+  /* If we opened the file by name, mark it cacheable; we can close it
+     and reopen it later.  However, if a file descriptor was provided,
+     then it may have been opened with special flags that make it
+     unsafe to close and reopen the file.  */
+  if (fd == -1)
+    bfd_set_cacheable (nbfd, TRUE);
 
   return nbfd;
 }
@@ -269,13 +276,15 @@ bfd_fdopenr (const char *filename, const char *target, int fd)
   int fdflags;
 #endif
 
-  bfd_set_error (bfd_error_system_call);
 #if ! defined(HAVE_FCNTL) || ! defined(F_GETFL)
   mode = FOPEN_RUB; /* Assume full access.  */
 #else
   fdflags = fcntl (fd, F_GETFL, NULL);
   if (fdflags == -1)
-    return NULL;
+    {
+      bfd_set_error (bfd_error_system_call);
+      return NULL;
+    }
 
   /* (O_ACCMODE) parens are to avoid Ultrix header file bug.  */
   switch (fdflags & (O_ACCMODE))
@@ -847,6 +856,45 @@ bfd_alloc (bfd *abfd, bfd_size_type size)
   return ret;
 }
 
+/*
+INTERNAL_FUNCTION
+       bfd_alloc2
+
+SYNOPSIS
+       void *bfd_alloc2 (bfd *abfd, bfd_size_type nmemb, bfd_size_type size);
+
+DESCRIPTION
+       Allocate a block of @var{nmemb} elements of @var{size} bytes each
+       of memory attached to <<abfd>> and return a pointer to it.
+*/
+
+void *
+bfd_alloc2 (bfd *abfd, bfd_size_type nmemb, bfd_size_type size)
+{
+  void *ret;
+
+  if ((nmemb | size) >= HALF_BFD_SIZE_TYPE
+      && size != 0
+      && nmemb > ~(bfd_size_type) 0 / size)
+    {
+      bfd_set_error (bfd_error_no_memory);
+      return NULL;
+    }
+
+  size *= nmemb;
+
+  if (size != (unsigned long) size)
+    {
+      bfd_set_error (bfd_error_no_memory);
+      return NULL;
+    }
+
+  ret = objalloc_alloc (abfd->memory, (unsigned long) size);
+  if (ret == NULL)
+    bfd_set_error (bfd_error_no_memory);
+  return ret;
+}
+
 /*
 INTERNAL_FUNCTION
        bfd_zalloc
@@ -870,6 +918,39 @@ bfd_zalloc (bfd *abfd, bfd_size_type size)
   return res;
 }
 
+/*
+INTERNAL_FUNCTION
+       bfd_zalloc2
+
+SYNOPSIS
+       void *bfd_zalloc2 (bfd *abfd, bfd_size_type nmemb, bfd_size_type size);
+
+DESCRIPTION
+       Allocate a block of @var{nmemb} elements of @var{size} bytes each
+       of zeroed memory attached to <<abfd>> and return a pointer to it.
+*/
+
+void *
+bfd_zalloc2 (bfd *abfd, bfd_size_type nmemb, bfd_size_type size)
+{
+  void *res;
+
+  if ((nmemb | size) >= HALF_BFD_SIZE_TYPE
+      && size != 0
+      && nmemb > ~(bfd_size_type) 0 / size)
+    {
+      bfd_set_error (bfd_error_no_memory);
+      return NULL;
+    }
+
+  size *= nmemb;
+
+  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!  */
 
This page took 0.032152 seconds and 4 git commands to generate.