aboutsummaryrefslogtreecommitdiffstats
path: root/src/ralloc.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/ralloc.c')
-rw-r--r--src/ralloc.c125
1 files changed, 33 insertions, 92 deletions
diff --git a/src/ralloc.c b/src/ralloc.c
index 7ccbdc7daf7..5f2b52fcc4b 100644
--- a/src/ralloc.c
+++ b/src/ralloc.c
@@ -37,14 +37,9 @@ along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
37typedef POINTER_TYPE *POINTER; 37typedef POINTER_TYPE *POINTER;
38typedef size_t SIZE; 38typedef size_t SIZE;
39 39
40/* Declared in dispnew.c, this version doesn't screw up if regions
41 overlap. */
42
43extern void safe_bcopy ();
44
45#ifdef DOUG_LEA_MALLOC 40#ifdef DOUG_LEA_MALLOC
46#define M_TOP_PAD -2 41#define M_TOP_PAD -2
47extern int mallopt (); 42extern int mallopt (int, int);
48#else /* not DOUG_LEA_MALLOC */ 43#else /* not DOUG_LEA_MALLOC */
49#ifndef SYSTEM_MALLOC 44#ifndef SYSTEM_MALLOC
50extern size_t __malloc_extra_blocks; 45extern size_t __malloc_extra_blocks;
@@ -61,9 +56,6 @@ typedef void *POINTER;
61#include <unistd.h> 56#include <unistd.h>
62#include <malloc.h> 57#include <malloc.h>
63 58
64#define safe_bcopy(x, y, z) memmove (y, x, z)
65#define bzero(x, len) memset (x, 0, len)
66
67#endif /* not emacs */ 59#endif /* not emacs */
68 60
69 61
@@ -81,13 +73,13 @@ typedef void *POINTER;
81 73
82static int r_alloc_initialized = 0; 74static int r_alloc_initialized = 0;
83 75
84static void r_alloc_init (); 76static void r_alloc_init (void);
85 77
86 78
87/* Declarations for working with the malloc, ralloc, and system breaks. */ 79/* Declarations for working with the malloc, ralloc, and system breaks. */
88 80
89/* Function to set the real break value. */ 81/* Function to set the real break value. */
90POINTER (*real_morecore) (); 82POINTER (*real_morecore) (long int);
91 83
92/* The break value, as seen by malloc. */ 84/* The break value, as seen by malloc. */
93static POINTER virtual_break_value; 85static POINTER virtual_break_value;
@@ -119,7 +111,7 @@ static int extra_bytes;
119 from the system. */ 111 from the system. */
120 112
121#ifndef SYSTEM_MALLOC 113#ifndef SYSTEM_MALLOC
122extern POINTER (*__morecore) (); 114extern POINTER (*__morecore) (long int);
123#endif 115#endif
124 116
125 117
@@ -210,8 +202,7 @@ static int r_alloc_freeze_level;
210/* Find the heap that ADDRESS falls within. */ 202/* Find the heap that ADDRESS falls within. */
211 203
212static heap_ptr 204static heap_ptr
213find_heap (address) 205find_heap (POINTER address)
214 POINTER address;
215{ 206{
216 heap_ptr heap; 207 heap_ptr heap;
217 208
@@ -243,9 +234,7 @@ find_heap (address)
243 allocate the memory. */ 234 allocate the memory. */
244 235
245static POINTER 236static POINTER
246obtain (address, size) 237obtain (POINTER address, SIZE size)
247 POINTER address;
248 SIZE size;
249{ 238{
250 heap_ptr heap; 239 heap_ptr heap;
251 SIZE already_available; 240 SIZE already_available;
@@ -326,7 +315,7 @@ obtain (address, size)
326 it can also eliminate the last heap entirely. */ 315 it can also eliminate the last heap entirely. */
327 316
328static void 317static void
329relinquish () 318relinquish (void)
330{ 319{
331 register heap_ptr h; 320 register heap_ptr h;
332 long excess = 0; 321 long excess = 0;
@@ -385,7 +374,7 @@ relinquish ()
385 above where malloc gets space. */ 374 above where malloc gets space. */
386 375
387long 376long
388r_alloc_size_in_use () 377r_alloc_size_in_use (void)
389{ 378{
390 return (char *) break_value - (char *) virtual_break_value; 379 return (char *) break_value - (char *) virtual_break_value;
391} 380}
@@ -396,8 +385,7 @@ r_alloc_size_in_use ()
396 to that block. */ 385 to that block. */
397 386
398static bloc_ptr 387static bloc_ptr
399find_bloc (ptr) 388find_bloc (POINTER *ptr)
400 POINTER *ptr;
401{ 389{
402 register bloc_ptr p = first_bloc; 390 register bloc_ptr p = first_bloc;
403 391
@@ -422,8 +410,7 @@ find_bloc (ptr)
422 memory for the new block. */ 410 memory for the new block. */
423 411
424static bloc_ptr 412static bloc_ptr
425get_bloc (size) 413get_bloc (SIZE size)
426 SIZE size;
427{ 414{
428 register bloc_ptr new_bloc; 415 register bloc_ptr new_bloc;
429 register heap_ptr heap; 416 register heap_ptr heap;
@@ -478,10 +465,7 @@ get_bloc (size)
478 Do not touch the contents of blocs or break_value. */ 465 Do not touch the contents of blocs or break_value. */
479 466
480static int 467static int
481relocate_blocs (bloc, heap, address) 468relocate_blocs (bloc_ptr bloc, heap_ptr heap, POINTER address)
482 bloc_ptr bloc;
483 heap_ptr heap;
484 POINTER address;
485{ 469{
486 register bloc_ptr b = bloc; 470 register bloc_ptr b = bloc;
487 471
@@ -535,44 +519,12 @@ relocate_blocs (bloc, heap, address)
535 519
536 return 1; 520 return 1;
537} 521}
538
539/* Reorder the bloc BLOC to go before bloc BEFORE in the doubly linked list.
540 This is necessary if we put the memory of space of BLOC
541 before that of BEFORE. */
542
543static void
544reorder_bloc (bloc, before)
545 bloc_ptr bloc, before;
546{
547 bloc_ptr prev, next;
548
549 /* Splice BLOC out from where it is. */
550 prev = bloc->prev;
551 next = bloc->next;
552
553 if (prev)
554 prev->next = next;
555 if (next)
556 next->prev = prev;
557
558 /* Splice it in before BEFORE. */
559 prev = before->prev;
560
561 if (prev)
562 prev->next = bloc;
563 bloc->prev = prev;
564
565 before->prev = bloc;
566 bloc->next = before;
567}
568 522
569/* Update the records of which heaps contain which blocs, starting 523/* Update the records of which heaps contain which blocs, starting
570 with heap HEAP and bloc BLOC. */ 524 with heap HEAP and bloc BLOC. */
571 525
572static void 526static void
573update_heap_bloc_correspondence (bloc, heap) 527update_heap_bloc_correspondence (bloc_ptr bloc, heap_ptr heap)
574 bloc_ptr bloc;
575 heap_ptr heap;
576{ 528{
577 register bloc_ptr b; 529 register bloc_ptr b;
578 530
@@ -634,9 +586,7 @@ update_heap_bloc_correspondence (bloc, heap)
634 that come after BLOC in memory. */ 586 that come after BLOC in memory. */
635 587
636static int 588static int
637resize_bloc (bloc, size) 589resize_bloc (bloc_ptr bloc, SIZE size)
638 bloc_ptr bloc;
639 SIZE size;
640{ 590{
641 register bloc_ptr b; 591 register bloc_ptr b;
642 heap_ptr heap; 592 heap_ptr heap;
@@ -689,7 +639,7 @@ resize_bloc (bloc, size)
689 } 639 }
690 else 640 else
691 { 641 {
692 safe_bcopy (b->data, b->new_data, b->size); 642 memmove (b->new_data, b->data, b->size);
693 *b->variable = b->data = b->new_data; 643 *b->variable = b->data = b->new_data;
694 } 644 }
695 } 645 }
@@ -700,8 +650,8 @@ resize_bloc (bloc, size)
700 } 650 }
701 else 651 else
702 { 652 {
703 safe_bcopy (bloc->data, bloc->new_data, old_size); 653 memmove (bloc->new_data, bloc->data, old_size);
704 bzero ((char *) bloc->new_data + old_size, size - old_size); 654 memset (bloc->new_data + old_size, 0, size - old_size);
705 *bloc->variable = bloc->data = bloc->new_data; 655 *bloc->variable = bloc->data = bloc->new_data;
706 } 656 }
707 } 657 }
@@ -716,7 +666,7 @@ resize_bloc (bloc, size)
716 } 666 }
717 else 667 else
718 { 668 {
719 safe_bcopy (b->data, b->new_data, b->size); 669 memmove (b->new_data, b->data, b->size);
720 *b->variable = b->data = b->new_data; 670 *b->variable = b->data = b->new_data;
721 } 671 }
722 } 672 }
@@ -733,8 +683,7 @@ resize_bloc (bloc, size)
733 This may return space to the system. */ 683 This may return space to the system. */
734 684
735static void 685static void
736free_bloc (bloc) 686free_bloc (bloc_ptr bloc)
737 bloc_ptr bloc;
738{ 687{
739 heap_ptr heap = bloc->heap; 688 heap_ptr heap = bloc->heap;
740 689
@@ -800,8 +749,7 @@ free_bloc (bloc)
800 GNU malloc package. */ 749 GNU malloc package. */
801 750
802POINTER 751POINTER
803r_alloc_sbrk (size) 752r_alloc_sbrk (long int size)
804 long size;
805{ 753{
806 register bloc_ptr b; 754 register bloc_ptr b;
807 POINTER address; 755 POINTER address;
@@ -871,7 +819,7 @@ r_alloc_sbrk (size)
871 header. */ 819 header. */
872 for (b = last_bloc; b != NIL_BLOC; b = b->prev) 820 for (b = last_bloc; b != NIL_BLOC; b = b->prev)
873 { 821 {
874 safe_bcopy (b->data, b->new_data, b->size); 822 memmove (b->new_data, b->data, b->size);
875 *b->variable = b->data = b->new_data; 823 *b->variable = b->data = b->new_data;
876 } 824 }
877 825
@@ -898,7 +846,7 @@ r_alloc_sbrk (size)
898 last_heap = first_heap; 846 last_heap = first_heap;
899 } 847 }
900 848
901 bzero (address, size); 849 memset (address, 0, size);
902 } 850 }
903 else /* size < 0 */ 851 else /* size < 0 */
904 { 852 {
@@ -917,7 +865,7 @@ r_alloc_sbrk (size)
917 865
918 for (b = first_bloc; b != NIL_BLOC; b = b->next) 866 for (b = first_bloc; b != NIL_BLOC; b = b->next)
919 { 867 {
920 safe_bcopy (b->data, b->new_data, b->size); 868 memmove (b->new_data, b->data, b->size);
921 *b->variable = b->data = b->new_data; 869 *b->variable = b->data = b->new_data;
922 } 870 }
923 } 871 }
@@ -952,9 +900,7 @@ r_alloc_sbrk (size)
952 return zero. */ 900 return zero. */
953 901
954POINTER 902POINTER
955r_alloc (ptr, size) 903r_alloc (POINTER *ptr, SIZE size)
956 POINTER *ptr;
957 SIZE size;
958{ 904{
959 register bloc_ptr new_bloc; 905 register bloc_ptr new_bloc;
960 906
@@ -977,8 +923,7 @@ r_alloc (ptr, size)
977 Store 0 in *PTR to show there's no block allocated. */ 923 Store 0 in *PTR to show there's no block allocated. */
978 924
979void 925void
980r_alloc_free (ptr) 926r_alloc_free (register POINTER *ptr)
981 register POINTER *ptr;
982{ 927{
983 register bloc_ptr dead_bloc; 928 register bloc_ptr dead_bloc;
984 929
@@ -1012,9 +957,7 @@ r_alloc_free (ptr)
1012 return zero. */ 957 return zero. */
1013 958
1014POINTER 959POINTER
1015r_re_alloc (ptr, size) 960r_re_alloc (POINTER *ptr, SIZE size)
1016 POINTER *ptr;
1017 SIZE size;
1018{ 961{
1019 register bloc_ptr bloc; 962 register bloc_ptr bloc;
1020 963
@@ -1075,8 +1018,7 @@ r_re_alloc (ptr, size)
1075 malloc must return a null pointer. */ 1018 malloc must return a null pointer. */
1076 1019
1077void 1020void
1078r_alloc_freeze (size) 1021r_alloc_freeze (long int size)
1079 long size;
1080{ 1022{
1081 if (! r_alloc_initialized) 1023 if (! r_alloc_initialized)
1082 r_alloc_init (); 1024 r_alloc_init ();
@@ -1093,7 +1035,7 @@ r_alloc_freeze (size)
1093} 1035}
1094 1036
1095void 1037void
1096r_alloc_thaw () 1038r_alloc_thaw (void)
1097{ 1039{
1098 1040
1099 if (! r_alloc_initialized) 1041 if (! r_alloc_initialized)
@@ -1103,7 +1045,7 @@ r_alloc_thaw ()
1103 abort (); 1045 abort ();
1104 1046
1105 /* This frees all unused blocs. It is not too inefficient, as the resize 1047 /* This frees all unused blocs. It is not too inefficient, as the resize
1106 and bcopy is done only once. Afterwards, all unreferenced blocs are 1048 and memcpy is done only once. Afterwards, all unreferenced blocs are
1107 already shrunk to zero size. */ 1049 already shrunk to zero size. */
1108 if (!r_alloc_freeze_level) 1050 if (!r_alloc_freeze_level)
1109 { 1051 {
@@ -1122,7 +1064,7 @@ r_alloc_thaw ()
1122/* Reinitialize the morecore hook variables after restarting a dumped 1064/* Reinitialize the morecore hook variables after restarting a dumped
1123 Emacs. This is needed when using Doug Lea's malloc from GNU libc. */ 1065 Emacs. This is needed when using Doug Lea's malloc from GNU libc. */
1124void 1066void
1125r_alloc_reinit () 1067r_alloc_reinit (void)
1126{ 1068{
1127 /* Only do this if the hook has been reset, so that we don't get an 1069 /* Only do this if the hook has been reset, so that we don't get an
1128 infinite loop, in case Emacs was linked statically. */ 1070 infinite loop, in case Emacs was linked statically. */
@@ -1235,8 +1177,7 @@ r_alloc_check ()
1235 is checked to ensure that memory corruption does not occur due to 1177 is checked to ensure that memory corruption does not occur due to
1236 misuse. */ 1178 misuse. */
1237void 1179void
1238r_alloc_reset_variable (old, new) 1180r_alloc_reset_variable (POINTER *old, POINTER *new)
1239 POINTER *old, *new;
1240{ 1181{
1241 bloc_ptr bloc = first_bloc; 1182 bloc_ptr bloc = first_bloc;
1242 1183
@@ -1266,7 +1207,7 @@ r_alloc_reset_variable (old, new)
1266/* Initialize various things for memory allocation. */ 1207/* Initialize various things for memory allocation. */
1267 1208
1268static void 1209static void
1269r_alloc_init () 1210r_alloc_init (void)
1270{ 1211{
1271 if (r_alloc_initialized) 1212 if (r_alloc_initialized)
1272 return; 1213 return;
@@ -1314,8 +1255,8 @@ r_alloc_init ()
1314 even though it is after the sbrk value. */ 1255 even though it is after the sbrk value. */
1315 /* Doubly true, with the additional call that explicitly adds the 1256 /* Doubly true, with the additional call that explicitly adds the
1316 rest of that page to the address space. */ 1257 rest of that page to the address space. */
1317 bzero (first_heap->start, 1258 memset (first_heap->start, 0,
1318 (char *) first_heap->end - (char *) first_heap->start); 1259 (char *) first_heap->end - (char *) first_heap->start);
1319 virtual_break_value = break_value = first_heap->bloc_start = first_heap->end; 1260 virtual_break_value = break_value = first_heap->bloc_start = first_heap->end;
1320#endif 1261#endif
1321 1262