OLD | NEW |
1 #ifndef SkVarAlloc_DEFINED | 1 #ifndef SkVarAlloc_DEFINED |
2 #define SkVarAlloc_DEFINED | 2 #define SkVarAlloc_DEFINED |
3 | 3 |
4 #include "SkTypes.h" | 4 #include "SkTypes.h" |
5 | 5 |
6 class SkVarAlloc : SkNoncopyable { | 6 class SkVarAlloc : SkNoncopyable { |
7 public: | 7 public: |
8 SkVarAlloc(); | 8 // SkVarAlloc will never allocate less than smallest bytes at a time. |
| 9 // When it allocates a new block, it will be at least growth times bigger th
an the last. |
| 10 SkVarAlloc(size_t smallest, float growth); |
9 ~SkVarAlloc(); | 11 ~SkVarAlloc(); |
10 | 12 |
11 // Returns contiguous bytes aligned at least for pointers. You may pass SK_
MALLOC_THROW, etc. | 13 // Returns contiguous bytes aligned at least for pointers. You may pass SK_
MALLOC_THROW, etc. |
12 char* alloc(size_t bytes, unsigned sk_malloc_flags) { | 14 char* alloc(size_t bytes, unsigned sk_malloc_flags) { |
13 bytes = SkAlignPtr(bytes); | 15 bytes = SkAlignPtr(bytes); |
14 | 16 |
15 if (bytes > fRemaining) { | 17 if (fByte + bytes > fLimit) { |
16 this->makeSpace(bytes, sk_malloc_flags); | 18 this->makeSpace(bytes, sk_malloc_flags); |
17 } | 19 } |
18 SkASSERT(bytes <= fRemaining); | 20 SkASSERT(fByte + bytes <= fLimit); |
19 | 21 |
20 char* ptr = fByte; | 22 char* ptr = fByte; |
21 fByte += bytes; | 23 fByte += bytes; |
22 fRemaining -= bytes; | |
23 return ptr; | 24 return ptr; |
24 } | 25 } |
25 | 26 |
26 private: | 27 private: |
27 void makeSpace(size_t bytes, unsigned flags); | 28 void makeSpace(size_t bytes, unsigned flags); |
28 | 29 |
29 char* fByte; | 30 char* fByte; |
30 unsigned fRemaining; | 31 const char* fLimit; |
31 unsigned fLgMinSize; | 32 |
| 33 unsigned fSmallest; |
| 34 const float fGrowth; |
32 | 35 |
33 struct Block; | 36 struct Block; |
34 Block* fBlock; | 37 Block* fBlock; |
35 }; | 38 }; |
36 | 39 |
37 #endif//SkVarAlloc_DEFINED | 40 #endif//SkVarAlloc_DEFINED |
OLD | NEW |