Skip to content

Commit c4c0ecd

Browse files
authored
Remove makeMalloc utility function. NFC (#19160)
It seems much safer/cleaner to simply use explicit dependencies on `malloc`. The one function where an optional dependency might be useful is `allocate` since it can operator both with and without `malloc`, but that function is deprecated/legacy. See #19159
1 parent 7d2ca62 commit c4c0ecd

7 files changed

+11
-40
lines changed

src/library_glfw.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1272,10 +1272,11 @@ var LibraryGLFW = {
12721272
glfwPostEmptyEvent: function() {},
12731273

12741274
glfwGetMonitors__sig: 'ii',
1275+
glfwGetMonitors__deps: ['malloc'],
12751276
glfwGetMonitors: function(count) {
12761277
{{{ makeSetValue('count', '0', '1', 'i32') }}};
12771278
if (!GLFW.monitors) {
1278-
GLFW.monitors = {{{ makeMalloc('glfwGetMonitors', POINTER_SIZE) }}};
1279+
GLFW.monitors = _malloc({{{ POINTER_SIZE }}});
12791280
{{{ makeSetValue('GLFW.monitors', '0', '1', 'i32') }}};
12801281
}
12811282
return GLFW.monitors;

src/library_legacy.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ mergeInto(LibraryManager.library, {
1717
* @param {(Uint8Array|Array<number>)} slab: An array of data.
1818
* @param {number=} allocator : How to allocate memory, see ALLOC_*
1919
*/
20-
$allocate__deps: ['$ALLOC_NORMAL', '$ALLOC_STACK'],
20+
$allocate__deps: ['$ALLOC_NORMAL', '$ALLOC_STACK', 'malloc'],
2121
$allocate: function(slab, allocator) {
2222
var ret;
2323
#if ASSERTIONS
@@ -28,7 +28,7 @@ mergeInto(LibraryManager.library, {
2828
if (allocator == ALLOC_STACK) {
2929
ret = stackAlloc(slab.length);
3030
} else {
31-
ret = {{{ makeMalloc('allocate', 'slab.length') }}};
31+
ret = _malloc(slab.length);
3232
}
3333

3434
if (!slab.subarray && !slab.slice) {

src/library_strings.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -479,10 +479,10 @@ mergeInto(LibraryManager.library, {
479479

480480
// Allocate heap space for a JS string, and write it there.
481481
// It is the responsibility of the caller to free() that memory.
482-
$stringToNewUTF8__deps: ['$lengthBytesUTF8', '$stringToUTF8'],
482+
$stringToNewUTF8__deps: ['$lengthBytesUTF8', '$stringToUTF8', 'malloc'],
483483
$stringToNewUTF8: function(str) {
484484
var size = lengthBytesUTF8(str) + 1;
485-
var ret = {{{ makeMalloc('stringToNewUTF8', 'size') }}};
485+
var ret = _malloc(size);
486486
if (ret) stringToUTF8(str, ret, size);
487487
return ret;
488488
},

src/parseTools.js

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -925,21 +925,6 @@ function awaitIf(condition) {
925925
return condition ? 'await' : '';
926926
}
927927

928-
function makeMalloc(source, param) {
929-
if (hasExportedSymbol('malloc')) {
930-
return `_malloc(${param})`;
931-
}
932-
// It should be impossible to call some functions without malloc being
933-
// included, unless we have a deps_info.json bug. To let closure not error
934-
// on `_malloc` not being present, they don't call malloc and instead abort
935-
// with an error at runtime.
936-
// TODO: A more comprehensive deps system could catch this at compile time.
937-
if (!ASSERTIONS) {
938-
return 'abort();';
939-
}
940-
return `abort('malloc was not included, but is needed in ${source}. Adding "_malloc" to EXPORTED_FUNCTIONS should fix that. This may be a bug in the compiler, please file an issue.');`;
941-
}
942-
943928
// Adds a call to runtimeKeepalivePush, if needed by the current build
944929
// configuration.
945930
// We skip this completely in MINIMAL_RUNTIME and also in builds that

src/parseTools_legacy.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@ function makeCopyValues(dest, src, num, type, modifier, align, sep = ';') {
8585
return ret.join(sep);
8686
}
8787

88+
function makeMalloc(source, param) {
89+
return `_malloc(${param})`;
90+
}
91+
8892
global.Runtime = {
8993
getNativeTypeSize: getNativeTypeSize,
9094
getNativeFieldSize: getNativeFieldSize,

test/test_other.py

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11519,25 +11519,6 @@ def test_missing_malloc_export(self):
1151911519
"malloc() called but not included in the build - add '_malloc' to EXPORTED_FUNCTIONS",
1152011520
"free() called but not included in the build - add '_free' to EXPORTED_FUNCTIONS"))
1152111521

11522-
def test_missing_malloc_export_indirect(self):
11523-
# we used to include malloc by default. show a clear error in builds with
11524-
# ASSERTIONS to help with any confusion when the user calls a JS API that
11525-
# requires malloc
11526-
create_file('unincluded_malloc.c', r'''
11527-
#include <emscripten.h>
11528-
EM_JS_DEPS(main, "$stringToNewUTF8");
11529-
int main() {
11530-
EM_ASM({
11531-
try {
11532-
stringToNewUTF8("foo");
11533-
} catch(e) {
11534-
console.log('exception:', e);
11535-
}
11536-
});
11537-
}
11538-
''')
11539-
self.do_runf('unincluded_malloc.c', 'malloc was not included, but is needed in stringToNewUTF8. Adding "_malloc" to EXPORTED_FUNCTIONS should fix that. This may be a bug in the compiler, please file an issue.')
11540-
1154111522
def test_getrusage(self):
1154211523
self.do_runf(test_file('other/test_getrusage.c'))
1154311524

tools/deps_info.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@
150150
'glewInit': ['malloc'],
151151
'glfwGetProcAddress': ['malloc'],
152152
'glfwInit': ['malloc', 'free'],
153-
'glfwSleep': ['sleep'],
153+
'glfwSleep': ['sleep', 'malloc'],
154154
'glfwGetMonitors': ['malloc'],
155155
'localtime': ['malloc'],
156156
'localtime_r': ['malloc'],

0 commit comments

Comments
 (0)