custom_realloc.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. /**
  18. * \file custom_realloc.c
  19. * \author Ioannis Koutras (joko@microlab.ntua.gr)
  20. * \date November, 2011
  21. *
  22. * \brief Implementation of a function to change the size of an allocated
  23. * block.
  24. */
  25. #include <dmmlib/custom_realloc.h>
  26. #include <dmmlib/dmmlib.h>
  27. #include <dmmlib/initialize_allocator.h>
  28. #include "block_header.h"
  29. #ifndef WITH_MEMORY_SPACE_AWARENESS
  30. #include "other.h"
  31. #endif /* WITH_MEMORY_SPACE_AWARENESS */
  32. /**
  33. * Copy a memory area
  34. */
  35. static void * custom_memcpy(void* dest, const void* src, size_t n) {
  36. char *dst8, *src8;
  37. dst8 = (char *) dest;
  38. src8 = (char *) src;
  39. while(n--) {
  40. *dst8++ = *src8++;
  41. }
  42. return dest;
  43. }
  44. /**
  45. * \details The realloc() function tries to change the size of the allocation
  46. * pointed to by ptr to size, and returns ptr. realloc() creates a new
  47. * allocation, copies as much of the old data pointed to by ptr as will fit to
  48. * the new allocation, frees the old allocation and returns a pointer to the
  49. * allocated memory. If ptr is NULL, realloc() is identical to a call to
  50. * malloc() for size bytes.
  51. */
  52. void * custom_ahrealloc(allocator_t *allocator, heap_t *heap, void *ptr, size_t size) {
  53. void *new_ptr;
  54. size_t old_size;
  55. #ifndef WITH_MEMORY_SPACE_AWARENESS
  56. int heap_id;
  57. /* Go to the system allocator if none was given */
  58. if(allocator == NULL) {
  59. allocator = &systemallocator;
  60. if(allocator->initialized != true) {
  61. initialize_allocator(allocator);
  62. }
  63. }
  64. if(heap == NULL) {
  65. heap_id = map_thread_heap();
  66. heap = &allocator->heaps[heap_id];
  67. }
  68. #endif /* WITH_MEMORY_SPACE_AWARENESS */
  69. new_ptr = custom_ahmalloc(allocator, heap, size);
  70. if(ptr != NULL) {
  71. old_size = get_size(ptr);
  72. if(old_size < size) {
  73. new_ptr = custom_memcpy(new_ptr, ptr, old_size);
  74. } else {
  75. new_ptr = custom_memcpy(new_ptr, ptr, size);
  76. }
  77. custom_ahfree(allocator, heap, ptr);
  78. }
  79. return new_ptr;
  80. }
  81. #ifndef WITH_MEMORY_SPACE_AWARENESS
  82. void * custom_realloc(void *ptr, size_t size) {
  83. return custom_ahrealloc(NULL, NULL, ptr, size);
  84. }
  85. #endif /* WITH_MEMORY_SPACE_AWARENESS */