diff options
author | Davi Arnaut <Davi.Arnaut@Sun.COM> | 2010-07-08 18:20:08 -0300 |
---|---|---|
committer | Davi Arnaut <Davi.Arnaut@Sun.COM> | 2010-07-08 18:20:08 -0300 |
commit | f56dd32bf7c5b8a8cf35984f39f1a253b75945ff (patch) | |
tree | 6c88c3c07b30acb464ca8bf81bbef916216da616 /mysys | |
parent | d3b01fef18b20f3ac589f2ecc95d64326570583f (diff) | |
download | mariadb-git-f56dd32bf7c5b8a8cf35984f39f1a253b75945ff.tar.gz |
Bug#34043: Server loops excessively in _checkchunk() when safemalloc is enabled
Essentially, the problem is that safemalloc is excruciatingly
slow as it checks all allocated blocks for overrun at each
memory management primitive, yielding a almost exponential
slowdown for the memory management functions (malloc, realloc,
free). The overrun check basically consists of verifying some
bytes of a block for certain magic keys, which catches some
simple forms of overrun. Another minor problem is violation
of aliasing rules and that its own internal list of blocks
is prone to corruption.
Another issue with safemalloc is rather the maintenance cost
as the tool has a significant impact on the server code.
Given the magnitude of memory debuggers available nowadays,
especially those that are provided with the platform malloc
implementation, maintenance of a in-house and largely obsolete
memory debugger becomes a burden that is not worth the effort
due to its slowness and lack of support for detecting more
common forms of heap corruption.
Since there are third-party tools that can provide the same
functionality at a lower or comparable performance cost, the
solution is to simply remove safemalloc. Third-party tools
can provide the same functionality at a lower or comparable
performance cost.
The removal of safemalloc also allows a simplification of the
malloc wrappers, removing quite a bit of kludge: redefinition
of my_malloc, my_free and the removal of the unused second
argument of my_free. Since free() always check whether the
supplied pointer is null, redudant checks are also removed.
Also, this patch adds unit testing for my_malloc and moves
my_realloc implementation into the same file as the other
memory allocation primitives.
client/mysqldump.c:
Pass my_free directly as its signature is compatible with the
callback type -- which wasn't the case for free_table_ent.
Diffstat (limited to 'mysys')
42 files changed, 179 insertions, 793 deletions
diff --git a/mysys/CMakeLists.txt b/mysys/CMakeLists.txt index 0ea65ce8e4b..83615c82e2a 100755 --- a/mysys/CMakeLists.txt +++ b/mysys/CMakeLists.txt @@ -29,10 +29,10 @@ SET(MYSYS_SOURCES array.c charset-def.c charset.c checksum.c default.c default_ my_gethwaddr.c my_getopt.c my_getsystime.c my_getwd.c my_handler.c my_init.c my_lib.c my_lock.c my_lockmem.c my_malloc.c my_mess.c my_mkdir.c my_mmap.c my_net.c my_once.c my_open.c my_pread.c my_pthread.c - my_quick.c my_read.c my_realloc.c my_redel.c my_rename.c my_seek.c my_sleep.c + my_quick.c my_read.c my_redel.c my_rename.c my_seek.c my_sleep.c my_static.c my_symlink.c my_symlink2.c my_sync.c my_thr_init.c my_write.c ptr_cmp.c queues.c stacktrace.c - rijndael.c safemalloc.c sha1.c string.c thr_alarm.c thr_lock.c thr_mutex.c + rijndael.c sha1.c string.c thr_alarm.c thr_lock.c thr_mutex.c thr_rwlock.c tree.c typelib.c my_vle.c base64.c my_memmem.c my_getpagesize.c lf_alloc-pin.c lf_dynarray.c lf_hash.c my_atomic.c my_getncpus.c diff --git a/mysys/Makefile.am b/mysys/Makefile.am index d5bffd874b2..f4fab89d5a5 100644 --- a/mysys/Makefile.am +++ b/mysys/Makefile.am @@ -29,9 +29,8 @@ libmysys_a_SOURCES = my_init.c my_getwd.c mf_getdate.c my_mmap.c \ mf_keycaches.c my_crc32.c \ mf_iocache.c mf_iocache2.c mf_cache.c mf_tempfile.c \ mf_tempdir.c my_lock.c mf_brkhant.c my_alarm.c \ - my_malloc.c my_realloc.c my_once.c mulalloc.c \ - my_alloc.c safemalloc.c my_new.cc \ - my_vle.c my_atomic.c lf_hash.c \ + my_malloc.c my_once.c mulalloc.c \ + my_alloc.c my_new.cc my_vle.c my_atomic.c lf_hash.c \ lf_dynarray.c lf_alloc-pin.c \ my_fopen.c my_fstream.c my_getsystime.c \ my_error.c errors.c my_div.c my_mess.c \ diff --git a/mysys/array.c b/mysys/array.c index a1c49c2589d..70c7a59aa3a 100644 --- a/mysys/array.c +++ b/mysys/array.c @@ -42,7 +42,7 @@ my_bool init_dynamic_array2(DYNAMIC_ARRAY *array, uint element_size, void *init_buffer, uint init_alloc, - uint alloc_increment CALLER_INFO_PROTO) + uint alloc_increment) { DBUG_ENTER("init_dynamic_array"); if (!alloc_increment) @@ -67,14 +67,13 @@ my_bool init_dynamic_array2(DYNAMIC_ARRAY *array, uint element_size, Since the dynamic array is usable even if allocation fails here malloc should not throw an error */ - if (!(array->buffer= (uchar*) my_malloc_ci(element_size*init_alloc, MYF(0)))) + if (!(array->buffer= (uchar*) my_malloc(element_size*init_alloc, MYF(0)))) array->max_element=0; DBUG_RETURN(FALSE); } my_bool init_dynamic_array(DYNAMIC_ARRAY *array, uint element_size, - uint init_alloc, - uint alloc_increment CALLER_INFO_PROTO) + uint init_alloc, uint alloc_increment) { /* placeholder to preserve ABI */ return my_init_dynamic_array_ci(array, element_size, init_alloc, @@ -306,7 +305,7 @@ void delete_dynamic(DYNAMIC_ARRAY *array) else if (array->buffer) { - my_free(array->buffer,MYF(MY_WME)); + my_free(array->buffer); array->buffer=0; array->elements=array->max_element=0; } diff --git a/mysys/charset.c b/mysys/charset.c index 9f9d18e31a4..167d6b8ff6e 100644 --- a/mysys/charset.c +++ b/mysys/charset.c @@ -383,11 +383,11 @@ static my_bool my_read_charset_file(const char *filename, myf myflags) #endif } - my_free(buf, myflags); + my_free(buf); return FALSE; error: - my_free(buf, myflags); + my_free(buf); return TRUE; } diff --git a/mysys/default_modify.c b/mysys/default_modify.c index b214a1df445..edf4907cd4b 100644 --- a/mysys/default_modify.c +++ b/mysys/default_modify.c @@ -223,11 +223,11 @@ int modify_defaults_file(const char *file_location, const char *option, if (my_fclose(cnf_file, MYF(MY_WME))) DBUG_RETURN(1); - my_free(file_buffer, MYF(0)); + my_free(file_buffer); DBUG_RETURN(0); err: - my_free(file_buffer, MYF(0)); + my_free(file_buffer); malloc_err: my_fclose(cnf_file, MYF(0)); DBUG_RETURN(1); /* out of resources */ diff --git a/mysys/hash.c b/mysys/hash.c index 39f3ad8d31e..f54ac1a4abb 100644 --- a/mysys/hash.c +++ b/mysys/hash.c @@ -67,8 +67,6 @@ static my_hash_value_type calc_hash(const HASH *hash, @param[in] get_key get the key for the hash @param[in] free_element pointer to the function that does cleanup - @param[in] CALLER_INFO_PROTO flag that define the behaviour - of the hash @return inidicates success or failure of initialization @retval 0 success @retval 1 failure @@ -77,7 +75,7 @@ my_bool _my_hash_init(HASH *hash, uint growth_size, CHARSET_INFO *charset, ulong size, size_t key_offset, size_t key_length, my_hash_get_key get_key, - void (*free_element)(void*), uint flags CALLER_INFO_PROTO) + void (*free_element)(void*), uint flags) { DBUG_ENTER("my_hash_init"); DBUG_PRINT("enter",("hash: 0x%lx size: %u", (long) hash, (uint) size)); diff --git a/mysys/lf_alloc-pin.c b/mysys/lf_alloc-pin.c index 7fd10703871..c264d3ac4c5 100644 --- a/mysys/lf_alloc-pin.c +++ b/mysys/lf_alloc-pin.c @@ -472,7 +472,7 @@ void lf_alloc_destroy(LF_ALLOCATOR *allocator) uchar *tmp= anext_node(node); if (allocator->destructor) allocator->destructor(node); - my_free((void *)node, MYF(0)); + my_free(node); node= tmp; } lf_pinbox_destroy(&allocator->pinbox); diff --git a/mysys/lf_dynarray.c b/mysys/lf_dynarray.c index b1cdce698a9..0941c8762bb 100644 --- a/mysys/lf_dynarray.c +++ b/mysys/lf_dynarray.c @@ -57,10 +57,10 @@ static void recursive_free(void **alloc, int level) int i; for (i= 0; i < LF_DYNARRAY_LEVEL_LENGTH; i++) recursive_free(alloc[i], level-1); - my_free((void *)alloc, MYF(0)); + my_free(alloc); } else - my_free(alloc[-1], MYF(0)); + my_free(alloc[-1]); } void lf_dynarray_destroy(LF_DYNARRAY *array) @@ -115,7 +115,7 @@ void *_lf_dynarray_lvalue(LF_DYNARRAY *array, uint idx) if (my_atomic_casptr(ptr_ptr, &ptr, alloc)) ptr= alloc; else - my_free(alloc, MYF(0)); + my_free(alloc); } ptr_ptr= ((void **)ptr) + idx / dynarray_idxes_in_prev_level[i]; idx%= dynarray_idxes_in_prev_level[i]; @@ -139,7 +139,7 @@ void *_lf_dynarray_lvalue(LF_DYNARRAY *array, uint idx) if (my_atomic_casptr(ptr_ptr, &ptr, data)) ptr= data; else - my_free(alloc, MYF(0)); + my_free(alloc); } return ((uchar*)ptr) + array->size_of_element * idx; } diff --git a/mysys/lf_hash.c b/mysys/lf_hash.c index f478196c7c8..9c51ff1766e 100644 --- a/mysys/lf_hash.c +++ b/mysys/lf_hash.c @@ -344,7 +344,7 @@ void lf_hash_destroy(LF_HASH *hash) if (el->hashnr & 1) lf_alloc_direct_free(&hash->alloc, el); /* normal node */ else - my_free((void *)el, MYF(0)); /* dummy node */ + my_free(el); /* dummy node */ el= (LF_SLIST *)next; } lf_alloc_destroy(&hash->alloc); @@ -489,7 +489,7 @@ static int initialize_bucket(LF_HASH *hash, LF_SLIST * volatile *node, dummy->keylen= 0; if ((cur= linsert(el, hash->charset, dummy, pins, LF_HASH_UNIQUE))) { - my_free((void *)dummy, MYF(0)); + my_free(dummy); dummy= cur; } my_atomic_casptr((void **)node, (void **)&tmp, dummy); diff --git a/mysys/list.c b/mysys/list.c index aaadd686365..e68fbf519d1 100644 --- a/mysys/list.c +++ b/mysys/list.c @@ -61,8 +61,8 @@ void list_free(LIST *root, uint free_data) { next=root->next; if (free_data) - my_free((uchar*) root->data,MYF(0)); - my_free((uchar*) root,MYF(0)); + my_free(root->data); + my_free(root); root=next; } } diff --git a/mysys/mf_cache.c b/mysys/mf_cache.c index f0df0f3fa77..691532c0d80 100644 --- a/mysys/mf_cache.c +++ b/mysys/mf_cache.c @@ -71,8 +71,8 @@ my_bool open_cached_file(IO_CACHE *cache, const char* dir, const char *prefix, { DBUG_RETURN(0); } - my_free(cache->dir, MYF(MY_ALLOW_ZERO_PTR)); - my_free(cache->prefix,MYF(MY_ALLOW_ZERO_PTR)); + my_free(cache->dir); + my_free(cache->prefix); DBUG_RETURN(1); } @@ -110,12 +110,12 @@ void close_cached_file(IO_CACHE *cache) if (cache->file_name) { (void) my_delete(cache->file_name,MYF(MY_WME | ME_NOINPUT)); - my_free(cache->file_name,MYF(0)); + my_free(cache->file_name); } #endif } - my_free(cache->dir,MYF(MY_ALLOW_ZERO_PTR)); - my_free(cache->prefix,MYF(MY_ALLOW_ZERO_PTR)); + my_free(cache->dir); + my_free(cache->prefix); } DBUG_VOID_RETURN; } diff --git a/mysys/mf_iocache.c b/mysys/mf_iocache.c index 620ac667a8b..daacc08044b 100644 --- a/mysys/mf_iocache.c +++ b/mysys/mf_iocache.c @@ -1831,7 +1831,7 @@ int end_io_cache(IO_CACHE *info) info->alloced_buffer=0; if (info->file != -1) /* File doesn't exist */ error= my_b_flush_io_cache(info,1); - my_free((uchar*) info->buffer,MYF(MY_WME)); + my_free(info->buffer); info->buffer=info->read_pos=(uchar*) 0; } if (info->type == SEQ_READ_APPEND) @@ -1917,7 +1917,7 @@ int main(int argc, char** argv) total_bytes += 4+block_size; } close_file(&sra_cache); - my_free(block,MYF(MY_WME)); + my_free(block); if (!my_stat(fname,&status,MYF(MY_WME))) die("%s failed to stat, but I had just closed it,\ wonder how that happened"); diff --git a/mysys/mf_keycache.c b/mysys/mf_keycache.c index 9cbe3a21bce..5d0808933e3 100644 --- a/mysys/mf_keycache.c +++ b/mysys/mf_keycache.c @@ -446,7 +446,7 @@ int init_key_cache(KEY_CACHE *keycache, uint key_cache_block_size, if ((keycache->block_root= (BLOCK_LINK*) my_malloc(length, MYF(0)))) break; - my_large_free(keycache->block_mem, MYF(0)); + my_large_free(keycache->block_mem); keycache->block_mem= 0; } if (blocks < 8) @@ -521,12 +521,12 @@ err: keycache->blocks= 0; if (keycache->block_mem) { - my_large_free((uchar*) keycache->block_mem, MYF(0)); + my_large_free((uchar*) keycache->block_mem); keycache->block_mem= NULL; } if (keycache->block_root) { - my_free((uchar*) keycache->block_root, MYF(0)); + my_free(keycache->block_root); keycache->block_root= NULL; } my_errno= error; @@ -747,9 +747,9 @@ void end_key_cache(KEY_CACHE *keycache, my_bool cleanup) { if (keycache->block_mem) { - my_large_free((uchar*) keycache->block_mem, MYF(0)); + my_large_free((uchar*) keycache->block_mem); keycache->block_mem= NULL; - my_free((uchar*) keycache->block_root, MYF(0)); + my_free(keycache->block_root); keycache->block_root= NULL; } keycache->disk_blocks= -1; @@ -4080,7 +4080,7 @@ restart: #endif err: if (cache != cache_buff) - my_free((uchar*) cache, MYF(0)); + my_free(cache); if (last_errno) errno=last_errno; /* Return first error */ DBUG_RETURN(last_errno != 0); diff --git a/mysys/mf_keycaches.c b/mysys/mf_keycaches.c index 999e8cc7975..ee4ad025b0b 100644 --- a/mysys/mf_keycaches.c +++ b/mysys/mf_keycaches.c @@ -71,7 +71,7 @@ typedef struct st_safe_hash_with_default static void safe_hash_entry_free(SAFE_HASH_ENTRY *entry) { DBUG_ENTER("free_assign_entry"); - my_free((uchar*) entry, MYF(0)); + my_free(entry); DBUG_VOID_RETURN; } @@ -234,7 +234,7 @@ static my_bool safe_hash_set(SAFE_HASH *hash, const uchar *key, uint length, if (my_hash_insert(&hash->hash, (uchar*) entry)) { /* This can only happen if hash got out of memory */ - my_free((char*) entry, MYF(0)); + my_free(entry); error= 1; goto end; } diff --git a/mysys/mf_sort.c b/mysys/mf_sort.c index 686ebbc1d14..a3e7465ead0 100644 --- a/mysys/mf_sort.c +++ b/mysys/mf_sort.c @@ -27,7 +27,7 @@ void my_string_ptr_sort(uchar *base, uint items, size_t size) (ptr= (uchar**) my_malloc(items*sizeof(char*),MYF(0)))) { radixsort_for_str_ptr((uchar**) base,items,size,ptr); - my_free((uchar*) ptr,MYF(0)); + my_free(ptr); } else #endif diff --git a/mysys/mf_tempdir.c b/mysys/mf_tempdir.c index 5633182ab3a..1c6c01cef9f 100644 --- a/mysys/mf_tempdir.c +++ b/mysys/mf_tempdir.c @@ -88,7 +88,7 @@ void free_tmpdir(MY_TMPDIR *tmpdir) if (!tmpdir->full_list.elements) return; for (i=0; i<=tmpdir->max; i++) - my_free(tmpdir->list[i], MYF(0)); + my_free(tmpdir->list[i]); delete_dynamic(&tmpdir->full_list); mysql_mutex_destroy(&tmpdir->mutex); } diff --git a/mysys/mf_wfile.c b/mysys/mf_wfile.c index 4a4fb466600..95c4c006b2c 100644 --- a/mysys/mf_wfile.c +++ b/mysys/mf_wfile.c @@ -118,7 +118,6 @@ found: void wf_end(WF_PACK *buffer) { DBUG_ENTER("wf_end"); - if (buffer) - my_free(buffer, MYF(0)); + my_free(buffer); DBUG_VOID_RETURN; } /* wf_end */ diff --git a/mysys/my_alloc.c b/mysys/my_alloc.c index 19e51880209..903826dd975 100644 --- a/mysys/my_alloc.c +++ b/mysys/my_alloc.c @@ -120,7 +120,7 @@ void reset_root_defaults(MEM_ROOT *mem_root, size_t block_size, { /* remove block from the list and free it */ *prev= mem->next; - my_free(mem, MYF(0)); + my_free(mem); } else prev= &mem->next; @@ -362,13 +362,13 @@ void free_root(MEM_ROOT *root, myf MyFlags) { old=next; next= next->next ; if (old != root->pre_alloc) - my_free(old,MYF(0)); + my_free(old); } for (next=root->free ; next ;) { old=next; next= next->next; if (old != root->pre_alloc) - my_free(old,MYF(0)); + my_free(old); } root->used=root->free=0; if (root->pre_alloc) diff --git a/mysys/my_bitmap.c b/mysys/my_bitmap.c index 91370bd3727..25d2f3fda30 100644 --- a/mysys/my_bitmap.c +++ b/mysys/my_bitmap.c @@ -150,7 +150,7 @@ void bitmap_free(MY_BITMAP *map) if (map->mutex) mysql_mutex_destroy(map->mutex); #endif - my_free((char*) map->bitmap, MYF(0)); + my_free(map->bitmap); map->bitmap=0; } DBUG_VOID_RETURN; diff --git a/mysys/my_compress.c b/mysys/my_compress.c index a3d9d56915e..360390d376a 100644 --- a/mysys/my_compress.c +++ b/mysys/my_compress.c @@ -51,7 +51,7 @@ my_bool my_compress(uchar *packet, size_t *len, size_t *complen) if (!compbuf) DBUG_RETURN(*complen ? 0 : 1); memcpy(packet,compbuf,*len); - my_free(compbuf,MYF(MY_WME)); + my_free(compbuf); } DBUG_RETURN(0); } @@ -73,14 +73,14 @@ uchar *my_compress_alloc(const uchar *packet, size_t *len, size_t *complen) if (res != Z_OK) { - my_free(compbuf, MYF(MY_WME)); + my_free(compbuf); return 0; } if (*complen >= *len) { *complen= 0; - my_free(compbuf, MYF(MY_WME)); + my_free(compbuf); DBUG_PRINT("note",("Packet got longer on compression; Not compressed")); return 0; } @@ -125,11 +125,11 @@ my_bool my_uncompress(uchar *packet, size_t len, size_t *complen) if (error != Z_OK) { /* Probably wrong packet */ DBUG_PRINT("error",("Can't uncompress packet, error: %d",error)); - my_free(compbuf, MYF(MY_WME)); + my_free(compbuf); DBUG_RETURN(1); } memcpy(packet, compbuf, *complen); - my_free(compbuf, MYF(MY_WME)); + my_free(compbuf); } else *complen= len; @@ -250,7 +250,7 @@ int unpackfrm(uchar **unpack_data, size_t *unpack_len, if (my_uncompress(data, complen, &orglen)) { - my_free(data, MYF(0)); + my_free(data); DBUG_RETURN(3); } diff --git a/mysys/my_error.c b/mysys/my_error.c index e2523a39d0b..fa62cc604b6 100644 --- a/mysys/my_error.c +++ b/mysys/my_error.c @@ -211,7 +211,7 @@ int my_error_register(const char** (*get_errmsgs) (), int first, int last) /* Error numbers must be unique. No overlapping is allowed. */ if (*search_meh_pp && ((*search_meh_pp)->meh_first <= last)) { - my_free((uchar*)meh_p, MYF(0)); + my_free(meh_p); return 1; } @@ -267,7 +267,7 @@ const char **my_error_unregister(int first, int last) /* Save the return value and free the header. */ errmsgs= meh_p->get_errmsgs(); - my_free((uchar*) meh_p, MYF(0)); + my_free(meh_p); return errmsgs; } @@ -282,7 +282,7 @@ void my_error_unregister_all(void) /* We need this ptr, but we're about to free its container, so save it. */ saved_next= cursor->meh_next; - my_free((uchar*) cursor, MYF(0)); + my_free(cursor); } my_errmsgs_globerrs.meh_next= NULL; /* Freed in first iteration above. */ diff --git a/mysys/my_file.c b/mysys/my_file.c index a31673b31bc..e4b7cd7779f 100644 --- a/mysys/my_file.c +++ b/mysys/my_file.c @@ -127,7 +127,7 @@ void my_free_open_file_info() /* Copy data back for my_print_open_files */ memcpy((char*) my_file_info_default, my_file_info, sizeof(*my_file_info_default)* MY_NFILE); - my_free((char*) my_file_info, MYF(0)); + my_free(my_file_info); my_file_info= my_file_info_default; my_file_limit= MY_NFILE; } diff --git a/mysys/my_fopen.c b/mysys/my_fopen.c index 3f2bc08df65..861e4380690 100644 --- a/mysys/my_fopen.c +++ b/mysys/my_fopen.c @@ -117,7 +117,7 @@ int my_fclose(FILE *fd, myf MyFlags) if ((uint) file < my_file_limit && my_file_info[file].type != UNOPEN) { my_file_info[file].type = UNOPEN; - my_free(my_file_info[file].name, MYF(MY_ALLOW_ZERO_PTR)); + my_free(my_file_info[file].name); } mysql_mutex_unlock(&THR_LOCK_open); DBUG_RETURN(err); diff --git a/mysys/my_gethwaddr.c b/mysys/my_gethwaddr.c index c6a7af58f57..87e6519cafe 100644 --- a/mysys/my_gethwaddr.c +++ b/mysys/my_gethwaddr.c @@ -196,7 +196,7 @@ my_bool my_gethwaddr(uchar *to) /* Clean up memory allocation. */ if (pAdapterAddresses != &adapterAddresses) - my_free(pAdapterAddresses, 0); + my_free(pAdapterAddresses); return return_val; } diff --git a/mysys/my_getopt.c b/mysys/my_getopt.c index 1e94dd2d761..f51dd7befd5 100644 --- a/mysys/my_getopt.c +++ b/mysys/my_getopt.c @@ -596,8 +596,7 @@ static int setval(const struct my_option *opts, void *value, char *argument, case GET_STR_ALLOC: if (argument == enabled_my_option) break; /* string options don't use this default of "1" */ - if ((*((char**) value))) - my_free((*(char**) value), MYF(MY_WME | MY_FAE)); + my_free(*((char**) value)); if (!(*((char**) value)= my_strdup(argument, MYF(MY_WME)))) { res= EXIT_OUT_OF_MEMORY; @@ -1054,8 +1053,9 @@ static void init_one_value(const struct my_option *option, void *variable, */ if ((char*) (intptr) value) { - my_free((*(char**) variable), MYF(MY_ALLOW_ZERO_PTR)); - *((char**) variable)= my_strdup((char*) (intptr) value, MYF(MY_WME)); + char **pstr= (char **) variable; + my_free(*pstr); + *pstr= my_strdup((char*) (intptr) value, MYF(MY_WME)); } break; default: /* dummy default to avoid compiler warnings */ @@ -1080,7 +1080,7 @@ static void fini_one_value(const struct my_option *option, void *variable, DBUG_ENTER("fini_one_value"); switch ((option->var_type & GET_TYPE_MASK)) { case GET_STR_ALLOC: - my_free((*(char**) variable), MYF(MY_ALLOW_ZERO_PTR)); + my_free(*((char**) variable)); *((char**) variable)= NULL; break; default: /* dummy default to avoid compiler warnings */ diff --git a/mysys/my_init.c b/mysys/my_init.c index 5bda2cb03ac..41a2efe0b90 100644 --- a/mysys/my_init.c +++ b/mysys/my_init.c @@ -141,6 +141,7 @@ my_bool my_init(void) { if (my_init_done) return 0; + my_init_done= 1; if (my_basic_init()) @@ -241,9 +242,7 @@ Voluntary context switches %ld, Involuntary context switches %ld\n", #if defined(__NETWARE__) && !defined(__WIN__) fprintf(info_file,"\nRun time: %.1f\n",(double) clock()/CLOCKS_PER_SEC); #endif -#if defined(SAFEMALLOC) - TERMINATE(stderr, (infoflag & MY_GIVE_INFO) != 0); -#elif defined(__WIN__) && defined(_MSC_VER) +#if defined(__WIN__) && defined(_MSC_VER) _CrtSetReportMode( _CRT_WARN, _CRTDBG_MODE_FILE ); _CrtSetReportFile( _CRT_WARN, _CRTDBG_FILE_STDERR ); _CrtSetReportMode( _CRT_ERROR, _CRTDBG_MODE_FILE ); @@ -254,10 +253,6 @@ Voluntary context switches %ld, Involuntary context switches %ld\n", _CrtDumpMemoryLeaks(); #endif } - else if (infoflag & MY_CHECK_ERROR) - { - TERMINATE(stderr, 0); /* Print memory leaks on screen */ - } if (!(infoflag & MY_DONT_FREE_DBUG)) { @@ -280,6 +275,7 @@ Voluntary context switches %ld, Involuntary context switches %ld\n", if (have_tcpip) WSACleanup(); #endif /* __WIN__ */ + my_init_done=0; my_basic_init_done= 0; } /* my_end */ diff --git a/mysys/my_largepage.c b/mysys/my_largepage.c index e65d3a0a5f5..9f5ab01a2b7 100644 --- a/mysys/my_largepage.c +++ b/mysys/my_largepage.c @@ -27,7 +27,7 @@ static uint my_get_large_page_size_int(void); static uchar* my_large_malloc_int(size_t size, myf my_flags); -static my_bool my_large_free_int(uchar* ptr, myf my_flags); +static my_bool my_large_free_int(uchar* ptr); /* Gets the size of large pages from the OS */ @@ -70,7 +70,7 @@ uchar* my_large_malloc(size_t size, myf my_flags) to my_free_lock() in case of failure */ -void my_large_free(uchar* ptr, myf my_flags __attribute__((unused))) +void my_large_free(uchar* ptr) { DBUG_ENTER("my_large_free"); @@ -79,9 +79,8 @@ void my_large_free(uchar* ptr, myf my_flags __attribute__((unused))) my_large_malloc_int(), i.e. my_malloc_lock() was used so we should free it with my_free_lock() */ - if (!my_use_large_pages || !my_large_page_size || - !my_large_free_int(ptr, my_flags)) - my_free_lock(ptr, my_flags); + if (!my_use_large_pages || !my_large_page_size || !my_large_free_int(ptr)) + my_free_lock(ptr); DBUG_VOID_RETURN; } @@ -157,7 +156,7 @@ uchar* my_large_malloc_int(size_t size, myf my_flags) /* Linux-specific large pages deallocator */ -my_bool my_large_free_int(uchar *ptr, myf my_flags __attribute__((unused))) +my_bool my_large_free_int(uchar *ptr) { DBUG_ENTER("my_large_free_int"); DBUG_RETURN(shmdt(ptr) == 0); diff --git a/mysys/my_lib.c b/mysys/my_lib.c index 0113d1498df..e70a70d47ed 100644 --- a/mysys/my_lib.c +++ b/mysys/my_lib.c @@ -77,7 +77,7 @@ void my_dirend(MY_DIR *buffer) ALIGN_SIZE(sizeof(MY_DIR)))); free_root((MEM_ROOT*)((char*)buffer + ALIGN_SIZE(sizeof(MY_DIR)) + ALIGN_SIZE(sizeof(DYNAMIC_ARRAY))), MYF(0)); - my_free((uchar*) buffer,MYF(0)); + my_free(buffer); } DBUG_VOID_RETURN; } /* my_dirend */ @@ -131,7 +131,7 @@ MY_DIR *my_dir(const char *path, myf MyFlags) if (my_init_dynamic_array(dir_entries_storage, sizeof(FILEINFO), ENTRIES_START_SIZE, ENTRIES_INCREMENT)) { - my_free((uchar*) buffer,MYF(0)); + my_free(buffer); goto error; } init_alloc_root(names_storage, NAMES_START_SIZE, NAMES_START_SIZE); @@ -400,7 +400,7 @@ MY_DIR *my_dir(const char *path, myf MyFlags) if (my_init_dynamic_array(dir_entries_storage, sizeof(FILEINFO), ENTRIES_START_SIZE, ENTRIES_INCREMENT)) { - my_free((uchar*) buffer,MYF(0)); + my_free(buffer); goto error; } init_alloc_root(names_storage, NAMES_START_SIZE, NAMES_START_SIZE); @@ -547,7 +547,7 @@ MY_STAT *my_stat(const char *path, MY_STAT *stat_area, myf my_flags) DBUG_PRINT("error",("Got errno: %d from stat", errno)); my_errno= errno; if (m_used) /* Free if new area */ - my_free((uchar*) stat_area,MYF(0)); + my_free(stat_area); error: if (my_flags & (MY_FAE+MY_WME)) diff --git a/mysys/my_lockmem.c b/mysys/my_lockmem.c index 1b582783d33..a37db6b2089 100644 --- a/mysys/my_lockmem.c +++ b/mysys/my_lockmem.c @@ -74,7 +74,7 @@ uchar *my_malloc_lock(uint size,myf MyFlags) } -void my_free_lock(uchar *ptr,myf Myflags __attribute__((unused))) +void my_free_lock(uchar *ptr) { LIST *list; struct st_mem_list *element=0; @@ -91,8 +91,7 @@ void my_free_lock(uchar *ptr,myf Myflags __attribute__((unused))) } } mysql_mutex_unlock(&THR_LOCK_malloc); - if (element) - my_free((uchar*) element,MYF(0)); + my_free(element); free(ptr); /* Free even if not locked */ } diff --git a/mysys/my_malloc.c b/mysys/my_malloc.c index 13d2375eb99..fc2dc98c3c5 100644 --- a/mysys/my_malloc.c +++ b/mysys/my_malloc.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2000 MySQL AB +/* Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -11,28 +11,31 @@ You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software - Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ - -#ifdef SAFEMALLOC /* We don't need SAFEMALLOC here */ -#undef SAFEMALLOC -#endif + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ #include "mysys_priv.h" #include "mysys_err.h" #include <m_string.h> - /* My memory allocator */ +/** + Allocate a sized block of memory. + @param size The size of the memory block in bytes. + @param flags Failure action modifiers (bitmasks). + + @return A pointer to the allocated memory block, or NULL on failure. +*/ void *my_malloc(size_t size, myf my_flags) { void* point; DBUG_ENTER("my_malloc"); DBUG_PRINT("my",("size: %lu my_flags: %d", (ulong) size, my_flags)); + /* Safety */ if (!size) - size=1; /* Safety */ + size=1; - point= (char *) malloc(size); + point= malloc(size); DBUG_EXECUTE_IF("simulate_out_of_memory", { free(point); @@ -52,33 +55,87 @@ void *my_malloc(size_t size, myf my_flags) exit(1); } else if (my_flags & MY_ZEROFILL) - bzero(point,size); - DBUG_PRINT("exit",("ptr: 0x%lx", (long) point)); - DBUG_RETURN((void*) point); -} /* my_malloc */ + bzero(point, size); + DBUG_PRINT("exit",("ptr: %p", point)); + DBUG_RETURN(point); +} + + +/** + @brief wrapper around realloc() + + @param oldpoint pointer to currently allocated area + @param size new size requested, must be >0 + @param my_flags flags + + @note if size==0 realloc() may return NULL; my_realloc() treats this as an + error which is not the intention of realloc() +*/ +void *my_realloc(void *oldpoint, size_t size, myf my_flags) +{ + void *point; + DBUG_ENTER("my_realloc"); + DBUG_PRINT("my",("ptr: %p size: %lu my_flags: %d", oldpoint, + (ulong) size, my_flags)); + + DBUG_ASSERT(size > 0); + if (!oldpoint && (my_flags & MY_ALLOW_ZERO_PTR)) + DBUG_RETURN(my_malloc(size, my_flags)); +#ifdef USE_HALLOC + if (!(point = malloc(size))) + { + if (my_flags & MY_FREE_ON_ERROR) + my_free(oldpoint); + if (my_flags & MY_HOLD_ON_ERROR) + DBUG_RETURN(oldpoint); + my_errno=errno; + if (my_flags & MY_FAE+MY_WME) + my_error(EE_OUTOFMEMORY, MYF(ME_BELL+ME_WAITTANG),size); + } + else + { + memcpy(point,oldpoint,size); + free(oldpoint); + } +#else + if ((point= realloc(oldpoint, size)) == NULL) + { + if (my_flags & MY_FREE_ON_ERROR) + my_free(oldpoint); + if (my_flags & MY_HOLD_ON_ERROR) + DBUG_RETURN(oldpoint); + my_errno=errno; + if (my_flags & (MY_FAE+MY_WME)) + my_error(EE_OUTOFMEMORY, MYF(ME_BELL+ME_WAITTANG), size); + } +#endif + DBUG_PRINT("exit",("ptr: %p", point)); + DBUG_RETURN(point); +} - /* Free memory allocated with my_malloc */ - /*ARGSUSED*/ +/** + Free memory allocated with my_malloc. -void my_no_flags_free(void* ptr) + @remark Relies on free being able to handle a NULL argument. + + @param ptr Pointer to the memory allocated by my_malloc. +*/ +void my_free(void *ptr) { DBUG_ENTER("my_free"); - DBUG_PRINT("my",("ptr: 0x%lx", (long) ptr)); - if (ptr) - free(ptr); + DBUG_PRINT("my",("ptr: %p", ptr)); + free(ptr); DBUG_VOID_RETURN; -} /* my_free */ - +} - /* malloc and copy */ -void* my_memdup(const void *from, size_t length, myf my_flags) +void *my_memdup(const void *from, size_t length, myf my_flags) { void *ptr; if ((ptr= my_malloc(length,my_flags)) != 0) memcpy(ptr, from, length); - return(ptr); + return ptr; } @@ -87,18 +144,19 @@ char *my_strdup(const char *from, myf my_flags) char *ptr; size_t length= strlen(from)+1; if ((ptr= (char*) my_malloc(length, my_flags))) - memcpy((uchar*) ptr, (uchar*) from,(size_t) length); - return(ptr); + memcpy(ptr, from, length); + return ptr; } char *my_strndup(const char *from, size_t length, myf my_flags) { char *ptr; - if ((ptr= (char*) my_malloc(length+1,my_flags)) != 0) + if ((ptr= (char*) my_malloc(length+1, my_flags))) { - memcpy((uchar*) ptr, (uchar*) from, length); - ptr[length]=0; + memcpy(ptr, from, length); + ptr[length]= 0; } - return((char*) ptr); + return ptr; } + diff --git a/mysys/my_once.c b/mysys/my_once.c index 727b8477365..32d07802028 100644 --- a/mysys/my_once.c +++ b/mysys/my_once.c @@ -15,10 +15,6 @@ /* Not MT-SAFE */ -#ifdef SAFEMALLOC /* We don't need SAFEMALLOC here */ -#undef SAFEMALLOC -#endif - #include "mysys_priv.h" #include "my_static.h" #include "mysys_err.h" diff --git a/mysys/my_open.c b/mysys/my_open.c index a50baf2c417..8f34ce1c6dc 100644 --- a/mysys/my_open.c +++ b/mysys/my_open.c @@ -88,7 +88,7 @@ int my_close(File fd, myf MyFlags) } if ((uint) fd < my_file_limit && my_file_info[fd].type != UNOPEN) { - my_free(my_file_info[fd].name, MYF(0)); + my_free(my_file_info[fd].name); #if defined(THREAD) && !defined(HAVE_PREAD) && !defined(_WIN32) mysql_mutex_destroy(&my_file_info[fd].mutex); #endif diff --git a/mysys/my_realloc.c b/mysys/my_realloc.c deleted file mode 100644 index a55282e03a0..00000000000 --- a/mysys/my_realloc.c +++ /dev/null @@ -1,75 +0,0 @@ -/* Copyright (C) 2000 MySQL AB - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; version 2 of the License. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ - -#ifdef SAFEMALLOC /* We don't need SAFEMALLOC here */ -#undef SAFEMALLOC -#endif - -#include "mysys_priv.h" -#include "mysys_err.h" - - /* My memory re allocator */ - -/** - @brief wrapper around realloc() - - @param oldpoint pointer to currently allocated area - @param size new size requested, must be >0 - @param my_flags flags - - @note if size==0 realloc() may return NULL; my_realloc() treats this as an - error which is not the intention of realloc() -*/ -void* my_realloc(void* oldpoint, size_t size, myf my_flags) -{ - void *point; - DBUG_ENTER("my_realloc"); - DBUG_PRINT("my",("ptr: 0x%lx size: %lu my_flags: %d", (long) oldpoint, - (ulong) size, my_flags)); - - DBUG_ASSERT(size > 0); - if (!oldpoint && (my_flags & MY_ALLOW_ZERO_PTR)) - DBUG_RETURN(my_malloc(size,my_flags)); -#ifdef USE_HALLOC - if (!(point = malloc(size))) - { - if (my_flags & MY_FREE_ON_ERROR) - my_free(oldpoint,my_flags); - if (my_flags & MY_HOLD_ON_ERROR) - DBUG_RETURN(oldpoint); - my_errno=errno; - if (my_flags & MY_FAE+MY_WME) - my_error(EE_OUTOFMEMORY, MYF(ME_BELL+ME_WAITTANG),size); - } - else - { - memcpy(point,oldpoint,size); - free(oldpoint); - } -#else - if ((point= (uchar*) realloc(oldpoint,size)) == NULL) - { - if (my_flags & MY_FREE_ON_ERROR) - my_free(oldpoint, my_flags); - if (my_flags & MY_HOLD_ON_ERROR) - DBUG_RETURN(oldpoint); - my_errno=errno; - if (my_flags & (MY_FAE+MY_WME)) - my_error(EE_OUTOFMEMORY, MYF(ME_BELL+ME_WAITTANG), size); - } -#endif - DBUG_PRINT("exit",("ptr: 0x%lx", (long) point)); - DBUG_RETURN(point); -} /* my_realloc */ diff --git a/mysys/my_windac.c b/mysys/my_windac.c index f846853f7be..0c924188623 100644 --- a/mysys/my_windac.c +++ b/mysys/my_windac.c @@ -194,8 +194,8 @@ error: FreeSid(everyone_sid); if (htoken) CloseHandle(htoken); - my_free((uchar*) sa, MYF(MY_ALLOW_ZERO_PTR)); - my_free((uchar*) dacl, MYF(MY_ALLOW_ZERO_PTR)); + my_free(sa); + my_free(dacl); *psa= 0; return 1; } @@ -215,8 +215,8 @@ void my_security_attr_free(SECURITY_ATTRIBUTES *sa) My_security_attr *attr= (My_security_attr*) (((char*)sa) + ALIGN_SIZE(sizeof(*sa))); FreeSid(attr->everyone_sid); - my_free((uchar*) attr->dacl, MYF(0)); - my_free((uchar*) sa, MYF(0)); + my_free(attr->dacl); + my_free(sa); } } diff --git a/mysys/queues.c b/mysys/queues.c index 9c85e493141..25a310c0752 100644 --- a/mysys/queues.c +++ b/mysys/queues.c @@ -194,11 +194,8 @@ int resize_queue(QUEUE *queue, uint max_elements) void delete_queue(QUEUE *queue) { DBUG_ENTER("delete_queue"); - if (queue->root) - { - my_free((uchar*) queue->root,MYF(0)); - queue->root=0; - } + my_free(queue->root); + queue->root= NULL; DBUG_VOID_RETURN; } diff --git a/mysys/safemalloc.c b/mysys/safemalloc.c deleted file mode 100644 index 6d0f7e5dd53..00000000000 --- a/mysys/safemalloc.c +++ /dev/null @@ -1,576 +0,0 @@ -/* Copyright (C) 2000-2003 MySQL AB, 2008-2009 Sun Microsystems, Inc - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; version 2 of the License. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ - -/* - * Memory sub-system, written by Bjorn Benson - Fixed to use my_sys scheme by Michael Widenius - - [This posting refers to an article entitled "oops, corrupted memory - again!" in net.lang.c. I am posting it here because it is source.] - - My tool for approaching this problem is to build another level of data - abstraction on top of malloc() and free() that implements some checking. - This does a number of things for you: - - Checks for overruns and underruns on allocated data - - Keeps track of where in the program the memory was malloc'ed - - Reports on pieces of memory that were not free'ed - - Records some statistics such as maximum memory used - - Marks newly malloc'ed and newly free'ed memory with special values - You can use this scheme to: - - Find bugs such as overrun, underrun, etc because you know where - a piece of data was malloc'ed and where it was free'ed - - Find bugs where memory was not free'ed - - Find bugs where newly malloc'ed memory is used without initializing - - Find bugs where newly free'ed memory is still used - - Determine how much memory your program really uses - - and other things - - To implement my scheme you must have a C compiler that has __LINE__ and - __FILE__ macros. If your compiler doesn't have these then (a) buy another: - compilers that do are available on UNIX 4.2bsd based systems and the PC, - and probably on other machines; or (b) change my scheme somehow. I have - recomendations on both these points if you would like them (e-mail please). - - There are 4 functions in my package: - char *NEW( uSize ) Allocate memory of uSize bytes - (equivalent to malloc()) - char *REA( pPtr, uSize) Allocate memory of uSize bytes, move data and - free pPtr. - (equivalent to realloc()) - FREE( pPtr ) Free memory allocated by NEW - (equivalent to free()) - TERMINATE(file,flag) End system, report errors and stats on file - I personally use two more functions, but have not included them here: - char *STRSAVE( sPtr ) Save a copy of the string in dynamic memory - char *RENEW( pPtr, uSize ) - (equivalent to realloc()) - -*/ - -#ifndef SAFEMALLOC -#define SAFEMALLOC /* Get protos from my_sys */ -#endif - -#include "mysys_priv.h" -#include <m_string.h> -#include "my_static.h" -#include "mysys_err.h" - -ulonglong sf_malloc_mem_limit= ~(ulonglong)0; - -#ifndef PEDANTIC_SAFEMALLOC -/* - Set to 1 after TERMINATE() if we had to fiddle with sf_malloc_count and - the linked list of blocks so that _sanity() will not fuss when it - is not supposed to -*/ -static int sf_malloc_tampered= 0; -#endif - - - /* Static functions prototypes */ - -static int check_ptr(const char *where, uchar *ptr, const char *sFile, - uint uLine); -static int _checkchunk(struct st_irem *pRec, const char *sFile, uint uLine); - -/* - Note: We only fill up the allocated block. This do not include - malloc() roundoff or the extra space required by the irem - structures. -*/ - -/* - NEW'ed memory is filled with this value so that references to it will - end up being very strange. -*/ -#define ALLOC_VAL (uchar) 0xA5 -/* - FEEE'ed memory is filled with this value so that references to it will - end up being very strange. -*/ -#define FREE_VAL (uchar) 0x8F -#define MAGICKEY 0x14235296 /* A magic value for underrun key */ - -/* - Warning: do not change the MAGICEND? values to something with the - high bit set. Various C compilers (like the 4.2bsd one) do not do - the sign extension right later on in this code and you will get - erroneous errors. -*/ - -#define MAGICEND0 0x68 /* Magic values for overrun keys */ -#define MAGICEND1 0x34 /* " */ -#define MAGICEND2 0x7A /* " */ -#define MAGICEND3 0x15 /* " */ - - -/* Allocate some memory. */ - -void *_mymalloc(size_t size, const char *filename, uint lineno, myf MyFlags) -{ - struct st_irem *irem; - uchar *data; - DBUG_ENTER("_mymalloc"); - DBUG_PRINT("enter",("Size: %lu", (ulong) size)); - - if (!sf_malloc_quick) - (void) _sanity (filename, lineno); - - if (size + sf_malloc_cur_memory > sf_malloc_mem_limit) - irem= 0; - else - { - /* Allocate the physical memory */ - irem= (struct st_irem *) malloc (ALIGN_SIZE(sizeof(struct st_irem)) + - sf_malloc_prehunc + - size + /* size requested */ - 4 + /* overrun mark */ - sf_malloc_endhunc); - DBUG_EXECUTE_IF("simulate_out_of_memory", - { - free(irem); - irem= NULL; - }); - } - /* Check if there isn't anymore memory avaiable */ - if (!irem) - { - if (MyFlags & MY_FAE) - error_handler_hook=fatal_error_handler_hook; - if (MyFlags & (MY_FAE+MY_WME)) - { - char buff[256]; - my_errno=errno; - sprintf(buff,"Out of memory at line %d, '%s'", lineno, filename); - my_message(EE_OUTOFMEMORY, buff, MYF(ME_BELL+ME_WAITTANG+ME_NOREFRESH)); - sprintf(buff,"needed %lu byte (%luk), memory in use: %lu bytes (%luk)", - (ulong) size, (ulong) (size + 1023L) / 1024L, - (ulong) sf_malloc_max_memory, - (ulong) (sf_malloc_max_memory + 1023L) / 1024L); - my_message(EE_OUTOFMEMORY, buff, MYF(ME_BELL+ME_WAITTANG+ME_NOREFRESH)); - } - DBUG_PRINT("error",("Out of memory, in use: %ld at line %d, '%s'", - (long)sf_malloc_max_memory,lineno, filename)); - DBUG_EXECUTE_IF("simulate_out_of_memory", - DBUG_SET("-d,simulate_out_of_memory");); - if (MyFlags & MY_FAE) - exit(1); - DBUG_RETURN ((void*) 0); - } - - /* Fill up the structure */ - data= (((uchar*) irem) + ALIGN_SIZE(sizeof(struct st_irem)) + - sf_malloc_prehunc); - *((uint32*) (data-sizeof(uint32)))= MAGICKEY; - data[size + 0]= MAGICEND0; - data[size + 1]= MAGICEND1; - data[size + 2]= MAGICEND2; - data[size + 3]= MAGICEND3; - irem->filename= (char *) filename; - irem->linenum= lineno; - irem->datasize= size; - irem->prev= NULL; - - /* Add this remember structure to the linked list */ - mysql_mutex_lock(&THR_LOCK_malloc); - if ((irem->next= sf_malloc_root)) - sf_malloc_root->prev= irem; - sf_malloc_root= irem; - - /* Keep the statistics */ - sf_malloc_cur_memory+= size; - if (sf_malloc_cur_memory > sf_malloc_max_memory) - sf_malloc_max_memory= sf_malloc_cur_memory; - sf_malloc_count++; - mysql_mutex_unlock(&THR_LOCK_malloc); - - MEM_CHECK_ADDRESSABLE(data, size); - /* Set the memory to the aribtrary wierd value */ - if ((MyFlags & MY_ZEROFILL) || !sf_malloc_quick) - bfill(data, size, (char) (MyFlags & MY_ZEROFILL ? 0 : ALLOC_VAL)); - if (!(MyFlags & MY_ZEROFILL)) - MEM_UNDEFINED(data, size); - /* Return a pointer to the real data */ - DBUG_PRINT("exit",("ptr: %p", data)); - if (sf_min_adress > data) - sf_min_adress= data; - if (sf_max_adress < data) - sf_max_adress= data; - DBUG_RETURN((void*) data); -} - - -/* - Allocate some new memory and move old memoryblock there. - Free then old memoryblock -*/ - -void *_myrealloc(register void *ptr, register size_t size, - const char *filename, uint lineno, myf MyFlags) -{ - struct st_irem *irem; - char *data; - DBUG_ENTER("_myrealloc"); - - if (!ptr && (MyFlags & MY_ALLOW_ZERO_PTR)) - DBUG_RETURN(_mymalloc(size, filename, lineno, MyFlags)); - - if (!sf_malloc_quick) - (void) _sanity (filename, lineno); - - if (check_ptr("Reallocating", (uchar*) ptr, filename, lineno)) - DBUG_RETURN((uchar*) NULL); - - irem= (struct st_irem *) (((char*) ptr) - ALIGN_SIZE(sizeof(struct st_irem))- - sf_malloc_prehunc); - if (*((uint32*) (((char*) ptr)- sizeof(uint32))) != MAGICKEY) - { - fprintf(stderr, "Error: Reallocating unallocated data at line %d, '%s'\n", - lineno, filename); - DBUG_PRINT("safe",("Reallocating unallocated data at line %d, '%s'", - lineno, filename)); - (void) fflush(stderr); - DBUG_RETURN((uchar*) NULL); - } - - if ((data= _mymalloc(size,filename,lineno,MyFlags))) /* Allocate new area */ - { - size=min(size, irem->datasize); /* Move as much as possibly */ - memcpy((uchar*) data, ptr, (size_t) size); /* Copy old data */ - _myfree(ptr, filename, lineno, 0); /* Free not needed area */ - } - else - { - if (MyFlags & MY_HOLD_ON_ERROR) - DBUG_RETURN(ptr); - if (MyFlags & MY_FREE_ON_ERROR) - _myfree(ptr, filename, lineno, 0); - } - DBUG_RETURN(data); -} /* _myrealloc */ - - -/* Deallocate some memory. */ - -void _myfree(void *ptr, const char *filename, uint lineno, myf myflags) -{ - struct st_irem *irem; - DBUG_ENTER("_myfree"); - DBUG_PRINT("enter",("ptr: %p", ptr)); - - if (!sf_malloc_quick) - (void) _sanity (filename, lineno); - - if ((!ptr && (myflags & MY_ALLOW_ZERO_PTR)) || - check_ptr("Freeing",(uchar*) ptr,filename,lineno)) - DBUG_VOID_RETURN; - - /* Calculate the address of the remember structure */ - irem= (struct st_irem *) ((char*) ptr- ALIGN_SIZE(sizeof(struct st_irem))- - sf_malloc_prehunc); - - /* - Check to make sure that we have a real remember structure. - Note: this test could fail for four reasons: - (1) The memory was already free'ed - (2) The memory was never new'ed - (3) There was an underrun - (4) A stray pointer hit this location - */ - - if (*((uint32*) ((char*) ptr- sizeof(uint32))) != MAGICKEY) - { - fprintf(stderr, "Error: Freeing unallocated data at line %d, '%s'\n", - lineno, filename); - DBUG_PRINT("safe",("Unallocated data at line %d, '%s'",lineno,filename)); - (void) fflush(stderr); - DBUG_VOID_RETURN; - } - - /* Remove this structure from the linked list */ - mysql_mutex_lock(&THR_LOCK_malloc); - if (irem->prev) - irem->prev->next= irem->next; - else - sf_malloc_root= irem->next; - - if (irem->next) - irem->next->prev= irem->prev; - /* Handle the statistics */ - sf_malloc_cur_memory-= irem->datasize; - sf_malloc_count--; - mysql_mutex_unlock(&THR_LOCK_malloc); - -#ifndef HAVE_purify - /* Mark this data as free'ed */ - if (!sf_malloc_quick) - bfill(ptr, irem->datasize, (pchar) FREE_VAL); -#endif - MEM_NOACCESS(ptr, irem->datasize); - *((uint32*) ((char*) ptr- sizeof(uint32)))= ~MAGICKEY; - MEM_NOACCESS((char*) ptr - sizeof(uint32), sizeof(uint32)); - /* Actually free the memory */ - free((char*) irem); - DBUG_VOID_RETURN; -} - - /* Check if we have a wrong pointer */ - -static int check_ptr(const char *where, uchar *ptr, const char *filename, - uint lineno) -{ - if (!ptr) - { - fprintf(stderr, "Error: %s NULL pointer at line %d, '%s'\n", - where,lineno, filename); - DBUG_PRINT("safe",("Null pointer at line %d '%s'", lineno, filename)); - (void) fflush(stderr); - return 1; - } -#ifndef _MSC_VER - if ((long) ptr & (ALIGN_SIZE(1)-1)) - { - fprintf(stderr, "Error: %s wrong aligned pointer at line %d, '%s'\n", - where,lineno, filename); - DBUG_PRINT("safe",("Wrong aligned pointer at line %d, '%s'", - lineno,filename)); - (void) fflush(stderr); - return 1; - } -#endif - if (ptr < sf_min_adress || ptr > sf_max_adress) - { - fprintf(stderr, "Error: %s pointer out of range at line %d, '%s'\n", - where,lineno, filename); - DBUG_PRINT("safe",("Pointer out of range at line %d '%s'", - lineno,filename)); - (void) fflush(stderr); - return 1; - } - return 0; -} - - -/* - Report on all the memory pieces that have not been free'ed - - SYNOPSIS - TERMINATE() - file Write output to this file - flag If <> 0, also write statistics - */ - -void TERMINATE(FILE *file, uint flag) -{ - struct st_irem *irem; - DBUG_ENTER("TERMINATE"); - mysql_mutex_lock(&THR_LOCK_malloc); - - /* - Report the difference between number of calls to - NEW and the number of calls to FREE. >0 means more - NEWs than FREEs. <0, etc. - */ - - if (sf_malloc_count) - { - if (file) - { - fprintf(file, "Warning: Not freed memory segments: %u\n", sf_malloc_count); - (void) fflush(file); - } - DBUG_PRINT("safe",("sf_malloc_count: %u", sf_malloc_count)); - } - - /* - Report on all the memory that was allocated with NEW - but not free'ed with FREE. - */ - - if ((irem= sf_malloc_root)) - { - if (file) - { - fprintf(file, "Warning: Memory that was not free'ed (%lu bytes):\n", - (ulong) sf_malloc_cur_memory); - (void) fflush(file); - } - DBUG_PRINT("safe",("Memory that was not free'ed (%lu bytes):", - (ulong) sf_malloc_cur_memory)); - while (irem) - { - char *data= (((char*) irem) + ALIGN_SIZE(sizeof(struct st_irem)) + - sf_malloc_prehunc); - if (file) - { - fprintf(file, - "\t%6lu bytes at %p, allocated at line %4u in '%s'", - (ulong) irem->datasize, data, irem->linenum, irem->filename); - fprintf(file, "\n"); - (void) fflush(file); - } - DBUG_PRINT("safe", - ("%6lu bytes at %p, allocated at line %4d in '%s'", - (ulong) irem->datasize, - data, irem->linenum, irem->filename)); - irem= irem->next; - } - } - /* Report the memory usage statistics */ - if (file && flag) - { - fprintf(file, "Maximum memory usage: %lu bytes (%luk)\n", - (ulong) sf_malloc_max_memory, - (ulong) (sf_malloc_max_memory + 1023L) / 1024L); - (void) fflush(file); - } - DBUG_PRINT("safe",("Maximum memory usage: %lu bytes (%luk)", - (ulong) sf_malloc_max_memory, - (ulong) (sf_malloc_max_memory + 1023L) /1024L)); - mysql_mutex_unlock(&THR_LOCK_malloc); - DBUG_VOID_RETURN; -} - - -/* - Report where a piece of memory was allocated - - This is usefull to call from withing a debugger -*/ - - -void sf_malloc_report_allocated(void *memory) -{ - struct st_irem *irem; - for (irem= sf_malloc_root ; irem ; irem=irem->next) - { - char *data= (((char*) irem) + ALIGN_SIZE(sizeof(struct st_irem)) + - sf_malloc_prehunc); - if (data <= (char*) memory && (char*) memory <= data + irem->datasize) - { - printf("%lu bytes at %p, allocated at line %u in '%s'\n", - (ulong) irem->datasize, data, irem->linenum, irem->filename); - break; - } - } -} - - /* Returns 0 if chunk is ok */ - -static int _checkchunk(register struct st_irem *irem, const char *filename, - uint lineno) -{ - int flag=0; - char *magicp, *data; - - data= (((char*) irem) + ALIGN_SIZE(sizeof(struct st_irem)) + - sf_malloc_prehunc); - /* Check for a possible underrun */ - if (*((uint32*) (data- sizeof(uint32))) != MAGICKEY) - { - fprintf(stderr, "Error: Memory allocated at %s:%d was underrun,", - irem->filename, irem->linenum); - fprintf(stderr, " discovered at %s:%d\n", filename, lineno); - (void) fflush(stderr); - DBUG_PRINT("safe",("Underrun at %p, allocated at %s:%d", - data, irem->filename, irem->linenum)); - flag=1; - } - - /* Check for a possible overrun */ - magicp= data + irem->datasize; - if (*magicp++ != MAGICEND0 || - *magicp++ != MAGICEND1 || - *magicp++ != MAGICEND2 || - *magicp++ != MAGICEND3) - { - fprintf(stderr, "Error: Memory allocated at %s:%d was overrun,", - irem->filename, irem->linenum); - fprintf(stderr, " discovered at '%s:%d'\n", filename, lineno); - (void) fflush(stderr); - DBUG_PRINT("safe",("Overrun at %p, allocated at %s:%d", - data, irem->filename, irem->linenum)); - flag=1; - } - return(flag); -} - - - /* Returns how many wrong chunks */ - -int _sanity(const char *filename, uint lineno) -{ - reg1 struct st_irem *irem; - reg2 int flag=0; - uint count=0; - - mysql_mutex_lock(&THR_LOCK_malloc); -#ifndef PEDANTIC_SAFEMALLOC - if (sf_malloc_tampered && (int) sf_malloc_count < 0) - sf_malloc_count=0; -#endif - count=sf_malloc_count; - for (irem= sf_malloc_root; irem != NULL && count-- ; irem= irem->next) - flag+= _checkchunk (irem, filename, lineno); - mysql_mutex_unlock(&THR_LOCK_malloc); - if (count || irem) - { - const char *format="Error: Safemalloc link list destroyed, discovered at '%s:%d'"; - fprintf(stderr, format, filename, lineno); fputc('\n',stderr); - fprintf(stderr, "root=%p,count=%d,irem=%p\n", sf_malloc_root,count,irem); - (void) fflush(stderr); - DBUG_PRINT("safe",(format, filename, lineno)); - flag=1; - } - return flag; -} /* _sanity */ - - - /* malloc and copy */ - -void *_my_memdup(const void *from, size_t length, const char *filename, - uint lineno, myf MyFlags) -{ - void *ptr; - if ((ptr= _mymalloc(length,filename,lineno,MyFlags)) != 0) - memcpy(ptr, from, length); - return(ptr); -} /*_my_memdup */ - - -char *_my_strdup(const char *from, const char *filename, uint lineno, - myf MyFlags) -{ - char *ptr; - size_t length= strlen(from)+1; - if ((ptr= (char*) _mymalloc(length,filename,lineno,MyFlags)) != 0) - memcpy((uchar*) ptr, (uchar*) from, (size_t) length); - return(ptr); -} /* _my_strdup */ - - -char *_my_strndup(const char *from, size_t length, - const char *filename, uint lineno, - myf MyFlags) -{ - char *ptr; - if ((ptr= (char*) _mymalloc(length+1,filename,lineno,MyFlags)) != 0) - { - memcpy((uchar*) ptr, (uchar*) from, (size_t) length); - ptr[length]=0; - } - return(ptr); -} diff --git a/mysys/string.c b/mysys/string.c index 10a72b8a295..b1eded0664c 100644 --- a/mysys/string.c +++ b/mysys/string.c @@ -177,9 +177,6 @@ my_bool dynstr_append_os_quoted(DYNAMIC_STRING *str, const char *append, ...) void dynstr_free(DYNAMIC_STRING *str) { - if (str->str) - { - my_free(str->str,MYF(MY_WME)); - str->str=0; - } + my_free(str->str); + str->str= NULL; } diff --git a/mysys/test_charset.c b/mysys/test_charset.c index d867b49304e..5b399071d11 100644 --- a/mysys/test_charset.c +++ b/mysys/test_charset.c @@ -80,11 +80,11 @@ int main(int argc, char **argv) { #ifdef NOT_USED_ANYMORE cs_list = list_charsets(MYF(MY_CS_COMPILED | MY_CS_CONFIG)); printf("LIST OF CHARSETS (compiled + *.conf):\n%s\n", cs_list); - my_free(cs_list,MYF(0)); + my_free(cs_list); cs_list = list_charsets(MYF(MY_CS_INDEX | MY_CS_LOADED)); printf("LIST OF CHARSETS (index + loaded):\n%s\n", cs_list); - my_free(cs_list,MYF(0)); + my_free(cs_list); #endif return 0; diff --git a/mysys/testhash.c b/mysys/testhash.c index 2add2ebd2d7..376303f29be 100644 --- a/mysys/testhash.c +++ b/mysys/testhash.c @@ -286,5 +286,5 @@ static int rnd(int max_value) void free_record(void *record) { - my_free(record,MYF(0)); + my_free(record); } diff --git a/mysys/thr_alarm.c b/mysys/thr_alarm.c index 8ee89cfb55d..555ac1cf990 100644 --- a/mysys/thr_alarm.c +++ b/mysys/thr_alarm.c @@ -262,7 +262,7 @@ void thr_end_alarm(thr_alarm_t *alarmed) { queue_remove(&alarm_queue,i),MYF(0); if (alarm_data->malloced) - my_free((uchar*) alarm_data,MYF(0)); + my_free(alarm_data); found++; #ifdef DBUG_OFF break; diff --git a/mysys/tree.c b/mysys/tree.c index ef33f75b7c6..8ea7102ed4c 100644 --- a/mysys/tree.c +++ b/mysys/tree.c @@ -183,7 +183,7 @@ static void delete_tree_element(TREE *tree, TREE_ELEMENT *element) (*tree->free)(ELEMENT_KEY(tree,element), free_free, tree->custom_arg); delete_tree_element(tree,element->right); if (tree->with_delete) - my_free((char*) element,MYF(0)); + my_free(element); } } @@ -326,7 +326,7 @@ int tree_delete(TREE *tree, void *key, uint key_size, void *custom_arg) if (tree->free) (*tree->free)(ELEMENT_KEY(tree,element), free_free, tree->custom_arg); tree->allocated-= sizeof(TREE_ELEMENT) + tree->size_of_element + key_size; - my_free((uchar*) element,MYF(0)); + my_free(element); tree->elements_in_tree--; return 0; } diff --git a/mysys/trie.c b/mysys/trie.c index 5738b9b866b..b2e93fcceac 100644 --- a/mysys/trie.c +++ b/mysys/trie.c @@ -211,7 +211,7 @@ my_bool ac_trie_prepare (TRIE *trie) fail= fail->fail; } } - my_free((uchar*)tmp_nodes, MYF(0)); + my_free(tmp_nodes); DBUG_RETURN(FALSE); } |