зеркало из https://github.com/mozilla/gecko-dev.git
Correct comments; thanks for pointing this out, dp!
This commit is contained in:
Родитель
8b75779a00
Коммит
d4df523a78
|
@ -55,11 +55,19 @@
|
||||||
public:
|
public:
|
||||||
// Implement placement new & delete operators that will
|
// Implement placement new & delete operators that will
|
||||||
// use the fixed size allocator.
|
// use the fixed size allocator.
|
||||||
static operator new(size_t aSize, nsFixedSizeAllocator& aAllocator) {
|
static Foo *
|
||||||
return aAllocator.Alloc(aSize); }
|
Create(nsFixedSizeAllocator &aAllocator)
|
||||||
|
{
|
||||||
|
void *place = aAllocator.Alloc(sizeof(Foo));
|
||||||
|
return place ? ::new (place) Foo() : nsnull;
|
||||||
|
}
|
||||||
|
|
||||||
static operator delete(void* aPtr, size_t aSize) {
|
static void
|
||||||
nsFixedSizeAllocator::Free(aPtr, aSize); }
|
Destroy(nsFixedSizeAllocator &aAllocator, Foo *aFoo)
|
||||||
|
{
|
||||||
|
aFoo->~Foo();
|
||||||
|
aAllocator.Free(aFoo, sizeof(Foo));
|
||||||
|
}
|
||||||
|
|
||||||
// ctor & dtor
|
// ctor & dtor
|
||||||
Foo() {}
|
Foo() {}
|
||||||
|
@ -96,18 +104,18 @@
|
||||||
// Now we can use the pool.
|
// Now we can use the pool.
|
||||||
|
|
||||||
// Create a new Foo object using the pool:
|
// Create a new Foo object using the pool:
|
||||||
Foo* foo = new (pool) Foo();
|
Foo* foo = Foo::Create(pool);
|
||||||
if (! foo) {
|
if (! foo) {
|
||||||
// uh oh, out of memory!
|
// uh oh, out of memory!
|
||||||
}
|
}
|
||||||
|
|
||||||
// Delete the object. The memory used by `foo' is recycled in
|
// Delete the object. The memory used by `foo' is recycled in
|
||||||
// the pool, and placed in a freelist
|
// the pool, and placed in a freelist
|
||||||
delete foo;
|
Foo::Destroy(foo);
|
||||||
|
|
||||||
// Create another foo: this one will be allocated from the
|
// Create another foo: this one will be allocated from the
|
||||||
// free-list
|
// free-list
|
||||||
foo = new (pool) foo();
|
foo = Foo::Create(pool);
|
||||||
|
|
||||||
// When pool is destroyed, all of its memory is automatically
|
// When pool is destroyed, all of its memory is automatically
|
||||||
// freed. N.B. it will *not* call your objects' destructors! In
|
// freed. N.B. it will *not* call your objects' destructors! In
|
||||||
|
|
Загрузка…
Ссылка в новой задаче