@ebowwa/bun-native-page 0.2.3 → 0.2.4
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/dist/index.ts +34 -20
- package/package.json +4 -1
package/dist/index.ts
CHANGED
|
@@ -187,6 +187,16 @@ export function isPageAligned(ptr: bigint | number): boolean {
|
|
|
187
187
|
return result !== 0;
|
|
188
188
|
}
|
|
189
189
|
|
|
190
|
+
// ============================================================================
|
|
191
|
+
// Helper to convert Pointer to raw ptr for FFI calls
|
|
192
|
+
// ============================================================================
|
|
193
|
+
|
|
194
|
+
function toRawPtr(p: unknown): number {
|
|
195
|
+
// Bun FFI Pointer is an opaque number-like type
|
|
196
|
+
// Use ptr() to convert, then extract the raw value
|
|
197
|
+
return ptr(p as number) as unknown as number;
|
|
198
|
+
}
|
|
199
|
+
|
|
190
200
|
// ============================================================================
|
|
191
201
|
// Allocation API
|
|
192
202
|
// ============================================================================
|
|
@@ -221,16 +231,17 @@ export function allocPageAligned(
|
|
|
221
231
|
}
|
|
222
232
|
|
|
223
233
|
let freed = false;
|
|
234
|
+
const rawPtr = toRawPtr(actualPtr);
|
|
224
235
|
|
|
225
236
|
return {
|
|
226
|
-
ptr: BigInt(
|
|
237
|
+
ptr: BigInt(rawPtr),
|
|
227
238
|
size,
|
|
228
239
|
allocatedSize: actualSize,
|
|
229
240
|
get buffer() {
|
|
230
241
|
if (freed) {
|
|
231
242
|
throw new PageSizeError('Cannot access freed buffer');
|
|
232
243
|
}
|
|
233
|
-
return toArrayBuffer(
|
|
244
|
+
return toArrayBuffer(rawPtr as any, 0, actualSize);
|
|
234
245
|
},
|
|
235
246
|
free() {
|
|
236
247
|
if (!freed) {
|
|
@@ -271,15 +282,16 @@ export function mmapAnonymous(
|
|
|
271
282
|
}
|
|
272
283
|
|
|
273
284
|
let unmapped = false;
|
|
285
|
+
const rawPtr = toRawPtr(actualPtr);
|
|
274
286
|
|
|
275
287
|
return {
|
|
276
|
-
ptr: BigInt(
|
|
288
|
+
ptr: BigInt(rawPtr),
|
|
277
289
|
size: actualSize,
|
|
278
290
|
get buffer() {
|
|
279
291
|
if (unmapped) {
|
|
280
292
|
throw new PageSizeError('Cannot access unmapped region');
|
|
281
293
|
}
|
|
282
|
-
return toArrayBuffer(
|
|
294
|
+
return toArrayBuffer(rawPtr as any, 0, actualSize);
|
|
283
295
|
},
|
|
284
296
|
unmap() {
|
|
285
297
|
if (!unmapped) {
|
|
@@ -292,23 +304,23 @@ export function mmapAnonymous(
|
|
|
292
304
|
},
|
|
293
305
|
protect(prot: MemoryProtection) {
|
|
294
306
|
const protValue = PROTECTION_MAP[prot];
|
|
295
|
-
const result = ffi.mprotect(
|
|
307
|
+
const result = ffi.mprotect(rawPtr as any, BigInt(actualSize), protValue);
|
|
296
308
|
if (result !== 0) {
|
|
297
309
|
throw new ProtectionError(`mprotect failed with code ${result}`);
|
|
298
310
|
}
|
|
299
311
|
},
|
|
300
312
|
advise(advice: MemoryAdvice) {
|
|
301
313
|
const adviceValue = ADVICE_MAP[advice];
|
|
302
|
-
ffi.madvise(
|
|
314
|
+
ffi.madvise(rawPtr as any, BigInt(actualSize), adviceValue);
|
|
303
315
|
},
|
|
304
316
|
lock() {
|
|
305
|
-
const result = ffi.mlock(
|
|
317
|
+
const result = ffi.mlock(rawPtr as any, BigInt(actualSize));
|
|
306
318
|
if (result !== 0) {
|
|
307
319
|
throw new LockError(`mlock failed with code ${result}`);
|
|
308
320
|
}
|
|
309
321
|
},
|
|
310
322
|
unlock() {
|
|
311
|
-
const result = ffi.munlock(
|
|
323
|
+
const result = ffi.munlock(rawPtr as any, BigInt(actualSize));
|
|
312
324
|
if (result !== 0) {
|
|
313
325
|
throw new LockError(`munlock failed with code ${result}`);
|
|
314
326
|
}
|
|
@@ -344,15 +356,16 @@ export function mmapFile(
|
|
|
344
356
|
}
|
|
345
357
|
|
|
346
358
|
let unmapped = false;
|
|
359
|
+
const rawPtr = toRawPtr(actualPtr);
|
|
347
360
|
|
|
348
361
|
return {
|
|
349
|
-
ptr: BigInt(
|
|
362
|
+
ptr: BigInt(rawPtr),
|
|
350
363
|
size: actualSize,
|
|
351
364
|
get buffer() {
|
|
352
365
|
if (unmapped) {
|
|
353
366
|
throw new PageSizeError('Cannot access unmapped region');
|
|
354
367
|
}
|
|
355
|
-
return toArrayBuffer(
|
|
368
|
+
return toArrayBuffer(rawPtr as any, 0, actualSize);
|
|
356
369
|
},
|
|
357
370
|
unmap() {
|
|
358
371
|
if (!unmapped) {
|
|
@@ -362,17 +375,17 @@ export function mmapFile(
|
|
|
362
375
|
},
|
|
363
376
|
protect(prot: MemoryProtection) {
|
|
364
377
|
const protValue = PROTECTION_MAP[prot];
|
|
365
|
-
ffi.mprotect(
|
|
378
|
+
ffi.mprotect(rawPtr as any, BigInt(actualSize), protValue);
|
|
366
379
|
},
|
|
367
380
|
advise(advice: MemoryAdvice) {
|
|
368
381
|
const adviceValue = ADVICE_MAP[advice];
|
|
369
|
-
ffi.madvise(
|
|
382
|
+
ffi.madvise(rawPtr as any, BigInt(actualSize), adviceValue);
|
|
370
383
|
},
|
|
371
384
|
lock() {
|
|
372
|
-
ffi.mlock(
|
|
385
|
+
ffi.mlock(rawPtr as any, BigInt(actualSize));
|
|
373
386
|
},
|
|
374
387
|
unlock() {
|
|
375
|
-
ffi.munlock(
|
|
388
|
+
ffi.munlock(rawPtr as any, BigInt(actualSize));
|
|
376
389
|
},
|
|
377
390
|
};
|
|
378
391
|
}
|
|
@@ -399,15 +412,16 @@ export function mmapHuge(size: number): MappedRegion | null {
|
|
|
399
412
|
}
|
|
400
413
|
|
|
401
414
|
let unmapped = false;
|
|
415
|
+
const rawPtr = toRawPtr(actualPtr);
|
|
402
416
|
|
|
403
417
|
return {
|
|
404
|
-
ptr: BigInt(
|
|
418
|
+
ptr: BigInt(rawPtr),
|
|
405
419
|
size: actualSize,
|
|
406
420
|
get buffer() {
|
|
407
421
|
if (unmapped) {
|
|
408
422
|
throw new PageSizeError('Cannot access unmapped region');
|
|
409
423
|
}
|
|
410
|
-
return toArrayBuffer(
|
|
424
|
+
return toArrayBuffer(rawPtr as any, 0, actualSize);
|
|
411
425
|
},
|
|
412
426
|
unmap() {
|
|
413
427
|
if (!unmapped) {
|
|
@@ -417,17 +431,17 @@ export function mmapHuge(size: number): MappedRegion | null {
|
|
|
417
431
|
},
|
|
418
432
|
protect(prot: MemoryProtection) {
|
|
419
433
|
const protValue = PROTECTION_MAP[prot];
|
|
420
|
-
ffi.mprotect(
|
|
434
|
+
ffi.mprotect(rawPtr as any, BigInt(actualSize), protValue);
|
|
421
435
|
},
|
|
422
436
|
advise(advice: MemoryAdvice) {
|
|
423
437
|
const adviceValue = ADVICE_MAP[advice];
|
|
424
|
-
ffi.madvise(
|
|
438
|
+
ffi.madvise(rawPtr as any, BigInt(actualSize), adviceValue);
|
|
425
439
|
},
|
|
426
440
|
lock() {
|
|
427
|
-
ffi.mlock(
|
|
441
|
+
ffi.mlock(rawPtr as any, BigInt(actualSize));
|
|
428
442
|
},
|
|
429
443
|
unlock() {
|
|
430
|
-
ffi.munlock(
|
|
444
|
+
ffi.munlock(rawPtr as any, BigInt(actualSize));
|
|
431
445
|
},
|
|
432
446
|
};
|
|
433
447
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ebowwa/bun-native-page",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.4",
|
|
4
4
|
"description": "Cross-platform page-size aware memory operations for Bun - BufferPool, PageArena, mmap/munmap, mlock, mprotect, Windows file mapping",
|
|
5
5
|
"main": "dist/index.ts",
|
|
6
6
|
"exports": {
|
|
@@ -65,5 +65,8 @@
|
|
|
65
65
|
"homepage": "https://github.com/ebowwa/bun-native-page#readme",
|
|
66
66
|
"devDependencies": {
|
|
67
67
|
"bun-types": "^1.3.10"
|
|
68
|
+
},
|
|
69
|
+
"dependencies": {
|
|
70
|
+
"@ebowwa/bun-native-page": "0.2.3"
|
|
68
71
|
}
|
|
69
72
|
}
|