| 1 | /* GDB support for special malloc using 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 | #if defined (HAVE_MMAP) |
| 25 | |
| 26 | /* Redefine the external visible symbols in gmalloc.c to be mmap versions */ |
| 27 | |
| 28 | #define free _mmap_free |
| 29 | #define malloc _mmap_malloc |
| 30 | #define realloc _mmap_realloc |
| 31 | #define valloc _mmap_valloc |
| 32 | |
| 33 | #define _bytes_free _mmap__bytes_free |
| 34 | #define _bytes_used _mmap__bytes_used |
| 35 | #define _chunks_free _mmap__chunks_free |
| 36 | #define _chunks_used _mmap__chunks_used |
| 37 | #define _fraghead _mmap__fraghead |
| 38 | #define _heapbase _mmap__heapbase |
| 39 | #define _heapindex _mmap__heapindex |
| 40 | #define _heapinfo _mmap__heapinfo |
| 41 | #define _heaplimit _mmap__heaplimit |
| 42 | |
| 43 | #define __default_morecore _mmap___default_morecore |
| 44 | #define __free _mmap___free |
| 45 | #define __free_hook _mmap___free_hook |
| 46 | #define __malloc_hook _mmap___malloc_hook |
| 47 | #define __malloc_initialized _mmap___malloc_initialized |
| 48 | #define __morecore _mmap___morecore |
| 49 | #define __realloc_hook _mmap___realloc_hook |
| 50 | |
| 51 | /* Arrange that instead of calling sbrk() we call mmap_sbrk() */ |
| 52 | |
| 53 | #define sbrk mmap_sbrk |
| 54 | |
| 55 | /* Now simply include the standard GNU malloc source, and all the |
| 56 | externally visible symbols will become _mmap_* versions, and |
| 57 | _mmap_sbrk will be called to get more core instead of sbrk. */ |
| 58 | |
| 59 | #include "gmalloc.c" |
| 60 | |
| 61 | /* Like mmap_malloc but get error if no storage available. */ |
| 62 | |
| 63 | PTR |
| 64 | mmap_xmalloc (size) |
| 65 | long size; |
| 66 | { |
| 67 | register char *val = NULL; |
| 68 | |
| 69 | /* Protect against gdb wanting to allocate zero bytes. */ |
| 70 | |
| 71 | if (size > 0) |
| 72 | { |
| 73 | if ((val = (char *) _mmap_malloc (size)) == NULL) |
| 74 | { |
| 75 | fatal ("virtual memory exhausted.", 0); |
| 76 | } |
| 77 | } |
| 78 | return (val); |
| 79 | } |
| 80 | |
| 81 | /* Like mmap_realloc but get error if no storage available. */ |
| 82 | |
| 83 | PTR |
| 84 | mmap_xrealloc (ptr, size) |
| 85 | PTR ptr; |
| 86 | long size; |
| 87 | { |
| 88 | register char *val; |
| 89 | |
| 90 | if ((val = (char *) _mmap_realloc (ptr, size)) == NULL) |
| 91 | { |
| 92 | fatal ("virtual memory exhausted.", 0); |
| 93 | } |
| 94 | return (val); |
| 95 | } |
| 96 | |
| 97 | PTR |
| 98 | mmap_malloc (size) |
| 99 | long size; |
| 100 | { |
| 101 | return (_mmap_malloc (size)); |
| 102 | } |
| 103 | |
| 104 | PTR |
| 105 | mmap_realloc (ptr, size) |
| 106 | PTR ptr; |
| 107 | long size; |
| 108 | { |
| 109 | return (_mmap_realloc (ptr, size)); |
| 110 | } |
| 111 | |
| 112 | void |
| 113 | mmap_free (ptr) |
| 114 | PTR ptr; |
| 115 | { |
| 116 | _mmap_free (ptr); |
| 117 | } |
| 118 | |
| 119 | #else /* !defined (HAVE_MMAP) */ |
| 120 | |
| 121 | static char *errmsg = "This version of gdb does not support dumpable state."; |
| 122 | |
| 123 | PTR |
| 124 | mmap_malloc (size) |
| 125 | long size; |
| 126 | { |
| 127 | error (errmsg); |
| 128 | } |
| 129 | |
| 130 | PTR |
| 131 | mmap_xmalloc (size) |
| 132 | long size; |
| 133 | { |
| 134 | error (errmsg); |
| 135 | } |
| 136 | |
| 137 | PTR |
| 138 | mmap_realloc (ptr, size) |
| 139 | PTR ptr; |
| 140 | long size; |
| 141 | { |
| 142 | error (errmsg); |
| 143 | } |
| 144 | |
| 145 | PTR |
| 146 | mmap_xrealloc (ptr, size) |
| 147 | PTR ptr; |
| 148 | long size; |
| 149 | { |
| 150 | error (errmsg); |
| 151 | } |
| 152 | |
| 153 | void |
| 154 | mmap_free (ptr) |
| 155 | PTR ptr; |
| 156 | { |
| 157 | error (errmsg); |
| 158 | } |
| 159 | |
| 160 | #endif /* HAVE_MMAP */ |