Skip to content

Commit

Permalink
Merge branch 'sverker/stub-module-code-leakage' into dev
Browse files Browse the repository at this point in the history
* sverker/stub-module-code-leakage:
  Prevent valgrind warning for erts_alloc_permanent_cache_aligned
  [erts] Fix memory leak in erts_make_stub_module (hipe loading)
  • Loading branch information
garazdawi committed Sep 29, 2011
2 parents 1be1c1c + b75eb2e commit beaa58d
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 34 deletions.
37 changes: 19 additions & 18 deletions erts/emulator/beam/beam_load.c
Expand Up @@ -634,6 +634,25 @@ bin_load(Process *c_p, ErtsProcLocks c_p_locks,
goto load_error;
}

/*
* Initialize code area.
*/
state.code_buffer_size = erts_next_heap_size(2048 + state.num_functions, 0);
state.code = (BeamInstr *) erts_alloc(ERTS_ALC_T_CODE,
sizeof(BeamInstr) * state.code_buffer_size);

state.code[MI_NUM_FUNCTIONS] = state.num_functions;
state.ci = MI_FUNCTIONS + state.num_functions + 1;

state.code[MI_ATTR_PTR] = 0;
state.code[MI_ATTR_SIZE] = 0;
state.code[MI_ATTR_SIZE_ON_HEAP] = 0;
state.code[MI_COMPILE_PTR] = 0;
state.code[MI_COMPILE_SIZE] = 0;
state.code[MI_COMPILE_SIZE_ON_HEAP] = 0;
state.code[MI_NUM_BREAKPOINTS] = 0;


/*
* Read the atom table.
*/
Expand Down Expand Up @@ -1363,24 +1382,6 @@ read_code_header(LoaderState* stp)
#endif
}

/*
* Initialize code area.
*/
stp->code_buffer_size = erts_next_heap_size(2048 + stp->num_functions, 0);
stp->code = (BeamInstr *) erts_alloc(ERTS_ALC_T_CODE,
sizeof(BeamInstr) * stp->code_buffer_size);

stp->code[MI_NUM_FUNCTIONS] = stp->num_functions;
stp->ci = MI_FUNCTIONS + stp->num_functions + 1;

stp->code[MI_ATTR_PTR] = 0;
stp->code[MI_ATTR_SIZE] = 0;
stp->code[MI_ATTR_SIZE_ON_HEAP] = 0;
stp->code[MI_COMPILE_PTR] = 0;
stp->code[MI_COMPILE_SIZE] = 0;
stp->code[MI_COMPILE_SIZE_ON_HEAP] = 0;
stp->code[MI_NUM_BREAKPOINTS] = 0;

stp->new_bs_put_strings = 0;
stp->catches = 0;
return 1;
Expand Down
25 changes: 23 additions & 2 deletions erts/emulator/beam/erl_alloc.c
Expand Up @@ -2960,6 +2960,29 @@ erts_allocator_options(void *proc)
return res;
}

void *erts_alloc_permanent_cache_aligned(ErtsAlcType_t type, Uint size)
{
UWord v = (UWord) erts_alloc(type, size + (ERTS_CACHE_LINE_SIZE-1));

#ifdef VALGRIND
{ /* Avoid Leak_PossiblyLost */
static UWord vg_root_set[10];
static unsigned ix = 0;
if (ix >= sizeof(vg_root_set) / sizeof(*vg_root_set)) {
erl_exit(ERTS_ABORT_EXIT, "Too many erts_alloc_permanent_cache_aligned's\n");
}
vg_root_set[ix++] = v; /* not thread safe */
}
#endif

if (v & ERTS_CACHE_LINE_MASK) {
v = (v & ~ERTS_CACHE_LINE_MASK) + ERTS_CACHE_LINE_SIZE;
}
ASSERT((v & ERTS_CACHE_LINE_MASK) == 0);
return (void*)v;
}


/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
* Deprecated functions *
* *
Expand Down Expand Up @@ -3583,6 +3606,4 @@ install_debug_functions(void)
return FENCE_SZ;
}



#endif /* #ifdef DEBUG */
16 changes: 2 additions & 14 deletions erts/emulator/beam/erl_alloc.h
Expand Up @@ -180,11 +180,11 @@ void *erts_realloc(ErtsAlcType_t type, void *ptr, Uint size);
void erts_free(ErtsAlcType_t type, void *ptr);
void *erts_alloc_fnf(ErtsAlcType_t type, Uint size);
void *erts_realloc_fnf(ErtsAlcType_t type, void *ptr, Uint size);
void *erts_alloc_permanent_cache_aligned(ErtsAlcType_t type, Uint size);


#endif /* #if !ERTS_ALC_DO_INLINE */

void *erts_alloc_permanent_cache_aligned(ErtsAlcType_t type, Uint size);

#ifndef ERTS_CACHE_LINE_SIZE
/* Assume a cache line size of 64 bytes */
# define ERTS_CACHE_LINE_SIZE ((UWord) 64)
Expand Down Expand Up @@ -250,18 +250,6 @@ void *erts_realloc_fnf(ErtsAlcType_t type, void *ptr, Uint size)
size);
}

ERTS_ALC_INLINE
void *erts_alloc_permanent_cache_aligned(ErtsAlcType_t type, Uint size)
{
UWord v = (UWord) erts_alloc(type, size + (ERTS_CACHE_LINE_SIZE-1));

if (v & ERTS_CACHE_LINE_MASK) {
v = (v & ~ERTS_CACHE_LINE_MASK) + ERTS_CACHE_LINE_SIZE;
}
ASSERT((v & ERTS_CACHE_LINE_MASK) == 0);
return (void*)v;
}

#endif /* #if ERTS_ALC_DO_INLINE || defined(ERTS_ALC_INTERNAL__) */

typedef void (*erts_alloc_verify_func_t)(Allctr_t *);
Expand Down

0 comments on commit beaa58d

Please sign in to comment.