You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

642 lines
16 KiB

20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
  1. /************************************************************************
  2. The memory management
  3. (c) 1994, 1995 Innobase Oy
  4. Created 6/8/1994 Heikki Tuuri
  5. *************************************************************************/
  6. #include "mem0dbg.ic"
  7. #include "mem0pool.h"
  8. /*******************************************************************
  9. Creates a memory heap block where data can be allocated. */
  10. mem_block_t*
  11. mem_heap_create_block(
  12. /*==================*/
  13. /* out, own: memory heap block, NULL if
  14. did not succeed (only possible for
  15. MEM_HEAP_BTR_SEARCH type heaps) */
  16. mem_heap_t* heap, /* in: memory heap or NULL if first block
  17. should be created */
  18. ulint n, /* in: number of bytes needed for user data, or
  19. if init_block is not NULL, its size in bytes */
  20. void* init_block, /* in: init block in fast create,
  21. type must be MEM_HEAP_DYNAMIC */
  22. ulint type, /* in: type of heap: MEM_HEAP_DYNAMIC or
  23. MEM_HEAP_BUFFER */
  24. const char* file_name,/* in: file name where created */
  25. ulint line); /* in: line where created */
  26. /**********************************************************************
  27. Frees a block from a memory heap. */
  28. void
  29. mem_heap_block_free(
  30. /*================*/
  31. mem_heap_t* heap, /* in: heap */
  32. mem_block_t* block); /* in: block to free */
  33. /**********************************************************************
  34. Frees the free_block field from a memory heap. */
  35. void
  36. mem_heap_free_block_free(
  37. /*=====================*/
  38. mem_heap_t* heap); /* in: heap */
  39. /*******************************************************************
  40. Adds a new block to a memory heap. */
  41. mem_block_t*
  42. mem_heap_add_block(
  43. /*===============*/
  44. /* out: created block, NULL if did not
  45. succeed (only possible for
  46. MEM_HEAP_BTR_SEARCH type heaps)*/
  47. mem_heap_t* heap, /* in: memory heap */
  48. ulint n); /* in: number of bytes user needs */
  49. UNIV_INLINE
  50. void
  51. mem_block_set_len(mem_block_t* block, ulint len)
  52. {
  53. ut_ad(len > 0);
  54. block->len = len;
  55. }
  56. UNIV_INLINE
  57. ulint
  58. mem_block_get_len(mem_block_t* block)
  59. {
  60. return(block->len);
  61. }
  62. UNIV_INLINE
  63. void
  64. mem_block_set_type(mem_block_t* block, ulint type)
  65. {
  66. ut_ad((type == MEM_HEAP_DYNAMIC) || (type == MEM_HEAP_BUFFER)
  67. || (type == MEM_HEAP_BUFFER + MEM_HEAP_BTR_SEARCH));
  68. block->type = type;
  69. }
  70. UNIV_INLINE
  71. ulint
  72. mem_block_get_type(mem_block_t* block)
  73. {
  74. return(block->type);
  75. }
  76. UNIV_INLINE
  77. void
  78. mem_block_set_free(mem_block_t* block, ulint free)
  79. {
  80. ut_ad(free > 0);
  81. ut_ad(free <= mem_block_get_len(block));
  82. block->free = free;
  83. }
  84. UNIV_INLINE
  85. ulint
  86. mem_block_get_free(mem_block_t* block)
  87. {
  88. return(block->free);
  89. }
  90. UNIV_INLINE
  91. void
  92. mem_block_set_start(mem_block_t* block, ulint start)
  93. {
  94. ut_ad(start > 0);
  95. block->start = start;
  96. }
  97. UNIV_INLINE
  98. ulint
  99. mem_block_get_start(mem_block_t* block)
  100. {
  101. return(block->start);
  102. }
  103. /*******************************************************************
  104. Allocates n bytes of memory from a memory heap. */
  105. UNIV_INLINE
  106. void*
  107. mem_heap_alloc(
  108. /*===========*/
  109. /* out: allocated storage, NULL if did not
  110. succeed (only possible for
  111. MEM_HEAP_BTR_SEARCH type heaps) */
  112. mem_heap_t* heap, /* in: memory heap */
  113. ulint n) /* in: number of bytes; if the heap is allowed
  114. to grow into the buffer pool, this must be
  115. <= MEM_MAX_ALLOC_IN_BUF */
  116. {
  117. mem_block_t* block;
  118. void* buf;
  119. ulint free;
  120. ut_ad(mem_heap_check(heap));
  121. block = UT_LIST_GET_LAST(heap->base);
  122. ut_ad(!(block->type & MEM_HEAP_BUFFER) || (n <= MEM_MAX_ALLOC_IN_BUF));
  123. /* Check if there is enough space in block. If not, create a new
  124. block to the heap */
  125. if (mem_block_get_len(block)
  126. < mem_block_get_free(block) + MEM_SPACE_NEEDED(n)) {
  127. block = mem_heap_add_block(heap, n);
  128. if (block == NULL) {
  129. return(NULL);
  130. }
  131. }
  132. free = mem_block_get_free(block);
  133. buf = (byte*)block + free;
  134. mem_block_set_free(block, free + MEM_SPACE_NEEDED(n));
  135. #ifdef UNIV_MEM_DEBUG
  136. /* In the debug version write debugging info to the field */
  137. mem_field_init((byte*)buf, n);
  138. /* Advance buf to point at the storage which will be given to the
  139. caller */
  140. buf = (byte*)buf + MEM_FIELD_HEADER_SIZE;
  141. #endif
  142. #ifdef UNIV_SET_MEM_TO_ZERO
  143. memset(buf, '\0', n);
  144. #endif
  145. return(buf);
  146. }
  147. /*********************************************************************
  148. Returns a pointer to the heap top. */
  149. UNIV_INLINE
  150. byte*
  151. mem_heap_get_heap_top(
  152. /*==================*/
  153. /* out: pointer to the heap top */
  154. mem_heap_t* heap) /* in: memory heap */
  155. {
  156. mem_block_t* block;
  157. byte* buf;
  158. ut_ad(mem_heap_check(heap));
  159. block = UT_LIST_GET_LAST(heap->base);
  160. buf = (byte*)block + mem_block_get_free(block);
  161. return(buf);
  162. }
  163. /*********************************************************************
  164. Frees the space in a memory heap exceeding the pointer given. The
  165. pointer must have been acquired from mem_heap_get_heap_top. The first
  166. memory block of the heap is not freed. */
  167. UNIV_INLINE
  168. void
  169. mem_heap_free_heap_top(
  170. /*===================*/
  171. mem_heap_t* heap, /* in: heap from which to free */
  172. byte* old_top)/* in: pointer to old top of heap */
  173. {
  174. mem_block_t* block;
  175. mem_block_t* prev_block;
  176. #ifdef UNIV_MEM_DEBUG
  177. ibool error;
  178. ulint total_size;
  179. ulint size;
  180. #endif
  181. ut_ad(mem_heap_check(heap));
  182. #ifdef UNIV_MEM_DEBUG
  183. /* Validate the heap and get its total allocated size */
  184. mem_heap_validate_or_print(heap, NULL, FALSE, &error, &total_size,
  185. NULL, NULL);
  186. ut_a(!error);
  187. /* Get the size below top pointer */
  188. mem_heap_validate_or_print(heap, old_top, FALSE, &error, &size, NULL,
  189. NULL);
  190. ut_a(!error);
  191. #endif
  192. block = UT_LIST_GET_LAST(heap->base);
  193. while (block != NULL) {
  194. if (((byte*)block + mem_block_get_free(block) >= old_top)
  195. && ((byte*)block <= old_top)) {
  196. /* Found the right block */
  197. break;
  198. }
  199. /* Store prev_block value before freeing the current block
  200. (the current block will be erased in freeing) */
  201. prev_block = UT_LIST_GET_PREV(list, block);
  202. mem_heap_block_free(heap, block);
  203. block = prev_block;
  204. }
  205. ut_ad(block);
  206. /* Set the free field of block */
  207. mem_block_set_free(block, old_top - (byte*)block);
  208. #ifdef UNIV_MEM_DEBUG
  209. ut_ad(mem_block_get_start(block) <= mem_block_get_free(block));
  210. /* In the debug version erase block from top up */
  211. mem_erase_buf(old_top, (byte*)block + block->len - old_top);
  212. /* Update allocated memory count */
  213. mutex_enter(&mem_hash_mutex);
  214. mem_current_allocated_memory -= (total_size - size);
  215. mutex_exit(&mem_hash_mutex);
  216. #endif
  217. /* If free == start, we may free the block if it is not the first
  218. one */
  219. if ((heap != block) && (mem_block_get_free(block)
  220. == mem_block_get_start(block))) {
  221. mem_heap_block_free(heap, block);
  222. }
  223. }
  224. /*********************************************************************
  225. Empties a memory heap. The first memory block of the heap is not freed. */
  226. UNIV_INLINE
  227. void
  228. mem_heap_empty(
  229. /*===========*/
  230. mem_heap_t* heap) /* in: heap to empty */
  231. {
  232. mem_heap_free_heap_top(heap, (byte*)heap + mem_block_get_start(heap));
  233. if (heap->free_block) {
  234. mem_heap_free_block_free(heap);
  235. }
  236. }
  237. /*********************************************************************
  238. Returns a pointer to the topmost element in a memory heap. The size of the
  239. element must be given. */
  240. UNIV_INLINE
  241. void*
  242. mem_heap_get_top(
  243. /*=============*/
  244. /* out: pointer to the topmost element */
  245. mem_heap_t* heap, /* in: memory heap */
  246. ulint n) /* in: size of the topmost element */
  247. {
  248. mem_block_t* block;
  249. void* buf;
  250. ut_ad(mem_heap_check(heap));
  251. block = UT_LIST_GET_LAST(heap->base);
  252. buf = (byte*)block + mem_block_get_free(block) - MEM_SPACE_NEEDED(n);
  253. #ifdef UNIV_MEM_DEBUG
  254. ut_ad(mem_block_get_start(block) <=(ulint)((byte*)buf - (byte*)block));
  255. /* In the debug version, advance buf to point at the storage which
  256. was given to the caller in the allocation*/
  257. buf = (byte*)buf + MEM_FIELD_HEADER_SIZE;
  258. /* Check that the field lengths agree */
  259. ut_ad(n == (ulint)mem_field_header_get_len(buf));
  260. #endif
  261. return(buf);
  262. }
  263. /*********************************************************************
  264. Frees the topmost element in a memory heap. The size of the element must be
  265. given. */
  266. UNIV_INLINE
  267. void
  268. mem_heap_free_top(
  269. /*==============*/
  270. mem_heap_t* heap, /* in: memory heap */
  271. ulint n) /* in: size of the topmost element */
  272. {
  273. mem_block_t* block;
  274. ut_ad(mem_heap_check(heap));
  275. block = UT_LIST_GET_LAST(heap->base);
  276. /* Subtract the free field of block */
  277. mem_block_set_free(block, mem_block_get_free(block)
  278. - MEM_SPACE_NEEDED(n));
  279. #ifdef UNIV_MEM_DEBUG
  280. ut_ad(mem_block_get_start(block) <= mem_block_get_free(block));
  281. /* In the debug version check the consistency, and erase field */
  282. mem_field_erase((byte*)block + mem_block_get_free(block), n);
  283. #endif
  284. /* If free == start, we may free the block if it is not the first
  285. one */
  286. if ((heap != block) && (mem_block_get_free(block)
  287. == mem_block_get_start(block))) {
  288. mem_heap_block_free(heap, block);
  289. }
  290. }
  291. /*********************************************************************
  292. NOTE: Use the corresponding macros instead of this function. Creates a
  293. memory heap. For debugging purposes, takes also the file name and line as
  294. argument. */
  295. UNIV_INLINE
  296. mem_heap_t*
  297. mem_heap_create_func(
  298. /*=================*/
  299. /* out, own: memory heap, NULL if
  300. did not succeed (only possible for
  301. MEM_HEAP_BTR_SEARCH type heaps)*/
  302. ulint n, /* in: desired start block size,
  303. this means that a single user buffer
  304. of size n will fit in the block,
  305. 0 creates a default size block;
  306. if init_block is not NULL, n tells
  307. its size in bytes */
  308. void* init_block, /* in: if very fast creation is
  309. wanted, the caller can reserve some
  310. memory from its stack, for example,
  311. and pass it as the the initial block
  312. to the heap: then no OS call of malloc
  313. is needed at the creation. CAUTION:
  314. the caller must make sure the initial
  315. block is not unintentionally erased
  316. (if allocated in the stack), before
  317. the memory heap is explicitly freed. */
  318. ulint type, /* in: heap type */
  319. const char* file_name, /* in: file name where created */
  320. ulint line) /* in: line where created */
  321. {
  322. mem_block_t* block;
  323. if (n > 0) {
  324. block = mem_heap_create_block(NULL, n, init_block, type,
  325. file_name, line);
  326. } else {
  327. block = mem_heap_create_block(NULL, MEM_BLOCK_START_SIZE,
  328. init_block, type,
  329. file_name, line);
  330. }
  331. if (block == NULL) {
  332. return(NULL);
  333. }
  334. UT_LIST_INIT(block->base);
  335. /* Add the created block itself as the first block in the list */
  336. UT_LIST_ADD_FIRST(list, block->base, block);
  337. #ifdef UNIV_MEM_DEBUG
  338. mem_hash_insert(block, file_name, line);
  339. #endif
  340. return(block);
  341. }
  342. /*********************************************************************
  343. NOTE: Use the corresponding macro instead of this function. Frees the space
  344. occupied by a memory heap. In the debug version erases the heap memory
  345. blocks. */
  346. UNIV_INLINE
  347. void
  348. mem_heap_free_func(
  349. /*===============*/
  350. mem_heap_t* heap, /* in, own: heap to be freed */
  351. const char* file_name __attribute__((unused)),
  352. /* in: file name where freed */
  353. ulint line __attribute__((unused)))
  354. {
  355. mem_block_t* block;
  356. mem_block_t* prev_block;
  357. ut_ad(mem_heap_check(heap));
  358. block = UT_LIST_GET_LAST(heap->base);
  359. #ifdef UNIV_MEM_DEBUG
  360. /* In the debug version remove the heap from the hash table of heaps
  361. and check its consistency */
  362. mem_hash_remove(heap, file_name, line);
  363. #endif
  364. if (heap->free_block) {
  365. mem_heap_free_block_free(heap);
  366. }
  367. while (block != NULL) {
  368. /* Store the contents of info before freeing current block
  369. (it is erased in freeing) */
  370. prev_block = UT_LIST_GET_PREV(list, block);
  371. mem_heap_block_free(heap, block);
  372. block = prev_block;
  373. }
  374. }
  375. /*******************************************************************
  376. NOTE: Use the corresponding macro instead of this function.
  377. Allocates a single buffer of memory from the dynamic memory of
  378. the C compiler. Is like malloc of C. The buffer must be freed
  379. with mem_free. */
  380. UNIV_INLINE
  381. void*
  382. mem_alloc_func(
  383. /*===========*/
  384. /* out, own: free storage */
  385. ulint n, /* in: desired number of bytes */
  386. const char* file_name, /* in: file name where created */
  387. ulint line /* in: line where created */
  388. )
  389. {
  390. mem_heap_t* heap;
  391. void* buf;
  392. heap = mem_heap_create_func(n, NULL, MEM_HEAP_DYNAMIC, file_name,
  393. line);
  394. /* Note that as we created the first block in the heap big enough
  395. for the buffer requested by the caller, the buffer will be in the
  396. first block and thus we can calculate the pointer to the heap from
  397. the pointer to the buffer when we free the memory buffer. */
  398. buf = mem_heap_alloc(heap, n);
  399. ut_a((byte*)heap == (byte*)buf - MEM_BLOCK_HEADER_SIZE
  400. - MEM_FIELD_HEADER_SIZE);
  401. return(buf);
  402. }
  403. /*******************************************************************
  404. NOTE: Use the corresponding macro instead of this function. Frees a single
  405. buffer of storage from the dynamic memory of the C compiler. Similar to the
  406. free of C. */
  407. UNIV_INLINE
  408. void
  409. mem_free_func(
  410. /*==========*/
  411. void* ptr, /* in, own: buffer to be freed */
  412. const char* file_name, /* in: file name where created */
  413. ulint line /* in: line where created */
  414. )
  415. {
  416. mem_heap_t* heap;
  417. heap = (mem_heap_t*)((byte*)ptr - MEM_BLOCK_HEADER_SIZE
  418. - MEM_FIELD_HEADER_SIZE);
  419. mem_heap_free_func(heap, file_name, line);
  420. }
  421. /*********************************************************************
  422. Returns the space in bytes occupied by a memory heap. */
  423. UNIV_INLINE
  424. ulint
  425. mem_heap_get_size(
  426. /*==============*/
  427. mem_heap_t* heap) /* in: heap */
  428. {
  429. mem_block_t* block;
  430. ulint size = 0;
  431. ut_ad(mem_heap_check(heap));
  432. block = heap;
  433. while (block != NULL) {
  434. size += mem_block_get_len(block);
  435. block = UT_LIST_GET_NEXT(list, block);
  436. }
  437. if (heap->free_block) {
  438. size += UNIV_PAGE_SIZE;
  439. }
  440. return(size);
  441. }
  442. /**************************************************************************
  443. Duplicates a NUL-terminated string. */
  444. UNIV_INLINE
  445. char*
  446. mem_strdup(
  447. /*=======*/
  448. /* out, own: a copy of the string,
  449. must be deallocated with mem_free */
  450. const char* str) /* in: string to be copied */
  451. {
  452. ulint len = strlen(str) + 1;
  453. return(memcpy(mem_alloc(len), str, len));
  454. }
  455. /**************************************************************************
  456. Makes a NUL-terminated copy of a nonterminated string. */
  457. UNIV_INLINE
  458. char*
  459. mem_strdupl(
  460. /*========*/
  461. /* out, own: a copy of the string,
  462. must be deallocated with mem_free */
  463. const char* str, /* in: string to be copied */
  464. ulint len) /* in: length of str, in bytes */
  465. {
  466. char* s = mem_alloc(len + 1);
  467. s[len] = 0;
  468. return(memcpy(s, str, len));
  469. }
  470. /**************************************************************************
  471. Makes a NUL-terminated quoted copy of a NUL-terminated string. */
  472. UNIV_INLINE
  473. char*
  474. mem_strdupq(
  475. /*========*/
  476. /* out, own: a quoted copy of the string,
  477. must be deallocated with mem_free */
  478. const char* str, /* in: string to be copied */
  479. char q) /* in: quote character */
  480. {
  481. char* dst;
  482. char* d;
  483. const char* s = str;
  484. size_t len = strlen(str) + 3;
  485. /* calculate the number of quote characters in the string */
  486. while((s = strchr(s, q)) != NULL) {
  487. s++;
  488. len++;
  489. }
  490. /* allocate the quoted string, and copy it */
  491. d = dst = mem_alloc(len);
  492. *d++ = q;
  493. s = str;
  494. while(*s) {
  495. if ((*d++ = *s++) == q) {
  496. *d++ = q;
  497. }
  498. }
  499. *d++ = q;
  500. *d++ = '\0';
  501. ut_ad((ssize_t) len == d - dst);
  502. return(dst);
  503. }
  504. /**************************************************************************
  505. Makes a NUL-terminated copy of a nonterminated string,
  506. allocated from a memory heap. */
  507. UNIV_INLINE
  508. char*
  509. mem_heap_strdupl(
  510. /*=============*/
  511. /* out, own: a copy of the string */
  512. mem_heap_t* heap, /* in: memory heap where string is allocated */
  513. const char* str, /* in: string to be copied */
  514. ulint len) /* in: length of str, in bytes */
  515. {
  516. char* s = mem_heap_alloc(heap, len + 1);
  517. s[len] = 0;
  518. return(memcpy(s, str, len));
  519. }