Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(305)

Side by Side Diff: src/core/SkVarAlloc.cpp

Issue 674263002: SkVarAlloc (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: linux too Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/core/SkVarAlloc.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 #include "SkVarAlloc.h"
2
3 // We use non-standard malloc diagnostic methods to make sure our allocations ar e sized well.
4 #if defined(SK_BUILD_FOR_MAC)
5 #include <malloc/malloc.h>
6 #elif defined(SK_BUILD_FOR_LINUX)
7 #include <malloc.h>
8 #endif
9
10 struct SkVarAlloc::Block {
11 Block* prev;
12 char* data() { return (char*)(this + 1); }
13
14 static Block* Alloc(Block* prev, size_t size, unsigned flags) {
15 SkASSERT(size >= sizeof(Block));
16 Block* b = (Block*)sk_malloc_flags(size, flags);
17 b->prev = prev;
18 return b;
19 }
20 };
21
22 SkVarAlloc::SkVarAlloc(size_t smallest, float growth)
23 : fByte(NULL)
24 , fLimit(NULL)
25 , fSmallest(SkToUInt(smallest))
26 , fGrowth(growth)
27 , fBlock(NULL) {}
28
29 SkVarAlloc::~SkVarAlloc() {
30 Block* b = fBlock;
31 while (b) {
32 Block* prev = b->prev;
33 sk_free(b);
34 b = prev;
35 }
36 }
37
38 void SkVarAlloc::makeSpace(size_t bytes, unsigned flags) {
39 SkASSERT(SkIsAlignPtr(bytes));
40
41 size_t alloc = fSmallest;
42 while (alloc < bytes + sizeof(Block)) {
43 alloc *= 2;
44 }
45 fBlock = Block::Alloc(fBlock, alloc, flags);
46 fByte = fBlock->data();
47 fLimit = fByte + alloc - sizeof(Block);
48 fSmallest = SkToUInt(fSmallest * fGrowth);
reed1 2014/11/12 22:12:25 I think you may get warnings about float->int conv
mtklein 2014/11/12 22:23:39 Now explicitly truncating.
49
50 #if defined(SK_BUILD_FOR_MAC)
51 SkASSERT(alloc == malloc_good_size(alloc));
52 #elif defined(SK_BUILD_FOR_LINUX)
53 SkASSERT(alloc == malloc_usable_size(fByte - sizeof(Block)));
54 #endif
55 }
OLDNEW
« no previous file with comments | « src/core/SkVarAlloc.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698