From 4bf90e535550c05df2cc7a961b44a4cdf70ec3dc Mon Sep 17 00:00:00 2001 From: marko Date: Fri, 21 Sep 2007 12:45:46 +0000 Subject: [PATCH] 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(). --- include/mem0mem.h | 7 +++++-- include/mem0mem.ic | 14 ++++++++++++++ rem/rem0rec.c | 6 ++---- 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/include/mem0mem.h b/include/mem0mem.h index b9d32527d44..fa371a02a29 100644 --- a/include/mem0mem.h +++ b/include/mem0mem.h @@ -199,7 +199,8 @@ Macro for memory buffer allocation */ #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. Allocates a single buffer of memory from the dynamic memory of @@ -210,7 +211,9 @@ void* mem_alloc_func( /*===========*/ /* 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 */ ulint line); /* in: line where created */ diff --git a/include/mem0mem.ic b/include/mem0mem.ic index 65b270847ca..6cfe1a6d532 100644 --- a/include/mem0mem.ic +++ b/include/mem0mem.ic @@ -496,6 +496,8 @@ mem_alloc_func( /*===========*/ /* out, own: free storage */ 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 */ ulint line) /* in: line where created */ { @@ -509,6 +511,18 @@ mem_alloc_func( first block and thus we can calculate the pointer to the heap from 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); ut_a((byte*)heap == (byte*)buf - MEM_BLOCK_HEADER_SIZE diff --git a/rem/rem0rec.c b/rem/rem0rec.c index 826a43e9411..a9efdaeaa4a 100644 --- a/rem/rem0rec.c +++ b/rem/rem0rec.c @@ -1260,8 +1260,7 @@ rec_copy_prefix_to_buf_old( mem_free(*buf); } - *buf = mem_alloc(prefix_len); - *buf_size = prefix_len; + *buf = mem_alloc2(prefix_len, buf_size); } ut_memcpy(*buf, rec - area_start, prefix_len); @@ -1378,8 +1377,7 @@ rec_copy_prefix_to_buf( mem_free(*buf); } - *buf = mem_alloc(prefix_len); - *buf_size = prefix_len; + *buf = mem_alloc2(prefix_len, buf_size); } memcpy(*buf, lens + 1, prefix_len);