|
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- |
|
2 * vim: set ts=8 sts=4 et sw=4 tw=99: |
|
3 * This Source Code Form is subject to the terms of the Mozilla Public |
|
4 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
6 |
|
7 #ifndef vm_SharedArrayObject_h |
|
8 #define vm_SharedArrayObject_h |
|
9 |
|
10 #include "mozilla/Atomics.h" |
|
11 |
|
12 #include "jsapi.h" |
|
13 #include "jsobj.h" |
|
14 #include "jstypes.h" |
|
15 |
|
16 #include "gc/Barrier.h" |
|
17 #include "vm/ArrayBufferObject.h" |
|
18 |
|
19 typedef struct JSProperty JSProperty; |
|
20 |
|
21 namespace js { |
|
22 |
|
23 /* |
|
24 * SharedArrayRawBuffer |
|
25 * |
|
26 * A bookkeeping object always stored immediately before the raw buffer. |
|
27 * The buffer itself is mmap()'d and refcounted. |
|
28 * SharedArrayBufferObjects and AsmJS code may hold references. |
|
29 * |
|
30 * |<------ sizeof ------>|<- length ->| |
|
31 * |
|
32 * | waste | SharedArrayRawBuffer | data array | waste | |
|
33 */ |
|
34 class SharedArrayRawBuffer |
|
35 { |
|
36 private: |
|
37 mozilla::Atomic<uint32_t, mozilla::ReleaseAcquire> refcount; |
|
38 uint32_t length; |
|
39 |
|
40 protected: |
|
41 SharedArrayRawBuffer(uint8_t *buffer, uint32_t length) |
|
42 : refcount(1), length(length) |
|
43 { |
|
44 JS_ASSERT(buffer == dataPointer()); |
|
45 } |
|
46 |
|
47 public: |
|
48 static SharedArrayRawBuffer *New(uint32_t length); |
|
49 |
|
50 inline uint8_t *dataPointer() const { |
|
51 return ((uint8_t *)this) + sizeof(SharedArrayRawBuffer); |
|
52 } |
|
53 |
|
54 inline uint32_t byteLength() const { |
|
55 return length; |
|
56 } |
|
57 |
|
58 void addReference(); |
|
59 void dropReference(); |
|
60 }; |
|
61 |
|
62 /* |
|
63 * SharedArrayBufferObject |
|
64 * |
|
65 * When transferred to a WebWorker, the buffer is not neutered on the parent side, |
|
66 * and both child and parent reference the same buffer. |
|
67 */ |
|
68 class SharedArrayBufferObject : public ArrayBufferObject |
|
69 { |
|
70 static bool byteLengthGetterImpl(JSContext *cx, CallArgs args); |
|
71 |
|
72 public: |
|
73 static const Class class_; |
|
74 static const Class protoClass; |
|
75 |
|
76 // Slot used for storing a pointer to the SharedArrayRawBuffer. |
|
77 static const uint8_t RAWBUF_SLOT = ArrayBufferObject::RESERVED_SLOTS; |
|
78 |
|
79 static const uint8_t RESERVED_SLOTS = ArrayBufferObject::RESERVED_SLOTS + 1; |
|
80 |
|
81 static bool class_constructor(JSContext *cx, unsigned argc, Value *vp); |
|
82 |
|
83 // Create a SharedArrayBufferObject with a new SharedArrayRawBuffer. |
|
84 static JSObject *New(JSContext *cx, uint32_t length); |
|
85 |
|
86 // Create a SharedArrayBufferObject using an existing SharedArrayRawBuffer. |
|
87 static JSObject *New(JSContext *cx, SharedArrayRawBuffer *buffer); |
|
88 |
|
89 static bool byteLengthGetter(JSContext *cx, unsigned argc, Value *vp); |
|
90 |
|
91 static void Finalize(FreeOp *fop, JSObject *obj); |
|
92 |
|
93 void acceptRawBuffer(SharedArrayRawBuffer *buffer); |
|
94 void dropRawBuffer(); |
|
95 |
|
96 SharedArrayRawBuffer *rawBufferObject() const; |
|
97 uint8_t *dataPointer() const; |
|
98 uint32_t byteLength() const; |
|
99 }; |
|
100 |
|
101 bool |
|
102 IsSharedArrayBuffer(HandleValue v); |
|
103 |
|
104 } // namespace js |
|
105 |
|
106 #endif // vm_SharedArrayObject_h |