* }}} */ * GopherLua original APIs {{{ */ Set maximum memory size. This function can only be called from the main thread.
(mx int)
| 2240 | |
| 2241 | // Set maximum memory size. This function can only be called from the main thread. |
| 2242 | func (ls *LState) SetMx(mx int) { |
| 2243 | if ls.Parent != nil { |
| 2244 | ls.RaiseError("sub threads are not allowed to set a memory limit") |
| 2245 | } |
| 2246 | go func() { |
| 2247 | limit := uint64(mx * 1024 * 1024) //MB |
| 2248 | var s runtime.MemStats |
| 2249 | for atomic.LoadInt32(&ls.stop) == 0 { |
| 2250 | runtime.ReadMemStats(&s) |
| 2251 | if s.Alloc >= limit { |
| 2252 | fmt.Println("out of memory") |
| 2253 | os.Exit(3) |
| 2254 | } |
| 2255 | time.Sleep(100 * time.Millisecond) |
| 2256 | } |
| 2257 | }() |
| 2258 | } |
| 2259 | |
| 2260 | // SetContext set a context ctx to this LState. The provided ctx must be non-nil. |
| 2261 | func (ls *LState) SetContext(ctx context.Context) { |