slab.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 OSCAR lab, 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 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 General Public License for more details.
  13. You should have received a copy of the GNU 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_defs.h"
  21. #include "pal_internal.h"
  22. #include "pal_error.h"
  23. #include "pal_debug.h"
  24. #include "linux_list.h"
  25. #include "api.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 4096 * 20480
  32. static char mem_pool[POOL_SIZE];
  33. static char *bump = mem_pool;
  34. static char *mem_pool_end = &mem_pool[POOL_SIZE];
  35. #else
  36. # define PAGE_SIZE (slab_alignment)
  37. #endif
  38. #define STARTUP_SIZE 8
  39. static inline void * __malloc (int size)
  40. {
  41. #if STATIC_SLAB == 1
  42. void * addr = (void *) bump;
  43. bump += size;
  44. if (bump >= mem_pool_end) {
  45. printf("Pal out of internal memory!\n");
  46. _DkProcessExit(-1);
  47. return NULL;
  48. }
  49. #else
  50. void * addr = NULL;
  51. _DkVirtualMemory(&addr, size, 0, PAL_PROT_READ|PAL_PROT_WRITE);
  52. #endif /* STATIC_SLAB != 1 */
  53. return addr;
  54. }
  55. #define system_malloc(size) __malloc(size)
  56. static inline void __free (void * addr, int size)
  57. {
  58. _DkVirtualMemoryFree(addr, size);
  59. }
  60. #define system_free(addr, size) __free(addr, size)
  61. #include "slabmgr.h"
  62. static SLAB_MGR slab_mgr = NULL;
  63. void init_slab_mgr (int alignment)
  64. {
  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 (int size)
  79. {
  80. #if PROFILING == 1
  81. unsigned long before_slab = _DkSystemTimeQuery();
  82. #endif
  83. void * ptr = slab_alloc(slab_mgr, size);
  84. /* the slab manger will always remain at least one byte of padding,
  85. so we can feel free to assign an offset at the byte prior to
  86. the pointer */
  87. if (ptr)
  88. *(((unsigned char *) ptr) - 1) = 0;
  89. #if PROFILING == 1
  90. pal_state.slab_time += _DkSystemTimeQuery() - before_slab;
  91. #endif
  92. return ptr;
  93. }
  94. void * remalloc (const void * mem, int size)
  95. {
  96. void * nmem = malloc(size);
  97. if (nmem)
  98. memcpy(nmem, mem, size);
  99. return nmem;
  100. }
  101. void * calloc (int nmem, int size)
  102. {
  103. void * ptr = malloc(nmem * size + size);
  104. void * old_ptr = ptr;
  105. if (ptr) {
  106. // align ptr to size
  107. ptr += size - 1 - ((uintptr_t) ptr + size - 1) % size;
  108. *(((unsigned char *) ptr) - 1) = ptr - old_ptr;
  109. }
  110. return ptr;
  111. }
  112. void free (void * ptr)
  113. {
  114. #if PROFILING == 1
  115. unsigned long before_slab = _DkSystemTimeQuery();
  116. #endif
  117. ptr -= *(((unsigned char *) ptr) - 1);
  118. slab_free(slab_mgr, ptr);
  119. #if PROFILING == 1
  120. pal_state.slab_time += _DkSystemTimeQuery() - before_slab;
  121. #endif
  122. }