OLD | NEW |
1 #include "SkVarAlloc.h" | 1 #include "SkVarAlloc.h" |
2 #include "SkScalar.h" | 2 #include "SkScalar.h" |
3 | 3 |
4 // We use non-standard malloc diagnostic methods to make sure our allocations ar
e sized well. | 4 // We use non-standard malloc diagnostic methods to make sure our allocations ar
e sized well. |
5 #if defined(SK_BUILD_FOR_MAC) | 5 #if defined(SK_BUILD_FOR_MAC) |
6 #include <malloc/malloc.h> | 6 #include <malloc/malloc.h> |
7 #elif defined(SK_BUILD_FOR_LINUX) | 7 #elif defined(SK_BUILD_FOR_LINUX) |
8 #include <malloc.h> | 8 #include <malloc.h> |
9 #endif | 9 #endif |
10 | 10 |
11 struct SkVarAlloc::Block { | 11 struct SkVarAlloc::Block { |
12 Block* prev; | 12 Block* prev; |
13 char* data() { return (char*)(this + 1); } | 13 char* data() { return (char*)(this + 1); } |
14 | 14 |
15 static Block* Alloc(Block* prev, size_t size, unsigned flags) { | 15 static Block* Alloc(Block* prev, size_t size, unsigned flags) { |
16 SkASSERT(size >= sizeof(Block)); | 16 SkASSERT(size >= sizeof(Block)); |
17 Block* b = (Block*)sk_malloc_flags(size, flags); | 17 Block* b = (Block*)sk_malloc_flags(size, flags); |
18 b->prev = prev; | 18 b->prev = prev; |
19 return b; | 19 return b; |
20 } | 20 } |
21 }; | 21 }; |
22 | 22 |
23 SkVarAlloc::SkVarAlloc() | 23 SkVarAlloc::SkVarAlloc(size_t smallest, float growth) |
24 : fByte(NULL) | 24 : fByte(NULL) |
25 , fRemaining(0) | 25 , fLimit(NULL) |
26 , fLgMinSize(0) | 26 , fSmallest(SkToUInt(smallest)) |
| 27 , fGrowth(growth) |
27 , fBlock(NULL) {} | 28 , fBlock(NULL) {} |
28 | 29 |
29 SkVarAlloc::~SkVarAlloc() { | 30 SkVarAlloc::~SkVarAlloc() { |
30 Block* b = fBlock; | 31 Block* b = fBlock; |
31 while (b) { | 32 while (b) { |
32 Block* prev = b->prev; | 33 Block* prev = b->prev; |
33 sk_free(b); | 34 sk_free(b); |
34 b = prev; | 35 b = prev; |
35 } | 36 } |
36 } | 37 } |
37 | 38 |
38 void SkVarAlloc::makeSpace(size_t bytes, unsigned flags) { | 39 void SkVarAlloc::makeSpace(size_t bytes, unsigned flags) { |
39 SkASSERT(SkIsAlignPtr(bytes)); | 40 SkASSERT(SkIsAlignPtr(bytes)); |
40 | 41 |
41 size_t alloc = 1<<(fLgMinSize++); | 42 size_t alloc = fSmallest; |
42 while (alloc < bytes + sizeof(Block)) { | 43 while (alloc < bytes + sizeof(Block)) { |
43 alloc *= 2; | 44 alloc *= 2; |
44 } | 45 } |
45 fBlock = Block::Alloc(fBlock, alloc, flags); | 46 fBlock = Block::Alloc(fBlock, alloc, flags); |
46 fByte = fBlock->data(); | 47 fByte = fBlock->data(); |
47 fRemaining = alloc - sizeof(Block); | 48 fLimit = fByte + alloc - sizeof(Block); |
| 49 fSmallest = SkToUInt(SkScalarTruncToInt(fSmallest * fGrowth)); |
48 | 50 |
49 #if defined(SK_BUILD_FOR_MAC) | 51 #if defined(SK_BUILD_FOR_MAC) |
50 SkASSERT(alloc == malloc_good_size(alloc)); | 52 SkASSERT(alloc == malloc_good_size(alloc)); |
51 #elif defined(SK_BUILD_FOR_LINUX) | 53 #elif defined(SK_BUILD_FOR_LINUX) |
52 SkASSERT(alloc == malloc_usable_size(fByte - sizeof(Block))); | 54 SkASSERT(alloc == malloc_usable_size(fByte - sizeof(Block))); |
53 #endif | 55 #endif |
54 } | 56 } |
OLD | NEW |