michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- michael@0: * vim: set ts=8 sts=4 et sw=4 tw=99: michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef vm_SharedArrayObject_h michael@0: #define vm_SharedArrayObject_h michael@0: michael@0: #include "mozilla/Atomics.h" michael@0: michael@0: #include "jsapi.h" michael@0: #include "jsobj.h" michael@0: #include "jstypes.h" michael@0: michael@0: #include "gc/Barrier.h" michael@0: #include "vm/ArrayBufferObject.h" michael@0: michael@0: typedef struct JSProperty JSProperty; michael@0: michael@0: namespace js { michael@0: michael@0: /* michael@0: * SharedArrayRawBuffer michael@0: * michael@0: * A bookkeeping object always stored immediately before the raw buffer. michael@0: * The buffer itself is mmap()'d and refcounted. michael@0: * SharedArrayBufferObjects and AsmJS code may hold references. michael@0: * michael@0: * |<------ sizeof ------>|<- length ->| michael@0: * michael@0: * | waste | SharedArrayRawBuffer | data array | waste | michael@0: */ michael@0: class SharedArrayRawBuffer michael@0: { michael@0: private: michael@0: mozilla::Atomic refcount; michael@0: uint32_t length; michael@0: michael@0: protected: michael@0: SharedArrayRawBuffer(uint8_t *buffer, uint32_t length) michael@0: : refcount(1), length(length) michael@0: { michael@0: JS_ASSERT(buffer == dataPointer()); michael@0: } michael@0: michael@0: public: michael@0: static SharedArrayRawBuffer *New(uint32_t length); michael@0: michael@0: inline uint8_t *dataPointer() const { michael@0: return ((uint8_t *)this) + sizeof(SharedArrayRawBuffer); michael@0: } michael@0: michael@0: inline uint32_t byteLength() const { michael@0: return length; michael@0: } michael@0: michael@0: void addReference(); michael@0: void dropReference(); michael@0: }; michael@0: michael@0: /* michael@0: * SharedArrayBufferObject michael@0: * michael@0: * When transferred to a WebWorker, the buffer is not neutered on the parent side, michael@0: * and both child and parent reference the same buffer. michael@0: */ michael@0: class SharedArrayBufferObject : public ArrayBufferObject michael@0: { michael@0: static bool byteLengthGetterImpl(JSContext *cx, CallArgs args); michael@0: michael@0: public: michael@0: static const Class class_; michael@0: static const Class protoClass; michael@0: michael@0: // Slot used for storing a pointer to the SharedArrayRawBuffer. michael@0: static const uint8_t RAWBUF_SLOT = ArrayBufferObject::RESERVED_SLOTS; michael@0: michael@0: static const uint8_t RESERVED_SLOTS = ArrayBufferObject::RESERVED_SLOTS + 1; michael@0: michael@0: static bool class_constructor(JSContext *cx, unsigned argc, Value *vp); michael@0: michael@0: // Create a SharedArrayBufferObject with a new SharedArrayRawBuffer. michael@0: static JSObject *New(JSContext *cx, uint32_t length); michael@0: michael@0: // Create a SharedArrayBufferObject using an existing SharedArrayRawBuffer. michael@0: static JSObject *New(JSContext *cx, SharedArrayRawBuffer *buffer); michael@0: michael@0: static bool byteLengthGetter(JSContext *cx, unsigned argc, Value *vp); michael@0: michael@0: static void Finalize(FreeOp *fop, JSObject *obj); michael@0: michael@0: void acceptRawBuffer(SharedArrayRawBuffer *buffer); michael@0: void dropRawBuffer(); michael@0: michael@0: SharedArrayRawBuffer *rawBufferObject() const; michael@0: uint8_t *dataPointer() const; michael@0: uint32_t byteLength() const; michael@0: }; michael@0: michael@0: bool michael@0: IsSharedArrayBuffer(HandleValue v); michael@0: michael@0: } // namespace js michael@0: michael@0: #endif // vm_SharedArrayObject_h