/************************************************************************
 * AUTHOR: NiuJiuRu
 * FILENAME: swmem.c
 * DESCRIPTION: 可分块/可跟踪内存泄漏位置的内存管理
 * NOTE:
 * HISTORY:
 * 1, [2010-09-06] created by NiuJiuRu
 * 2, [2013-05-30] modified by NiuJiuRu, 优化"sw_mem_alloc()"函数, 修改
 *                 分配顺序(整个缓冲全部分配完毕后,再在节点空隙间分配),
 *                 提高内存分配的速度
 ***********************************************************************/
#include "swapi.h"
#include "swdir.h"
#include "swmutex.h"
#include "swmem.h"

#if defined _DEBUG && !defined _MEM_DEBUG_DETAIL
#define _MEM_DEBUG_DETAIL
#endif

// 判断对齐粒度是否有效(2的n次方 n >= 0)
#define ALIGN_VALID(align) ((align) > 0 ? (((align) & (~(align)+1)) == (align) ? true : false) : false)

// 整数对齐, align = 1, 2, 4, 8, 16, ...
#define ALIGN_SIZE(size, align) (((size)+((align)-1)) & ~((align)-1))

// 指针对齐, align = 1, 2, 4, 8, 16, ...
#define ALIGN_PTR(ptr, size, align) (((long)(ptr)+(size)+((align)-1)) & ~((align)-1))

typedef struct _MemNode_
{
#ifdef _MEM_DEBUG_DETAIL
	char filename[MAX_PATH_CHARS];
	int line;
#endif
	int size;
	struct _MemNode_ *next;
} SMemNode;

typedef struct
{
	// 工作缓冲区
	char *buf;
	// 工作缓冲区大小
	int size;
	// 内存对齐字节数
	int align;
	// 访问工作缓冲区的互斥锁
	void *hMutex;
	
	// 空间分配链表
	SMemNode *list;	
	// 最前的查找块结点(遍历分配内存使用)
	SMemNode *first;
	// 最后的查找块结点(遍历分配内存使用)
	SMemNode *tail;
	
	// 历史上最大分配过的内存块大小(字节)
	int history_maxsize;	
} SMemHeader;

/* 创建内存管理模块, align = 1, 2, 4, 8, 16, ... */
void *sw_mem_create(char *buf, int size, int align)
{
	SMemHeader *xm = NULL;
	
	if(!buf || size <= 0 || !ALIGN_VALID(align)) goto ErrorP;
	
	xm = (SMemHeader *)ALIGN_PTR(buf, 0, align);
	memset(xm, 0, sizeof(SMemHeader));
	xm->align = align;
	xm->buf = (char *)ALIGN_PTR(xm, sizeof(SMemHeader), xm->align);
	xm->size = buf+size-xm->buf;
	xm->hMutex = sw_mutex_create();
	if(!xm->hMutex) goto ErrorP;
	
	return xm;
	
ErrorP:
	sw_mem_destroy(xm);
	return NULL;	
}

/* 销毁内存管理模块 */
void sw_mem_destroy(void *hMem)
{
	SMemHeader *xm = (SMemHeader *)hMem;
	
	if(!xm) return;
	
	if(xm->hMutex)
	{
		sw_mutex_destroy(xm->hMutex);
	}
	
	memset(xm, 0, sizeof(SMemHeader));
}

/* 复位内存管理模块 */
void sw_mem_reset(void *hMem)
{
	SMemHeader *xm = (SMemHeader *)hMem;
	
	if(!xm) return;
	
	sw_mutex_lock(xm->hMutex, WAIT_FOREVER); // lock
	xm->list = NULL;
	xm->first = NULL;
	xm->tail = NULL;
	xm->history_maxsize = 0;
	sw_mutex_unlock(xm->hMutex); // unlock
}

/* 检查内存节点, 并返回历史上最大消耗过的内存数 */
int sw_mem_check(const void *hMem, PMemCheckCallback proc, void *lpParameter)
{
	SMemHeader *xm = (SMemHeader *)hMem;
	SMemNode *node = NULL;
	char *ptr = NULL;
	
	if(!xm) return -1;
	
	node = xm->list;
	while(node && proc)
	{
		ptr = (char *)ALIGN_PTR(node, sizeof(SMemNode), xm->align);
#ifdef _MEM_DEBUG_DETAIL
		proc(xGetPathFileName(node->filename), node->line, ptr, node->size, lpParameter);
#else
		proc("", -1, ptr, node->size, lpParameter);
#endif
		node = node->next;
	}
	
	return xm->history_maxsize;
}

