Helper functions to extract buffer sizes using unsafe pointers The struct layout must match pool.Conn exactly to avoid checkptr violations. checkptr is Go's pointer safety checker, which ensures that unsafe pointer conversions are valid. If the struct layouts do not match exactly, this can cause run
(cn *pool.Conn)
| 133 | // conversions are valid. If the struct layouts do not match exactly, this can |
| 134 | // cause runtime panics or incorrect memory access due to invalid pointer dereferencing. |
| 135 | func getWriterBufSizeUnsafe(cn *pool.Conn) int { |
| 136 | cnPtr := (*struct { |
| 137 | id uint64 // First field in pool.Conn |
| 138 | usedAt atomic.Int64 // Second field (atomic) |
| 139 | lastPutAt atomic.Int64 // Third field (atomic) |
| 140 | checkoutAt atomic.Int64 // Fourth field (atomic) |
| 141 | netConnAtomic atomic.Value // atomic.Value for net.Conn |
| 142 | rd *proto.Reader |
| 143 | bw *bufio.Writer |
| 144 | wr *proto.Writer |
| 145 | // We only need fields up to bw, so we can stop here |
| 146 | })(unsafe.Pointer(cn)) |
| 147 | |
| 148 | if cnPtr.bw == nil { |
| 149 | return -1 |
| 150 | } |
| 151 | |
| 152 | // bufio.Writer internal structure |
| 153 | bwPtr := (*struct { |
| 154 | err error |
| 155 | buf []byte |
| 156 | n int |
| 157 | wr interface{} |
| 158 | })(unsafe.Pointer(cnPtr.bw)) |
| 159 | |
| 160 | return len(bwPtr.buf) |
| 161 | } |
| 162 | |
| 163 | func getReaderBufSizeUnsafe(cn *pool.Conn) int { |
| 164 | cnPtr := (*struct { |