summaryrefslogtreecommitdiff
path: root/mysys/safemalloc.c
diff options
context:
space:
mode:
Diffstat (limited to 'mysys/safemalloc.c')
-rw-r--r--mysys/safemalloc.c147
1 files changed, 87 insertions, 60 deletions
diff --git a/mysys/safemalloc.c b/mysys/safemalloc.c
index 8c611a8556b..36d07b475e9 100644
--- a/mysys/safemalloc.c
+++ b/mysys/safemalloc.c
@@ -51,7 +51,7 @@
(equivalent to realloc())
FREE( pPtr ) Free memory allocated by NEW
(equivalent to free())
- TERMINATE(file) End system, report errors and stats on file
+ 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 )
@@ -82,7 +82,7 @@ static int sf_malloc_tampered= 0;
/* Static functions prototypes */
-static int check_ptr(const char *where, byte *ptr, const char *sFile,
+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);
@@ -119,10 +119,10 @@ static int _checkchunk(struct st_irem *pRec, const char *sFile, uint uLine);
/* Allocate some memory. */
-gptr _mymalloc(size_t size, const char *filename, uint lineno, myf MyFlags)
+void *_mymalloc(size_t size, const char *filename, uint lineno, myf MyFlags)
{
struct st_irem *irem;
- char *data;
+ uchar *data;
DBUG_ENTER("_mymalloc");
DBUG_PRINT("enter",("Size: %lu", (ulong) size));
@@ -147,31 +147,32 @@ gptr _mymalloc(size_t size, const char *filename, uint lineno, myf MyFlags)
error_handler_hook=fatal_error_handler_hook;
if (MyFlags & (MY_FAE+MY_WME))
{
- char buff[SC_MAXWIDTH];
+ 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 %u byte (%ldk), memory in use: %ld bytes (%ldk)",
- (uint) size, (uint) (size + 1023L) / 1024L,
- sf_malloc_max_memory, (sf_malloc_max_memory + 1023L) / 1024L);
+ 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'",
sf_malloc_max_memory,lineno, filename));
if (MyFlags & MY_FAE)
exit(1);
- DBUG_RETURN ((gptr) 0);
+ DBUG_RETURN ((void*) 0);
}
/* Fill up the structure */
- data= (((char*) irem) + ALIGN_SIZE(sizeof(struct st_irem)) +
+ 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= (my_string) filename;
+ irem->filename= (char *) filename;
irem->linenum= lineno;
irem->datasize= (uint32) size;
irem->prev= NULL;
@@ -193,12 +194,12 @@ gptr _mymalloc(size_t size, const char *filename, uint lineno, myf MyFlags)
if ((MyFlags & MY_ZEROFILL) || !sf_malloc_quick)
bfill(data, size, (char) (MyFlags & MY_ZEROFILL ? 0 : ALLOC_VAL));
/* Return a pointer to the real data */
- DBUG_PRINT("exit",("ptr: 0x%lx", (long) 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 ((gptr) data);
+ DBUG_RETURN((void*) data);
}
@@ -207,8 +208,8 @@ gptr _mymalloc(size_t size, const char *filename, uint lineno, myf MyFlags)
Free then old memoryblock
*/
-gptr _myrealloc(register gptr ptr, register size_t size,
- const char *filename, uint lineno, myf MyFlags)
+void *_myrealloc(register void *ptr, register size_t size,
+ const char *filename, uint lineno, myf MyFlags)
{
struct st_irem *irem;
char *data;
@@ -220,8 +221,8 @@ gptr _myrealloc(register gptr ptr, register size_t size,
if (!sf_malloc_quick)
(void) _sanity (filename, lineno);
- if (check_ptr("Reallocating", (byte*) ptr, filename, lineno))
- DBUG_RETURN((gptr) NULL);
+ 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);
@@ -232,13 +233,13 @@ gptr _myrealloc(register gptr ptr, register size_t size,
DBUG_PRINT("safe",("Reallocating unallocated data at line %d, '%s'",
lineno, filename));
(void) fflush(stderr);
- DBUG_RETURN((gptr) NULL);
+ 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((byte*) data, ptr, (size_t) size); /* Copy old data */
+ memcpy((uchar*) data, ptr, (size_t) size); /* Copy old data */
_myfree(ptr, filename, lineno, 0); /* Free not needed area */
}
else
@@ -254,17 +255,17 @@ gptr _myrealloc(register gptr ptr, register size_t size,
/* Deallocate some memory. */
-void _myfree(gptr ptr, const char *filename, uint lineno, myf myflags)
+void _myfree(void *ptr, const char *filename, uint lineno, myf myflags)
{
struct st_irem *irem;
DBUG_ENTER("_myfree");
- DBUG_PRINT("enter",("ptr: 0x%lx", (long) ptr));
+ DBUG_PRINT("enter",("ptr: %p", ptr));
if (!sf_malloc_quick)
(void) _sanity (filename, lineno);
if ((!ptr && (myflags & MY_ALLOW_ZERO_PTR)) ||
- check_ptr("Freeing",(byte*) ptr,filename,lineno))
+ check_ptr("Freeing",(uchar*) ptr,filename,lineno))
DBUG_VOID_RETURN;
/* Calculate the address of the remember structure */
@@ -316,7 +317,7 @@ void _myfree(gptr ptr, const char *filename, uint lineno, myf myflags)
/* Check if we have a wrong pointer */
-static int check_ptr(const char *where, byte *ptr, const char *filename,
+static int check_ptr(const char *where, uchar *ptr, const char *filename,
uint lineno)
{
if (!ptr)
@@ -352,12 +353,15 @@ static int check_ptr(const char *where, byte *ptr, const char *filename,
/*
- TERMINATE(FILE *file)
- Report on all the memory pieces that have not been
- free'ed as well as the statistics.
+ 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)
+void TERMINATE(FILE *file, uint flag)
{
struct st_irem *irem;
DBUG_ENTER("TERMINATE");
@@ -388,12 +392,12 @@ void TERMINATE(FILE *file)
{
if (file)
{
- fprintf(file, "Warning: Memory that was not free'ed (%ld bytes):\n",
- sf_malloc_cur_memory);
+ 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 (%ld bytes):",
- sf_malloc_cur_memory));
+ 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)) +
@@ -401,32 +405,57 @@ void TERMINATE(FILE *file)
if (file)
{
fprintf(file,
- "\t%6u bytes at 0x%09lx, allocated at line %4u in '%s'",
- irem->datasize, (long) data, irem->linenum, irem->filename);
+ "\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",
- ("%6u bytes at 0x%09lx, allocated at line %4d in '%s'",
- irem->datasize, (long) data, irem->linenum, irem->filename));
+ ("%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)
+ if (file && flag)
{
- fprintf(file, "Maximum memory usage: %ld bytes (%ldk)\n",
- sf_malloc_max_memory, (sf_malloc_max_memory + 1023L) / 1024L);
+ 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: %ld bytes (%ldk)",
- sf_malloc_max_memory, (sf_malloc_max_memory + 1023L) /
- 1024L));
+ DBUG_PRINT("safe",("Maximum memory usage: %lu bytes (%luk)",
+ (ulong) sf_malloc_max_memory,
+ (ulong) (sf_malloc_max_memory + 1023L) /1024L));
pthread_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,
@@ -444,8 +473,8 @@ static int _checkchunk(register struct st_irem *irem, const char *filename,
irem->filename, irem->linenum);
fprintf(stderr, " discovered at %s:%d\n", filename, lineno);
(void) fflush(stderr);
- DBUG_PRINT("safe",("Underrun at 0x%lx, allocated at %s:%d",
- (long) data, irem->filename, irem->linenum));
+ DBUG_PRINT("safe",("Underrun at %p, allocated at %s:%d",
+ data, irem->filename, irem->linenum));
flag=1;
}
@@ -460,10 +489,8 @@ static int _checkchunk(register struct st_irem *irem, const char *filename,
irem->filename, irem->linenum);
fprintf(stderr, " discovered at '%s:%d'\n", filename, lineno);
(void) fflush(stderr);
- DBUG_PRINT("safe",("Overrun at 0x%lx, allocated at %s:%d",
- (long) data,
- irem->filename,
- irem->linenum));
+ DBUG_PRINT("safe",("Overrun at %p, allocated at %s:%d",
+ data, irem->filename, irem->linenum));
flag=1;
}
return(flag);
@@ -502,12 +529,12 @@ int _sanity(const char *filename, uint lineno)
/* malloc and copy */
-gptr _my_memdup(const byte *from, size_t length, const char *filename,
+void *_my_memdup(const void *from, size_t length, const char *filename,
uint lineno, myf MyFlags)
{
- gptr ptr;
- if ((ptr=_mymalloc(length,filename,lineno,MyFlags)) != 0)
- memcpy((byte*) ptr, (byte*) from,(size_t) length);
+ void *ptr;
+ if ((ptr= _mymalloc(length,filename,lineno,MyFlags)) != 0)
+ memcpy(ptr, from, length);
return(ptr);
} /*_my_memdup */
@@ -515,23 +542,23 @@ gptr _my_memdup(const byte *from, size_t length, const char *filename,
char *_my_strdup(const char *from, const char *filename, uint lineno,
myf MyFlags)
{
- gptr ptr;
+ char *ptr;
size_t length= strlen(from)+1;
- if ((ptr=_mymalloc(length,filename,lineno,MyFlags)) != 0)
- memcpy((byte*) ptr, (byte*) from,(size_t) length);
- return((char*) ptr);
+ if ((ptr= (char*) _mymalloc(length,filename,lineno,MyFlags)) != 0)
+ memcpy((uchar*) ptr, (uchar*) from, (size_t) length);
+ return(ptr);
} /* _my_strdup */
-char *_my_strdup_with_length(const char *from, size_t length,
+char *_my_strndup(const char *from, size_t length,
const char *filename, uint lineno,
myf MyFlags)
{
- gptr ptr;
- if ((ptr=_mymalloc(length+1,filename,lineno,MyFlags)) != 0)
+ char *ptr;
+ if ((ptr= (char*) _mymalloc(length+1,filename,lineno,MyFlags)) != 0)
{
- memcpy((byte*) ptr, (byte*) from,(size_t) length);
+ memcpy((uchar*) ptr, (uchar*) from, (size_t) length);
ptr[length]=0;
}
- return((char *) ptr);
+ return(ptr);
}