Skip to content

Commit

Permalink
Merge pull request #279 from ichiban/make-slice
Browse files Browse the repository at this point in the history
respect debug.SetMemoryLimit() when allocate a slice.
  • Loading branch information
ichiban authored Dec 31, 2022
2 parents ff2a162 + 3011ae4 commit 983a67f
Show file tree
Hide file tree
Showing 7 changed files with 200 additions and 21 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.18
go-version: 1.19

- name: Build
run: go build -v ./...
Expand Down
1 change: 1 addition & 0 deletions engine/atom.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ var (
atomMax = NewAtom("max")
atomMaxArity = NewAtom("max_arity")
atomMaxInteger = NewAtom("max_integer")
atomMemory = NewAtom("memory")
atomMin = NewAtom("min")
atomMinInteger = NewAtom("min_integer")
atomMod = NewAtom("mod")
Expand Down
38 changes: 31 additions & 7 deletions engine/builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ func Call(vm *VM, goal Term, k Cont, env *Env) *Promise {
return Error(instantiationError(env))
default:
fvs := env.freeVariables(g)
args := make([]Term, len(fvs))
args, err := makeSlice[Term](len(fvs))
if err != nil {
return Error(resourceError(resourceMemory, env))
}
for i, fv := range fvs {
args[i] = fv
}
Expand Down Expand Up @@ -95,7 +98,11 @@ func callN(vm *VM, closure Term, additional []Term, k Cont, env *Env) *Promise {
if err != nil {
return Error(err)
}
args := make([]Term, pi.arity, int(pi.arity)+len(additional))
args, err := makeSlice[Term](int(pi.arity) + len(additional))
if err != nil {
return Error(resourceError(resourceMemory, env))
}
args = args[:pi.arity]
for i := 0; i < int(pi.arity); i++ {
args[i] = arg(i)
}
Expand Down Expand Up @@ -276,7 +283,10 @@ func Functor(vm *VM, t, name, arity Term, k Cont, env *Env) *Promise {
return Error(typeError(validTypeAtom, name, env))
}

vs := make([]Term, arity)
vs, err := makeSlice[Term](int(arity))
if err != nil {
return Error(resourceError(resourceMemory, env))
}
for i := range vs {
vs[i] = NewVariable()
}
Expand Down Expand Up @@ -404,9 +414,13 @@ func renamedCopy(t Term, copied map[termID]Term, env *Env) Term {
p.tail = &tail
return &p
case Compound:
args, err := makeSlice[Term](t.Arity())
if err != nil {
return resourceError(resourceMemory, env)
}
c := compound{
functor: t.Functor(),
args: make([]Term, t.Arity()),
args: args,
}
copied[id(t)] = &c
for i := 0; i < t.Arity(); i++ {
Expand Down Expand Up @@ -435,7 +449,10 @@ func TermVariables(vm *VM, term, vars Term, k Cont, env *Env) *Promise {
}
witness[t] = struct{}{}
case Compound:
args := make([]Term, t.Arity())
args, err := makeSlice[Term](t.Arity())
if err != nil {
return Error(resourceError(resourceMemory, env))
}
for i := 0; i < t.Arity(); i++ {
args[i] = t.Arg(i)
}
Expand Down Expand Up @@ -697,7 +714,11 @@ func SetOf(vm *VM, template, goal, instances Term, k Cont, env *Env) *Promise {

func collectionOf(vm *VM, agg func([]Term, *Env) Term, template, goal, instances Term, k Cont, env *Env) *Promise {
fvs := newFreeVariablesSet(goal, template, env)
w := make([]Term, 0, len(fvs))
w, err := makeSlice[Term](len(fvs))
if err != nil {
return Error(resourceError(resourceMemory, env))
}
w = w[:0]
for v := range fvs {
w = append(w, v)
}
Expand Down Expand Up @@ -2785,7 +2806,10 @@ func Length(vm *VM, list, length Term, k Cont, env *Env) *Promise {
}

func lengthRundown(vm *VM, list Variable, n Integer, k Cont, env *Env) *Promise {
elems := make([]Term, n)
elems, err := makeSlice[Term](int(n))
if err != nil {
return Error(resourceError(resourceMemory, env))
}
for i := range elems {
elems[i] = NewVariable()
}
Expand Down
Loading

0 comments on commit 983a67f

Please sign in to comment.