Skip to content

Revert "Prefer calloc over of malloc+zeroMemory. NFC" #22568

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/library_dylink.js
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ var LibraryDylink = {
// Allocate memory even if malloc isn't ready yet. The allocated memory here
// must be zero initialized since its used for all static data, including bss.
$getMemory__noleakcheck: true,
$getMemory__deps: ['$GOT', '__heap_base', '$alignMemory', 'calloc'],
$getMemory__deps: ['$GOT', '__heap_base', '$zeroMemory', '$alignMemory', 'malloc'],
$getMemory: (size) => {
// After the runtime is initialized, we must only use sbrk() normally.
#if DYLINK_DEBUG
Expand All @@ -373,7 +373,7 @@ var LibraryDylink = {
// Currently we don't support freeing of static data when modules are
// unloaded via dlclose. This function is tagged as `noleakcheck` to
// avoid having this reported as leak.
return _calloc(size, 1);
return zeroMemory(_malloc(size), size);
}
var ret = ___heap_base;
// Keep __heap_base stack aligned.
Expand Down Expand Up @@ -599,7 +599,7 @@ var LibraryDylink = {
$loadWebAssemblyModule__deps: [
'$loadDynamicLibrary', '$getMemory',
'$relocateExports', '$resolveGlobalSymbol', '$GOTHandler',
'$getDylinkMetadata', '$alignMemory',
'$getDylinkMetadata', '$alignMemory', '$zeroMemory',
'$currentModuleWeakSymbols',
'$updateTableMap',
'$wasmTable',
Expand Down
2 changes: 1 addition & 1 deletion src/library_pthread.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ var LibraryPThread = {
$PThread__postset: 'PThread.init();',
$PThread__deps: ['_emscripten_thread_init',
'$terminateWorker',
'$cleanupThread',
'$cleanupThread', '$zeroMemory',
#if MAIN_MODULE
'$markAsFinished',
#endif
Expand Down
10 changes: 6 additions & 4 deletions src/library_sdl.js
Original file line number Diff line number Diff line change
Expand Up @@ -1357,7 +1357,7 @@ var LibrarySDL = {
return SDL.version;
},

SDL_Init__deps: ['calloc', 'memcpy'],
SDL_Init__deps: ['$zeroMemory', 'memcpy'],
SDL_Init__proxy: 'sync',
SDL_Init__docs: '/** @param{number} initFlags */',
SDL_Init: (initFlags) => {
Expand All @@ -1376,7 +1376,8 @@ var LibrarySDL = {
}

window.addEventListener("unload", SDL.receiveEvent);
SDL.keyboardState = _calloc(0x10000, 1); // Our SDL needs 512, but 64K is safe for older SDLs
SDL.keyboardState = _malloc(0x10000); // Our SDL needs 512, but 64K is safe for older SDLs
zeroMemory(SDL.keyboardState, 0x10000);
// Initialize this structure carefully for closure
SDL.DOMEventToSDLEvent['keydown'] = 0x300 /* SDL_KEYDOWN */;
SDL.DOMEventToSDLEvent['keyup'] = 0x301 /* SDL_KEYUP */;
Expand Down Expand Up @@ -1412,10 +1413,11 @@ var LibrarySDL = {
return 1;
},

SDL_GetVideoInfo__deps: ['calloc'],
SDL_GetVideoInfo__deps: ['$zeroMemory'],
SDL_GetVideoInfo__proxy: 'sync',
SDL_GetVideoInfo: () => {
var ret = _calloc({{{ C_STRUCTS.SDL_VideoInfo.__size__ }}}, 1);
var ret = _malloc({{{ C_STRUCTS.SDL_VideoInfo.__size__ }}});
zeroMemory(ret, {{{ C_STRUCTS.SDL_version.__size__ }}});
{{{ makeSetValue('ret', C_STRUCTS.SDL_VideoInfo.current_w, 'Module["canvas"].width', 'i32') }}};
{{{ makeSetValue('ret', C_STRUCTS.SDL_VideoInfo.current_h, 'Module["canvas"].height', 'i32') }}};
return ret;
Expand Down
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_hello_dylink.exports
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ __wasm_apply_data_relocs
__wasm_call_ctors
_emscripten_stack_alloc
_emscripten_stack_restore
calloc
dynCall_jiji
emscripten_stack_get_current
main
malloc
setThrew
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_hello_dylink.funcs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ $__wasm_apply_global_relocs
$__wasm_call_ctors
$_emscripten_stack_alloc
$_emscripten_stack_restore
$dlcalloc
$dlmalloc
$emscripten_stack_get_current
$legalstub$dynCall_jiji
$main
Expand Down
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_hello_dylink.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
6285
6292
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_hello_dylink.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
13820
13833
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_hello_dylink.size
Original file line number Diff line number Diff line change
@@ -1 +1 @@
9763
9350
4 changes: 3 additions & 1 deletion test/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1943,7 +1943,9 @@ def test_em_asm_side_module(self):
def test_em_js(self, args, force_c):
if '-sMAIN_MODULE=2' in args:
self.check_dylink()
self.emcc_args += ['-sEXPORTED_FUNCTIONS=_main,_malloc'] + args
else:
self.emcc_args += ['-sEXPORTED_FUNCTIONS=_main,_malloc']
self.emcc_args += args
if '-pthread' in args:
self.setup_node_pthreads()

Expand Down
1 change: 0 additions & 1 deletion test/test_other.py
Original file line number Diff line number Diff line change
Expand Up @@ -2189,7 +2189,6 @@ def test_dylink_pthread_bigint_em_asm(self):
def test_dylink_pthread_bigint_em_js(self):
self.set_setting('MAIN_MODULE', 2)
self.set_setting('WASM_BIGINT')
self.set_setting('EXPORTED_FUNCTIONS', '_malloc,_main')
self.emcc_args += ['-Wno-experimental', '-pthread']
self.do_runf('core/test_em_js.cpp')

Expand Down
1 change: 0 additions & 1 deletion tools/emscripten.py
Original file line number Diff line number Diff line change
Expand Up @@ -1023,7 +1023,6 @@ def create_pointer_conversion_wrappers(metadata):
'_emscripten_stack_alloc': 'pp',
'emscripten_builtin_malloc': 'pp',
'malloc': 'pp',
'calloc': 'ppp',
'webidl_malloc': 'pp',
'memalign': 'ppp',
'memcmp': '_ppp',
Expand Down
Loading