/* 获得总的内存大小 */
int sw_mem_getTotalSize(const void *hMem)
{
	SMemHeader *xm = (SMemHeader *)hMem;
	if(!xm) return -1;
	return xm->size;
}

// 查找内存节点
static SMemNode *sw_mem_find(const void *hMem, void *ptr)
{
	SMemHeader *xm = (SMemHeader *)hMem;
	SMemNode *node = NULL;
	
	if(!xm || !ptr) return NULL;
	
	node = xm->list;
	while(node)
	{
		if(ptr == (void *)ALIGN_PTR(node, sizeof(SMemNode), xm->align)) break;
		node = node->next;
	}
	
	return node;
}

// 扫描现有的内存节点链表, 查看是否有符合申请条件的节点间空隙-遍历分配
static void *sw_mem_visitAllocGap(void *hMem, int size, const char *filename, int line)
{
	SMemHeader *xm = (SMemHeader *)hMem;
	SMemNode *node = NULL, *new_node = NULL;
	char *ptr = NULL;
	int gapLen = 0, firstGapLen = 0;
	
	if(!xm) return NULL;
	
	// 1, 检查链表头和buf之间的空隙
	if(xm->first == NULL)
	{
		if(xm->list == NULL) return NULL;
		
		node = xm->list; // 首节点
		firstGapLen = ((char *)node) - (char *)ALIGN_PTR(xm->buf, sizeof(SMemNode), xm->align);
		if(size <= firstGapLen)
		{ // 找到了符合申请要求的内存空间
			new_node = (SMemNode *)xm->buf;
			memset(new_node, 0, sizeof(SMemNode));
#ifdef _MEM_DEBUG_DETAIL
			if(filename)
			{
				strncpy(new_node->filename, filename, sizeof(new_node->filename)-1);
				new_node->line = line;
			}
#endif
			new_node->size = size;
			
			xm->list = new_node;
			new_node->next = node;
			xm->first = new_node;
			
			return (char *)ALIGN_PTR(new_node, sizeof(SMemNode), xm->align);
		}
	}
	else
	{
		node = xm->first; // 查找空闲内存, 从"xm->first"处开始查找, 而不是从"xm->list"处
		firstGapLen = 0;
	}
	
	// 2, 检查相临两块之间的空隙
	while(node->next != NULL)
	{
		ptr = (char *)ALIGN_PTR(node, sizeof(SMemNode), xm->align) + ALIGN_SIZE(node->size, xm->align);
		gapLen = ((char*)node->next) - (char *)ALIGN_PTR(ptr, sizeof(SMemNode), xm->align);
		
		if(firstGapLen < xm->align)
		{ // 如果第一间隔空隙太小, 不够最低分配字节数, 就修改"xm->first"指针
			xm->first = node;
			firstGapLen = gapLen;
		}
		
		if(size <= gapLen)
		{ // 找到了符合申请要求的内存空间
			new_node = (SMemNode *)ptr;
			memset(new_node, 0, sizeof(SMemNode));
#ifdef _MEM_DEBUG_DETAIL
			if(filename)
			{
				strncpy(new_node->filename, filename, sizeof(new_node->filename)-1);
				new_node->line = line;
			}
#endif
			new_node->size = size;
			
			new_node->next = node->next;
			node->next = new_node;
			
			return (char *)ALIGN_PTR(new_node, sizeof(SMemNode), xm->align);
		}
		
		node = node->next;
	}
	
	return NULL;
}

