dmmlib.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * Copyright 2012 Institute of Communication and Computer Systems (ICCS)
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. */
  17. /**
  18. * @file dmmlib.c
  19. * @author Ioannis Koutras (joko@microlab.ntua.gr)
  20. * @date September 2012
  21. *
  22. * @brief Implementations of malloc(), free() and calloc() calls.
  23. */
  24. #include "dmmlib/dmmlib.h"
  25. #ifdef FL_RB_ONLY
  26. #include "freelist/freelist.h"
  27. #include "freelist/freelist_rb.h"
  28. #endif /* FL_RB_ONLY */
  29. #ifdef BITMAP_RB_ONLY
  30. #include "bitmap/bitmap.h"
  31. #include "bitmap/bitmap_rb.h"
  32. #endif /* BITMAP_RB_ONLY */
  33. #include "release_memory.h"
  34. #include <stdbool.h>
  35. #include <string.h> /* for memset() */
  36. #include "trace.h"
  37. void * malloc(size_t size) {
  38. raw_block_header_t *raw_block, *new_raw_block;
  39. size_t allocation_size;
  40. void *ptr;
  41. raw_block = systemallocator.raw_block_head;
  42. ptr = NULL;
  43. TRACE_1("dmmlib - malloc - request %zu bytes\n", size);
  44. /* Try to find a raw block available for allocation */
  45. while(raw_block != NULL) {
  46. ptr = dmmlib_malloc(raw_block, size);
  47. if(ptr != NULL) {
  48. break;
  49. }
  50. raw_block = raw_block->next_raw_block;
  51. }
  52. if(ptr == NULL) {
  53. #ifdef FL_RB_ONLY
  54. allocation_size = size + sizeof(raw_block_header_t) +
  55. sizeof(freelist_rb_t);
  56. #endif /* FL_RB_ONLY */
  57. #ifdef BITMAP_RB_ONLY
  58. allocation_size = size + sizeof(raw_block_header_t) +
  59. sizeof(bitmap_rb_t);
  60. #endif /* BITMAP_RB_ONLY */
  61. if(allocation_size < SYS_ALLOC_SIZE / 2) {
  62. allocation_size = SYS_ALLOC_SIZE;
  63. } else {
  64. ptr = (void *)create_new_raw_block(allocation_size, BIGBLOCK);
  65. if(ptr != NULL) {
  66. TRACE_1("dmmlib - malloc - allocated a whole raw block of %zu"
  67. " bytes at %p\n", allocation_size, (void *)ptr);
  68. ptr = (void *)((char *)ptr + sizeof(raw_block_header_t));
  69. }
  70. return ptr;
  71. }
  72. pthread_mutex_lock(&systemallocator.creation_mutex);
  73. #ifdef FL_RB_ONLY
  74. new_raw_block = create_new_raw_block(allocation_size, FREELIST);
  75. #endif /* FL_RB_ONLY */
  76. #ifdef BITMAP_RB_ONLY
  77. new_raw_block = create_new_raw_block(allocation_size, BITMAP);
  78. #endif /* BITMAP_RB_ONLY */
  79. if(new_raw_block != NULL) {
  80. new_raw_block->next_raw_block = systemallocator.raw_block_head;
  81. systemallocator.raw_block_head = new_raw_block;
  82. pthread_mutex_unlock(&systemallocator.creation_mutex);
  83. ptr = dmmlib_malloc(new_raw_block, size);
  84. }
  85. }
  86. return ptr;
  87. }
  88. void free(void *ptr) {
  89. raw_block_header_t *current_raw_block;
  90. bool found;
  91. if(ptr == NULL) {
  92. return;
  93. }
  94. found = false;
  95. current_raw_block = systemallocator.raw_block_head;
  96. while(current_raw_block) {
  97. if((char *)ptr - (char *)(current_raw_block) -
  98. sizeof(raw_block_header_t) < current_raw_block->size) {
  99. found = true;
  100. break;
  101. }
  102. current_raw_block = current_raw_block->next_raw_block;
  103. }
  104. if(found == true) {
  105. dmmlib_free(current_raw_block, ptr);
  106. } else { // It has to be a BIGBLOCK, just munmap it
  107. release_memory(ptr);
  108. current_raw_block = (raw_block_header_t *)((char *)ptr -
  109. sizeof(raw_block_header_t));
  110. TRACE_1("dmmlib - free - free'ing %zu bytes from raw block %p\n",
  111. current_raw_block->size, (void *)current_raw_block);
  112. }
  113. }
  114. void * realloc(void *ptr, size_t size) {
  115. raw_block_header_t *current_raw_block;
  116. bool found;
  117. if(ptr == NULL) {
  118. return malloc(size);
  119. }
  120. if(size == 0) {
  121. free(ptr);
  122. return malloc(CHUNK_HDR_SIZE + 32); // FIXME 32 <- minimum size
  123. }
  124. found = false;
  125. current_raw_block = systemallocator.raw_block_head;
  126. while(current_raw_block) {
  127. if((char *)ptr - (char *)(current_raw_block) -
  128. sizeof(raw_block_header_t) < current_raw_block->size) {
  129. found = true;
  130. break;
  131. }
  132. current_raw_block = current_raw_block->next_raw_block;
  133. }
  134. if(found == true) {
  135. return bitmap_realloc(current_raw_block, ptr, size);
  136. } else {
  137. return NULL;
  138. }
  139. }
  140. void * calloc(size_t nmemb, size_t size) {
  141. size_t i;
  142. char *buf[nmemb];
  143. if(nmemb == 0 || size == 0) {
  144. return NULL;
  145. }
  146. i = 0;
  147. while(i < nmemb) {
  148. buf[i] = (char*)malloc(size);
  149. memset(buf[i], '\0', size);
  150. i++;
  151. }
  152. return buf[0];
  153. }