]> Cypherpunks repositories - gostls13.git/commitdiff
runtime: remove C-style strcmp and strncmp helpers
authorMatthew Dempsky <mdempsky@google.com>
Fri, 13 Feb 2015 03:09:12 +0000 (12:09 +0900)
committerIan Lance Taylor <iant@golang.org>
Mon, 16 Feb 2015 04:07:19 +0000 (04:07 +0000)
Change-Id: I4aa23e3a0e765651c91907507a0194fd528b6223
Reviewed-on: https://go-review.googlesource.com/4662
Reviewed-by: Ian Lance Taylor <iant@golang.org>
src/runtime/os3_plan9.go
src/runtime/signal_plan9.go
src/runtime/string1.go

index facaab2546395f875f8dd3a10bbde4092d08a3c2..8ecbca01746279e569e8a15601c2d8b2a6986f70 100644 (file)
@@ -10,11 +10,11 @@ func sighandler(_ureg *ureg, note *byte, gp *g) int {
        _g_ := getg()
        var t sigTabT
        var docrash bool
-       var length int
        var sig int
        var flags int
 
        c := &sigctxt{_ureg}
+       notestr := gostringnocopy(note)
 
        // The kernel will never pass us a nil note or ureg so we probably
        // made a mistake somewhere in sigtramp.
@@ -24,8 +24,7 @@ func sighandler(_ureg *ureg, note *byte, gp *g) int {
        }
        // Check that the note is no more than ERRMAX bytes (including
        // the trailing NUL). We should never receive a longer note.
-       length = findnull(note)
-       if length > _ERRMAX-1 {
+       if len(notestr) > _ERRMAX-1 {
                print("sighandler: note is longer than ERRMAX\n")
                goto Throw
        }
@@ -34,11 +33,7 @@ func sighandler(_ureg *ureg, note *byte, gp *g) int {
        // level by the program but will otherwise be ignored.
        flags = _SigNotify
        for sig, t = range sigtable {
-               n := len(t.name)
-               if length < n {
-                       continue
-               }
-               if strncmp(note, &t.name[0], uintptr(n)) == 0 {
+               if hasprefix(notestr, t.name) {
                        flags = t.flags
                        break
                }
@@ -49,7 +44,7 @@ func sighandler(_ureg *ureg, note *byte, gp *g) int {
        if flags&_SigPanic != 0 {
                // Copy the error string from sigtramp's stack into m->notesig so
                // we can reliably access it from the panic routines.
-               memmove(unsafe.Pointer(_g_.m.notesig), unsafe.Pointer(note), uintptr(length+1))
+               memmove(unsafe.Pointer(_g_.m.notesig), unsafe.Pointer(note), uintptr(len(notestr)+1))
                gp.sig = uint32(sig)
                gp.sigpc = c.pc()
                // Only push sigpanic if PC != 0.
@@ -86,7 +81,7 @@ Throw:
        _g_.m.throwing = 1
        _g_.m.caughtsig = gp
        startpanic()
-       print(gostringnocopy(note), "\n")
+       print(notestr, "\n")
        print("PC=", hex(c.pc()), "\n")
        print("\n")
        if gotraceback(&docrash) > 0 {
index 37d24359bfdf3578666956a062149d6e1c92db2f..302f1561b8a0c3125e66e1a13c0624bd0ce9a755 100644 (file)
@@ -6,7 +6,7 @@ package runtime
 
 type sigTabT struct {
        flags int
-       name  []byte
+       name  string
 }
 
 // Incoming notes are compared against this table using strncmp, so the
@@ -18,37 +18,37 @@ type sigTabT struct {
 // and also update the constant values is os2_plan9.go.
 var sigtable = [...]sigTabT{
        // Traps that we cannot be recovered.
-       {_SigThrow, []byte("sys: trap: debug exception")},
-       {_SigThrow, []byte("sys: trap: invalid opcode")},
+       {_SigThrow, "sys: trap: debug exception"},
+       {_SigThrow, "sys: trap: invalid opcode"},
 
        // We can recover from some memory errors in runtime·sigpanic.
-       {_SigPanic, []byte("sys: trap: fault read addr")},  // SIGRFAULT
-       {_SigPanic, []byte("sys: trap: fault write addr")}, // SIGWFAULT
+       {_SigPanic, "sys: trap: fault read addr"},  // SIGRFAULT
+       {_SigPanic, "sys: trap: fault write addr"}, // SIGWFAULT
 
        // We can also recover from math errors.
-       {_SigPanic, []byte("sys: trap: divide error")}, // SIGINTDIV
-       {_SigPanic, []byte("sys: fp:")},                // SIGFLOAT
+       {_SigPanic, "sys: trap: divide error"}, // SIGINTDIV
+       {_SigPanic, "sys: fp:"},                // SIGFLOAT
 
        // All other traps are normally handled as if they were marked SigThrow.
        // We mark them SigPanic here so that debug.SetPanicOnFault will work.
-       {_SigPanic, []byte("sys: trap:")}, // SIGTRAP
+       {_SigPanic, "sys: trap:"}, // SIGTRAP
 
        // Writes to a closed pipe can be handled if desired, otherwise they're ignored.
-       {_SigNotify, []byte("sys: write on closed pipe")},
+       {_SigNotify, "sys: write on closed pipe"},
 
        // Other system notes are more serious and cannot be recovered.
-       {_SigThrow, []byte("sys:")},
+       {_SigThrow, "sys:"},
 
        // Issued to all other procs when calling runtime·exit.
-       {_SigGoExit, []byte("go: exit ")},
+       {_SigGoExit, "go: exit "},
 
        // Kill is sent by external programs to cause an exit.
-       {_SigKill, []byte("kill")},
+       {_SigKill, "kill"},
 
        // Interrupts can be handled if desired, otherwise they cause an exit.
-       {_SigNotify + _SigKill, []byte("interrupt")},
-       {_SigNotify + _SigKill, []byte("hangup")},
+       {_SigNotify + _SigKill, "interrupt"},
+       {_SigNotify + _SigKill, "hangup"},
 
        // Alarms can be handled if desired, otherwise they're ignored.
-       {_SigNotify, []byte("alarm")},
+       {_SigNotify, "alarm"},
 }
index 35cde43be07a231cb2ac6cfd690e7d3da816789f..feeb341b627ec65adaa4563a8bde7e13ed9664f4 100644 (file)
@@ -67,42 +67,3 @@ func gostringw(strw *uint16) string {
        b[n2] = 0 // for luck
        return s[:n2]
 }
-
-func strcmp(s1, s2 *byte) int32 {
-       p1 := (*[_MaxMem/2 - 1]byte)(unsafe.Pointer(s1))
-       p2 := (*[_MaxMem/2 - 1]byte)(unsafe.Pointer(s2))
-
-       for i := uintptr(0); ; i++ {
-               c1 := p1[i]
-               c2 := p2[i]
-               if c1 < c2 {
-                       return -1
-               }
-               if c1 > c2 {
-                       return +1
-               }
-               if c1 == 0 {
-                       return 0
-               }
-       }
-}
-
-func strncmp(s1, s2 *byte, n uintptr) int32 {
-       p1 := (*[_MaxMem/2 - 1]byte)(unsafe.Pointer(s1))
-       p2 := (*[_MaxMem/2 - 1]byte)(unsafe.Pointer(s2))
-
-       for i := uintptr(0); i < n; i++ {
-               c1 := p1[i]
-               c2 := p2[i]
-               if c1 < c2 {
-                       return -1
-               }
-               if c1 > c2 {
-                       return +1
-               }
-               if c1 == 0 {
-                       break
-               }
-       }
-       return 0
-}