diff options
Diffstat (limited to 'storage/innobase/mem')
-rw-r--r-- | storage/innobase/mem/mem0dbg.c | 3 | ||||
-rw-r--r-- | storage/innobase/mem/mem0mem.c | 187 | ||||
-rw-r--r-- | storage/innobase/mem/mem0pool.c | 3 |
3 files changed, 185 insertions, 8 deletions
diff --git a/storage/innobase/mem/mem0dbg.c b/storage/innobase/mem/mem0dbg.c index 86c33a22531..1220c2c8e82 100644 --- a/storage/innobase/mem/mem0dbg.c +++ b/storage/innobase/mem/mem0dbg.c @@ -122,8 +122,7 @@ mem_init( /* Initialize the hash table */ ut_a(FALSE == mem_hash_initialized); - mutex_create(&mem_hash_mutex); - mutex_set_level(&mem_hash_mutex, SYNC_MEM_HASH); + mutex_create(&mem_hash_mutex, SYNC_MEM_HASH); for (i = 0; i < MEM_HASH_SIZE; i++) { UT_LIST_INIT(*mem_hash_get_nth_cell(i)); diff --git a/storage/innobase/mem/mem0mem.c b/storage/innobase/mem/mem0mem.c index 5e7c48d3d3d..3c42c1d0fff 100644 --- a/storage/innobase/mem/mem0mem.c +++ b/storage/innobase/mem/mem0mem.c @@ -17,6 +17,7 @@ Created 6/9/1994 Heikki Tuuri #include "btr0sea.h" #include "srv0srv.h" #include "mem0dbg.c" +#include <stdarg.h> /* THE MEMORY MANAGEMENT @@ -107,11 +108,45 @@ char* mem_heap_strdup( /*============*/ /* out, own: a copy of the string */ - mem_heap_t* heap, /* in: memory heap where string is allocated */ - const char* str) /* in: string to be copied */ + mem_heap_t* heap, /* in: memory heap where string is allocated */ + const char* str) /* in: string to be copied */ { - ulint len = strlen(str) + 1; - return(memcpy(mem_heap_alloc(heap, len), str, len)); + return(mem_heap_dup(heap, str, strlen(str) + 1)); +} + +/************************************************************************** +Duplicate a block of data, allocated from a memory heap. */ + +void* +mem_heap_dup( +/*=========*/ + /* out, own: a copy of the data */ + mem_heap_t* heap, /* in: memory heap where copy is allocated */ + const void* data, /* in: data to be copied */ + ulint len) /* in: length of data, in bytes */ +{ + return(memcpy(mem_heap_alloc(heap, len), data, len)); +} + +/************************************************************************** +Concatenate two memory blocks and return the result, using a memory heap. */ + +void* +mem_heap_cat( +/*=========*/ + /* out, own: the result */ + mem_heap_t* heap, /* in: memory heap where result is allocated */ + const void* b1, /* in: block 1 */ + ulint len1, /* in: length of b1, in bytes */ + const void* b2, /* in: block 2 */ + ulint len2) /* in: length of b2, in bytes */ +{ + void* res = mem_heap_alloc(heap, len1 + len2); + + memcpy(res, b1, len1); + memcpy(res + len1, b2, len2); + + return(res); } /************************************************************************** @@ -139,6 +174,150 @@ mem_heap_strcat( return(s); } + +/******************************************************************** +Helper function for mem_heap_printf. */ +static +ulint +mem_heap_printf_low( +/*================*/ + /* out: length of formatted string, + including terminating NUL */ + char* buf, /* in/out: buffer to store formatted string + in, or NULL to just calculate length */ + const char* format, /* in: format string */ + va_list ap) /* in: arguments */ +{ + ulint len = 0; + + while (*format) { + + /* Does this format specifier have the 'l' length modifier. */ + ibool is_long = FALSE; + + /* Length of one parameter. */ + size_t plen; + + if (*format++ != '%') { + /* Non-format character. */ + + len++; + + if (buf) { + *buf++ = *(format - 1); + } + + continue; + } + + if (*format == 'l') { + is_long = TRUE; + format++; + } + + switch (*format++) { + case 's': + /* string */ + { + char* s = va_arg(ap, char*); + + /* "%ls" is a non-sensical format specifier. */ + ut_a(!is_long); + + plen = strlen(s); + len += plen; + + if (buf) { + memcpy(buf, s, plen); + buf += plen; + } + } + + break; + + case 'u': + /* unsigned int */ + { + char tmp[32]; + unsigned long val; + + /* We only support 'long' values for now. */ + ut_a(is_long); + + val = va_arg(ap, unsigned long); + + plen = sprintf(tmp, "%lu", val); + len += plen; + + if (buf) { + memcpy(buf, tmp, plen); + buf += plen; + } + } + + break; + + case '%': + + /* "%l%" is a non-sensical format specifier. */ + ut_a(!is_long); + + len++; + + if (buf) { + *buf++ = '%'; + } + + break; + + default: + ut_error; + } + } + + /* For the NUL character. */ + len++; + + if (buf) { + *buf = '\0'; + } + + return(len); +} + +/******************************************************************** +A simple (s)printf replacement that dynamically allocates the space for the +formatted string from the given heap. This supports a very limited set of +the printf syntax: types 's' and 'u' and length modifier 'l' (which is +required for the 'u' type). */ + +char* +mem_heap_printf( +/*============*/ + /* out: heap-allocated formatted string */ + mem_heap_t* heap, /* in: memory heap */ + const char* format, /* in: format string */ + ...) +{ + va_list ap; + char* str; + ulint len; + + /* Calculate length of string */ + len = 0; + va_start(ap, format); + len = mem_heap_printf_low(NULL, format, ap); + va_end(ap); + + /* Now create it for real. */ + str = mem_heap_alloc(heap, len); + va_start(ap, format); + mem_heap_printf_low(str, format, ap); + va_end(ap); + + return(str); +} + /******************************************************************* Creates a memory heap block where data can be allocated. */ diff --git a/storage/innobase/mem/mem0pool.c b/storage/innobase/mem/mem0pool.c index a61ab1ce170..5606921758c 100644 --- a/storage/innobase/mem/mem0pool.c +++ b/storage/innobase/mem/mem0pool.c @@ -204,8 +204,7 @@ mem_pool_create( pool->buf = ut_malloc_low(size, FALSE, TRUE); pool->size = size; - mutex_create(&(pool->mutex)); - mutex_set_level(&(pool->mutex), SYNC_MEM_POOL); + mutex_create(&pool->mutex, SYNC_MEM_POOL); /* Initialize the free lists */ |