annotate src/ralloc.c @ 5090:0ca81354c4c7

Further frame-geometry cleanups -------------------- ChangeLog entries follow: -------------------- man/ChangeLog addition: 2010-03-03 Ben Wing <ben@xemacs.org> * internals/internals.texi (Intro to Window and Frame Geometry): * internals/internals.texi (The Paned Area): * internals/internals.texi (The Displayable Area): Update to make note of e.g. the fact that the bottom gutter is actually above the minibuffer. src/ChangeLog addition: 2010-03-03 Ben Wing <ben@xemacs.org> * emacs.c: * emacs.c (assert_equal_failed): * lisp.h: * lisp.h (assert_equal): New fun assert_equal, asserting that two values == each other, and printing out both values upon failure. * frame-gtk.c (gtk_initialize_frame_size): * frame-impl.h: * frame-impl.h (FRAME_TOP_INTERNAL_BORDER_START): * frame-impl.h (FRAME_BOTTOM_INTERNAL_BORDER_START): * frame-impl.h (FRAME_LEFT_INTERNAL_BORDER_START): * frame-impl.h (FRAME_PANED_TOP_EDGE): * frame-impl.h (FRAME_NONPANED_SIZE): * frame-x.c (x_initialize_frame_size): * frame.c: * gutter.c (get_gutter_coords): * gutter.c (calculate_gutter_size): * gutter.h: * gutter.h (WINDOW_REAL_TOP_GUTTER_BOUNDS): * gutter.h (FRAME_TOP_GUTTER_BOUNDS): * input-method-xlib.c: * input-method-xlib.c (XIM_SetGeometry): * redisplay-output.c (clear_left_border): * redisplay-output.c (clear_right_border): * redisplay-output.c (redisplay_output_pixmap): * redisplay-output.c (redisplay_clear_region): * redisplay-output.c (redisplay_clear_top_of_window): * redisplay-output.c (redisplay_clear_to_window_end): * redisplay-xlike-inc.c (XLIKE_clear_frame): * redisplay.c: * redisplay.c (UPDATE_CACHE_RETURN): * redisplay.c (pixel_to_glyph_translation): * toolbar.c (update_frame_toolbars_geometry): * window.c (Fwindow_pixel_edges): Get rid of some redundant macros. Consistently use the FRAME_TOP_*_START, FRAME_RIGHT_*_END, etc. format. Rename FRAME_*_BORDER_* to FRAME_*_INTERNAL_BORDER_*. Comment out FRAME_BOTTOM_* for gutters and the paned area due to the uncertainty over where the paned area actually begins. (Eventually we should probably move the gutters outside the minibuffer so that the paned area is contiguous.) Use FRAME_PANED_* more often in the code to make things clearer. Update the diagram to show that the bottom gutter is inside the minibuffer (!) and that there are "junk boxes" when you have left and/or right gutters (dead boxes that are mistakenly left uncleared, unlike the corresponding scrollbar dead boxes). Update the text appropriately to cover the bottom gutter position, etc. Rewrite gutter-geometry code to use the FRAME_*_GUTTER_* in place of equivalent expressions referencing other frame elements, to make the code more portable in case we move around the gutter location. Cleanup FRAME_*_GUTTER_BOUNDS() in gutter.h. Add some #### GEOM! comments where I think code is incorrect -- typically, it wasn't fixed up properly when the gutter was added. Some cosmetic changes.
author Ben Wing <ben@xemacs.org>
date Wed, 03 Mar 2010 05:07:47 -0600
parents 6f2158fa75ed
children 308d34e9f07d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1 /* Block-relocating memory allocator.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
2 Copyright (C) 1992, 1993, 1994, 1995 Free Software Foundation, Inc.
5050
6f2158fa75ed Fix quick-build, use asserts() in place of ABORT()
Ben Wing <ben@xemacs.org>
parents: 3263
diff changeset
3 Copyright (C) 2010 Ben Wing.
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
4
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
5 This file is part of XEmacs.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
6
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
7 XEmacs is free software; you can redistribute it and/or modify it
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
8 under the terms of the GNU General Public License as published by the
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
9 Free Software Foundation; either version 2, or (at your option) any
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
10 later version.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
11
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
12 XEmacs is distributed in the hope that it will be useful, but WITHOUT
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
15 for more details.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
16
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
17 You should have received a copy of the GNU General Public License
613
023b83f4e54b [xemacs-hg @ 2001-06-10 10:42:16 by ben]
ben
parents: 563
diff changeset
18 along with XEmacs; see the file COPYING. If not, write to
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
20 Boston, MA 02111-1307, USA.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
21
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
22 Synched Up with: FSF 20.2 (non-mmap portion only)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
23 */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
24
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
25 /* NOTES:
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
26
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
27 Only relocate the blocs necessary for SIZE in r_alloc_sbrk,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
28 rather than all of them. This means allowing for a possible
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
29 hole between the first bloc and the end of malloc storage. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
30
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
31 #ifdef HAVE_CONFIG_H
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
32 #include <config.h>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
33 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
34
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
35 #ifdef HAVE_UNISTD_H
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
36 #include <unistd.h> /* for getpagesize() */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
37 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
38
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
39 #ifdef emacs
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
40
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
41 #include "lisp.h"
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
42
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
43 /* The important properties of this type are that 1) it's a pointer, and
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
44 2) arithmetic on it should work as if the size of the object pointed
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
45 to has a size of 1. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
46 #if 0 /* Arithmetic on void* is a GCC extension. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
47 #ifdef __STDC__
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
48 typedef void *POINTER;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
49 #else
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
50 typedef unsigned char *POINTER;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
51 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
52 #endif /* 0 */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
53
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
54 /* Unconditionally use unsigned char * for this. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
55 typedef unsigned char *POINTER;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
56
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
57 #ifdef DOUG_LEA_MALLOC
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
58 #define M_TOP_PAD -2
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
59 #include <malloc.h>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
60 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
61
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
62 #include "getpagesize.h"
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
63
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
64 #include <string.h>
3263
d674024a8674 [xemacs-hg @ 2006-02-27 16:29:00 by crestani]
crestani
parents: 3025
diff changeset
65 #ifndef NEW_GC
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
66 void refill_memory_reserve (void);
3263
d674024a8674 [xemacs-hg @ 2006-02-27 16:29:00 by crestani]
crestani
parents: 3025
diff changeset
67 #endif /* not NEW_GC */
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
68
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
69 #else /* Not emacs. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
70
1333
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents: 909
diff changeset
71 #define REGEX_MALLOC_CHECK()
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents: 909
diff changeset
72
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
73 #include <stddef.h>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
74
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
75 typedef void *POINTER;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
76
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
77 #include <unistd.h>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
78 #include <malloc.h>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
79 #include <string.h>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
80
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
81 #endif /* emacs. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
82
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
83 void init_ralloc (void);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
84
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
85 #define NIL ((POINTER) 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
86
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
87
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
88 #if !defined(HAVE_MMAP) || defined(DOUG_LEA_MALLOC)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
89
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
90 /* A flag to indicate whether we have initialized ralloc yet. For
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
91 Emacs's sake, please do not make this local to malloc_init; on some
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
92 machines, the dumping procedure makes all static variables
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
93 read-only. On these machines, the word static is #defined to be
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
94 the empty string, meaning that r_alloc_initialized becomes an
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
95 automatic variable, and loses its value each time Emacs is started up. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
96 static int r_alloc_initialized = 0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
97
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
98
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
99 /* Declarations for working with the malloc, ralloc, and system breaks. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
100
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
101 /* Function to set the real break value. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
102 static POINTER (*real_morecore) (ptrdiff_t size);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
103
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
104 /* The break value, as seen by malloc (). */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
105 static POINTER virtual_break_value;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
106
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
107 /* The break value, viewed by the relocatable blocs. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
108 static POINTER break_value;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
109
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
110 /* This is the size of a page. We round memory requests to this boundary. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
111 static int page_size;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
112
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
113 /* Whenever we get memory from the system, get this many extra bytes. This
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
114 must be a multiple of page_size. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
115 static int extra_bytes;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
116
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
117 /* Macros for rounding. Note that rounding to any value is possible
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
118 by changing the definition of PAGE. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
119 #define PAGE (getpagesize ())
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
120 #define ALIGNED(addr) (((unsigned long int) (addr) & (page_size - 1)) == 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
121 #define ROUNDUP(size) (((unsigned long int) (size) + page_size - 1) \
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
122 & ~(page_size - 1))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
123 #define ROUND_TO_PAGE(addr) (addr & (~(page_size - 1)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
124
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
125 #define MEM_ALIGN sizeof(double)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
126 #define MEM_ROUNDUP(addr) (((unsigned long int)(addr) + MEM_ALIGN - 1) \
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
127 & ~(MEM_ALIGN - 1))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
128
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
129 /* Data structures of heaps and blocs. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
130
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
131 /* The relocatable objects, or blocs, and the malloc data
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
132 both reside within one or more heaps.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
133 Each heap contains malloc data, running from `start' to `bloc_start',
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
134 and relocatable objects, running from `bloc_start' to `free'.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
135
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
136 Relocatable objects may relocate within the same heap
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
137 or may move into another heap; the heaps themselves may grow
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
138 but they never move.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
139
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
140 We try to make just one heap and make it larger as necessary.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
141 But sometimes we can't do that, because we can't get contiguous
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
142 space to add onto the heap. When that happens, we start a new heap. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
143
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
144 typedef struct heap
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
145 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
146 struct heap *next;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
147 struct heap *prev;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
148 /* Start of memory range of this heap. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
149 POINTER start;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
150 /* End of memory range of this heap. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
151 POINTER end;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
152 /* Start of relocatable data in this heap. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
153 POINTER bloc_start;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
154 /* Start of unused space in this heap. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
155 POINTER free;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
156 /* First bloc in this heap. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
157 struct bp *first_bloc;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
158 /* Last bloc in this heap. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
159 struct bp *last_bloc;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
160 } *heap_ptr;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
161
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
162 #define NIL_HEAP ((heap_ptr) 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
163 #define HEAP_PTR_SIZE (sizeof (struct heap))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
164
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
165 /* This is the first heap object.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
166 If we need additional heap objects, each one resides at the beginning of
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
167 the space it covers. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
168 static struct heap heap_base;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
169
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
170 /* Head and tail of the list of heaps. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
171 static heap_ptr first_heap, last_heap;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
172
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
173 /* These structures are allocated in the malloc arena.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
174 The linked list is kept in order of increasing '.data' members.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
175 The data blocks abut each other; if b->next is non-nil, then
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
176 b->data + b->size == b->next->data.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
177
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
178 An element with variable==NIL denotes a freed block, which has not yet
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
179 been collected. They may only appear while r_alloc_freeze > 0, and will be
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
180 freed when the arena is thawed. Currently, these blocs are not reusable,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
181 while the arena is frozen. Very inefficient. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
182
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
183 typedef struct bp
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
184 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
185 struct bp *next;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
186 struct bp *prev;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
187 POINTER *variable;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
188 POINTER data;
440
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
189 size_t size;
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
190 POINTER new_data; /* temporarily used for relocation */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
191 struct heap *heap; /* Heap this bloc is in. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
192 } *bloc_ptr;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
193
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
194 #define NIL_BLOC ((bloc_ptr) 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
195 #define BLOC_PTR_SIZE (sizeof (struct bp))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
196
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
197 /* Head and tail of the list of relocatable blocs. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
198 static bloc_ptr first_bloc, last_bloc;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
199
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
200 static int use_relocatable_buffers;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
201
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
202 /* If >0, no relocation whatsoever takes place. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
203 static int r_alloc_freeze_level;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
204
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
205 /* Obtain SIZE bytes of space. If enough space is not presently available
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
206 in our process reserve, (i.e., (page_break_value - break_value)),
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
207 this means getting more page-aligned space from the system.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
208
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
209 Return non-zero if all went well, or zero if we couldn't allocate
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
210 the memory. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
211
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
212 /* Functions to get and return memory from the system. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
213
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
214 /* Find the heap that ADDRESS falls within. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
215
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
216 static heap_ptr
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
217 find_heap (POINTER address)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
218 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
219 heap_ptr heap;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
220
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
221 for (heap = last_heap; heap; heap = heap->prev)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
222 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
223 if (heap->start <= address && address <= heap->end)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
224 return heap;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
225 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
226
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
227 return NIL_HEAP;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
228 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
229
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
230 /* Find SIZE bytes of space in a heap.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
231 Try to get them at ADDRESS (which must fall within some heap's range)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
232 if we can get that many within one heap.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
233
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
234 If enough space is not presently available in our reserve, this means
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
235 getting more page-aligned space from the system. If the returned space
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
236 is not contiguous to the last heap, allocate a new heap, and append it
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
237
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
238 obtain does not try to keep track of whether space is in use
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
239 or not in use. It just returns the address of SIZE bytes that
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
240 fall within a single heap. If you call obtain twice in a row
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
241 with the same arguments, you typically get the same value.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
242 to the heap list. It's the caller's responsibility to keep
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
243 track of what space is in use.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
244
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
245 Return the address of the space if all went well, or zero if we couldn't
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
246 allocate the memory. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
247
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
248 static POINTER
440
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
249 obtain (POINTER address, size_t size)
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
250 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
251 heap_ptr heap;
440
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
252 size_t already_available;
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
253
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
254 /* Find the heap that ADDRESS falls within. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
255 for (heap = last_heap; heap; heap = heap->prev)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
256 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
257 if (heap->start <= address && address <= heap->end)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
258 break;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
259 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
260
5050
6f2158fa75ed Fix quick-build, use asserts() in place of ABORT()
Ben Wing <ben@xemacs.org>
parents: 3263
diff changeset
261 assert (heap);
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
262
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
263 /* If we can't fit SIZE bytes in that heap,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
264 try successive later heaps. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
265 while (heap && address + size > heap->end)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
266 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
267 heap = heap->next;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
268 if (heap == NIL_HEAP)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
269 break;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
270 address = heap->bloc_start;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
271 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
272
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
273 /* If we can't fit them within any existing heap,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
274 get more space. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
275 if (heap == NIL_HEAP)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
276 {
3025
facf3239ba30 [xemacs-hg @ 2005-10-25 11:16:19 by ben]
ben
parents: 2796
diff changeset
277 POINTER new_ = (*real_morecore)(0);
440
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
278 size_t get;
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
279
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
280 already_available = (char *)last_heap->end - (char *)address;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
281
3025
facf3239ba30 [xemacs-hg @ 2005-10-25 11:16:19 by ben]
ben
parents: 2796
diff changeset
282 if (new_ != last_heap->end)
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
283 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
284 /* Someone else called sbrk. Make a new heap. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
285
3025
facf3239ba30 [xemacs-hg @ 2005-10-25 11:16:19 by ben]
ben
parents: 2796
diff changeset
286 heap_ptr new_heap = (heap_ptr) MEM_ROUNDUP (new_);
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
287 POINTER bloc_start = (POINTER) MEM_ROUNDUP ((POINTER)(new_heap + 1));
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
288
3025
facf3239ba30 [xemacs-hg @ 2005-10-25 11:16:19 by ben]
ben
parents: 2796
diff changeset
289 if ((*real_morecore) (bloc_start - new_) != new_)
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
290 return 0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
291
3025
facf3239ba30 [xemacs-hg @ 2005-10-25 11:16:19 by ben]
ben
parents: 2796
diff changeset
292 new_heap->start = new_;
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
293 new_heap->end = bloc_start;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
294 new_heap->bloc_start = bloc_start;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
295 new_heap->free = bloc_start;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
296 new_heap->next = NIL_HEAP;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
297 new_heap->prev = last_heap;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
298 new_heap->first_bloc = NIL_BLOC;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
299 new_heap->last_bloc = NIL_BLOC;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
300 last_heap->next = new_heap;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
301 last_heap = new_heap;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
302
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
303 address = bloc_start;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
304 already_available = 0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
305 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
306
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
307 /* Add space to the last heap (which we may have just created).
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
308 Get some extra, so we can come here less often. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
309
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
310 get = size + extra_bytes - already_available;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
311 get = (char *) ROUNDUP ((char *)last_heap->end + get)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
312 - (char *) last_heap->end;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
313
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
314 if ((*real_morecore) (get) != last_heap->end)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
315 return 0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
316
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
317 last_heap->end += get;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
318 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
319
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
320 return address;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
321 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
322
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
323 #if 0
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
324 /* Obtain SIZE bytes of space and return a pointer to the new area.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
325 If we could not allocate the space, return zero. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
326
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
327 static POINTER
440
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
328 get_more_space (size_t size)
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
329 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
330 POINTER ptr = break_value;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
331 if (obtain (size))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
332 return ptr;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
333 else
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
334 return 0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
335 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
336 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
337
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
338 /* Note that SIZE bytes of space have been relinquished by the process.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
339 If SIZE is more than a page, return the space to the system. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
340
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
341 static void
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
342 relinquish (void)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
343 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
344 register heap_ptr h;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
345 int excess = 0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
346
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
347 /* Add the amount of space beyond break_value
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
348 in all heaps which have extend beyond break_value at all. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
349
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
350 for (h = last_heap; h && break_value < h->end; h = h->prev)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
351 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
352 excess += (char *) h->end - (char *) ((break_value < h->bloc_start)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
353 ? h->bloc_start : break_value);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
354 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
355
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
356 if (excess > extra_bytes * 2 && (*real_morecore) (0) == last_heap->end)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
357 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
358 /* Keep extra_bytes worth of empty space.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
359 And don't free anything unless we can free at least extra_bytes. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
360 excess -= extra_bytes;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
361
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
362 if ((char *)last_heap->end - (char *)last_heap->bloc_start <= excess)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
363 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
364 /* This heap should have no blocs in it. */
5050
6f2158fa75ed Fix quick-build, use asserts() in place of ABORT()
Ben Wing <ben@xemacs.org>
parents: 3263
diff changeset
365 assert (last_heap->first_bloc == NIL_BLOC &&
6f2158fa75ed Fix quick-build, use asserts() in place of ABORT()
Ben Wing <ben@xemacs.org>
parents: 3263
diff changeset
366 last_heap->last_bloc == NIL_BLOC);
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
367
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
368 /* Return the last heap, with its header, to the system. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
369 excess = (char *)last_heap->end - (char *)last_heap->start;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
370 last_heap = last_heap->prev;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
371 last_heap->next = NIL_HEAP;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
372 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
373 else
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
374 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
375 excess = (char *) last_heap->end
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
376 - (char *) ROUNDUP ((char *)last_heap->end - excess);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
377 last_heap->end -= excess;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
378 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
379
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
380 if ((*real_morecore) (- excess) == 0)
2500
3d8143fc88e1 [xemacs-hg @ 2005-01-24 23:33:30 by ben]
ben
parents: 1799
diff changeset
381 ABORT ();
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
382 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
383 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
384
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
385 /* Return the total size in use by relocating allocator,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
386 above where malloc gets space. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
387
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
388 long r_alloc_size_in_use (void);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
389 long
440
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
390 r_alloc_size_in_use (void)
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
391 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
392 return break_value - virtual_break_value;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
393 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
394
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
395 /* The meat - allocating, freeing, and relocating blocs. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
396
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
397
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
398 /* Find the bloc referenced by the address in PTR. Returns a pointer
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
399 to that block. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
400
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
401 static bloc_ptr
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
402 find_bloc (POINTER *ptr)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
403 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
404 register bloc_ptr p = first_bloc;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
405
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
406 while (p != NIL_BLOC)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
407 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
408 if (p->variable == ptr && p->data == *ptr)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
409 return p;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
410
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
411 p = p->next;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
412 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
413
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
414 return p;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
415 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
416
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
417 /* Allocate a bloc of SIZE bytes and append it to the chain of blocs.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
418 Returns a pointer to the new bloc, or zero if we couldn't allocate
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
419 memory for the new block. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
420
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
421 static bloc_ptr
440
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
422 get_bloc (size_t size)
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
423 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
424 register bloc_ptr new_bloc;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
425 register heap_ptr heap;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
426
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
427 if (! (new_bloc = (bloc_ptr) malloc (BLOC_PTR_SIZE))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
428 || ! (new_bloc->data = obtain (break_value, size)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
429 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
430 if (new_bloc)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
431 free (new_bloc);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
432
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
433 return 0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
434 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
435
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
436 break_value = new_bloc->data + size;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
437
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
438 new_bloc->size = size;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
439 new_bloc->next = NIL_BLOC;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
440 new_bloc->variable = (POINTER *) NIL;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
441 new_bloc->new_data = 0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
442
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
443 /* Record in the heap that this space is in use. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
444 heap = find_heap (new_bloc->data);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
445 heap->free = break_value;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
446
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
447 /* Maintain the correspondence between heaps and blocs. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
448 new_bloc->heap = heap;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
449 heap->last_bloc = new_bloc;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
450 if (heap->first_bloc == NIL_BLOC)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
451 heap->first_bloc = new_bloc;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
452
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
453 /* Put this bloc on the doubly-linked list of blocs. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
454 if (first_bloc)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
455 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
456 new_bloc->prev = last_bloc;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
457 last_bloc->next = new_bloc;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
458 last_bloc = new_bloc;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
459 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
460 else
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
461 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
462 first_bloc = last_bloc = new_bloc;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
463 new_bloc->prev = NIL_BLOC;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
464 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
465
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
466 return new_bloc;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
467 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
468
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
469 /* Calculate new locations of blocs in the list beginning with BLOC,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
470 relocating it to start at ADDRESS, in heap HEAP. If enough space is
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
471 not presently available in our reserve, call obtain for
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
472 more space.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
473
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
474 Store the new location of each bloc in its new_data field.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
475 Do not touch the contents of blocs or break_value. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
476
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
477 static int
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
478 relocate_blocs (bloc_ptr bloc, heap_ptr heap, POINTER address)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
479 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
480 register bloc_ptr b = bloc;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
481
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
482 /* No need to ever call this if arena is frozen, bug somewhere! */
5050
6f2158fa75ed Fix quick-build, use asserts() in place of ABORT()
Ben Wing <ben@xemacs.org>
parents: 3263
diff changeset
483 assert (!r_alloc_freeze_level);
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
484
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
485 while (b)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
486 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
487 /* If bloc B won't fit within HEAP,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
488 move to the next heap and try again. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
489 while (heap && address + b->size > heap->end)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
490 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
491 heap = heap->next;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
492 if (heap == NIL_HEAP)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
493 break;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
494 address = heap->bloc_start;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
495 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
496
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
497 /* If BLOC won't fit in any heap,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
498 get enough new space to hold BLOC and all following blocs. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
499 if (heap == NIL_HEAP)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
500 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
501 register bloc_ptr tb = b;
440
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
502 register size_t s = 0;
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
503
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
504 /* Add up the size of all the following blocs. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
505 while (tb != NIL_BLOC)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
506 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
507 if (tb->variable)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
508 s += tb->size;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
509
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
510 tb = tb->next;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
511 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
512
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
513 /* Get that space. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
514 address = obtain (address, s);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
515 if (address == 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
516 return 0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
517
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
518 heap = last_heap;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
519 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
520
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
521 /* Record the new address of this bloc
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
522 and update where the next bloc can start. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
523 b->new_data = address;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
524 if (b->variable)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
525 address += b->size;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
526 b = b->next;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
527 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
528
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
529 return 1;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
530 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
531
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
532 #if 0 /* unused */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
533 /* Reorder the bloc BLOC to go before bloc BEFORE in the doubly linked list.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
534 This is necessary if we put the memory of space of BLOC
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
535 before that of BEFORE. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
536
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
537 static void
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
538 reorder_bloc (bloc_ptr bloc, bloc_ptr before)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
539 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
540 bloc_ptr prev, next;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
541
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
542 /* Splice BLOC out from where it is. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
543 prev = bloc->prev;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
544 next = bloc->next;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
545
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
546 if (prev)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
547 prev->next = next;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
548 if (next)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
549 next->prev = prev;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
550
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
551 /* Splice it in before BEFORE. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
552 prev = before->prev;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
553
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
554 if (prev)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
555 prev->next = bloc;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
556 bloc->prev = prev;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
557
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
558 before->prev = bloc;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
559 bloc->next = before;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
560 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
561 #endif /* unused */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
562
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
563 /* Update the records of which heaps contain which blocs, starting
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
564 with heap HEAP and bloc BLOC. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
565
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
566 static void
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
567 update_heap_bloc_correspondence (bloc_ptr bloc, heap_ptr heap)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
568 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
569 register bloc_ptr b;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
570
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
571 /* Initialize HEAP's status to reflect blocs before BLOC. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
572 if (bloc != NIL_BLOC && bloc->prev != NIL_BLOC && bloc->prev->heap == heap)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
573 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
574 /* The previous bloc is in HEAP. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
575 heap->last_bloc = bloc->prev;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
576 heap->free = bloc->prev->data + bloc->prev->size;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
577 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
578 else
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
579 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
580 /* HEAP contains no blocs before BLOC. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
581 heap->first_bloc = NIL_BLOC;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
582 heap->last_bloc = NIL_BLOC;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
583 heap->free = heap->bloc_start;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
584 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
585
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
586 /* Advance through blocs one by one. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
587 for (b = bloc; b != NIL_BLOC; b = b->next)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
588 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
589 /* Advance through heaps, marking them empty,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
590 till we get to the one that B is in. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
591 while (heap)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
592 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
593 if (heap->bloc_start <= b->data && b->data <= heap->end)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
594 break;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
595 heap = heap->next;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
596 /* We know HEAP is not null now,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
597 because there has to be space for bloc B. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
598 heap->first_bloc = NIL_BLOC;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
599 heap->last_bloc = NIL_BLOC;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
600 heap->free = heap->bloc_start;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
601 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
602
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
603 /* Update HEAP's status for bloc B. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
604 heap->free = b->data + b->size;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
605 heap->last_bloc = b;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
606 if (heap->first_bloc == NIL_BLOC)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
607 heap->first_bloc = b;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
608
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
609 /* Record that B is in HEAP. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
610 b->heap = heap;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
611 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
612
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
613 /* If there are any remaining heaps and no blocs left,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
614 mark those heaps as empty. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
615 heap = heap->next;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
616 while (heap)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
617 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
618 heap->first_bloc = NIL_BLOC;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
619 heap->last_bloc = NIL_BLOC;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
620 heap->free = heap->bloc_start;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
621 heap = heap->next;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
622 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
623 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
624
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
625 /* Resize BLOC to SIZE bytes. This relocates the blocs
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
626 that come after BLOC in memory. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
627
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
628 static int
440
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
629 resize_bloc (bloc_ptr bloc, size_t size)
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
630 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
631 register bloc_ptr b;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
632 heap_ptr heap;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
633 POINTER address;
440
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
634 size_t old_size;
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
635
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
636 /* No need to ever call this if arena is frozen, bug somewhere! */
5050
6f2158fa75ed Fix quick-build, use asserts() in place of ABORT()
Ben Wing <ben@xemacs.org>
parents: 3263
diff changeset
637 assert (!r_alloc_freeze_level);
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
638
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
639 if (bloc == NIL_BLOC || size == bloc->size)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
640 return 1;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
641
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
642 for (heap = first_heap; heap != NIL_HEAP; heap = heap->next)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
643 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
644 if (heap->bloc_start <= bloc->data && bloc->data <= heap->end)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
645 break;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
646 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
647
5050
6f2158fa75ed Fix quick-build, use asserts() in place of ABORT()
Ben Wing <ben@xemacs.org>
parents: 3263
diff changeset
648 assert (heap != NIL_HEAP);
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
649
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
650 old_size = bloc->size;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
651 bloc->size = size;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
652
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
653 /* Note that bloc could be moved into the previous heap. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
654 address = (bloc->prev ? bloc->prev->data + bloc->prev->size
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
655 : first_heap->bloc_start);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
656 while (heap)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
657 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
658 if (heap->bloc_start <= address && address <= heap->end)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
659 break;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
660 heap = heap->prev;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
661 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
662
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
663 if (! relocate_blocs (bloc, heap, address))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
664 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
665 bloc->size = old_size;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
666 return 0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
667 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
668
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
669 if (size > old_size)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
670 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
671 for (b = last_bloc; b != bloc; b = b->prev)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
672 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
673 if (!b->variable)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
674 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
675 b->size = 0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
676 b->data = b->new_data;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
677 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
678 else
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
679 {
440
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
680 memmove (b->new_data, b->data, b->size);
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
681 *b->variable = b->data = b->new_data;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
682 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
683 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
684 if (!bloc->variable)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
685 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
686 bloc->size = 0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
687 bloc->data = bloc->new_data;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
688 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
689 else
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
690 {
440
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
691 memmove (bloc->new_data, bloc->data, old_size);
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
692 memset (bloc->new_data + old_size, 0, size - old_size);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
693 *bloc->variable = bloc->data = bloc->new_data;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
694 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
695 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
696 else
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
697 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
698 for (b = bloc; b != NIL_BLOC; b = b->next)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
699 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
700 if (!b->variable)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
701 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
702 b->size = 0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
703 b->data = b->new_data;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
704 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
705 else
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
706 {
440
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
707 memmove (b->new_data, b->data, b->size);
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
708 *b->variable = b->data = b->new_data;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
709 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
710 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
711 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
712
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
713 update_heap_bloc_correspondence (bloc, heap);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
714
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
715 break_value = (last_bloc ? last_bloc->data + last_bloc->size
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
716 : first_heap->bloc_start);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
717 return 1;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
718 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
719
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
720 /* Free BLOC from the chain of blocs, relocating any blocs above it
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
721 and returning BLOC->size bytes to the free area. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
722
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
723 static void
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
724 free_bloc (bloc_ptr bloc)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
725 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
726 heap_ptr heap = bloc->heap;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
727
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
728 if (r_alloc_freeze_level)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
729 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
730 bloc->variable = (POINTER *) NIL;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
731 return;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
732 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
733
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
734 resize_bloc (bloc, 0);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
735
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
736 if (bloc == first_bloc && bloc == last_bloc)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
737 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
738 first_bloc = last_bloc = NIL_BLOC;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
739 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
740 else if (bloc == last_bloc)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
741 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
742 last_bloc = bloc->prev;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
743 last_bloc->next = NIL_BLOC;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
744 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
745 else if (bloc == first_bloc)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
746 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
747 first_bloc = bloc->next;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
748 first_bloc->prev = NIL_BLOC;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
749 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
750 else
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
751 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
752 bloc->next->prev = bloc->prev;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
753 bloc->prev->next = bloc->next;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
754 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
755
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
756 /* Update the records of which blocs are in HEAP. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
757 if (heap->first_bloc == bloc)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
758 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
759 if (bloc->next != 0 && bloc->next->heap == heap)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
760 heap->first_bloc = bloc->next;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
761 else
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
762 heap->first_bloc = heap->last_bloc = NIL_BLOC;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
763 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
764 if (heap->last_bloc == bloc)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
765 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
766 if (bloc->prev != 0 && bloc->prev->heap == heap)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
767 heap->last_bloc = bloc->prev;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
768 else
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
769 heap->first_bloc = heap->last_bloc = NIL_BLOC;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
770 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
771
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
772 relinquish ();
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
773 free (bloc);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
774 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
775
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
776 /* Interface routines. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
777
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
778 /* Obtain SIZE bytes of storage from the free pool, or the system, as
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
779 necessary. If relocatable blocs are in use, this means relocating
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
780 them. This function gets plugged into the GNU malloc's __morecore
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
781 hook.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
782
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
783 We provide hysteresis, never relocating by less than extra_bytes.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
784
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
785 If we're out of memory, we should return zero, to imitate the other
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
786 __morecore hook values - in particular, __default_morecore in the
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
787 GNU malloc package. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
788
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
789 POINTER r_alloc_sbrk (ptrdiff_t size);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
790 POINTER
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
791 r_alloc_sbrk (ptrdiff_t size)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
792 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
793 register bloc_ptr b;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
794 POINTER address;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
795
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
796 if (! r_alloc_initialized)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
797 init_ralloc ();
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
798
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
799 if (! use_relocatable_buffers)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
800 return (*real_morecore) (size);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
801
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
802 if (size == 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
803 return virtual_break_value;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
804
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
805 if (size > 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
806 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
807 /* Allocate a page-aligned space. GNU malloc would reclaim an
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
808 extra space if we passed an unaligned one. But we could
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
809 not always find a space which is contiguous to the previous. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
810 POINTER new_bloc_start;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
811 heap_ptr h = first_heap;
440
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
812 size_t get = ROUNDUP (size);
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
813
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
814 address = (POINTER) ROUNDUP (virtual_break_value);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
815
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
816 /* Search the list upward for a heap which is large enough. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
817 while ((char *) h->end < (char *) MEM_ROUNDUP ((char *)address + get))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
818 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
819 h = h->next;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
820 if (h == NIL_HEAP)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
821 break;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
822 address = (POINTER) ROUNDUP (h->start);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
823 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
824
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
825 /* If not found, obtain more space. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
826 if (h == NIL_HEAP)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
827 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
828 get += extra_bytes + page_size;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
829
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
830 if (! obtain (address, get))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
831 return 0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
832
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
833 if (first_heap == last_heap)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
834 address = (POINTER) ROUNDUP (virtual_break_value);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
835 else
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
836 address = (POINTER) ROUNDUP (last_heap->start);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
837 h = last_heap;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
838 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
839
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
840 new_bloc_start = (POINTER) MEM_ROUNDUP ((char *)address + get);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
841
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
842 if (first_heap->bloc_start < new_bloc_start)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
843 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
844 /* This is no clean solution - no idea how to do it better. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
845 if (r_alloc_freeze_level)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
846 return NIL;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
847
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
848 /* There is a bug here: if the above obtain call succeeded, but the
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
849 relocate_blocs call below does not succeed, we need to free
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
850 the memory that we got with obtain. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
851
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
852 /* Move all blocs upward. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
853 if (! relocate_blocs (first_bloc, h, new_bloc_start))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
854 return 0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
855
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
856 /* Note that (POINTER)(h+1) <= new_bloc_start since
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
857 get >= page_size, so the following does not destroy the heap
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
858 header. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
859 for (b = last_bloc; b != NIL_BLOC; b = b->prev)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
860 {
440
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
861 memmove (b->new_data, b->data, b->size);
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
862 *b->variable = b->data = b->new_data;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
863 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
864
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
865 h->bloc_start = new_bloc_start;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
866
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
867 update_heap_bloc_correspondence (first_bloc, h);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
868 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
869 if (h != first_heap)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
870 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
871 /* Give up managing heaps below the one the new
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
872 virtual_break_value points to. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
873 first_heap->prev = NIL_HEAP;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
874 first_heap->next = h->next;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
875 first_heap->start = h->start;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
876 first_heap->end = h->end;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
877 first_heap->free = h->free;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
878 first_heap->first_bloc = h->first_bloc;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
879 first_heap->last_bloc = h->last_bloc;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
880 first_heap->bloc_start = h->bloc_start;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
881
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
882 if (first_heap->next)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
883 first_heap->next->prev = first_heap;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
884 else
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
885 last_heap = first_heap;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
886 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
887
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
888 memset (address, 0, size);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
889 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
890 else /* size < 0 */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
891 {
440
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
892 size_t excess = (char *)first_heap->bloc_start
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
893 - ((char *)virtual_break_value + size);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
894
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
895 address = virtual_break_value;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
896
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
897 if (r_alloc_freeze_level == 0 && excess > 2 * extra_bytes)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
898 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
899 excess -= extra_bytes;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
900 first_heap->bloc_start
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
901 = (POINTER) MEM_ROUNDUP ((char *)first_heap->bloc_start - excess);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
902
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
903 relocate_blocs (first_bloc, first_heap, first_heap->bloc_start);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
904
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
905 for (b = first_bloc; b != NIL_BLOC; b = b->next)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
906 {
440
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
907 memmove (b->new_data, b->data, b->size);
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
908 *b->variable = b->data = b->new_data;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
909 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
910 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
911
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
912 if ((char *)virtual_break_value + size < (char *)first_heap->start)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
913 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
914 /* We found an additional space below the first heap */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
915 first_heap->start = (POINTER) ((char *)virtual_break_value + size);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
916 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
917 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
918
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
919 virtual_break_value = (POINTER) ((char *)address + size);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
920 break_value = (last_bloc
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
921 ? last_bloc->data + last_bloc->size
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
922 : first_heap->bloc_start);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
923 if (size < 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
924 relinquish ();
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
925
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
926 return address;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
927 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
928
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
929 /* Allocate a relocatable bloc of storage of size SIZE. A pointer to
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
930 the data is returned in *PTR. PTR is thus the address of some variable
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
931 which will use the data area.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
932
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
933 The allocation of 0 bytes is valid.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
934 In case r_alloc_freeze is set, a best fit of unused blocs could be done
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
935 before allocating a new area. Not yet done.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
936
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
937 If we can't allocate the necessary memory, set *PTR to zero, and
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
938 return zero. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
939
440
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
940 POINTER r_alloc (POINTER *ptr, size_t size);
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
941 POINTER
440
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
942 r_alloc (POINTER *ptr, size_t size)
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
943 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
944 bloc_ptr new_bloc;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
945
1333
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents: 909
diff changeset
946 REGEX_MALLOC_CHECK ();
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents: 909
diff changeset
947
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
948 if (! r_alloc_initialized)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
949 init_ralloc ();
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
950
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
951 new_bloc = get_bloc (size);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
952 if (new_bloc)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
953 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
954 new_bloc->variable = ptr;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
955 *ptr = new_bloc->data;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
956 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
957 else
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
958 *ptr = 0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
959
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
960 return *ptr;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
961 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
962
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
963 /* Free a bloc of relocatable storage whose data is pointed to by PTR.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
964 Store 0 in *PTR to show there's no block allocated. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
965
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
966 void r_alloc_free (POINTER *ptr);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
967 void
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
968 r_alloc_free (POINTER *ptr)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
969 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
970 register bloc_ptr dead_bloc;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
971
1333
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents: 909
diff changeset
972 REGEX_MALLOC_CHECK ();
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents: 909
diff changeset
973
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
974 if (! r_alloc_initialized)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
975 init_ralloc ();
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
976
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
977 dead_bloc = find_bloc (ptr);
5050
6f2158fa75ed Fix quick-build, use asserts() in place of ABORT()
Ben Wing <ben@xemacs.org>
parents: 3263
diff changeset
978 assert (dead_bloc != NIL_BLOC);
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
979
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
980 free_bloc (dead_bloc);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
981 *ptr = 0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
982
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
983 #ifdef emacs
3263
d674024a8674 [xemacs-hg @ 2006-02-27 16:29:00 by crestani]
crestani
parents: 3025
diff changeset
984 #ifndef NEW_GC
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
985 refill_memory_reserve ();
3263
d674024a8674 [xemacs-hg @ 2006-02-27 16:29:00 by crestani]
crestani
parents: 3025
diff changeset
986 #endif /* not NEW_GC */
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
987 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
988 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
989
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
990 /* Given a pointer at address PTR to relocatable data, resize it to SIZE.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
991 Do this by shifting all blocks above this one up in memory, unless
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
992 SIZE is less than or equal to the current bloc size, in which case
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
993 do nothing.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
994
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
995 In case r_alloc_freeze is set, a new bloc is allocated, and the
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
996 memory copied to it. Not very efficient. We could traverse the
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
997 bloc_list for a best fit of free blocs first.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
998
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
999 Change *PTR to reflect the new bloc, and return this value.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1000
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1001 If more memory cannot be allocated, then leave *PTR unchanged, and
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1002 return zero. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1003
440
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
1004 POINTER r_re_alloc (POINTER *ptr, size_t size);
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1005 POINTER
440
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
1006 r_re_alloc (POINTER *ptr, size_t size)
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1007 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1008 register bloc_ptr bloc;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1009
1333
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents: 909
diff changeset
1010 REGEX_MALLOC_CHECK ();
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents: 909
diff changeset
1011
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1012 if (! r_alloc_initialized)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1013 init_ralloc ();
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1014
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1015 if (!*ptr)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1016 return r_alloc (ptr, size);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1017 if (!size)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1018 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1019 r_alloc_free (ptr);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1020 return r_alloc (ptr, 0);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1021 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1022
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1023 bloc = find_bloc (ptr);
5050
6f2158fa75ed Fix quick-build, use asserts() in place of ABORT()
Ben Wing <ben@xemacs.org>
parents: 3263
diff changeset
1024 assert (bloc != NIL_BLOC);
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1025
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1026 if (size < bloc->size)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1027 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1028 /* Wouldn't it be useful to actually resize the bloc here? */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1029 /* I think so too, but not if it's too expensive... */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1030 if ((bloc->size - MEM_ROUNDUP (size) >= page_size)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1031 && r_alloc_freeze_level == 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1032 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1033 resize_bloc (bloc, MEM_ROUNDUP (size));
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1034 /* Never mind if this fails, just do nothing... */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1035 /* It *should* be infallible! */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1036 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1037 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1038 else if (size > bloc->size)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1039 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1040 if (r_alloc_freeze_level)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1041 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1042 bloc_ptr new_bloc;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1043 new_bloc = get_bloc (MEM_ROUNDUP (size));
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1044 if (new_bloc)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1045 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1046 new_bloc->variable = ptr;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1047 *ptr = new_bloc->data;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1048 bloc->variable = (POINTER *) NIL;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1049 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1050 else
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1051 return NIL;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1052 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1053 else
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1054 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1055 if (! resize_bloc (bloc, MEM_ROUNDUP (size)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1056 return NIL;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1057 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1058 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1059 return *ptr;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1060 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1061
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1062 /* Disable relocations, after making room for at least SIZE bytes
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1063 of non-relocatable heap if possible. The relocatable blocs are
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1064 guaranteed to hold still until thawed, even if this means that
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1065 malloc must return a null pointer. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1066
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1067 void r_alloc_freeze (long size);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1068 void
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1069 r_alloc_freeze (long size)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1070 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1071 if (! r_alloc_initialized)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1072 init_ralloc ();
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1073
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1074 /* If already frozen, we can't make any more room, so don't try. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1075 if (r_alloc_freeze_level > 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1076 size = 0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1077 /* If we can't get the amount requested, half is better than nothing. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1078 while (size > 0 && r_alloc_sbrk (size) == 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1079 size /= 2;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1080 ++r_alloc_freeze_level;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1081 if (size > 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1082 r_alloc_sbrk (-size);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1083 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1084
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1085 void r_alloc_thaw (void);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1086 void
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1087 r_alloc_thaw (void)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1088 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1089
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1090 if (! r_alloc_initialized)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1091 init_ralloc ();
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1092
5050
6f2158fa75ed Fix quick-build, use asserts() in place of ABORT()
Ben Wing <ben@xemacs.org>
parents: 3263
diff changeset
1093 assert (--r_alloc_freeze_level >= 0);
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1094
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1095 /* This frees all unused blocs. It is not too inefficient, as the resize
440
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
1096 and memmove is done only once. Afterwards, all unreferenced blocs are
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1097 already shrunk to zero size. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1098 if (!r_alloc_freeze_level)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1099 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1100 bloc_ptr *b = &first_bloc;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1101 while (*b)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1102 if (!(*b)->variable)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1103 free_bloc (*b);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1104 else
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1105 b = &(*b)->next;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1106 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1107 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1108
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1109
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1110 /* The hook `malloc' uses for the function which gets more space
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1111 from the system. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1112 #ifndef DOUG_LEA_MALLOC
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1113 extern POINTER (*__morecore) (ptrdiff_t size);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1114 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1115
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1116 /* Initialize various things for memory allocation. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1117
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1118 void
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1119 init_ralloc (void)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1120 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1121 if (r_alloc_initialized)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1122 return;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1123
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1124 r_alloc_initialized = 1;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1125 real_morecore = (POINTER (*) (ptrdiff_t)) __morecore;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1126 __morecore =
1799
19c89a2e24b6 [xemacs-hg @ 2003-11-20 16:04:45 by james]
james
parents: 1792
diff changeset
1127 #ifdef TYPEOF
19c89a2e24b6 [xemacs-hg @ 2003-11-20 16:04:45 by james]
james
parents: 1792
diff changeset
1128 (TYPEOF (__morecore))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1129 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1130 r_alloc_sbrk;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1131
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1132 first_heap = last_heap = &heap_base;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1133 first_heap->next = first_heap->prev = NIL_HEAP;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1134 first_heap->start = first_heap->bloc_start
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1135 = virtual_break_value = break_value = (*real_morecore) (0);
5050
6f2158fa75ed Fix quick-build, use asserts() in place of ABORT()
Ben Wing <ben@xemacs.org>
parents: 3263
diff changeset
1136 assert (break_value != NIL);
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1137
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1138 page_size = PAGE;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1139 extra_bytes = ROUNDUP (50000);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1140
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1141 #ifdef DOUG_LEA_MALLOC
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1142 mallopt (M_TOP_PAD, 64 * 4096);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1143 #else
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1144 #if 0 /* Hasn't been synched yet */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1145 /* Give GNU malloc's morecore some hysteresis
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1146 so that we move all the relocatable blocks much less often. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1147 __malloc_extra_blocks = 64;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1148 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1149 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1150
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1151 first_heap->end = (POINTER) ROUNDUP (first_heap->start);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1152
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1153 /* The extra call to real_morecore guarantees that the end of the
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1154 address space is a multiple of page_size, even if page_size is
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1155 not really the page size of the system running the binary in
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1156 which page_size is stored. This allows a binary to be built on a
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1157 system with one page size and run on a system with a smaller page
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1158 size. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1159 (*real_morecore) (first_heap->end - first_heap->start);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1160
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1161 /* Clear the rest of the last page; this memory is in our address space
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1162 even though it is after the sbrk value. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1163 /* Doubly true, with the additional call that explicitly adds the
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1164 rest of that page to the address space. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1165 memset (first_heap->start, 0, first_heap->end - first_heap->start);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1166 virtual_break_value = break_value = first_heap->bloc_start = first_heap->end;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1167 use_relocatable_buffers = 1;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1168 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1169
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1170 #if defined (emacs) && defined (DOUG_LEA_MALLOC)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1171
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1172 /* Reinitialize the morecore hook variables after restarting a dumped
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1173 Emacs. This is needed when using Doug Lea's malloc from GNU libc. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1174 void r_alloc_reinit (void);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1175 void
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1176 r_alloc_reinit (void)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1177 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1178 /* Only do this if the hook has been reset, so that we don't get an
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1179 infinite loop, in case Emacs was linked statically. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1180 if ( (POINTER (*) (ptrdiff_t)) __morecore != r_alloc_sbrk)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1181 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1182 real_morecore = (POINTER (*) (ptrdiff_t)) __morecore;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1183 __morecore =
1799
19c89a2e24b6 [xemacs-hg @ 2003-11-20 16:04:45 by james]
james
parents: 1792
diff changeset
1184 #ifdef TYPEOF
19c89a2e24b6 [xemacs-hg @ 2003-11-20 16:04:45 by james]
james
parents: 1792
diff changeset
1185 (TYPEOF (__morecore))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1186 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1187 r_alloc_sbrk;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1188 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1189 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1190 #if 0
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1191 #ifdef DEBUG
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1192
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1193 void
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1194 r_alloc_check (void)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1195 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1196 int found = 0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1197 heap_ptr h, ph = 0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1198 bloc_ptr b, pb = 0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1199
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1200 if (!r_alloc_initialized)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1201 return;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1202
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1203 assert (first_heap);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1204 assert (last_heap->end <= (POINTER) sbrk (0));
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1205 assert ((POINTER) first_heap < first_heap->start);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1206 assert (first_heap->start <= virtual_break_value);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1207 assert (virtual_break_value <= first_heap->end);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1208
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1209 for (h = first_heap; h; h = h->next)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1210 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1211 assert (h->prev == ph);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1212 assert ((POINTER) ROUNDUP (h->end) == h->end);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1213 #if 0 /* ??? The code in ralloc.c does not really try to ensure
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1214 the heap start has any sort of alignment.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1215 Perhaps it should. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1216 assert ((POINTER) MEM_ROUNDUP (h->start) == h->start);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1217 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1218 assert ((POINTER) MEM_ROUNDUP (h->bloc_start) == h->bloc_start);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1219 assert (h->start <= h->bloc_start && h->bloc_start <= h->end);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1220
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1221 if (ph)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1222 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1223 assert (ph->end < h->start);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1224 assert (h->start <= (POINTER)h && (POINTER)(h+1) <= h->bloc_start);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1225 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1226
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1227 if (h->bloc_start <= break_value && break_value <= h->end)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1228 found = 1;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1229
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1230 ph = h;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1231 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1232
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1233 assert (found);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1234 assert (last_heap == ph);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1235
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1236 for (b = first_bloc; b; b = b->next)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1237 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1238 assert (b->prev == pb);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1239 assert ((POINTER) MEM_ROUNDUP (b->data) == b->data);
440
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
1240 assert ((size_t) MEM_ROUNDUP (b->size) == b->size);
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1241
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1242 ph = 0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1243 for (h = first_heap; h; h = h->next)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1244 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1245 if (h->bloc_start <= b->data && b->data + b->size <= h->end)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1246 break;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1247 ph = h;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1248 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1249
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1250 assert (h);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1251
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1252 if (pb && pb->data + pb->size != b->data)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1253 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1254 assert (ph && b->data == h->bloc_start);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1255 while (ph)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1256 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1257 if (ph->bloc_start <= pb->data
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1258 && pb->data + pb->size <= ph->end)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1259 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1260 assert (pb->data + pb->size + b->size > ph->end);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1261 break;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1262 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1263 else
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1264 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1265 assert (ph->bloc_start + b->size > ph->end);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1266 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1267 ph = ph->prev;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1268 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1269 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1270 pb = b;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1271 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1272
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1273 assert (last_bloc == pb);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1274
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1275 if (last_bloc)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1276 assert (last_bloc->data + last_bloc->size == break_value);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1277 else
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1278 assert (first_heap->bloc_start == break_value);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1279 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1280 #endif /* DEBUG */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1281 #endif /* 0 */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1282
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1283 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1284
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1285 #else /* HAVE_MMAP */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1286
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1287 /*
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1288 A relocating allocator built using the mmap(2) facility available
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1289 in some OSes. Based on another version written by Paul Flinders,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1290 from which code (and comments) are snarfed.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1291
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1292 The OS should support mmap() with MAP_ANONYMOUS attribute, or have
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1293 /dev/zero. It should support private memory mapping.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1294
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1295 Paul Flinders wrote a version which works well for systems that
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1296 allow callers to specify (virtual) addresses to mmap().
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1297 Unfortunately, such a scheme doesn't work for certain systems like
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1298 HP-UX that have a system-wide virtual->real address map, and
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1299 consequently impose restrictions on the virtual address values
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1300 permitted.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1301
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1302 NB: The mapping scheme in HP-UX is motivated by the inverted page
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1303 table design in some HP processors.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1304
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1305 This alternate implementation allows for the addresses to be
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1306 optionally chosen by the system. Fortunately, buffer allocation
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1307 doesn't insist upon contiguous memory which Flinders' scheme
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1308 provides, and this one doesn't.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1309
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1310 We don't really provide for hysteresis here, but add some metering
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1311 to monitor how poorly the allocator actually works. See the
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1312 documentation for `mmap-hysteresis'.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1313
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1314 This implementation actually cycles through the blocks allocated
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1315 via mmap() and only sends it to free() if it wasn't one of them.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1316 Unfortunately, this is O(n) in the number of mmapped blocks. (Not
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1317 really, as we have a hash table which tries to reduce the cost.)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1318 Also, this dereferences the pointer passed, so it would cause a
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1319 segfault if garbage was passed to it. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1320
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1321 #include <fcntl.h>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1322 #include <sys/mman.h>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1323 #include <stdio.h>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1324
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1325 typedef void *VM_ADDR; /* VM addresses */
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 440
diff changeset
1326 static const VM_ADDR VM_FAILURE_ADDR = (VM_ADDR) -1; /* mmap returns this when it fails. */
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1327
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1328 /* Configuration for relocating allocator. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1329
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1330 /* #define MMAP_GENERATE_ADDRESSES */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1331 /* Define this if you want Emacs to manage the address table.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1332 It is not recommended unless you have major problems with the
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1333 default scheme, which allows the OS to pick addresses. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1334
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1335 /* USELESS_LOWER_ADDRESS_BITS defines the number of bits which can be
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1336 discarded while computing the hash, as they're always zero. The
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1337 default is appropriate for a page size of 4096 bytes. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1338
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1339 #define USELESS_LOWER_ADDRESS_BITS 12
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1340
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1341
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1342 /* Size of hash table for inverted VM_ADDR->MMAP_HANDLE lookup */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1343
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1344 #define MHASH_PRIME 89
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1345
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1346
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1347 /* Whether we want to enable metering of some ralloc performance.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1348 This incurs a constant penalty for each mmap operation. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1349
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1350 #define MMAP_METERING
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1351
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1352
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1353 /* Rename the following to protect against a some smartness elsewhere.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1354 We need access to the allocator used for non-mmap allocation
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1355 elsewhere, in case we get passed a handle that we didn't allocate
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1356 ourselves. Currently, this default allocator is also used to
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1357 maintain local structures for relocatable blocks. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1358
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1359 #define UNDERLYING_MALLOC malloc
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1360 #define UNDERLYING_FREE free
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1361 #define UNDERLYING_REALLOC realloc
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1362
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1363 /* MAP_ADDRCHOICE_FLAG is set to MAP_FIXED if MMAP_GENERATE_ADDRESSES
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1364 is defined, and MAP_VARIABLE otherwise. Some losing systems don't
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1365 define the _FIXED/_VARIABLE flags, in which case it is set to 0 */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1366
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1367 #ifdef MMAP_GENERATE_ADDRESSES
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1368 # ifdef MAP_FIXED
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1369 # define MAP_ADDRCHOICE_FLAG MAP_FIXED
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1370 # endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1371 #else /* !MMAP_GENERATE_ADDRESSES */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1372 # ifdef MAP_VARIABLE
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1373 # define MAP_ADDRCHOICE_FLAG MAP_VARIABLE
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1374 # endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1375 #endif /* MMAP_GENERATE_ADDRESSES */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1376
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1377 /* Default case. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1378 #ifndef MAP_ADDRCHOICE_FLAG
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1379 # define MAP_ADDRCHOICE_FLAG 0
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1380 #endif /* MAP_ADDRCHOICE_FLAG */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1381
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1382 #ifdef MAP_ANONYMOUS
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1383 # define MAP_FLAGS (MAP_PRIVATE | MAP_ADDRCHOICE_FLAG | MAP_ANONYMOUS)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1384 #else
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1385 # define MAP_FLAGS (MAP_PRIVATE | MAP_ADDRCHOICE_FLAG)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1386 #endif /* MAP_ANONYMOUS */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1387
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1388
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1389 /* (ptf): A flag to indicate whether we have initialized ralloc yet. For
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1390 Emacs's sake, please do not make this local to malloc_init; on some
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1391 machines, the dumping procedure makes all static variables
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1392 read-only. On these machines, the word static is #defined to be
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1393 the empty string, meaning that r_alloc_initialized becomes an
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1394 automatic variable, and loses its value each time Emacs is started up.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1395
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1396 If we're using mmap this flag has three possible values
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1397 0 - initial value
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1398 1 - Normal value when running temacs. In this case buffers
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1399 are allocated using malloc so that any data that they
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1400 contain becomes part of the undumped executable.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1401 2 - Normal value when running emacs */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1402 static int r_alloc_initialized = 0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1403
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1404 /* (ptf): Macros for rounding. Note that rounding to any value is possible
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1405 by changing the definition of PAGE. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1406 #define PAGE (getpagesize ())
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1407 #define PAGES_FOR(size) (((unsigned long int) (size) + page_size - 1)/page_size)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1408 #define ROUNDUP(size) ((unsigned long int)PAGES_FOR(size)*page_size)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1409
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1410
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1411 /* DEV_ZERO_FD is -1 normally, but for systems without MAP_ANONYMOUS
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1412 points to a file descriptor opened on /dev/zero */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1413
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1414 static int DEV_ZERO_FD = -1;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1415
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1416
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1417 /* We actually need a data structure that can be usefully structured
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1418 based on the VM address, and allows an ~O(1) lookup on an arbitrary
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1419 address, i.e. a hash table. Maybe the XEmacs hash table can be
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1420 coaxed enough. At the moment, we use lookup on a hash table to
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1421 decide whether to do an O(n) search on the malloced block list.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1422 Addresses are hashed to a bucket modulo MHASH_PRIME. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1423
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1424
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1425 /* We settle for a standard doubly-linked-list. The dynarr type isn't
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1426 very amenable to deletion of items in the middle, so we conjure up
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1427 yet another stupid datastructure. The structure is maintained as a
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1428 ring, and the singleton ring has the sole element as its left and
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1429 right neighbours. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1430
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1431 static void init_MHASH_table (void); /* Forward reference */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1432
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1433 typedef struct alloc_dll
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1434 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1435 size_t size; /* #bytes currently in use */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1436 size_t space_for; /* #bytes we really have */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1437 POINTER* aliased_address; /* Address of aliased variable, to tweak if relocating */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1438 VM_ADDR vm_addr; /* VM address returned by mmap */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1439 struct alloc_dll *left; /* Left link in circular doubly linked list */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1440 struct alloc_dll *right;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1441 } *MMAP_HANDLE;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1442
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1443 static MMAP_HANDLE mmap_start = 0; /* Head of linked list */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1444 static size_t page_size = 0; /* Size of VM pages */
458
c33ae14dd6d0 Import from CVS: tag r21-2-44
cvs
parents: 442
diff changeset
1445 static Fixnum mmap_hysteresis; /* Logically a "size_t" */
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1446
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1447 /* Get a new handle for a fresh block. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1448 static MMAP_HANDLE
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1449 new_mmap_handle (size_t nsiz)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1450 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1451 MMAP_HANDLE h = (MMAP_HANDLE) UNDERLYING_MALLOC( sizeof (struct alloc_dll));
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1452 if ( h == 0) return 0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1453 h->size = nsiz;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1454 if (mmap_start == 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1455 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1456 init_MHASH_table ();
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1457 mmap_start = h; mmap_start->left = h; mmap_start->right = h;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1458 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1459 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1460 MMAP_HANDLE prev = mmap_start->left;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1461 MMAP_HANDLE nex = mmap_start;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1462
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1463 /* Four pointers need fixing. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1464 h->right = nex;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1465 h->left = prev;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1466 prev->right = h;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1467 nex->left = h;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1468 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1469 return h;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1470 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1471
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1472 /* Find a handle given the aliased address using linear search. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1473 static MMAP_HANDLE
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1474 find_mmap_handle_lsearch (POINTER *alias)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1475 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1476 MMAP_HANDLE h = mmap_start;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1477 if (h == 0) return 0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1478 do {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1479 if (h->aliased_address == alias && *alias == h->vm_addr)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1480 return h;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1481 h = h->right;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1482 } while( h != mmap_start );
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1483 return 0; /* Bogus alias passed. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1484 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1485
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1486 /* Free a handle. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1487 static void
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1488 free_mmap_handle (MMAP_HANDLE h)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1489 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1490 MMAP_HANDLE prev = h->left;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1491 MMAP_HANDLE nex = h->right;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1492 if (prev == h || nex == h) /* In fact, this should be && */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1493 { /* We're the singleton dll */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1494 UNDERLYING_FREE( h ); /* Free the sole item */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1495 mmap_start = 0; return;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1496 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1497 else if (h == mmap_start)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1498 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1499 mmap_start = nex; /* Make sure mmap_start isn't bogus. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1500 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1501 prev->right = nex;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1502 nex->left = prev;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1503 UNDERLYING_FREE( h );
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1504 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1505
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1506 /* A simple hash table to speed up the inverted lookup of
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1507 VM_ADDR->MMAP_HANDLE. We maintain the number of hits for a
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1508 particular bucket. We invalidate a hash table entry during block
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1509 deletion if the hash has cached the deleted block's address. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1510
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1511 /* Simple hash check. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1512 struct {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1513 int n_hits; /* How many addresses map to this? */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1514 MMAP_HANDLE handle; /* What is the current handle? */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1515 VM_ADDR addr; /* What is its VM address? */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1516 } MHASH_HITS[ MHASH_PRIME ];
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1517
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1518 static void
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1519 init_MHASH_table (void)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1520 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1521 int i = 0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1522 for (; i < MHASH_PRIME; i++)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1523 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1524 MHASH_HITS[i].n_hits = 0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1525 MHASH_HITS[i].addr = 0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1526 MHASH_HITS[i].handle = 0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1527 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1528 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1529
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1530 /* Compute the hash value for an address. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1531 static int
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1532 MHASH (VM_ADDR addr)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1533 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1534 #if (LONGBITS == 64)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1535 unsigned long int addr_shift = (unsigned long int)(addr) >> USELESS_LOWER_ADDRESS_BITS;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1536 #else
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1537 unsigned int addr_shift = (unsigned int)(addr) >> USELESS_LOWER_ADDRESS_BITS;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1538 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1539 int hval = addr_shift % MHASH_PRIME; /* We could have addresses which are -ve
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1540 when converted to signed ints */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1541 return ((hval >= 0) ? hval : MHASH_PRIME + hval);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1542 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1543
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1544 /* Add a VM address with its corresponding handle to the table. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1545 static void
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1546 MHASH_ADD (VM_ADDR addr, MMAP_HANDLE h)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1547 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1548 int kVal = MHASH( addr );
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1549 if (MHASH_HITS[kVal].n_hits++ == 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1550 { /* Only overwrite the table if there were no hits so far. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1551 MHASH_HITS[kVal].addr = addr;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1552 MHASH_HITS[kVal].handle = h;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1553 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1554 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1555
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1556 /* Delete a VM address entry from the hash table. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1557 static void
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1558 MHASH_DEL (VM_ADDR addr)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1559 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1560 int kVal = MHASH( addr );
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1561 MHASH_HITS[kVal].n_hits--;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1562 if (addr == MHASH_HITS[kVal].addr)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1563 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1564 MHASH_HITS[kVal].addr = 0; /* Invalidate cache. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1565 MHASH_HITS[kVal].handle = 0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1566 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1567 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1568
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1569 /* End of hash buckets */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1570
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1571 /* Metering malloc performance. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1572 #ifdef MMAP_METERING
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1573 /* If we're metering, we introduce some extra symbols to aid the noble
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1574 cause of bloating XEmacs core size. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1575
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1576 static Lisp_Object Qmmap_times_mapped;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1577 static Lisp_Object Qmmap_pages_mapped;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1578 static Lisp_Object Qmmap_times_unmapped;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1579 static Lisp_Object Qmmap_times_remapped;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1580 static Lisp_Object Qmmap_didnt_copy;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1581 static Lisp_Object Qmmap_pages_copied;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1582 static Lisp_Object Qmmap_average_bumpval;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1583 static Lisp_Object Qmmap_wastage;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1584 static Lisp_Object Qmmap_live_pages;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1585 static Lisp_Object Qmmap_addr_looked_up;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1586 static Lisp_Object Qmmap_hash_worked;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1587 static Lisp_Object Qmmap_addrlist_size;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1588
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1589 #define M_Map 0 /* How many times allocated? */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1590 #define M_Pages_Map 1 /* How many pages allocated? */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1591 #define M_Unmap 2 /* How many times freed? */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1592 #define M_Remap 3 /* How many times increased in size? */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1593 #define M_Didnt_Copy 4 /* How many times didn't need to copy? */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1594 #define M_Copy_Pages 5 /* Total # pages copied */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1595 #define M_Average_Bumpval 6 /* Average bump value */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1596 #define M_Wastage 7 /* Remaining (unused space) */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1597 #define M_Live_Pages 8 /* #live pages */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1598 #define M_Address_Lookup 9 /* How many times did we need to check if an addr is in the block? */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1599 #define M_Hash_Worked 10 /* How many times did the simple hash check work? */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1600 #define M_Addrlist_Size 11 /* What is the size of the XEmacs memory map? */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1601
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1602 #define N_Meterables 12 /* Total number of meterables */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1603 #define MEMMETER(x) {x;}
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1604 #define MVAL(x) (meter[x])
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1605 #define MLVAL(x) (make_int (meter[x]))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1606 static int meter[N_Meterables];
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1607
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1608 DEFUN ("mmap-allocator-status", Fmmap_allocator_status, 0, 0, 0, /*
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1609 Return some information about mmap-based allocator.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1610
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1611 mmap-times-mapped: number of times r_alloc was called.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1612 mmap-pages-mapped: number of pages mapped by r_alloc calls only.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1613 mmap-times-unmapped: number of times r_free was called.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1614 mmap-times-remapped: number of times r_re_alloc was called.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1615 mmap-didnt-copy: number of times re-alloc did NOT have to move the block.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1616 mmap-pages-copied: total number of pages copied.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1617 mmap-average-bumpval: average increase in size demanded to re-alloc.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1618 mmap-wastage: total number of bytes allocated, but not currently in use.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1619 mmap-live-pages: total number of pages live.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1620 mmap-addr-looked-up: total number of times needed to check if addr is in block.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1621 mmap-hash-worked: total number of times the simple hash check worked.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1622 mmap-addrlist-size: number of entries in address picking list.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1623 */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1624 ())
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1625 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1626 Lisp_Object result = Qnil;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1627
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1628 result = cons3 (Qmmap_addrlist_size, MLVAL (M_Addrlist_Size), result);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1629 result = cons3 (Qmmap_hash_worked, MLVAL (M_Hash_Worked), result);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1630 result = cons3 (Qmmap_addr_looked_up, MLVAL (M_Address_Lookup), result);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1631 result = cons3 (Qmmap_live_pages, MLVAL (M_Live_Pages), result);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1632 result = cons3 (Qmmap_wastage, MLVAL (M_Wastage), result);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1633 result = cons3 (Qmmap_average_bumpval,MLVAL (M_Average_Bumpval), result);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1634 result = cons3 (Qmmap_pages_copied, MLVAL (M_Copy_Pages), result);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1635 result = cons3 (Qmmap_didnt_copy, MLVAL (M_Didnt_Copy), result);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1636 result = cons3 (Qmmap_times_remapped, MLVAL (M_Remap), result);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1637 result = cons3 (Qmmap_times_unmapped, MLVAL (M_Unmap), result);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1638 result = cons3 (Qmmap_pages_mapped, MLVAL (M_Pages_Map), result);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1639 result = cons3 (Qmmap_times_mapped, MLVAL (M_Map), result);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1640
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1641 return result;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1642 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1643
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1644 #else /* !MMAP_METERING */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1645
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1646 #define MEMMETER(x)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1647 #define MVAL(x)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1648
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1649 #endif /* MMAP_METERING */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1650
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1651 static MMAP_HANDLE
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1652 find_mmap_handle (POINTER *alias)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1653 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1654 int kval = MHASH( *alias );
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1655 MEMMETER( MVAL(M_Address_Lookup)++ )
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1656 switch( MHASH_HITS[kval].n_hits)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1657 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1658 case 0:
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1659 MEMMETER( MVAL( M_Hash_Worked )++ )
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1660 return 0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1661
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1662 case 1:
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1663 if (*alias == MHASH_HITS[kval].addr)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1664 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1665 MEMMETER( MVAL( M_Hash_Worked) ++ );
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1666 return MHASH_HITS[kval].handle;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1667 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1668 /* FALL THROUGH */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1669 default:
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1670 return find_mmap_handle_lsearch( alias );
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1671 } /* switch */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1672 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1673
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1674 /*
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1675 Some kernels don't like being asked to pick addresses for mapping
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1676 themselves---IRIX is known to become extremely slow if mmap is
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1677 passed a ZERO as the first argument. In such cases, we use an
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1678 address map which is managed local to the XEmacs process. The
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1679 address map maintains an ordered linked list of (address, size,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1680 occupancy) triples ordered by the absolute address. Initially, a
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1681 large address area is marked as being empty. The address picking
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1682 scheme takes bites off the first block which is still empty and
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1683 large enough. If mmap with the specified address fails, it is
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1684 marked unavailable and not attempted thereafter. The scheme will
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1685 keep fragmenting the large empty block until it finds an address
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1686 which can be successfully mmapped, or until there are no free
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1687 blocks of the given size left.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1688
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1689 Note that this scheme, given its first-fit strategy, is prone to
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1690 fragmentation of the first part of memory earmarked for this
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1691 purpose. [ACP Vol I]. We can't use the workaround of using a
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1692 randomized first fit because we don't want to presume too much
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1693 about the memory map. Instead, we try to coalesce empty or
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1694 unavailable blocks at any available opportunity. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1695
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1696 /* Initialization procedure for address picking scheme */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1697 static void Addr_Block_initialize(void);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1698
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1699 /* Get a suitable VM_ADDR via mmap */
440
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
1700 static VM_ADDR New_Addr_Block (size_t sz);
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1701
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1702 /* Free a VM_ADDR allocated via New_Addr_Block */
440
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
1703 static void Free_Addr_Block (VM_ADDR addr, size_t sz);
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1704
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1705 #ifdef MMAP_GENERATE_ADDRESSES
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1706 /* Implementation of the three calls for address picking when XEmacs is incharge */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1707
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1708 /* The enum denotes the status of the following block. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1709 typedef enum { empty = 0, occupied, unavailable } addr_status;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1710
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1711 typedef struct addr_chain
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1712 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1713 POINTER addr;
440
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
1714 size_t sz;
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1715 addr_status flag;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1716 struct addr_chain *next;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1717 } ADDRESS_BLOCK, *ADDRESS_CHAIN;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1718 /* NB: empty and unavailable blocks are concatenated. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1719
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1720 static ADDRESS_CHAIN addr_chain = 0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1721 /* Start off the address block chain with a humongous address block
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1722 which is empty to start with. Note that addr_chain is invariant
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1723 WRT the addition/deletion of address blocks because of the assert
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1724 in Coalesce() and the strict ordering of blocks by their address
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1725 */
440
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
1726 static void
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
1727 Addr_Block_initialize (void)
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1728 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1729 MEMMETER( MVAL( M_Addrlist_Size )++)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1730 addr_chain = (ADDRESS_CHAIN) UNDERLYING_MALLOC( sizeof( ADDRESS_BLOCK ));
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1731 addr_chain->next = 0; /* Last block in chain */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1732 addr_chain->sz = 0x0c000000; /* Size */
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 440
diff changeset
1733 addr_chain->addr = (POINTER) (0x04000000);
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1734 addr_chain->flag = empty;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1735 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1736
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1737 /* Coalesce address blocks if they are contiguous. Only empty and
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1738 unavailable slots are coalesced. */
440
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
1739 static void
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
1740 Coalesce_Addr_Blocks (void)
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1741 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1742 ADDRESS_CHAIN p;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1743 for (p = addr_chain; p; p = p->next)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1744 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1745 while (p->next && p->flag == p->next->flag)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1746 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1747 ADDRESS_CHAIN np;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1748 np = p->next;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1749
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1750 if (p->flag == occupied) break; /* No cigar */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1751
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1752 /* Check if the addresses are contiguous. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1753 if (p->addr + p->sz != np->addr) break;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1754
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1755 MEMMETER( MVAL( M_Addrlist_Size )--)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1756 /* We can coalesce these two. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1757 p->sz += np->sz;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1758 p->next = np->next;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1759 assert( np != addr_chain ); /* We're not freeing the head of the list. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1760 UNDERLYING_FREE( np );
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1761 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1762 } /* for all p */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1763 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1764
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1765 /* Get an empty address block of specified size. */
440
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
1766 static VM_ADDR
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
1767 New_Addr_Block (size_t sz)
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1768 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1769 ADDRESS_CHAIN p = addr_chain;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1770 VM_ADDR new_addr = VM_FAILURE_ADDR;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1771 for (; p; p = p->next)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1772 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1773 if (p->flag == empty && p->sz > sz)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1774 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1775 /* Create a new entry following p which is empty. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1776 ADDRESS_CHAIN remainder = (ADDRESS_CHAIN) UNDERLYING_MALLOC( sizeof( ADDRESS_BLOCK ) );
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1777 remainder->next = p->next;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1778 remainder->flag = empty;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1779 remainder->addr = p->addr + sz;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1780 remainder->sz = p->sz - sz;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1781
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1782 MEMMETER( MVAL( M_Addrlist_Size )++)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1783
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1784 /* Now make p become an occupied block with the appropriate size */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1785 p->next = remainder;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1786 p->sz = sz;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1787 new_addr = mmap( (VM_ADDR) p->addr, p->sz, PROT_READ|PROT_WRITE,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1788 MAP_FLAGS, DEV_ZERO_FD, 0 );
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1789 if (new_addr == VM_FAILURE_ADDR)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1790 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1791 p->flag = unavailable;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1792 continue;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1793 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1794 p->flag = occupied;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1795 break;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1796 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1797 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1798 Coalesce_Addr_Blocks();
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1799 return new_addr;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1800 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1801
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1802 /* Free an address block. We mark the block as being empty, and attempt to
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1803 do any coalescing that may have resulted from this. */
440
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
1804 static void
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
1805 Free_Addr_Block (VM_ADDR addr, size_t sz)
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1806 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1807 ADDRESS_CHAIN p = addr_chain;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1808 for (; p; p = p->next )
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1809 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1810 if (p->addr == addr)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1811 {
5050
6f2158fa75ed Fix quick-build, use asserts() in place of ABORT()
Ben Wing <ben@xemacs.org>
parents: 3263
diff changeset
1812 assert (p->sz == sz); /* ACK! Shouldn't happen at all. */
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1813 munmap( (VM_ADDR) p->addr, p->sz );
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1814 p->flag = empty;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1815 break;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1816 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1817 }
5050
6f2158fa75ed Fix quick-build, use asserts() in place of ABORT()
Ben Wing <ben@xemacs.org>
parents: 3263
diff changeset
1818 assert (p); /* Can't happen... we've got a block to free which is not in
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1819 the address list. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1820 Coalesce_Addr_Blocks();
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1821 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1822 #else /* !MMAP_GENERATE_ADDRESSES */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1823 /* This is an alternate (simpler) implementation in cases where the
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1824 address is picked by the kernel. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1825
440
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
1826 static void
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
1827 Addr_Block_initialize (void)
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1828 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1829 /* Nothing. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1830 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1831
440
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
1832 static VM_ADDR
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
1833 New_Addr_Block (size_t sz)
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1834 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1835 return mmap (0, sz, PROT_READ|PROT_WRITE, MAP_FLAGS,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1836 DEV_ZERO_FD, 0 );
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1837 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1838
440
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
1839 static void
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
1840 Free_Addr_Block (VM_ADDR addr, size_t sz)
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1841 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1842 munmap ((caddr_t) addr, sz );
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1843 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1844
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1845 #endif /* MMAP_GENERATE_ADDRESSES */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1846
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1847
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1848 /* IMPLEMENTATION OF EXPORTED RELOCATOR INTERFACE */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1849
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1850 /*
440
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
1851 r_alloc (POINTER, SIZE): Allocate a relocatable area with the start
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1852 address aliased to the first parameter.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1853 */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1854
440
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
1855 POINTER r_alloc (POINTER *ptr, size_t size);
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1856 POINTER
440
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
1857 r_alloc (POINTER *ptr, size_t size)
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1858 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1859 MMAP_HANDLE mh;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1860
1333
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents: 909
diff changeset
1861 REGEX_MALLOC_CHECK ();
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents: 909
diff changeset
1862
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1863 switch(r_alloc_initialized)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1864 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1865 case 0:
2500
3d8143fc88e1 [xemacs-hg @ 2005-01-24 23:33:30 by ben]
ben
parents: 1799
diff changeset
1866 ABORT();
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1867 case 1:
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1868 *ptr = (POINTER) UNDERLYING_MALLOC(size);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1869 break;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1870 default:
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1871 mh = new_mmap_handle( size );
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1872 if (mh)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1873 {
440
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
1874 size_t hysteresis = (mmap_hysteresis > 0 ? mmap_hysteresis : 0);
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
1875 size_t mmapped_size = ROUNDUP( size + hysteresis );
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1876 MEMMETER( MVAL(M_Map)++ )
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1877 MEMMETER( MVAL(M_Pages_Map) += (mmapped_size/page_size) )
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1878 MEMMETER( MVAL(M_Wastage) += mmapped_size - size )
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1879 MEMMETER( MVAL(M_Live_Pages) += (mmapped_size/page_size) )
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1880 mh->vm_addr = New_Addr_Block( mmapped_size );
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1881 if (mh->vm_addr == VM_FAILURE_ADDR) {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1882 free_mmap_handle( mh ); /* Free the loser */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1883 *ptr = 0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1884 return 0; /* ralloc failed due to mmap() failure. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1885 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1886 MHASH_ADD( mh->vm_addr, mh );
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1887 mh->space_for = mmapped_size;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1888 mh->aliased_address = ptr;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1889 *ptr = (POINTER) mh->vm_addr;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1890 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1891 else
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1892 *ptr = 0; /* Malloc of block failed */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1893 break;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1894 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1895 return *ptr;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1896 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1897
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1898 /* Free a bloc of relocatable storage whose data is pointed to by PTR.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1899 Store 0 in *PTR to show there's no block allocated. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1900
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1901 void r_alloc_free (POINTER *ptr);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1902 void
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1903 r_alloc_free (POINTER *ptr)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1904 {
1333
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents: 909
diff changeset
1905 REGEX_MALLOC_CHECK ();
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents: 909
diff changeset
1906
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1907 switch( r_alloc_initialized) {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1908 case 0:
2500
3d8143fc88e1 [xemacs-hg @ 2005-01-24 23:33:30 by ben]
ben
parents: 1799
diff changeset
1909 ABORT();
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1910
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1911 case 1:
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1912 UNDERLYING_FREE( *ptr ); /* Certain this is from the heap. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1913 break;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1914
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1915 default:
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1916 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1917 MMAP_HANDLE dead_handle = find_mmap_handle( ptr );
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1918 /* Check if we've got it. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1919 if (dead_handle == 0) /* Didn't find it in the list of mmap handles */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1920 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1921 UNDERLYING_FREE( *ptr );
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1922 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1923 else
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1924 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1925 MEMMETER( MVAL( M_Wastage ) -= (dead_handle->space_for - dead_handle->size) )
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1926 MEMMETER( MVAL( M_Live_Pages ) -= (dead_handle->space_for / page_size ))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1927 MEMMETER(MVAL(M_Unmap)++)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1928 MHASH_DEL( dead_handle->vm_addr );
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1929 Free_Addr_Block( dead_handle->vm_addr, dead_handle->space_for );
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1930 free_mmap_handle (dead_handle);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1931 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1932 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1933 break;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1934 } /* r_alloc_initialized */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1935 *ptr = 0; /* Zap the pointer's contents. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1936 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1937
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1938 /* Given a pointer at address PTR to relocatable data, resize it to SIZE.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1939
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1940 Change *PTR to reflect the new bloc, and return this value.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1941
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1942 If more memory cannot be allocated, then leave *PTR unchanged, and
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1943 return zero. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1944
440
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
1945 POINTER r_re_alloc (POINTER *ptr, size_t sz);
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1946 POINTER
440
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
1947 r_re_alloc (POINTER *ptr, size_t sz)
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1948 {
1333
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents: 909
diff changeset
1949 REGEX_MALLOC_CHECK ();
1b0339b048ce [xemacs-hg @ 2003-03-02 09:38:37 by ben]
ben
parents: 909
diff changeset
1950
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1951 if (r_alloc_initialized == 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1952 {
2500
3d8143fc88e1 [xemacs-hg @ 2005-01-24 23:33:30 by ben]
ben
parents: 1799
diff changeset
1953 ABORT ();
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1954 return 0; /* suppress compiler warning */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1955 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1956 else if (r_alloc_initialized == 1)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1957 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1958 POINTER tmp = (POINTER) realloc(*ptr, sz);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1959 if (tmp)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1960 *ptr = tmp;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1961 return tmp;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1962 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1963 else
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1964 {
440
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
1965 size_t hysteresis = (mmap_hysteresis > 0 ? mmap_hysteresis : 0);
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
1966 size_t actual_sz = ROUNDUP( sz + hysteresis );
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1967 MMAP_HANDLE h = find_mmap_handle( ptr );
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1968 VM_ADDR new_vm_addr;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1969
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1970 if ( h == 0 ) /* Was allocated using malloc. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1971 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1972 POINTER tmp = (POINTER) UNDERLYING_REALLOC(*ptr, sz);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1973 if (tmp)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1974 *ptr = tmp;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1975 return tmp;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1976 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1977
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1978 MEMMETER(
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1979 MVAL(M_Average_Bumpval) =
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1980 (((double) MVAL(M_Remap) * MVAL(M_Average_Bumpval)) + (sz - h->size))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1981 / (double) (MVAL(M_Remap) + 1))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1982 MEMMETER(MVAL(M_Remap)++)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1983 if (h->space_for > sz) /* We've got some more room */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1984 { /* Also, if a shrinkage was asked for. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1985 MEMMETER( MVAL(M_Didnt_Copy)++ )
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1986 MEMMETER( MVAL(M_Wastage) -= (sz - h->size))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1987 /* We're pretty dumb at handling shrinkage. We should check for
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1988 a larger gap than the standard hysteresis allowable, and if so,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1989 shrink the number of pages. Right now, we simply reset the size
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1990 component and return. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1991 h->size = sz;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1992 return *ptr;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1993 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1994
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1995 new_vm_addr = New_Addr_Block( actual_sz );
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1996 if (new_vm_addr == VM_FAILURE_ADDR)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1997 {/* Failed to realloc. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1998 /* *ptr = 0; */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1999 return 0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
2000 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
2001
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
2002 MHASH_ADD( new_vm_addr, h );
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
2003 /* We got a block OK: now we should move the old contents to the
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
2004 new address. We use the old size of this block. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
2005 memmove(new_vm_addr, h->vm_addr, h->size);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
2006 MHASH_DEL( h->vm_addr );
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
2007 Free_Addr_Block( h->vm_addr, h->space_for ); /* Unmap old area. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
2008
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
2009 MEMMETER( MVAL( M_Copy_Pages ) += (h->space_for/page_size) )
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
2010 MEMMETER( MVAL( M_Live_Pages ) -= (h->space_for / page_size))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
2011 MEMMETER( MVAL( M_Live_Pages ) += (actual_sz / page_size))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
2012 MEMMETER( MVAL( M_Wastage ) -= (h->space_for - h->size))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
2013 MEMMETER( MVAL( M_Wastage ) += (actual_sz - sz) )
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
2014
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
2015 /* Update block datastructure. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
2016 h->space_for = actual_sz; /* New total space */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
2017 h->size = sz; /* New (requested) size */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
2018 h->vm_addr = new_vm_addr; /* New VM start address */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
2019 h->aliased_address = ptr; /* Change alias to reflect block relocation. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
2020 *ptr = (POINTER) h->vm_addr;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
2021 return *ptr;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
2022 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
2023 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
2024
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
2025
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
2026 /* Initialize various things for memory allocation.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
2027 */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
2028 void
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
2029 init_ralloc (void)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
2030 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
2031 int i = 0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
2032 if (r_alloc_initialized > 1)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
2033 return; /* used to return 1 */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
2034
909
a703d313962d [xemacs-hg @ 2002-07-09 11:55:35 by michaels]
michaels
parents: 613
diff changeset
2035 #ifdef PDUMP
a703d313962d [xemacs-hg @ 2002-07-09 11:55:35 by michaels]
michaels
parents: 613
diff changeset
2036 /* Under pdump, we need to activate ralloc on the first go. */
a703d313962d [xemacs-hg @ 2002-07-09 11:55:35 by michaels]
michaels
parents: 613
diff changeset
2037 ++r_alloc_initialized;
a703d313962d [xemacs-hg @ 2002-07-09 11:55:35 by michaels]
michaels
parents: 613
diff changeset
2038 #endif
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
2039 if (++r_alloc_initialized == 1)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
2040 return; /* used to return 1 */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
2041
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
2042 Addr_Block_initialize(); /* Initialize the address picker, if required. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
2043 page_size = PAGE;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
2044 assert( page_size > 0 ); /* getpagesize() bogosity check. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
2045
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
2046 #ifndef MAP_ANONYMOUS
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
2047 DEV_ZERO_FD = open( "/dev/zero", O_RDWR );
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
2048 if (DEV_ZERO_FD < 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
2049 /* Failed. Perhaps we should abort here? */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
2050 return; /* used to return 0 */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
2051 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
2052
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
2053 #ifdef MMAP_METERING
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
2054 for(i = 0; i < N_Meterables; i++ )
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
2055 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
2056 meter[i] = 0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
2057 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
2058 #endif /* MMAP_METERING */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
2059 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
2060
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
2061 void
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
2062 syms_of_ralloc (void)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
2063 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
2064 #ifdef MMAP_METERING
563
183866b06e0b [xemacs-hg @ 2001-05-24 07:50:48 by ben]
ben
parents: 458
diff changeset
2065 DEFSYMBOL (Qmmap_times_mapped);
183866b06e0b [xemacs-hg @ 2001-05-24 07:50:48 by ben]
ben
parents: 458
diff changeset
2066 DEFSYMBOL (Qmmap_pages_mapped);
183866b06e0b [xemacs-hg @ 2001-05-24 07:50:48 by ben]
ben
parents: 458
diff changeset
2067 DEFSYMBOL (Qmmap_times_unmapped);
183866b06e0b [xemacs-hg @ 2001-05-24 07:50:48 by ben]
ben
parents: 458
diff changeset
2068 DEFSYMBOL (Qmmap_times_remapped);
183866b06e0b [xemacs-hg @ 2001-05-24 07:50:48 by ben]
ben
parents: 458
diff changeset
2069 DEFSYMBOL (Qmmap_didnt_copy);
183866b06e0b [xemacs-hg @ 2001-05-24 07:50:48 by ben]
ben
parents: 458
diff changeset
2070 DEFSYMBOL (Qmmap_pages_copied);
183866b06e0b [xemacs-hg @ 2001-05-24 07:50:48 by ben]
ben
parents: 458
diff changeset
2071 DEFSYMBOL (Qmmap_average_bumpval);
183866b06e0b [xemacs-hg @ 2001-05-24 07:50:48 by ben]
ben
parents: 458
diff changeset
2072 DEFSYMBOL (Qmmap_wastage);
183866b06e0b [xemacs-hg @ 2001-05-24 07:50:48 by ben]
ben
parents: 458
diff changeset
2073 DEFSYMBOL (Qmmap_live_pages);
183866b06e0b [xemacs-hg @ 2001-05-24 07:50:48 by ben]
ben
parents: 458
diff changeset
2074 DEFSYMBOL (Qmmap_addr_looked_up);
183866b06e0b [xemacs-hg @ 2001-05-24 07:50:48 by ben]
ben
parents: 458
diff changeset
2075 DEFSYMBOL (Qmmap_hash_worked);
183866b06e0b [xemacs-hg @ 2001-05-24 07:50:48 by ben]
ben
parents: 458
diff changeset
2076 DEFSYMBOL (Qmmap_addrlist_size);
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
2077 DEFSUBR (Fmmap_allocator_status);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
2078 #endif /* MMAP_METERING */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
2079 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
2080
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
2081 void
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
2082 vars_of_ralloc (void)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
2083 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
2084 DEFVAR_INT ("mmap-hysteresis", &mmap_hysteresis /*
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
2085 Extra room left at the end of an allocated arena,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
2086 so that a re-alloc requesting extra space smaller than this
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
2087 does not actually cause a new arena to be allocated.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
2088
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
2089 A negative value is considered equal to zero. This is the
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
2090 minimum amount of space guaranteed to be left at the end of
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
2091 the arena. Because allocation happens in multiples of the OS
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
2092 page size, it is possible for more space to be left unused.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
2093 */ );
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
2094 mmap_hysteresis = 0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
2095 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
2096
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
2097 #endif /* HAVE_MMAP */