diff options
Diffstat (limited to 'src/ralloc.c')
| -rw-r--r-- | src/ralloc.c | 125 |
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/>. */ | |||
| 37 | typedef POINTER_TYPE *POINTER; | 37 | typedef POINTER_TYPE *POINTER; |
| 38 | typedef size_t SIZE; | 38 | typedef size_t SIZE; |
| 39 | 39 | ||
| 40 | /* Declared in dispnew.c, this version doesn't screw up if regions | ||
| 41 | overlap. */ | ||
| 42 | |||
| 43 | extern 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 |
| 47 | extern int mallopt (); | 42 | extern int mallopt (int, int); |
| 48 | #else /* not DOUG_LEA_MALLOC */ | 43 | #else /* not DOUG_LEA_MALLOC */ |
| 49 | #ifndef SYSTEM_MALLOC | 44 | #ifndef SYSTEM_MALLOC |
| 50 | extern size_t __malloc_extra_blocks; | 45 | extern 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 | ||
| 82 | static int r_alloc_initialized = 0; | 74 | static int r_alloc_initialized = 0; |
| 83 | 75 | ||
| 84 | static void r_alloc_init (); | 76 | static 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. */ |
| 90 | POINTER (*real_morecore) (); | 82 | POINTER (*real_morecore) (long int); |
| 91 | 83 | ||
| 92 | /* The break value, as seen by malloc. */ | 84 | /* The break value, as seen by malloc. */ |
| 93 | static POINTER virtual_break_value; | 85 | static 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 |
| 122 | extern POINTER (*__morecore) (); | 114 | extern 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 | ||
| 212 | static heap_ptr | 204 | static heap_ptr |
| 213 | find_heap (address) | 205 | find_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 | ||
| 245 | static POINTER | 236 | static POINTER |
| 246 | obtain (address, size) | 237 | obtain (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 | ||
| 328 | static void | 317 | static void |
| 329 | relinquish () | 318 | relinquish (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 | ||
| 387 | long | 376 | long |
| 388 | r_alloc_size_in_use () | 377 | r_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 | ||
| 398 | static bloc_ptr | 387 | static bloc_ptr |
| 399 | find_bloc (ptr) | 388 | find_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 | ||
| 424 | static bloc_ptr | 412 | static bloc_ptr |
| 425 | get_bloc (size) | 413 | get_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 | ||
| 480 | static int | 467 | static int |
| 481 | relocate_blocs (bloc, heap, address) | 468 | relocate_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 | |||
| 543 | static void | ||
| 544 | reorder_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 | ||
| 572 | static void | 526 | static void |
| 573 | update_heap_bloc_correspondence (bloc, heap) | 527 | update_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 | ||
| 636 | static int | 588 | static int |
| 637 | resize_bloc (bloc, size) | 589 | resize_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 | ||
| 735 | static void | 685 | static void |
| 736 | free_bloc (bloc) | 686 | free_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 | ||
| 802 | POINTER | 751 | POINTER |
| 803 | r_alloc_sbrk (size) | 752 | r_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 | ||
| 954 | POINTER | 902 | POINTER |
| 955 | r_alloc (ptr, size) | 903 | r_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 | ||
| 979 | void | 925 | void |
| 980 | r_alloc_free (ptr) | 926 | r_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 | ||
| 1014 | POINTER | 959 | POINTER |
| 1015 | r_re_alloc (ptr, size) | 960 | r_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 | ||
| 1077 | void | 1020 | void |
| 1078 | r_alloc_freeze (size) | 1021 | r_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 | ||
| 1095 | void | 1037 | void |
| 1096 | r_alloc_thaw () | 1038 | r_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. */ |
| 1124 | void | 1066 | void |
| 1125 | r_alloc_reinit () | 1067 | r_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. */ |
| 1237 | void | 1179 | void |
| 1238 | r_alloc_reset_variable (old, new) | 1180 | r_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 | ||
| 1268 | static void | 1209 | static void |
| 1269 | r_alloc_init () | 1210 | r_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 | ||