First public contribution.
1 // Copyright (c) 1994-2009 Nokia Corporation and/or its subsidiary(-ies).
2 // All rights reserved.
3 // This component and the accompanying materials are made available
4 // under the terms of the License "Eclipse Public License v1.0"
5 // which accompanies this distribution, and is available
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
14 // e32\common\arm\cheap.cia
19 #include "../common.h"
21 #if defined(__HEAP_MACHINE_CODED__) && !defined(_DEBUG)
22 GLREF_C void RHeap_PanicBadAllocatedCellSize();
23 GLREF_C void RHeap_PanicBadNextCell();
24 GLREF_C void RHeap_PanicBadPrevCell();
25 GLREF_C void RHeap_PanicBadCellAddress();
27 IMPORT_D extern const TInt KHeapShrinkHysRatio;
30 __NAKED__ RHeap::SCell* RHeap::DoAlloc(TInt /*aSize*/, SCell*& /*aLastFree*/)
35 asm("stmfd sp!, {r4,r5,lr} ");
36 asm("mov r4, r0 "); // r4=this
37 asm("add r3, r0, #%a0" : : "i" _FOFF(RHeap,iFree)); // r3=pP=&iFree
38 asm("ldr r0, [r3, #4] "); // r0=pC=pP->next
40 asm("beq 0f "); // if no free cells, alloc failed
42 // optimised unfolded scanning loop
44 asm("ldmia r0, {r12,r14} "); // r12=pC->size, r14=pC->next
45 asm("cmp r1, r12 "); // compare aSize to size
46 asm("movhi r3, r0 "); // if aSize>size, pP=pC
47 asm("movhis r0, r14 "); // and pC=pC->next
48 #ifndef __CPU_ARMV6 // don't unroll on armv6
49 asm("ldmhiia r0, {r12,r14} "); // r12=pC->size, r14=pC->next
50 asm("cmphi r1, r12 "); // compare aSize to size
51 asm("movhi r3, r0 "); // if aSize>size, pP=pC
52 asm("movhis r0, r14 "); // and pC=pC->next
54 asm("bhi 2b "); // branch back if scan not finished
57 asm("subs r5, r12, r1 "); // r5 = pC->len - aSize
58 asm("ldrhs r2, [r4, #%a0]" : : "i" _FOFF(RHeap,iMinCell)); // if big enough, r2=iMinCell
59 asm("blo 0f "); // branch if no free cell was big enough
60 asm("cmp r5, r2 "); // leftover big enough?
61 asm("movlo r1, r12 "); // if not, aSize=pC->len ...
62 asm("strlo r14, [r3, #4] "); // ... and pP->next = pC->next
63 asm("addhs r2, r0, r1 "); // else r2 = pE = address of new free cell ...
64 asm("stmhsia r2, {r5, r14} "); // ... pE->len = pC->len - aSize, pE->next = pC->next ...
65 asm("strhs r2, [r3, #4] "); // ... and pP->next = pE
66 asm("str r1, [r0] "); // pC->len = aSize
67 __POPRET("r4,r5,"); // restore and exit, return pC
70 asm("str r3, [r2] "); // alloc failed - aLastFree=pP
71 asm("mov r0, #0 "); // return NULL
75 __NAKED__ void RHeap::DoFree(SCell* /*pC*/)
80 asm("add r2, r0, #%a0" : : "i" _FOFF(RHeap,iFree)); // r2=pP=&iFree
81 asm("ldr r3, [r2, #4] "); // r3=pE=pP->next
82 asm("stmfd sp!, {r4, r5} ");
85 asm("cmp r3, #0 "); // check if pE=NULL
86 asm("cmphi r1, r3 "); // if not, check if pC>pE
87 asm("movhi r2, r3 "); // if so, pP=pE
88 asm("ldrhi r3, [r3, #4] "); // and pE=pE->next
89 #ifndef __CPU_ARMV6 // don't unroll on armv6
90 asm("cmphi r3, #0 "); // check if pE=NULL
91 asm("cmphi r1, r3 "); // if not, check if pC>pE
92 asm("movhi r2, r3 "); // if so, pP=pE
93 asm("ldrhi r3, [r3, #4] "); // and pE=pE->next
95 asm("bhi 1b "); // loop if free cell position not found
97 asm("ldr r4, [r1, #0] "); // r4=pC->len
98 asm("cmp r3, #0 "); // is there a following free cell ?
99 asm("streq r3, [r1, #4] "); // if not, pC->next=NULL
100 asm("beq 2f "); // and skip next section
101 asm("add r5, r1, r4 "); // r5=pN=pC + pC->len (cell after pC)
102 asm("cmp r5, r3 "); // compare pN with pE
103 asm("ldmeqia r3, {r5, r12} "); // if pN==pE, r5=pE->len, r12=pE->next
104 asm("bhi " CSM_Z22RHeap_PanicBadNextCellv ); // if pN>pE, panic
105 asm("strne r3, [r1, #4] "); // if pN<pE, pC->next=pE
106 asm("addeq r4, r4, r5 "); // if pN==pE r4 = pC->len + pE->len
107 asm("stmeqia r1, {r4,r12} "); // if pN==pE pC->len+=pE->len, pC->next=pE->next
109 asm("ldr r3, [r2, #0] "); // r3=pP->len
110 asm("sub r5, r1, r2 "); // r5=pC-pP (gap between preceding free cell and this one)
111 asm("cmp r5, r3 "); // compare gap with predecessor length
112 asm("ldreq r12, [r1, #4] "); // if predecessor is adjacent, r12=pC->next
113 asm("blo RHeap_PanicBadPrevCell__Fv "); // if predecessor overlaps, panic
114 asm("addeq r4, r4, r3 "); // if predecessor is adjacent, r4=pC->len + pP->len
115 asm("stmeqia r2, {r4,r12} "); // if predecessor is adjacent, pP->len+=pC->len, pP->next=pC->next
116 asm("strne r1, [r2, #4] "); // else pP->next = pC
117 asm("moveq r1, r2 "); // if predecessor is adjacent, pC=pP (final amalgamated free cell)
119 asm("ldr r12, [r0, #%a0]" : : "i" _FOFF(RHeap,iTop)); // r12=iTop
120 asm("add r3, r1, r4 "); // end of amalgamated free cell
121 asm("cmp r3, r12 "); // end of amalgamated free cell = iTop ?
122 asm("ldmneia sp!, {r4,r5} "); // restore registers
123 __JUMP(ne,lr); // if not, finished
124 asm("ldr r12, [r0, #%a0]" : : "i" _FOFF(RHeap,iFlags)); // r12=iFlags
125 asm("tst r12, #%a0" : : "i" ((TInt)RAllocator::EFixedSize)); // check fixed size flag
126 asm("ldmneia sp!, {r4,r5} "); // restore registers
127 __JUMP(ne,lr); // if set, finished
128 asm("ldr r2, [r0, #%a0]" : : "i" _FOFF(RHeap,iGrowBy)); // r2=iGrowBy
129 asm("mov r3, r2, LSR #8"); // r3=iGrowBy>>8
130 asm("ldr r2, const_addr"); // r2=&KHeapShrinkHysRatio
131 asm("ldr r5, [r2]"); // r5=KHeapShrinkHysRatio
132 asm("mul r2, r5, r3"); // r2=KHeapShrinkHysRatio*(iGrowBy>>8) - low order bits
133 asm("cmp r4, r2"); // compare len(r4) to (iGrowBy>>8)*KHeapShrinkHysRatio(r2)
134 asm("ldmia sp!, {r4,r5} "); // restore registers
135 __JUMP(lo,lr); // if less, finished
136 asm("b Reduce__5RHeapPQ25RHeap5SCell "); // else reduce heap
139 asm(".word %a0" : : "i" ((TInt)&KHeapShrinkHysRatio));