src/Reference.h
author Asher Baker <asherkin@gmail.com>
Mon Jun 10 01:04:43 2013 +0100 (2013-06-10)
changeset 263 ba85a47ee414
parent 258 241d082d6d89
permissions -rw-r--r--
Normalise line endings.
asherkin@263
     1
/*
asherkin@263
     2
 * Copyright (C) 2012 David Anderson
asherkin@263
     3
 *
asherkin@263
     4
 * This file is part of SourcePawn.
asherkin@263
     5
 *
asherkin@263
     6
 * SourcePawn is free software: you can redistribute it and/or modify it under
asherkin@263
     7
 * the terms of the GNU General Public License as published by the Free
asherkin@263
     8
 * Software Foundation, either version 3 of the License, or (at your option)
asherkin@263
     9
 * any later version.
asherkin@263
    10
 * 
asherkin@263
    11
 * SourcePawn is distributed in the hope that it will be useful, but WITHOUT ANY
asherkin@263
    12
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
asherkin@263
    13
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
asherkin@263
    14
 *
asherkin@263
    15
 * You should have received a copy of the GNU General Public License along with
asherkin@263
    16
 * SourcePawn. If not, see http://www.gnu.org/licenses/.
asherkin@263
    17
 */
asherkin@263
    18
#ifndef _include_jitcraft_reference_h_
asherkin@263
    19
#define _include_jitcraft_reference_h_
asherkin@263
    20
asherkin@263
    21
#include "Heap.h"
asherkin@263
    22
#include "Barriers.h"
asherkin@263
    23
#include "Stack.h"
asherkin@263
    24
asherkin@263
    25
namespace ke {
asherkin@263
    26
asherkin@263
    27
class Array;
asherkin@263
    28
class Struct;
asherkin@263
    29
asherkin@263
    30
// A Reference is a typed pointer to another location in memory.
asherkin@263
    31
class Reference : public Object
asherkin@263
    32
{
asherkin@263
    33
    void *location_;
asherkin@263
    34
    Barriered<Object> base_;
asherkin@263
    35
asherkin@263
    36
    // We do not compute references to objects, so this does not need to be
asherkin@263
    37
    // traced.
asherkin@263
    38
    LocalSlot slot_;
asherkin@263
    39
asherkin@263
    40
  public:
asherkin@263
    41
    static Reference *NewStack(Zone *zone, void *location, Heap::Tenure tenure = Heap::Tenure_Default);
asherkin@263
    42
    static Reference *NewStack(Zone *zone, const LocalSlot &slot, Heap::Tenure tenure = Heap::Tenure_Default);
asherkin@263
    43
asherkin@263
    44
    int getInt() {
asherkin@263
    45
        return *reinterpret_cast<int *>(location_);
asherkin@263
    46
    }
asherkin@263
    47
    void setInt(int i) {
asherkin@263
    48
        *reinterpret_cast<int *>(location_) = i;
asherkin@263
    49
    }
asherkin@263
    50
    float getFloat() {
asherkin@263
    51
        return *reinterpret_cast<float *>(location_);
asherkin@263
    52
    }
asherkin@263
    53
    void setFloat(float f) {
asherkin@263
    54
        *reinterpret_cast<float *>(location_) = f;
asherkin@263
    55
    }
asherkin@263
    56
asherkin@263
    57
    // WARNING! This should only be for Native calls.
asherkin@263
    58
    void *location() {
asherkin@263
    59
        return location_;
asherkin@263
    60
    }
asherkin@263
    61
asherkin@263
    62
    // Begin GC Descriptor.
asherkin@263
    63
    static inline size_t offsetOfBase() {
asherkin@263
    64
        return offsetof(Reference, base_);
asherkin@263
    65
    }
asherkin@263
    66
    // End GC Descriptor.
asherkin@263
    67
asherkin@263
    68
    static inline Reference *cast(Object *obj) {
asherkin@263
    69
        assert(!obj || obj->is(MapKind_Reference));
asherkin@263
    70
        return reinterpret_cast<Reference *>(obj);
asherkin@263
    71
    }
asherkin@263
    72
};
asherkin@263
    73
asherkin@263
    74
}
asherkin@263
    75
asherkin@263
    76
#endif // _include_jitcraft_reference_h_