Skip to content
Snippets Groups Projects
Commit 11011743 authored by Jon Coppeard's avatar Jon Coppeard
Browse files

Bug 1395509 - Add APIs to handle memory accounting while initializing object...

Bug 1395509 - Add APIs to handle memory accounting while initializing object pointers to malloc memory r=sfink?

Depends on D33313

Differential Revision: https://phabricator.services.mozilla.com/D33314

--HG--
extra : moz-landing-system : lando
parent adb44ba6
No related branches found
No related tags found
No related merge requests found
......@@ -50,7 +50,11 @@ class FreeOp : public JSFreeOp {
void free_(void* p) { js_free(p); }
// Free memory that was associated with a GC thing using js::AddCellMemory.
// Free memory associated with a GC thing and update the memory accounting.
//
// The memory should have been associated with the GC thing using
// js::InitReservedSlot or js::InitObjectPrivate, or possibly
// js::AddCellMemory.
void free_(gc::Cell* cell, void* p, size_t nbytes, MemoryUse use);
// Queue an allocation to be freed when the FreeOp is destroyed.
......@@ -82,16 +86,23 @@ class FreeOp : public JSFreeOp {
}
}
// Delete a C++ object that was associated with a GC thing using
// js::AddCellMemory. The size is determined by the type T.
// Delete a C++ object that associated with a GC thing and update the memory
// accounting. The size is determined by the type T.
//
// The memory should have been associated with the GC thing using
// js::InitReservedSlot or js::InitObjectPrivate, or possibly
// js::AddCellMemory.
template <class T>
void delete_(gc::Cell* cell, T* p, MemoryUse use) {
delete_(cell, p, sizeof(T), use);
}
// Delete a C++ object that was associated with a GC thing using
// js::AddCellMemory. The size of the allocation is passed in to allow for
// allocations with trailing data after the object.
// Delete a C++ object that associated with a GC thing and update the memory
// accounting.
//
// The memory should have been associated with the GC thing using
// js::InitReservedSlot or js::InitObjectPrivate, or possibly
// js::AddCellMemory.
template <class T>
void delete_(gc::Cell* cell, T* p, size_t nbytes, MemoryUse use) {
if (p) {
......
......@@ -268,11 +268,15 @@ class ZoneAllocPolicy : public MallocProvider<ZoneAllocPolicy> {
void decMemory(size_t nbytes) { zone_->decPolicyMemory(this, nbytes); }
};
// Convenience functions for memory accounting on the zone.
// Functions for memory accounting on the zone.
// Associate malloc memory with a GC thing. This call must be matched by a
// Associate malloc memory with a GC thing. This call should be matched by a
// following call to RemoveCellMemory with the same size and use. The total
// amount of malloc memory associated with a zone is used to trigger GC.
//
// You should use InitReservedSlot / InitObjectPrivate in preference to this
// where possible.
inline void AddCellMemory(gc::TenuredCell* cell, size_t nbytes, MemoryUse use) {
if (nbytes) {
ZoneAllocator::from(cell->zone())->addCellMemory(cell, nbytes, use);
......@@ -284,8 +288,9 @@ inline void AddCellMemory(gc::Cell* cell, size_t nbytes, MemoryUse use) {
}
}
// Remove association between malloc memory and a GC thing. This call must
// Remove association between malloc memory and a GC thing. This call should
// follow a call to AddCellMemory with the same size and use.
inline void RemoveCellMemory(gc::TenuredCell* cell, size_t nbytes,
MemoryUse use) {
if (nbytes) {
......@@ -299,6 +304,39 @@ inline void RemoveCellMemory(gc::Cell* cell, size_t nbytes, MemoryUse use) {
}
}
// Initialize an object's reserved slot with a private value pointing to
// malloc-allocated memory and associate the memory with the object.
//
// This call should be matched with a call to FreeOp::free_/delete_ in the
// object's finalizer to free the memory and update the memory accounting.
inline void InitReservedSlot(NativeObject* obj, uint32_t slot, void* ptr,
size_t nbytes, MemoryUse use) {
AddCellMemory(obj, nbytes, use);
obj->initReservedSlot(slot, PrivateValue(ptr));
}
template <typename T>
inline void InitReservedSlot(NativeObject* obj, uint32_t slot, T* ptr,
MemoryUse use) {
InitReservedSlot(obj, slot, ptr, sizeof(T), use);
}
// Initialize an object's private slot with a pointer to malloc-allocated memory
// and associate the memory with the object.
//
// This call should be matched with a call to FreeOp::free_/delete_ in the
// object's finalizer to free the memory and update the memory accounting.
inline void InitObjectPrivate(NativeObject* obj, void* ptr, size_t nbytes,
MemoryUse use) {
AddCellMemory(obj, nbytes, use);
obj->initPrivate(ptr);
}
template <typename T>
inline void InitObjectPrivate(NativeObject* obj, T* ptr, MemoryUse use) {
InitObjectPrivate(obj, ptr, sizeof(T), use);
}
} // namespace js
#endif // gc_ZoneAllocator_h
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment