⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 memalloc.cpp

📁 内存管理程序
💻 CPP
字号:
/*=========================================================
*	File name	:	MemAlloc.cpp
*	Description	:	内存管理:管理固定长度的内存,不支持多线程
*
*	Modify  	:	
*=========================================================*/
#include "memalloc.h"
//#include "memmgr.h"
#include <sys/wait.h>

#ifdef MEM_CONTROL_FLUX

int g_newhttp = 0;
int g_delhttp = 0;
int g_CacheNum[100];
unsigned int g_MemTotal = 0;
int g_control1 = 0;
int g_control2 =0;
int g_linktimeout = 60;
int g_UserdMem[2] = {0,0};
#endif
MemAlloc::MemAlloc(int size)
{
	m_lpCache = NULL;
	m_size = size;
	m_MaxValue = m_CurValue = m_IdleValue = 0;
}

MemAlloc::~MemAlloc()
{

	Close();
}

void   MemAlloc::SetSize(int size)
{
	Close();
	m_lpCache = NULL;
	m_size = size;
	m_MaxValue = m_CurValue = 0;
}
#ifdef MEM_CONTROL_FLUX

//Get used memory from top
//int Mem[2]: pointer to int[2] to hold the fields of size and res in top
//if successful, return 1;otherwise return 0

int GetUsedMem(int Mem[2])
{
	FILE *fopen;
	int status;
	int nBytes;
	char strCmd[64] = "top|grep porsche|awk \'{print $5,$6}\'";
	char strRes[32];
	if( (fopen = popen(strCmd, "r")) == NULL)
	{
		fprintf(stderr, "failed to popen in GetUsedMem!\n");
		return 0;
	}
	else
	{
		wait(&status);
		if( (nBytes = fread(strRes, 1, sizeof(strRes), fopen)) > 0)
		{
			sscanf(strRes, "%dM %dM", &Mem[0], &Mem[1]);
			pclose(fopen);
			return 1;
		}

		else
		{
			pclose(fopen);
			return 0;
		}
	}
}

void printfmeminfo()
{
/*	printf("New http object is %u \r\n",g_newhttp);
	printf("Del http object is %u \r\n",g_delhttp);
	printf("total used mem is %uM \r\n",g_MemTotal/1000000);
	for (int i=0;i<10;i++)
		printf("g_cachenum is %d ,num is %d\r\n ",g_CacheNum[i+90],i+90);
*/
}

void SetUpControl()
{
	if (g_UserdMem[1] > FIRST_UP_LIMIT && g_control1 == 0)
	{
		g_control1 = 1;
		printf("exceed the FIRST_UP_LIMIT \r\n");
	}
	if (g_UserdMem[1] > SECOND_UP_LIMIT && g_control2 == 0)
	{
		g_control2 = 1;
		printf("exceed the SECOND_UP_LIMIT! \r\n");
	}
	/*if (g_MemTotal > FIRST_UP_LIMIT && g_control1 == 0)
	{
		g_control1 = 1;
		printf("exceed the FIRST_UP_LIMIT \r\n");
	}
	if (g_MemTotal > SECOND_UP_LIMIT && g_control2 == 0)
	{
		g_control2 = 1;
		printf("exceed the SECOND_UP_LIMIT! \r\n");
	}*/
}

void SetDownControl()
{
	if (g_UserdMem[1]< FIRST_DOWN_LIMIT && g_control1 == 1)
	{
		g_control1 = 0;
		printf("return the MEM_SECOND_MIN \r\n");
	}
	if (g_UserdMem[1]< SECOND_DOWN_LIMIT && g_control2 == 1)
	{
		g_control2 = 0;
		printf("return the SECOND_DOWN_LIMIT \r\n");
	}	
/*	if (g_MemTotal < FIRST_DOWN_LIMIT && g_control1 == 1)
	{
		g_control1 = 0;
		printf("return the MEM_SECOND_MIN \r\n");
	}
	if (g_MemTotal < SECOND_DOWN_LIMIT && g_control2 == 1)
	{
		g_control2 = 0;
		printf("return the SECOND_DOWN_LIMIT \r\n");
	}	*/
}
#endif

#ifdef _DEBUG
#define MEMALLOC_SIZE_D   4
#else
#define MEMALLOC_SIZE_D  0
#endif

#ifdef _DEBUG
void MemAlloc::VerifyMem(char *x)
{
	ASSERT(x);
	for(int i = 0;i<MEMALLOC_SIZE_D;i++)
	{
		ASSERT(*(x+m_size+i)==0x78);
	}
}
#else
void MemAlloc::VerifyMem(char *x)
{
}
#endif

char*  MemAlloc::Alloc()
{
	char* x;
	m_lock.Lock();
	x = m_lpCache;
	if (x == NULL )
	{
		x = (char*)(malloc(m_size+MEMALLOC_SIZE_D));
#ifdef MEM_CONTROL_FLUX
		g_MemTotal = g_MemTotal + m_size;
#endif
		if (x== NULL)
		{
#ifdef MEM_CONTROL_FLUX
			printfmeminfo();
#endif
			m_lock.Unlock(1);

			return NULL;
		}
#ifdef _DEBUG
		memset(x+m_size,0x78,MEMALLOC_SIZE_D);
#endif
		m_MaxValue ++;
	}
	else
	{
		// Return block from cache, update it
		char** x_fp = (char**) x;
		m_lpCache = *x_fp;
		m_IdleValue --;
	}
	m_CurValue ++;
	VerifyMem(x);
	m_lock.Unlock(2);
	return x;	
}

void   MemAlloc::Free(void *blk)
{
	if (blk == NULL)
		return;
#ifdef MEM_CONTROL_FLUX
	if(m_IdleValue < MEM_MEMALLOC_MAX)
	{
#endif
                m_lock.Lock();
		VerifyMem((char*)blk);
		// Put reference to next free block in first 4 bytes
		char** blk_fp = (char**) blk;
		*blk_fp = m_lpCache;
		// And add block as head of cache
		m_lpCache = (char*)blk;
		m_lock.Unlock(3);	
#ifdef MEM_CONTROL_FLUX

		m_IdleValue ++;

	}
	else
	{
		free(blk);
		m_MaxValue --;
		g_MemTotal = g_MemTotal - m_size;
	}
#endif

	if(m_CurValue)
		m_CurValue --;	
}

void   MemAlloc::Close()
{
	m_lock.Lock();
	while (m_lpCache)
	{
		char** x = (char**)m_lpCache;
		m_lpCache = *x;
		VerifyMem((char*)x);
		free((char*)x);
		m_MaxValue--;
	}
	m_lock.Unlock(4);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -