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 */
void myfree_f(void *ptr) { /* this one is used for function pointers, when you need &myfree */ _myfree(ptr, "FUNCTION_PTR", 0); }