mss.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /* MSS -- Memory Supervision System version 1.2
  2. * Copyright (C) 1998 Juan Jesus Alcolea Picazo and Peter Palotas
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. *
  18. * You can contact the authors of this library at the following e-mail
  19. * addreses. For more information, look in the documentation that came
  20. * with this library.
  21. *
  22. * Juan Jesus Alcolea Picazo, a920101@zipi.fi.upm.es
  23. * Peter Palotas, blizzar@hem1.passagen.se
  24. *
  25. */
  26. #ifndef __mss_h__
  27. #define __mss_h__
  28. /*************************************************************************/
  29. /* COMPILER SPECIFIC OPTIONS */
  30. /*************************************************************************/
  31. /* MSS_PTR_SIZE_T should be defined to an integer type that is large enough
  32. to hold the value of a pointer. */
  33. #define MSS_PTR_SIZE_T size_t
  34. /* MSS_PRINTF_SIZE_T should be defined to a printf() type-specifier string
  35. that will correctly print an argument of the type #defined by
  36. MSS_PTR_SIZE_T. (Ex. if MSS_PTR_SIZE_T is #defined to be 'long'
  37. MSS_PRINTF_SIZE_T should be "%l").
  38. (If MSS_PTR_SIZE_T is set to 'size_t' this should be "%lu" under DJGPP and
  39. "%u" under Linux GCC). */
  40. #define MSS_PRINTF_SIZE_T "%lu"
  41. /* MSS_FUNCTION_MACRO should represent a predefined macro or something like
  42. that, which will contain the current function name. (Set it to "unknown"
  43. or something like that if there is no such thing on your compiler.
  44. (GNU C Uses __PRETTY_FUNCTION__ or __FUNCTION__). */
  45. //#define MSS_FUNCTION_MACRO __PRETTY_FUNCTION__
  46. #define MSS_FUNCTION_MACRO "(unknown function)"
  47. /* Some compiler needs a redefinition of new[] and delete[], some don't.
  48. If your compiler does, define this, otherwise don't. (GCC needs this). */
  49. #define MSS_DEFINE_NEW_DELETE_ARRAY
  50. /* MSS_USE_EXCEPTIONS should be defined if your compiler supports exceptions.
  51. If you get a warning/error about operator delete throwing diffrent
  52. exceptions try enabling/disabling this, that should help, if not, don't
  53. ask me, I don't know anything about exceptions yet. (GCC 2.8.0 supports
  54. exceptions, earlier versions does not, well, it does support them a little,
  55. but this should be undefined if you use any version earlier than GCC 2.8.0.
  56. (With PGCC-2.90.23 based on EGCS 1.0.1 this should be defined).
  57. Anyway, if you get the errors above, try undefining this if defined, and
  58. defining it if undefined. */
  59. #define MSS_USE_EXCEPTIONS
  60. /* MSS_DTOR_AFTER_ATEXIT should be defined in case your compiler will run
  61. the destructors of global objects AFTER any registered atexit functions.
  62. It is not essential that this setting is correct, but it will make the
  63. logfile look nicer :)
  64. If your compiler is advanced enough to follow the C++ standard that says
  65. that any "exit functions", including both destructors of global objects
  66. and functions registered with atexit should be called in the reverse order
  67. that they were registered, your compiler will probably mix atexit
  68. functions and destructors of global objects. In that case you will just
  69. have to test by toggeling this option and see which give the nicest
  70. looking result. Remember, whatever setting you choose for this option,
  71. a crash or severe error will NEVER occur. */
  72. /* #define MSS_DTOR_AFTER_ATEXIT */
  73. /* These macros are for multithreading environments. They should disable
  74. respectively enable threading. (If you work in a non-multithreading
  75. environment or you are certain that two threads never will use MSS at
  76. the same time, you may safely define these to nothing. */
  77. #define MSS_DISABLE_THREADING_FUNC
  78. #define MSS_ENABLE_THREADING_FUNC
  79. /**************************************************************************/
  80. /* DO NOT CHANGE ANYTHING BELOW THIS LINE */
  81. /**************************************************************************/
  82. #include <stdlib.h>
  83. #include <string.h>
  84. #define MSS_VERSION "1.2"
  85. #ifdef __cplusplus
  86. extern "C"
  87. {
  88. #endif /* !__cplusplus */
  89. /****************/
  90. /* USER.C */
  91. /****************/
  92. void mss_enter_scope(const char *filename, const char *function,
  93. unsigned long line);
  94. void mss_leave_scope(const char *filename, const char *function,
  95. unsigned long line);
  96. void mss_unregister_constant_block(void *ptr, const char *filename,
  97. const char *function, unsigned long line);
  98. void mss_register_constant_block(void *ptr, const char *filename,
  99. const char *function, unsigned long line);
  100. unsigned int mss_currently_used_mem(void);
  101. unsigned int mss_maximum_used_nem(void);
  102. unsigned int mss_number_of_allocated_blocks(void);
  103. void mss_set_block_label(void *ptr, const char *message,
  104. const char *filename, const char *function, unsigned long line);
  105. void mss_dump_block(void *ptr, const char *file, const char *filename,
  106. const char *function, unsigned long line);
  107. /****************/
  108. /* ALLOC.C */
  109. /****************/
  110. #define MSS_BY_NEW 1
  111. #define MSS_BY_NEW_ARRAY 2
  112. #define MSS_BY_DELETE 3
  113. #define MSS_BY_DELETE_ARRAY 4
  114. #define MSS_BY_MALLOC 5
  115. #define MSS_BY_REALLOC 6
  116. #define MSS_BY_CALLOC 7
  117. #define MSS_BY_FREE 8
  118. #define MSS_BY_XMALLOC 9
  119. #define MSS_BY_XREALLOC 10
  120. #define MSS_BY_XFREE 11
  121. #define MSS_BY_CFREE 12
  122. #define MSS_BY_STRDUP 13
  123. void *mss_malloc(size_t s, int called_by, const char *filename,
  124. const char *function, unsigned long line);
  125. char *mss_strdup(const char *source, const char *filename,
  126. const char *function, unsigned long line);
  127. void *mss_realloc(void *ptr, size_t size, int called_by,
  128. const char *filename, const char *function, unsigned long line);
  129. void mss_free(void *ptr, int called_by, const char *filename,
  130. const char *function, unsigned long line);
  131. /****************/
  132. /* CHECK.C */
  133. /****************/
  134. void mss_check_pointer_validity(void *ptr, const char *filename,
  135. const char *function, unsigned long line);
  136. void mss_check_all_blocks(const char *filename, const char *function,
  137. unsigned long line);
  138. void mss_check_block(void *ptr, const char *filename, const char *function,
  139. unsigned long line);
  140. /****************/
  141. /* CPPSPEC.CC */
  142. /****************/
  143. #ifdef __cplusplus
  144. } /* End of extern "C" */
  145. extern unsigned int MSS_DEL_LINE;
  146. extern char *MSS_DEL_FILENAME;
  147. extern char *MSS_DEL_FUNCTION;
  148. /* Overloaded and redefined operators */
  149. void *operator new(size_t s);
  150. void *operator new(size_t s, const char *filename, const char *function,
  151. unsigned long line);
  152. void operator delete(void *p)
  153. #ifdef MSS_USE_EXCEPTIONS
  154. throw()
  155. #endif
  156. ; /* Yes, this semicolon has to be here! */
  157. #ifdef MSS_DEFINE_NEW_DELETE_ARRAY
  158. void *operator new[] (size_t s);
  159. void *operator new[] (size_t s, const char *filename,
  160. const char *function, unsigned long line);
  161. void operator delete[] (void *p)
  162. #ifdef MSS_USE_EXCEPTIONS
  163. throw()
  164. #endif /* !MSS_USE_EXCEPTIONS */
  165. ; /* Yes, this semicolon has to be here! */
  166. #endif /* !MSS_DEFINE_NEW_DELETE_ARRAY */
  167. extern "C"
  168. {
  169. #endif /* !__cplusplus */
  170. /****************/
  171. /* LOG.C */
  172. /****************/
  173. typedef int (*MssCallBackFunc) (char *outbuf,
  174. void *blkptr,
  175. unsigned int blklen,
  176. const char *blklab,
  177. const char *blksrc, const char *blkfnc, unsigned int blklin);
  178. void mss_log_block_list_filtered(const char *filename,
  179. const char *function, unsigned int line,
  180. MssCallBackFunc callback_func);
  181. void mss_log_msg(const char *msg, const char *filename,
  182. const char *function, unsigned long line);
  183. void mss_log_disable(const char *filename, const char *function,
  184. unsigned long line);
  185. void mss_log_enable(const char *filename, const char *function,
  186. unsigned long line);
  187. void mss_log_info(const char *filename, const char *function,
  188. unsigned long line);
  189. void mss_log_internal_info(const char *filename, const char *function,
  190. unsigned long line);
  191. void mss_log_list_blocks(const char *filename, const char *function,
  192. unsigned long line);
  193. /****************/
  194. /* INIT.C */
  195. /****************/
  196. void mss_startup(void);
  197. #ifdef __cplusplus
  198. }
  199. #endif /* !__cplusplus */
  200. /****************/
  201. /* User Macros */
  202. /****************/
  203. /* Don't define these if in MSS source */
  204. #ifndef __mss_internal__
  205. /* If MSS is not defined, we do not use MSS either ;) */
  206. #ifdef MSS
  207. #ifdef __cplusplus
  208. #define new new(__FILE__, MSS_FUNCTION_MACRO, __LINE__)
  209. #define delete MSS_DEL_FILENAME = __FILE__, MSS_DEL_LINE = __LINE__, MSS_DEL_FUNCTION = MSS_FUNCTION_MACRO, delete
  210. #endif /* !__cplusplus */
  211. #define malloc(s) mss_malloc(s, MSS_BY_MALLOC, __FILE__, MSS_FUNCTION_MACRO, __LINE__)
  212. #define xmalloc(s) mss_malloc(s, MSS_BY_XMALLOC, __FILE__, MSS_FUNCTION_MACRO, __LINE__)
  213. #define free(p) mss_free(p, MSS_BY_FREE, __FILE__, MSS_FUNCTION_MACRO, __LINE__)
  214. #define realloc(p, s) mss_realloc(p, s, MSS_BY_REALLOC, __FILE__, MSS_FUNCTION_MACRO, __LINE__)
  215. #define xrealloc(p, s) mss_realloc(p, s, MSS_BY_XREALLOC, __FILE__, MSS_FUNCTION_MACRO, __LINE__)
  216. #define calloc(num, size) mss_malloc((num) * (size), MSS_BY_CALLOC, __FILE__, MSS_FUNCTION_MACRO, __LINE__)
  217. #define xfree(p) mss_free(p, MSS_BY_XFREE, __FILE__, MSS_FUNCTION_MACRO, __LINE__)
  218. #define cfree(p) mss_free(p, MSS_BY_CFREE, __FILE__, MSS_FUNCTION_MACRO, __LINE__)
  219. #define strdup(s) mss_strdup(s, __FILE__, MSS_FUNCTION_MACRO, __LINE__)
  220. #define MSS_ENTER_SCOPE mss_enter_scope(__FILE__, MSS_FUNCTION_MACRO, __LINE__)
  221. #define MSS_LEAVE_SCOPE mss_leave_scope(__FILE__, MSS_FUNCTION_MACRO, __LINE__)
  222. #define MSS_LOG_INFO mss_log_info(__FILE__, MSS_FUNCTION_MACRO, __LINE__)
  223. #define MSS_LOG_INTERNAL_INFO mss_log_internal_info(__FILE__, MSS_FUNCTION_MACRO, __LINE__)
  224. #define MSS_LOG_BLOCK_LIST mss_log_list_blocks(__FILE__, MSS_FUNCTION_MACRO, __LINE__)
  225. #define MSS_LOG_BLOCK_LIST_FILTERED(callback_func) mss_log_block_list_filtered(__FILE__, MSS_FUNCTION_MACRO, __LINE__, callback_func)
  226. #define MSS_LOG_MSG(m) mss_log_msg(m, __FILE__, MSS_FUNCTION_MACRO, __LINE__)
  227. #define MSS_CHECK_ALL_BLOCKS mss_check_all_blocks(__FILE__, MSS_FUNCTION_MACRO, __LINE__)
  228. #define MSS_CHECK_POINTER_VALIDITY(ptr) mss_check_pointer_validity(ptr, __FILE__, MSS_FUNCTION_MACRO, __LINE__)
  229. #define MSS_CHECK_BLOCK_AT(ptr) mss_check_block(ptr, __FILE__, MSS_FUNCTION_MACRO, __LINE__)
  230. #define MSS_DISABLE_LOG_OUTPUT mss_log_disable(__FILE__, MSS_FUNCTION_MACRO, __LINE__)
  231. #define MSS_ENABLE_LOG_OUTPUT mss_log_enable(__FILE__, MSS_FUNCTION_MACRO, __LINE__)
  232. #define MSS_REGISTER_CONSTANT_BLOCK(ptr) mss_register_constant_block(ptr, __FILE__, MSS_FUNCTION_MACRO, __LINE__)
  233. #define MSS_UNREGISTER_CONSTANT_BLOCK(ptr) mss_unregister_constant_block(ptr, __FILE__, MSS_FUNCTION_MACRO, __LINE__)
  234. #define MSS_CURRENTLY_USED_MEM mss_currently_used_mem()
  235. #define MSS_MAXIMUM_USED_MEM mss_maximum_used_nem()
  236. #define MSS_NUMBER_OF_ALLOCATED_BLOCKS mss_number_of_allocated_blocks()
  237. #define MSS_STARTUP mss_startup()
  238. #define MSS_SET_BLOCK_LABEL(ptr, str) mss_set_block_label(ptr, str, __FILE__, MSS_FUNCTION_MACRO, __LINE__)
  239. #define MSS_DUMP_BLOCK(ptr, filename) mss_dump_block(ptr, filename, __FILE__, MSS_FUNCTION_MACRO, __LINE__)
  240. #else /* #ifndef MSS */
  241. /* We define these macros to nothing if MSS is undefined */
  242. #define MSS_ENTER_SCOPE ;
  243. #define MSS_LEAVE_SCOPE ;
  244. #define MSS_LOG_INFO ;
  245. #define MSS_LOG_INTERNAL_INFO ;
  246. #define MSS_LOG_BLOCK_LIST ;
  247. #define MSS_LOG_BLOCK_LIST_FILTERED(callback_func) ;
  248. #define MSS_LOG_MSG(m) ;
  249. #define MSS_CHECK_ALL_BLOCKS ;
  250. #define MSS_CHECK_POINTER_VALIDITY(ptr) ;
  251. #define MSS_CHECK_BLOCK_AT(ptr) ;
  252. #define MSS_DISABLE_LOG_OUTPUT ;
  253. #define MSS_ENABLE_LOG_OUTPUT ;
  254. #define MSS_REGISTER_CONSTANT_BLOCK(ptr) ;
  255. #define MSS_UNREGISTER_CONSTANT_BLOCK(ptr) ;
  256. #define MSS_CURRENTLY_USED_MEM 0
  257. #define MSS_MAXIMUM_USED_MEM 0
  258. #define MSS_NUMBER_OF_ALLOCATED_BLOCKS ;
  259. #define MSS_STARTUP ;
  260. #define MSS_SET_BLOCK_LABEL(ptr, str) ;
  261. #define MSS_DUMP_BLOCK(ptr, filename) ;
  262. #endif /* !MSS */
  263. #endif /* !__mss_internal__ */
  264. #endif /* !__mss_h__ */