My favorites | Sign in
Project Home Downloads Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#include "Core.h"

NAMESPACE_UPP

#ifdef UPP_HEAP

#define LLOG(x) // LOG((void *)this << ' ' << x)

#include "HeapImp.h"

word Heap::BinSz[LBINS];
byte Heap::SzBin[MAXBLOCK / 8 + 1];
byte Heap::BlBin[MAXBLOCK / 8 + 1];

Heap::DLink Heap::lempty[1];

void Heap::GlobalLInit()
{
ONCELOCK {
int p = 32;
int bi = 0;
while(p < MAXBLOCK) {
BinSz[bi++] = p;
int add = minmax(6 * p / 100 / 32 * 32, 32, 2048);
p += add;
}
ASSERT(bi == LBINS - 1);
BinSz[LBINS - 1] = MAXBLOCK;
int k = 0;
for(int i = 0; i < MAXBLOCK / 8; i++) {
while(i * 8 + 7 > BinSz[k])
k++;
SzBin[i] = k;
}
k = LBINS - 1;
for(int i = MAXBLOCK / 8; i >= 0; i--) {
while(i * 8 < BinSz[k]) k--;
BlBin[i] = k;
}
BlBin[0] = 0;
big->LinkSelf();
lempty->LinkSelf();
}
}

inline
void Heap::LinkFree(DLink *b, int size)
{
int q = BlBin[size >> 4];
b->Link(freebin[q]);
}

Heap::DLink *Heap::AddChunk(int reqsize)
{
DLink *ml;
if(lempty->next != lempty) {
ml = lempty->next;
ml->Unlink();
LLOG("Retrieved empty large " << (void *)ml);
}
else {
ml = (DLink *)AllocRaw64KB(reqsize);
LLOG("AllocRaw64KB " << (void *)ml);
}
lcount++;
LLOG("lcount = " << lcount);
if(!ml) return NULL;
ml->Link(large);
Header *bh = (Header *)((byte *)ml + LARGEHDRSZ);
bh->size = MAXBLOCK;
bh->prev = 0;
bh->free = true;
bh->heap = this;
DLink *b = bh->GetBlock();
LinkFree(b, MAXBLOCK);
DbgFreeFill(b + 1, MAXBLOCK - sizeof(DLink));
bh = bh->Next();
bh->prev = MAXBLOCK;
bh->size = 0;
bh->free = false;
bh->heap = this;
return b;
}

inline
void *Heap::DivideBlock(DLink *b, int size, int ii)
{
b->Unlink();
Header *bh = b->GetHeader();
ASSERT(bh->size >= size && size > 0);
bh->free = false;
int sz2 = bh->size - size - sizeof(Header);
if(sz2 >= 32) {
Header *bh2 = (Header *)((byte *)b + size);
bh2->prev = size;
bh2->free = true;
bh2->heap = this;
LinkFree(bh2->GetBlock(), sz2);
bh->Next()->prev = bh2->size = sz2;
bh->size = size;
}
DbgFreeCheck(b + 1, size - sizeof(DLink));
return b;
}

void Heap::MoveLarge(Heap *dest, DLink *l)
{
dest->lcount++;
LLOG("Moving large " << (void *)l << " to " << (void *)dest << " lcount " << dest->lcount);
Mutex::Lock __(mutex);
l->Unlink();
l->Link(dest->large);
Header *h = (Header *)((byte *)l + LARGEHDRSZ);
while(h->size) {
h->heap = dest;
if(h->free) {
DLink *b = h->GetBlock();
b->Unlink();
dest->LinkFree(b, h->size);
}
h = h->Next();
}
aux.lcount = 10000;
}

void Heap::MoveToEmpty(DLink *l, Header *bh)
{
LLOG("Moving empty large " << (void *)l << " to global empty storage, lcount " << lcount);
bh->GetBlock()->Unlink();
l->Unlink();
Mutex::Lock __(mutex);
l->Link(lempty);
aux.lcount = 10000;
}

