1
0
mirror of https://github.com/adtools/clib2.git synced 2026-05-03 18:56:04 +00:00

The memory debugging code now places the the damage detection areas which bracket the allocation area so that the originally requested allocation size is used, not the rounded-up size. Debug output in memory node checking no longer counts the non-breaking space character as unprintable.

This commit is contained in:
obarthel
2023-09-08 16:11:01 +02:00
parent 3d70b18c23
commit 71708e84ce

View File

@@ -64,7 +64,7 @@ get_hex_char(int n)
else
result = n + 'A' - 10;
return(result);
return result;
}
STATIC VOID
@@ -97,7 +97,7 @@ int_to_hex(unsigned long v,char * buffer,int min_digits)
}
STATIC VOID
dump_memory(unsigned char * m,int size,int ignore)
dump_memory(const unsigned char * m, int size, int ignore)
{
const int mod = 20;
int position, i, c;
@@ -137,7 +137,7 @@ dump_memory(unsigned char * m,int size,int ignore)
buffer[10 + 2 * position + 1] = get_hex_char(m[i] & 15);
c = m[i];
if(c < ' ' || (c >= 127 && c <= 160))
if (c < ' ' || (127 <= c && c < 160))
c = '.';
buffer[10 + 2 * mod + 1 + position] = c;
@@ -167,10 +167,10 @@ dump_memory(unsigned char * m,int size,int ignore)
STATIC VOID
check_memory_node(struct MemoryNode * mn, const char * file, int line)
{
ULONG size = GET_MN_SIZE(mn);
unsigned char * head = (unsigned char *)(mn + 1);
unsigned char * body = head + MALLOC_HEAD_SIZE;
unsigned char * tail = body + size;
ULONG size = mn->mn_OriginalSize;
const unsigned char * head = (unsigned char *)&mn[1];
const unsigned char * body = &head[MALLOC_HEAD_SIZE];
const unsigned char * tail = &body[size];
int max_head_damage = 0;
int max_tail_damage = 0;
int max_body_damage = 0;
@@ -331,7 +331,7 @@ __find_memory_node(void * address)
result = &((struct MemoryNode *)address)[-1];
return(result);
return result;
}
/****************************************************************************/
@@ -349,6 +349,8 @@ remove_and_free_memory_node(struct MemoryNode * mn)
__memory_lock();
allocation_size = mn->mn_AllocationSize;
#if defined(__MEM_DEBUG)
{
Remove((struct Node *)mn);
@@ -359,16 +361,8 @@ remove_and_free_memory_node(struct MemoryNode * mn)
}
#endif /* __USE_MEM_TREES */
allocation_size = sizeof(*mn) + MALLOC_HEAD_SIZE + GET_MN_SIZE(mn) + MALLOC_TAIL_SIZE;
assert( allocation_size == mn->mn_AllocationSize );
memset(mn, MALLOC_FREE_FILL, allocation_size);
}
#else
{
allocation_size = sizeof(*mn) + GET_MN_SIZE(mn);
}
#endif /* __MEM_DEBUG */
#if defined(__USE_SLAB_ALLOCATOR)
@@ -376,70 +370,64 @@ remove_and_free_memory_node(struct MemoryNode * mn)
/* Are we using the slab allocator? */
if (__slab_data.sd_InUse)
{
/* No need to remove the memory node because it was never
* added or has already been removed if the memory debugging
* option is in effect.
*/
__slab_free(mn, allocation_size);
}
else
{
if(__memory_pool != NULL)
/* Are we using the memory pool? */
else if (__memory_pool != NULL)
{
/* No need to remove the memory node because it was never
* added or has already been removed if the memory debugging
* option is in effect.
*/
PROFILE_OFF();
FreePooled(__memory_pool, mn, allocation_size);
PROFILE_ON();
}
/* So we have to do this the hard way... */
else
{
#if defined(__MEM_DEBUG)
#if NOT defined(__MEM_DEBUG)
{
Remove((struct Node *)mn);
}
#endif /* __MEM_DEBUG */
PROFILE_OFF();
FreeMem(mn, allocation_size);
PROFILE_ON();
}
#else
{
struct MinNode * mln = (struct MinNode *)mn;
mln--;
Remove((struct Node *)mln);
PROFILE_OFF();
FreeMem(mln,sizeof(*mln) + allocation_size);
PROFILE_ON();
}
#endif /* __MEM_DEBUG */
}
}
}
#else
{
if (__memory_pool != NULL)
{
/* No need to remove the memory node because it was never
* added or has already been removed if the memory debugging
* option is in effect.
*/
PROFILE_OFF();
FreePooled(__memory_pool, mn, allocation_size);
PROFILE_ON();
}
else
{
#if defined(__MEM_DEBUG)
#if NOT defined(__MEM_DEBUG)
{
Remove((struct Node *)mn);
}
#endif /* __MEM_DEBUG */
/* No need to remove the memory node because the memory
* debugging option is in effect.
*/
PROFILE_OFF();
FreeMem(mn, allocation_size);
PROFILE_ON();
}
#else
{
struct MinNode * mln = (struct MinNode *)mn;
mln--;
Remove((struct Node *)mln);
PROFILE_OFF();
FreeMem(mln,sizeof(*mln) + allocation_size);
PROFILE_ON();
}
#endif /* __MEM_DEBUG */
}
}
#endif /* __USE_SLAB_ALLOCATOR */
@@ -458,15 +446,13 @@ __free_memory_node(struct MemoryNode * mn,const char * UNUSED file,int UNUSED li
#ifdef __MEM_DEBUG
{
ULONG size = GET_MN_SIZE(mn);
check_memory_node(mn, file, line);
if (NOT mn->mn_AlreadyFree)
{
#ifdef __MEM_DEBUG_LOG
{
kprintf("[%s] - %10ld 0x%08lx [",__program_name,size,mn->mn_Allocation);
kprintf("[%s] - %10ld 0x%08lx [",__program_name, mn->mn_OriginalSize, mn->mn_Allocation);
if (mn->mn_File != NULL)
kprintf("allocated at %s:%ld, ", mn->mn_File, mn->mn_Line);
@@ -482,7 +468,7 @@ __free_memory_node(struct MemoryNode * mn,const char * UNUSED file,int UNUSED li
mn->mn_FreeFile = (char *)file;
mn->mn_FreeLine = line;
memset(mn->mn_Allocation,MALLOC_FREE_FILL,size);
memset(&mn[1], MALLOC_FREE_FILL, MALLOC_HEAD_SIZE + mn->mn_OriginalSize + MALLOC_TAIL_SIZE);
}
else
{
@@ -493,14 +479,14 @@ __free_memory_node(struct MemoryNode * mn,const char * UNUSED file,int UNUSED li
{
#ifdef __MEM_DEBUG_LOG
{
kprintf("[%s] - %10ld 0x%08lx [",__program_name,size,mn->mn_Allocation);
kprintf("[%s] - %10ld 0x%08lx [", __program_name, mn->mn_AllocationSize, mn);
kprintf("FAILED]\n");
}
#endif /* __MEM_DEBUG_LOG */
kprintf("[%s] %s:%ld:Allocation at address 0x%08lx, size %ld",
__program_name,file,line,mn->mn_Allocation,size);
__program_name, file, line, mn->mn_Allocation, mn->mn_OriginalSize);
if (mn->mn_File != NULL)
kprintf(", allocated at %s:%ld", mn->mn_File, mn->mn_Line);
@@ -540,7 +526,7 @@ __free_memory(void * ptr,BOOL force,const char * file,int line)
{
if (mn != NULL)
{
if(force || FLAG_IS_CLEAR(mn->mn_Size, MN_SIZE_NEVERFREE))
if (force || FLAG_IS_CLEAR(mn->mn_Flags, MNF_NEVER_FREE))
__free_memory_node(mn, file, line);
}
else
@@ -562,9 +548,12 @@ __free_memory(void * ptr,BOOL force,const char * file,int line)
{
assert( mn != NULL );
SHOWVALUE(mn->mn_Size);
if (FLAG_IS_SET(mn->mn_Flags, MNF_NEVER_FREE))
D(("mn->mn_AllocationSize=%ld (0x%08lx), not to be freed", mn->mn_AllocationSize, mn->mn_AllocationSize));
else
D(("mn->mn_AllocationSize=%ld (0x%08lx)", mn->mn_AllocationSize, mn->mn_AllocationSize));
if(mn != NULL && (force || FLAG_IS_CLEAR(mn->mn_Size, MN_SIZE_NEVERFREE)))
if (mn != NULL && (force || FLAG_IS_CLEAR(mn->mn_Flags, MNF_NEVER_FREE)))
__free_memory_node(mn, file, line);
}
#endif /* __MEM_DEBUG */