free.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * Copyright 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 src/free.c
  19. * @author Ioannis Koutras (joko@microlab.ntua.gr)
  20. * @date September 2012
  21. *
  22. * @brief Implementation of the free() call.
  23. */
  24. #include "dmmlib/dmmlib.h"
  25. #include <inttypes.h>
  26. #include "locks.h"
  27. #include "default_rb.h"
  28. #include "other.h"
  29. #ifdef WITH_ALLOCATOR_STATS
  30. #include "statistics.h"
  31. #endif /* WITH_ALLOCATOR_STATS */
  32. #ifdef WITH_DEBUG
  33. #include "debug.h"
  34. #endif /* WITH_DEBUG */
  35. #include "release_memory.h"
  36. #include "trace.h"
  37. void free(void *ptr) {
  38. raw_block_header_t *owner_raw_block;
  39. if(ptr == NULL) {
  40. return;
  41. }
  42. TRACE_1("dmmlib - f %p\n", ptr);
  43. owner_raw_block = find_raw_block_owner(systemallocator.rb_head, ptr);
  44. if(owner_raw_block != NULL) {
  45. DEFAULT_RB_T *encapsulated_rb = (DEFAULT_RB_T *)
  46. ((uintptr_t) owner_raw_block + sizeof(raw_block_header_t));
  47. lock_raw_block(owner_raw_block);
  48. dmmlib_free(encapsulated_rb, ptr);
  49. unlock_raw_block(owner_raw_block);
  50. } else { // It has to be a BIGBLOCK, just munmap it
  51. owner_raw_block = (raw_block_header_t *)
  52. ((uintptr_t) ptr - sizeof(raw_block_header_t));
  53. #ifdef WITH_DEBUG
  54. SLIST_REMOVE(&systemallocator.bb_head, owner_raw_block,
  55. raw_block_header_s, pointers);
  56. #endif /* WITH_DEBUG */
  57. #ifdef WITH_ALLOCATOR_STATS
  58. lock_global();
  59. update_stats(&systemallocator.dmm_stats,
  60. FREE,
  61. #ifdef REQUEST_SIZE_INFO
  62. owner_raw_block->requested_size,
  63. #endif /* REQUEST_SIZE_INFO */
  64. owner_raw_block->size);
  65. #endif /* WITH_ALLOCATOR_STATS */
  66. unlock_global();
  67. release_memory(owner_raw_block);
  68. }
  69. }