inline
void *Heap::TryLAlloc(int ii, size_t size)
{
while(ii < LBINS) {
if(freebin[ii] != freebin[ii]->next) {
void *ptr = DivideBlock(freebin[ii]->next, (int)size, ii);
LLOG("TryLAlloc succeeded " << (void *)ptr);
ASSERT((size_t)ptr & 8);
return ptr;
}
ii++;
}
return NULL;
}

int sBig__;

void *Heap::LAlloc(size_t& size) {
LLOG("LAlloc " << size);
ASSERT(size > 256);
if(!initialized)
Init();
if(size > MAXBLOCK) {
Mutex::Lock __(mutex);
BigHdr *h = (BigHdr *)SysAllocRaw(size + BIGHDRSZ, size);
h->Link(big);
h->size = size = ((size + BIGHDRSZ + 4095) & ~4095) - BIGHDRSZ;
Header *b = (Header *)((byte *)h + BIGHDRSZ - sizeof(Header));
b->size = 0;
b->free = false;
sBig__++;
LLOG("Big alloc " << (void *)b->GetBlock());
return b->GetBlock();
}
int bini = SizeToBin((int)size);
size = BinSz[bini];
void *ptr = TryLAlloc(bini, size);
if(ptr)
return ptr;
Mutex::Lock __(mutex);
if(remote_free) {
FreeRemoteRaw();
ptr = TryLAlloc(bini, size);
if(ptr) return ptr;
}
aux.FreeRemoteRaw();
while(aux.large->next != aux.large) {
LLOG("Adopting large block " << (void *)aux.large->next);
MoveLarge(this, aux.large->next);
lcount++;
ptr = TryLAlloc(bini, size);
if(ptr) return ptr;
}
DLink *n = AddChunk(size);
if(!n)
Panic("Out of memory!");
ptr = DivideBlock(n, (int)size, LBINS - 1);
LLOG("LAlloc via AddChunk " << (void *)ptr);
ASSERT((size_t)ptr & 8);
return ptr;
}

void Heap::LFree(void *ptr) {
DLink *b = (DLink *)ptr;
Header *bh = b->GetHeader();
if(bh->size == 0) {
Mutex::Lock __(mutex);
ASSERT(((dword)(uintptr_t)bh & 4095) == BIGHDRSZ - sizeof(Header));
BigHdr *h = (BigHdr *)((byte *)ptr - BIGHDRSZ);
h->Unlink();
LLOG("Big free " << (void *) ptr << " size " << h->size);
SysFreeRaw(h, h->size);
sBig__--;
return;
}
if(bh->heap != this) {
LLOG("Remote large, heap " << (void *)bh->heap);
bh->heap->RemoteFree(ptr);
return;
}
if(bh->prev) {
Header *p = bh->Prev();
if(p->free) {
b = p->GetBlock();
b->Unlink();
p->size += bh->size + sizeof(Header);
p->Next()->prev = p->size;
bh = p;
}
}
Header *n = bh->Next();
if(n->free) {
n->GetBlock()->Unlink();
bh->size += n->size + sizeof(Header);
n->Next()->prev = bh->size;
}
bh->free = true;
LinkFree(b, bh->size);
DbgFreeFill(b + 1, bh->size - sizeof(DLink));
LLOG("Freed, joined size " << bh->size << " lcount " << lcount);
if(bh->size == MAXBLOCK && lcount > 1) {
DLink *l = (DLink *)((byte *)bh - LARGEHDRSZ);
lcount--;
MoveToEmpty(l, bh);
}
}

#endif

END_UPP_NAMESPACE

Change log

r4471 by cxl on Jan 26, 2012   Diff
Core: Out of memory panic now displays
info about requested block size and
allocated memory (RM #240)
Go to: 
Project members, sign in to write a code review

Older revisions

r300 by mdelfede on Jun 16, 2008   Diff
new uvs2 releases : uppsrc-2598
tutorial-38  examples-141
reference-113
r281 by mdelfede on Jun 7, 2008   Diff
changed svn layout
r272 by mdelfede on May 28, 2008   Diff
new uvs2 releases : uppsrc-2567
tutorial-38  examples-141
reference-113
All revisions of this file

File info

Size: 5313 bytes, 245 lines

File properties

svn:eol-style
native
Powered by Google Project Hosting