aboutsummaryrefslogtreecommitdiffstats
path: root/src/ralloc.c
diff options
context:
space:
mode:
authorStephen Berman2013-06-14 22:07:55 +0200
committerStephen Berman2013-06-14 22:07:55 +0200
commitbd358779861f265a7acff31ead40172735af693e (patch)
tree345217a9889dbd29b09bdc80a94265c17719d41f /src/ralloc.c
parent2a97b47f0878cbda86cb6ba0e7e744924810b70e (diff)
parentf7394b12358ae453a0c8b85fc307afc1b740010d (diff)
downloademacs-bd358779861f265a7acff31ead40172735af693e.tar.gz
emacs-bd358779861f265a7acff31ead40172735af693e.zip
Merge from trunk.
Diffstat (limited to 'src/ralloc.c')
-rw-r--r--src/ralloc.c251
1 files changed, 127 insertions, 124 deletions
diff --git a/src/ralloc.c b/src/ralloc.c
index 74834333aa3..13fd65cbb0c 100644
--- a/src/ralloc.c
+++ b/src/ralloc.c
@@ -1,5 +1,5 @@
1/* Block-relocating memory allocator. 1/* Block-relocating memory allocator.
2 Copyright (C) 1993, 1995, 2000-2012 Free Software Foundation, Inc. 2 Copyright (C) 1993, 1995, 2000-2013 Free Software Foundation, Inc.
3 3
4This file is part of GNU Emacs. 4This file is part of GNU Emacs.
5 5
@@ -25,7 +25,7 @@ along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
25#ifdef emacs 25#ifdef emacs
26 26
27#include <config.h> 27#include <config.h>
28#include <setjmp.h> 28
29#include "lisp.h" /* Needed for VALBITS. */ 29#include "lisp.h" /* Needed for VALBITS. */
30#include "blockinput.h" 30#include "blockinput.h"
31 31
@@ -52,10 +52,6 @@ extern size_t __malloc_extra_blocks;
52 52
53#include "getpagesize.h" 53#include "getpagesize.h"
54 54
55typedef size_t SIZE;
56typedef void *POINTER;
57#define NIL ((POINTER) 0)
58
59/* A flag to indicate whether we have initialized ralloc yet. For 55/* A flag to indicate whether we have initialized ralloc yet. For
60 Emacs's sake, please do not make this local to malloc_init; on some 56 Emacs's sake, please do not make this local to malloc_init; on some
61 machines, the dumping procedure makes all static variables 57 machines, the dumping procedure makes all static variables
@@ -72,14 +68,14 @@ static void r_alloc_init (void);
72/* Declarations for working with the malloc, ralloc, and system breaks. */ 68/* Declarations for working with the malloc, ralloc, and system breaks. */
73 69
74/* Function to set the real break value. */ 70/* Function to set the real break value. */
75POINTER (*real_morecore) (long int); 71void *(*real_morecore) (ptrdiff_t);
76 72
77/* The break value, as seen by malloc. */ 73/* The break value, as seen by malloc. */
78static POINTER virtual_break_value; 74static void *virtual_break_value;
79 75
80/* The address of the end of the last data in use by ralloc, 76/* The address of the end of the last data in use by ralloc,
81 including relocatable blocs as well as malloc data. */ 77 including relocatable blocs as well as malloc data. */
82static POINTER break_value; 78static void *break_value;
83 79
84/* This is the size of a page. We round memory requests to this boundary. */ 80/* This is the size of a page. We round memory requests to this boundary. */
85static int page_size; 81static int page_size;
@@ -91,18 +87,18 @@ static int extra_bytes;
91/* Macros for rounding. Note that rounding to any value is possible 87/* Macros for rounding. Note that rounding to any value is possible
92 by changing the definition of PAGE. */ 88 by changing the definition of PAGE. */
93#define PAGE (getpagesize ()) 89#define PAGE (getpagesize ())
94#define ROUNDUP(size) (((unsigned long int) (size) + page_size - 1) \ 90#define ROUNDUP(size) (((size_t) (size) + page_size - 1) \
95 & ~(page_size - 1)) 91 & ~((size_t) (page_size - 1)))
96 92
97#define MEM_ALIGN sizeof (double) 93#define MEM_ALIGN sizeof (double)
98#define MEM_ROUNDUP(addr) (((unsigned long int)(addr) + MEM_ALIGN - 1) \ 94#define MEM_ROUNDUP(addr) (((size_t) (addr) + MEM_ALIGN - 1) \
99 & ~(MEM_ALIGN - 1)) 95 & ~(MEM_ALIGN - 1))
100 96
101/* The hook `malloc' uses for the function which gets more space 97/* The hook `malloc' uses for the function which gets more space
102 from the system. */ 98 from the system. */
103 99
104#ifndef SYSTEM_MALLOC 100#ifndef SYSTEM_MALLOC
105extern POINTER (*__morecore) (long int); 101extern void *(*__morecore) (ptrdiff_t);
106#endif 102#endif
107 103
108 104
@@ -131,13 +127,13 @@ typedef struct heap
131 struct heap *next; 127 struct heap *next;
132 struct heap *prev; 128 struct heap *prev;
133 /* Start of memory range of this heap. */ 129 /* Start of memory range of this heap. */
134 POINTER start; 130 void *start;
135 /* End of memory range of this heap. */ 131 /* End of memory range of this heap. */
136 POINTER end; 132 void *end;
137 /* Start of relocatable data in this heap. */ 133 /* Start of relocatable data in this heap. */
138 POINTER bloc_start; 134 void *bloc_start;
139 /* Start of unused space in this heap. */ 135 /* Start of unused space in this heap. */
140 POINTER free; 136 void *free;
141 /* First bloc in this heap. */ 137 /* First bloc in this heap. */
142 struct bp *first_bloc; 138 struct bp *first_bloc;
143 /* Last bloc in this heap. */ 139 /* Last bloc in this heap. */
@@ -159,7 +155,7 @@ static heap_ptr first_heap, last_heap;
159 The data blocks abut each other; if b->next is non-nil, then 155 The data blocks abut each other; if b->next is non-nil, then
160 b->data + b->size == b->next->data. 156 b->data + b->size == b->next->data.
161 157
162 An element with variable==NIL denotes a freed block, which has not yet 158 An element with variable==NULL denotes a freed block, which has not yet
163 been collected. They may only appear while r_alloc_freeze_level > 0, 159 been collected. They may only appear while r_alloc_freeze_level > 0,
164 and will be freed when the arena is thawed. Currently, these blocs are 160 and will be freed when the arena is thawed. Currently, these blocs are
165 not reusable, while the arena is frozen. Very inefficient. */ 161 not reusable, while the arena is frozen. Very inefficient. */
@@ -168,10 +164,10 @@ typedef struct bp
168{ 164{
169 struct bp *next; 165 struct bp *next;
170 struct bp *prev; 166 struct bp *prev;
171 POINTER *variable; 167 void **variable;
172 POINTER data; 168 void *data;
173 SIZE size; 169 size_t size;
174 POINTER new_data; /* temporarily used for relocation */ 170 void *new_data; /* temporarily used for relocation */
175 struct heap *heap; /* Heap this bloc is in. */ 171 struct heap *heap; /* Heap this bloc is in. */
176} *bloc_ptr; 172} *bloc_ptr;
177 173
@@ -192,7 +188,7 @@ static int r_alloc_freeze_level;
192/* Find the heap that ADDRESS falls within. */ 188/* Find the heap that ADDRESS falls within. */
193 189
194static heap_ptr 190static heap_ptr
195find_heap (POINTER address) 191find_heap (void *address)
196{ 192{
197 heap_ptr heap; 193 heap_ptr heap;
198 194
@@ -223,11 +219,11 @@ find_heap (POINTER address)
223 Return the address of the space if all went well, or zero if we couldn't 219 Return the address of the space if all went well, or zero if we couldn't
224 allocate the memory. */ 220 allocate the memory. */
225 221
226static POINTER 222static void *
227obtain (POINTER address, SIZE size) 223obtain (void *address, size_t size)
228{ 224{
229 heap_ptr heap; 225 heap_ptr heap;
230 SIZE already_available; 226 size_t already_available;
231 227
232 /* Find the heap that ADDRESS falls within. */ 228 /* Find the heap that ADDRESS falls within. */
233 for (heap = last_heap; heap; heap = heap->prev) 229 for (heap = last_heap; heap; heap = heap->prev)
@@ -253,19 +249,19 @@ obtain (POINTER address, SIZE size)
253 get more space. */ 249 get more space. */
254 if (heap == NIL_HEAP) 250 if (heap == NIL_HEAP)
255 { 251 {
256 POINTER new = (*real_morecore)(0); 252 void *new = real_morecore (0);
257 SIZE get; 253 size_t get;
258 254
259 already_available = (char *)last_heap->end - (char *)address; 255 already_available = (char *) last_heap->end - (char *) address;
260 256
261 if (new != last_heap->end) 257 if (new != last_heap->end)
262 { 258 {
263 /* Someone else called sbrk. Make a new heap. */ 259 /* Someone else called sbrk. Make a new heap. */
264 260
265 heap_ptr new_heap = (heap_ptr) MEM_ROUNDUP (new); 261 heap_ptr new_heap = (heap_ptr) MEM_ROUNDUP (new);
266 POINTER bloc_start = (POINTER) MEM_ROUNDUP ((POINTER)(new_heap + 1)); 262 void *bloc_start = (void *) MEM_ROUNDUP ((void *) (new_heap + 1));
267 263
268 if ((*real_morecore) ((char *) bloc_start - (char *) new) != new) 264 if (real_morecore ((char *) bloc_start - (char *) new) != new)
269 return 0; 265 return 0;
270 266
271 new_heap->start = new; 267 new_heap->start = new;
@@ -287,10 +283,10 @@ obtain (POINTER address, SIZE size)
287 Get some extra, so we can come here less often. */ 283 Get some extra, so we can come here less often. */
288 284
289 get = size + extra_bytes - already_available; 285 get = size + extra_bytes - already_available;
290 get = (char *) ROUNDUP ((char *)last_heap->end + get) 286 get = (char *) ROUNDUP ((char *) last_heap->end + get)
291 - (char *) last_heap->end; 287 - (char *) last_heap->end;
292 288
293 if ((*real_morecore) (get) != last_heap->end) 289 if (real_morecore (get) != last_heap->end)
294 return 0; 290 return 0;
295 291
296 last_heap->end = (char *) last_heap->end + get; 292 last_heap->end = (char *) last_heap->end + get;
@@ -308,7 +304,7 @@ static void
308relinquish (void) 304relinquish (void)
309{ 305{
310 register heap_ptr h; 306 register heap_ptr h;
311 long excess = 0; 307 ptrdiff_t excess = 0;
312 308
313 /* Add the amount of space beyond break_value 309 /* Add the amount of space beyond break_value
314 in all heaps which have extend beyond break_value at all. */ 310 in all heaps which have extend beyond break_value at all. */
@@ -319,43 +315,44 @@ relinquish (void)
319 ? h->bloc_start : break_value); 315 ? h->bloc_start : break_value);
320 } 316 }
321 317
322 if (excess > extra_bytes * 2 && (*real_morecore) (0) == last_heap->end) 318 if (excess > extra_bytes * 2 && real_morecore (0) == last_heap->end)
323 { 319 {
324 /* Keep extra_bytes worth of empty space. 320 /* Keep extra_bytes worth of empty space.
325 And don't free anything unless we can free at least extra_bytes. */ 321 And don't free anything unless we can free at least extra_bytes. */
326 excess -= extra_bytes; 322 excess -= extra_bytes;
327 323
328 if ((char *)last_heap->end - (char *)last_heap->bloc_start <= excess) 324 if ((char *) last_heap->end - (char *) last_heap->bloc_start <= excess)
329 { 325 {
330 /* This heap should have no blocs in it. */ 326 heap_ptr lh_prev;
327
328 /* This heap should have no blocs in it. If it does, we
329 cannot return it to the system. */
331 if (last_heap->first_bloc != NIL_BLOC 330 if (last_heap->first_bloc != NIL_BLOC
332 || last_heap->last_bloc != NIL_BLOC) 331 || last_heap->last_bloc != NIL_BLOC)
333 emacs_abort (); 332 return;
334 333
335 /* Return the last heap, with its header, to the system. */ 334 /* Return the last heap, with its header, to the system. */
336 excess = (char *)last_heap->end - (char *)last_heap->start; 335 excess = (char *) last_heap->end - (char *) last_heap->start;
337 last_heap = last_heap->prev; 336 lh_prev = last_heap->prev;
338 last_heap->next = NIL_HEAP; 337 /* If the system doesn't want that much memory back, leave
338 last_heap unaltered to reflect that. This can occur if
339 break_value is still within the original data segment. */
340 if (real_morecore (- excess) != 0)
341 {
342 last_heap = lh_prev;
343 last_heap->next = NIL_HEAP;
344 }
339 } 345 }
340 else 346 else
341 { 347 {
342 excess = (char *) last_heap->end 348 excess = ((char *) last_heap->end
343 - (char *) ROUNDUP ((char *)last_heap->end - excess); 349 - (char *) ROUNDUP ((char *) last_heap->end - excess));
344 last_heap->end = (char *) last_heap->end - excess; 350 /* If the system doesn't want that much memory back, leave
345 } 351 the end of the last heap unchanged to reflect that. This
346 352 can occur if break_value is still within the original
347 if ((*real_morecore) (- excess) == 0) 353 data segment. */
348 { 354 if (real_morecore (- excess) != 0)
349 /* If the system didn't want that much memory back, adjust 355 last_heap->end = (char *) last_heap->end - excess;
350 the end of the last heap to reflect that. This can occur
351 if break_value is still within the original data segment. */
352 last_heap->end = (char *) last_heap->end + excess;
353 /* Make sure that the result of the adjustment is accurate.
354 It should be, for the else clause above; the other case,
355 which returns the entire last heap to the system, seems
356 unlikely to trigger this mode of failure. */
357 if (last_heap->end != (*real_morecore) (0))
358 emacs_abort ();
359 } 356 }
360 } 357 }
361} 358}
@@ -366,9 +363,9 @@ relinquish (void)
366 to that block. */ 363 to that block. */
367 364
368static bloc_ptr 365static bloc_ptr
369find_bloc (POINTER *ptr) 366find_bloc (void **ptr)
370{ 367{
371 register bloc_ptr p = first_bloc; 368 bloc_ptr p = first_bloc;
372 369
373 while (p != NIL_BLOC) 370 while (p != NIL_BLOC)
374 { 371 {
@@ -391,10 +388,10 @@ find_bloc (POINTER *ptr)
391 memory for the new block. */ 388 memory for the new block. */
392 389
393static bloc_ptr 390static bloc_ptr
394get_bloc (SIZE size) 391get_bloc (size_t size)
395{ 392{
396 register bloc_ptr new_bloc; 393 bloc_ptr new_bloc;
397 register heap_ptr heap; 394 heap_ptr heap;
398 395
399 if (! (new_bloc = malloc (BLOC_PTR_SIZE)) 396 if (! (new_bloc = malloc (BLOC_PTR_SIZE))
400 || ! (new_bloc->data = obtain (break_value, size))) 397 || ! (new_bloc->data = obtain (break_value, size)))
@@ -408,7 +405,7 @@ get_bloc (SIZE size)
408 405
409 new_bloc->size = size; 406 new_bloc->size = size;
410 new_bloc->next = NIL_BLOC; 407 new_bloc->next = NIL_BLOC;
411 new_bloc->variable = (POINTER *) NIL; 408 new_bloc->variable = NULL;
412 new_bloc->new_data = 0; 409 new_bloc->new_data = 0;
413 410
414 /* Record in the heap that this space is in use. */ 411 /* Record in the heap that this space is in use. */
@@ -446,9 +443,9 @@ get_bloc (SIZE size)
446 Do not touch the contents of blocs or break_value. */ 443 Do not touch the contents of blocs or break_value. */
447 444
448static int 445static int
449relocate_blocs (bloc_ptr bloc, heap_ptr heap, POINTER address) 446relocate_blocs (bloc_ptr bloc, heap_ptr heap, void *address)
450{ 447{
451 register bloc_ptr b = bloc; 448 bloc_ptr b = bloc;
452 449
453 /* No need to ever call this if arena is frozen, bug somewhere! */ 450 /* No need to ever call this if arena is frozen, bug somewhere! */
454 if (r_alloc_freeze_level) 451 if (r_alloc_freeze_level)
@@ -470,8 +467,8 @@ relocate_blocs (bloc_ptr bloc, heap_ptr heap, POINTER address)
470 get enough new space to hold BLOC and all following blocs. */ 467 get enough new space to hold BLOC and all following blocs. */
471 if (heap == NIL_HEAP) 468 if (heap == NIL_HEAP)
472 { 469 {
473 register bloc_ptr tb = b; 470 bloc_ptr tb = b;
474 register SIZE s = 0; 471 size_t s = 0;
475 472
476 /* Add up the size of all the following blocs. */ 473 /* Add up the size of all the following blocs. */
477 while (tb != NIL_BLOC) 474 while (tb != NIL_BLOC)
@@ -567,12 +564,12 @@ update_heap_bloc_correspondence (bloc_ptr bloc, heap_ptr heap)
567 that come after BLOC in memory. */ 564 that come after BLOC in memory. */
568 565
569static int 566static int
570resize_bloc (bloc_ptr bloc, SIZE size) 567resize_bloc (bloc_ptr bloc, size_t size)
571{ 568{
572 register bloc_ptr b; 569 bloc_ptr b;
573 heap_ptr heap; 570 heap_ptr heap;
574 POINTER address; 571 void *address;
575 SIZE old_size; 572 size_t old_size;
576 573
577 /* No need to ever call this if arena is frozen, bug somewhere! */ 574 /* No need to ever call this if arena is frozen, bug somewhere! */
578 if (r_alloc_freeze_level) 575 if (r_alloc_freeze_level)
@@ -674,7 +671,7 @@ free_bloc (bloc_ptr bloc)
674 671
675 if (r_alloc_freeze_level) 672 if (r_alloc_freeze_level)
676 { 673 {
677 bloc->variable = (POINTER *) NIL; 674 bloc->variable = NULL;
678 return; 675 return;
679 } 676 }
680 677
@@ -751,17 +748,17 @@ free_bloc (bloc_ptr bloc)
751 __morecore hook values - in particular, __default_morecore in the 748 __morecore hook values - in particular, __default_morecore in the
752 GNU malloc package. */ 749 GNU malloc package. */
753 750
754static POINTER 751static void *
755r_alloc_sbrk (long int size) 752r_alloc_sbrk (ptrdiff_t size)
756{ 753{
757 register bloc_ptr b; 754 bloc_ptr b;
758 POINTER address; 755 void *address;
759 756
760 if (! r_alloc_initialized) 757 if (! r_alloc_initialized)
761 r_alloc_init (); 758 r_alloc_init ();
762 759
763 if (use_relocatable_buffers <= 0) 760 if (use_relocatable_buffers <= 0)
764 return (*real_morecore) (size); 761 return real_morecore (size);
765 762
766 if (size == 0) 763 if (size == 0)
767 return virtual_break_value; 764 return virtual_break_value;
@@ -771,19 +768,19 @@ r_alloc_sbrk (long int size)
771 /* Allocate a page-aligned space. GNU malloc would reclaim an 768 /* Allocate a page-aligned space. GNU malloc would reclaim an
772 extra space if we passed an unaligned one. But we could 769 extra space if we passed an unaligned one. But we could
773 not always find a space which is contiguous to the previous. */ 770 not always find a space which is contiguous to the previous. */
774 POINTER new_bloc_start; 771 void *new_bloc_start;
775 heap_ptr h = first_heap; 772 heap_ptr h = first_heap;
776 SIZE get = ROUNDUP (size); 773 size_t get = ROUNDUP (size);
777 774
778 address = (POINTER) ROUNDUP (virtual_break_value); 775 address = (void *) ROUNDUP (virtual_break_value);
779 776
780 /* Search the list upward for a heap which is large enough. */ 777 /* Search the list upward for a heap which is large enough. */
781 while ((char *) h->end < (char *) MEM_ROUNDUP ((char *)address + get)) 778 while ((char *) h->end < (char *) MEM_ROUNDUP ((char *) address + get))
782 { 779 {
783 h = h->next; 780 h = h->next;
784 if (h == NIL_HEAP) 781 if (h == NIL_HEAP)
785 break; 782 break;
786 address = (POINTER) ROUNDUP (h->start); 783 address = (void *) ROUNDUP (h->start);
787 } 784 }
788 785
789 /* If not found, obtain more space. */ 786 /* If not found, obtain more space. */
@@ -795,19 +792,19 @@ r_alloc_sbrk (long int size)
795 return 0; 792 return 0;
796 793
797 if (first_heap == last_heap) 794 if (first_heap == last_heap)
798 address = (POINTER) ROUNDUP (virtual_break_value); 795 address = (void *) ROUNDUP (virtual_break_value);
799 else 796 else
800 address = (POINTER) ROUNDUP (last_heap->start); 797 address = (void *) ROUNDUP (last_heap->start);
801 h = last_heap; 798 h = last_heap;
802 } 799 }
803 800
804 new_bloc_start = (POINTER) MEM_ROUNDUP ((char *)address + get); 801 new_bloc_start = (void *) MEM_ROUNDUP ((char *) address + get);
805 802
806 if (first_heap->bloc_start < new_bloc_start) 803 if (first_heap->bloc_start < new_bloc_start)
807 { 804 {
808 /* This is no clean solution - no idea how to do it better. */ 805 /* This is no clean solution - no idea how to do it better. */
809 if (r_alloc_freeze_level) 806 if (r_alloc_freeze_level)
810 return NIL; 807 return NULL;
811 808
812 /* There is a bug here: if the above obtain call succeeded, but the 809 /* There is a bug here: if the above obtain call succeeded, but the
813 relocate_blocs call below does not succeed, we need to free 810 relocate_blocs call below does not succeed, we need to free
@@ -817,7 +814,7 @@ r_alloc_sbrk (long int size)
817 if (! relocate_blocs (first_bloc, h, new_bloc_start)) 814 if (! relocate_blocs (first_bloc, h, new_bloc_start))
818 return 0; 815 return 0;
819 816
820 /* Note that (POINTER)(h+1) <= new_bloc_start since 817 /* Note that (char *) (h + 1) <= (char *) new_bloc_start since
821 get >= page_size, so the following does not destroy the heap 818 get >= page_size, so the following does not destroy the heap
822 header. */ 819 header. */
823 for (b = last_bloc; b != NIL_BLOC; b = b->prev) 820 for (b = last_bloc; b != NIL_BLOC; b = b->prev)
@@ -854,8 +851,8 @@ r_alloc_sbrk (long int size)
854 } 851 }
855 else /* size < 0 */ 852 else /* size < 0 */
856 { 853 {
857 SIZE excess = (char *)first_heap->bloc_start 854 size_t excess = ((char *) first_heap->bloc_start
858 - ((char *)virtual_break_value + size); 855 - ((char *) virtual_break_value + size));
859 856
860 address = virtual_break_value; 857 address = virtual_break_value;
861 858
@@ -863,7 +860,7 @@ r_alloc_sbrk (long int size)
863 { 860 {
864 excess -= extra_bytes; 861 excess -= extra_bytes;
865 first_heap->bloc_start 862 first_heap->bloc_start
866 = (POINTER) MEM_ROUNDUP ((char *)first_heap->bloc_start - excess); 863 = (void *) MEM_ROUNDUP ((char *) first_heap->bloc_start - excess);
867 864
868 relocate_blocs (first_bloc, first_heap, first_heap->bloc_start); 865 relocate_blocs (first_bloc, first_heap, first_heap->bloc_start);
869 866
@@ -875,14 +872,14 @@ r_alloc_sbrk (long int size)
875 } 872 }
876 } 873 }
877 874
878 if ((char *)virtual_break_value + size < (char *)first_heap->start) 875 if ((char *) virtual_break_value + size < (char *) first_heap->start)
879 { 876 {
880 /* We found an additional space below the first heap */ 877 /* We found an additional space below the first heap */
881 first_heap->start = (POINTER) ((char *)virtual_break_value + size); 878 first_heap->start = (void *) ((char *) virtual_break_value + size);
882 } 879 }
883 } 880 }
884 881
885 virtual_break_value = (POINTER) ((char *)address + size); 882 virtual_break_value = (void *) ((char *) address + size);
886 break_value = (last_bloc 883 break_value = (last_bloc
887 ? (char *) last_bloc->data + last_bloc->size 884 ? (char *) last_bloc->data + last_bloc->size
888 : (char *) first_heap->bloc_start); 885 : (char *) first_heap->bloc_start);
@@ -904,10 +901,10 @@ r_alloc_sbrk (long int size)
904 If we can't allocate the necessary memory, set *PTR to zero, and 901 If we can't allocate the necessary memory, set *PTR to zero, and
905 return zero. */ 902 return zero. */
906 903
907POINTER 904void *
908r_alloc (POINTER *ptr, SIZE size) 905r_alloc (void **ptr, size_t size)
909{ 906{
910 register bloc_ptr new_bloc; 907 bloc_ptr new_bloc;
911 908
912 if (! r_alloc_initialized) 909 if (! r_alloc_initialized)
913 r_alloc_init (); 910 r_alloc_init ();
@@ -928,9 +925,9 @@ r_alloc (POINTER *ptr, SIZE size)
928 Store 0 in *PTR to show there's no block allocated. */ 925 Store 0 in *PTR to show there's no block allocated. */
929 926
930void 927void
931r_alloc_free (register POINTER *ptr) 928r_alloc_free (void **ptr)
932{ 929{
933 register bloc_ptr dead_bloc; 930 bloc_ptr dead_bloc;
934 931
935 if (! r_alloc_initialized) 932 if (! r_alloc_initialized)
936 r_alloc_init (); 933 r_alloc_init ();
@@ -961,10 +958,10 @@ r_alloc_free (register POINTER *ptr)
961 If more memory cannot be allocated, then leave *PTR unchanged, and 958 If more memory cannot be allocated, then leave *PTR unchanged, and
962 return zero. */ 959 return zero. */
963 960
964POINTER 961void *
965r_re_alloc (POINTER *ptr, SIZE size) 962r_re_alloc (void **ptr, size_t size)
966{ 963{
967 register bloc_ptr bloc; 964 bloc_ptr bloc;
968 965
969 if (! r_alloc_initialized) 966 if (! r_alloc_initialized)
970 r_alloc_init (); 967 r_alloc_init ();
@@ -1003,15 +1000,15 @@ r_re_alloc (POINTER *ptr, SIZE size)
1003 { 1000 {
1004 new_bloc->variable = ptr; 1001 new_bloc->variable = ptr;
1005 *ptr = new_bloc->data; 1002 *ptr = new_bloc->data;
1006 bloc->variable = (POINTER *) NIL; 1003 bloc->variable = NULL;
1007 } 1004 }
1008 else 1005 else
1009 return NIL; 1006 return NULL;
1010 } 1007 }
1011 else 1008 else
1012 { 1009 {
1013 if (! resize_bloc (bloc, MEM_ROUNDUP (size))) 1010 if (! resize_bloc (bloc, MEM_ROUNDUP (size)))
1014 return NIL; 1011 return NULL;
1015 } 1012 }
1016 } 1013 }
1017 return *ptr; 1014 return *ptr;
@@ -1051,27 +1048,27 @@ r_alloc_check (void)
1051 return; 1048 return;
1052 1049
1053 assert (first_heap); 1050 assert (first_heap);
1054 assert (last_heap->end <= (POINTER) sbrk (0)); 1051 assert (last_heap->end <= (void *) sbrk (0));
1055 assert ((POINTER) first_heap < first_heap->start); 1052 assert ((void *) first_heap < first_heap->start);
1056 assert (first_heap->start <= virtual_break_value); 1053 assert (first_heap->start <= virtual_break_value);
1057 assert (virtual_break_value <= first_heap->end); 1054 assert (virtual_break_value <= first_heap->end);
1058 1055
1059 for (h = first_heap; h; h = h->next) 1056 for (h = first_heap; h; h = h->next)
1060 { 1057 {
1061 assert (h->prev == ph); 1058 assert (h->prev == ph);
1062 assert ((POINTER) ROUNDUP (h->end) == h->end); 1059 assert ((void *) ROUNDUP (h->end) == h->end);
1063#if 0 /* ??? The code in ralloc.c does not really try to ensure 1060#if 0 /* ??? The code in ralloc.c does not really try to ensure
1064 the heap start has any sort of alignment. 1061 the heap start has any sort of alignment.
1065 Perhaps it should. */ 1062 Perhaps it should. */
1066 assert ((POINTER) MEM_ROUNDUP (h->start) == h->start); 1063 assert ((void *) MEM_ROUNDUP (h->start) == h->start);
1067#endif 1064#endif
1068 assert ((POINTER) MEM_ROUNDUP (h->bloc_start) == h->bloc_start); 1065 assert ((void *) MEM_ROUNDUP (h->bloc_start) == h->bloc_start);
1069 assert (h->start <= h->bloc_start && h->bloc_start <= h->end); 1066 assert (h->start <= h->bloc_start && h->bloc_start <= h->end);
1070 1067
1071 if (ph) 1068 if (ph)
1072 { 1069 {
1073 assert (ph->end < h->start); 1070 assert (ph->end < h->start);
1074 assert (h->start <= (POINTER)h && (POINTER)(h+1) <= h->bloc_start); 1071 assert (h->start <= (void *) h && (void *) (h + 1) <= h->bloc_start);
1075 } 1072 }
1076 1073
1077 if (h->bloc_start <= break_value && break_value <= h->end) 1074 if (h->bloc_start <= break_value && break_value <= h->end)
@@ -1086,8 +1083,8 @@ r_alloc_check (void)
1086 for (b = first_bloc; b; b = b->next) 1083 for (b = first_bloc; b; b = b->next)
1087 { 1084 {
1088 assert (b->prev == pb); 1085 assert (b->prev == pb);
1089 assert ((POINTER) MEM_ROUNDUP (b->data) == b->data); 1086 assert ((void *) MEM_ROUNDUP (b->data) == b->data);
1090 assert ((SIZE) MEM_ROUNDUP (b->size) == b->size); 1087 assert ((size_t) MEM_ROUNDUP (b->size) == b->size);
1091 1088
1092 ph = 0; 1089 ph = 0;
1093 for (h = first_heap; h; h = h->next) 1090 for (h = first_heap; h; h = h->next)
@@ -1136,7 +1133,7 @@ r_alloc_check (void)
1136 is checked to ensure that memory corruption does not occur due to 1133 is checked to ensure that memory corruption does not occur due to
1137 misuse. */ 1134 misuse. */
1138void 1135void
1139r_alloc_reset_variable (POINTER *old, POINTER *new) 1136r_alloc_reset_variable (void **old, void **new)
1140{ 1137{
1141 bloc_ptr bloc = first_bloc; 1138 bloc_ptr bloc = first_bloc;
1142 1139
@@ -1191,27 +1188,33 @@ r_alloc_init (void)
1191 first_heap = last_heap = &heap_base; 1188 first_heap = last_heap = &heap_base;
1192 first_heap->next = first_heap->prev = NIL_HEAP; 1189 first_heap->next = first_heap->prev = NIL_HEAP;
1193 first_heap->start = first_heap->bloc_start 1190 first_heap->start = first_heap->bloc_start
1194 = virtual_break_value = break_value = (*real_morecore) (0); 1191 = virtual_break_value = break_value = real_morecore (0);
1195 if (break_value == NIL) 1192 if (break_value == NULL)
1196 emacs_abort (); 1193 emacs_abort ();
1197 1194
1198 extra_bytes = ROUNDUP (50000); 1195 extra_bytes = ROUNDUP (50000);
1199#endif 1196#endif
1200 1197
1201#ifdef DOUG_LEA_MALLOC 1198#ifdef DOUG_LEA_MALLOC
1202 BLOCK_INPUT; 1199 block_input ();
1203 mallopt (M_TOP_PAD, 64 * 4096); 1200 mallopt (M_TOP_PAD, 64 * 4096);
1204 UNBLOCK_INPUT; 1201 unblock_input ();
1205#else 1202#else
1206#ifndef SYSTEM_MALLOC 1203#ifndef SYSTEM_MALLOC
1207 /* Give GNU malloc's morecore some hysteresis 1204 /* Give GNU malloc's morecore some hysteresis so that we move all
1208 so that we move all the relocatable blocks much less often. */ 1205 the relocatable blocks much less often. The number used to be
1209 __malloc_extra_blocks = 64; 1206 64, but alloc.c would override that with 32 in code that was
1207 removed when SYNC_INPUT became the only input handling mode.
1208 That code was conditioned on !DOUG_LEA_MALLOC, so the call to
1209 mallopt above is left unchanged. (Actually, I think there's no
1210 system nowadays that uses DOUG_LEA_MALLOC and also uses
1211 REL_ALLOC.) */
1212 __malloc_extra_blocks = 32;
1210#endif 1213#endif
1211#endif 1214#endif
1212 1215
1213#ifndef SYSTEM_MALLOC 1216#ifndef SYSTEM_MALLOC
1214 first_heap->end = (POINTER) ROUNDUP (first_heap->start); 1217 first_heap->end = (void *) ROUNDUP (first_heap->start);
1215 1218
1216 /* The extra call to real_morecore guarantees that the end of the 1219 /* The extra call to real_morecore guarantees that the end of the
1217 address space is a multiple of page_size, even if page_size is 1220 address space is a multiple of page_size, even if page_size is
@@ -1219,7 +1222,7 @@ r_alloc_init (void)
1219 which page_size is stored. This allows a binary to be built on a 1222 which page_size is stored. This allows a binary to be built on a
1220 system with one page size and run on a system with a smaller page 1223 system with one page size and run on a system with a smaller page
1221 size. */ 1224 size. */
1222 (*real_morecore) ((char *) first_heap->end - (char *) first_heap->start); 1225 real_morecore ((char *) first_heap->end - (char *) first_heap->start);
1223 1226
1224 /* Clear the rest of the last page; this memory is in our address space 1227 /* Clear the rest of the last page; this memory is in our address space
1225 even though it is after the sbrk value. */ 1228 even though it is after the sbrk value. */