From ce4973d61619837caa946974453746e8eda29d36 Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 16 Jun 2004 23:42:58 +0300 Subject: 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 --- innobase/ut/ut0mem.c | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) (limited to 'innobase/ut') 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 @@ -165,6 +165,88 @@ ut_free( os_fast_mutex_unlock(&ut_list_mutex); } +/************************************************************************** +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. */ -- cgit v1.2.1 From c0edb2e0ceed434c2209e5e7083fb512f25f5433 Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 16 Jun 2004 23:46:47 +0300 Subject: ut0mem.c: Remove printf's that were accidentally pushed in the last push innobase/ut/ut0mem.c: Remove printf's that were accidentally pushed in the last push --- innobase/ut/ut0mem.c | 7 ------- 1 file changed, 7 deletions(-) (limited to 'innobase/ut') diff --git a/innobase/ut/ut0mem.c b/innobase/ut/ut0mem.c index a0b41d08771..2cab36a9580 100644 --- a/innobase/ut/ut0mem.c +++ b/innobase/ut/ut0mem.c @@ -202,10 +202,7 @@ ut_realloc( 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)); } @@ -222,8 +219,6 @@ ut_realloc( 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 { @@ -240,8 +235,6 @@ ut_realloc( /* 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); -- cgit v1.2.1