js/src/vm/SharedArrayObject.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/js/src/vm/SharedArrayObject.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,106 @@
     1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
     1.5 + * vim: set ts=8 sts=4 et sw=4 tw=99:
     1.6 + * This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +#ifndef vm_SharedArrayObject_h
    1.11 +#define vm_SharedArrayObject_h
    1.12 +
    1.13 +#include "mozilla/Atomics.h"
    1.14 +
    1.15 +#include "jsapi.h"
    1.16 +#include "jsobj.h"
    1.17 +#include "jstypes.h"
    1.18 +
    1.19 +#include "gc/Barrier.h"
    1.20 +#include "vm/ArrayBufferObject.h"
    1.21 +
    1.22 +typedef struct JSProperty JSProperty;
    1.23 +
    1.24 +namespace js {
    1.25 +
    1.26 +/*
    1.27 + * SharedArrayRawBuffer
    1.28 + *
    1.29 + * A bookkeeping object always stored immediately before the raw buffer.
    1.30 + * The buffer itself is mmap()'d and refcounted.
    1.31 + * SharedArrayBufferObjects and AsmJS code may hold references.
    1.32 + *
    1.33 + *           |<------ sizeof ------>|<- length ->|
    1.34 + *
    1.35 + *   | waste | SharedArrayRawBuffer | data array | waste |
    1.36 + */
    1.37 +class SharedArrayRawBuffer
    1.38 +{
    1.39 +  private:
    1.40 +    mozilla::Atomic<uint32_t, mozilla::ReleaseAcquire> refcount;
    1.41 +    uint32_t length;
    1.42 +
    1.43 +  protected:
    1.44 +    SharedArrayRawBuffer(uint8_t *buffer, uint32_t length)
    1.45 +      : refcount(1), length(length)
    1.46 +    {
    1.47 +        JS_ASSERT(buffer == dataPointer());
    1.48 +    }
    1.49 +
    1.50 +  public:
    1.51 +    static SharedArrayRawBuffer *New(uint32_t length);
    1.52 +
    1.53 +    inline uint8_t *dataPointer() const {
    1.54 +        return ((uint8_t *)this) + sizeof(SharedArrayRawBuffer);
    1.55 +    }
    1.56 +
    1.57 +    inline uint32_t byteLength() const {
    1.58 +        return length;
    1.59 +    }
    1.60 +
    1.61 +    void addReference();
    1.62 +    void dropReference();
    1.63 +};
    1.64 +
    1.65 +/*
    1.66 + * SharedArrayBufferObject
    1.67 + *
    1.68 + * When transferred to a WebWorker, the buffer is not neutered on the parent side,
    1.69 + * and both child and parent reference the same buffer.
    1.70 + */
    1.71 +class SharedArrayBufferObject : public ArrayBufferObject
    1.72 +{
    1.73 +    static bool byteLengthGetterImpl(JSContext *cx, CallArgs args);
    1.74 +
    1.75 +  public:
    1.76 +    static const Class class_;
    1.77 +    static const Class protoClass;
    1.78 +
    1.79 +    // Slot used for storing a pointer to the SharedArrayRawBuffer.
    1.80 +    static const uint8_t RAWBUF_SLOT = ArrayBufferObject::RESERVED_SLOTS;
    1.81 +
    1.82 +    static const uint8_t RESERVED_SLOTS = ArrayBufferObject::RESERVED_SLOTS + 1;
    1.83 +
    1.84 +    static bool class_constructor(JSContext *cx, unsigned argc, Value *vp);
    1.85 +
    1.86 +    // Create a SharedArrayBufferObject with a new SharedArrayRawBuffer.
    1.87 +    static JSObject *New(JSContext *cx, uint32_t length);
    1.88 +
    1.89 +    // Create a SharedArrayBufferObject using an existing SharedArrayRawBuffer.
    1.90 +    static JSObject *New(JSContext *cx, SharedArrayRawBuffer *buffer);
    1.91 +
    1.92 +    static bool byteLengthGetter(JSContext *cx, unsigned argc, Value *vp);
    1.93 +
    1.94 +    static void Finalize(FreeOp *fop, JSObject *obj);
    1.95 +
    1.96 +    void acceptRawBuffer(SharedArrayRawBuffer *buffer);
    1.97 +    void dropRawBuffer();
    1.98 +
    1.99 +    SharedArrayRawBuffer *rawBufferObject() const;
   1.100 +    uint8_t *dataPointer() const;
   1.101 +    uint32_t byteLength() const;
   1.102 +};
   1.103 +
   1.104 +bool
   1.105 +IsSharedArrayBuffer(HandleValue v);
   1.106 +
   1.107 +} // namespace js
   1.108 +
   1.109 +#endif // vm_SharedArrayObject_h

mercurial