slab.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /* Copyright (C) 2014 Stony Brook University
  2. This file is part of Graphene Library OS.
  3. Graphene Library OS is free software: you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public License
  5. as published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. Graphene Library OS is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  13. /*
  14. * slab.c
  15. *
  16. * This file contains implementation of PAL's internal memory allocator.
  17. */
  18. #include "api.h"
  19. #include "pal_internal.h"
  20. #ifndef NO_INTERNAL_ALLOC
  21. #include "pal_debug.h"
  22. #include "pal_defs.h"
  23. #include "pal_error.h"
  24. static int slab_alignment;
  25. static PAL_LOCK slab_mgr_lock = LOCK_INIT;
  26. #define SYSTEM_LOCK() _DkInternalLock(&slab_mgr_lock)
  27. #define SYSTEM_UNLOCK() _DkInternalUnlock(&slab_mgr_lock)
  28. #define SYSTEM_LOCKED() _DkInternalIsLocked(&slab_mgr_lock)
  29. #if STATIC_SLAB == 1
  30. #define POOL_SIZE 64 * 1024 * 1024 /* 64MB by default */
  31. static char mem_pool[POOL_SIZE];
  32. static void* bump = mem_pool;
  33. static void* mem_pool_end = &mem_pool[POOL_SIZE];
  34. #else
  35. #define ALLOC_ALIGNMENT slab_alignment
  36. #endif
  37. #define STARTUP_SIZE 2
  38. /* This function is protected by slab_mgr_lock. */
  39. static inline void* __malloc(int size) {
  40. void* addr = NULL;
  41. #if STATIC_SLAB == 1
  42. if (bump + size <= mem_pool_end) {
  43. addr = bump;
  44. bump += size;
  45. return addr;
  46. }
  47. #endif
  48. _DkVirtualMemoryAlloc(&addr, size, PAL_ALLOC_INTERNAL, PAL_PROT_READ | PAL_PROT_WRITE);
  49. return addr;
  50. }
  51. #define system_malloc(size) __malloc(size)
  52. static inline void __free(void* addr, int size) {
  53. if (!addr)
  54. return;
  55. #if STATIC_SLAB == 1
  56. if (addr >= (void*)mem_pool && addr < mem_pool_end)
  57. return;
  58. #endif
  59. _DkVirtualMemoryFree(addr, size);
  60. }
  61. #define system_free(addr, size) __free(addr, size)
  62. #include "slabmgr.h"
  63. static SLAB_MGR slab_mgr = NULL;
  64. void init_slab_mgr(int alignment) {
  65. if (slab_mgr)
  66. return;
  67. #if PROFILING == 1
  68. unsigned long before_slab = _DkSystemTimeQuery();
  69. #endif
  70. slab_alignment = alignment;
  71. slab_mgr = create_slab_mgr();
  72. if (!slab_mgr)
  73. INIT_FAIL(PAL_ERROR_NOMEM, "cannot initialize slab manager");
  74. #if PROFILING == 1
  75. pal_state.slab_time += _DkSystemTimeQuery() - before_slab;
  76. #endif
  77. }
  78. void* malloc(size_t size) {
  79. #if PROFILING == 1
  80. unsigned long before_slab = _DkSystemTimeQuery();
  81. #endif
  82. void* ptr = slab_alloc(slab_mgr, size);
  83. #ifdef DEBUG
  84. /* In debug builds, try to break code that uses uninitialized heap
  85. * memory by explicitly initializing to a non-zero value. */
  86. if (ptr)
  87. memset(ptr, 0xa5, size);
  88. #endif
  89. if (!ptr) {
  90. /*
  91. * Normally, the PAL should not run out of memory.
  92. * If malloc() failed internally, we cannot handle the
  93. * condition and must terminate the current process.
  94. */
  95. printf("******** Out-of-memory in PAL ********\n");
  96. _DkProcessExit(-ENOMEM);
  97. }
  98. #if PROFILING == 1
  99. pal_state.slab_time += _DkSystemTimeQuery() - before_slab;
  100. #endif
  101. return ptr;
  102. }
  103. // Copies data from `mem` to a newly allocated buffer of a specified size.
  104. void* malloc_copy(const void* mem, size_t size) {
  105. void* nmem = malloc(size);
  106. if (nmem)
  107. memcpy(nmem, mem, size);
  108. return nmem;
  109. }
  110. char* strdup(const char* s) {
  111. size_t len = strlen(s) + 1;
  112. char* new = malloc(len);
  113. if (new)
  114. memcpy(new, s, len);
  115. return new;
  116. }
  117. void* calloc(size_t nmem, size_t size) {
  118. void* ptr = malloc(nmem * size);
  119. if (ptr)
  120. memset(ptr, 0, nmem * size);
  121. return ptr;
  122. }
  123. void free(void* ptr) {
  124. if (!ptr)
  125. return;
  126. #if PROFILING == 1
  127. unsigned long before_slab = _DkSystemTimeQuery();
  128. #endif
  129. slab_free(slab_mgr, ptr);
  130. #if PROFILING == 1
  131. pal_state.slab_time += _DkSystemTimeQuery() - before_slab;
  132. #endif
  133. }
  134. #endif /* !NO_INTERNAL_ALLOC */