#ifndef SkFixedAlloc_DEFINED
#define SkFixedAlloc_DEFINED
+#include "SkRefCnt.h"
#include "SkTFitsIn.h"
#include "SkTypes.h"
#include <cstddef>
return new(objStart) T(std::forward<Args>(args)...);
}
+ template <typename T, typename... Args>
+ sk_sp<T> makeSkSp(Args&&... args) {
+ SkASSERT(SkTFitsIn<uint32_t>(sizeof(T)));
+
+ // The arena takes a ref for itself to account for the destructor. The sk_sp count can't
+ // become zero or the sk_sp will try to call free on the pointer.
+ return sk_sp<T>(SkRef(this->make<T>(std::forward<Args>(args)...)));
+ }
+
template <typename T>
T* makeArrayDefault(size_t count) {
T* array = (T*)this->commonArrayAlloc<T>(count);
#include "Test.h"
#include "SkArenaAlloc.h"
+#include "SkRefCnt.h"
namespace {
Node* start;
};
+ struct FooRefCnt : public SkRefCnt {
+ FooRefCnt() : x(-2), y(-3.0f) { created++; }
+ FooRefCnt(int X, float Y) : x(X), y(Y) { created++; }
+ ~FooRefCnt() { destroyed++; }
+
+ int x;
+ float y;
+ };
+
}
struct WithDtor {
REPORTER_ASSERT(r, created == 128);
REPORTER_ASSERT(r, destroyed == 128);
+
+ {
+ created = 0;
+ destroyed = 0;
+ char storage[64];
+ SkArenaAlloc arena{storage};
+
+ sk_sp<FooRefCnt> f = arena.makeSkSp<FooRefCnt>(4, 5.0f);
+ REPORTER_ASSERT(r, f->x == 4);
+ REPORTER_ASSERT(r, f->y == 5.0f);
+ REPORTER_ASSERT(r, created == 1);
+ REPORTER_ASSERT(r, destroyed == 0);
+ }
+ REPORTER_ASSERT(r, created == 1);
+ REPORTER_ASSERT(r, destroyed == 1);
}