jitcraft/Spaces.cpp
changeset 11 a350c2cc736a
child 12 75e385f68b7e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/jitcraft/Spaces.cpp	Fri Jun 29 19:35:17 2012 -0700
     1.3 @@ -0,0 +1,82 @@
     1.4 +/*
     1.5 + * Copyright (C) 2012 David Anderson
     1.6 + *
     1.7 + * This file is part of JITCraft.
     1.8 + *
     1.9 + * JITCraft is free software: you can redistribute it and/or modify it under
    1.10 + * the terms of the GNU General Public License as published by the Free
    1.11 + * Software Foundation, either version 3 of the License, or (at your option)
    1.12 + * any later version.
    1.13 + * 
    1.14 + * Foobar is distributed in the hope that it will be useful, but WITHOUT ANY
    1.15 + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    1.16 + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License along with
    1.19 + * JITCraft. If not, see http://www.gnu.org/licenses/.
    1.20 + */
    1.21 +#include "Spaces.h"
    1.22 +#include "Platform.h"
    1.23 +#include "Spaces-inl.h"
    1.24 +
    1.25 +using namespace jc;
    1.26 +
    1.27 +HeapChunk *
    1.28 +HeapChunk::New(HeapChunk *prev, size_t size)
    1.29 +{
    1.30 +    OS::AlignableMapping map = 
    1.31 +        OS::MapAlignedMemory(size, HeapChunk::kAlignment, OS::Map_Commit);
    1.32 +
    1.33 +    if (!map.base())
    1.34 +        return NULL;
    1.35 +
    1.36 +    HeapChunk *chunk = reinterpret_cast<HeapChunk *>(map.aligned());
    1.37 +    chunk->base_ = map.base();
    1.38 +    chunk->end_ = map.base() + map.size();
    1.39 +    chunk->cursor_ = chunk->first();
    1.40 +    if (prev)
    1.41 +        prev->next_ = chunk;
    1.42 +
    1.43 +    return chunk;
    1.44 +}
    1.45 +
    1.46 +void
    1.47 +HeapChunk::Destroy(HeapChunk *chunk)
    1.48 +{
    1.49 +    OS::UnmapAlignedMemory(chunk->base(), chunk->size());
    1.50 +}
    1.51 +
    1.52 +OldSpace::OldSpace()
    1.53 +  : first_(NULL),
    1.54 +    last_(NULL)
    1.55 +{
    1.56 +}
    1.57 +
    1.58 +bool
    1.59 +OldSpace::initialize()
    1.60 +{
    1.61 +    first_ = HeapChunk::New(first_, HeapChunk::kDefaultSize);
    1.62 +    last_ = first_;
    1.63 +
    1.64 +    return !!first_;
    1.65 +}
    1.66 +
    1.67 +Address
    1.68 +OldSpace::allocateSlow(size_t nbytes)
    1.69 +{
    1.70 +    HeapChunk *chunk = HeapChunk::New(last_, HeapChunk::kDefaultSize);
    1.71 +    if (!chunk)
    1.72 +        return NULL;
    1.73 +
    1.74 +    return chunk->allocate(nbytes);
    1.75 +}
    1.76 +
    1.77 +OldSpace::~OldSpace()
    1.78 +{
    1.79 +    while (first_) {
    1.80 +        HeapChunk *temp = first_->next();
    1.81 +        HeapChunk::Destroy(first_);
    1.82 +        first_ = temp;
    1.83 +    }
    1.84 +}
    1.85 +