free.c 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. lock_global();
  55. SLIST_REMOVE(&systemallocator.bb_head, owner_raw_block,
  56. raw_block_header_s, pointers);
  57. unlock_global();
  58. #endif /* WITH_DEBUG */
  59. #ifdef WITH_ALLOCATOR_STATS
  60. lock_global();
  61. update_stats(&systemallocator.dmm_stats,
  62. FREE,
  63. #ifdef REQUEST_SIZE_INFO
  64. owner_raw_block->requested_size,
  65. #endif /* REQUEST_SIZE_INFO */
  66. owner_raw_block->size);
  67. unlock_global();
  68. #endif /* WITH_ALLOCATOR_STATS */
  69. /* release_memory(owner_raw_block); */
  70. }
  71. }