return 0;
}
\f
+/* Allocate memory using malloc. */
-#ifndef bfd_zmalloc
-/* allocate and clear storage */
+PTR
+bfd_malloc (size)
+ size_t size;
+{
+ PTR ptr;
-char *
+ ptr = (PTR) malloc (size);
+ if (ptr == NULL && size != 0)
+ bfd_set_error (bfd_error_no_memory);
+ return ptr;
+}
+
+/* Reallocate memory using realloc. */
+
+PTR
+bfd_realloc (ptr, size)
+ PTR ptr;
+ size_t size;
+{
+ PTR ret;
+
+ if (ptr == NULL)
+ ret = malloc (size);
+ else
+ ret = realloc (ptr, size);
+
+ if (ret == NULL)
+ bfd_set_error (bfd_error_no_memory);
+
+ return ret;
+}
+
+/* Allocate memory using malloc and clear it. */
+
+PTR
bfd_zmalloc (size)
- bfd_size_type size;
+ size_t size;
{
- char *ptr = (char *) malloc ((size_t) size);
+ PTR ptr;
+
+ ptr = (PTR) malloc (size);
if (size != 0)
{
if (ptr == NULL)
bfd_set_error (bfd_error_no_memory);
else
- memset (ptr, 0, (size_t) size);
+ memset (ptr, 0, size);
}
return ptr;
}
-#endif /* bfd_zmalloc */
\f
/* Some IO code */
bfd *abfd;
{
int nread;
+
+ if ((abfd->flags & BFD_IN_MEMORY) != 0)
+ {
+ struct bfd_in_memory *bim;
+ bfd_size_type get;
+
+ bim = (struct bfd_in_memory *) abfd->iostream;
+ get = size * nitems;
+ if (abfd->where + get > bim->size)
+ {
+ get = bim->size - abfd->where;
+ bfd_set_error (bfd_error_file_truncated);
+ }
+ memcpy (ptr, bim->buffer + abfd->where, get);
+ abfd->where += get;
+ return get;
+ }
+
nread = real_read (ptr, 1, (size_t)(size*nitems), bfd_cache_lookup(abfd));
#ifdef FILE_OFFSET_IS_CHAR_INDEX
if (nread > 0)
i->data = 0;
}
#ifdef HAVE_MMAP
- if (ok_to_map && (i->data == 0 || i->mapped == 1))
+ if (ok_to_map
+ && (i->data == 0 || i->mapped == 1)
+ && (abfd->flags & BFD_IN_MEMORY) == 0)
{
file_ptr file_offset, offset2;
size_t real_size;
if (debug_windows)
fprintf (stderr, "\n\t%s(%6ld)",
i->data ? "realloc" : " malloc", (long) size_to_alloc);
- if (i->data)
- i->data = (PTR) realloc (i->data, size_to_alloc);
- else
- i->data = (PTR) malloc (size_to_alloc);
+ i->data = (PTR) bfd_realloc (i->data, size_to_alloc);
if (debug_windows)
fprintf (stderr, "\t-> %p\n", i->data);
i->refcount = 1;
bfd_size_type nitems;
bfd *abfd;
{
- long nwrote = fwrite (ptr, 1, (size_t) (size * nitems),
- bfd_cache_lookup (abfd));
+ long nwrote;
+
+ if ((abfd->flags & BFD_IN_MEMORY) != 0)
+ abort ();
+
+ nwrote = fwrite (ptr, 1, (size_t) (size * nitems),
+ bfd_cache_lookup (abfd));
#ifdef FILE_OFFSET_IS_CHAR_INDEX
if (nwrote > 0)
abfd->where += nwrote;
{
file_ptr ptr;
+ if ((abfd->flags & BFD_IN_MEMORY) != 0)
+ return abfd->where;
+
ptr = ftell (bfd_cache_lookup(abfd));
if (abfd->my_archive)
bfd_flush (abfd)
bfd *abfd;
{
+ if ((abfd->flags & BFD_IN_MEMORY) != 0)
+ return 0;
return fflush (bfd_cache_lookup(abfd));
}
{
FILE *f;
int result;
+
+ if ((abfd->flags & BFD_IN_MEMORY) != 0)
+ abort ();
+
f = bfd_cache_lookup (abfd);
if (f == NULL)
{
if (direction == SEEK_CUR && position == 0)
return 0;
+
+ if ((abfd->flags & BFD_IN_MEMORY) != 0)
+ {
+ if (direction == SEEK_SET)
+ abfd->where = position;
+ else
+ abfd->where += position;
+ return 0;
+ }
+
#ifdef FILE_OFFSET_IS_CHAR_INDEX
if (abfd->format != bfd_archive && abfd->my_archive == 0)
{
w->i = (bfd_window_internal *) bfd_zmalloc (sizeof (bfd_window_internal));
if (w->i == NULL)
return false;
- w->i->data = (PTR) malloc ((size_t) count);
+ w->i->data = (PTR) bfd_malloc ((size_t) count);
if (w->i->data == NULL)
{
free (w->i);