config.rs 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. //! Configuration.
  2. //!
  3. //! This module contains anything which can be tweaked and customized to the users preferences.
  4. use core::{intrinsics, cmp, fmt};
  5. /// The memtrim limit.
  6. ///
  7. /// Whenever this is exceeded, the allocator will try to free as much memory to the system
  8. /// as it can.
  9. pub const OS_MEMTRIM_LIMIT: usize = 200000000;
  10. /// Minimum size before a block is worthy to memtrim.
  11. pub const OS_MEMTRIM_WORTHY: usize = 4000;
  12. /// The fragmentation scale constant.
  13. ///
  14. /// This is used for determining the minimum avarage block size before locally memtrimming.
  15. pub const FRAGMENTATION_SCALE: usize = 10;
  16. /// The local memtrim limit.
  17. ///
  18. /// Whenever an local allocator has more free bytes than this value, it will be memtrimmed.
  19. pub const LOCAL_MEMTRIM_LIMIT: usize = 16384;
  20. /// The local memtrim chock.
  21. ///
  22. /// The local memtrimming will continue until the allocator has less memory (in bytes, of course)
  23. /// than this value.
  24. pub const LOCAL_MEMTRIM_STOP: usize = 1024;
  25. /// The default OOM handler.
  26. #[cold]
  27. pub fn default_oom_handler() -> ! {
  28. // Log some message.
  29. log(6, "ERROR", "\x1b[31;1mThe application ran out of memory. Aborting.", file!(), line!());
  30. unsafe {
  31. intrinsics::abort();
  32. }
  33. }
  34. /// Canonicalize a fresh allocation.
  35. ///
  36. /// The return value specifies how much _more_ space is requested to the fresh allocator.
  37. #[inline]
  38. pub fn extra_fresh(size: usize) -> usize {
  39. /// The multiplier.
  40. ///
  41. /// The factor determining the linear dependence between the minimum segment, and the acquired
  42. /// segment.
  43. const MULTIPLIER: usize = 2;
  44. /// The minimum extra size to be BRK'd.
  45. const MIN_EXTRA: usize = 512;
  46. /// The maximal amount of _extra_ bytes.
  47. const MAX_EXTRA: usize = 1024;
  48. cmp::max(MIN_EXTRA, cmp::min(MULTIPLIER * size, MAX_EXTRA))
  49. }
  50. /// Canonicalize a BRK request.
  51. ///
  52. /// Syscalls can be expensive, which is why we would rather accquire more memory than necessary,
  53. /// than having many syscalls acquiring memory stubs. Memory stubs are small blocks of memory,
  54. /// which are essentially useless until merge with another block.
  55. ///
  56. /// To avoid many syscalls and accumulating memory stubs, we BRK a little more memory than
  57. /// necessary. This function calculate the memory to be BRK'd based on the necessary memory.
  58. ///
  59. /// The return value specifies how much _more_ space is requested.
  60. // TODO: Move to shim.
  61. #[inline]
  62. pub fn extra_brk(size: usize) -> usize {
  63. // TODO: Tweak this.
  64. /// The BRK multiplier.
  65. ///
  66. /// The factor determining the linear dependence between the minimum segment, and the acquired
  67. /// segment.
  68. const MULTIPLIER: usize = 4;
  69. /// The minimum extra size to be BRK'd.
  70. const MIN_EXTRA: usize = 8192;
  71. /// The maximal amount of _extra_ bytes.
  72. const MAX_EXTRA: usize = 131072;
  73. cmp::max(MIN_EXTRA, cmp::min(MULTIPLIER * size, MAX_EXTRA))
  74. }