@daneren2005/shared-memory-objects 1.1.0 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (80) hide show
  1. package/README.md +91 -56
  2. package/dist/{src/allocated-memory.d.ts → allocated-memory.d.ts} +6 -4
  3. package/dist/allocated-memory.js +47 -37
  4. package/dist/allocated-memory.js.map +1 -1
  5. package/dist/{src/cached-item-list.d.ts → cached-item-list.d.ts} +5 -4
  6. package/dist/cached-item-list.js +75 -75
  7. package/dist/cached-item-list.js.map +1 -1
  8. package/dist/constant-string.d.ts +15 -0
  9. package/dist/constant-string.js +67 -0
  10. package/dist/constant-string.js.map +1 -0
  11. package/dist/interfaces/pow2.js +0 -2
  12. package/dist/interfaces/typed-array-constructor.js +0 -2
  13. package/dist/interfaces/typed-array.js +0 -2
  14. package/dist/{src/local-pool.d.ts → local-pool.d.ts} +2 -2
  15. package/dist/local-pool.js +89 -94
  16. package/dist/local-pool.js.map +1 -1
  17. package/dist/lock/read-write-lock.js +15 -21
  18. package/dist/lock/read-write-lock.js.map +1 -1
  19. package/dist/lock/simple-lock.js +9 -12
  20. package/dist/lock/simple-lock.js.map +1 -1
  21. package/dist/{src/memory-buffer.d.ts → memory-buffer.d.ts} +8 -5
  22. package/dist/memory-buffer.js +269 -326
  23. package/dist/memory-buffer.js.map +1 -1
  24. package/dist/{src/memory-heap.d.ts → memory-heap.d.ts} +4 -2
  25. package/dist/memory-heap.js +105 -115
  26. package/dist/memory-heap.js.map +1 -1
  27. package/dist/{src/serialize-object.d.ts → serialize-object.d.ts} +2 -2
  28. package/dist/serialize-object.js +43 -55
  29. package/dist/serialize-object.js.map +1 -1
  30. package/dist/{src/shared-list.d.ts → shared-list.d.ts} +11 -5
  31. package/dist/shared-list.js +162 -164
  32. package/dist/shared-list.js.map +1 -1
  33. package/dist/{src/shared-map.d.ts → shared-map.d.ts} +2 -2
  34. package/dist/shared-map.js +105 -138
  35. package/dist/shared-map.js.map +1 -1
  36. package/dist/{src/shared-pointer-list.d.ts → shared-pointer-list.d.ts} +4 -3
  37. package/dist/shared-pointer-list.js +49 -48
  38. package/dist/shared-pointer-list.js.map +1 -1
  39. package/dist/{src/shared-pool.d.ts → shared-pool.d.ts} +15 -8
  40. package/dist/shared-pool.js +151 -164
  41. package/dist/shared-pool.js.map +1 -1
  42. package/dist/shared-stack.d.ts +59 -0
  43. package/dist/shared-stack.js +221 -0
  44. package/dist/shared-stack.js.map +1 -0
  45. package/dist/{src/shared-string.d.ts → shared-string.d.ts} +2 -2
  46. package/dist/shared-string.js +72 -78
  47. package/dist/shared-string.js.map +1 -1
  48. package/dist/{src/shared-vector.d.ts → shared-vector.d.ts} +5 -4
  49. package/dist/shared-vector.js +150 -164
  50. package/dist/shared-vector.js.map +1 -1
  51. package/dist/utils/16-from-32-array.js +14 -16
  52. package/dist/utils/16-from-32-array.js.map +1 -1
  53. package/dist/utils/16-from-64-array.js +15 -10
  54. package/dist/utils/16-from-64-array.js.map +1 -1
  55. package/dist/utils/atomic-math.js +30 -32
  56. package/dist/utils/atomic-math.js.map +1 -1
  57. package/dist/utils/float32-atomics.js +18 -22
  58. package/dist/utils/float32-atomics.js.map +1 -1
  59. package/dist/utils/float64-atomics.js +18 -22
  60. package/dist/utils/float64-atomics.js.map +1 -1
  61. package/dist/utils/pointer.js +34 -44
  62. package/dist/utils/pointer.js.map +1 -1
  63. package/dist/{src/utils → utils}/typedarray.d.ts +47 -47
  64. package/dist/utils/typedarray.js +36 -43
  65. package/dist/utils/typedarray.js.map +1 -1
  66. package/package.json +23 -22
  67. package/dist/interfaces/pow2.js.map +0 -1
  68. package/dist/interfaces/typed-array-constructor.js.map +0 -1
  69. package/dist/interfaces/typed-array.js.map +0 -1
  70. /package/dist/{src/interfaces → interfaces}/pow2.d.ts +0 -0
  71. /package/dist/{src/interfaces → interfaces}/typed-array-constructor.d.ts +0 -0
  72. /package/dist/{src/interfaces → interfaces}/typed-array.d.ts +0 -0
  73. /package/dist/{src/lock → lock}/read-write-lock.d.ts +0 -0
  74. /package/dist/{src/lock → lock}/simple-lock.d.ts +0 -0
  75. /package/dist/{src/utils → utils}/16-from-32-array.d.ts +0 -0
  76. /package/dist/{src/utils → utils}/16-from-64-array.d.ts +0 -0
  77. /package/dist/{src/utils → utils}/atomic-math.d.ts +0 -0
  78. /package/dist/{src/utils → utils}/float32-atomics.d.ts +0 -0
  79. /package/dist/{src/utils → utils}/float64-atomics.d.ts +0 -0
  80. /package/dist/{src/utils → utils}/pointer.d.ts +0 -0
