mem0dbg.c
上传用户:tsgydb
上传日期:2007-04-14
资源大小:10674k
文件大小:19k
源码类别:

MySQL数据库

开发平台:

Visual C++

  1. /************************************************************************
  2. The memory management: the debug code. This is not a compilation module,
  3. but is included in mem0mem.* !
  4. (c) 1994, 1995 Innobase Oy
  5. Created 6/9/1994 Heikki Tuuri
  6. *************************************************************************/
  7. mutex_t mem_hash_mutex;  /* The mutex which protects in the
  8. debug version the hash table containing
  9. the list of live memory heaps, and
  10. also the global variables below. */
  11. /* The following variables contain information about the
  12. extent of memory allocations. Only used in the debug version.
  13. Protected by mem_hash_mutex above. */
  14. ulint mem_n_created_heaps  = 0;
  15. ulint mem_n_allocations    = 0;
  16. ulint mem_total_allocated_memory = 0;
  17. ulint mem_current_allocated_memory = 0;
  18. ulint mem_max_allocated_memory = 0;
  19. ulint mem_last_print_info = 0;
  20. /* Size of the hash table for memory management tracking */
  21. #define MEM_HASH_SIZE 997
  22. /* The node of the list containing currently allocated memory heaps */
  23. typedef struct mem_hash_node_struct mem_hash_node_t;
  24. struct mem_hash_node_struct {
  25. UT_LIST_NODE_T(mem_hash_node_t)
  26. list; /* hash list node */
  27. mem_heap_t* heap; /* memory heap */
  28. char* file_name;/* file where heap was created*/
  29. ulint line; /* file line of creation */
  30. ulint nth_heap;/* this is the nth heap created */
  31. UT_LIST_NODE_T(mem_hash_node_t)
  32. all_list;/* list of all created heaps */
  33. };
  34. typedef UT_LIST_BASE_NODE_T(mem_hash_node_t) mem_hash_cell_t;
  35. /* The hash table of allocated heaps */
  36. mem_hash_cell_t mem_hash_table[MEM_HASH_SIZE];
  37. /* The base node of the list of all allocated heaps */
  38. mem_hash_cell_t mem_all_list_base;
  39. ibool mem_hash_initialized = FALSE;
  40. UNIV_INLINE
  41. mem_hash_cell_t*
  42. mem_hash_get_nth_cell(ulint i);
  43. /* Accessor function for the hash table. Returns a pointer to the
  44. table cell. */
  45. UNIV_INLINE
  46. mem_hash_cell_t*
  47. mem_hash_get_nth_cell(ulint i)
  48. {
  49. ut_a(i < MEM_HASH_SIZE);
  50. return(&(mem_hash_table[i]));
  51. }
  52. /* Accessor functions for a memory field in the debug version */
  53. void
  54. mem_field_header_set_len(byte* field, ulint len)
  55. {
  56. ut_ad(len >= 0);
  57. mach_write(field - 2 * sizeof(ulint), len);
  58. }
  59. ulint
  60. mem_field_header_get_len(byte* field)
  61. {
  62. return(mach_read(field - 2 * sizeof(ulint)));
  63. }
  64. void
  65. mem_field_header_set_check(byte* field, ulint check)
  66. {
  67. mach_write(field - sizeof(ulint), check);
  68. }
  69. ulint
  70. mem_field_header_get_check(byte* field)
  71. {
  72. return(mach_read(field - sizeof(ulint)));
  73. }
  74. void
  75. mem_field_trailer_set_check(byte* field, ulint check)
  76. {
  77. mach_write(field + mem_field_header_get_len(field), check);
  78. }
  79. ulint
  80. mem_field_trailer_get_check(byte* field)
  81. {
  82. return(mach_read(field +
  83. mem_field_header_get_len(field)));
  84. }
  85. /**********************************************************************
  86. Initializes the memory system. */
  87. void
  88. mem_init(
  89. /*=====*/
  90. ulint size) /* in: common pool size in bytes */
  91. {
  92. #ifdef UNIV_MEM_DEBUG
  93. ulint i;
  94. /* Initialize the hash table */
  95. ut_a(FALSE == mem_hash_initialized);
  96. mutex_create(&mem_hash_mutex);
  97. mutex_set_level(&mem_hash_mutex, SYNC_MEM_HASH);
  98. for (i = 0; i < MEM_HASH_SIZE; i++) {
  99. UT_LIST_INIT(*mem_hash_get_nth_cell(i));
  100. }
  101. UT_LIST_INIT(mem_all_list_base);
  102. mem_hash_initialized = TRUE;
  103. #endif
  104. mem_comm_pool = mem_pool_create(size);
  105. }
  106. /**********************************************************************
  107. Initializes an allocated memory field in the debug version. */
  108. void
  109. mem_field_init(
  110. /*===========*/
  111. byte* buf, /* in: memory field */
  112. ulint n) /* in: how many bytes the user requested */
  113. {
  114. ulint rnd;
  115. byte* usr_buf;
  116. usr_buf = buf + MEM_FIELD_HEADER_SIZE;
  117. /* In the debug version write the length field and the 
  118. check fields to the start and the end of the allocated storage.
  119. The field header consists of a length field and
  120. a random number field, in this order. The field trailer contains
  121. the same random number as a check field. */
  122. mem_field_header_set_len(usr_buf, n);
  123. rnd = ut_rnd_gen_ulint();
  124. mem_field_header_set_check(usr_buf, rnd);
  125. mem_field_trailer_set_check(usr_buf, rnd);
  126. /* Update the memory allocation information */
  127. mutex_enter(&mem_hash_mutex);
  128. mem_total_allocated_memory += n;
  129. mem_current_allocated_memory += n;
  130. mem_n_allocations++;
  131. if (mem_current_allocated_memory > mem_max_allocated_memory) {
  132. mem_max_allocated_memory = mem_current_allocated_memory;
  133. }
  134. mutex_exit(&mem_hash_mutex);
  135. /* In the debug version set the buffer to a random
  136. combination of 0xBA and 0xBE */
  137. mem_init_buf(usr_buf, n);
  138. }
  139. /**********************************************************************
  140. Erases an allocated memory field in the debug version. */
  141. void
  142. mem_field_erase(
  143. /*============*/
  144. byte* buf, /* in: memory field */
  145. ulint n) /* in: how many bytes the user requested */
  146. {
  147. byte* usr_buf;
  148. usr_buf = buf + MEM_FIELD_HEADER_SIZE;
  149. mutex_enter(&mem_hash_mutex);
  150. mem_current_allocated_memory    -= n;
  151. mutex_exit(&mem_hash_mutex);
  152. /* Check that the field lengths agree */
  153. ut_ad(n == (ulint)mem_field_header_get_len(usr_buf));
  154. /* In the debug version, set the freed space to a random
  155. combination of 0xDE and 0xAD */
  156. mem_erase_buf(buf, MEM_SPACE_NEEDED(n));
  157. }
  158. /*******************************************************************
  159. Initializes a buffer to a random combination of hex BA and BE.
  160. Used to initialize allocated memory. */
  161. void
  162. mem_init_buf(
  163. /*=========*/
  164. byte*   buf,    /* in: pointer to buffer */
  165. ulint    n)     /* in: length of buffer */
  166. {
  167. byte*   ptr;
  168. for (ptr = buf; ptr < buf + n; ptr++) {
  169. if (ut_rnd_gen_ibool()) {
  170. *ptr = 0xBA;
  171. } else {
  172. *ptr = 0xBE;
  173. }
  174. }
  175. }
  176. /*******************************************************************
  177. Initializes a buffer to a random combination of hex DE and AD.
  178. Used to erase freed memory.*/
  179. void
  180. mem_erase_buf(
  181. /*==========*/
  182. byte*   buf,    /* in: pointer to buffer */
  183. ulint    n)      /* in: length of buffer */
  184. {
  185. byte*   ptr;
  186. for (ptr = buf; ptr < buf + n; ptr++) {
  187. if (ut_rnd_gen_ibool()) {
  188. *ptr = 0xDE;
  189. } else {
  190. *ptr = 0xAD;
  191. }
  192. }
  193. }
  194. /*******************************************************************
  195. Inserts a created memory heap to the hash table of current allocated
  196. memory heaps. */
  197. void
  198. mem_hash_insert(
  199. /*============*/
  200. mem_heap_t* heap,    /* in: the created heap */
  201. char* file_name, /* in: file name of creation */
  202. ulint line)    /* in: line where created */
  203. {
  204. mem_hash_node_t* new_node;
  205. ulint cell_no ;
  206. ut_ad(mem_heap_check(heap));
  207. mutex_enter(&mem_hash_mutex);
  208. cell_no = ut_hash_ulint((ulint)heap, MEM_HASH_SIZE);
  209. /* Allocate a new node to the list */
  210. new_node = ut_malloc(sizeof(mem_hash_node_t));
  211. new_node->heap = heap;
  212. new_node->file_name = file_name;
  213. new_node->line = line;
  214. new_node->nth_heap = mem_n_created_heaps;
  215. /* Insert into lists */
  216. UT_LIST_ADD_FIRST(list, *mem_hash_get_nth_cell(cell_no), new_node);
  217. UT_LIST_ADD_LAST(all_list, mem_all_list_base, new_node);
  218. mem_n_created_heaps++;
  219. mutex_exit(&mem_hash_mutex);
  220. }
  221. /*******************************************************************
  222. Removes a memory heap (which is going to be freed by the caller)
  223. from the list of live memory heaps. Returns the size of the heap
  224. in terms of how much memory in bytes was allocated for the user of
  225. the heap (not the total space occupied by the heap).
  226. Also validates the heap.
  227. NOTE: This function does not free the storage occupied by the
  228. heap itself, only the node in the list of heaps. */
  229. void
  230. mem_hash_remove(
  231. /*============*/
  232. mem_heap_t* heap,    /* in: the heap to be freed */
  233. char* file_name, /* in: file name of freeing */
  234. ulint line)    /* in: line where freed */
  235. {
  236. mem_hash_node_t* node;
  237. ulint cell_no;
  238. ibool error;
  239. ulint size;
  240. ut_ad(mem_heap_check(heap));
  241. mutex_enter(&mem_hash_mutex);
  242. cell_no = ut_hash_ulint((ulint)heap, MEM_HASH_SIZE);
  243. /* Look for the heap in the hash table list */
  244. node = UT_LIST_GET_FIRST(*mem_hash_get_nth_cell(cell_no));
  245. while (node != NULL) {
  246. if (node->heap == heap) {
  247. break;
  248. }
  249. node = UT_LIST_GET_NEXT(list, node);
  250. }
  251. if (node == NULL) {
  252. printf(
  253.          "Memory heap or buffer freed in %s line %lu did not exist.n",
  254. file_name, line);
  255. ut_error;
  256. }
  257. /* Remove from lists */
  258. UT_LIST_REMOVE(list, *mem_hash_get_nth_cell(cell_no), node);
  259. UT_LIST_REMOVE(all_list, mem_all_list_base, node);
  260. /* Validate the heap which will be freed */
  261. mem_heap_validate_or_print(node->heap, NULL, FALSE, &error, &size,
  262. NULL, NULL);
  263. if (error) {
  264.    printf("Inconsistency in memory heap or buffer n:o %lu createdn",
  265. node->nth_heap);
  266.    printf("in %s line %lu and tried to free in %s line %lu.n",
  267.    node->file_name, node->line, file_name, line);
  268.    ut_error;
  269. }
  270. /* Free the memory occupied by the node struct */
  271. ut_free(node);
  272. mem_current_allocated_memory -= size;
  273. mutex_exit(&mem_hash_mutex);
  274. }
  275. /*******************************************************************
  276. Checks a memory heap for consistency and prints the contents if requested.
  277. Outputs the sum of sizes of buffers given to the user (only in
  278. the debug version), the physical size of the heap and the number of
  279. blocks in the heap. In case of error returns 0 as sizes and number
  280. of blocks. */
  281. void
  282. mem_heap_validate_or_print(
  283. /*=======================*/
  284. mem_heap_t*  heap,  /* in: memory heap */
  285. byte* top, /* in: calculate and validate only until
  286. this top pointer in the heap is reached,
  287. if this pointer is NULL, ignored */
  288. ibool print, /* in: if TRUE, prints the contents
  289. of the heap; works only in
  290. the debug version */
  291. ibool* error, /* out: TRUE if error */
  292. ulint* us_size,/* out: allocated memory 
  293. (for the user) in the heap,
  294. if a NULL pointer is passed as this
  295. argument, it is ignored; in the
  296. non-debug version this is always -1 */
  297. ulint* ph_size,/* out: physical size of the heap,
  298. if a NULL pointer is passed as this
  299. argument, it is ignored */
  300. ulint* n_blocks) /* out: number of blocks in the heap,
  301. if a NULL pointer is passed as this
  302. argument, it is ignored */
  303. {
  304. mem_block_t* block;
  305. ulint total_len  = 0;
  306. ulint block_count = 0;
  307. ulint phys_len = 0;
  308. #ifdef UNIV_MEM_DEBUG
  309. ulint len;
  310. byte* field;
  311. byte* user_field;
  312. ulint check_field;
  313. #endif
  314. /* Pessimistically, we set the parameters to error values */
  315.   if (us_size != NULL) {
  316. *us_size = 0;
  317. }
  318.   if (ph_size != NULL) {
  319. *ph_size = 0;
  320. }
  321. if (n_blocks != NULL) {
  322. *n_blocks = 0;
  323. }
  324. *error = TRUE;
  325. block = heap;
  326. if (block->magic_n != MEM_BLOCK_MAGIC_N) {
  327. return;
  328. }
  329. if (print) {
  330. printf("Memory heap:");
  331. }
  332. while (block != NULL) {
  333. phys_len += mem_block_get_len(block);
  334. if ((block->type == MEM_HEAP_BUFFER)
  335.     && (mem_block_get_len(block) > UNIV_PAGE_SIZE)) {
  336.      /* error */
  337.      return;
  338. }
  339. #ifdef UNIV_MEM_DEBUG
  340. /* We can trace the fields of the block only in the debug
  341. version */
  342. if (print) {
  343. printf(" Block %ld:", block_count);
  344. }
  345. field = (byte*)block + mem_block_get_start(block);
  346. if (top && (field == top)) {
  347. goto completed;
  348. }
  349. while (field < (byte*)block + mem_block_get_free(block)) {
  350. /* Calculate the pointer to the storage
  351. which was given to the user */
  352. user_field = field + MEM_FIELD_HEADER_SIZE;
  353. len = mem_field_header_get_len(user_field); 
  354.    
  355. if (print) {
  356. ut_print_buf(user_field, len);
  357. }
  358. total_len += len;
  359. check_field = mem_field_header_get_check(user_field);
  360. if (check_field != 
  361.     mem_field_trailer_get_check(user_field)) {
  362. /* error */
  363.       return;
  364. }
  365. /* Move to next field */
  366. field = field + MEM_SPACE_NEEDED(len);
  367. if (top && (field == top)) {
  368. goto completed;
  369. }
  370. }
  371. /* At the end check that we have arrived to the first free
  372. position */
  373. if (field != (byte*)block + mem_block_get_free(block)) {
  374. /* error */
  375. return;
  376. }
  377. #endif
  378. block = UT_LIST_GET_NEXT(list, block);
  379. block_count++;
  380. }
  381. #ifdef UNIV_MEM_DEBUG
  382. completed:
  383. #endif
  384.   if (us_size != NULL) {
  385. *us_size = total_len;
  386. }
  387.   if (ph_size != NULL) {
  388. *ph_size = phys_len;
  389. }
  390. if (n_blocks != NULL) {
  391. *n_blocks = block_count;
  392. }
  393. *error = FALSE;
  394. }
  395. /******************************************************************
  396. Prints the contents of a memory heap. */
  397. void
  398. mem_heap_print(
  399. /*===========*/
  400. mem_heap_t* heap) /* in: memory heap */
  401. {
  402. ibool error;
  403. ulint us_size;
  404. ulint phys_size;
  405. ulint n_blocks;
  406. ut_ad(mem_heap_check(heap));
  407. mem_heap_validate_or_print(heap, NULL, TRUE, &error, 
  408. &us_size, &phys_size, &n_blocks);
  409. printf(
  410.   "nheap type: %lu; size: user size %lu; physical size %lu; blocks %lu.n",
  411. heap->type, us_size, phys_size, n_blocks);
  412. ut_a(!error);
  413. }
  414. /******************************************************************
  415. Checks that an object is a memory heap (or a block of it). */
  416. ibool
  417. mem_heap_check(
  418. /*===========*/
  419. /* out: TRUE if ok */
  420. mem_heap_t* heap) /* in: memory heap */
  421. {
  422. ut_a(heap->magic_n == MEM_BLOCK_MAGIC_N);
  423. return(TRUE);
  424. }
  425. /******************************************************************
  426. Validates the contents of a memory heap. */
  427. ibool
  428. mem_heap_validate(
  429. /*==============*/
  430. /* out: TRUE if ok */
  431. mem_heap_t* heap) /* in: memory heap */
  432. {
  433. ibool error;
  434. ulint us_size;
  435. ulint phys_size;
  436. ulint n_blocks;
  437. ut_ad(mem_heap_check(heap));
  438. mem_heap_validate_or_print(heap, NULL, FALSE, &error, &us_size,
  439. &phys_size, &n_blocks);
  440. ut_a(!error);
  441. return(TRUE);
  442. }
  443. /*********************************************************************
  444. Prints information of dynamic memory usage and currently allocated
  445. memory heaps or buffers. Can only be used in the debug version. */
  446. static
  447. void
  448. mem_print_info_low(
  449. /*===============*/
  450. ibool print_all) /* in: if TRUE, all heaps are printed,
  451. else only the heaps allocated after the
  452. previous call of this function */
  453. {
  454. #ifdef UNIV_MEM_DEBUG
  455. mem_hash_node_t* node;
  456. ulint n_heaps  = 0;
  457. ulint allocated_mem;
  458. ulint ph_size;
  459. ulint total_allocated_mem  = 0;
  460. ibool error;
  461. ulint n_blocks;
  462. #endif
  463. FILE* outfile;
  464. /* outfile = fopen("ibdebug", "a"); */
  465. outfile = stdout;
  466. fprintf(outfile, "n");
  467. fprintf(outfile,
  468. "________________________________________________________n");
  469. fprintf(outfile, "MEMORY ALLOCATION INFORMATIONnn");
  470. #ifndef UNIV_MEM_DEBUG
  471. mem_pool_print_info(outfile, mem_comm_pool);
  472. fprintf(outfile,
  473. "Sorry, non-debug version cannot give more memory infon");
  474. /* fclose(outfile); */
  475. return;
  476. #else
  477. mutex_enter(&mem_hash_mutex);
  478. fprintf(outfile, "LIST OF CREATED HEAPS AND ALLOCATED BUFFERS: nn");
  479. if (!print_all) {
  480. fprintf(outfile, "AFTER THE LAST PRINT INFOn");
  481. }
  482. node = UT_LIST_GET_FIRST(mem_all_list_base);
  483. while (node != NULL) {
  484. n_heaps++;
  485. if (!print_all && node->nth_heap < mem_last_print_info) {
  486. goto next_heap;
  487. }
  488. mem_heap_validate_or_print(node->heap, NULL, 
  489. FALSE, &error, &allocated_mem, 
  490. &ph_size, &n_blocks);
  491. total_allocated_mem += allocated_mem;
  492. fprintf(outfile,
  493.  "%lu: file %s line %lu of size %lu phys.size %lu with %lu blocks, type %lun",
  494. node->nth_heap, node->file_name, node->line, 
  495. allocated_mem, ph_size, n_blocks,
  496. (node->heap)->type);
  497. next_heap:
  498. node = UT_LIST_GET_NEXT(all_list, node);
  499. }
  500. fprintf(outfile, "n");
  501. fprintf(outfile, "Current allocated memory    : %lun", 
  502. mem_current_allocated_memory);
  503. fprintf(outfile, "Current allocated heaps and buffers : %lun", 
  504. n_heaps);
  505. fprintf(outfile, "Cumulative allocated memory    : %lun", 
  506. mem_total_allocated_memory);
  507. fprintf(outfile, "Maximum allocated memory    : %lun",
  508. mem_max_allocated_memory);
  509. fprintf(outfile, "Cumulative created heaps and buffers : %lun", 
  510. mem_n_created_heaps);
  511. fprintf(outfile, "Cumulative number of allocations : %lun", 
  512. mem_n_allocations);
  513. mem_last_print_info = mem_n_created_heaps;
  514. mutex_exit(&mem_hash_mutex);
  515. mem_pool_print_info(outfile, mem_comm_pool);
  516. mem_validate();
  517. /*  fclose(outfile); */
  518. #endif
  519. }
  520. /*********************************************************************
  521. Prints information of dynamic memory usage and currently allocated memory
  522. heaps or buffers. Can only be used in the debug version. */
  523. void
  524. mem_print_info(void)
  525. /*================*/
  526. {
  527. mem_print_info_low(TRUE);
  528. }
  529. /*********************************************************************
  530. Prints information of dynamic memory usage and currently allocated memory
  531. heaps or buffers since the last ..._print_info or..._print_new_info. */
  532. void
  533. mem_print_new_info(void)
  534. /*====================*/
  535. {
  536. mem_print_info_low(FALSE);
  537. }
  538. /*********************************************************************
  539. TRUE if no memory is currently allocated. */
  540. ibool
  541. mem_all_freed(void)
  542. /*===============*/
  543. /* out: TRUE if no heaps exist */
  544. {
  545. #ifdef UNIV_MEM_DEBUG
  546. mem_hash_node_t* node;
  547. ulint heap_count = 0;
  548. ulint i;
  549. mem_validate();
  550. mutex_enter(&mem_hash_mutex);
  551. for (i = 0; i < MEM_HASH_SIZE; i++) {
  552. node = UT_LIST_GET_FIRST(*mem_hash_get_nth_cell(i));
  553. while (node != NULL) {
  554. heap_count++;
  555. node = UT_LIST_GET_NEXT(list, node);
  556. }
  557. }
  558. mutex_exit(&mem_hash_mutex);
  559. if (heap_count == 0) {
  560. ut_a(mem_pool_get_reserved(mem_comm_pool) == 0);
  561. return(TRUE);
  562. } else {
  563. return(FALSE);
  564. }
  565. #else
  566. printf(
  567. "Sorry, non-debug version cannot check if all memory is freed.n");
  568. return(FALSE);
  569. #endif
  570. }
  571. /*********************************************************************
  572. Validates the dynamic memory allocation system. */
  573. ibool
  574. mem_validate_no_assert(void)
  575. /*========================*/
  576. /* out: TRUE if error */
  577. {
  578. #ifdef UNIV_MEM_DEBUG
  579. mem_hash_node_t* node;
  580. ulint n_heaps  = 0;
  581. ulint allocated_mem;
  582. ulint ph_size;
  583. ulint total_allocated_mem  = 0;
  584. ibool error = FALSE;
  585. ulint n_blocks;
  586. ulint i;
  587. mem_pool_validate(mem_comm_pool);
  588. mutex_enter(&mem_hash_mutex);
  589. for (i = 0; i < MEM_HASH_SIZE; i++) {
  590. node = UT_LIST_GET_FIRST(*mem_hash_get_nth_cell(i));
  591. while (node != NULL) {
  592. n_heaps++;
  593. mem_heap_validate_or_print(node->heap, NULL,
  594. FALSE, &error, &allocated_mem, 
  595. &ph_size, &n_blocks);
  596. if (error) {
  597.      printf("nERROR!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!nn");
  598.    printf("Inconsistency in memory heap or buffer createdn");
  599.    printf("in %s line %lu.n", node->file_name, node->line);
  600. mutex_exit(&mem_hash_mutex);
  601. return(TRUE);
  602. }
  603. total_allocated_mem += allocated_mem;
  604. node = UT_LIST_GET_NEXT(list, node);
  605. }
  606. }
  607. if ((n_heaps == 0) && (mem_current_allocated_memory != 0)) {
  608. error = TRUE;
  609. }
  610. if (mem_total_allocated_memory < mem_current_allocated_memory) {
  611. error = TRUE;
  612. }
  613. if (mem_max_allocated_memory > mem_total_allocated_memory) {
  614. error = TRUE;
  615. }
  616. if (mem_n_created_heaps < n_heaps) {
  617. error = TRUE;
  618. }
  619. mutex_exit(&mem_hash_mutex);
  620. return(error);
  621. #else
  622. printf("Sorry, non-debug version cannot validate dynamic memoryn");
  623. return(FALSE);
  624. #endif
  625. }
  626. /****************************************************************
  627. Validates the dynamic memory */
  628. ibool
  629. mem_validate(void)
  630. /*==============*/
  631. /* out: TRUE if ok */
  632. {
  633. ut_a(!mem_validate_no_assert());
  634. return(TRUE);
  635. }