aboutsummaryrefslogtreecommitdiffhomepage
path: root/include/core
diff options
context:
space:
mode:
authorGravatar bsalomon@google.com <bsalomon@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81>2011-09-02 22:06:24 +0000
committerGravatar bsalomon@google.com <bsalomon@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81>2011-09-02 22:06:24 +0000
commit7d4679a2e1aaa1953bc20d668135c517ee488c11 (patch)
tree399446d052eb73fb4d876e7582c8381ab6e2470e /include/core
parentf8cead5e08fe3298ba09da5a15c2d835747c567d (diff)
Rename existing nonpreserving reallocs to reset, add reset to SkAutoMalloc, use reset in GrBufferAllocPool
Review URL: http://codereview.appspot.com/4951058/ git-svn-id: http://skia.googlecode.com/svn/trunk@2215 2bbb7eff-a529-9590-31e7-b0007b416f81
Diffstat (limited to 'include/core')
-rw-r--r--include/core/SkTemplates.h4
-rw-r--r--include/core/SkTypes.h63
2 files changed, 52 insertions, 15 deletions
diff --git a/include/core/SkTemplates.h b/include/core/SkTemplates.h
index f79afff753..75a8b42497 100644
--- a/include/core/SkTemplates.h
+++ b/include/core/SkTemplates.h
@@ -181,7 +181,7 @@ public:
}
// doesn't preserve contents
- void realloc (size_t count) {
+ void reset (size_t count) {
sk_free(fPtr);
fPtr = fPtr = (T*)sk_malloc_flags(count * sizeof(T), SK_MALLOC_THROW | SK_MALLOC_TEMP);
}
@@ -225,7 +225,7 @@ public:
}
// doesn't preserve contents
- void realloc (size_t count) {
+ void reset(size_t count) {
if (fPtr != fTStorage) {
sk_free(fPtr);
}
diff --git a/include/core/SkTypes.h b/include/core/SkTypes.h
index d299223215..b38d4d016c 100644
--- a/include/core/SkTypes.h
+++ b/include/core/SkTypes.h
@@ -387,20 +387,57 @@ private:
SkAutoFree& operator=(const SkAutoFree&);
};
-class SkAutoMalloc : public SkAutoFree {
+/**
+ * Manage an allocated block of heap memory. This object is the sole manager of
+ * the lifetime of the block, so the caller must not call sk_free() or delete
+ * on the block.
+ */
+class SkAutoMalloc : public SkNoncopyable {
public:
- explicit SkAutoMalloc(size_t size)
- : SkAutoFree(sk_malloc_flags(size, SK_MALLOC_THROW | SK_MALLOC_TEMP)) {}
+ explicit SkAutoMalloc(size_t size = 0,
+ unsigned flags = SK_MALLOC_THROW | SK_MALLOC_TEMP) {
+ fPtr = size ? sk_malloc_flags(size, flags) : NULL;
+ fSize = size;
+ }
- SkAutoMalloc(size_t size, unsigned flags)
- : SkAutoFree(sk_malloc_flags(size, flags)) {}
- SkAutoMalloc() {}
+ ~SkAutoMalloc() {
+ sk_free(fPtr);
+ }
- void* alloc(size_t size,
- unsigned flags = (SK_MALLOC_THROW | SK_MALLOC_TEMP)) {
- sk_free(set(sk_malloc_flags(size, flags)));
- return get();
+ /**
+ * Reallocates the block to a new size. The ptr may or may not change.
+ */
+ void* reset(size_t size,
+ unsigned flags = (SK_MALLOC_THROW | SK_MALLOC_TEMP)) {
+ if (size != fSize) {
+ sk_free(fPtr);
+ fPtr = size ? sk_malloc_flags(size, flags) : NULL;
+ fSize = size;
+ }
+ return fPtr;
}
+
+ /**
+ * Releases the block back to the heap
+ */
+ void free() {
+ this->reset(0);
+ }
+
+ /**
+ * Return the allocated block.
+ */
+ void* get() { return fPtr; }
+ const void* get() const { return fPtr; }
+
+ /**
+ * Gets the size of the block in bytes
+ */
+ size_t getSize() const { return fSize; }
+
+private:
+ void* fPtr;
+ size_t fSize;
};
/**
@@ -413,7 +450,7 @@ template <size_t kSize> class SkAutoSMalloc : SkNoncopyable {
public:
/**
* Creates initially empty storage. get() returns a ptr, but it is to
- * a zero-byte allocation. Must call realloc(size) to return an allocated
+ * a zero-byte allocation. Must call reset(size) to return an allocated
* block.
*/
SkAutoSMalloc() {
@@ -427,7 +464,7 @@ public:
*/
explicit SkAutoSMalloc(size_t size) {
fPtr = fStorage;
- this->realloc(size);
+ this->reset(size);
}
/**
@@ -454,7 +491,7 @@ public:
* then the return block may be allocated locally, rather than from the
* heap.
*/
- void* realloc(size_t size) {
+ void* reset(size_t size) {
if (fPtr != (void*)fStorage) {
sk_free(fPtr);
}