diff -r 0c3bcd50e361 -r a350c2cc736a jitcraft/Spaces.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/jitcraft/Spaces.cpp Fri Jun 29 19:35:17 2012 -0700 @@ -0,0 +1,82 @@ +/* + * Copyright (C) 2012 David Anderson + * + * This file is part of JITCraft. + * + * JITCraft is free software: you can redistribute it and/or modify it under + * the terms of the GNU General Public License as published by the Free + * Software Foundation, either version 3 of the License, or (at your option) + * any later version. + * + * Foobar is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with + * JITCraft. If not, see http://www.gnu.org/licenses/. + */ +#include "Spaces.h" +#include "Platform.h" +#include "Spaces-inl.h" + +using namespace jc; + +HeapChunk * +HeapChunk::New(HeapChunk *prev, size_t size) +{ + OS::AlignableMapping map = + OS::MapAlignedMemory(size, HeapChunk::kAlignment, OS::Map_Commit); + + if (!map.base()) + return NULL; + + HeapChunk *chunk = reinterpret_cast(map.aligned()); + chunk->base_ = map.base(); + chunk->end_ = map.base() + map.size(); + chunk->cursor_ = chunk->first(); + if (prev) + prev->next_ = chunk; + + return chunk; +} + +void +HeapChunk::Destroy(HeapChunk *chunk) +{ + OS::UnmapAlignedMemory(chunk->base(), chunk->size()); +} + +OldSpace::OldSpace() + : first_(NULL), + last_(NULL) +{ +} + +bool +OldSpace::initialize() +{ + first_ = HeapChunk::New(first_, HeapChunk::kDefaultSize); + last_ = first_; + + return !!first_; +} + +Address +OldSpace::allocateSlow(size_t nbytes) +{ + HeapChunk *chunk = HeapChunk::New(last_, HeapChunk::kDefaultSize); + if (!chunk) + return NULL; + + return chunk->allocate(nbytes); +} + +OldSpace::~OldSpace() +{ + while (first_) { + HeapChunk *temp = first_->next(); + HeapChunk::Destroy(first_); + first_ = temp; + } +} +