/* 分配内存 */
void *sw_mem_alloc(void *hMem, int size, const char *filename, int line)
{
	SMemHeader *xm = (SMemHeader *)hMem;
	SMemNode *new_node = NULL;
	char *ptr = NULL;
	
	if(!xm || size < 0) return NULL;
	
	sw_mutex_lock(xm->hMutex, WAIT_FOREVER); // lock
	
	// 1, 新建内存节点
	if(xm->list == NULL) new_node = (SMemNode *)xm->buf; // 第一次申请
	else new_node = (SMemNode *)((char *)ALIGN_PTR(xm->tail, sizeof(SMemNode), xm->align) + ALIGN_SIZE(xm->tail->size, xm->align));
	ptr = (char *)ALIGN_PTR(new_node, sizeof(SMemNode), xm->align); // 指向新节点buf的开始位置
	
	// 2, 剩余内存空间不足时, 尝试在现有内存节点间的空隙中分配
	if((int)(xm->buf+xm->size-ptr) < size)
	{
		ptr = NULL;
		if(!ptr) ptr = (char *)sw_mem_visitAllocGap(xm, size, filename, line); // 遍历分配
		if(ptr)
		{
			if(xm->history_maxsize < (ptr-xm->buf)+size) xm->history_maxsize = (ptr-xm->buf)+size;
			sw_mutex_unlock(xm->hMutex); // unlock
			return ptr; // return a void pointer to the allocated space
		}

		sw_log_error("sorry, alloc memory fail, because %p memory container is full!!", xm);
		sw_mutex_unlock(xm->hMutex); // unlock
		return NULL;
	}
	
	// 3, 填充该内存节点
	memset(new_node, 0, sizeof(SMemNode));
#ifdef _MEM_DEBUG_DETAIL
	if(filename)
	{
		strncpy(new_node->filename, filename, sizeof(new_node->filename)-1);
		new_node->line = line;
	}
#endif
	new_node->size = size;
	
	if(xm->list == NULL) xm->list = new_node;
	else xm->tail->next = new_node;
	
	xm->tail = new_node;
	
	if(xm->history_maxsize < (ptr-xm->buf)+size) xm->history_maxsize = (ptr-xm->buf)+size;
	
	sw_mutex_unlock(xm->hMutex); // unlock
	
	// 4, return a void pointer to the allocated space
	return ptr;
}

/* 分配内存并初始化为"0x00" */
void *sw_mem_calloc(void *hMem, int size, const char *filename, int line)
{
	SMemHeader *xm = (SMemHeader *)hMem;
	char *ptr = NULL;
	
	ptr = sw_mem_alloc(xm, size, filename, line);
	if(ptr) memset(ptr, 0, size);
	
	return ptr;
}

/* 复制字符串 */
char *sw_mem_strdup(void *hMem, const char *str, const char *filename, int line)
{
	SMemHeader *xm = (SMemHeader *)hMem;
	char *ptr = NULL;
	
	if(str) ptr = sw_mem_alloc(xm, strlen(str)+1, filename, line);
	if(ptr) memcpy(ptr, str, strlen(str)+1);
	
	return ptr;
}

/* 重新申请内存 */
void *sw_mem_realloc(void *hMem, void *ptr, int size, const char *filename, int line)
{
	SMemHeader *xm = (SMemHeader *)hMem;
	SMemNode *node = NULL;
	char *new_ptr = NULL;

	sw_mutex_lock(xm->hMutex, WAIT_FOREVER); // lock
	node = sw_mem_find(xm, ptr);
	if(node && (size <= node->size || \
		(long)((char *)ALIGN_PTR(node, sizeof(SMemNode), xm->align)+size) <= (long)(node->next ? node->next : (SMemNode *)(xm->buf+xm->size))))
	{
		node->size = size;
		sw_mutex_unlock(xm->hMutex); // unlock
		return ptr;
	}
	sw_mutex_unlock(xm->hMutex); // unlock
	
	new_ptr = sw_mem_alloc(xm, size, filename, line);
	if(new_ptr && ptr)
	{
		memcpy(new_ptr, ptr, node ? node->size : size);
		sw_mem_free(xm, ptr, filename, line);
	}

	return new_ptr;
}

/* 释放内存 */
void sw_mem_free(void *hMem, void *ptr, const char *filename, int line)
{
	SMemHeader *xm = (SMemHeader *)hMem;
	SMemNode *node = NULL, *previous = NULL;

	if(!xm) return;
	
	sw_mutex_lock(xm->hMutex, WAIT_FOREVER); // lock
	
	// 寻找该节点
	for(node = xm->list; node; node = node->next)
	{
		if(ptr == (void *)ALIGN_PTR(node, sizeof(SMemNode), xm->align)) break;
		previous = node;
	}
	if(!node) goto EndP;
	
	// 在内存节点链上断开该节点
	if(node == xm->list) xm->list = node->next; // 释放的为首节点
	else previous->next = node->next;
		
	// 调整"最前的查找块结点"和"最后的查找块结点"指针
	if(xm->first >= node) xm->first = previous;
	if(xm->tail == node) xm->tail = previous;
	
EndP:
	sw_mutex_unlock(xm->hMutex); // unlock
}
