free.c 2.9 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 <inttypes.h>
  25. #include "dmmlib/dmmlib.h"
  26. #include "tls_allocator.h"
  27. #include "slab.h"
  28. #include "default_rb.h"
  29. #include "dmmlib_trace.h"
  30. #include "locks.h"
  31. #include "other.h"
  32. #include "release_memory.h"
  33. #include "statistics.h"
  34. void free(void *ptr) {
  35. raw_block_header_t *owner_raw_block;
  36. if(ptr == NULL || ptr == (void *) 0xD34D) {
  37. return;
  38. }
  39. #if defined PARSE_ENV && defined WITH_MEM_TRACE
  40. get_tls_allocator();
  41. if(__builtin_expect (tls_allocator->initialized, true)) {
  42. #endif /* PARSE_ENV && WITH_MEM_TRACE */
  43. MEM_TRACE("dmmlib - f %p\n", ptr);
  44. #if defined PARSE_ENV && defined WITH_MEM_TRACE
  45. }
  46. #endif /* PARSE_ENV && WITH_MEM_TRACE */
  47. owner_raw_block = find_raw_block_owner(ptr);
  48. if(owner_raw_block->type == SLAB) {
  49. slab_rb_t *encapsulated_slab_rb = (slab_rb_t *)
  50. ((uintptr_t) owner_raw_block + sizeof(raw_block_header_t));
  51. DEALY_LOCK(owner_raw_block->lock);
  52. slab_free(encapsulated_slab_rb, ptr);
  53. DEALY_UNLOCK(owner_raw_block->lock);
  54. } else if(owner_raw_block->type == FREELIST) {
  55. DEFAULT_RB_T *encapsulated_rb = (DEFAULT_RB_T *)
  56. ((uintptr_t) owner_raw_block + sizeof(raw_block_header_t));
  57. DEALY_LOCK(owner_raw_block->lock);
  58. dmmlib_free(encapsulated_rb, ptr);
  59. DEALY_UNLOCK(owner_raw_block->lock);
  60. } else if(owner_raw_block->type == BIGBLOCK) {
  61. owner_raw_block = (raw_block_header_t *)
  62. ((uintptr_t) ptr - sizeof(raw_block_header_t));
  63. #ifdef WITH_ALLOCATOR_STATS
  64. #if !defined PARSE_ENV || (defined PARSE_ENV && !defined MEM_TRACE) || \
  65. !defined COALESCING_VARIABLE
  66. get_tls_allocator();
  67. #endif /* !PARSE_ENV || (PARSE_ENV && !MEM_TRACE) || !COALESCING_VARIABLE */
  68. DEALY_LOCK(tls_allocator->edit_lock);
  69. update_stats
  70. ( &tls_allocator->dmm_stats
  71. , FREE
  72. #ifdef REQUEST_SIZE_INFO
  73. , owner_raw_block->requested_size
  74. #endif /* REQUEST_SIZE_INFO */
  75. );
  76. tls_allocator->dmm_stats.total_mem_allocated -= owner_raw_block->size;
  77. DEALY_UNLOCK(tls_allocator->edit_lock);
  78. #endif /* WITH_ALLOCATOR_STATS */
  79. release_memory(owner_raw_block);
  80. }
  81. }