blob: bb4ec8faecad9cb499d7156604f836c6549f8d0e [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@android.com8a1c16f2008-12-17 15:59:43 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2006 The Android Open Source Project
reed@android.com8a1c16f2008-12-17 15:59:43 +00004 *
epoger@google.comec3ed6a2011-07-28 14:26:00 +00005 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
reed@android.com8a1c16f2008-12-17 15:59:43 +00007 */
8
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
reed@android.com8a1c16f2008-12-17 15:59:43 +000010#ifndef SkChunkAlloc_DEFINED
11#define SkChunkAlloc_DEFINED
12
13#include "SkTypes.h"
14
15class SkChunkAlloc : SkNoncopyable {
16public:
17 SkChunkAlloc(size_t minSize);
18 ~SkChunkAlloc();
19
reed@google.comebd24962012-05-17 14:28:11 +000020 /**
21 * Free up all allocated blocks. This invalidates all returned
22 * pointers.
23 */
reed@android.com8a1c16f2008-12-17 15:59:43 +000024 void reset();
robertphillipsa9061de2015-02-27 08:31:57 -080025 /**
26 * Reset to 0 used bytes preserving as much memory as possible.
27 * This invalidates all returned pointers.
28 */
29 void rewind();
reed@android.com8a1c16f2008-12-17 15:59:43 +000030
reed@android.com8a1c16f2008-12-17 15:59:43 +000031 enum AllocFailType {
32 kReturnNil_AllocFailType,
33 kThrow_AllocFailType
34 };
rmistry@google.comfbfcd562012-08-23 18:09:54 +000035
milko.leporis401e77c2016-06-05 13:14:21 -070036 /**
37 * Allocates a memory block of size bytes.
38 * On success: returns a pointer to beginning of memory block that is
39 * 8 byte aligned. The content of allocated block is not initialized.
40 * On failure: calls abort() if called with kThrow_AllocFailType,
41 * otherwise returns NULL pointer.
42 */
reed@android.com8a1c16f2008-12-17 15:59:43 +000043 void* alloc(size_t bytes, AllocFailType);
milko.leporis401e77c2016-06-05 13:14:21 -070044
45 /**
46 * Shortcut for calling alloc with kThrow_AllocFailType.
47 */
reed@android.com8a1c16f2008-12-17 15:59:43 +000048 void* allocThrow(size_t bytes) {
49 return this->alloc(bytes, kThrow_AllocFailType);
50 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000051
reed@android.comaefd2bc2009-03-30 21:02:14 +000052 /** Call this to unalloc the most-recently allocated ptr by alloc(). On
53 success, the number of bytes freed is returned, or 0 if the block could
54 not be unallocated. This is a hint to the underlying allocator that
55 the previous allocation may be reused, but the implementation is free
56 to ignore this call (and return 0).
57 */
58 size_t unalloc(void* ptr);
rmistry@google.comfbfcd562012-08-23 18:09:54 +000059
reed@android.com8a1c16f2008-12-17 15:59:43 +000060 size_t totalCapacity() const { return fTotalCapacity; }
reed@google.com6757a3c2013-06-19 19:25:36 +000061 size_t totalUsed() const { return fTotalUsed; }
robertphillipsa9061de2015-02-27 08:31:57 -080062 SkDEBUGCODE(int blockCount() const { return fBlockCount; })
63 SkDEBUGCODE(size_t totalLost() const { return fTotalLost; })
reed@android.comf2b98d62010-12-20 18:26:13 +000064
65 /**
66 * Returns true if the specified address is within one of the chunks, and
67 * has at least 1-byte following the address (i.e. if addr points to the
68 * end of a chunk, then contains() will return false).
69 */
70 bool contains(const void* addr) const;
71
reed@android.com8a1c16f2008-12-17 15:59:43 +000072private:
73 struct Block;
reed@google.comebd24962012-05-17 14:28:11 +000074
reed@android.com8a1c16f2008-12-17 15:59:43 +000075 Block* fBlock;
76 size_t fMinSize;
reed@google.comebd24962012-05-17 14:28:11 +000077 size_t fChunkSize;
reed@android.com8a1c16f2008-12-17 15:59:43 +000078 size_t fTotalCapacity;
reed@google.com6757a3c2013-06-19 19:25:36 +000079 size_t fTotalUsed; // will be <= fTotalCapacity
robertphillipsa9061de2015-02-27 08:31:57 -080080 SkDEBUGCODE(int fBlockCount;)
81 SkDEBUGCODE(size_t fTotalLost;) // will be <= fTotalCapacity
reed@android.com8a1c16f2008-12-17 15:59:43 +000082
83 Block* newBlock(size_t bytes, AllocFailType ftype);
robertphillipsa9061de2015-02-27 08:31:57 -080084 Block* addBlockIfNecessary(size_t bytes, AllocFailType ftype);
85
86 SkDEBUGCODE(void validate();)
reed@android.com8a1c16f2008-12-17 15:59:43 +000087};
88
89#endif