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; });
void _myfree (gptr pPtr, const char *sFile, uint uLine, myf myflags) { struct remember *pRec; DBUG_ENTER("_myfree"); DBUG_PRINT("enter",("ptr: %lx",pPtr)); if (!sf_malloc_quick) (void) _sanity (sFile, uLine); if ((!pPtr && (myflags & MY_ALLOW_ZERO_PTR)) || check_ptr("Freeing",(byte*) pPtr,sFile,uLine)) DBUG_VOID_RETURN; /* Calculate the address of the remember structure */ pRec = (struct remember *) ((byte*) pPtr-sizeof(struct 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 (*((long*) ((char*) &pRec -> lSpecialValue+sf_malloc_prehunc)) != MAGICKEY) { fprintf (stderr, "Freeing unallocated data at line %d, '%s'\n", uLine, sFile); DBUG_PRINT("safe",("Unallocated data at line %d, '%s'",uLine,sFile)); (void) fflush(stderr); DBUG_VOID_RETURN; } /* Remove this structure from the linked list */ pthread_mutex_lock(&THR_LOCK_malloc); if (pRec -> pPrev) { pRec -> pPrev -> pNext = pRec -> pNext; } else { pRememberRoot = pRec -> pNext; } if (pRec -> pNext) { pRec -> pNext -> pPrev = pRec -> pPrev; } /* Handle the statistics */ lCurMemory -= pRec -> uDataSize; cNewCount--; pthread_mutex_unlock(&THR_LOCK_malloc); #ifndef HAVE_purify /* Mark this data as free'ed */ bfill(&pRec->aData[sf_malloc_prehunc],pRec->uDataSize,(pchar) FREE_VAL); #endif *((long*) ((char*) &pRec -> lSpecialValue+sf_malloc_prehunc)) = ~MAGICKEY; /* Actually free the memory */ free ((my_string ) pRec); DBUG_VOID_RETURN; }
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 */ pthread_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--; pthread_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 *((uint32*) ((char*) ptr- sizeof(uint32)))= ~MAGICKEY; /* Actually free the memory */ free((char*) irem); DBUG_VOID_RETURN; }
gptr _myrealloc (register gptr pPtr, register uint uSize, const char *sFile, uint uLine, myf MyFlags) { struct remember *pRec; gptr ptr; DBUG_ENTER("_myrealloc"); if (!pPtr && (MyFlags & MY_ALLOW_ZERO_PTR)) DBUG_RETURN(_mymalloc(uSize,sFile,uLine,MyFlags)); if (!sf_malloc_quick) (void) _sanity (sFile, uLine); if (check_ptr("Reallocating",(byte*) pPtr,sFile,uLine)) DBUG_RETURN((gptr) NULL); pRec = (struct remember *) ((char*) pPtr - sizeof (struct irem)- sf_malloc_prehunc); if (*((long*) ((char*) &pRec -> lSpecialValue+sf_malloc_prehunc)) != MAGICKEY) { fprintf (stderr, "Reallocating unallocated data at line %d, '%s'\n", uLine, sFile); DBUG_PRINT("safe",("Reallocating unallocated data at line %d, '%s'", uLine, sFile)); (void) fflush(stderr); DBUG_RETURN((gptr) NULL); } if ((ptr=_mymalloc(uSize,sFile,uLine,MyFlags))) /* Allocate new area */ { uSize=min(uSize,pRec-> uDataSize); /* Move as much as possibly */ memcpy((byte*) ptr,pPtr,(size_t) uSize); /* Copy old data */ _myfree(pPtr,sFile,uLine,0); /* Free not needed area */ } else { if (MyFlags & MY_HOLD_ON_ERROR) DBUG_RETURN(pPtr); if (MyFlags & MY_FREE_ON_ERROR) _myfree(pPtr,sFile,uLine,0); } DBUG_RETURN(ptr); } /* _myrealloc */
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 */
gptr _mymalloc (uint uSize, const char *sFile, uint uLine, myf MyFlags) { struct remember *pTmp; DBUG_ENTER("_mymalloc"); DBUG_PRINT("enter",("Size: %u",uSize)); if (!sf_malloc_quick) (void) _sanity (sFile, uLine); if(uSize + lCurMemory > safemalloc_mem_limit) pTmp = 0; else /* Allocate the physical memory */ pTmp = (struct remember *) malloc ( sizeof (struct irem) /* remember data */ + sf_malloc_prehunc + uSize /* size requested */ + 4 /* overrun mark */ + sf_malloc_endhunc ); /* Check if there isn't anymore memory avaiable */ if (pTmp == NULL) { if (MyFlags & MY_FAE) error_handler_hook=fatal_error_handler_hook; if (MyFlags & (MY_FAE+MY_WME)) { char buff[SC_MAXWIDTH]; my_errno=errno; sprintf(buff,"Out of memory at line %d, '%s'", uLine, sFile); my_message(EE_OUTOFMEMORY,buff,MYF(ME_BELL+ME_WAITTANG)); sprintf(buff,"needed %d byte (%ldk), memory in use: %ld bytes (%ldk)", uSize, (uSize + 1023L) / 1024L, lMaxMemory, (lMaxMemory + 1023L) / 1024L); my_message(EE_OUTOFMEMORY,buff,MYF(ME_BELL+ME_WAITTANG)); } DBUG_PRINT("error",("Out of memory, in use: %ld at line %d, '%s'", lMaxMemory,uLine, sFile)); if (MyFlags & MY_FAE) exit(1); DBUG_RETURN ((gptr) NULL); } /* Fill up the structure */ *((long*) ((char*) &pTmp -> lSpecialValue+sf_malloc_prehunc)) = MAGICKEY; pTmp -> aData[uSize + sf_malloc_prehunc+0] = MAGICEND0; pTmp -> aData[uSize + sf_malloc_prehunc+1] = MAGICEND1; pTmp -> aData[uSize + sf_malloc_prehunc+2] = MAGICEND2; pTmp -> aData[uSize + sf_malloc_prehunc+3] = MAGICEND3; pTmp -> sFileName = (my_string) sFile; pTmp -> uLineNum = uLine; pTmp -> uDataSize = uSize; pTmp -> pPrev = NULL; /* Add this remember structure to the linked list */ pthread_mutex_lock(&THR_LOCK_malloc); if ((pTmp->pNext=pRememberRoot)) { pRememberRoot -> pPrev = pTmp; } pRememberRoot = pTmp; /* Keep the statistics */ lCurMemory += uSize; if (lCurMemory > lMaxMemory) { lMaxMemory = lCurMemory; } cNewCount++; pthread_mutex_unlock(&THR_LOCK_malloc); /* Set the memory to the aribtrary wierd value */ #ifdef HAVE_purify if (MyFlags & MY_ZEROFILL) #endif bfill(&pTmp -> aData[sf_malloc_prehunc],uSize, (char) (MyFlags & MY_ZEROFILL ? 0 : ALLOC_VAL)); /* Return a pointer to the real data */ DBUG_PRINT("exit",("ptr: %lx",&(pTmp -> aData[sf_malloc_prehunc]))); if (sf_min_adress > &(pTmp -> aData[sf_malloc_prehunc])) sf_min_adress = &(pTmp -> aData[sf_malloc_prehunc]); if (sf_max_adress < &(pTmp -> aData[sf_malloc_prehunc])) sf_max_adress = &(pTmp -> aData[sf_malloc_prehunc]); DBUG_RETURN ((gptr) &(pTmp -> aData[sf_malloc_prehunc])); }
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); } /* 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'", sf_malloc_max_memory,lineno, filename)); 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 */ pthread_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++; pthread_mutex_unlock(&THR_LOCK_malloc); /* 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)); /* 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); }