realloc.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 realloc.c
  19. * @author Ioannis Koutras (joko@microlab.ntua.gr)
  20. * @date September 2012
  21. *
  22. * @brief Implementation of realloc() call.
  23. */
  24. #include "dmmlib/dmmlib.h"
  25. #include <stdbool.h>
  26. #ifdef BITMAP_RB_ONLY
  27. #include "bitmap/bitmap.h"
  28. #include "bitmap/bitmap_rb.h"
  29. #endif /* BITMAP_RB_ONLY */
  30. #include "release_memory.h"
  31. void * realloc(void *ptr, size_t size) {
  32. raw_block_header_t *current_raw_block;
  33. bool found;
  34. if(ptr == NULL) {
  35. return malloc(size);
  36. }
  37. if(size == 0) {
  38. free(ptr);
  39. #ifdef BITMAP_RB_ONLY
  40. return malloc(CHUNK_HDR_SIZE + 32); // FIXME 32 <- minimum size
  41. #endif /* BITMAP_RB_ONLY */
  42. #ifdef FL_RB_ONLY
  43. return malloc((size_t) 32); // FIXME 32 <- minimum size
  44. #endif /* FL_RB_ONLY */
  45. }
  46. found = false;
  47. current_raw_block = systemallocator.raw_block_head;
  48. while(current_raw_block) {
  49. if(((char *)ptr > (char *)current_raw_block) &&
  50. ((char *)ptr < (char *)(current_raw_block) +
  51. current_raw_block->size)) {
  52. found = true;
  53. break;
  54. }
  55. current_raw_block = current_raw_block->next_raw_block;
  56. }
  57. if(found == true) {
  58. #ifdef BITMAP_RB_ONLY
  59. bitmap_rb_t *bitmap_rb;
  60. bitmap_rb = (bitmap_rb_t *)((char *)current_raw_block +
  61. sizeof(raw_block_header_t));
  62. return bitmap_realloc(bitmap_rb, ptr, size);
  63. #endif /* BITMAP_RB_ONLY */
  64. #ifdef FL_RB_ONLY
  65. return freelist_realloc(current_raw_block, ptr, size);
  66. #endif /* FL_RB_ONLY */
  67. } else {
  68. return NULL; // FIXME what about big blocks?
  69. }
  70. }