📄 jmalloc.c
字号:
/* -1 = error, */
/* 0 = no error detected */
/*--------------------------------------------------------------*/
int j_memcheck(int CheckFree)
{
int status = 0;
MLINK *CurMal = MalTop;
/* Walk the alloc list */
if ( CheckFree >= 2 )
{
DBUG_PRINT("alloc", ("There should be %d unfreed blocks", memc));
}
while ( CurMal != NULL )
{
if ( CurMal->MAddr[CurMal->MSize] != CKBYT )
{
DBUG_PRINT("alloc", ("Memory overrun detected in link "
"allocated at %s Line %d", CurMal->MFile, CurMal->MLine));
status = -1;
}
if ( CheckFree >= 1 )
{
DBUG_PRINT("alloc", ("Unfreed block of size %d allocated at "
"%s Line %d", CurMal->MSize, CurMal->MFile, CurMal->MLine));
status = -1;
}
CurMal = CurMal->NextLink;
}
return(status);
}
/*-------------------------[ JMemValid ]------------------------*/
/* Verify an address is in a JMalloc space */
/*--------------------------------------------------------------*/
int JMemValid(char *Addr)
{
MLINK *CurMal = MalTop;
while ( CurMal != NULL )
{
if ( Addr >= CurMal->MAddr && Addr <= CurMal->MAddr + CurMal->MSize )
return(TRUE);
CurMal = CurMal->NextLink;
}
return(FALSE);
}
/*-------------------------[ JMemOwner ]------------------------*/
/* Return the owner of a block of ram */
/*--------------------------------------------------------------*/
static MLINK *JMemOwner(char *Addr)
{
MLINK *CurMal = MalTop;
while ( CurMal != NULL )
{
unsigned long Cur = (unsigned long)CurMal->MAddr,
Adr = (unsigned long)Addr;
if ( Adr >= Cur && Adr <= Cur + CurMal->MSize )
return(CurMal);
CurMal = CurMal->NextLink;
}
return((void*)NULL);
}
/*-------------------------[ j_strcpy ]-------------------------*/
/* String copy with checks and protections */
/*--------------------------------------------------------------*/
char *j_strcpy(char *__dest, const char *__src, char *file, int line)
{
MLINK *MemOwner = JMemOwner(__dest);
if ( MemOwner == NULL )
{
DBUG_PRINT("alloc", ("Strcpy destination invalid - %s: line %d",
file, line));
}
else if ( strlen(__src) > MemOwner->MAddr + MemOwner->MSize - __dest )
{
DBUG_PRINT("alloc", ("Strcpy destination overrun - %s: line %d",
file, line));
}
return(strcpy(__dest, __src));
}
/*-------------------------[ j_strncpy ]------------------------*/
/* String n copy with checks */
/*--------------------------------------------------------------*/
char *j_strncpy(char *__dest, const char *__src, size_t maxlen, char *file,
int line)
{
MLINK *MemOwner = JMemOwner(__dest);
if ( MemOwner == NULL )
{
DBUG_PRINT("alloc", ("strncpy destination invalid - %s: line %d",
file, line));
}
else if ( maxlen > MemOwner->MAddr + MemOwner->MSize - __dest )
{
DBUG_PRINT("alloc", ("strncpy destination overrun - %s: line %d",
file, line));
}
return(strncpy(__dest, __src, maxlen));
}
char *j_strcat(char *__dest, char *__src, char *file, int line)
{
MLINK *MemOwner = JMemOwner(__dest);
if ( MemOwner == NULL )
{
DBUG_PRINT("alloc", ("strcat destination invalid - %s: line %d",
file, line));
}
else if ( strlen(MemOwner->MAddr) + strlen(__src) + 1 > MemOwner->MSize )
{
DBUG_PRINT("alloc", ("strcat destination overrun - %s: line %d",
file, line));
}
return(strcat(__dest, __src));
}
char *j_strdup(char *__str, char *file, int line)
{
char *p = j_malloc(strlen(__str) + 1, file, line);
if ( p != NULL )
{
j_strcpy(p, __str, file, line);
}
return(p);
}
char *j_strnset(char *str, int ch, size_t n, char *file, int line)
{
MLINK *MemOwner = JMemOwner(str);
if ( MemOwner == NULL )
{
DBUG_PRINT("alloc", ("strnset destination invalid - %s: line %d",
file, line));
}
return(strnset(str, ch, n));
}
char *j_strset(char *str, int ch, char *file, int line)
{
MLINK *MemOwner = JMemOwner(str);
if ( MemOwner == NULL )
{
DBUG_PRINT("alloc", ("strset destination invalid - %s: line %d",
file, line));
}
return(strset(str, ch));
}
/*------------------------[ j_checkstr ]------------------------*/
/* Check a MLINK string for problems */
/* j_sprintf sprintf replacement */
/*--------------------------------------------------------------*/
int j_checkstr(MLINK *str)
{
if ( str->MAddr[str->MSize] != CKBYT || strlen(str->MAddr) > str->MSize - 1 )
{
DBUG_PRINT("alloc",
("Bad string, allocated at %s Line %d", str->MFile, str->MLine));
return 1;
}
return 0;
}
void *j_memcpy(void *dest, const void *src, size_t n, char *file, int line)
{
MLINK *MemOwner = JMemOwner(dest);
if ( MemOwner == NULL )
{
DBUG_PRINT("alloc", ("memcpy destination invalid - %s: line %d",
file, line));
}
else if ( n > MemOwner->MAddr + MemOwner->MSize - (char *)dest )
{
DBUG_PRINT("alloc", ("memcpy destination overrun - %s: line %d",
file, line));
}
return(memcpy(dest, src, n));
}
void *j_memset(void *dest, int ch, size_t n, char *file, int line)
{
MLINK *MemOwner = JMemOwner(dest);
if ( MemOwner == NULL )
{
DBUG_PRINT("alloc", ("memset destination invalid - %s: line %d",
file, line));
}
else if ( n > MemOwner->MAddr + MemOwner->MSize - (char *)dest )
{
DBUG_PRINT("alloc", ("memset destination overrun - %s: line %d",
file, line));
}
return(memset(dest, ch, n));
}
char *a_strcpy(char *__dest, const char *__src, int size, char *file,
int line)
{
if ( strlen(__src) >= size)
DBUG_PRINT("alloc", ("strcpy destination overrun - %s: line %d",
file, line));
return(strcpy(__dest, __src));
}
char *a_strncpy(char *dest, const char *src, size_t n, int size, char *file,
int line)
{
if ( n >= size )
DBUG_PRINT("alloc", ("strncpy destination overrun - %s: line %d",
file, line));
return(strncpy(dest, src, n));
}
char *a_strcat(char *dest, const char *src, int size, char *file, int line)
{
if ( strlen(dest) + strlen(src) + 1 > size )
DBUG_PRINT("alloc", ("strcat destination overrun = %s: line %d",
file, line));
return(strcat(dest, src));
}
char *a_strnset(char *str, int ch, size_t n, int size, char *file, int line)
{
if ( n + 1 > size )
DBUG_PRINT("alloc", ("strnset destination overrun - %s: line %d",
file, line));
return(strnset(str, ch, n));
}
void *a_memcpy(void *dest, const void *src, size_t n, int size, char *file,
int line)
{
if ( n > size )
DBUG_PRINT("alloc", ("memcpy destination overrun - %s: line %d",
file, line));
return(memcpy(dest, src, n));
}
void *a_memset(void *dest, int ch, size_t n, int size, char *file, int line)
{
if ( n > size )
DBUG_PRINT("alloc", ("memset destination overrun - %s: line %d",
file, line));
return memset(dest, ch, n);
}
#ifdef TEST
#ifdef __WATCOMC__
#pragma off (unreferenced);
#endif
#ifdef __TURBOC__
#pragma argsused
#endif
int main(int argc, char *argv[])
{
char *new,
*p,
*q,
r[30],
*s[256] = {NULL};
int i = 1;
while ( (p = argv[i++]) != NULL )
{
switch ( *p++ )
case '-':
switch ( *p++ )
case '#':
DBUG_PUSH(argv[i - 1]);
}
new = JCalloc(64, 56);
new = JRealloc(new, 64);
JStrCpy(new + 10, "Test string");
DBUG_PRINT("alloc", ("r is invalid"));
JStrCpy(r, "Test");
q = JCalloc(4, 4);
q[4 * 4] = 3;
DBUG_PRINT("alloc", ("p was never allocated"));
JFree(p);
i = 0;
DBUG_PRINT("alloc", ("Deplete memory"));
do
{
s[i] = JMalloc(32000);
i++;
} while ( s[i - 1] != NULL );
i = 0;
while ( s[i] != NULL )
{
JFree(s[i++]);
}
DBUG_PRINT("alloc", ("New and q are orphaned blocks"));
DBUG_PRINT("alloc", ("q has an overrun"));
JMemcheck(1);
return(0);
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -