Skip to content

Commit

Permalink
added license info for luaj; returning actual memory value for totalM…
Browse files Browse the repository at this point in the history
…emory to luaj, freeMemory is arbitrarily just half of that, since there's no way to actually track how much memory luaj uses itself.
  • Loading branch information
fnuecke committed Jan 21, 2014
1 parent 74a53be commit 568bd7b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 17 deletions.
19 changes: 19 additions & 0 deletions LICENSE-luaj
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2007 LuaJ. All rights reserved.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
24 changes: 7 additions & 17 deletions li/cil/oc/server/component/machine/LuaJLuaArchitecture.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ class LuaJLuaArchitecture(machine: Machine) extends Architecture {

private var doneWithInitRun = false

private var memory = 0

// ----------------------------------------------------------------------- //

private def node = machine.node
Expand All @@ -34,7 +36,7 @@ class LuaJLuaArchitecture(machine: Machine) extends Architecture {

def isInitialized = doneWithInitRun

def recomputeMemory() {}
def recomputeMemory() = memory = machine.owner.installedMemory

// ----------------------------------------------------------------------- //

Expand All @@ -59,15 +61,6 @@ class LuaJLuaArchitecture(machine: Machine) extends Architecture {
val result = thread.resume(LuaValue.NONE)
// We expect to get nothing here, if we do we had an error.
if (result.narg == 1) {
// Run the garbage collector to get rid of stuff left behind after
// the initialization phase to get a good estimate of the base
// memory usage the kernel has (including libraries). We remember
// that size to grant user-space programs a fixed base amount of
// memory, regardless of the memory need of the underlying system
// (which may change across releases).
// TODO kernelMemory = math.max(lua.getTotalMemory - lua.getFreeMemory, 1)
recomputeMemory()

// Fake zero sleep to avoid stopping if there are no signals.
LuaValue.varargsOf(LuaValue.TRUE, LuaValue.valueOf(0))
}
Expand Down Expand Up @@ -121,7 +114,6 @@ class LuaJLuaArchitecture(machine: Machine) extends Architecture {
new ExecutionResult.Shutdown(false)
}
else {
// TODO lua.setTotalMemory(Int.MaxValue)
val error = results.tojstring(3)
if (error != null) new ExecutionResult.Error(error)
else new ExecutionResult.Error("unknown error")
Expand Down Expand Up @@ -253,13 +245,9 @@ class LuaJLuaArchitecture(machine: Machine) extends Architecture {
// Are we a robot? (No this is not a CAPTCHA.)
computer.set("isRobot", (_: Varargs) => LuaValue.valueOf(machine.isRobot))

computer.set("freeMemory", (_: Varargs) =>
// This is *very* unlikely, but still: avoid this getting larger than
// what we report as the total memory.
LuaValue.valueOf(32 * 1024)) // ((lua.getFreeMemory min (lua.getTotalMemory - kernelMemory)) / ramScale).toInt
computer.set("freeMemory", (_: Varargs) => LuaValue.valueOf(memory / 2))

// Allow the system to read how much memory it uses and has available.
computer.set("totalMemory", (_: Varargs) => LuaValue.valueOf(64 * 1024)) // ((lua.getTotalMemory - kernelMemory) / ramScale).toInt
computer.set("totalMemory", (_: Varargs) => LuaValue.valueOf(memory))

computer.set("pushSignal", (args: Varargs) => LuaValue.valueOf(machine.signal(args.checkjstring(1), toSimpleJavaObjects(args, 2): _*)))

Expand Down Expand Up @@ -379,6 +367,8 @@ class LuaJLuaArchitecture(machine: Machine) extends Architecture {

lua.set("component", component)

recomputeMemory()

val kernel = lua.load(classOf[Machine].getResourceAsStream(Settings.scriptPath + "kernel.lua"), "=kernel", "t", lua)
thread = new LuaThread(lua, kernel) // Left as the first value on the stack.

Expand Down

0 comments on commit 568bd7b

Please sign in to comment.