kduma.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. * KDUMA - Kernel Mode Red-Zone memory allocator.
  3. * Copyright (C) 2006 Michael Eddington <meddington@gmail.com>
  4. * Copyright (C) 2006 Eric Rachner <eric@rachner.us>
  5. * License: GNU GPL (GNU General Public License, see COPYING-GPL)
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. *
  21. */
  22. /* $Id$ */
  23. /*
  24. * #include <stdlib.h>
  25. *
  26. * You must include <stdlib.h> before including <duma.h> !
  27. *
  28. */
  29. /* for enabling inclusion of duma.h after inclusion of efencint.h */
  30. /* remove previous definitions */
  31. #include "noduma.h"
  32. #ifndef __KDUMA_H__
  33. #define __KDUMA_H__
  34. #include "duma_config.h"
  35. #ifdef __cplusplus
  36. #define DUMA_EXTERN_C extern "C"
  37. #else
  38. #define DUMA_EXTERN_C extern
  39. #endif
  40. /* global DUMA variables */
  41. DUMA_EXTERN_C int DUMA_OUTPUT_DEBUG;
  42. DUMA_EXTERN_C int DUMA_OUTPUT_STDOUT;
  43. DUMA_EXTERN_C int DUMA_OUTPUT_STDERR;
  44. DUMA_EXTERN_C char* DUMA_OUTPUT_FILE;
  45. DUMA_EXTERN_C int DUMA_OUTPUT_STACKTRACE;
  46. DUMA_EXTERN_C int DUMA_PROTECT_BELOW;
  47. DUMA_EXTERN_C size_t DUMA_ALIGNMENT;
  48. DUMA_EXTERN_C int DUMA_FILL;
  49. DUMA_EXTERN_C struct _DUMA_Slot * _duma_allocList;
  50. #ifndef DUMA_NO_CPP_SUPPORT
  51. DUMA_EXTERN_C void * _duma_cxx_null_addr;
  52. #endif
  53. #endif /* DUMA_EXTERNS_DECLARED */
  54. /* Enum: _DUMA_Allocator
  55. *
  56. * Passed to duma allocator such as we know who
  57. * is calling us.
  58. */
  59. enum _DUMA_Allocator
  60. {
  61. EFA_INT_ALLOC
  62. , EFA_INT_DEALLOC
  63. , EFA_MALLOC
  64. , EFA_CALLOC
  65. , EFA_FREE
  66. , EFA_MEMALIGN
  67. , EFA_REALLOC
  68. , EFA_VALLOC
  69. , EFA_STRDUP
  70. , EFA_NEW_ELEM
  71. , EFA_DEL_ELEM
  72. , EFA_NEW_ARRAY
  73. , EFA_DEL_ARRAY
  74. /* use following enums when calling _duma_allocate()/_duma_deallocate()
  75. * from user defined member operators
  76. */
  77. , EFA_MEMBER_NEW_ELEM
  78. , EFA_MEMBER_DEL_ELEM
  79. , EFA_MEMBER_NEW_ARRAY
  80. , EFA_MEMBER_DEL_ARRAY
  81. };
  82. /* Enum: _DUMA_FailReturn
  83. *
  84. * Unknown use.
  85. */
  86. enum _DUMA_FailReturn
  87. {
  88. DUMA_FAIL_NULL
  89. , DUMA_FAIL_ENV
  90. };
  91. DUMA_EXTERN_C void _duma_init(void);
  92. DUMA_EXTERN_C void _duma_assert(const char * exprstr, const char * filename, int lineno);
  93. DUMA_EXTERN_C void duma_init(void);
  94. DUMA_EXTERN_C void * _duma_allocate(size_t alignment, size_t userSize, int protectBelow, int fillByte, int protectAllocList, enum _DUMA_Allocator allocator, enum _DUMA_FailReturn fail, const char * filename, int lineno);
  95. DUMA_EXTERN_C void _duma_deallocate(void * baseAdr, int protectAllocList, enum _DUMA_Allocator allocator, const char * filename, int lineno);
  96. DUMA_EXTERN_C void * _duma_kalloc(size_t size, int flags, const char * filename, int lineno);
  97. DUMA_EXTERN_C void _duma_kfree(void *baseAdr, const char * filename, int lineno);
  98. DUMA_EXTERN_C void * _duma_valloc(size_t size, const char * filename, int lineno);
  99. DUMA_EXTERN_C void * _duma_vfree(void *baseAdr, const char * filename, int lineno);
  100. DUMA_EXTERN_C char * _duma_strdup(const char *str, const char * filename, int lineno);
  101. DUMA_EXTERN_C void * _duma_memcpy(void *dest, const void *src, size_t size, const char * filename, int lineno);
  102. DUMA_EXTERN_C void * _duma_memmove(void *dest, const void *src, size_t size);
  103. DUMA_EXTERN_C char * _duma_strcpy(char *dest, const char *src, const char * filename, int lineno);
  104. DUMA_EXTERN_C char * _duma_strncpy(char *dest, const char *src, size_t size, const char * filename, int lineno);
  105. DUMA_EXTERN_C char * _duma_strcat(char *dest, const char *src, const char * filename, int lineno);
  106. DUMA_EXTERN_C char * _duma_strncat(char *dest, const char *src, size_t size, const char * filename, int lineno);
  107. DUMA_EXTERN_C void DUMA_newFrame(void);
  108. DUMA_EXTERN_C void DUMA_delFrame(void);
  109. #define kalloc(SIZE, FLAGS) _duma_kalloc(SIZE, FLAGS, __FILE__, __LINE__)
  110. #define kfree(BASEADR) _duma_kfree(BASEADR, __FILE__, __LINE__)
  111. #define valloc(SIZE) _duma_valloc(SIZE, __FILE__, __LINE__)
  112. #define vfree(BASEADR) _duma_vfree(BASEADR, __FILE__, __LINE__)
  113. #define strdup(STR) _duma_strdup(STR, __FILE__, __LINE__)
  114. #define memcpy(DEST, SRC, SIZE) _duma_memcpy(DEST, SRC, SIZE, __FILE__, __LINE__)
  115. #define memmove(DEST, SRC, SIZE) _duma_memmove(DEST, SRC, SIZE)
  116. #define strcpy(DEST, SRC) _duma_strcpy(DEST, SRC, __FILE__, __LINE__)
  117. #define strncpy(DEST, SRC, SIZE) _duma_strncpy(DEST, SRC, SIZE, __FILE__, __LINE__)
  118. #define strcat(DEST, SRC) _duma_strcat(DEST, SRC, __FILE__, __LINE__)
  119. #define strncat(DEST, SRC, SIZE) _duma_strncat(DEST, SRC, SIZE, __FILE__, __LINE__)
  120. #ifndef DUMA_ASSERT
  121. #define DUMA_ASSERT(EXPR) ( (EXPR) || ( _duma_assert(#EXPR, __FILE__, __LINE__), 0 ) )
  122. #endif
  123. /*
  124. * protection of functions return address
  125. */
  126. #ifdef __GNUC__
  127. #define DUMA_FN_PROT_START const void * DUMA_RET_ADDR = __builtin_return_address(0); {
  128. #define DUMA_FN_PROT_END } DUMA_ASSERT( __builtin_return_address(0) == DUMA_RET_ADDR );
  129. #define DUMA_FN_PROT_RET(EXPR) do { DUMA_ASSERT( __builtin_return_address(0) == DUMA_RET_ADDR ); return( EXPR ); } while (0)
  130. #define DUMA_FN_PROT_RET_VOID() do { DUMA_ASSERT( __builtin_return_address(0) == DUMA_RET_ADDR ); return; } while (0)
  131. #else
  132. #define DUMA_FN_PROT_START int aiDUMA_PROT[ 4 ] = { 'E', 'F', 'P', 'R' }; {
  133. #define DUMA_FN_PROT_END } DUMA_ASSERT( 'E'==aiDUMA_PROT[0] && 'F'==aiDUMA_PROT[1] && 'P'==aiDUMA_PROT[2] && 'R'==aiDUMA_PROT[3] );
  134. #define DUMA_FN_PROT_RET(EXPR) do { DUMA_ASSERT( 'E'==aiDUMA_PROT[0] && 'F'==aiDUMA_PROT[1] && 'P'==aiDUMA_PROT[2] && 'R'==aiDUMA_PROT[3] ); return( EXPR ); } while (0)
  135. #define DUMA_FN_PROT_RET_VOID() do { DUMA_ASSERT( 'E'==aiDUMA_PROT[0] && 'F'==aiDUMA_PROT[1] && 'P'==aiDUMA_PROT[2] && 'R'==aiDUMA_PROT[3] ); return; } while (0)
  136. #endif
  137. /* declaration of an already defined array to enable checking at every reference
  138. * when using CA_REF()
  139. */
  140. #define CA_DECLARE(NAME,SIZE) \
  141. const unsigned long NAME ## _checkedsize = (SIZE); \
  142. unsigned long NAME ## _checkedidx
  143. /* definition of a checked array adds definitions for its size and an extra temporary.
  144. * every array gets its own temporary to avoid problems with threading
  145. * a global temporary would have.
  146. */
  147. #define CA_DEFINE(TYPE,NAME,SIZE) TYPE NAME[SIZE]; CA_DECLARE(NAME,SIZE)
  148. /* every access to a checked array is preceded an assert() on the index;
  149. * the index parameter is stored to a temporary to avoid double execution of index,
  150. * when index contains f.e. a "++".
  151. */
  152. #define CA_REF(NAME,INDEX) \
  153. NAME[ DUMA_ASSERT( (NAME ## _checkedidx = (INDEX)) < NAME ## _checkedsize ), NAME ## _checkedidx ]
  154. #endif /* end ifdef __KDUMA_H__ */
  155. // end