Skia
2DGraphicsLibrary
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
SkChunkAlloc.h
1 
2 /*
3  * Copyright 2006 The Android Open Source Project
4  *
5  * Use of this source code is governed by a BSD-style license that can be
6  * found in the LICENSE file.
7  */
8 
9 
10 #ifndef SkChunkAlloc_DEFINED
11 #define SkChunkAlloc_DEFINED
12 
13 #include "SkTypes.h"
14 
15 class SkChunkAlloc : SkNoncopyable {
16 public:
17  SkChunkAlloc(size_t minSize);
18  ~SkChunkAlloc();
19 
24  void reset();
29  void rewind();
30 
31  enum AllocFailType {
32  kReturnNil_AllocFailType,
33  kThrow_AllocFailType
34  };
35 
43  void* alloc(size_t bytes, AllocFailType);
44 
48  void* allocThrow(size_t bytes) {
49  return this->alloc(bytes, kThrow_AllocFailType);
50  }
51 
58  size_t unalloc(void* ptr);
59 
60  size_t totalCapacity() const { return fTotalCapacity; }
61  size_t totalUsed() const { return fTotalUsed; }
62  SkDEBUGCODE(int blockCount() const { return fBlockCount; })
63  SkDEBUGCODE(size_t totalLost() const { return fTotalLost; })
64 
70  bool contains(const void* addr) const;
71 
72 private:
73  struct Block;
74 
75  Block* fBlock;
76  size_t fMinSize;
77  size_t fChunkSize;
78  size_t fTotalCapacity;
79  size_t fTotalUsed; // will be <= fTotalCapacity
80  SkDEBUGCODE(int fBlockCount;)
81  SkDEBUGCODE(size_t fTotalLost;) // will be <= fTotalCapacity
82 
83  Block* newBlock(size_t bytes, AllocFailType ftype);
84  Block* addBlockIfNecessary(size_t bytes, AllocFailType ftype);
85 
86  SkDEBUGCODE(void validate();)
87 };
88 
89 #endif
bool contains(const void *addr) const
Returns true if the specified address is within one of the chunks, and has at least 1-byte following ...
void * allocThrow(size_t bytes)
Shortcut for calling alloc with kThrow_AllocFailType.
Definition: SkChunkAlloc.h:48
void rewind()
Reset to 0 used bytes preserving as much memory as possible.
Definition: SkChunkAlloc.h:15
size_t unalloc(void *ptr)
Call this to unalloc the most-recently allocated ptr by alloc().
void * alloc(size_t bytes, AllocFailType)
Allocates a memory block of size bytes.
void reset()
Free up all allocated blocks.