Commit 4bf90e53 authored by marko's avatar marko

branches/zip: Reduce internal memory fragmentation.

mem_alloc2(): New macro.  This is a variant of mem_alloc() that
returns the allocated size, which is equal to or greater than
the requested size.

mem_alloc_func(): Add the output parameter *size for the allocated size.
When it is set, adjust the parameter passed to mem_heap_alloc().

rec_copy_prefix_to_buf_old(), rec_copy_prefix_to_buf(): Use mem_alloc2()
instead of mem_alloc().
parent 7a0a4df5
...@@ -199,7 +199,8 @@ Macro for memory buffer allocation */ ...@@ -199,7 +199,8 @@ Macro for memory buffer allocation */
#define mem_zalloc(N) memset(mem_alloc(N), 0, (N)); #define mem_zalloc(N) memset(mem_alloc(N), 0, (N));
#define mem_alloc(N) mem_alloc_func((N), __FILE__, __LINE__) #define mem_alloc(N) mem_alloc_func((N), NULL, __FILE__, __LINE__)
#define mem_alloc2(N,S) mem_alloc_func((N), (S), __FILE__, __LINE__)
/******************************************************************* /*******************************************************************
NOTE: Use the corresponding macro instead of this function. NOTE: Use the corresponding macro instead of this function.
Allocates a single buffer of memory from the dynamic memory of Allocates a single buffer of memory from the dynamic memory of
...@@ -210,7 +211,9 @@ void* ...@@ -210,7 +211,9 @@ void*
mem_alloc_func( mem_alloc_func(
/*===========*/ /*===========*/
/* out, own: free storage */ /* out, own: free storage */
ulint n, /* in: desired number of bytes */ ulint n, /* in: requested size in bytes */
ulint* size, /* out: allocated size in bytes,
or NULL */
const char* file_name, /* in: file name where created */ const char* file_name, /* in: file name where created */
ulint line); /* in: line where created */ ulint line); /* in: line where created */
......
...@@ -496,6 +496,8 @@ mem_alloc_func( ...@@ -496,6 +496,8 @@ mem_alloc_func(
/*===========*/ /*===========*/
/* out, own: free storage */ /* out, own: free storage */
ulint n, /* in: desired number of bytes */ ulint n, /* in: desired number of bytes */
ulint* size, /* out: allocated size in bytes,
or NULL */
const char* file_name, /* in: file name where created */ const char* file_name, /* in: file name where created */
ulint line) /* in: line where created */ ulint line) /* in: line where created */
{ {
...@@ -509,6 +511,18 @@ mem_alloc_func( ...@@ -509,6 +511,18 @@ mem_alloc_func(
first block and thus we can calculate the pointer to the heap from first block and thus we can calculate the pointer to the heap from
the pointer to the buffer when we free the memory buffer. */ the pointer to the buffer when we free the memory buffer. */
if (UNIV_LIKELY_NULL(size)) {
/* Adjust the allocation to the actual size of the
memory block. */
ulint m = mem_block_get_len(heap)
- mem_block_get_free(heap);
#ifdef UNIV_MEM_DEBUG
m -= MEM_FIELD_HEADER_SIZE + MEM_FIELD_TRAILER_SIZE;
#endif /* UNIV_MEM_DEBUG */
ut_ad(m >= n);
*size = n = m;
}
buf = mem_heap_alloc(heap, n); buf = mem_heap_alloc(heap, n);
ut_a((byte*)heap == (byte*)buf - MEM_BLOCK_HEADER_SIZE ut_a((byte*)heap == (byte*)buf - MEM_BLOCK_HEADER_SIZE
......
...@@ -1260,8 +1260,7 @@ rec_copy_prefix_to_buf_old( ...@@ -1260,8 +1260,7 @@ rec_copy_prefix_to_buf_old(
mem_free(*buf); mem_free(*buf);
} }
*buf = mem_alloc(prefix_len); *buf = mem_alloc2(prefix_len, buf_size);
*buf_size = prefix_len;
} }
ut_memcpy(*buf, rec - area_start, prefix_len); ut_memcpy(*buf, rec - area_start, prefix_len);
...@@ -1378,8 +1377,7 @@ rec_copy_prefix_to_buf( ...@@ -1378,8 +1377,7 @@ rec_copy_prefix_to_buf(
mem_free(*buf); mem_free(*buf);
} }
*buf = mem_alloc(prefix_len); *buf = mem_alloc2(prefix_len, buf_size);
*buf_size = prefix_len;
} }
memcpy(*buf, lens + 1, prefix_len); memcpy(*buf, lens + 1, prefix_len);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment