block_header_funcs.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. #include "freelist/block_header_funcs.h"
  18. #include <inttypes.h>
  19. block_header_t * get_header(void *ptr) {
  20. return (block_header_t *) ((uintptr_t) ptr - HEADER_SIZE);
  21. }
  22. size_t get_size(block_header_t *ptr) {
  23. return ptr->size >> 1;
  24. }
  25. #ifdef REQUEST_SIZE_INFO
  26. void set_requested_size(block_header_t *ptr, size_t size) {
  27. ptr->requested_size = size;
  28. }
  29. size_t get_requested_size(block_header_t *ptr) {
  30. return ptr->requested_size;
  31. }
  32. #endif /* REQUEST_SIZE_INFO */
  33. size_t get_size_availability(block_header_t *ptr) {
  34. return ptr->size;
  35. }
  36. void set_size_and_free(freelist_rb_t *raw_block, block_header_t *ptr,
  37. size_t size) {
  38. block_header_t *next_block_header;
  39. ptr->size = size << 1;
  40. next_block_header = get_dlnext(raw_block, ptr);
  41. if(next_block_header != NULL) {
  42. next_block_header->previous_size = size << 1;
  43. }
  44. }
  45. void set_size_and_used(freelist_rb_t *raw_block, block_header_t *ptr,
  46. size_t size) {
  47. block_header_t *next_block_header;
  48. ptr->size = size << 1;
  49. ptr->size |= 1;
  50. next_block_header = get_dlnext(raw_block, ptr);
  51. if(next_block_header != NULL) {
  52. next_block_header->previous_size = ptr->size;
  53. }
  54. }
  55. void mark_used(freelist_rb_t *raw_block, block_header_t *ptr) {
  56. block_header_t *next_block_header;
  57. ptr->size |= 1;
  58. next_block_header = get_dlnext(raw_block, ptr);
  59. if(next_block_header != NULL) {
  60. next_block_header->previous_size |= 1;
  61. }
  62. }
  63. void mark_free(freelist_rb_t *raw_block, block_header_t *ptr) {
  64. block_header_t *next_block_header;
  65. ptr->size &= (~ ((size_t) 0x1));
  66. next_block_header = get_dlnext(raw_block, ptr);
  67. if(next_block_header != NULL) {
  68. next_block_header->previous_size &= (~ ((size_t) 0x1));
  69. }
  70. }
  71. bool is_free(block_header_t *ptr) {
  72. if(ptr->size & (size_t) 1) {
  73. return false;
  74. } else {
  75. return true;
  76. }
  77. }
  78. bool is_previous_free(block_header_t *ptr) {
  79. if(ptr->previous_size & (size_t) 1) {
  80. return false;
  81. } else {
  82. return true;
  83. }
  84. }
  85. void set_previous_size_availability(block_header_t *ptr,
  86. size_t previous_size_availability) {
  87. ptr->previous_size = previous_size_availability;
  88. }
  89. size_t get_previous_size(block_header_t *ptr) {
  90. return ptr->previous_size >> 1;
  91. }
  92. size_t get_previous_size_availability(block_header_t *ptr) {
  93. return ptr->previous_size;
  94. }
  95. block_header_t * get_dlprevious(block_header_t *ptr) {
  96. return (block_header_t *) ((uintptr_t) ptr - HEADER_SIZE -
  97. get_previous_size(ptr));
  98. }
  99. block_header_t * get_dlnext(freelist_rb_t *raw_block, block_header_t *ptr) {
  100. if(raw_block->border_ptr != ptr) {
  101. return (block_header_t *) ((uintptr_t) ptr + get_size(ptr) +
  102. HEADER_SIZE);
  103. } else {
  104. return (block_header_t *) NULL;
  105. }
  106. }