💾 Archived View for gemini.rmf-dev.com › repo › Vaati › Vgmi › files › 22ffd73ec9b4a78d694d3407e2bdb… captured on 2022-07-16 at 17:12:32. Gemini links have been rewritten to link to archived content

View Raw

More Information

-=-=-=-=-=-=-

0 /* See LICENSE file for copyright and license details. */

1 #ifdef MEM_CHECK

2 #include <stdlib.h>

3 #include <stdio.h>

4 #include <stdint.h>

5 #include <string.h>

6 #include <termbox.h>

7 #define allocation (__allocation)

8 #define allocationCount (__allocationCount)

9 #define output (__output)

10 #define PATH "mem.log"

11

12 void __free(void*, const char*, int, const char*);

13 void* __malloc(size_t, const char*, int, const char*);

14 void* __calloc(size_t, size_t, const char*, int, const char*);

15 void* __realloc(void*, size_t, const char*, int, const char*);

16 void __init();

17 void __check();

18 struct __allocation {

19 void* ptr;

20 int line;

21 size_t size;

22 const char* file;

23 const char* func;

24 };

25

26 struct __allocation* __allocation;

27 uint64_t __allocationCount;

28 FILE* __output = NULL;

29 int __warnings = 0;

30

31 void __init()

32 {

33 allocation=NULL;

34 allocationCount=0;

35 output = fopen(PATH,"wb");

36 }

37

38 void __free(void* ptr, const char* file, int line, const char* func)

39 {

40 uint32_t i=0;

41 if(ptr) {

42 while(i!=allocationCount && ptr!=allocation[i].ptr) i++;

43 if(i==allocationCount) {

44 fprintf(output, "(WARNING Freeing non-allocated memory) free : %!p(MISSING), " \

45 "%!s(MISSING), Line %!d(MISSING) : %!s(MISSING)\n",

46 ptr, file, line, func);

47 fflush(output);

48 #ifndef EXIT_ON_ERROR

49 __warnings++;

50 return;

51 #else

52 fclose(output);

53 tb_shutdown();

54 printf("mem_check detected a fatal error\n");

55 exit(0);

56 #endif

57 } else {

58 for(uint32_t j=i; j!=allocationCount-1; j++)

59 allocation[j] = allocation[j+1];

60 allocationCount--;

61 allocation = realloc(allocation,

62 sizeof(struct __allocation) * allocationCount);

63 }

64 }

65 if(i>allocationCount)

66 fprintf(output, "Error free : %!p(MISSING) | %!s(MISSING), Line %!d(MISSING) : %!s(MISSING)\n",

67 ptr, file, line, func);

68 else

69 fprintf(output, "Free : %!p(MISSING) | %!s(MISSING), Line %!d(MISSING) : %!s(MISSING)\n",

70 ptr, file, line, func);

71 fflush(output);

72 free(ptr);

73 }

74

75 void* __malloc(size_t size, const char* file, int line, const char* func)

76 {

77 void* ptr = malloc(size);

78 if(ptr) {

79 allocationCount++;

80 allocation=realloc(allocation,sizeof(struct __allocation)*allocationCount);

81 allocation[allocationCount-1].ptr=ptr;

82 allocation[allocationCount-1].line=line;

83 allocation[allocationCount-1].size=size;

84 allocation[allocationCount-1].file = file;

85 allocation[allocationCount-1].func = func;

86 }

87 fprintf(output, "malloc : %!p(MISSING), size : %!l(MISSING)d | %!s(MISSING), Line %!d(MISSING) : %!s(MISSING)\n",

88 ptr, size, file, line, func);

89 fflush(output);

90 return ptr;

91 }

92

93 void* __calloc(size_t num, size_t size, const char* file, int line, const char* func)

94 {

95 void* ptr = calloc(num, size);

96 if(ptr) {

97 allocationCount++;

98 allocation=realloc(allocation,sizeof(struct __allocation)*allocationCount);

99 allocation[allocationCount-1].ptr=ptr;

100 allocation[allocationCount-1].line=line;

101 allocation[allocationCount-1].size=size;

102 allocation[allocationCount-1].file = file;

103 allocation[allocationCount-1].func = func;

104 }

105 fprintf(output, "calloc : %!p(MISSING), size : %!l(MISSING)d | %!s(MISSING), Line %!d(MISSING) : %!s(MISSING)\n",

106 ptr, size, file, line, func);

107 fflush(output);

108 return ptr;

109 }

110

111 void* __realloc(void* ptr, size_t size, const char* file, int line, const char* func)

112 {

113 if (ptr==NULL) return __malloc(size, file, line, func);

114 void* _ptr = realloc(ptr, size);

115 if(_ptr != ptr) {

116 uint32_t i=0;

117 for(i=0; i!=allocationCount && ptr != allocation[i].ptr; i++) ;

118 if(allocationCount>i) {

119 allocation[i].ptr=_ptr;

120 allocation[i].line=line;

121 allocation[i].size=size;

122 allocation[i].file = file;

123 allocation[i].func = func;

124 }

125 }

126 fprintf(output, "realloc : %!p(MISSING), size : %!l(MISSING)d | %!s(MISSING), Line %!d(MISSING) : %!s(MISSING)\n",

127 _ptr, size, file, line, func);

128 fflush(output);

129 return _ptr;

130 }

131

132 void __check()

133 {

134 fprintf(output, "-----------------------\n");

135 if (allocationCount == 0)

136 fprintf(output, "No memory leak detected\n");

137 else {

138 fprintf(output, "%!l(MISSING)d memory leaks detected\n", allocationCount);

139 printf("WARNING: Memory leaks detected (%!l(MISSING)d)\n", allocationCount);

140 }

141 if (__warnings) {

142 fprintf(output, "%!d(MISSING) invalid memory operations detected\n", __warnings);

143 printf("WARNING: %!d(MISSING) invalid memory operations detected\n", __warnings);

144 }

145 for(uint32_t i=0; i!=allocationCount; i++)

146 fprintf(output, "Leak : %!p(MISSING), size : %!l(MISSING)d | %!s(MISSING), Line %!d(MISSING) : %!s(MISSING)\n",

147 allocation[i].ptr,

148 allocation[i].size,

149 allocation[i].file,

150 allocation[i].line,

151 allocation[i].func);

152 fclose(output);

153 }

154 #else

155 typedef int remove_iso_warning;

156 #endif

157