* configure.in (mips-big-* target): Same as Sony News.
[deliverable/binutils-gdb.git] / gdb / mmap-sbrk.c
1 /* GDB support for an sbrk-like function that uses mmap.
2 Copyright 1992 Free Software Foundation, Inc.
3 Contributed by Cygnus Support, using pieces from other GDB modules.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
20
21 #include <stdio.h>
22 #include "defs.h"
23
24 #ifdef HAVE_MMAP
25
26 #include <fcntl.h>
27 #include <sys/mman.h>
28
29 #ifdef i386
30 #define MMAP_BASE ((caddr_t) 0x81000000)
31 #define MMAP_PGSZ 0x00002000 /* Must be multiple of real page size */
32 #else
33 #define MMAP_BASE ((caddr_t) 0xC2000000)
34 #define MMAP_PGSZ 0x00002000 /* Must be multiple of real page size */
35 #endif
36
37 #define PAGE_ALIGN(addr) (caddr_t) (((long)(addr) + MMAP_PGSZ - 1) & \
38 ~(MMAP_PGSZ - 1))
39
40 static caddr_t mbase = MMAP_BASE; /* Current base of mmap'd region */
41 static caddr_t mbreak = MMAP_BASE; /* Current "break" address */
42 static caddr_t mtop = MMAP_BASE; /* Current top of mmap'd region */
43
44 static int fd = -1; /* Open fd for /dev/zero */
45
46
47 /* Provide a utility routine for other modules to obtain compatible
48 page alignment. */
49
50 PTR
51 mmap_page_align (addr)
52 PTR addr;
53 {
54 return (PAGE_ALIGN (addr));
55 }
56
57 /* Return the base address of the start of the mmap'd region. Note that
58 we can find the end of the region at anytime by calling mmap_sbrk(0) */
59
60 PTR
61 mmap_base ()
62 {
63 return (mbase);
64 }
65
66 /* Works like sbrk(), but uses mmap to add to or subtract from a
67 memory region. */
68
69 PTR
70 mmap_sbrk (size)
71 int size;
72 {
73 PTR result = NULL;
74 int minc;
75 caddr_t moveto;
76
77 if (size == 0)
78 {
79 /* Just return the current "break" value. */
80 result = mbreak;
81 }
82 else if (size < 0)
83 {
84 /* We are deallocating memory. If the amount requested would cause
85 us to try to deallocate back past the base of the mmap'd region
86 then do nothing, and return NULL. Otherwise, deallocate the
87 memory and return the old break value. */
88 if (mbreak + size >= mbase)
89 {
90 result = (PTR) mbreak;
91 mbreak += size;
92 moveto = PAGE_ALIGN (mbreak);
93 munmap (moveto, (size_t) (mtop - moveto));
94 mtop = moveto;
95 }
96 }
97 else
98 {
99 /* We are allocating memory. Make sure we have an open file
100 descriptor and then go on to get the memory. */
101 if ((fd == -1) && (fd = open ("/dev/zero", O_RDONLY)) < 0)
102 {
103 result = NULL;
104 }
105 else if (mbreak + size > mtop)
106 {
107 /* The request would move us past the end of the currently
108 mapped memory, so map in enough more memory to satisfy
109 the request. */
110 moveto = PAGE_ALIGN (mbreak + size);
111 if (mmap (mtop, moveto - mtop, PROT_READ | PROT_WRITE,
112 MAP_PRIVATE | MAP_FIXED, fd, 0) == mtop)
113 {
114 mtop = moveto;
115 result = (PTR) mbreak;
116 mbreak += size;
117 }
118 }
119 else
120 {
121 result = (PTR) mbreak;
122 mbreak += size;
123 }
124 }
125 return (result);
126 }
127
128 PTR
129 mmap_remap (base, mapsize, fd, foffset)
130 PTR base;
131 long mapsize;
132 int fd;
133 long foffset;
134 {
135 /* FIXME: Quick hack, needs error checking and other attention. */
136 munmap (mbase, (size_t) (mtop - mbase));
137 mbase = base;
138 mtop = mbase + mapsize;
139 base = mmap (base, mapsize, PROT_READ | PROT_WRITE,
140 MAP_PRIVATE | MAP_FIXED, dup (fd), foffset);
141 return (base);
142 }
143
144
145 #endif /* HAVE_MMAP */
This page took 0.032002 seconds and 4 git commands to generate.