balloc 1.3.1 → 1.4.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.
- package/CHANGELOG.md +9 -0
- package/README.MD +37 -7
- package/README.ar.md +31 -1
- package/README.en.md +37 -7
- package/README.es.md +31 -1
- package/README.jp.md +31 -1
- package/README.pt.md +36 -7
- package/README.zh.md +31 -1
- package/docs/API /342/200/224 balloc.ar.md" +56 -0
- package/docs/API /342/200/224 balloc.en.md" +55 -0
- package/docs/API /342/200/224 balloc.es.md" +56 -0
- package/docs/API /342/200/224 balloc.jp.md" +56 -0
- package/docs/API /342/200/224 balloc.pt.md" +56 -0
- package/docs/API /342/200/224 balloc.zh.md" +56 -0
- package/package.json +1 -1
- package/src/ar/balloc.cjs +54 -24
- package/src/ar/balloc.js +1 -1
- package/src/ar/balloc.mjs +71 -28
- package/src/en/balloc.cjs +56 -24
- package/src/en/balloc.js +1 -1
- package/src/en/balloc.mjs +73 -28
- package/src/es/balloc.cjs +56 -24
- package/src/es/balloc.js +1 -1
- package/src/es/balloc.mjs +73 -28
- package/src/jp/balloc.cjs +56 -24
- package/src/jp/balloc.js +1 -1
- package/src/jp/balloc.mjs +73 -28
- package/src/pt/balloc.cjs +56 -23
- package/src/pt/balloc.js +1 -1
- package/src/pt/balloc.mjs +79 -28
- package/src/zh/balloc.cjs +54 -24
- package/src/zh/balloc.js +1 -1
- package/src/zh/balloc.mjs +71 -28
- package/types/ar/balloc.d.mts +32 -1
- package/types/en/balloc.d.mts +34 -1
- package/types/es/balloc.d.mts +34 -1
- package/types/jp/balloc.d.mts +34 -1
- package/types/pt/balloc.d.mts +37 -0
- package/types/zh/balloc.d.mts +32 -1
|
@@ -31,6 +31,10 @@ Balloc
|
|
|
31
31
|
│ ├── free()
|
|
32
32
|
│ └── gc()
|
|
33
33
|
│
|
|
34
|
+
├── Ingesta de datos
|
|
35
|
+
│ └── loadData()
|
|
36
|
+
│
|
|
37
|
+
│
|
|
34
38
|
├── Estructuras lineales
|
|
35
39
|
│ ├── Stack
|
|
36
40
|
│ │ ├── push()
|
|
@@ -402,6 +406,57 @@ memory.gc();
|
|
|
402
406
|
|
|
403
407
|
---
|
|
404
408
|
|
|
409
|
+
## `loadData(adapter)`
|
|
410
|
+
|
|
411
|
+
Delega la ingesta síncrona de datos a un adaptador específico del formato. El adaptador recibe la instancia actual de `Balloc`, utiliza su API pública para asignar y rellenar bloques, y devuelve el handler numérico que identifica el bloque principal.
|
|
412
|
+
|
|
413
|
+
Balloc no interpreta el formato de entrada ni administra transacciones del adaptador. El propio adaptador es responsable del análisis de datos, la organización de los bloques y la reversión de las asignaciones que haya realizado si la ingesta falla. Las excepciones lanzadas por el adaptador se propagan sin modificaciones a través de `loadData()`.
|
|
414
|
+
|
|
415
|
+
### Contrato del adaptador
|
|
416
|
+
|
|
417
|
+
```ts
|
|
418
|
+
interface FormatAdapter {
|
|
419
|
+
load(allocator: Balloc): number;
|
|
420
|
+
}
|
|
421
|
+
```
|
|
422
|
+
|
|
423
|
+
El método `load()` debe completarse de forma síncrona y devolver un handler de bloque válido. Devolver una `Promise` no convierte la ingesta en asíncrona: `loadData()` no espera ni resuelve esa promesa.
|
|
424
|
+
|
|
425
|
+
### Parámetros
|
|
426
|
+
|
|
427
|
+
| Parámetro | Tipo | Descripción |
|
|
428
|
+
| --- | --- | --- |
|
|
429
|
+
| `adapter` | `FormatAdapter` | Adaptador que implementa `load(allocator)`. |
|
|
430
|
+
|
|
431
|
+
### Retorno
|
|
432
|
+
|
|
433
|
+
```text
|
|
434
|
+
number
|
|
435
|
+
```
|
|
436
|
+
|
|
437
|
+
El handler devuelto por el adaptador, que identifica el bloque principal cargado.
|
|
438
|
+
|
|
439
|
+
### Excepciones
|
|
440
|
+
|
|
441
|
+
- `TypeError` — `adapter` no está presente o no proporciona un método `load()` invocable.
|
|
442
|
+
- Cualquier excepción lanzada por el método `load()` del adaptador se propaga sin modificaciones.
|
|
443
|
+
|
|
444
|
+
### Ejemplo
|
|
445
|
+
|
|
446
|
+
```js
|
|
447
|
+
const handler = memory.loadData({
|
|
448
|
+
load(allocator) {
|
|
449
|
+
const handler = allocator.alloc(4);
|
|
450
|
+
allocator.put(handler, new Uint8Array([255, 0, 0, 255]));
|
|
451
|
+
return handler;
|
|
452
|
+
}
|
|
453
|
+
});
|
|
454
|
+
```
|
|
455
|
+
|
|
456
|
+
Los adaptadores específicos de cada formato pueden distribuirse como paquetes independientes. Por ejemplo, `imagedata-adapter` carga instancias `ImageData` RGBA8 en un asignador `Balloc`.
|
|
457
|
+
|
|
458
|
+
---
|
|
459
|
+
|
|
405
460
|
# 3. Estructuras lineales
|
|
406
461
|
|
|
407
462
|
Los bloques pueden utilizarse como estructuras lineales con acceso por los extremos.
|
|
@@ -1223,6 +1278,7 @@ La generación participa en la validación mediante cookie. En consecuencia, un
|
|
|
1223
1278
|
| Ciclo de vida | `shrink()` | `void` |
|
|
1224
1279
|
| Ciclo de vida | `free()` | `void` |
|
|
1225
1280
|
| Ciclo de vida | `gc()` | `void` |
|
|
1281
|
+
| Ingesta de datos | `loadData()` | `number` |
|
|
1226
1282
|
| Stack | `push()` | `boolean` |
|
|
1227
1283
|
| Stack | `pop()` | `number \| undefined` |
|
|
1228
1284
|
| Stack | `peek()` | `number \| undefined` |
|
|
@@ -31,6 +31,10 @@ Balloc
|
|
|
31
31
|
│ ├── free()
|
|
32
32
|
│ └── gc()
|
|
33
33
|
│
|
|
34
|
+
├── データの取り込み
|
|
35
|
+
│ └── loadData()
|
|
36
|
+
│
|
|
37
|
+
│
|
|
34
38
|
├── 線形データ構造
|
|
35
39
|
│ ├── Stack
|
|
36
40
|
│ │ ├── push()
|
|
@@ -400,6 +404,57 @@ memory.gc();
|
|
|
400
404
|
|
|
401
405
|
---
|
|
402
406
|
|
|
407
|
+
## `loadData(adapter)`
|
|
408
|
+
|
|
409
|
+
形式固有のアダプターに同期的なデータ取り込みを委譲します。アダプターは現在の `Balloc` インスタンスを受け取り、その公開 API を使用してブロックを割り当ててデータを格納し、主要ブロックを識別する数値ハンドラーを返します。
|
|
410
|
+
|
|
411
|
+
Balloc は入力形式を解釈せず、アダプターのトランザクションも管理しません。データの解析、ブロックの構成、取り込みに失敗した場合にアダプターが行った割り当てをロールバックする責任は、アダプター側にあります。アダプターがスローした例外は変更されずに `loadData()` から伝播します。
|
|
412
|
+
|
|
413
|
+
### アダプターの契約
|
|
414
|
+
|
|
415
|
+
```ts
|
|
416
|
+
interface FormatAdapter {
|
|
417
|
+
load(allocator: Balloc): number;
|
|
418
|
+
}
|
|
419
|
+
```
|
|
420
|
+
|
|
421
|
+
`load()` メソッドは同期的に完了し、有効なブロックハンドラーを返す必要があります。`Promise` を返しても非同期の取り込みにはなりません。`loadData()` は Promise を待機せず、解決もしません。
|
|
422
|
+
|
|
423
|
+
### パラメーター
|
|
424
|
+
|
|
425
|
+
| パラメーター | 型 | 説明 |
|
|
426
|
+
| --- | --- | --- |
|
|
427
|
+
| `adapter` | `FormatAdapter` | `load(allocator)` を実装するアダプター。 |
|
|
428
|
+
|
|
429
|
+
### 戻り値
|
|
430
|
+
|
|
431
|
+
```text
|
|
432
|
+
number
|
|
433
|
+
```
|
|
434
|
+
|
|
435
|
+
アダプターが返す、読み込まれた主要ブロックを識別するハンドラー。
|
|
436
|
+
|
|
437
|
+
### 例外
|
|
438
|
+
|
|
439
|
+
- `TypeError` — `adapter` が指定されていないか、呼び出し可能な `load()` メソッドを提供していない場合。
|
|
440
|
+
- アダプターの `load()` メソッドがスローした例外は変更されずに伝播します。
|
|
441
|
+
|
|
442
|
+
### 使用例
|
|
443
|
+
|
|
444
|
+
```js
|
|
445
|
+
const handler = memory.loadData({
|
|
446
|
+
load(allocator) {
|
|
447
|
+
const handler = allocator.alloc(4);
|
|
448
|
+
allocator.put(handler, new Uint8Array([255, 0, 0, 255]));
|
|
449
|
+
return handler;
|
|
450
|
+
}
|
|
451
|
+
});
|
|
452
|
+
```
|
|
453
|
+
|
|
454
|
+
形式固有のアダプターは独立したパッケージとして配布できます。たとえば `imagedata-adapter` は RGBA8 の `ImageData` インスタンスを `Balloc` アロケーターに読み込みます。
|
|
455
|
+
|
|
456
|
+
---
|
|
457
|
+
|
|
403
458
|
# 3. 線形データ構造
|
|
404
459
|
|
|
405
460
|
ブロックは、両端からアクセスする線形データ構造として使用できます。
|
|
@@ -1161,6 +1216,7 @@ capacityBytes = 2 ** capacity × wordSize
|
|
|
1161
1216
|
| ライフサイクル | `shrink()` | `void` |
|
|
1162
1217
|
| ライフサイクル | `free()` | `void` |
|
|
1163
1218
|
| ライフサイクル | `gc()` | `void` |
|
|
1219
|
+
| データ取り込み | `loadData()` | `number` |
|
|
1164
1220
|
| Stack | `push()` | `boolean` |
|
|
1165
1221
|
| Stack | `pop()` | `number \| undefined` |
|
|
1166
1222
|
| Stack | `peek()` | `number \| undefined` |
|
|
@@ -33,6 +33,10 @@ Balloc
|
|
|
33
33
|
│ ├── free()
|
|
34
34
|
│ └── gc()
|
|
35
35
|
│
|
|
36
|
+
├── Ingestão de dados
|
|
37
|
+
│ └── loadData()
|
|
38
|
+
│
|
|
39
|
+
│
|
|
36
40
|
├── Estruturas lineares
|
|
37
41
|
│ ├── Stack
|
|
38
42
|
│ │ ├── push()
|
|
@@ -408,6 +412,57 @@ O `gc()` não altera os handlers, pois os headers permanecem nos mesmos endereç
|
|
|
408
412
|
|
|
409
413
|
---
|
|
410
414
|
|
|
415
|
+
## `loadData(adapter)`
|
|
416
|
+
|
|
417
|
+
Delega a ingestão síncrona de dados a um adaptador específico do formato. O adaptador recebe a instância atual de `Balloc`, utiliza sua API pública para alocar e preencher blocos e retorna o handler numérico que identifica o bloco principal.
|
|
418
|
+
|
|
419
|
+
Balloc não interpreta o formato de entrada nem gerencia transações do adaptador. A análise dos dados, a organização dos blocos e o rollback das alocações realizadas caso a ingestão falhe são responsabilidades do próprio adaptador. Exceções lançadas pelo adaptador são propagadas por `loadData()` sem alterações.
|
|
420
|
+
|
|
421
|
+
### Contrato do adaptador
|
|
422
|
+
|
|
423
|
+
```ts
|
|
424
|
+
interface FormatAdapter {
|
|
425
|
+
load(allocator: Balloc): number;
|
|
426
|
+
}
|
|
427
|
+
```
|
|
428
|
+
|
|
429
|
+
O método `load()` deve ser concluído de forma síncrona e retornar um handler de bloco válido. Retornar uma `Promise` não torna a ingestão assíncrona: `loadData()` não aguarda nem resolve essa Promise.
|
|
430
|
+
|
|
431
|
+
### Parâmetros
|
|
432
|
+
|
|
433
|
+
| Parâmetro | Tipo | Descrição |
|
|
434
|
+
| --- | --- | --- |
|
|
435
|
+
| `adapter` | `FormatAdapter` | Adaptador que implementa `load(allocator)`. |
|
|
436
|
+
|
|
437
|
+
### Retorno
|
|
438
|
+
|
|
439
|
+
```text
|
|
440
|
+
number
|
|
441
|
+
```
|
|
442
|
+
|
|
443
|
+
O handler retornado pelo adaptador, que identifica o bloco principal carregado.
|
|
444
|
+
|
|
445
|
+
### Exceções
|
|
446
|
+
|
|
447
|
+
- `TypeError` — `adapter` está ausente ou não fornece um método `load()` chamável.
|
|
448
|
+
- Qualquer exceção lançada pelo método `load()` do adaptador é propagada sem alterações.
|
|
449
|
+
|
|
450
|
+
### Exemplo
|
|
451
|
+
|
|
452
|
+
```js
|
|
453
|
+
const handler = memory.loadData({
|
|
454
|
+
load(allocator) {
|
|
455
|
+
const handler = allocator.alloc(4);
|
|
456
|
+
allocator.put(handler, new Uint8Array([255, 0, 0, 255]));
|
|
457
|
+
return handler;
|
|
458
|
+
}
|
|
459
|
+
});
|
|
460
|
+
```
|
|
461
|
+
|
|
462
|
+
Adaptadores específicos de formato podem ser distribuídos como pacotes independentes. Por exemplo, `imagedata-adapter` carrega instâncias `ImageData` RGBA8 em um alocador `Balloc`.
|
|
463
|
+
|
|
464
|
+
---
|
|
465
|
+
|
|
411
466
|
# 3. Estruturas lineares
|
|
412
467
|
|
|
413
468
|
Os blocos podem ser utilizados como estruturas lineares de acesso pelas extremidades.
|
|
@@ -1291,6 +1346,7 @@ A geração participa da validação por cookie. Consequentemente, um handler in
|
|
|
1291
1346
|
| Ciclo de vida | `shrink()` | `void` |
|
|
1292
1347
|
| Ciclo de vida | `free()` | `void`<br> |
|
|
1293
1348
|
| Ciclo de vida | `gc()` | `void`<br> |
|
|
1349
|
+
| Ingestão de dados | `loadData()` | `number` |
|
|
1294
1350
|
| Stack | `push()` | `boolean`<br> |
|
|
1295
1351
|
| Stack | `pop()` | `number | undefined`<br> |
|
|
1296
1352
|
| Stack | `peek()` | `number | undefined`<br> |
|
|
@@ -31,6 +31,10 @@ Balloc
|
|
|
31
31
|
│ ├── free()
|
|
32
32
|
│ └── gc()
|
|
33
33
|
│
|
|
34
|
+
├── 数据导入
|
|
35
|
+
│ └── loadData()
|
|
36
|
+
│
|
|
37
|
+
│
|
|
34
38
|
├── 线性结构
|
|
35
39
|
│ ├── Stack
|
|
36
40
|
│ │ ├── push()
|
|
@@ -402,6 +406,57 @@ memory.gc();
|
|
|
402
406
|
|
|
403
407
|
---
|
|
404
408
|
|
|
409
|
+
## `loadData(adapter)`
|
|
410
|
+
|
|
411
|
+
将同步数据导入委托给特定格式的适配器。适配器接收当前的 `Balloc` 实例,使用其公共 API 分配并填充数据块,然后返回用于标识主数据块的数字句柄。
|
|
412
|
+
|
|
413
|
+
Balloc 不解析输入格式,也不管理适配器事务。数据解析、数据块组织,以及导入失败时回滚适配器已执行的分配,均由适配器负责。适配器抛出的异常会由 `loadData()` 原样传播。
|
|
414
|
+
|
|
415
|
+
### 适配器契约
|
|
416
|
+
|
|
417
|
+
```ts
|
|
418
|
+
interface FormatAdapter {
|
|
419
|
+
load(allocator: Balloc): number;
|
|
420
|
+
}
|
|
421
|
+
```
|
|
422
|
+
|
|
423
|
+
`load()` 方法必须同步完成,并返回有效的数据块句柄。返回 `Promise` 并不会使导入变为异步:`loadData()` 不会等待或解析该 Promise。
|
|
424
|
+
|
|
425
|
+
### 参数
|
|
426
|
+
|
|
427
|
+
| 参数 | 类型 | 说明 |
|
|
428
|
+
| --- | --- | --- |
|
|
429
|
+
| `adapter` | `FormatAdapter` | 实现 `load(allocator)` 的适配器。 |
|
|
430
|
+
|
|
431
|
+
### 返回值
|
|
432
|
+
|
|
433
|
+
```text
|
|
434
|
+
number
|
|
435
|
+
```
|
|
436
|
+
|
|
437
|
+
适配器返回的句柄,用于标识已加载的主数据块。
|
|
438
|
+
|
|
439
|
+
### 异常
|
|
440
|
+
|
|
441
|
+
- `TypeError` — `adapter` 缺失,或未提供可调用的 `load()` 方法。
|
|
442
|
+
- 适配器的 `load()` 方法抛出的任何异常都会原样传播。
|
|
443
|
+
|
|
444
|
+
### 示例
|
|
445
|
+
|
|
446
|
+
```js
|
|
447
|
+
const handler = memory.loadData({
|
|
448
|
+
load(allocator) {
|
|
449
|
+
const handler = allocator.alloc(4);
|
|
450
|
+
allocator.put(handler, new Uint8Array([255, 0, 0, 255]));
|
|
451
|
+
return handler;
|
|
452
|
+
}
|
|
453
|
+
});
|
|
454
|
+
```
|
|
455
|
+
|
|
456
|
+
特定格式的适配器可以作为独立软件包发布。例如,`imagedata-adapter` 可将 RGBA8 `ImageData` 实例加载到 `Balloc` 分配器中。
|
|
457
|
+
|
|
458
|
+
---
|
|
459
|
+
|
|
405
460
|
# 3. 线性数据结构
|
|
406
461
|
|
|
407
462
|
数据块可用作从两端进行访问的线性结构。
|
|
@@ -1221,6 +1276,7 @@ generation 参与 cookie 校验。因此,如果句柄无效或其 header 已
|
|
|
1221
1276
|
| 生命周期 | `shrink()` | `void` |
|
|
1222
1277
|
| 生命周期 | `free()` | `void` |
|
|
1223
1278
|
| 生命周期 | `gc()` | `void` |
|
|
1279
|
+
| 数据导入 | `loadData()` | `number` |
|
|
1224
1280
|
| Stack | `push()` | `boolean` |
|
|
1225
1281
|
| Stack | `pop()` | `number \| undefined` |
|
|
1226
1282
|
| Stack | `peek()` | `number \| undefined` |
|
package/package.json
CHANGED
package/src/ar/balloc.cjs
CHANGED
|
@@ -190,10 +190,13 @@ var Balloc = class {
|
|
|
190
190
|
const occupied = this.controlAdapter.getUint32(headerAddress + OCCUPIED_OFFSET, IS_LITTLE_ENDIAN);
|
|
191
191
|
const capacity = this.controlAdapter.getUint8(headerAddress + CAPACITY_OFFSET);
|
|
192
192
|
const cookie = this.controlAdapter.getUint8(headerAddress + COOKIE_OFFSET);
|
|
193
|
+
const borrowed = this.controlAdapter.getUint8(headerAddress + FLAGS_OFFSET) & VIEW_LOCK;
|
|
193
194
|
if (cookie !== this.bakeCookie(headerAddress, generation)) {
|
|
194
195
|
throw new Error("Invalid or corrupted handle");
|
|
195
196
|
}
|
|
196
|
-
|
|
197
|
+
if (borrowed) {
|
|
198
|
+
throw new Error("Block is borrowed");
|
|
199
|
+
}
|
|
197
200
|
if (capacity >= 31) throw new AggregateError([new Error("Can\xB4t reallocate handler."), new RangeError("Capacity has reached its limit.")], "Internal Error.");
|
|
198
201
|
const newCapacity = capacity + 1;
|
|
199
202
|
const newBlockSize = (1 << newCapacity >>> 0) * this.wordSize;
|
|
@@ -229,7 +232,7 @@ var Balloc = class {
|
|
|
229
232
|
*/
|
|
230
233
|
shrink(handler) {
|
|
231
234
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
232
|
-
|
|
235
|
+
if (meta.borrowed) throw new Error("Block is borrowed");
|
|
233
236
|
const newCapacity = Math.max(1, 32 - Math.clz32(Math.max(2, meta.occupied) - 1));
|
|
234
237
|
if (newCapacity >= meta.capacityExponent) return;
|
|
235
238
|
const oldBlockSize = meta.byteCapacity;
|
|
@@ -253,10 +256,13 @@ var Balloc = class {
|
|
|
253
256
|
const dataAddress = this.controlAdapter.getUint32(headerAddress + ADDRESS_OFFSET, IS_LITTLE_ENDIAN);
|
|
254
257
|
const capacity = this.controlAdapter.getUint8(headerAddress + CAPACITY_OFFSET);
|
|
255
258
|
const cookie = this.controlAdapter.getUint8(headerAddress + COOKIE_OFFSET);
|
|
259
|
+
const borrowed = this.controlAdapter.getUint8(headerAddress + FLAGS_OFFSET) & VIEW_LOCK;
|
|
256
260
|
if (cookie !== this.bakeCookie(headerAddress, generation)) {
|
|
257
261
|
throw new Error("Invalid or corrupted handle");
|
|
258
262
|
}
|
|
259
|
-
|
|
263
|
+
if (borrowed) {
|
|
264
|
+
throw new Error("Block is borrowed");
|
|
265
|
+
}
|
|
260
266
|
const blockSize = (1 << capacity >>> 0) * this.wordSize;
|
|
261
267
|
this.controlAdapter.setUint8(headerAddress + COOKIE_OFFSET, 0);
|
|
262
268
|
if (this.echoPointer + 4 <= this.controlBus.byteLength) {
|
|
@@ -428,7 +434,7 @@ var Balloc = class {
|
|
|
428
434
|
*/
|
|
429
435
|
push(handler, value) {
|
|
430
436
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
431
|
-
|
|
437
|
+
if (meta.borrowed) throw new Error("Block is borrowed");
|
|
432
438
|
if ((meta.occupied + 1) * this.wordSize > meta.byteCapacity) return false;
|
|
433
439
|
this.writer(meta.dataAddress + meta.occupied * this.wordSize, value, IS_LITTLE_ENDIAN);
|
|
434
440
|
this.controlAdapter.setUint32(meta.headerAddress + OCCUPIED_OFFSET, meta.occupied + 1, IS_LITTLE_ENDIAN);
|
|
@@ -448,7 +454,7 @@ var Balloc = class {
|
|
|
448
454
|
*/
|
|
449
455
|
pop(handler) {
|
|
450
456
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
451
|
-
|
|
457
|
+
if (meta.borrowed) throw new Error("Block is borrowed");
|
|
452
458
|
if (meta.occupied === 0) return void 0;
|
|
453
459
|
this.controlAdapter.setUint32(meta.headerAddress + OCCUPIED_OFFSET, meta.occupied - 1, IS_LITTLE_ENDIAN);
|
|
454
460
|
return this.reader(meta.dataAddress + (meta.occupied - 1) * this.wordSize, IS_LITTLE_ENDIAN);
|
|
@@ -485,7 +491,7 @@ var Balloc = class {
|
|
|
485
491
|
*/
|
|
486
492
|
nudge(handler, value) {
|
|
487
493
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
488
|
-
|
|
494
|
+
if (meta.borrowed) throw new Error("Block is borrowed");
|
|
489
495
|
if ((meta.occupied + 1) * this.wordSize > meta.byteCapacity) return false;
|
|
490
496
|
this.dataBus.copyWithin(meta.dataAddress + this.wordSize, meta.dataAddress, meta.dataAddress + meta.occupied * this.wordSize);
|
|
491
497
|
this.writer(meta.dataAddress, value, IS_LITTLE_ENDIAN);
|
|
@@ -507,7 +513,7 @@ var Balloc = class {
|
|
|
507
513
|
*/
|
|
508
514
|
pull(handler) {
|
|
509
515
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
510
|
-
|
|
516
|
+
if (meta.borrowed) throw new Error("Block is borrowed");
|
|
511
517
|
if (meta.occupied === 0) return void 0;
|
|
512
518
|
const result = this.reader(meta.dataAddress, IS_LITTLE_ENDIAN);
|
|
513
519
|
this.dataBus.copyWithin(meta.dataAddress, meta.dataAddress + this.wordSize, meta.dataAddress + meta.occupied * this.wordSize);
|
|
@@ -625,7 +631,7 @@ var Balloc = class {
|
|
|
625
631
|
*/
|
|
626
632
|
set(handler, index, value) {
|
|
627
633
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
628
|
-
|
|
634
|
+
if (meta.borrowed) throw new Error("Block is borrowed");
|
|
629
635
|
if (index < 0 || index >= meta.occupied) return false;
|
|
630
636
|
this.writer(meta.dataAddress + index * this.wordSize, value, IS_LITTLE_ENDIAN);
|
|
631
637
|
return true;
|
|
@@ -646,7 +652,7 @@ var Balloc = class {
|
|
|
646
652
|
*/
|
|
647
653
|
clear(handler, value = 0) {
|
|
648
654
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
649
|
-
|
|
655
|
+
if (meta.borrowed) throw new Error("Block is borrowed");
|
|
650
656
|
const slots = meta.byteCapacity / this.wordSize;
|
|
651
657
|
const rawAddress = meta.dataAddress >>> this.wordShift;
|
|
652
658
|
this.auxiliary.fill(value, rawAddress, rawAddress + slots);
|
|
@@ -675,7 +681,7 @@ var Balloc = class {
|
|
|
675
681
|
*/
|
|
676
682
|
fill(handler, value, start = 0, end = null) {
|
|
677
683
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
678
|
-
|
|
684
|
+
if (meta.borrowed) throw new Error("Block is borrowed");
|
|
679
685
|
const slots = meta.byteCapacity / this.wordSize;
|
|
680
686
|
const lastIndex = end ?? slots;
|
|
681
687
|
if (start > lastIndex) {
|
|
@@ -704,7 +710,7 @@ var Balloc = class {
|
|
|
704
710
|
*/
|
|
705
711
|
fillAll(handler, value) {
|
|
706
712
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
707
|
-
|
|
713
|
+
if (meta.borrowed) throw new Error("Block is borrowed");
|
|
708
714
|
const rawAddress = meta.dataAddress >>> this.wordShift;
|
|
709
715
|
const capacityElements = meta.byteCapacity / this.wordSize;
|
|
710
716
|
this.auxiliary.fill(value, rawAddress, rawAddress + capacityElements);
|
|
@@ -730,7 +736,7 @@ var Balloc = class {
|
|
|
730
736
|
*/
|
|
731
737
|
put(handler, data) {
|
|
732
738
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
733
|
-
|
|
739
|
+
if (meta.borrowed) throw new Error("Block is borrowed");
|
|
734
740
|
const rawAddress = meta.dataAddress >>> this.wordShift;
|
|
735
741
|
const length = Math.min(1 << meta.capacityExponent >>> 0, data.length);
|
|
736
742
|
if (Array.isArray(data)) {
|
|
@@ -788,21 +794,24 @@ var Balloc = class {
|
|
|
788
794
|
const callback = args[lastIndex];
|
|
789
795
|
if (typeof callback !== "function") throw new TypeError("callback must be a function");
|
|
790
796
|
const lookup = /* @__PURE__ */ Object.create(null);
|
|
797
|
+
const acquired = [];
|
|
791
798
|
const views = [];
|
|
792
799
|
try {
|
|
793
800
|
for (let i = 0; i < lastIndex; i++) {
|
|
794
|
-
const metadata = this.extractMetadata(args[i],
|
|
795
|
-
|
|
801
|
+
const metadata = this.extractMetadata(args[i], this.metadata);
|
|
802
|
+
const headerAddr = metadata.headerAddress;
|
|
803
|
+
if (Object.hasOwn(lookup, headerAddr)) throw new Error("Duplicate handler");
|
|
796
804
|
if (metadata.borrowed) throw new Error("Block is already borrowed");
|
|
797
|
-
this.controlBus[
|
|
805
|
+
this.controlBus[headerAddr + FLAGS_OFFSET] |= VIEW_LOCK;
|
|
798
806
|
const rawAddress = metadata.dataAddress >>> this.wordShift;
|
|
799
|
-
lookup[
|
|
807
|
+
lookup[headerAddr] = true;
|
|
808
|
+
acquired.push(headerAddr);
|
|
800
809
|
views.push(this.auxiliary.subarray(rawAddress, rawAddress + metadata.occupied));
|
|
801
810
|
}
|
|
802
811
|
return callback(...views);
|
|
803
812
|
} finally {
|
|
804
|
-
for (
|
|
805
|
-
this.controlBus[
|
|
813
|
+
for (let i = 0; i < acquired.length; i++) {
|
|
814
|
+
this.controlBus[acquired[i] + FLAGS_OFFSET] &= ~VIEW_LOCK;
|
|
806
815
|
}
|
|
807
816
|
}
|
|
808
817
|
}
|
|
@@ -890,7 +899,7 @@ var Balloc = class {
|
|
|
890
899
|
const metaB = {};
|
|
891
900
|
this.extractMetadata(source, metaA);
|
|
892
901
|
this.extractMetadata(destination, metaB);
|
|
893
|
-
|
|
902
|
+
if (metaB.borrowed) throw new Error("Block is borrowed");
|
|
894
903
|
const maxSlotsDest = Math.floor(metaB.byteCapacity / this.wordSize);
|
|
895
904
|
const slotsToCopy = Math.min(metaA.occupied, maxSlotsDest, slots);
|
|
896
905
|
const newOccupied = Math.max(metaB.occupied, slotsToCopy);
|
|
@@ -921,7 +930,7 @@ var Balloc = class {
|
|
|
921
930
|
*/
|
|
922
931
|
copyWithin(handler, target, start, length) {
|
|
923
932
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
924
|
-
|
|
933
|
+
if (meta.borrowed) throw new Error("Block is borrowed");
|
|
925
934
|
const slots = Math.floor(meta.byteCapacity / this.wordSize);
|
|
926
935
|
target = Math.trunc(target);
|
|
927
936
|
start = Math.trunc(start);
|
|
@@ -961,7 +970,7 @@ var Balloc = class {
|
|
|
961
970
|
const metaSource = {};
|
|
962
971
|
this.extractMetadata(target, metaTarget);
|
|
963
972
|
this.extractMetadata(source, metaSource);
|
|
964
|
-
|
|
973
|
+
if (metaTarget.borrowed) throw new Error("Block is borrowed");
|
|
965
974
|
const available = metaTarget.byteCapacity / this.wordSize - metaTarget.occupied;
|
|
966
975
|
const count = Math.min(available, metaSource.occupied);
|
|
967
976
|
if (count === 0) return 0;
|
|
@@ -1016,8 +1025,8 @@ var Balloc = class {
|
|
|
1016
1025
|
const metaB = {};
|
|
1017
1026
|
this.extractMetadata(source, metaA);
|
|
1018
1027
|
this.extractMetadata(target, metaB);
|
|
1019
|
-
|
|
1020
|
-
|
|
1028
|
+
if (metaA.borrowed) throw new Error("Block is borrowed");
|
|
1029
|
+
if (metaB.borrowed) throw new Error("Block is borrowed");
|
|
1021
1030
|
this.controlAdapter.setUint32(metaA.headerAddress + ADDRESS_OFFSET, metaB.dataAddress, IS_LITTLE_ENDIAN);
|
|
1022
1031
|
this.controlAdapter.setUint32(metaB.headerAddress + ADDRESS_OFFSET, metaA.dataAddress, IS_LITTLE_ENDIAN);
|
|
1023
1032
|
this.controlAdapter.setUint8(metaA.headerAddress + CAPACITY_OFFSET, metaB.capacityExponent);
|
|
@@ -1106,6 +1115,26 @@ var Balloc = class {
|
|
|
1106
1115
|
this.controlAdapter.setUint32(meta.headerAddress + OCCUPIED_OFFSET, envelope.occupied, IS_LITTLE_ENDIAN);
|
|
1107
1116
|
return handler;
|
|
1108
1117
|
}
|
|
1118
|
+
// ========== التكامل وإدخال البيانات ==========
|
|
1119
|
+
/**
|
|
1120
|
+
* يحمّل البيانات من خلال محوّل متخصص.
|
|
1121
|
+
*
|
|
1122
|
+
* يتلقى المحوّل هذا المثيل ويستخدم واجهته العامة لتخصيص الكتل المطلوبة
|
|
1123
|
+
* وملئها. تقع مسؤولية تنظيم البيانات وتفسيرها بالكامل على عاتق المحوّل؛
|
|
1124
|
+
* أما Balloc فيفوّض العملية فقط ويعيد المعالج الناتج.
|
|
1125
|
+
*
|
|
1126
|
+
* العملية متزامنة. يجب على المحوّل إكمال إدخال البيانات قبل الإرجاع،
|
|
1127
|
+
* وهو مسؤول عن التراجع عن تخصيصاته إذا فشلت عملية التحميل.
|
|
1128
|
+
*
|
|
1129
|
+
* @param {FormatAdapter} adapter - محوّل يطبّق load(allocator).
|
|
1130
|
+
* @returns {number} المعالج الذي يعيده المحوّل.
|
|
1131
|
+
* @throws {TypeError} إذا لم يطبّق المحوّل الدالة load().
|
|
1132
|
+
* @throws {Error} إذا رفض المحوّل البنية التحتية المقدمة.
|
|
1133
|
+
*/
|
|
1134
|
+
loadData(adapter) {
|
|
1135
|
+
if (!adapter || typeof adapter.load !== "function") throw new TypeError("adapter must implement load(allocator)");
|
|
1136
|
+
return adapter.load(this);
|
|
1137
|
+
}
|
|
1109
1138
|
// ============= الأدوات المساعدة (UTILITIES) ==============
|
|
1110
1139
|
/**
|
|
1111
1140
|
* يمنع عمليات التعديل على كتلة معارة.
|
|
@@ -1116,7 +1145,8 @@ var Balloc = class {
|
|
|
1116
1145
|
* @returns {void}
|
|
1117
1146
|
* @throws {Error} إذا كانت الكتلة معارة بواسطة withView().
|
|
1118
1147
|
* @private
|
|
1119
|
-
|
|
1148
|
+
* @deprecated
|
|
1149
|
+
*/
|
|
1120
1150
|
assertNotBorrowed(headerAddress) {
|
|
1121
1151
|
if (this.controlBus[headerAddress + FLAGS_OFFSET] & VIEW_LOCK) {
|
|
1122
1152
|
throw new Error("Block is borrowed");
|