allocators, config: add CONFIG_OVERRUN_PROTECTION - allows disabling the lengthy work done by OverrunProtector

h_mgr: clarification of fn_store code
mem.cpp: fix recursive taking of a lock

This was SVN commit r3048.
This commit is contained in:
janwas 2005-10-29 21:40:41 +00:00
parent f323726118
commit a7d6725f45
4 changed files with 24 additions and 7 deletions

View File

@ -152,13 +152,17 @@ public:
void lock()
{
#if CONFIG_OVERRUN_PROTECTION
da_set_prot(&da, PROT_NONE);
#endif
}
private:
void unlock()
{
#if CONFIG_OVERRUN_PROTECTION
da_set_prot(&da, PROT_READ|PROT_WRITE);
#endif
}
void init()

View File

@ -26,6 +26,16 @@
# endif
#endif
// this enables/disables the actual checking done by OverrunProtector-s
// (quite slow, entailing mprotect() before/after each access).
// define to 1 here or in the relevant module if you suspect mem corruption.
// we provide this option because OverrunProtector requires some changes to
// the object being wrapped, and we want to leave those intact but not
// significantly slow things down except when needed.
#ifndef CONFIG_OVERRUN_PROTECTION
# define CONFIG_OVERRUN_PROTECTION 0
#endif
// enable memory tracking (slow). see mmgr.cpp.
#ifndef CONFIG_USE_MMGR
# define CONFIG_USE_MMGR 0

View File

@ -323,6 +323,8 @@ static int free_idx(i32 idx)
}
//-----------------------------------------------------------------------------
// lookup data structure
//-----------------------------------------------------------------------------
// speed up h_find (called every h_alloc)
@ -420,21 +422,21 @@ static void fn_store(HDATA* hd, const char* fn)
{
ONCE(fn_init());
const size_t len = strlen(fn);
const size_t size = strlen(fn)+1;
hd->fn = 0;
// stuff it in unused space at the end of HDATA
if(hd->type->user_size + len < HDATA_USER_SIZE)
if(hd->type->user_size+size <= HDATA_USER_SIZE)
hd->fn = (const char*)hd->user + hd->type->user_size;
else if(len+1 <= FN_POOL_EL_SIZE)
else if(size <= FN_POOL_EL_SIZE)
hd->fn = (const char*)pool_alloc(&fn_pool);
// in case none of the above applied and/or were successful:
// fall back to heap alloc.
if(!hd->fn)
{
debug_printf("H_MGR| very long filename (%d) %s\n", len, fn);
hd->fn = (const char*)malloc(len+1);
debug_printf("H_MGR| very long filename (%d) %s\n", size, fn);
hd->fn = (const char*)malloc(size);
// still failed - bail (avoid strcpy to 0)
if(!hd->fn)
{

View File

@ -203,6 +203,7 @@ static int Mem_to_string(const Mem* m, char* buf)
//////////////////////////////////////////////////////////////////////////////
// implementation must be thread-safe! (since mem_alloc doesn't take a lock)
static void heap_free(void* raw_p, size_t UNUSED(raw_size), uintptr_t UNUSED(ctx))
{
@ -300,10 +301,10 @@ int mem_assign_user(Handle hm, void* user_p, size_t user_size)
*/
// implementation note: does not currently take a lock; all the
// heavy-lifting happens inside mem_wrap.
void* mem_alloc(size_t size, const size_t align, uint flags, Handle* phm)
{
SCOPED_LOCK;
if(phm)
*phm = ERR_NO_MEM;