slab.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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_debug.h"
  23. #include "linux_list.h"
  24. #include "api.h"
  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. #if STATIC_SLAB == 1
  29. # define POOL_SIZE 4096 * 20480
  30. static char mem_pool[POOL_SIZE];
  31. static char *bump = mem_pool;
  32. static char *mem_pool_end = &mem_pool[POOL_SIZE];
  33. #else
  34. # define PAGE_SIZE 4096
  35. #endif
  36. static inline void * __malloc (size_t size)
  37. {
  38. #if STATIC_SLAB == 1
  39. void * addr = (void *) bump;
  40. bump += size;
  41. if (bump >= mem_pool_end) {
  42. printf("Pal out of internal memory!\n");
  43. DkProcessExit(-1);
  44. return NULL;
  45. }
  46. #else
  47. void * addr = NULL;
  48. _DkVirtualMemory(&addr, size, 0, PAL_PROT_READ|PAL_PROT_WRITE);
  49. #endif /* STATIC_SLAB != 1 */
  50. return addr;
  51. }
  52. #define system_malloc(size) __malloc(size)
  53. static inline void __free (void * addr, size_t size)
  54. {
  55. _DkVirtualMemoryFree(addr, size);
  56. }
  57. #define system_free(addr, size) __free(addr, size)
  58. #include "slabmgr.h"
  59. static SLAB_MGR slab_mgr = NULL;
  60. void init_slab_mgr (void)
  61. {
  62. if (!slab_mgr) {
  63. slab_mgr = create_slab_mgr();
  64. }
  65. }
  66. void * malloc (int size)
  67. {
  68. void * ptr = slab_alloc(slab_mgr, size);
  69. /* the slab manger will always remain at least one byte of padding,
  70. so we can feel free to assign an offset at the byte prior to
  71. the pointer */
  72. if (ptr)
  73. *(((unsigned char *) ptr) - 1) = 0;
  74. return ptr;
  75. }
  76. void * remalloc (const void * mem, int size)
  77. {
  78. void * nmem = malloc(size);
  79. if (nmem)
  80. memcpy(nmem, mem, size);
  81. return nmem;
  82. }
  83. void * calloc (int nmem, int size)
  84. {
  85. void * ptr = malloc(nmem * size + size);
  86. void * old_ptr = ptr;
  87. if (ptr) {
  88. // align ptr to size
  89. ptr += size - 1 - ((uintptr_t) ptr + size - 1) % size;
  90. *(((unsigned char *) ptr) - 1) = ptr - old_ptr;
  91. }
  92. return ptr;
  93. }
  94. void free (void * ptr)
  95. {
  96. ptr -= *(((unsigned char *) ptr) - 1);
  97. slab_free(slab_mgr, ptr);
  98. }