Rev 4424 | Rev 5356 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 4424 | Rev 5130 | ||
---|---|---|---|
Line 3... | Line 3... | ||
3 | ;; Copyright (C) KolibriOS team 2004-2012. All rights reserved. ;; |
3 | ;; Copyright (C) KolibriOS team 2004-2012. All rights reserved. ;; |
4 | ;; Distributed under terms of the GNU General Public License ;; |
4 | ;; Distributed under terms of the GNU General Public License ;; |
5 | ;; ;; |
5 | ;; ;; |
6 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
6 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
Line 7... | Line 7... | ||
7 | 7 | ||
Line 8... | Line 8... | ||
8 | $Revision: 4424 $ |
8 | $Revision: 5130 $ |
9 | 9 | ||
10 | 10 | ||
Line 556... | Line 556... | ||
556 | restore block_list |
556 | restore block_list |
557 | restore block_base |
557 | restore block_base |
558 | restore block_size |
558 | restore block_size |
559 | restore block_flags |
559 | restore block_flags |
Line 560... | Line 560... | ||
560 | 560 | ||
Line 561... | Line 561... | ||
561 | ;;;;;;;;;;;;;; USER ;;;;;;;;;;;;;;;;; |
561 | ;;;;;;;;;;;;;; USER HEAP ;;;;;;;;;;;;;;;;; |
Line 562... | Line 562... | ||
562 | 562 | ||
563 | HEAP_TOP equ 0x80000000 |
563 | HEAP_TOP equ 0x80000000 |
Line 564... | Line 564... | ||
564 | 564 | ||
565 | align 4 |
565 | align 4 |
566 | proc init_heap |
566 | proc init_heap |
567 | 567 | ||
568 | mov ebx, [current_slot] |
568 | mov ebx, [current_process] |
569 | mov eax, [ebx+APPDATA.heap_top] |
569 | mov eax, [ebx+PROC.heap_top] |
570 | test eax, eax |
570 | test eax, eax |
571 | jz @F |
571 | jz @F |
- | 572 | sub eax, [ebx+PROC.heap_base] |
|
- | 573 | sub eax, PAGE_SIZE |
|
- | 574 | ret |
|
572 | sub eax, [ebx+APPDATA.heap_base] |
575 | @@: |
573 | sub eax, 4096 |
576 | lea ecx, [ebx+PROC.heap_lock] |
574 | ret |
577 | call mutex_init |
575 | @@: |
578 | |
576 | mov esi, [ebx+APPDATA.mem_size] |
579 | mov esi, [ebx+PROC.mem_used] |
577 | add esi, 4095 |
580 | add esi, 4095 |
578 | and esi, not 4095 |
581 | and esi, not 4095 |
Line 579... | Line 582... | ||
579 | mov [ebx+APPDATA.mem_size], esi |
582 | mov [ebx+PROC.mem_used], esi |
580 | mov eax, HEAP_TOP |
583 | mov eax, HEAP_TOP |
581 | mov [ebx+APPDATA.heap_base], esi |
584 | mov [ebx+PROC.heap_base], esi |
582 | mov [ebx+APPDATA.heap_top], eax |
585 | mov [ebx+PROC.heap_top], eax |
583 | 586 | ||
584 | sub eax, esi |
587 | sub eax, esi |
585 | shr esi, 10 |
588 | shr esi, 10 |
586 | mov ecx, eax |
589 | mov ecx, eax |
Line 595... | Line 598... | ||
595 | 598 | ||
596 | push ebx |
599 | push ebx |
597 | push esi |
600 | push esi |
Line -... | Line 601... | ||
- | 601 | push edi |
|
- | 602 | ||
- | 603 | mov ebx, [current_process] |
|
- | 604 | lea ecx, [ebx+PROC.heap_lock] |
|
598 | push edi |
605 | call mutex_lock |
599 | 606 | ||
600 | mov ecx, [alloc_size] |
607 | mov ecx, [alloc_size] |
Line 601... | Line -... | ||
601 | add ecx, (4095+4096) |
- | |
602 | and ecx, not 4095 |
608 | add ecx, (4095+PAGE_SIZE) |
603 | 609 | and ecx, not 4095 |
|
604 | mov ebx, [current_slot] |
610 | |
605 | mov esi, dword [ebx+APPDATA.heap_base] ; heap_base |
611 | mov esi, dword [ebx+PROC.heap_base] ; heap_base |
606 | mov edi, dword [ebx+APPDATA.heap_top] ; heap_top |
612 | mov edi, dword [ebx+PROC.heap_top] ; heap_top |
Line 607... | Line 613... | ||
607 | l_0: |
613 | .scan: |
608 | cmp esi, edi |
614 | cmp esi, edi |
609 | jae m_exit |
615 | jae .m_exit |
610 | 616 | ||
611 | mov ebx, esi |
617 | mov ebx, esi |
612 | shr ebx, 12 |
618 | shr ebx, 12 |
613 | mov eax, [page_tabs+ebx*4] |
619 | mov eax, [page_tabs+ebx*4] |
614 | test al, FREE_BLOCK |
620 | test al, FREE_BLOCK |
615 | jz test_used |
621 | jz .test_used |
Line 616... | Line 622... | ||
616 | and eax, 0xFFFFF000 |
622 | and eax, 0xFFFFF000 |
617 | cmp eax, ecx ;alloc_size |
623 | cmp eax, ecx ;alloc_size |
618 | jb m_next |
624 | jb .m_next |
Line 635... | Line 641... | ||
635 | inc ebx |
641 | inc ebx |
636 | dec ecx |
642 | dec ecx |
637 | jnz @B |
643 | jnz @B |
638 | .no: |
644 | .no: |
Line 639... | Line 645... | ||
639 | 645 | ||
640 | mov edx, [current_slot] |
646 | mov edx, [current_process] |
641 | mov ebx, [alloc_size] |
647 | mov ebx, [alloc_size] |
642 | add ebx, 0xFFF |
648 | add ebx, 0xFFF |
- | 649 | and ebx, not 0xFFF |
|
- | 650 | add [edx+PROC.mem_used], ebx |
|
643 | and ebx, not 0xFFF |
651 | |
644 | add ebx, [edx+APPDATA.mem_size] |
652 | lea ecx, [edx+PROC.heap_lock] |
Line 645... | Line 653... | ||
645 | call update_mem_size |
653 | call mutex_unlock |
Line 646... | Line 654... | ||
646 | 654 | ||
647 | lea eax, [esi+4096] |
655 | lea eax, [esi+4096] |
648 | 656 | ||
649 | pop edi |
657 | pop edi |
650 | pop esi |
658 | pop esi |
651 | pop ebx |
659 | pop ebx |
652 | ret |
660 | ret |
Line 653... | Line 661... | ||
653 | test_used: |
661 | .test_used: |
654 | test al, USED_BLOCK |
662 | test al, USED_BLOCK |
655 | jz m_exit |
663 | jz .m_exit |
656 | 664 | ||
657 | and eax, 0xFFFFF000 |
665 | and eax, 0xFFFFF000 |
- | 666 | .m_next: |
|
- | 667 | add esi, eax |
|
- | 668 | jmp .scan |
|
- | 669 | .m_exit: |
|
658 | m_next: |
670 | mov ecx, [current_process] |
659 | add esi, eax |
671 | lea ecx, [ecx+PROC.heap_lock] |
660 | jmp l_0 |
672 | call mutex_unlock |
661 | m_exit: |
673 | |
662 | xor eax, eax |
674 | xor eax, eax |
Line 671... | Line 683... | ||
671 | 683 | ||
672 | push ebx |
684 | push ebx |
673 | push esi |
685 | push esi |
Line 674... | Line 686... | ||
674 | push edi |
686 | push edi |
- | 687 | ||
- | 688 | mov ebx, [current_process] |
|
- | 689 | lea ecx, [ebx+PROC.heap_lock] |
|
675 | 690 | call mutex_lock |
|
676 | mov ebx, [current_slot] |
691 | |
677 | mov edx, [address] |
692 | mov edx, [address] |
678 | and edx, not 0xFFF |
693 | and edx, not 0xFFF |
679 | mov [address], edx |
694 | mov [address], edx |
680 | sub edx, 0x1000 |
695 | sub edx, 0x1000 |
681 | jb .error |
696 | jb .error |
682 | mov esi, [ebx+APPDATA.heap_base] |
697 | mov esi, [ebx+PROC.heap_base] |
683 | mov edi, [ebx+APPDATA.heap_top] |
698 | mov edi, [ebx+PROC.heap_top] |
684 | cmp edx, esi |
699 | cmp edx, esi |
685 | jb .error |
700 | jb .error |
686 | .scan: |
701 | .scan: |
Line 695... | Line 710... | ||
695 | cmp edx, ecx |
710 | cmp edx, ecx |
696 | jb .found |
711 | jb .found |
697 | mov esi, ecx |
712 | mov esi, ecx |
698 | jmp .scan |
713 | jmp .scan |
699 | .error: |
714 | .error: |
- | 715 | mov ecx, [current_process] |
|
- | 716 | lea ecx, [ecx+PROC.heap_lock] |
|
- | 717 | call mutex_unlock |
|
- | 718 | ||
700 | xor eax, eax |
719 | xor eax, eax |
701 | pop edi |
720 | pop edi |
702 | pop esi |
721 | pop esi |
703 | pop ebx |
722 | pop ebx |
704 | ret |
723 | ret |
Line 746... | Line 765... | ||
746 | jz .nothird |
765 | jz .nothird |
747 | or cl, FREE_BLOCK |
766 | or cl, FREE_BLOCK |
748 | mov [page_tabs+ebx*4], ecx |
767 | mov [page_tabs+ebx*4], ecx |
Line 749... | Line 768... | ||
749 | 768 | ||
750 | .nothird: |
- | |
751 | 769 | .nothird: |
|
752 | mov edx, [current_slot] |
770 | mov edx, [current_process] |
753 | mov ebx, [alloc_size] |
771 | mov ebx, [alloc_size] |
754 | add ebx, 0xFFF |
772 | add ebx, 0xFFF |
- | 773 | and ebx, not 0xFFF |
|
- | 774 | add [edx+PROC.mem_used], ebx |
|
755 | and ebx, not 0xFFF |
775 | |
756 | add ebx, [edx+APPDATA.mem_size] |
776 | lea ecx, [edx+PROC.heap_lock] |
Line 757... | Line 777... | ||
757 | call update_mem_size |
777 | call mutex_unlock |
Line 758... | Line 778... | ||
758 | 778 | ||
759 | mov eax, [address] |
779 | mov eax, [address] |
Line 769... | Line 789... | ||
769 | 789 | ||
Line 770... | Line 790... | ||
770 | push esi |
790 | push esi |
771 | 791 | ||
772 | mov esi, [base] |
792 | mov esi, [base] |
Line 773... | Line 793... | ||
773 | test esi, esi |
793 | test esi, esi |
Line -... | Line 794... | ||
- | 794 | jz .fail |
|
- | 795 | ||
- | 796 | push ebx |
|
- | 797 | ||
774 | jz .exit |
798 | mov ebx, [current_process] |
775 | 799 | lea ecx, [ebx+PROC.heap_lock] |
|
776 | push ebx |
800 | call mutex_lock |
777 | 801 | ||
778 | xor ebx, ebx |
802 | xor ebx, ebx |
Line 808... | Line 832... | ||
808 | jnz .release |
832 | jnz .release |
Line 809... | Line 833... | ||
809 | 833 | ||
810 | .released: |
834 | .released: |
Line 811... | Line 835... | ||
811 | push edi |
835 | push edi |
- | 836 | ||
812 | 837 | mov edx, [current_process] |
|
813 | mov edx, [current_slot] |
838 | lea ecx, [edx+PROC.heap_lock] |
814 | mov esi, dword [edx+APPDATA.heap_base] |
839 | mov esi, dword [edx+PROC.heap_base] |
815 | mov edi, dword [edx+APPDATA.heap_top] |
840 | mov edi, dword [edx+PROC.heap_top] |
816 | sub ebx, [edx+APPDATA.mem_size] |
841 | sub ebx, [edx+PROC.mem_used] |
817 | neg ebx |
842 | neg ebx |
818 | call update_mem_size |
843 | mov [edx+PROC.mem_used], ebx |
819 | call user_normalize |
- | |
820 | pop edi |
- | |
821 | pop ebx |
- | |
822 | pop esi |
844 | call user_normalize |
- | 845 | pop edi |
|
- | 846 | .exit: |
|
823 | ret |
847 | call mutex_unlock |
824 | .exit: |
848 | |
- | 849 | xor eax, eax |
|
825 | xor eax, eax |
850 | inc eax |
826 | inc eax |
851 | pop ebx |
- | 852 | pop esi |
|
827 | pop esi |
853 | ret |
- | 854 | ||
- | 855 | .cantfree: |
|
- | 856 | mov ecx, [current_process] |
|
- | 857 | lea ecx, [ecx+PROC.heap_lock] |
|
828 | ret |
858 | jmp .exit |
829 | .cantfree: |
- | |
830 | xor eax, eax |
859 | .fail: |
831 | pop ebx |
860 | xor eax, eax |
832 | pop esi |
861 | pop esi |
Line 955... | Line 984... | ||
955 | push ebx |
984 | push ebx |
956 | call user_alloc |
985 | call user_alloc |
957 | ret |
986 | ret |
958 | @@: |
987 | @@: |
959 | push ecx edx |
988 | push ecx edx |
- | 989 | ||
- | 990 | push eax |
|
- | 991 | mov ecx, [current_process] |
|
- | 992 | lea ecx, [ecx+PROC.heap_lock] |
|
- | 993 | call mutex_lock |
|
- | 994 | pop eax |
|
- | 995 | ||
960 | lea ecx, [eax - 0x1000] |
996 | lea ecx, [eax - 0x1000] |
961 | shr ecx, 12 |
997 | shr ecx, 12 |
962 | mov edx, [page_tabs+ecx*4] |
998 | mov edx, [page_tabs+ecx*4] |
963 | test dl, USED_BLOCK |
999 | test dl, USED_BLOCK |
964 | jnz @f |
1000 | jnz @f |
965 | ; attempt to realloc invalid pointer |
1001 | ; attempt to realloc invalid pointer |
966 | .ret0: |
1002 | .ret0: |
- | 1003 | mov ecx, [current_process] |
|
- | 1004 | lea ecx, [ecx+PROC.heap_lock] |
|
- | 1005 | call mutex_unlock |
|
- | 1006 | ||
967 | pop edx ecx |
1007 | pop edx ecx |
968 | xor eax, eax |
1008 | xor eax, eax |
969 | ret |
1009 | ret |
970 | @@: |
1010 | @@: |
971 | test dl, DONT_FREE_BLOCK |
1011 | test dl, DONT_FREE_BLOCK |
Line 996... | Line 1036... | ||
996 | sub ebx, ecx |
1036 | sub ebx, ecx |
997 | cmp ebx, 1 |
1037 | cmp ebx, 1 |
998 | jnz .nofreeall |
1038 | jnz .nofreeall |
999 | mov eax, [page_tabs+ecx*4] |
1039 | mov eax, [page_tabs+ecx*4] |
1000 | and eax, not 0xFFF |
1040 | and eax, not 0xFFF |
1001 | mov edx, [current_slot] |
1041 | mov edx, [current_process] |
1002 | mov ebx, [APPDATA.mem_size+edx] |
1042 | mov ebx, [edx+PROC.mem_used] |
1003 | sub ebx, eax |
1043 | sub ebx, eax |
1004 | add ebx, 0x1000 |
1044 | add ebx, 0x1000 |
1005 | or al, FREE_BLOCK |
1045 | or al, FREE_BLOCK |
1006 | mov [page_tabs+ecx*4], eax |
1046 | mov [page_tabs+ecx*4], eax |
1007 | push esi edi |
1047 | push esi edi |
1008 | mov esi, [APPDATA.heap_base+edx] |
1048 | mov esi, [edx+PROC.heap_base] |
1009 | mov edi, [APPDATA.heap_top+edx] |
1049 | mov edi, [edx+PROC.heap_top] |
1010 | call update_mem_size |
1050 | mov [edx+PROC.mem_used], ebx |
1011 | call user_normalize |
1051 | call user_normalize |
1012 | pop edi esi |
1052 | pop edi esi |
1013 | jmp .ret0 ; all freed |
1053 | jmp .ret0 ; all freed |
1014 | .nofreeall: |
1054 | .nofreeall: |
1015 | sub edx, ecx |
1055 | sub edx, ecx |
Line 1017... | Line 1057... | ||
1017 | or ebx, USED_BLOCK |
1057 | or ebx, USED_BLOCK |
1018 | xchg [page_tabs+ecx*4], ebx |
1058 | xchg [page_tabs+ecx*4], ebx |
1019 | shr ebx, 12 |
1059 | shr ebx, 12 |
1020 | sub ebx, edx |
1060 | sub ebx, edx |
1021 | push ebx ecx edx |
1061 | push ebx ecx edx |
1022 | mov edx, [current_slot] |
1062 | mov edx, [current_process] |
1023 | shl ebx, 12 |
1063 | shl ebx, 12 |
1024 | sub ebx, [APPDATA.mem_size+edx] |
1064 | sub ebx, [edx+PROC.mem_used] |
1025 | neg ebx |
1065 | neg ebx |
1026 | call update_mem_size |
1066 | mov [edx+PROC.mem_used], ebx |
1027 | pop edx ecx ebx |
1067 | pop edx ecx ebx |
1028 | lea eax, [ecx+1] |
1068 | lea eax, [ecx+1] |
1029 | shl eax, 12 |
1069 | shl eax, 12 |
1030 | push eax |
1070 | push eax |
1031 | add ecx, edx |
1071 | add ecx, edx |
1032 | lea edx, [ecx+ebx] |
1072 | lea edx, [ecx+ebx] |
1033 | shl ebx, 12 |
1073 | shl ebx, 12 |
1034 | jz .ret |
1074 | jz .ret |
1035 | push esi |
1075 | push esi |
1036 | mov esi, [current_slot] |
1076 | mov esi, [current_process] |
1037 | mov esi, [APPDATA.heap_top+esi] |
1077 | mov esi, [esi+PROC.heap_top] |
1038 | shr esi, 12 |
1078 | shr esi, 12 |
1039 | @@: |
1079 | @@: |
1040 | cmp edx, esi |
1080 | cmp edx, esi |
1041 | jae .merge_done |
1081 | jae .merge_done |
1042 | mov eax, [page_tabs+edx*4] |
1082 | mov eax, [page_tabs+edx*4] |
Line 1051... | Line 1091... | ||
1051 | .merge_done: |
1091 | .merge_done: |
1052 | pop esi |
1092 | pop esi |
1053 | or ebx, FREE_BLOCK |
1093 | or ebx, FREE_BLOCK |
1054 | mov [page_tabs+ecx*4], ebx |
1094 | mov [page_tabs+ecx*4], ebx |
1055 | .ret: |
1095 | .ret: |
- | 1096 | mov ecx, [current_process] |
|
- | 1097 | lea ecx, [ecx+PROC.heap_lock] |
|
- | 1098 | call mutex_unlock |
|
1056 | pop eax edx ecx |
1099 | pop eax edx ecx |
1057 | ret |
1100 | ret |
- | 1101 | ||
1058 | .realloc_add: |
1102 | .realloc_add: |
1059 | ; get some additional memory |
1103 | ; get some additional memory |
1060 | mov eax, [current_slot] |
1104 | mov eax, [current_process] |
1061 | mov eax, [APPDATA.heap_top+eax] |
1105 | mov eax, [eax+PROC.heap_top] |
1062 | shr eax, 12 |
1106 | shr eax, 12 |
1063 | cmp edx, eax |
1107 | cmp edx, eax |
1064 | jae .cant_inplace |
1108 | jae .cant_inplace |
1065 | mov eax, [page_tabs+edx*4] |
1109 | mov eax, [page_tabs+edx*4] |
1066 | test al, FREE_BLOCK |
1110 | test al, FREE_BLOCK |
Line 1088... | Line 1132... | ||
1088 | sub ebx, edx |
1132 | sub ebx, edx |
1089 | mov ecx, ebx |
1133 | mov ecx, ebx |
1090 | cld |
1134 | cld |
1091 | rep stosd |
1135 | rep stosd |
1092 | pop edi |
1136 | pop edi |
1093 | mov edx, [current_slot] |
1137 | mov edx, [current_process] |
1094 | shl ebx, 12 |
1138 | shl ebx, 12 |
1095 | add ebx, [APPDATA.mem_size+edx] |
1139 | add [edx+PROC.mem_used], ebx |
- | 1140 | ||
- | 1141 | mov ecx, [current_process] |
|
- | 1142 | lea ecx, [ecx+PROC.heap_lock] |
|
1096 | call update_mem_size |
1143 | call mutex_unlock |
1097 | pop eax edx ecx |
1144 | pop eax edx ecx |
1098 | ret |
1145 | ret |
- | 1146 | ||
1099 | .cant_inplace: |
1147 | .cant_inplace: |
1100 | push esi edi |
1148 | push esi edi |
1101 | mov eax, [current_slot] |
1149 | mov eax, [current_process] |
1102 | mov esi, [APPDATA.heap_base+eax] |
1150 | mov esi, [eax+PROC.heap_base] |
1103 | mov edi, [APPDATA.heap_top+eax] |
1151 | mov edi, [eax+PROC.heap_top] |
1104 | shr esi, 12 |
1152 | shr esi, 12 |
1105 | shr edi, 12 |
1153 | shr edi, 12 |
1106 | sub ebx, ecx |
1154 | sub ebx, ecx |
1107 | .find_place: |
1155 | .find_place: |
1108 | cmp esi, edi |
1156 | cmp esi, edi |
Line 1161... | Line 1209... | ||
1161 | dec ebx |
1209 | dec ebx |
1162 | dec edx |
1210 | dec edx |
1163 | jnz @b |
1211 | jnz @b |
1164 | .no: |
1212 | .no: |
1165 | push ebx |
1213 | push ebx |
1166 | mov edx, [current_slot] |
1214 | mov edx, [current_process] |
1167 | shl ebx, 12 |
1215 | shl ebx, 12 |
1168 | add ebx, [APPDATA.mem_size+edx] |
1216 | add [edx+PROC.mem_used], ebx |
1169 | call update_mem_size |
- | |
1170 | pop ebx |
1217 | pop ebx |
1171 | @@: |
1218 | @@: |
1172 | mov dword [page_tabs+esi*4], 2 |
1219 | mov dword [page_tabs+esi*4], 2 |
1173 | inc esi |
1220 | inc esi |
1174 | dec ebx |
1221 | dec ebx |
1175 | jnz @b |
1222 | jnz @b |
1176 | pop eax edi esi edx ecx |
- | |
1177 | ret |
- | |
Line 1178... | Line -... | ||
1178 | - | ||
1179 | if 0 |
- | |
1180 | align 4 |
- | |
1181 | proc alloc_dll |
- | |
1182 | pushf |
- | |
1183 | cli |
1223 | |
1184 | bsf eax, [dll_map] |
- | |
1185 | jnz .find |
- | |
1186 | popf |
- | |
1187 | xor eax, eax |
- | |
1188 | ret |
- | |
1189 | .find: |
- | |
1190 | btr [dll_map], eax |
- | |
1191 | popf |
- | |
1192 | shl eax, 5 |
- | |
1193 | add eax, dll_tab |
- | |
1194 | ret |
- | |
1195 | endp |
- | |
1196 | - | ||
1197 | align 4 |
- | |
1198 | proc alloc_service |
- | |
1199 | pushf |
- | |
1200 | cli |
1224 | mov ecx, [current_process] |
1201 | bsf eax, [srv_map] |
1225 | lea ecx, [ecx+PROC.heap_lock] |
1202 | jnz .find |
- | |
1203 | popf |
1226 | call mutex_unlock |
1204 | xor eax, eax |
- | |
1205 | ret |
- | |
1206 | .find: |
- | |
1207 | btr [srv_map], eax |
- | |
1208 | popf |
- | |
1209 | shl eax, 0x02 |
- | |
1210 | lea eax, [srv_tab+eax+eax*8] ;srv_tab+eax*36 |
1227 | pop eax edi esi edx ecx |
1211 | ret |
- | |
Line 1212... | Line -... | ||
1212 | endp |
- | |
Line 1213... | Line 1228... | ||
1213 | 1228 | ret |
|
Line 1214... | Line 1229... | ||
1214 | end if |
1229 | |
1215 | 1230 |