block_header.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * Copyright 2011 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. #include "block_header.h"
  18. /**
  19. * \brief Get the address of the block header of a memory block.
  20. *
  21. * \param ptr The data part of the memory block.
  22. */
  23. static block_header_t * get_header(void *ptr);
  24. static block_header_t * get_header(void *ptr) {
  25. return (block_header_t *) ((char *) ptr - HEADER_SIZE);
  26. }
  27. void * get_next(void *ptr) {
  28. return get_header(ptr)->next;
  29. }
  30. size_t get_size(void *ptr) {
  31. return get_header(ptr)->size >> 1;
  32. }
  33. size_t get_requested_size(void *ptr) {
  34. return get_header(ptr)->requested_size;
  35. }
  36. size_t get_size_availability(void *ptr) {
  37. return get_header(ptr)->size;
  38. }
  39. void set_size(void *ptr, size_t size) {
  40. get_header(ptr)->size = size << 1;
  41. }
  42. void set_requested_size(void *ptr, size_t size) {
  43. get_header(ptr)->requested_size = size;
  44. }
  45. void mark_used(void *ptr) {
  46. get_header(ptr)->size |= 1;
  47. }
  48. void mark_free(void *ptr) {
  49. get_header(ptr)->size &= (~ 0x1);
  50. }
  51. void set_next(void *ptr, void *next_block) {
  52. get_header(ptr)->next = next_block;
  53. }
  54. #ifdef WITH_OWNERSHIP
  55. void set_owner(void *ptr, heap_t *heap_owner) {
  56. get_header(ptr)->heap_owner = heap_owner;
  57. }
  58. heap_t * get_owner(void *ptr) {
  59. return get_header(ptr)->heap_owner;
  60. }
  61. #endif /* WITH_OWNERSHIP */
  62. bool is_previous_free(void *ptr) {
  63. if (get_header(ptr)->previous_size & (size_t) 1) {
  64. return false;
  65. } else {
  66. return true;
  67. }
  68. }
  69. void set_previous_size_availability(void *ptr, size_t previous_size_availability) {
  70. get_header(ptr)->previous_size = previous_size_availability;
  71. }
  72. size_t get_previous_size(void *ptr) {
  73. return get_header(ptr)->previous_size >> 1;
  74. }
  75. size_t get_previous_size_availability(void *ptr) {
  76. return get_header(ptr)->previous_size;
  77. }
  78. void * get_previous(void *ptr) {
  79. return (void *) ((char *) ptr - HEADER_SIZE - get_previous_size(ptr));
  80. }
  81. void remove_block(void *block, void *starting_node) {
  82. void *current_node, *previous_node;
  83. /* Traverse a list starting from the starting node until block is found. */
  84. for(current_node = starting_node; current_node != NULL;
  85. current_node = get_next(current_node)) {
  86. if(current_node == block) {
  87. if(current_node == starting_node) {
  88. starting_node = get_next(block);
  89. } else {
  90. set_next(previous_node, get_next(block));
  91. }
  92. break;
  93. }
  94. previous_node = current_node;
  95. }
  96. }