Skip to content

Commit

Permalink
feat(wasm): Add MemoryManager support
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromelaban committed Jan 21, 2022
1 parent aa3e6e9 commit 66038f1
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 2 deletions.
5 changes: 5 additions & 0 deletions src/Uno.UI/WasmScripts/Uno.UI.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1104,6 +1104,11 @@ declare namespace Uno.Storage.Streams {
static truncateAsync(streamId: string, length: number): Promise<string>;
}
}
declare namespace Windows.System {
class MemoryManager {
static getAppMemoryUsage(): any;
}
}
interface Navigator {
wakeLock: WakeLock;
}
Expand Down
18 changes: 18 additions & 0 deletions src/Uno.UI/WasmScripts/Uno.UI.js
Original file line number Diff line number Diff line change
Expand Up @@ -4038,6 +4038,24 @@ var Uno;
})(Streams = Storage.Streams || (Storage.Streams = {}));
})(Storage = Uno.Storage || (Uno.Storage = {}));
})(Uno || (Uno = {}));
var Windows;
(function (Windows) {
var System;
(function (System) {
class MemoryManager {
static getAppMemoryUsage() {
if (typeof Module === "object") {
// Returns an approximate memory usage for the current wasm module.
// Initial buffer size is determine by the initial wasm memory defined in
// emscripten.
return Module.HEAPU8.length;
}
return 0;
}
}
System.MemoryManager = MemoryManager;
})(System = Windows.System || (Windows.System = {}));
})(Windows || (Windows = {}));
var WakeLockType;
(function (WakeLockType) {
WakeLockType["screen"] = "screen";
Expand Down
16 changes: 16 additions & 0 deletions src/Uno.UI/ts/Windows/System/MemoryManager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace Windows.System {

export class MemoryManager {

static getAppMemoryUsage() {
if (typeof Module === "object") {

// Returns an approximate memory usage for the current wasm module.
// Initial buffer size is determine by the initial wasm memory defined in
// emscripten.
return (<any>Module).HEAPU8.length;
}
return 0;
}
}
}
4 changes: 2 additions & 2 deletions src/Uno.UWP/Generated/3.0.0.0/Windows.System/MemoryManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Windows.System
#endif
public static partial class MemoryManager
{
#if false || __IOS__ || NET461 || __WASM__ || __SKIA__ || __NETSTD_REFERENCE__ || __MACOS__
#if false || __IOS__ || NET461 || false || __SKIA__ || __NETSTD_REFERENCE__ || __MACOS__
[global::Uno.NotImplemented("__IOS__", "NET461", "__WASM__", "__SKIA__", "__NETSTD_REFERENCE__", "__MACOS__")]
public static ulong AppMemoryUsage
{
Expand All @@ -27,7 +27,7 @@ public static ulong AppMemoryUsage
}
}
#endif
#if false || __IOS__ || NET461 || __WASM__ || __SKIA__ || __NETSTD_REFERENCE__ || __MACOS__
#if false || __IOS__ || NET461 || false || __SKIA__ || __NETSTD_REFERENCE__ || __MACOS__
[global::Uno.NotImplemented("__IOS__", "NET461", "__WASM__", "__SKIA__", "__NETSTD_REFERENCE__", "__MACOS__")]
public static ulong AppMemoryUsageLimit
{
Expand Down
40 changes: 40 additions & 0 deletions src/Uno.UWP/System/MemoryManager.Wasm.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System;
using System.Globalization;
using Uno.Foundation;

namespace Windows.System
{
public partial class MemoryManager
{
private const string JsType = "Windows.System.MemoryManager";

public static ulong AppMemoryUsage
{
get
{
if(ulong.TryParse(WebAssemblyRuntime.InvokeJS(
$"{JsType}.getAppMemoryUsage()"),
NumberStyles.Any,
CultureInfo.InvariantCulture, out var value))
{
return value;
}

throw new Exception($"getAppMemoryUsage returned an unsupported value");
}
}

public static ulong AppMemoryUsageLimit
{
get
{
if (Environment.GetEnvironmentVariable("UNO_BOOTSTRAP_EMSCRIPTEN_MAXIMUM_MEMORY") == "4GB")
{
return 4ul * 1024 * 1024 * 1024;
}

return 2ul * 1024 * 1024 * 1024;
}
}
}
}

0 comments on commit 66038f1

Please sign in to comment.