GSI - Employe Self Service Mobile
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

48 lines
1.2 KiB

2 months ago
  1. "use strict";
  2. module.exports = pool;
  3. /**
  4. * An allocator as used by {@link util.pool}.
  5. * @typedef PoolAllocator
  6. * @type {function}
  7. * @param {number} size Buffer size
  8. * @returns {Uint8Array} Buffer
  9. */
  10. /**
  11. * A slicer as used by {@link util.pool}.
  12. * @typedef PoolSlicer
  13. * @type {function}
  14. * @param {number} start Start offset
  15. * @param {number} end End offset
  16. * @returns {Uint8Array} Buffer slice
  17. * @this {Uint8Array}
  18. */
  19. /**
  20. * A general purpose buffer pool.
  21. * @memberof util
  22. * @function
  23. * @param {PoolAllocator} alloc Allocator
  24. * @param {PoolSlicer} slice Slicer
  25. * @param {number} [size=8192] Slab size
  26. * @returns {PoolAllocator} Pooled allocator
  27. */
  28. function pool(alloc, slice, size) {
  29. var SIZE = size || 8192;
  30. var MAX = SIZE >>> 1;
  31. var slab = null;
  32. var offset = SIZE;
  33. return function pool_alloc(size) {
  34. if (size < 1 || size > MAX)
  35. return alloc(size);
  36. if (offset + size > SIZE) {
  37. slab = alloc(SIZE);
  38. offset = 0;
  39. }
  40. var buf = slice.call(slab, offset, offset += size);
  41. if (offset & 7) // align to 32 bit
  42. offset = (offset | 7) + 1;
  43. return buf;
  44. };
  45. }