slab.c 4.3 KB

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