#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <new>
#include <errno.h>
#include <pthread.h>
#include <semaphore.h>
#include <time.h>
#include <limits.h>
#include <unistd.h>
#include <sys/prctl.h>
#include "MemPoolCore.hpp"
#define M___8_BYTE_COUNT (1024*32)
#define M__16_BYTE_COUNT (1024*32)
#define M__32_BYTE_COUNT (1024*32)
#define M__64_BYTE_COUNT (1024*32)
#define M_128_BYTE_COUNT (1024*32)
#define M_256_BYTE_COUNT (1024*32)
#define M_512_BYTE_COUNT (1024*32)
#define M__1K_BYTE_COUNT (1024*32)
#define M__2K_BYTE_COUNT (1024*32)
#define M__4K_BYTE_COUNT (1024*32)
typedef struct {
void * pAddr;
int isUsed;
} MPC_MemBlock_t;
#define MGR___8_BYTE_TOTAL_SIZE ( M___8_BYTE_COUNT * sizeof(MPC_MemBlock_t) )
#define MGR__16_BYTE_TOTAL_SIZE ( M__16_BYTE_COUNT * sizeof(MPC_MemBlock_t) )
#define MGR__32_BYTE_TOTAL_SIZE ( M__32_BYTE_COUNT * sizeof(MPC_MemBlock_t) )
#define MGR__64_BYTE_TOTAL_SIZE ( M__64_BYTE_COUNT * sizeof(MPC_MemBlock_t) )
#define MGR_128_BYTE_TOTAL_SIZE ( M_128_BYTE_COUNT * sizeof(MPC_MemBlock_t) )
#define MGR_256_BYTE_TOTAL_SIZE ( M_256_BYTE_COUNT * sizeof(MPC_MemBlock_t) )
#define MGR_512_BYTE_TOTAL_SIZE ( M_512_BYTE_COUNT * sizeof(MPC_MemBlock_t) )
#define MGR__1K_BYTE_TOTAL_SIZE ( M__1K_BYTE_COUNT * sizeof(MPC_MemBlock_t) )
#define MGR__2K_BYTE_TOTAL_SIZE ( M__2K_BYTE_COUNT * sizeof(MPC_MemBlock_t) )
#define MGR__4K_BYTE_TOTAL_SIZE ( M__4K_BYTE_COUNT * sizeof(MPC_MemBlock_t) )
#define MEM___8_BYTE_TOTAL_SIZE ( M___8_BYTE_COUNT * 8 )
#define MEM__16_BYTE_TOTAL_SIZE ( M__16_BYTE_COUNT * 16 )
#define MEM__32_BYTE_TOTAL_SIZE ( M__32_BYTE_COUNT * 32 )
#define MEM__64_BYTE_TOTAL_SIZE ( M__64_BYTE_COUNT * 64 )
#define MEM_128_BYTE_TOTAL_SIZE ( M_128_BYTE_COUNT * 128 )
#define MEM_256_BYTE_TOTAL_SIZE ( M_256_BYTE_COUNT * 256 )
#define MEM_512_BYTE_TOTAL_SIZE ( M_512_BYTE_COUNT * 512 )
#define MEM__1K_BYTE_TOTAL_SIZE ( M__1K_BYTE_COUNT * 1024 )
#define MEM__2K_BYTE_TOTAL_SIZE ( M__2K_BYTE_COUNT * 2048 )
#define MEM__4K_BYTE_TOTAL_SIZE ( M__4K_BYTE_COUNT * 4096 )
/*
do not use array.
MPC_MemBlock_t m___8_BYTE_mgr_table[M___8_BYTE_COUNT];
MPC_MemBlock_t m__16_BYTE_mgr_table[M__16_BYTE_COUNT];
MPC_MemBlock_t m__32_BYTE_mgr_table[M__32_BYTE_COUNT];
MPC_MemBlock_t m__64_BYTE_mgr_table[M__64_BYTE_COUNT];
MPC_MemBlock_t m_128_BYTE_mgr_table[M_128_BYTE_COUNT];
MPC_MemBlock_t m_256_BYTE_mgr_table[M_256_BYTE_COUNT];
MPC_MemBlock_t m_512_BYTE_mgr_table[M_512_BYTE_COUNT];
MPC_MemBlock_t m__1K_BYTE_mgr_table[M__1K_BYTE_COUNT];
MPC_MemBlock_t m__2K_BYTE_mgr_table[M__2K_BYTE_COUNT];
MPC_MemBlock_t m__4K_BYTE_mgr_table[M__4K_BYTE_COUNT];
*/
MPC_MemBlock_t * p___8_BYTE_mgr_table; // [M___8_BYTE_COUNT];
MPC_MemBlock_t * p__16_BYTE_mgr_table; // [M__16_BYTE_COUNT];
MPC_MemBlock_t * p__32_BYTE_mgr_table; // [M__32_BYTE_COUNT];
MPC_MemBlock_t * p__64_BYTE_mgr_table; // [M__64_BYTE_COUNT];
MPC_MemBlock_t * p_128_BYTE_mgr_table; // [M_128_BYTE_COUNT];
MPC_MemBlock_t * p_256_BYTE_mgr_table; // [M_256_BYTE_COUNT];
MPC_MemBlock_t * p_512_BYTE_mgr_table; // [M_512_BYTE_COUNT];
MPC_MemBlock_t * p__1K_BYTE_mgr_table; // [M__1K_BYTE_COUNT];
MPC_MemBlock_t * p__2K_BYTE_mgr_table; // [M__2K_BYTE_COUNT];
MPC_MemBlock_t * p__4K_BYTE_mgr_table; // [M__4K_BYTE_COUNT];
char * p___8_BYTE_mem_table; // [M___8_BYTE_COUNT];
char * p__16_BYTE_mem_table; // [M__16_BYTE_COUNT];
char * p__32_BYTE_mem_table; // [M__32_BYTE_COUNT];
char * p__64_BYTE_mem_table; // [M__64_BYTE_COUNT];
char * p_128_BYTE_mem_table; // [M_128_BYTE_COUNT];
char * p_256_BYTE_mem_table; // [M_256_BYTE_COUNT];
char * p_512_BYTE_mem_table; // [M_512_BYTE_COUNT];
char * p__1K_BYTE_mem_table; // [M__1K_BYTE_COUNT];
char * p__2K_BYTE_mem_table; // [M__2K_BYTE_COUNT];
char * p__4K_BYTE_mem_table; // [M__4K_BYTE_COUNT];
/*
8 byte, 16 byte, 32 byte, 64 byte, 128 byte, 256 byte, 512 byte, 1K byte, 2K byte, 4K byte, (1024 * 32 : count)
8K byte, 16K byte, 32K byte, 64K byte, 128K byte, 256K byte, 512K byte, 1M byte, 2M byte, 4M byte,
*/
// mgr table
typedef struct {
int id;
int fragmentSize;
void * startAddr;
void * endAddr;
void * BlockAddr;
int BlockSize;
int BlockCount;
void * pNext;
} MPC_MgrBlock_t;
#define MPC_MGRBLOCK_COUNT (32)
static MPC_MgrBlock_t * g_MPC_MgrBlock_Head = NULL;
static void * g_pGlobalMem = NULL;
// Add by wangyl
#define INFINITE_WAIT 0xFFFFFFFF
#define LOCK_OBJ pthread_mutex_t
#define LOCK_ATT pthread_mutexattr_t
#define NOTIFY_OBJ sem_t
#define NOTIFY_TYPE sem_t *
typedef struct {
unsigned long long threadID;
char threadName[16];
unsigned long long mallocCount; // total count.
unsigned long long mallocSize; // total size.
// unsigned long long mallocUsedSize; // no free size.
} HoldThreadInfo_t;
// add by wangyl
#define DEF_HOLD_THREAD_TABLE_LOCK static LOCK_OBJ *hHoldThreadTableLock; \
static LOCK_OBJ HoldThreadTableLock; \
static LOCK_ATT HoldThreadTableLock_att
#define CRE_HOLD_THREAD_TABLE_LOCK() hHoldThreadTableLock = &HoldThreadTableLock; \
pthread_mutexattr_init( &HoldThreadTableLock_att ); \
pthread_mutexattr_settype( &HoldThreadTableLock_att, PTHREAD_MUTEX_RECURSIVE_NP ); \
pthread_mutex_init( hHoldThreadTableLock, &HoldThreadTableLock_att )
#define DEL_HOLD_THREAD_TABLE_LOCK() pthread_mutex_destroy( hHoldThreadTableLock ); \
hHoldThreadTableLock = NULL
#define LOCK_HOLD_THREAD_INFO() pthread_mutex_lock( hHoldThreadTableLock )
#define FREE_HOLD_THREAD_INFO() pthread_mutex_unlock( hHoldThreadTableLock )
#define OWN_THREAD_ID pthread_self()
#define SELF_THREAD_ID() pthread_self()
#define HOLD_THREAD_INFO_NUM (128)
#define TaskNameSize (16) //Max size of thread name is 16 byte.
DEF_HOLD_THREAD_TABLE_LOCK;
static HoldThreadInfo_t HoldThreadInfo[HOLD_THREAD_INFO_NUM];
static int LoaderRecordStatus = 0;
static long long HoldThreadInfoCount = 0;
void *MP_Core_malloc_impl(size_t c)
{
void * result = NULL;
// return __real_malloc( c );
MPC_MgrBlock_t * pMgr = g_MPC_MgrBlock_Head;
MPC_MemBlock_t * pBlockMgr = NULL;
int i=0;
for (i=0; i<5; i++)
{
if ( c <= pMgr[i].fragmentSize )
{
pBlockMgr = (MPC_MemBlock_t *)pMgr[i].BlockAddr;
break;
}
}
int j=0;
if ( pBlockMgr )
{
for (j=0; j<pMgr[i].BlockCount; j++)
{
if ( pBlockMgr[j].isUsed == 0 )
{
pBlockMgr[j].isUsed = 1;
result = pBlockMgr[j].pAddr;
break;
}
}
}
printf(" addr : [0x%lx][%ld].\n", result, c);
return result;
}
void MP_Core_free_impl( void * memblock )
{
// __real_free( memblock );
printf("free addr : [0x%lx].\n", memblock);
MPC_MgrBlock_t * pMgr = g_MPC_MgrBlock_Head;
MPC_MemBlock_t * pBlockMgr = NULL;
int i=0;
for (i=0; i<5; i++)
{
if ( pMgr[i].startAddr <= memblock && memblock <= pMgr[i].endAddr)
{
pBlockMgr = (MPC_MemBlock_t *)pMgr[i].BlockAddr;
break;
}
}
int j=0;
if ( pBlockMgr )
{
for (j=0; j<pMgr[i].BlockCount; j++)
{
if ( pBlockMgr[j].pAddr == memblock )
{
pBlockMgr[j].isUsed = 0;
break;
}
}
}
}
extern "C" {
void *__real_malloc(size_t);
void __real_free( void * memblock );
void *MP_Core_malloc(size_t c)
{
return MP_Core_malloc_impl( c );
}
void MP_Core_free( void * memblock )
{
MP_Core_free_impl( memblock );
}
void __attribute__((constructor)) MP_Core_init()
{
//printf("call my MP_Core_init[13:58].\n");
#if 1
// alloc g_pGlobalMem;
g_pGlobalMem = __real_malloc( 256 * 1024 * 1024 );
if ( g_pGlobalMem == NULL )
{
printf("g_pGlobalMem alloc NG.\n");
return ;
}
printf("g_pGlobalMem alloc OK[14:13].\n");
// init 8 byte table.
{
// MPC_MemBlock_t * p___8_BYTE_mgr_table; // [M___8_BYTE_COUNT];
p___8_BYTE_mgr_table = (MPC_MemBlock_t *)((char *)g_pGlobalMem + sizeof(MPC_MgrBlock_t) * MPC_MGRBLOCK_COUNT);
p___8_BYTE_mem_table = (char *)p___8_BYTE_mgr_table + M___8_BYTE_COUNT;
MPC_MemBlock_t * p = p___8_BYTE_mgr_table;
评论0