Skip to content

Commit cab2842

Browse files
committed
Append method Regexp.SetJITStackSize
1 parent fe320fa commit cab2842

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

pcre.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,25 @@ func (re *Regexp) Study(flags int) error {
491491
return nil
492492
}
493493

494+
// SetJITStackSize used for change JIT stack size.
495+
// The arguments are a starting size for the stack, and a maximum size to which it is
496+
// allowed to grow. See also pcre_assign_jit_stack
497+
func (re *Regexp) SetJITStackSize(startSize int, maxSize int) error {
498+
if len(re.extra) == 0 {
499+
return errors.New("can't set JIT stack size: have no extra data")
500+
}
501+
502+
stack := C.pcre_jit_stack_alloc(C.int(startSize), C.int(maxSize))
503+
if stack == nil {
504+
return errors.New("can't allocate JIT stack")
505+
}
506+
extra := (*C.pcre_extra)(unsafe.Pointer(&re.extra[0]))
507+
C.pcre_assign_jit_stack(extra, nil, unsafe.Pointer(stack))
508+
// We should later release stack by pcre_jit_stack_free, but do not it yet
509+
510+
return nil
511+
}
512+
494513
// Matcher objects provide a place for storing match results.
495514
// They can be created by the NewMatcher and NewMatcherString functions,
496515
// or they can be initialized with Reset or ResetString.

pcre_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,19 @@ func BenchmarkExecWithoutStudy(b *testing.B) {
224224
}
225225
}
226226

227+
func TestJITStackSize(t *testing.T) {
228+
re := MustCompileJIT(`aaa|bb|cc`, 0, STUDY_JIT_COMPILE)
229+
err := re.SetJITStackSize(64*1024, 1024*1024)
230+
if err != nil {
231+
t.Errorf("Error call of SetJITStackSize: %s", err)
232+
}
233+
234+
m := re.NewMatcherString(`bb`, 0)
235+
if !m.Matches {
236+
t.Error("The match should be matched")
237+
}
238+
}
239+
227240
func TestPartial(t *testing.T) {
228241
re := MustCompile(`^abc`, 0)
229242

0 commit comments

Comments
 (0)