this repo has no description
at trunk 58 lines 1.5 kB view raw
1/* Copyright (c) Facebook, Inc. and its affiliates. (http://www.facebook.com) */ 2#pragma once 3 4#include "globals.h" 5#include "objects.h" 6#include "space.h" 7#include "visitor.h" 8 9namespace py { 10 11class Heap { 12 public: 13 explicit Heap(word size); 14 ~Heap(); 15 16 // Returns true if allocation succeeded and writes output address + offset to 17 // *address. Returns false otherwise. 18 bool allocate(word size, uword* address_out); 19 bool allocateImmortal(word size, uword* address_out); 20 void collectGarbage(); 21 22 bool contains(uword address); 23 bool verify() { return verifySpace(space_) && verifySpace(immortal_); } 24 25 Space* space() { return space_; } 26 Space* immortal() { return immortal_; } 27 28 void setSpace(Space* new_space) { space_ = new_space; } 29 30 bool isImmortal(uword address) const { 31 return immortal_->isAllocated(address); 32 } 33 bool inHeap(uword address) const { 34 return space_->isAllocated(address) || isImmortal(address); 35 } 36 37 static int spaceOffset() { return offsetof(Heap, space_); }; 38 39 void visitAllObjects(HeapObjectVisitor* visitor); 40 41 private: 42 bool allocateRetry(word size, uword* address_out); 43 bool verifySpace(Space*); 44 void visitSpace(Space* space, HeapObjectVisitor* visitor); 45 46 Space* space_; 47 Space* immortal_; 48}; 49 50inline bool Heap::allocate(word size, uword* address_out) { 51 DCHECK(Utils::isAligned(size, kPointerSize), "request %ld not aligned", size); 52 if (UNLIKELY(!space_->allocate(size, address_out))) { 53 return allocateRetry(size, address_out); 54 } 55 return true; 56} 57 58} // namespace py