@@ -1,327 +1,270 @@
1
- import { lock as E, unlock as a } from "./lock/simple-lock.js";
2
- import { typedArray as z } from "./utils/typedarray.js";
3
- const A = 0, T = 1, B = 2, x = 3, N = 4, u = 5, g = 6, m = 1, _ = 2, w = 32, p = 0, b = 1, n = 8;
4
- class C {
5
- buf;
6
- start;
7
- u8;
8
- u32;
9
- state;
10
- lock;
11
- constructor(t = {}) {
12
- if (this.buf = t.buf ? t.buf : new ArrayBuffer(t.size || 4096), this.start = t.start != null ? k(Math.max(t.start, 0), 4) : 0, this.u8 = new Uint8Array(this.buf), this.u32 = new Uint32Array(this.buf), this.state = new Uint32Array(this.buf, this.start, w / 4), this.lock = new Int32Array(this.buf, this.start + this.state.byteLength - 4, 1), !t.skipInitialization) {
13
- const e = t.align || 8;
14
- if (e < 8)
15
- throw new Error(`invalid alignment: ${e}, must be a pow2 and >= 8`);
16
- const s = this.initialTop(e), i = t.end != null ? Math.min(t.end, this.buf.byteLength) : this.buf.byteLength;
17
- if (s >= i)
18
- throw new Error(
19
- `insufficient address range (0x${this.start.toString(
20
- 16
21
- )} - 0x${i.toString(16)})`
22
- );
23
- this.align = e, this.doCompact = t.compact !== !1, this.doSplit = t.split !== !1, this.minSplit = t.minSplit || 16, this.end = i, this.top = s, this._free = 0, this._used = 0;
24
- }
25
- }
26
- stats() {
27
- const t = (s) => {
28
- let i = 0, o = 0;
29
- for (; s; )
30
- if (i++, o += this.blockSize(s), s = this.blockNext(s), s > this.end) {
31
- console.error(`Trying to get stats for block past end of buffer: ${s} > ${this.end}`);
32
- break;
33
- }
34
- return { count: i, size: o };
35
- }, e = t(this._free);
36
- return {
37
- free: e,
38
- used: t(this._used),
39
- top: this.top,
40
- available: this.end - this.top + e.size,
41
- total: this.buf.byteLength
42
- };
43
- }
44
- callocAs(t, e, s = 0) {
45
- const i = this.mallocAs(t, e);
46
- return i && i.fill(s), i;
47
- }
48
- mallocAs(t, e) {
49
- const s = this.malloc(e * M[t]);
50
- return s ? z(t, this.buf, s, e) : void 0;
51
- }
52
- calloc(t, e = 0) {
53
- const s = this.malloc(t);
54
- return s && this.u8.fill(e, s, s + t), s;
55
- }
56
- malloc(t) {
57
- if (t <= 0)
58
- return 0;
59
- E(this.lock);
60
- const e = k(t + n, this.align), s = this.end;
61
- let i = this.top, o = this._free, l = 0;
62
- for (; o; ) {
63
- const h = this.blockSize(o), c = o + h >= i;
64
- if (c || h >= e) {
65
- let S = this.mallocTop(
66
- o,
67
- l,
68
- h,
69
- e,
70
- c
71
- );
72
- return a(this.lock), S;
73
- }
74
- l = o, o = this.blockNext(o);
75
- }
76
- if (o = i, i = o + e, i <= s) {
77
- this.initBlock(o, e, this._used), this._used = o, this.top = i;
78
- let h = f(o);
79
- return a(this.lock), h;
80
- }
81
- return a(this.lock), 0;
82
- }
83
- mallocTop(t, e, s, i, o) {
84
- if (o && t + i > this.end) return 0;
85
- if (e ? this.unlinkBlock(e, t) : this._free = this.blockNext(t), this.setBlockNext(t, this._used), this._used = t, o)
86
- this.top = t + this.setBlockSize(t, i);
87
- else if (this.doSplit) {
88
- const l = s - i;
89
- l >= this.minSplit && this.splitBlock(t, i, l);
90
- }
91
- return f(t);
92
- }
93
- realloc(t, e) {
94
- if (e <= 0)
95
- return 0;
96
- const s = d(t);
97
- let i = 0, o = this._used, l = 0;
98
- for (; o; ) {
99
- if (o === s) {
100
- [i, l] = this.reallocBlock(o, e);
101
- break;
102
- }
103
- o = this.blockNext(o);
104
- }
105
- return i && i !== s && this.u8.copyWithin(
106
- f(i),
107
- f(s),
108
- l
109
- ), f(i);
110
- }
111
- reallocBlock(t, e) {
112
- const s = this.blockSize(t), i = t + s, o = i >= this.top, l = k(e + n, this.align);
113
- if (l <= s) {
114
- if (this.doSplit) {
115
- const h = s - l;
116
- h >= this.minSplit ? this.splitBlock(t, l, h) : o && (this.top = t + l);
117
- } else o && (this.top = t + l);
118
- return [t, i];
119
- }
120
- return o && t + l < this.end ? (this.top = t + this.setBlockSize(t, l), [t, i]) : (this.free(t), [d(this.malloc(e)), i]);
121
- }
122
- reallocArray(t, e) {
123
- if (t.buffer !== this.buf)
124
- return;
125
- const s = this.realloc(
126
- t.byteOffset,
127
- e * t.BYTES_PER_ELEMENT
128
- );
129
- return s ? new t.constructor(this.buf, s, e) : void 0;
130
- }
131
- bytesFor(t) {
132
- let e;
133
- if (typeof t != "number") {
134
- if (t.buffer !== this.buf)
135
- return;
136
- e = t.byteOffset;
137
- } else
138
- e = t;
139
- e = d(e);
140
- let s = this._used;
141
- for (; s; ) {
142
- if (s === e)
143
- return this.blockSize(e) - n;
144
- s = this.blockNext(s);
145
- }
146
- }
147
- lengthOf(t) {
148
- let e = this.bytesFor(t);
149
- if (e)
150
- return e / this.u32.BYTES_PER_ELEMENT;
151
- }
152
- free(t) {
153
- let e;
154
- if (typeof t != "number") {
155
- if (t.buffer !== this.buf)
156
- return !1;
157
- e = t.byteOffset;
158
- } else
159
- e = t;
160
- E(this.lock), e = d(e);
161
- let s = this._used, i = 0;
162
- for (; s; ) {
163
- if (s === e)
164
- return i ? this.unlinkBlock(i, s) : this._used = this.blockNext(s), this.insert(s), this.doCompact && this.compact(), a(this.lock), !0;
165
- i = s, s = this.blockNext(s);
166
- }
167
- return a(this.lock), !1;
168
- }
169
- freeAll() {
170
- this._free = 0, this._used = 0, this.top = this.initialTop();
171
- }
172
- release() {
173
- return delete this.u8, delete this.u32, delete this.state, delete this.buf, !0;
174
- }
175
- get align() {
176
- return this.state[N];
177
- }
178
- set align(t) {
179
- this.state[N] = t;
180
- }
181
- get end() {
182
- return this.state[x];
183
- }
184
- set end(t) {
185
- this.state[x] = t;
186
- }
187
- get top() {
188
- return Atomics.load(this.state, B);
189
- }
190
- set top(t) {
191
- Atomics.store(this.state, B, t);
192
- }
193
- get _free() {
194
- return Atomics.load(this.state, A);
195
- }
196
- set _free(t) {
197
- Atomics.store(this.state, A, t);
198
- }
199
- get _used() {
200
- return Atomics.load(this.state, T);
201
- }
202
- set _used(t) {
203
- Atomics.store(this.state, T, t);
204
- }
205
- get doCompact() {
206
- return !!(this.state[u] & m);
207
- }
208
- set doCompact(t) {
209
- t ? this.state[u] |= 1 << m - 1 : this.state[u] &= ~m;
210
- }
211
- get doSplit() {
212
- return !!(this.state[u] & _);
213
- }
214
- set doSplit(t) {
215
- t ? this.state[u] |= 1 << _ - 1 : this.state[u] &= ~_;
216
- }
217
- get minSplit() {
218
- return this.state[g];
219
- }
220
- set minSplit(t) {
221
- if (t <= n)
222
- throw new Error(`illegal min split threshold: ${t}, require at least ${n + 1}`);
223
- this.state[g] = t;
224
- }
225
- blockSize(t) {
226
- return Atomics.load(this.u32, (t >> 2) + p);
227
- }
228
- /**
229
- * Sets & returns given block size.
230
- *
231
- * @param block -
232
- * @param size -
233
- */
234
- setBlockSize(t, e) {
235
- return Atomics.store(this.u32, (t >> 2) + p, e), e;
236
- }
237
- blockNext(t) {
238
- return Atomics.load(this.u32, (t >> 2) + b);
239
- }
240
- /**
241
- * Sets block next pointer to `next`. Use zero to indicate list end.
242
- *
243
- * @param block -
244
- */
245
- setBlockNext(t, e) {
246
- Atomics.store(this.u32, (t >> 2) + b, e);
247
- }
248
- /**
249
- * Initializes block header with given `size` and `next` pointer. Returns `block`.
250
- *
251
- * @param block -
252
- * @param size -
253
- * @param next -
254
- */
255
- initBlock(t, e, s) {
256
- const i = t >>> 2;
257
- return Atomics.store(this.u32, i + p, e), Atomics.store(this.u32, i + b, s), t;
258
- }
259
- unlinkBlock(t, e) {
260
- this.setBlockNext(t, this.blockNext(e));
261
- }
262
- splitBlock(t, e, s) {
263
- this.insert(
264
- this.initBlock(
265
- t + this.setBlockSize(t, e),
266
- s,
267
- 0
268
- )
269
- ), this.doCompact && this.compact();
270
- }
271
- initialTop(t = this.align) {
272
- return k(this.start + w + n, t) - n;
273
- }
274
- /**
275
- * Traverses free list and attempts to recursively merge blocks
276
- * occupying consecutive memory regions. Returns true if any blocks
277
- * have been merged. Only called if `compact` option is enabled.
278
- */
279
- compact() {
280
- let t = this._free, e = 0, s = 0, i, o = !1;
281
- for (; t; ) {
282
- for (i = t, s = this.blockNext(t); s && i + this.blockSize(i) === s; )
283
- i = s, s = this.blockNext(s);
284
- if (i !== t) {
285
- const l = i - t + this.blockSize(i);
286
- this.setBlockSize(t, l);
287
- const h = this.blockNext(i);
288
- let c = this.blockNext(t);
289
- for (; c && c !== h; ) {
290
- const S = this.blockNext(c);
291
- this.setBlockNext(c, 0), c = S;
292
- }
293
- this.setBlockNext(t, h), o = !0;
294
- }
295
- t + this.blockSize(t) >= this.top && (this.top = t, e ? this.unlinkBlock(e, t) : this._free = this.blockNext(t)), e = t, t = this.blockNext(t);
296
- }
297
- return o;
298
- }
299
- /**
300
- * Inserts given block into list of free blocks, sorted by address.
301
- *
302
- * @param block -
303
- */
304
- insert(t) {
305
- let e = this._free, s = 0;
306
- for (; e && !(t <= e); )
307
- s = e, e = this.blockNext(e);
308
- s ? this.setBlockNext(s, t) : this._free = t, this.setBlockNext(t, e);
309
- }
310
- }
311
- const f = (r) => r > 0 ? r + n : 0, d = (r) => r > 0 ? r - n : 0, k = (r, t) => (t--, r + t & ~t), M = {
312
- u8: 1,
313
- u8c: 1,
314
- i8: 1,
315
- u16: 2,
316
- i16: 2,
317
- u32: 4,
318
- i32: 4,
319
- i64: 8,
320
- u64: 8,
321
- f32: 4,
322
- f64: 8
323
- };
324
- export {
325
- C as default
1
+ import { lock as e, unlock as t } from "./lock/simple-lock.js";
2
+ import { typedArray as n } from "./utils/typedarray.js";
3
+ //#region src/memory-buffer.ts
4
+ var r = 0, i = 1, a = 2, o = 3, s = 4, c = 5, l = 6, u = 1, d = 2, f = {
5
+ u8: 1,
6
+ u8c: 1,
7
+ i8: 1,
8
+ u16: 2,
9
+ i16: 2,
10
+ u32: 4,
11
+ i32: 4,
12
+ i64: 8,
13
+ u64: 8,
14
+ f32: 4,
15
+ f64: 8
16
+ }, p = 32, m = 0, h = 1, g = 8, _ = class {
17
+ buf;
18
+ start;
19
+ u8;
20
+ u32;
21
+ state;
22
+ lock;
23
+ constructor(e = {}) {
24
+ if (this.buf = e.buf ? e.buf : new ArrayBuffer(e.size || 4096), this.start = e.start == null ? 0 : b(Math.max(e.start, 0), 4), this.u8 = new Uint8Array(this.buf), this.u32 = new Uint32Array(this.buf), this.state = new Uint32Array(this.buf, this.start, 8), this.lock = new Int32Array(this.buf, this.start + this.state.byteLength - 4, 1), !e.skipInitialization) {
25
+ let t = e.align || 8;
26
+ if (t < 8) throw Error(`invalid alignment: ${t}, must be a pow2 and >= 8`);
27
+ let n = this.initialTop(t), r = e.end == null ? this.buf.byteLength : Math.min(e.end, this.buf.byteLength);
28
+ if (n >= r) throw Error(`insufficient address range (0x${this.start.toString(16)} - 0x${r.toString(16)})`);
29
+ this.align = t, this.doCompact = e.compact !== !1, this.doSplit = e.split !== !1, this.minSplit = e.minSplit || 16, this.end = r, this.top = n, this._free = 0, this._used = 0;
30
+ }
31
+ }
32
+ stats() {
33
+ let e = (e) => {
34
+ let t = 0, n = 0;
35
+ for (; e;) if (t++, n += this.blockSize(e), e = this.blockNext(e), e > this.end) {
36
+ console.error(`Trying to get stats for block past end of buffer: ${e} > ${this.end}`);
37
+ break;
38
+ }
39
+ return {
40
+ count: t,
41
+ size: n
42
+ };
43
+ }, t = e(this._free);
44
+ return {
45
+ free: t,
46
+ used: e(this._used),
47
+ top: this.top,
48
+ available: this.end - this.top + t.size,
49
+ total: this.buf.byteLength
50
+ };
51
+ }
52
+ callocAs(e, t, n = 0) {
53
+ let r = this.mallocAs(e, t);
54
+ return r && r.fill(n), r;
55
+ }
56
+ mallocAs(e, t) {
57
+ let r = this.malloc(t * f[e]);
58
+ return r ? n(e, this.buf, r, t) : void 0;
59
+ }
60
+ calloc(e, t = 0) {
61
+ let n = this.malloc(e);
62
+ return n && this.u8.fill(t, n, n + e), n;
63
+ }
64
+ malloc(n) {
65
+ if (n <= 0) return 0;
66
+ e(this.lock);
67
+ let r = b(n + 8, this.align), i = this.end, a = this.top, o = this._free, s = 0;
68
+ for (; o;) {
69
+ let e = this.blockSize(o), n = o + e >= a;
70
+ if (n || e >= r) {
71
+ let i = this.mallocTop(o, s, e, r, n);
72
+ return t(this.lock), i;
73
+ }
74
+ s = o, o = this.blockNext(o);
75
+ }
76
+ if (o = a, a = o + r, a <= i) {
77
+ this.initBlock(o, r, this._used), this._used = o, this.top = a;
78
+ let e = v(o);
79
+ return t(this.lock), e;
80
+ }
81
+ return t(this.lock), 0;
82
+ }
83
+ mallocTop(e, t, n, r, i) {
84
+ if (i && e + r > this.end) return 0;
85
+ if (t ? this.unlinkBlock(t, e) : this._free = this.blockNext(e), this.setBlockNext(e, this._used), this._used = e, i) this.top = e + this.setBlockSize(e, r);
86
+ else if (this.doSplit) {
87
+ let t = n - r;
88
+ t >= this.minSplit && this.splitBlock(e, r, t);
89
+ }
90
+ return v(e);
91
+ }
92
+ realloc(e, t) {
93
+ if (t <= 0) return 0;
94
+ let n = y(e), r = 0, i = this._used, a = 0;
95
+ for (; i;) {
96
+ if (i === n) {
97
+ [r, a] = this.reallocBlock(i, t);
98
+ break;
99
+ }
100
+ i = this.blockNext(i);
101
+ }
102
+ return r && r !== n && this.u8.copyWithin(v(r), v(n), a), v(r);
103
+ }
104
+ reallocBlock(e, t) {
105
+ let n = this.blockSize(e), r = e + n, i = r >= this.top, a = b(t + 8, this.align);
106
+ if (a <= n) {
107
+ if (this.doSplit) {
108
+ let t = n - a;
109
+ t >= this.minSplit ? this.splitBlock(e, a, t) : i && (this.top = e + a);
110
+ } else i && (this.top = e + a);
111
+ return [e, r];
112
+ }
113
+ return i && e + a < this.end ? (this.top = e + this.setBlockSize(e, a), [e, r]) : (this.free(e), [y(this.malloc(t)), r]);
114
+ }
115
+ reallocArray(e, t) {
116
+ if (e.buffer !== this.buf) return;
117
+ let n = this.realloc(e.byteOffset, t * e.BYTES_PER_ELEMENT);
118
+ return n ? new e.constructor(this.buf, n, t) : void 0;
119
+ }
120
+ bytesFor(e) {
121
+ let t;
122
+ if (typeof e != "number") {
123
+ if (e.buffer !== this.buf) return;
124
+ t = e.byteOffset;
125
+ } else t = e;
126
+ t = y(t);
127
+ let n = this._used;
128
+ for (; n;) {
129
+ if (n === t) return this.blockSize(t) - 8;
130
+ n = this.blockNext(n);
131
+ }
132
+ }
133
+ lengthOf(e) {
134
+ let t = this.bytesFor(e);
135
+ if (t) return t / this.u32.BYTES_PER_ELEMENT;
136
+ }
137
+ free(n) {
138
+ let r;
139
+ if (typeof n != "number") {
140
+ if (n.buffer !== this.buf) return !1;
141
+ r = n.byteOffset;
142
+ } else r = n;
143
+ e(this.lock), r = y(r);
144
+ let i = this._used, a = 0;
145
+ for (; i;) {
146
+ if (i === r) return a ? this.unlinkBlock(a, i) : this._used = this.blockNext(i), this.insert(i), this.doCompact && this.compact(), t(this.lock), !0;
147
+ a = i, i = this.blockNext(i);
148
+ }
149
+ return t(this.lock), !1;
150
+ }
151
+ freeAll() {
152
+ this._free = 0, this._used = 0, this.top = this.initialTop();
153
+ }
154
+ release() {
155
+ return delete this.u8, delete this.u32, delete this.state, delete this.buf, !0;
156
+ }
157
+ get align() {
158
+ return this.state[s];
159
+ }
160
+ set align(e) {
161
+ this.state[s] = e;
162
+ }
163
+ get end() {
164
+ return this.state[o];
165
+ }
166
+ set end(e) {
167
+ this.state[o] = e;
168
+ }
169
+ get top() {
170
+ return Atomics.load(this.state, a);
171
+ }
172
+ set top(e) {
173
+ Atomics.store(this.state, a, e);
174
+ }
175
+ get isEmpty() {
176
+ return this._used === 0;
177
+ }
178
+ get _free() {
179
+ return Atomics.load(this.state, r);
180
+ }
181
+ set _free(e) {
182
+ Atomics.store(this.state, r, e);
183
+ }
184
+ get _used() {
185
+ return Atomics.load(this.state, i);
186
+ }
187
+ set _used(e) {
188
+ Atomics.store(this.state, i, e);
189
+ }
190
+ get doCompact() {
191
+ return !!(this.state[c] & u);
192
+ }
193
+ set doCompact(e) {
194
+ e ? this.state[c] |= 1 : this.state[c] &= -2;
195
+ }
196
+ get doSplit() {
197
+ return !!(this.state[c] & d);
198
+ }
199
+ set doSplit(e) {
200
+ e ? this.state[c] |= 2 : this.state[c] &= -3;
201
+ }
202
+ get minSplit() {
203
+ return this.state[l];
204
+ }
205
+ set minSplit(e) {
206
+ if (e <= 8) throw Error(`illegal min split threshold: ${e}, require at least 9`);
207
+ this.state[l] = e;
208
+ }
209
+ blockSize(e) {
210
+ return Atomics.load(this.u32, (e >> 2) + m);
211
+ }
212
+ setBlockSize(e, t) {
213
+ return Atomics.store(this.u32, (e >> 2) + m, t), t;
214
+ }
215
+ blockNext(e) {
216
+ return Atomics.load(this.u32, (e >> 2) + h);
217
+ }
218
+ setBlockNext(e, t) {
219
+ Atomics.store(this.u32, (e >> 2) + h, t);
220
+ }
221
+ initBlock(e, t, n) {
222
+ let r = e >>> 2;
223
+ return Atomics.store(this.u32, r + m, t), Atomics.store(this.u32, r + h, n), e;
224
+ }
225
+ unlinkBlock(e, t) {
226
+ this.setBlockNext(e, this.blockNext(t));
227
+ }
228
+ splitBlock(e, t, n) {
229
+ this.insert(this.initBlock(e + this.setBlockSize(e, t), n, 0)), this.doCompact && this.compact();
230
+ }
231
+ initialTop(e = this.align) {
232
+ return b(this.start + 32 + 8, e) - 8;
233
+ }
234
+ compact() {
235
+ let e = this._free, t = 0, n = 0, r, i = !1;
236
+ for (; e;) {
237
+ for (r = e, n = this.blockNext(e); n && r + this.blockSize(r) === n;) r = n, n = this.blockNext(n);
238
+ if (r !== e) {
239
+ let t = r - e + this.blockSize(r);
240
+ this.setBlockSize(e, t);
241
+ let n = this.blockNext(r), a = this.blockNext(e);
242
+ for (; a && a !== n;) {
243
+ let e = this.blockNext(a);
244
+ this.setBlockNext(a, 0), a = e;
245
+ }
246
+ this.setBlockNext(e, n), i = !0;
247
+ }
248
+ e + this.blockSize(e) >= this.top && (this.top = e, t ? this.unlinkBlock(t, e) : this._free = this.blockNext(e)), t = e, e = this.blockNext(e);
249
+ }
250
+ return i;
251
+ }
252
+ insert(e) {
253
+ let t = this._free, n = 0;
254
+ for (; t && !(e <= t);) n = t, t = this.blockNext(t);
255
+ n ? this.setBlockNext(n, e) : this._free = e, this.setBlockNext(e, t);
256
+ }
326
257
  };
327
- //# sourceMappingURL=memory-buffer.js.map
258
+ function v(e) {
259
+ return e > 0 ? e + 8 : 0;
260
+ }
261
+ function y(e) {
262
+ return e > 0 ? e - 8 : 0;
263
+ }
264
+ function b(e, t) {
265
+ return t--, e + t & ~t;
266
+ }
267
+ //#endregion
268
+ export { g as SIZEOF_MEM_BLOCK, p as SIZEOF_STATE, _ as default };
269
+
270
+ //# sourceMappingURL=memory-buffer.js.map