+ if (mmap_next_fd >= 0)
+ {
+ /* Check that given file is big enough. */
+ int rc = fstat (mmap_next_fd, &s);
+
+ if (rc < 0)
+ sim_io_error (sd, "Error, unable to stat file: %s\n",
+ strerror (errno));
+
+ /* Autosize the mapping to the file length. */
+ if (nr_bytes == 0)
+ nr_bytes = s.st_size;
+ }
+#endif
+
+ bytes = (modulo == 0 ? nr_bytes : modulo) + padding;
+
+ free_buffer = NULL;
+ free_length = bytes;
+
+#ifdef HAVE_MMAP
+ /* Memory map or malloc(). */
+ if (mmap_next_fd >= 0)
+ {
+ /* Some kernels will SIGBUS the application if mmap'd file
+ is not large enough. */
+ if (s.st_size < bytes)
+ {
+ sim_io_error (sd,
+ "Error, cannot confirm that mmap file is large enough "
+ "(>= %ld bytes)\n", bytes);
+ }
+
+ free_buffer = mmap (0, bytes, PROT_READ|PROT_WRITE, MAP_SHARED, mmap_next_fd, 0);
+ if (free_buffer == 0 || free_buffer == (char*)-1) /* MAP_FAILED */
+ {
+ sim_io_error (sd, "Error, cannot mmap file (%s).\n",
+ strerror (errno));
+ }
+ }
+#endif
+
+ /* Need heap allocation? */
+ if (free_buffer == NULL)
+ {
+ /* If filling with non-zero value, do not use clearing allocator. */
+ if (fill_byte_flag && fill_byte_value != 0)
+ free_buffer = xmalloc (bytes); /* don't clear */
+ else
+ free_buffer = zalloc (bytes); /* clear */
+ }