/* Support for an sbrk-like function that uses mmap.
- Copyright 1992 Free Software Foundation, Inc.
+ Copyright 1992, 2000 Free Software Foundation, Inc.
Contributed by Fred Fish at Cygnus Support. fnf@cygnus.com
#if defined(HAVE_MMAP)
+#ifdef HAVE_UNISTD_H
+#include <unistd.h> /* Prototypes for lseek */
+#endif
#include <stdio.h>
#include <fcntl.h>
#include <sys/mman.h>
it out. */
static size_t pagesize;
+#if NEED_DECLARATION_GETPAGESIZE
extern int getpagesize PARAMS ((void));
+#endif
#define PAGE_ALIGN(addr) (caddr_t) (((long)(addr) + pagesize - 1) & \
~(pagesize - 1))
int size;
{
int fd;
+ int flags;
caddr_t base = NULL;
+#ifdef MAP_ANONYMOUS
+ flags = MAP_SHARED | MAP_ANONYMOUS;
+ fd = -1;
+#else
+#ifdef MAP_FILE
+ flags = MAP_SHARED | MAP_FILE;
+#else
+ flags = MAP_SHARED;
+#endif
fd = open ("/dev/zero", O_RDWR);
if (fd != -1)
{
- base = mmap (0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
- if (base != (caddr_t) -1)
- {
- munmap (base, (size_t) size);
- }
+ return ((PTR) NULL);
+ }
+#endif
+ base = mmap (0, size, PROT_READ | PROT_WRITE, flags, fd, 0);
+ if (base != (caddr_t) -1)
+ {
+ munmap (base, (size_t) size);
+ }
+ if (fd != -1)
+ {
close (fd);
- if (base == 0)
- {
- /* Don't allow mapping at address zero. We use that value
- to signal an error return, and besides, it is useful to
- catch NULL pointers if it is unmapped. Instead start
- at the next page boundary. */
- base = (caddr_t) getpagesize ();
- }
- else if (base == (caddr_t) -1)
- {
- base = NULL;
- }
+ }
+ if (base == 0)
+ {
+ /* Don't allow mapping at address zero. We use that value
+ to signal an error return, and besides, it is useful to
+ catch NULL pointers if it is unmapped. Instead start
+ at the next page boundary. */
+ base = (caddr_t) getpagesize ();
+ }
+ else if (base == (caddr_t) -1)
+ {
+ base = NULL;
}
return ((PTR) base);
}