/factory/mmutil.c

https://github.com/vbraun/Sources · C · 235 lines · 199 code · 24 blank · 12 comment · 50 complexity · edf839f4c5a44e1d0a1f111b88f4f8b3 MD5 · raw file

  1. /* emacs edit mode for this file is -*- C -*- */
  2. /* $Id$ */
  3. #define _POSIX_SOURCE 1
  4. #include <config.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include "memman.h"
  9. #include "mmprivate.h"
  10. #ifdef __alpha__
  11. const size_t mm_mcbSizes [MAXLIST] = { 8, 16, 24, 32,
  12. 40, 48, 56, 64,
  13. 80, 96, 112, 128,
  14. 160, 192, 224, 256,
  15. 512, 1024 };
  16. #else
  17. /*const*/ size_t mm_mcbSizes [MAXLIST] = { 8, 12, 16, 20, 24, 28, 32,
  18. 40, 48, 56, 64,
  19. 80, 96, 112, 128,
  20. 160, 192, 224, 256,
  21. 512, 1024 };
  22. #endif
  23. int
  24. mmGetIndex( size_t size )
  25. {
  26. int i;
  27. if ( size > mm_mcbSizes[MAXLIST-1] )
  28. return -1;
  29. else {
  30. for ( i = 0; i < MAXLIST; i++ )
  31. if ( size <= mm_mcbSizes[i] )
  32. return i;
  33. }
  34. return MAXLIST-1;
  35. }
  36. mcb
  37. mmNewMCBList( int i )
  38. {
  39. char* blk = (char*)mmGetBlock();
  40. char* top;
  41. mcb dummy, p;
  42. size_t offset = mm_mcbSizes[i];
  43. dummy = (mcb)blk;
  44. p = dummy;
  45. top = blk + MAXDATA - offset;
  46. while ( (char*)p < top ) {
  47. *p = (void*)((char*)p + offset);
  48. p = (mcb)*p;
  49. }
  50. *p = NULL;
  51. return dummy;
  52. }
  53. #ifdef MDEBUG
  54. void
  55. mmMoveDBMCB ( pDBMCB from, pDBMCB to, DBMCB * what )
  56. {
  57. what->prev->next = what->next;
  58. if ( what->next != NULL )
  59. what->next->prev = what->prev;
  60. what->prev = to;
  61. what->next = to->next;
  62. if (to->next!=NULL)
  63. to->next->prev=what;
  64. to->next = what;
  65. }
  66. void
  67. mmMoveDBMCBInto ( pDBMCB to, pDBMCB what )
  68. {
  69. if (to->next !=NULL) {
  70. what->next = to->next;
  71. what->next->prev = what;
  72. }
  73. to->next = what;
  74. what->prev = to;
  75. }
  76. void
  77. mmTakeOutDBMCB ( pDBMCB from, pDBMCB what )
  78. {
  79. what->prev->next = what->next;
  80. if ( what->next != NULL )
  81. what->next->prev = what->prev;
  82. }
  83. int
  84. mmPrintDBMCB ( DBMCB * what, char* msg , int given_size)
  85. {
  86. DBMCB * before=NULL;
  87. (void)fprintf( stderr, "warning: %s\n", msg );
  88. (void)fprintf( stderr, "block %x allocated in: %s, line no %d\n",
  89. (int)what, what->fname, what->lineno );
  90. if (strcmp(msg,"size")==0)
  91. (void)fprintf( stderr, "size is: %d, but free call said %d \n",
  92. (int)what->size, given_size );
  93. else
  94. (void)fprintf( stderr, "size is: %d\n", (int)what->size );
  95. #ifdef MM_BEFORE
  96. {
  97. char * queue="used";
  98. DBMCB *x=&mm_theDBused;
  99. while (x!=NULL) {
  100. if (((unsigned)x<(unsigned)what) && ((unsigned)before<(unsigned)x))
  101. before=x;
  102. x=x->next;
  103. }
  104. x=&mm_theDBfree;
  105. while (x!=NULL) {
  106. if (((unsigned)x<(unsigned)what) && ((unsigned)before<(unsigned)x)) {
  107. before=x;
  108. queue="free";
  109. }
  110. x=x->next;
  111. }
  112. x=before;
  113. fprintf( stderr, "previous (%s queue) at %x to %x, alloc: %s, l: %d, size:%d\n",
  114. queue,x,(unsigned)x+RealSizeFromSize(x->size),x->fname,x->lineno,x->size);
  115. }
  116. #endif
  117. return 0;
  118. }
  119. void
  120. mmTestList ( )
  121. {
  122. DBMCB * what=mm_theDBused.next;
  123. fprintf(stderr,"list of used blocks:\n");
  124. while (what!=NULL) {
  125. (void)fprintf( stderr, "%d bytes at %p in: %s, line no %d\n",
  126. (int)what->size, what, what->fname, what->lineno);
  127. what=what->next;
  128. }
  129. /*
  130. //what=mm_theDBfree.next;
  131. //fprintf(stderr,"list of free blocks:\n");
  132. //while (what!=NULL)
  133. //{
  134. // (void)fprintf( stderr, "%d bytes at %p in: %s, line no %d\n",
  135. // what->size, what, what->fname, what->lineno );
  136. // what=what->next;
  137. // }
  138. */
  139. }
  140. void
  141. mmPrintFL( const char* fname, const int lineno )
  142. {
  143. (void)fprintf( stderr, "occured in %s line %d\n", fname, lineno );
  144. }
  145. int
  146. mmCheckDBMCB ( DBMCB * what, int flags, int size )
  147. {
  148. char * patptr;
  149. int i;
  150. if ( ((int)what % 4 ) != 0 ) {
  151. (void)fprintf( stderr, "warning: odd address" );
  152. return 0;
  153. }
  154. if ( (void*)what > mm_maxAddr ) {
  155. (void)fprintf( stderr, "warning: address too high" );
  156. return 0;
  157. }
  158. if ( (void*)what < mm_minAddr ) {
  159. (void)fprintf( stderr, "warning: address too low" );
  160. return 0;
  161. }
  162. if ( ( what->flags & flags ) != flags )
  163. return mmPrintDBMCB( what, "flags", 0 );
  164. if ( what->size != size )
  165. return mmPrintDBMCB( what, "size", size );
  166. if ( what->pattern != PATTERN )
  167. return mmPrintDBMCB( what, "front pattern", 0 );
  168. patptr = ((char*)&what->data) + size;
  169. for ( i = 4; i > 0; i-- )
  170. if ( *(patptr++) != PATTERN2 ) return mmPrintDBMCB( what, "back pattern", 0 );
  171. if ( ( what->next != NULL ) && ( what->next->prev != what ) )
  172. return mmPrintDBMCB( what, "chain:n", 0 );
  173. if ( ( what->prev != NULL ) && ( what->prev->next != what ) ) {
  174. mmPrintDBMCB( what->prev, "prev", 0 );
  175. return mmPrintDBMCB( what, "chain:p", 0 );
  176. }
  177. return 1;
  178. }
  179. int
  180. mmDBTestBlock( const void* adr, const size_t size, const char * fname, const int lineno )
  181. {
  182. DBMCB * what;
  183. if ( ( adr == NULL ) || ( size == 0 ) )
  184. return 1; /*TRUE*/
  185. what = (DBMCB*)((char*)(adr) - DebugOffsetFront);
  186. if ( mm_status == MM_TMP ) {
  187. if ( ! mmCheckDBMCB( what, MM_TMPFLAG | MM_USEDFLAG, size ) ) {
  188. mmPrintFL( fname, lineno );
  189. return 0; /*FALSE*/
  190. }
  191. }
  192. else {
  193. if ( ! mmCheckDBMCB( what, MM_NORMFLAG | MM_USEDFLAG, size ) ) {
  194. mmPrintFL( fname, lineno );
  195. return 0; /*FALSE*/
  196. }
  197. }
  198. return 1; /*TRUE*/
  199. }
  200. int
  201. mmDBTest( const void* adr, const char * fname, const int lineno )
  202. {
  203. if (adr!=NULL) {
  204. size_t l;
  205. adr = (size_t*)adr-1;
  206. l= (adr<=mm_maxAddr) ? (*(size_t*)((int)adr&(~3))) :0;
  207. return mmDBTestBlock( adr,l, fname, lineno );
  208. }
  209. return 1; /*TRUE*/
  210. }
  211. #endif /* MDEBUG */