📄 mem_alloc.cpp
字号:
#include "mem_alloc.h"
#include <memory.h>
#include <stdio.h>
#define MEM_TAG_66 4
#define MEM_TAG_33 2
#define MEM_TAG_03 1
#define MEM_03_SIZE 3*4
#define MEM_33_SIZE 33*4
#define MEM_66_SIZE 66*4
#if defined( MEM_DEBUG )
static int mem_max_used_03 = 0;
static int mem_max_used_33 = 0;
static int mem_max_used_66 = 0;
static int mem_used_count_03 = 0;
static int mem_used_count_33 = 0;
static int mem_used_count_66 = 0;
static int mem_chg_03_to_33 = 0;
static int mem_chg_33_to_66 = 0;
static int mem_chg_03_to_66 = 0;
static size_t size_record[512];
#endif
unsigned char g_mem03[MAX_03_LONG_MEM][MEM_03_SIZE + 1];
unsigned char g_mem33[MAX_33_LONG_MEM][MEM_33_SIZE + 1];
unsigned char g_mem66[MAX_66_LONG_MEM][MEM_66_SIZE + 1];
void mem_init(void)
{
memset(g_mem03,0x00,sizeof(g_mem03));
memset(g_mem33,0x00,sizeof(g_mem33));
memset(g_mem66,0x00,sizeof(g_mem66));
#if defined( MEM_DEBUG )
mem_used_count_03 = 0;
mem_used_count_33 = 0;
mem_used_count_66 = 0;
mem_max_used_03 = 0;
mem_max_used_33 = 0;
mem_max_used_66 = 0;
mem_chg_03_to_33 = 0;
mem_chg_33_to_66 = 0;
mem_chg_03_to_66 = 0;
memset(size_record,0,sizeof(size_record));
#endif
}
void * mem_realloc(void *base ,size_t size)
{
#if defined( MEM_DEBUG )
for(unsigned int idx = 0; idx < 512 ; idx ++)
{
if(size_record[idx] == 0)
{
size_record[idx] = size;
break;
}
}
#endif
if( base == NULL )
{
if(size > MEM_66_SIZE)
return NULL;
if(size > MEM_33_SIZE)
{
for(unsigned char idx = 0 ; idx < MAX_66_LONG_MEM ; idx ++)
{
if(g_mem66[idx][0] != MEM_TAG_66)
{
g_mem66[idx][0] = MEM_TAG_66;
#if defined( MEM_DEBUG )
mem_used_count_66 ++;
if(mem_used_count_66 > mem_max_used_66) mem_max_used_66 = mem_used_count_66;
#endif
return &(g_mem66[idx][1]);
}
}
if(idx == MAX_66_LONG_MEM)
return NULL;
}
if(size > MEM_03_SIZE)
{
for(unsigned char idx = 0 ; idx < MAX_33_LONG_MEM ; idx ++)
{
if(g_mem33[idx][0] != MEM_TAG_33)
{
g_mem33[idx][0] = MEM_TAG_33;
#if defined( MEM_DEBUG )
mem_used_count_33 ++;
if(mem_used_count_33 > mem_max_used_33) mem_max_used_33 = mem_used_count_33;
#endif
return &(g_mem33[idx][1]);
}
}
if(idx == MAX_33_LONG_MEM)
return NULL;
}
if(size > 0)
{
for(unsigned char idx = 0 ; idx < MAX_03_LONG_MEM ; idx ++)
{
if(g_mem03[idx][0] != MEM_TAG_03)
{
g_mem03[idx][0] = MEM_TAG_03;
#if defined( MEM_DEBUG )
mem_used_count_03 ++;
if(mem_used_count_03 > mem_max_used_03) mem_max_used_03 = mem_used_count_03;
#endif
return &(g_mem03[idx][1]);
}
}
if(idx == MAX_03_LONG_MEM)
return NULL;
}
return NULL;
}
else
{
switch( *((unsigned char*)base - 1) )
{
case MEM_TAG_03:
if(size > MEM_66_SIZE)
return NULL;
if(size > MEM_33_SIZE)
{
for(unsigned char idx = 0 ; idx < MAX_66_LONG_MEM ; idx ++)
{
if(g_mem66[idx][0] != MEM_TAG_66)
{
g_mem66[idx][0] = MEM_TAG_66;
*((unsigned char *)base - 1) = 0;
#if defined( MEM_DEBUG )
mem_used_count_66 ++;
if(mem_used_count_66 > mem_max_used_66) mem_max_used_66 = mem_used_count_66;
mem_chg_03_to_66 ++;
mem_used_count_03 --;
#endif
memcpy(&(g_mem66[idx][1]), base , MEM_03_SIZE);
return &(g_mem66[idx][1]);
}
}
if(idx == MAX_66_LONG_MEM)
return NULL;
}
if( size > MEM_03_SIZE )
{
for(unsigned char idx = 0 ; idx < MAX_33_LONG_MEM ; idx ++)
{
if(g_mem33[idx][0] != MEM_TAG_33)
{
g_mem33[idx][0] = MEM_TAG_33;
*((unsigned char *)base - 1) = 0;
#if defined( MEM_DEBUG )
mem_used_count_33 ++;
if(mem_used_count_33 > mem_max_used_66) mem_max_used_33 = mem_used_count_33;
mem_chg_03_to_33 ++;
mem_used_count_03 --;
#endif
memcpy(&(g_mem33[idx][1]), base , MEM_03_SIZE);
return &(g_mem33[idx][1]);
}
}
if(idx == MAX_33_LONG_MEM)
return NULL;
}
return base;
case MEM_TAG_33:
if(size > MEM_66_SIZE)
return NULL;
if(size > MEM_33_SIZE)
{
for(unsigned char idx = 0 ; idx < MAX_66_LONG_MEM ; idx ++)
{
if(g_mem66[idx][0] != MEM_TAG_66)
{
g_mem66[idx][0] = MEM_TAG_66;
*((unsigned char *)base - 1) = 0;
#if defined( MEM_DEBUG )
mem_used_count_66 ++;
if(mem_used_count_66 > mem_max_used_66) mem_max_used_66 = mem_used_count_66;
mem_chg_33_to_66 ++;
mem_used_count_33 --;
#endif
memcpy(&(g_mem66[idx][1]), base , MEM_33_SIZE);
return &(g_mem66[idx][1]);
}
}
if(idx == MAX_66_LONG_MEM)
return NULL;
}
return base;
case MEM_TAG_66:
if(size > MEM_66_SIZE)
return NULL;
return base;
default:
return mem_realloc(NULL,size);
}
}
}
void mem_free(void *base)
{
switch(*((unsigned char *)base - 1))
{
case MEM_TAG_66:
#if defined( MEM_DEBUG )
mem_used_count_66 --;
#endif
break;
case MEM_TAG_33:
#if defined( MEM_DEBUG )
mem_used_count_33 --;
#endif
break;
case MEM_TAG_03:
#if defined( MEM_DEBUG )
mem_used_count_03 --;
#endif
break;
default:
return;
}
*((unsigned char *)base - 1) = 0;
return;
}
#if defined( MEM_DEBUG )
void mem_report(void)
{
unsigned char count;
count = 0;
unsigned int num1,num2,num3;
for(unsigned int idx = 0 ; idx < MAX_66_LONG_MEM ; idx ++)
{
if(g_mem66[idx][0] != 0)
count ++;
}
if(count != 0)
printf("warning!! %d of 66 ulong memory is being used.\n",count);
count = 0;
for(unsigned int idx = 0 ; idx < MAX_33_LONG_MEM ; idx ++)
{
if(g_mem33[idx][0] != 0)
count ++;
}
if(count != 0)
printf("warning!! %d of 33 ulong memory is being used.\n",count);
count = 0;
for(unsigned int idx = 0 ; idx < MAX_03_LONG_MEM ; idx ++)
{
if(g_mem03[idx][0] != 0)
count ++;
}
if(count != 0)
printf("warning!! %d of 3 ulong memory is being used.\n",count);
printf("Need %d bytes memory totally.\n", mem_max_used_03*(MEM_03_SIZE + 1) + mem_max_used_33*(MEM_33_SIZE + 1) + mem_max_used_66*(MEM_66_SIZE + 1));
printf("3 ulong memory needs %d\n",mem_max_used_03);
printf("33 ulong memory needs %d\n",mem_max_used_33);
printf("66 ulong memory needs %d\n",mem_max_used_66);
printf("3 ulong memory to 33 ulong memory %d times\n",mem_chg_03_to_33);
printf("3 ulong memory to 66 ulong memory %d times\n",mem_chg_03_to_66);
printf("33 ulong memory to 66 ulong memory %d times\n",mem_chg_33_to_66);
num1 = num2 = num3 = 0;
for(unsigned int idx = 0 ; idx < 512 ; idx ++)
{
if( size_record[idx]/4 > 33)
num3 ++;
else if(size_record[idx]/4 > 3)
num2 ++;
else
num1 ++;
}
printf("3 ulong memory alloc %d times\n",num1);
printf("33 ulong memory alloc %d times\n",num2);
printf("66 ulong memory alloc %d times\n",num3);
}
#else
void mem_report(void)
{
printf("Memory report is not available.\n");
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -