RI_memory.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #include "RI_types.h"
  2. RI_context context;
  3. #define RI_realloc(__ptr, __size) written_RI_realloc(__ptr, __size, __func__, __LINE__, context)
  4. #define RI_malloc(__size) written_RI_malloc(__size, __func__, __LINE__, context)
  5. #define RI_calloc(__nmemb, __size) written_RI_calloc(__nmemb, __size, __func__, __LINE__, context)
  6. #define RI_free(__ptr) written_RI_free(__ptr, __func__, __LINE__, context)
  7. void* written_RI_realloc(void *__ptr, size_t __size, const char *caller, int line, RI_context context){
  8. void *pointer = realloc(__ptr, __size);
  9. if (context.memory.debug_memory) {
  10. int current_allocation_index = 0;
  11. int checking = 1;
  12. while (checking){
  13. if (!context.memory.allocation_table[current_allocation_index].reallocated_free && context.memory.allocation_table[current_allocation_index].pointer == __ptr){
  14. context.memory.allocation_table[current_allocation_index].reallocated_free = 1;
  15. checking = 0;
  16. }
  17. current_allocation_index++;
  18. if (current_allocation_index >= context.memory.allocation_search_limit){
  19. checking = 0;
  20. }
  21. }
  22. if (context.memory.current_allocation_index >= context.memory.allocation_table_length){
  23. context.memory.allocation_table_length += 50;
  24. context.memory.allocation_search_limit += 50;
  25. context.memory.allocation_table = (RI_memory_allocation*)RI_realloc(context.memory.allocation_table, sizeof(RI_memory_allocation) * context.memory.allocation_table_length);
  26. }
  27. context.memory.allocation_table[context.memory.current_allocation_index].allocated = 1;
  28. context.memory.allocation_table[context.memory.current_allocation_index].reallocated_alloc = 1;
  29. context.memory.allocation_table[context.memory.current_allocation_index].reallocated_free = 0;
  30. context.memory.allocation_table[context.memory.current_allocation_index].freed = 0;
  31. context.memory.allocation_table[context.memory.current_allocation_index].line = line;
  32. context.memory.allocation_table[context.memory.current_allocation_index].pointer = pointer;
  33. context.memory.allocation_table[context.memory.current_allocation_index].size = __size;
  34. context.memory.current_allocation_index++;
  35. }
  36. return pointer;
  37. }
  38. void* written_RI_malloc(size_t __size, const char *caller, int line, RI_context context){
  39. void *pointer = malloc(__size);
  40. if (context.memory.debug_memory) {
  41. if (context.memory.current_allocation_index >= context.memory.allocation_table_length){
  42. context.memory.allocation_table_length += 50;
  43. context.memory.allocation_search_limit += 50;
  44. context.memory.allocation_table = (RI_memory_allocation*)RI_realloc(context.memory.allocation_table, sizeof(RI_memory_allocation) * context.memory.allocation_table_length);
  45. }
  46. context.memory.allocation_table[context.memory.current_allocation_index].allocated = 1;
  47. context.memory.allocation_table[context.memory.current_allocation_index].reallocated_free = 0;
  48. context.memory.allocation_table[context.memory.current_allocation_index].reallocated_alloc = 0;
  49. context.memory.allocation_table[context.memory.current_allocation_index].freed = 0;
  50. context.memory.allocation_table[context.memory.current_allocation_index].line = line;
  51. context.memory.allocation_table[context.memory.current_allocation_index].pointer = pointer;
  52. context.memory.allocation_table[context.memory.current_allocation_index].size = __size;
  53. context.memory.current_allocation_index++;
  54. }
  55. return pointer;
  56. }
  57. void* written_RI_calloc(size_t __nmemb, size_t __size, const char *caller, int line, RI_context context){
  58. void *pointer = calloc(__nmemb, __size);
  59. if (context.memory.debug_memory) {
  60. if (context.memory.current_allocation_index >= context.memory.allocation_table_length){
  61. context.memory.allocation_table_length += 50;
  62. context.memory.allocation_search_limit += 50;
  63. context.memory.allocation_table = (RI_memory_allocation*)RI_realloc(context.memory.allocation_table, sizeof(RI_memory_allocation) * context.memory.allocation_table_length);
  64. }
  65. context.memory.allocation_table[context.memory.current_allocation_index].allocated = 1;
  66. context.memory.allocation_table[context.memory.current_allocation_index].reallocated_free = 0;
  67. context.memory.allocation_table[context.memory.current_allocation_index].reallocated_alloc = 0;
  68. context.memory.allocation_table[context.memory.current_allocation_index].freed = 0;
  69. context.memory.allocation_table[context.memory.current_allocation_index].line = line;
  70. context.memory.allocation_table[context.memory.current_allocation_index].pointer = pointer;
  71. context.memory.allocation_table[context.memory.current_allocation_index].size = __size * __nmemb;
  72. context.memory.current_allocation_index++;
  73. }
  74. return pointer;
  75. }
  76. void written_RI_free(void *__ptr, const char *caller, int line){
  77. if (context.memory.debug_memory) {
  78. // size_t size = 0;
  79. int current_allocation_index = 0;
  80. int checking = 1;
  81. while (checking){
  82. if (!context.memory.allocation_table[current_allocation_index].reallocated_free && context.memory.allocation_table[current_allocation_index].pointer == __ptr){
  83. // i dont know what this does?
  84. // size = context.memory.allocation_table[current_allocation_index].size;
  85. context.memory.allocation_table[current_allocation_index].freed = 1;
  86. checking = 0;
  87. }
  88. current_allocation_index++;
  89. if (current_allocation_index >= context.memory.allocation_search_limit){
  90. checking = 0;
  91. }
  92. }
  93. }
  94. free(__ptr);
  95. }