diff options
author | unknown <heikki@hundin.mysql.fi> | 2004-06-16 23:42:58 +0300 |
---|---|---|
committer | unknown <heikki@hundin.mysql.fi> | 2004-06-16 23:42:58 +0300 |
commit | ce4973d61619837caa946974453746e8eda29d36 (patch) | |
tree | cdb13121d261238aa31a38ad58063796455276e2 /innobase/ut | |
parent | fce76a7644e917acbf835f728c8b452a6cf772da (diff) | |
download | mariadb-git-ce4973d61619837caa946974453746e8eda29d36.tar.gz |
ut0mem.h, ut0mem.c:
Implement ut_realloc
lexyy.c, pars0lex.l:
Start using ut_malloc, ut_free, ut_realloc in pars0lex.l and lexyy.c; mem_realloc was broken; eliminate possible memcpy(NULL, ..., 0) from string_append()
innobase/pars/pars0lex.l:
Start using ut_malloc, ut_free, ut_realloc in pars0lex.l and lexyy.c; mem_realloc was broken; eliminate possible memcpy(NULL, ..., 0) from string_append()
innobase/pars/lexyy.c:
Start using ut_malloc, ut_free, ut_realloc in pars0lex.l and lexyy.c; mem_realloc was broken; eliminate possible memcpy(NULL, ..., 0) from string_append()
innobase/ut/ut0mem.c:
Implement ut_realloc
innobase/include/ut0mem.h:
Implement ut_realloc
Diffstat (limited to 'innobase/ut')
-rw-r--r-- | innobase/ut/ut0mem.c | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/innobase/ut/ut0mem.c b/innobase/ut/ut0mem.c index 13846630818..a0b41d08771 100644 --- a/innobase/ut/ut0mem.c +++ b/innobase/ut/ut0mem.c @@ -166,6 +166,88 @@ ut_free( } /************************************************************************** +Implements realloc. This is needed by /pars/lexyy.c. Otherwise, you should not +use this function because the allocation functions in mem0mem.h are the +recommended ones in InnoDB. + +man realloc in Linux, 2004: + + realloc() changes the size of the memory block pointed to + by ptr to size bytes. The contents will be unchanged to + the minimum of the old and new sizes; newly allocated mem + ory will be uninitialized. If ptr is NULL, the call is + equivalent to malloc(size); if size is equal to zero, the + call is equivalent to free(ptr). Unless ptr is NULL, it + must have been returned by an earlier call to malloc(), + calloc() or realloc(). + +RETURN VALUE + realloc() returns a pointer to the newly allocated memory, + which is suitably aligned for any kind of variable and may + be different from ptr, or NULL if the request fails. If + size was equal to 0, either NULL or a pointer suitable to + be passed to free() is returned. If realloc() fails the + original block is left untouched - it is not freed or + moved. */ + +void* +ut_realloc( +/*=======*/ + /* out, own: pointer to new mem block or NULL */ + void* ptr, /* in: pointer to old block or NULL */ + ulint size) /* in: desired size */ +{ + ut_mem_block_t* block; + ulint old_size; + ulint min_size; + void* new_ptr; + + printf("Calling realloc with size %lu\n", size); + + if (ptr == NULL) { + printf("ptr was NULL, calling malloc\n"); + + return(ut_malloc(size)); + } + + if (size == 0) { + ut_free(ptr); + + return(NULL); + } + + block = (ut_mem_block_t*)((byte*)ptr - sizeof(ut_mem_block_t)); + + ut_a(block->magic_n == UT_MEM_MAGIC_N); + + old_size = block->size - sizeof(ut_mem_block_t); + + printf("Old size was %lu\n", old_size); + + if (size < old_size) { + min_size = size; + } else { + min_size = old_size; + } + + new_ptr = ut_malloc(size); + + if (new_ptr == NULL) { + + return(NULL); + } + + /* Copy the old data from ptr */ + ut_memcpy(new_ptr, ptr, min_size); + + printf("Copying %lu bytes to new_ptr\n", min_size); + + ut_free(ptr); + + return(new_ptr); +} + +/************************************************************************** Frees in shutdown all allocated memory not freed yet. */ void |