Skip to content

Commit 032a742

Browse files
committed
keep go array, only convert to c array in init function
1 parent ce102e5 commit 032a742

3 files changed

Lines changed: 37 additions & 9 deletions

File tree

caddy/caddy.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ func init() {
3030
// Report Caddy version in phpinfo()
3131
simpleVersion, fullVersion := caddy.Version()
3232
if fullVersion != "" {
33-
frankenphp.AddPhpinfoEntry("Caddy Version", fullVersion)
33+
frankenphp.AddPhpinfoEntry("caddy", fullVersion)
3434
} else if simpleVersion != "" {
35-
frankenphp.AddPhpinfoEntry("Caddy Version", simpleVersion)
35+
frankenphp.AddPhpinfoEntry("caddy", simpleVersion)
3636
}
3737

3838
httpcaddyfile.RegisterGlobalOption("frankenphp", parseGlobalOption)

frankenphp.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1106,7 +1106,7 @@ PHP_MINIT_FUNCTION(frankenphp) {
11061106

11071107
PHP_MINFO_FUNCTION(frankenphp) {
11081108
php_info_print_table_start();
1109-
php_info_print_table_row(2, "Version", TOSTRING(FRANKENPHP_VERSION));
1109+
php_info_print_table_row(2, "frankenphp", TOSTRING(FRANKENPHP_VERSION));
11101110
if (frankenphp_phpinfo_entries) {
11111111
for (int i = 0; frankenphp_phpinfo_entries[i] != NULL; i += 2) {
11121112
php_info_print_table_row(2, frankenphp_phpinfo_entries[i], frankenphp_phpinfo_entries[i + 1]);

frankenphp.go

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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

161169
func 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

167173
func 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

175203
func calculateMaxThreads(opt *opt) (numWorkers int, _ error) {

0 commit comments

Comments
 (0)