SizeOf returns the byte size of the type t.
(t reflect.Type, m *device.MemoryLayout)
| 70 | |
| 71 | // SizeOf returns the byte size of the type t. |
| 72 | func SizeOf(t reflect.Type, m *device.MemoryLayout) uint64 { |
| 73 | switch t.Kind() { |
| 74 | case reflect.Uint8: |
| 75 | if t.Implements(tyCharTy) { |
| 76 | return uint64(m.GetChar().GetSize()) |
| 77 | } |
| 78 | return uint64(m.GetI8().GetSize()) |
| 79 | case reflect.Bool, reflect.Int8: |
| 80 | return uint64(m.GetI8().GetSize()) |
| 81 | case reflect.Int16, reflect.Uint16: |
| 82 | return uint64(m.GetI16().GetSize()) |
| 83 | case reflect.Int32, reflect.Uint32: |
| 84 | return uint64(m.GetI32().GetSize()) |
| 85 | case reflect.Float32: |
| 86 | return uint64(m.GetF32().GetSize()) |
| 87 | case reflect.Float64: |
| 88 | return uint64(m.GetF64().GetSize()) |
| 89 | case reflect.Int64, reflect.Uint64: |
| 90 | switch { |
| 91 | case t.Implements(tyPointer): |
| 92 | return uint64(m.GetPointer().GetSize()) |
| 93 | case t.Implements(tyIntTy) || t.Implements(tyUintTy): |
| 94 | return uint64(m.GetInteger().GetSize()) |
| 95 | case t.Implements(tySizeTy): |
| 96 | return uint64(m.GetSize().GetSize()) |
| 97 | default: |
| 98 | return uint64(m.GetI64().GetSize()) |
| 99 | } |
| 100 | case reflect.Int, reflect.Uint: |
| 101 | return uint64(m.GetInteger().GetSize()) |
| 102 | case reflect.Array: |
| 103 | return SizeOf(t.Elem(), m) * uint64(t.Len()) |
| 104 | case reflect.String: |
| 105 | return 1 |
| 106 | } |
| 107 | |
| 108 | switch { |
| 109 | case t.Implements(tyPointer): |
| 110 | return uint64(m.GetPointer().GetSize()) |
| 111 | case t.Implements(tySizedTy): |
| 112 | return reflect.New(t).Interface().(SizedTy).TypeSize(m) |
| 113 | } |
| 114 | |
| 115 | panic(fmt.Errorf("MemoryLayout.SizeOf not implemented for type %v (%v)", t, t.Kind())) |
| 116 | } |