jitcraft/UnsafeArray.h
changeset 0 c455ebb9a5dc
child 7 0478c27ba82c
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/jitcraft/UnsafeArray.h	Mon Jun 25 02:57:28 2012 -0700
     1.3 @@ -0,0 +1,58 @@
     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 +#ifndef _include_jc_unsafe_array_
    1.22 +#define _include_jc_unsafe_array_
    1.23 +
    1.24 +#include <assert.h>
    1.25 +
    1.26 +namespace jc {
    1.27 +
    1.28 +class UnsafeArray
    1.29 +{
    1.30 +  public:
    1.31 +    unsigned length() {
    1.32 +        return *(reinterpret_cast<unsigned *>(this) - 1);
    1.33 +    }
    1.34 +
    1.35 +    unsigned *uint32Vector() {
    1.36 +        return reinterpret_cast<unsigned *>(this);
    1.37 +    }
    1.38 +    int *int32Vector() {
    1.39 +        return reinterpret_cast<int *>(this);
    1.40 +    }
    1.41 +    int readInt32(size_t index) {
    1.42 +        assert(index < length());
    1.43 +        return int32Vector()[index];
    1.44 +    }
    1.45 +    void writeInt32(size_t index, int value) {
    1.46 +        assert(index < length());
    1.47 +        int32Vector()[index] = value;
    1.48 +    }
    1.49 +    UnsafeArray *indirect(size_t index) {
    1.50 +        assert(index < length());
    1.51 +        unsigned *address = uint32Vector() + index;
    1.52 +        return reinterpret_cast<UnsafeArray *>(reinterpret_cast<char *>(address) + *address);
    1.53 +    }
    1.54 +    void *allocationBase() {
    1.55 +        return reinterpret_cast<unsigned *>(this) - 1;
    1.56 +    }
    1.57 +};
    1.58 +
    1.59 +}
    1.60 +
    1.61 +#endif // _include_jc_unsafe_array_