+added friendly error promption when memory overflow.

This commit is contained in:
paladin-t 2016-01-26 11:39:56 +08:00
parent c1705c6a54
commit 25d76bb579
3 changed files with 14 additions and 4 deletions

View File

@ -1,4 +1,5 @@
Jan. 26 2016 Jan. 26 2016
Added friendly error promption when memory overflow
Added source file information to stepped handler Added source file information to stepped handler
Optimized cached list accessing Optimized cached list accessing

View File

@ -1078,7 +1078,7 @@ static int _ht_remove_exist(void* data, void* extra, _ht_node_t* ht);
} while(0) } while(0)
/** Memory manipulations */ /** Memory manipulations */
#define _MB_CHECK_MEM_TAG_SIZE(y, s) do { mb_mem_tag_t _tmp = (mb_mem_tag_t)s; if((y)_tmp != s) { mb_assert(0 && "\"mb_mem_tag_t\" is too small."); printf("The type \"mb_mem_tag_t\" is not precise enough to hold the given data, please redefine it!"); } } while(0) #define _MB_CHECK_MEM_TAG_SIZE(y, s) ((mb_mem_tag_t)(s) == (s))
#define _MB_WRITE_MEM_TAG_SIZE(t, s) (*((mb_mem_tag_t*)((char*)(t) - _MB_MEM_TAG_SIZE)) = (mb_mem_tag_t)s) #define _MB_WRITE_MEM_TAG_SIZE(t, s) (*((mb_mem_tag_t*)((char*)(t) - _MB_MEM_TAG_SIZE)) = (mb_mem_tag_t)s)
#define _MB_READ_MEM_TAG_SIZE(t) (*((mb_mem_tag_t*)((char*)(t) - _MB_MEM_TAG_SIZE))) #define _MB_READ_MEM_TAG_SIZE(t) (*((mb_mem_tag_t*)((char*)(t) - _MB_MEM_TAG_SIZE)))
@ -2625,7 +2625,8 @@ void* mb_malloc(size_t s) {
char* ret = NULL; char* ret = NULL;
size_t rs = s; size_t rs = s;
#ifdef MB_ENABLE_ALLOC_STAT #ifdef MB_ENABLE_ALLOC_STAT
_MB_CHECK_MEM_TAG_SIZE(size_t, s); if(!_MB_CHECK_MEM_TAG_SIZE(size_t, s))
return 0;
rs += _MB_MEM_TAG_SIZE; rs += _MB_MEM_TAG_SIZE;
#endif /* MB_ENABLE_ALLOC_STAT */ #endif /* MB_ENABLE_ALLOC_STAT */
if(_mb_allocate_func) if(_mb_allocate_func)

View File

@ -317,7 +317,11 @@ static char* _pop_mem(unsigned s) {
} else { } else {
/* Create a new node */ /* Create a new node */
result = _POOL_NODE_ALLOC(pl->size); result = _POOL_NODE_ALLOC(pl->size);
if((intptr_t)result == sizeof(_pool_tag_t)) {
result = 0;
} else {
_POOL_NODE_SIZE(result) = (_pool_chunk_size_t)s; _POOL_NODE_SIZE(result) = (_pool_chunk_size_t)s;
}
return result; return result;
} }
@ -327,7 +331,11 @@ static char* _pop_mem(unsigned s) {
/* Allocate directly */ /* Allocate directly */
result = _POOL_NODE_ALLOC(s); result = _POOL_NODE_ALLOC(s);
if((intptr_t)result == sizeof(_pool_tag_t)) {
result = 0;
} else {
_POOL_NODE_SIZE(result) = (_pool_chunk_size_t)s; _POOL_NODE_SIZE(result) = (_pool_chunk_size_t)s;
}
return result; return result;
} }