@@ -30,6 +30,7 @@ import (
3030 "os"
3131 "os/signal"
3232 "runtime"
33+ "sort"
3334 "strings"
3435 "sync"
3536 "sync/atomic"
@@ -156,20 +157,47 @@ func Config() PHPConfig {
156157 }
157158}
158159
159- var phpinfoEntries []* C.char
160+ type phpinfoEntry struct {
161+ key , value string
162+ }
163+
164+ var (
165+ phpinfoEntries []phpinfoEntry
166+ cPhpinfoArr []* C.char
167+ )
160168
161169func AddPhpinfoEntry (key , value string ) {
162- cKey := C .CString (key )
163- cValue := C .CString (value )
164- phpinfoEntries = append (phpinfoEntries , cKey , cValue )
170+ phpinfoEntries = append (phpinfoEntries , phpinfoEntry {key , value })
165171}
166172
167173func initPhpinfoEntries () {
174+ for _ , cstr := range cPhpinfoArr {
175+ if cstr != nil {
176+ C .free (unsafe .Pointer (cstr ))
177+ }
178+ }
179+ if cPhpinfoArr != nil {
180+ C .free (unsafe .Pointer (& cPhpinfoArr [0 ]))
181+ cPhpinfoArr = nil
182+ C .frankenphp_phpinfo_entries = nil
183+ }
184+
168185 if len (phpinfoEntries ) == 0 {
169186 return
170187 }
171- phpinfoEntries = append (phpinfoEntries , nil )
172- C .frankenphp_phpinfo_entries = (* * C .char )(unsafe .Pointer (& phpinfoEntries [0 ]))
188+
189+ sort .Slice (phpinfoEntries , func (i , j int ) bool {
190+ return phpinfoEntries [i ].key < phpinfoEntries [j ].key
191+ })
192+
193+ n := 2 * len (phpinfoEntries ) + 1
194+ cPhpinfoArr = (* [1 << 28 ]* C.char )(C .malloc (C .size_t (n ) * C .size_t (unsafe .Sizeof (uintptr (0 )))))[:n :n ]
195+ for i , e := range phpinfoEntries {
196+ cPhpinfoArr [2 * i ] = C .CString (e .key )
197+ cPhpinfoArr [2 * i + 1 ] = C .CString (e .value )
198+ }
199+ cPhpinfoArr [n - 1 ] = nil
200+ C .frankenphp_phpinfo_entries = & cPhpinfoArr [0 ]
173201}
174202
175203func calculateMaxThreads (opt * opt ) (numWorkers int , _ error ) {
0 commit comments