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

📄 unx_memmgr.c

📁 smallbasic for linux
💻 C
📖 第 1 页 / 共 2 页
字号:
	strcpy(err_module, file);	err_line = line;	if	( (h < 1) || (h > umm_count) )		uuerr(1,"\n\aMemMgr: MemHandleResize(h), invalid handle\n");	if	( memtable[h-1].deleted || (memtable[h-1].ptr == NULL) )			uuerr(1,"\n\aMemMgr: MemHandleResize(h), deleted block\n");	if	( memtable[h-1].lock )		uuerr(1, "\n\aMemMgr: MemHandleResize(): handle is locked\n");	if	( new_size <= 0 )		uuerr(1, "\n\aMemMgr: MemHandleResize(): size <= 0\n");#endif	cur_alloc -= memtable[h-1].size;	np = (char *) malloc(new_size);	memcpy(np, memtable[h-1].ptr, (new_size < memtable[h-1].size) ? new_size : memtable[h-1].size );	free(memtable[h-1].ptr);	memtable[h-1].ptr = np;	memtable[h-1].size = new_size;	cur_alloc += new_size;	return	0;}/**	allocate a memory block of dynamic-RAM*/MemPtr	MemPtrNewX(int size, const char *file, int line){	MemHandle	h;#if defined(CHECK_PTRS_LEV2)	strcpy(err_module, file);	err_line = line;	uuerr((size<=0),"\nWarning: MemPtrNew(%d)\n", size);#endif	h = MemHandleNewX(size, file, line);	return MemHandleLockX(h, file, line);}/**	deallocate a memory block of dynamic-RAM*/void	MemPtrFreeX(MemPtr p, const char *file, int line){	int	i;#if defined(CHECK_PTRS_LEV2)	strcpy(err_module, file);	err_line = line;	uuerr(!p, "\n\aMemMgr: MemPtrFree(p), pointer is NULL\n");#endif	for ( i = 0; i < umm_count; i ++ )	{		if	( memtable[i].ptr == p )	{			MemHandleUnlockX(i+1, file, line);			MemHandleFreeX(i+1, file, line);			return;			}		}	uuerr(1, "\n\aMemMgr: MemPtrFree(ptr), ptr does not exists\n");}/**/int		iCheckSize(const MemPtr p){	int		i;	uuerr(!p, "\n\aMemMgr: iCheckSize(p), pointer is NULL\n");	for ( i = 0; i < umm_count; i ++ )	{		if	( memtable[i].ptr == p )				return memtable[i].size;		else if ( (p >= memtable[i].ptr) && ( p <= (memtable[i].ptr + memtable[i].size)) )				return memtable[i].size - (p - memtable[i].ptr);		}	return MAX_UNDEF_SIZE;	// //	return 0x100000;	// 1MB}/**/void	CheckPtr(const void *p, int inside){	int		i;	uuerr(!p, "\n\aMemMgr: CheckPtr(p), pointer is NULL\n");	for ( i = 0; i < umm_count; i ++ )	{		if	( memtable[i].ptr == p )				return;		else if ( inside )	{			if	( (p >= memtable[i].ptr) && ( p <= (memtable[i].ptr + memtable[i].size)) )					return;			}		}	uuerr(1, "\n\aMemMgr: CheckPtr(p), pointer is out of allocated space\n");}/**	returns the size of the memory block (dynamic-RAM)*/int		MemPtrSizeX(const MemPtr p, const char *file, int line){	int		i;#if defined(CHECK_PTRS_LEV2)	strcpy(err_module, file);	err_line = line;	uuerr(!p, "\n\aMemMgr: MemPtrSize(p), pointer is NULL\n");#endif	for ( i = 0; i < umm_count; i ++ )	{		if	( memtable[i].ptr == p )				return memtable[i].size;		}	xprintf("\n\aMemMgr: MemPtrSize(ptr), ptr does not exists\n");	return 0;}/* =================================================================================*	mem*/void	MemMoveX(void *dst, const void *src, int size, const char *file, int line){#if defined(CHECK_PTRS_LEV2)	strcpy(err_module, file);	err_line = line;	uuerr(!src, "\n\aMemMgr: MemMove(dst,src), src is NULL\n");	uuerr(!dst, "\n\aMemMgr: MemMove(dst,src), dst is NULL\n");	if	( iCheckSize((MemPtr) dst) < size )		uuerr(1, "\n\aMemMgr: MemMove(dst,src), dst size error\n");	if	( iCheckSize((MemPtr) src) < size )		uuerr(1, "\n\aMemMgr: MemMove(dst,src), src size error\n");#endif	memcpy(dst, src, size);}void	MemSetX(void *dst, int size, int val, const char *file, int line){#if defined(CHECK_PTRS_LEV2)	strcpy(err_module, file);	err_line = line;	uuerr(!dst, "\n\aMemMgr: MemSet(dst), dst is NULL\n");	if	( iCheckSize(dst) < size )		uuerr(1, "\n\aMemMgr: MemSet(), dst size error\n");#endif	memset(dst, val, size);}int		MemCmpX(const void *p1, const void *p2, int n, const char *file, int line){#if defined(CHECK_PTRS_LEV2)	strcpy(err_module, file);	err_line = line;	uuerr(!p1, "\n\aMemMgr: MemCmp(p1,p2,n), p1 is NULL\n");	uuerr(!p2, "\n\aMemMgr: MemCmp(p1,p2,n), p2 is NULL\n");	uuerr((n<=0), "\n\aMemMgr: MemCmp(p1,p2,n), n <= 0\n");#endif	return memcmp(p1,p2,n);}/* ======================================================================================*	string*/int		StrLenX(const char *s, const char *file, int line){#if defined(CHECK_PTRS_LEV2)	strcpy(err_module, file);	err_line = line;	uuerr(!s, "\n\aMemMgr: StrLen(s), s is NULL\n");	if	( strlen(s) > iCheckSize(s) )		uuerr(1, "\n\aMemMgr: StrLen(s), s is strange\n");#endif	return strlen(s);}char*	StrCopyX(char *dst, const char *src, const char *file, int line){#if defined(CHECK_PTRS_LEV2)	strcpy(err_module, file);	err_line = line;	uuerr(!src, "\n\aMemMgr: StrCopy(dst,src), src is NULL\n");	uuerr(!dst, "\n\aMemMgr: StrCopy(dst,src), dst is NULL\n");	if	( iCheckSize(dst) <= strlen(src) )		uuerr(1, "\n\aMemMgr: StrCopy(dst,src), dst size error\n");#endif	return strcpy(dst, src);}char*	StrCatX(char *dst, const char *src, const char *file, int line){#if defined(CHECK_PTRS_LEV2)	strcpy(err_module, file);	err_line = line;	uuerr(!src, "\n\aMemMgr: StrCat(dst,src), src is NULL\n");	uuerr(!dst, "\n\aMemMgr: StrCat(dst,src), dst is NULL\n");	if	( iCheckSize(dst) < (strlen(src)+strlen(dst)+1) )		uuerr(1, "\n\aMemMgr: StrCat(dst,src), dst is small\n");#endif	return strcat(dst, src);}int		StrCompareX(const char *a, const char *b, const char *file, int line){#if defined(CHECK_PTRS_LEV2)	strcpy(err_module, file);	err_line = line;	uuerr(!a, "\n\aMemMgr: StrCompare(a,b), a is NULL\n");	uuerr(!b, "\n\aMemMgr: StrCompare(a,b), b is NULL\n");#endif	return strcmp(a,b);}char*	StrChrX(const char *src, int c, const char *file, int line){#if defined(CHECK_PTRS_LEV2)	strcpy(err_module, file);	err_line = line;	uuerr(!src, "\n\aMemMgr: StrChr(src), src is NULL\n");#endif	return strchr(src, c);}int		StrCaselessCompareX(const char *s1, const char *s2, const char *file, int line){#if defined(CHECK_PTRS_LEV2)	strcpy(err_module, file);	err_line = line;	uuerr(!s1, "\n\aMemMgr: StrCaselessCompare(s1,s2), s1 is NULL\n");	uuerr(!s2, "\n\aMemMgr: StrCaselessCompare(s1,s2), s1 is NULL\n");#endif	#if defined(_BCB_W32_IDE) || defined(_Win32)	return stricmp(s1, s2);	#else	return strcasecmp(s1, s2);	#endif}int		StrNCaselessCompareX(const char *s1, const char *s2, int n, const char *file, int line){#if defined(CHECK_PTRS_LEV2)	strcpy(err_module, file);	err_line = line;	uuerr(!s1, "\n\aMemMgr: StrNCaselessCompare(s1,s2,n), s1 is NULL\n");	uuerr(!s2, "\n\aMemMgr: StrNCaselessCompare(s1,s2,n), s1 is NULL\n");	uuerr((n <= 0), "\n\aMemMgr: StrNCaselessCompare(s1,s2,n), n<=0\n");#endif	#if defined(_BCB_W32_IDE) || defined(_Win32)	return strnicmp(s1, s2, n);	#else	return strncasecmp(s1, s2, n);	#endif}int		StrNCaselessCompareSM(const char *s1, const char *s2, int n){	#if defined(_BCB_W32_IDE) || defined(_Win32)	return strnicmp(s1, s2, n);	#else	return strncasecmp(s1, s2, n);	#endif}char*	StrNCatX(char *dst, const char *src, int n, const char *file, int line){#if defined(CHECK_PTRS_LEV2)	strcpy(err_module, file);	err_line = line;	uuerr(!src, "\n\aMemMgr: StrNCat(dst,src,n), src is NULL\n");	uuerr(!dst, "\n\aMemMgr: StrNCat(dst,src,n), dst is NULL\n");	uuerr((n <= 0), "\n\aMemMgr: StrNCat(dst,src,n), n<=0\n");	if	( MemPtrSizeX(dst, file, line) < n )		uuerr(1, "\n\aMemMgr: StrNCat(), dst is small\n");#endif	return strncat(dst, src, n);}int		StrNCompareX(const char *a, const char *b, int n, const char *file, int line){#if defined(CHECK_PTRS_LEV2)	strcpy(err_module, file);	err_line = line;	uuerr(!a, "\n\aMemMgr: StrNCompare(a,b,n), a is NULL\n");	uuerr(!b, "\n\aMemMgr: StrNCompare(a,b,n), b is NULL\n");	uuerr((n <= 0), "\n\aMemMgr: StrNCompare(a,b,n), n<=0\n");#endif	return strncmp(a, b, n);}char*	StrNCopyX(char *dst, const char *src, int n, const char *file, int line){#if defined(CHECK_PTRS_LEV2)	strcpy(err_module, file);	err_line = line;	uuerr(!src, "\n\aMemMgr: StrNCopy(dst,src,n), src is NULL\n");	uuerr(!dst, "\n\aMemMgr: StrNCopy(dst,src,n), dst is NULL\n");	uuerr((n <= 0), "\n\aMemMgr: StrNCopy(dst,src,n), n<=0\n");#endif	return strncpy(dst, src, n);}char*	StrStrX(const char *s1, const char *s2, const char *file, int line){#if defined(CHECK_PTRS_LEV2)	strcpy(err_module, file);	err_line = line;	uuerr(!s1, "\n\aMemMgr: StrStr(s1,s2), s1 is NULL\n");	uuerr(!s2, "\n\aMemMgr: StrStr(s1,s2), s2 is NULL\n");#endif	return strstr(s1, s2);}char*	StrRevChrX(const char *src, int c, const char *file, int line){#if defined(CHECK_PTRS_LEV2)	strcpy(err_module, file);	err_line = line;	uuerr(!src, "\n\aMemMgr: StrChr(src), src is NULL\n");#endif	return strrchr(src, c);}char*		StrErrorX(int err, const char *file, int line){#if defined(CHECK_PTRS_LEV2)	strcpy(err_module, file);	err_line = line;#endif	return strerror(err);}

⌨️ 快捷键说明

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