jitcraft/UnsafeArray.h
author David Anderson <dvander@alliedmods.net>
Tue Jun 26 00:35:15 2012 -0700 (2012-06-26)
changeset 2 46fc53b8fd2f
parent 0 c455ebb9a5dc
child 7 0478c27ba82c
permissions -rw-r--r--
Added support for enums.
dvander@0
     1
/*
dvander@0
     2
 * Copyright (C) 2012 David Anderson
dvander@0
     3
 *
dvander@0
     4
 * This file is part of JITCraft.
dvander@0
     5
 *
dvander@0
     6
 * JITCraft is free software: you can redistribute it and/or modify it under
dvander@0
     7
 * the terms of the GNU General Public License as published by the Free
dvander@0
     8
 * Software Foundation, either version 3 of the License, or (at your option)
dvander@0
     9
 * any later version.
dvander@0
    10
 * 
dvander@0
    11
 * Foobar is distributed in the hope that it will be useful, but WITHOUT ANY
dvander@0
    12
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
dvander@0
    13
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
dvander@0
    14
 *
dvander@0
    15
 * You should have received a copy of the GNU General Public License along with
dvander@0
    16
 * JITCraft. If not, see http://www.gnu.org/licenses/.
dvander@0
    17
 */
dvander@0
    18
#ifndef _include_jc_unsafe_array_
dvander@0
    19
#define _include_jc_unsafe_array_
dvander@0
    20
dvander@0
    21
#include <assert.h>
dvander@0
    22
dvander@0
    23
namespace jc {
dvander@0
    24
dvander@0
    25
class UnsafeArray
dvander@0
    26
{
dvander@0
    27
  public:
dvander@0
    28
    unsigned length() {
dvander@0
    29
        return *(reinterpret_cast<unsigned *>(this) - 1);
dvander@0
    30
    }
dvander@0
    31
dvander@0
    32
    unsigned *uint32Vector() {
dvander@0
    33
        return reinterpret_cast<unsigned *>(this);
dvander@0
    34
    }
dvander@0
    35
    int *int32Vector() {
dvander@0
    36
        return reinterpret_cast<int *>(this);
dvander@0
    37
    }
dvander@0
    38
    int readInt32(size_t index) {
dvander@0
    39
        assert(index < length());
dvander@0
    40
        return int32Vector()[index];
dvander@0
    41
    }
dvander@0
    42
    void writeInt32(size_t index, int value) {
dvander@0
    43
        assert(index < length());
dvander@0
    44
        int32Vector()[index] = value;
dvander@0
    45
    }
dvander@0
    46
    UnsafeArray *indirect(size_t index) {
dvander@0
    47
        assert(index < length());
dvander@0
    48
        unsigned *address = uint32Vector() + index;
dvander@0
    49
        return reinterpret_cast<UnsafeArray *>(reinterpret_cast<char *>(address) + *address);
dvander@0
    50
    }
dvander@0
    51
    void *allocationBase() {
dvander@0
    52
        return reinterpret_cast<unsigned *>(this) - 1;
dvander@0
    53
    }
dvander@0
    54
};
dvander@0
    55
dvander@0
    56
}
dvander@0
    57
dvander@0
    58
#endif // _include_jc_unsafe_array_