From 1338185eeaafe5ebb6b1c3302e87e42c221f1750 Mon Sep 17 00:00:00 2001 From: 20kdc Date: Thu, 26 Apr 2018 17:38:33 +0100 Subject: [PATCH] More memory improvements --- code/apps/sys-glacier.lua | 140 ++++++++++++++++++-------------------- code/apps/sys-icecap.lua | 36 +++++----- 2 files changed, 86 insertions(+), 90 deletions(-) diff --git a/code/apps/sys-glacier.lua b/code/apps/sys-glacier.lua index fc94d23..221481f 100644 --- a/code/apps/sys-glacier.lua +++ b/code/apps/sys-glacier.lua @@ -111,69 +111,76 @@ local function getMonitorSettings(a) return w, h, d, t end +-- Settings API + +local mBase = { + getSetting = function (name) + neo.ensureType(name, "string") + return settings[name] + end, + listSettings = function () + local s = {} + for k, v in pairs(settings) do + table.insert(s, k) + end + return s + end, + delSetting = function () + neo.ensureType(name, "string") + local val = nil + if name == "password" or name == "pub.clipboard" then val = "" end + settings[name] = val + sRattle(name, val) + pcall(saveSettings) + end, + setSetting = function (name, val) + neo.ensureType(name, "string") + neo.ensureType(val, "string") + settings[name] = val + -- NOTE: Either a monitor is under application control, + -- or it's not under any control. + -- Monitor settings are applied on the transition to control. + sRattle(name, val) + pcall(saveSettings) + end, + shutdown = function (reboot) + neo.ensureType(reboot, "boolean") + if shuttingDown then return end + shuttingDown = true + shutdownMode = reboot + local counter = 0 + neo.scheduleTimer(os.uptime() + 5) -- in case the upcoming code fails in some way + for f, v in pairs(targsSD) do + counter = counter + 1 + v("shutdown", reboot, function () + counter = counter - 1 + if counter == 0 then + shutdownFin(shutdownMode) + end + end) + end + if counter == 0 then + shutdownFin(shutdownMode) + end + -- donkonit will shutdown when the timer is hit. + end +} + donkonitSPProvider(function (pkg, pid, sendSig) targs[pid] = sendSig - return { - listSettings = function () - local s = {} - for k, v in pairs(settings) do - table.insert(s, k) - end - return s - end, - -- NOTE: REPLICATED IN GB - getSetting = function (name) - neo.ensureType(name, "string") - return settings[name] - end, - delSetting = function (name) - neo.ensureType(name, "string") - local val = nil - if name == "password" or name == "pub.clipboard" then val = "" end - settings[name] = val - sRattle(name, val) - pcall(saveSettings) - end, - setSetting = function (name, val) - neo.ensureType(name, "string") - neo.ensureType(val, "string") - settings[name] = val - -- NOTE: Either a monitor is under application control, - -- or it's not under any control. - -- Monitor settings are applied on the transition to control. - sRattle(name, val) - pcall(saveSettings) - end, - -- + local n = { registerForShutdownEvent = function () targsSD[pid] = sendSig end, registerSavingThrow = function (st) neo.ensureType(st, "function") targsST[pid] = st - end, - shutdown = function (reboot) - neo.ensureType(reboot, "boolean") - if shuttingDown then return end - shuttingDown = true - shutdownMode = reboot - local counter = 0 - neo.scheduleTimer(os.uptime() + 5) -- in case the upcoming code fails in some way - for f, v in pairs(targsSD) do - counter = counter + 1 - v("shutdown", reboot, function () - counter = counter - 1 - if counter == 0 then - shutdownFin(shutdownMode) - end - end) - end - if counter == 0 then - shutdownFin(shutdownMode) - end - -- donkonit will shutdown when the timer is hit. end } + return setmetatable(n, { + __index = mBase, + __metatable = 0 + }) end) donkonitRDProvider(function (pkg, pid, sendSig) @@ -250,6 +257,11 @@ pcall(saveSettings) glacierDCProvider(function (pkg, pid, sendSig) targsDC[pid] = sendSig + local function sWrap(f) + return function (s, ...) + return f("pub." .. s, ...) + end + end return { getKnownMonitors = function () local tbl = {} @@ -288,25 +300,9 @@ glacierDCProvider(function (pkg, pid, sendSig) end, forceRescan = rescanDevs, -- NOTE: "pub." prefixed version of functions in sys.manage - getSetting = function (name) - neo.ensureType(name, "string") - return settings["pub." .. name] - end, - delSetting = function (name) - neo.ensureType(name, "string") - local val = nil - if name == "clipboard" then val = "" end - settings["pub." .. name] = val - sRattle("pub." .. name, val) - pcall(saveSettings) - end, - setSetting = function (name, val) - neo.ensureType(name, "string") - neo.ensureType(val, "string") - settings["pub." .. name] = val - sRattle("pub." .. name, val) - pcall(saveSettings) - end + getSetting = sWrap(mBase.getSetting), + delSetting = sWrap(mBase.delSetting), + setSetting = sWrap(mBase.setSetting) } end) diff --git a/code/apps/sys-icecap.lua b/code/apps/sys-icecap.lua index 3cb4846..32fc700 100644 --- a/code/apps/sys-icecap.lua +++ b/code/apps/sys-icecap.lua @@ -4,10 +4,9 @@ -- s-icecap : Responsible for x.neo.pub API, crash dialogs, and security policy that isn't "sys- has ALL access, anything else has none" -- In general, this is what userspace will be interacting with in some way or another to get stuff done -local settings = neo.requireAccess("x.neo.sys.manage", "security sysconf access") -local fs = neo.requireAccess("c.filesystem", "file managers") -local donkonitDFProvider = neo.requireAccess("r.neo.pub.base", "creating basic NEO APIs") local rootAccess = neo.requireAccess("k.root", "installing GUI integration") +local settings = neo.requireAccess("x.neo.sys.manage", "security sysconf access") +local donkonitDFProvider = neo.requireAccess("r.neo.pub.base", "creating basic NEO APIs") local targsDH = {} -- data disposal @@ -98,8 +97,9 @@ end donkonitDFProvider(function (pkg, pid, sendSig) local prefixNS = "data/" .. pkg - local prefixWS = "data/" .. pkg .. "/" - fs.primary.makeDirectory(prefixNS) + local prefixWS = prefixNS .. "/" + local fs = rootAccess.primaryDisk + fs.makeDirectory(prefixNS) local openHandles = {} targsDH[pid] = function () for k, v in pairs(openHandles) do @@ -117,7 +117,7 @@ donkonitDFProvider(function (pkg, pid, sendSig) neo.scheduleTimer(0) table.insert(todo, function () -- sys-filedialog is yet another "library to control memory usage". - local closer = require("sys-filedialog")(event, nexus, function (res) openHandles[tag] = nil sendSig("filedialog", tag, res) end, fs, pkg, forWrite) + local closer = require("sys-filedialog")(event, nexus, function (res) openHandles[tag] = nil sendSig("filedialog", tag, res) end, neo.requireAccess("c.filesystem", "file managers"), pkg, forWrite) openHandles[tag] = closer end) return tag @@ -144,14 +144,14 @@ donkonitDFProvider(function (pkg, pid, sendSig) path = prefixNS .. path neo.ensurePath(path, prefixWS) if path:sub(#path, #path) ~= "/" then error("Expected / at end") end - return fs.primary.list(path:sub(1, #path - 1)) + return fs.list(path:sub(1, #path - 1)) end, makeDirectory = function (path) neo.ensureType(path, "string") path = prefixNS .. path neo.ensurePath(path, prefixWS) if path:sub(#path, #path) == "/" then error("Expected no / at end") end - return fs.primary.makeDirectory(path) + return fs.makeDirectory(path) end, rename = function (path1, path2) neo.ensureType(path1, "string") @@ -162,7 +162,7 @@ donkonitDFProvider(function (pkg, pid, sendSig) neo.ensurePath(path2, prefixWS) if path:sub(#path1, #path1) == "/" then error("Expected no / at end") end if path:sub(#path2, #path2) == "/" then error("Expected no / at end") end - return fs.primary.rename(path1, path2) + return fs.rename(path1, path2) end, open = function (path, mode) neo.ensureType(path, "string") @@ -170,7 +170,7 @@ donkonitDFProvider(function (pkg, pid, sendSig) path = prefixNS .. path neo.ensurePath(path, prefixWS) if path:sub(#path, #path) == "/" then error("Expected no / at end") end - local fw, closer = require("sys-filewrap").create(fs.primary, path, mode) + local fw, closer = require("sys-filewrap").create(fs, path, mode) if not fw then return nil, closer end local oc = fw.close fw.close = function () @@ -185,24 +185,24 @@ donkonitDFProvider(function (pkg, pid, sendSig) path = prefixNS .. path neo.ensurePath(path, prefixWS) if path:sub(#path, #path) == "/" then error("Expected no / at end") end - return fs.primary.remove(path) + return fs.remove(path) end, stat = function (path) neo.ensureType(path, "string") path = prefixNS .. path neo.ensurePath(path, prefixWS) if path:sub(#path, #path) == "/" then error("Expected no / at end") end - if not fs.primary.exists(path) then return nil end + if not fs.exists(path) then return nil end return { - fs.primary.isDirectory(path), - fs.primary.size(path), - fs.primary.lastModified(path) + fs.isDirectory(path), + fs.size(path), + fs.lastModified(path) } end, -- getLabel/setLabel have nothing to do with this - spaceUsed = fs.primary.spaceUsed, - spaceTotal = fs.primary.spaceTotal, - isReadOnly = fs.primary.isReadOnly + spaceUsed = fs.spaceUsed, + spaceTotal = fs.spaceTotal, + isReadOnly = fs.isReadOnly } end)