暂无描述
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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. }