2011-07-28 14:26:00 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
/*
|
2011-07-28 14:26:00 +00:00
|
|
|
* Copyright 2006 The Android Open Source Project
|
2008-12-17 15:59:43 +00:00
|
|
|
*
|
2011-07-28 14:26:00 +00:00
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
2008-12-17 15:59:43 +00:00
|
|
|
*/
|
|
|
|
|
2011-07-28 14:26:00 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
#ifndef SkChunkAlloc_DEFINED
|
|
|
|
#define SkChunkAlloc_DEFINED
|
|
|
|
|
|
|
|
#include "SkTypes.h"
|
|
|
|
|
|
|
|
class SkChunkAlloc : SkNoncopyable {
|
|
|
|
public:
|
|
|
|
SkChunkAlloc(size_t minSize);
|
|
|
|
~SkChunkAlloc();
|
|
|
|
|
2012-05-17 14:28:11 +00:00
|
|
|
/**
|
|
|
|
* Free up all allocated blocks. This invalidates all returned
|
|
|
|
* pointers.
|
|
|
|
*/
|
2008-12-17 15:59:43 +00:00
|
|
|
void reset();
|
2015-02-27 16:31:57 +00:00
|
|
|
/**
|
|
|
|
* Reset to 0 used bytes preserving as much memory as possible.
|
|
|
|
* This invalidates all returned pointers.
|
|
|
|
*/
|
|
|
|
void rewind();
|
2008-12-17 15:59:43 +00:00
|
|
|
|
|
|
|
enum AllocFailType {
|
|
|
|
kReturnNil_AllocFailType,
|
|
|
|
kThrow_AllocFailType
|
|
|
|
};
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2016-06-05 20:14:21 +00:00
|
|
|
/**
|
|
|
|
* Allocates a memory block of size bytes.
|
|
|
|
* On success: returns a pointer to beginning of memory block that is
|
|
|
|
* 8 byte aligned. The content of allocated block is not initialized.
|
|
|
|
* On failure: calls abort() if called with kThrow_AllocFailType,
|
|
|
|
* otherwise returns NULL pointer.
|
|
|
|
*/
|
2008-12-17 15:59:43 +00:00
|
|
|
void* alloc(size_t bytes, AllocFailType);
|
2016-06-05 20:14:21 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Shortcut for calling alloc with kThrow_AllocFailType.
|
|
|
|
*/
|
2008-12-17 15:59:43 +00:00
|
|
|
void* allocThrow(size_t bytes) {
|
|
|
|
return this->alloc(bytes, kThrow_AllocFailType);
|
|
|
|
}
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2009-03-30 21:02:14 +00:00
|
|
|
/** Call this to unalloc the most-recently allocated ptr by alloc(). On
|
|
|
|
success, the number of bytes freed is returned, or 0 if the block could
|
|
|
|
not be unallocated. This is a hint to the underlying allocator that
|
|
|
|
the previous allocation may be reused, but the implementation is free
|
|
|
|
to ignore this call (and return 0).
|
|
|
|
*/
|
|
|
|
size_t unalloc(void* ptr);
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
size_t totalCapacity() const { return fTotalCapacity; }
|
2013-06-19 19:25:36 +00:00
|
|
|
size_t totalUsed() const { return fTotalUsed; }
|
2015-02-27 16:31:57 +00:00
|
|
|
SkDEBUGCODE(int blockCount() const { return fBlockCount; })
|
|
|
|
SkDEBUGCODE(size_t totalLost() const { return fTotalLost; })
|
2010-12-20 18:26:13 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if the specified address is within one of the chunks, and
|
|
|
|
* has at least 1-byte following the address (i.e. if addr points to the
|
|
|
|
* end of a chunk, then contains() will return false).
|
|
|
|
*/
|
|
|
|
bool contains(const void* addr) const;
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
private:
|
|
|
|
struct Block;
|
2012-05-17 14:28:11 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
Block* fBlock;
|
|
|
|
size_t fMinSize;
|
2012-05-17 14:28:11 +00:00
|
|
|
size_t fChunkSize;
|
2008-12-17 15:59:43 +00:00
|
|
|
size_t fTotalCapacity;
|
2013-06-19 19:25:36 +00:00
|
|
|
size_t fTotalUsed; // will be <= fTotalCapacity
|
2015-02-27 16:31:57 +00:00
|
|
|
SkDEBUGCODE(int fBlockCount;)
|
|
|
|
SkDEBUGCODE(size_t fTotalLost;) // will be <= fTotalCapacity
|
2008-12-17 15:59:43 +00:00
|
|
|
|
|
|
|
Block* newBlock(size_t bytes, AllocFailType ftype);
|
2015-02-27 16:31:57 +00:00
|
|
|
Block* addBlockIfNecessary(size_t bytes, AllocFailType ftype);
|
|
|
|
|
|
|
|
SkDEBUGCODE(void validate();)
|
2008-12-17 15:59:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|