| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
package state |
| 16 |
|
| 17 |
import ( |
| 18 |
"bytes" |
| 19 |
"context" |
| 20 |
"fmt" |
| 21 |
"math" |
| 22 |
"reflect" |
| 23 |
|
| 24 |
"inet.af/netstack/log" |
| 25 |
"inet.af/netstack/state/wire" |
| 26 |
) |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
type internalCallback interface { |
| 32 |
|
| 33 |
source() *objectDecodeState |
| 34 |
|
| 35 |
|
| 36 |
callbackRun() |
| 37 |
} |
| 38 |
|
| 39 |
|
| 40 |
type userCallback func() |
| 41 |
|
| 42 |
|
| 43 |
func (userCallback) source() *objectDecodeState { |
| 44 |
return nil |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
func (uc userCallback) callbackRun() { |
| 49 |
uc() |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
type objectDecodeState struct { |
| 58 |
|
| 59 |
id objectID |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
typ typeID |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
obj reflect.Value |
| 78 |
|
| 79 |
|
| 80 |
blockedBy int |
| 81 |
|
| 82 |
|
| 83 |
callbacksInline [2]internalCallback |
| 84 |
|
| 85 |
|
| 86 |
callbacks []internalCallback |
| 87 |
|
| 88 |
completeEntry |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
func (ods *objectDecodeState) addCallback(ic internalCallback) { |
| 93 |
if ods.callbacks == nil { |
| 94 |
ods.callbacks = ods.callbacksInline[:0] |
| 95 |
} |
| 96 |
ods.callbacks = append(ods.callbacks, ic) |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
func (ods *objectDecodeState) findCycleFor(target *objectDecodeState) []*objectDecodeState { |
| 101 |
for _, ic := range ods.callbacks { |
| 102 |
other := ic.source() |
| 103 |
if other != nil && other == target { |
| 104 |
return []*objectDecodeState{target} |
| 105 |
} else if childList := other.findCycleFor(target); childList != nil { |
| 106 |
return append(childList, other) |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
Failf("no deadlock found?") |
| 112 |
panic("unreachable") |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
func (ods *objectDecodeState) findCycle() []*objectDecodeState { |
| 117 |
return append(ods.findCycleFor(ods), ods) |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
func (ods *objectDecodeState) source() *objectDecodeState { |
| 122 |
return ods |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
func (ods *objectDecodeState) callbackRun() { |
| 127 |
ods.blockedBy-- |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
type decodeState struct { |
| 142 |
|
| 143 |
ctx context.Context |
| 144 |
|
| 145 |
|
| 146 |
r wire.Reader |
| 147 |
|
| 148 |
|
| 149 |
types typeDecodeDatabase |
| 150 |
|
| 151 |
|
| 152 |
objectsByID []*objectDecodeState |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
deferred map[objectID]wire.Object |
| 157 |
|
| 158 |
|
| 159 |
pending completeList |
| 160 |
|
| 161 |
|
| 162 |
stats Stats |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
func (ds *decodeState) lookup(id objectID) *objectDecodeState { |
| 168 |
if len(ds.objectsByID) < int(id) { |
| 169 |
return nil |
| 170 |
} |
| 171 |
return ds.objectsByID[id-1] |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
func (ds *decodeState) checkComplete(ods *objectDecodeState) bool { |
| 176 |
|
| 177 |
if ods.blockedBy > 0 { |
| 178 |
return false |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
if ods.callbacks != nil && ods.typ != 0 { |
| 183 |
ds.stats.start(ods.typ) |
| 184 |
defer ds.stats.done() |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
for _, ic := range ods.callbacks { |
| 189 |
ic.callbackRun() |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
cbs := ods.callbacks |
| 194 |
ods.callbacks = nil |
| 195 |
ds.pending.Remove(ods) |
| 196 |
|
| 197 |
|
| 198 |
for _, ic := range cbs { |
| 199 |
if other := ic.source(); other != nil && other.blockedBy == 0 { |
| 200 |
ds.checkComplete(other) |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
return true |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
func (ds *decodeState) wait(waiter *objectDecodeState, id objectID, callback func()) { |
| 213 |
switch id { |
| 214 |
case waiter.id: |
| 215 |
|
| 216 |
fallthrough |
| 217 |
case 1: |
| 218 |
|
| 219 |
if callback != nil { |
| 220 |
callback() |
| 221 |
} |
| 222 |
return |
| 223 |
} |
| 224 |
|
| 225 |
|
| 226 |
waiter.blockedBy++ |
| 227 |
|
| 228 |
|
| 229 |
other := ds.lookup(id) |
| 230 |
if callback != nil { |
| 231 |
|
| 232 |
other.addCallback(userCallback(callback)) |
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
other.addCallback(waiter) |
| 237 |
} |
| 238 |
|
| 239 |
|
| 240 |
func (ds *decodeState) waitObject(ods *objectDecodeState, encoded wire.Object, callback func()) { |
| 241 |
if rv, ok := encoded.(*wire.Ref); ok && rv.Root != 0 { |
| 242 |
|
| 243 |
ds.wait(ods, objectID(rv.Root), callback) |
| 244 |
} else if sv, ok := encoded.(*wire.Slice); ok && sv.Ref.Root != 0 { |
| 245 |
|
| 246 |
ds.wait(ods, objectID(sv.Ref.Root), callback) |
| 247 |
} else if iv, ok := encoded.(*wire.Interface); ok { |
| 248 |
|
| 249 |
ds.waitObject(ods, iv.Value, callback) |
| 250 |
} else if callback != nil { |
| 251 |
|
| 252 |
callback() |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
func walkChild(path []wire.Dot, obj reflect.Value) reflect.Value { |
| 264 |
|
| 265 |
for i := len(path) - 1; i >= 0; i-- { |
| 266 |
switch pc := path[i].(type) { |
| 267 |
case *wire.FieldName: |
| 268 |
if obj.Kind() != reflect.Struct { |
| 269 |
Failf("next component in child path is a field name, but the current object is not a struct. Path: %v, current obj: %#v", path, obj) |
| 270 |
} |
| 271 |
obj = obj.FieldByName(string(*pc)) |
| 272 |
case wire.Index: |
| 273 |
if obj.Kind() != reflect.Array { |
| 274 |
Failf("next component in child path is an array index, but the current object is not an array. Path: %v, current obj: %#v", path, obj) |
| 275 |
} |
| 276 |
obj = obj.Index(int(pc)) |
| 277 |
default: |
| 278 |
panic("unreachable: switch should be exhaustive") |
| 279 |
} |
| 280 |
} |
| 281 |
return obj |
| 282 |
} |
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
func (ds *decodeState) register(r *wire.Ref, typ reflect.Type) reflect.Value { |
| 290 |
|
| 291 |
id := objectID(r.Root) |
| 292 |
if len(ds.objectsByID) < int(id) { |
| 293 |
ds.objectsByID = append(ds.objectsByID, make([]*objectDecodeState, int(id)-len(ds.objectsByID))...) |
| 294 |
} |
| 295 |
|
| 296 |
|
| 297 |
ods := ds.objectsByID[id-1] |
| 298 |
if ods != nil { |
| 299 |
return walkChild(r.Dots, ods.obj) |
| 300 |
} |
| 301 |
|
| 302 |
|
| 303 |
if len(r.Dots) != 0 { |
| 304 |
typ = ds.findType(r.Type) |
| 305 |
} |
| 306 |
v := reflect.New(typ) |
| 307 |
ods = &objectDecodeState{ |
| 308 |
id: id, |
| 309 |
obj: v.Elem(), |
| 310 |
} |
| 311 |
ds.objectsByID[id-1] = ods |
| 312 |
ds.pending.PushBack(ods) |
| 313 |
|
| 314 |
|
| 315 |
if encoded, ok := ds.deferred[id]; ok { |
| 316 |
delete(ds.deferred, id) |
| 317 |
ds.decodeObject(ods, ods.obj, encoded) |
| 318 |
} |
| 319 |
|
| 320 |
return walkChild(r.Dots, ods.obj) |
| 321 |
} |
| 322 |
|
| 323 |
|
| 324 |
type objectDecoder struct { |
| 325 |
|
| 326 |
ds *decodeState |
| 327 |
|
| 328 |
|
| 329 |
ods *objectDecodeState |
| 330 |
|
| 331 |
|
| 332 |
rte *reconciledTypeEntry |
| 333 |
|
| 334 |
|
| 335 |
encoded *wire.Struct |
| 336 |
} |
| 337 |
|
| 338 |
|
| 339 |
func (od *objectDecoder) load(slot int, objPtr reflect.Value, wait bool, fn func()) { |
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
v := *od.encoded.Field(od.rte.FieldOrder[slot]) |
| 346 |
od.ds.decodeObject(od.ods, objPtr.Elem(), v) |
| 347 |
if wait { |
| 348 |
|
| 349 |
od.ds.waitObject(od.ods, v, fn) |
| 350 |
} |
| 351 |
} |
| 352 |
|
| 353 |
|
| 354 |
func (od *objectDecoder) afterLoad(fn func()) { |
| 355 |
|
| 356 |
|
| 357 |
od.ods.addCallback(userCallback(fn)) |
| 358 |
} |
| 359 |
|
| 360 |
|
| 361 |
func (ds *decodeState) decodeStruct(ods *objectDecodeState, obj reflect.Value, encoded *wire.Struct) { |
| 362 |
if encoded.TypeID == 0 { |
| 363 |
|
| 364 |
|
| 365 |
if encoded.Fields() == 0 && obj.NumField() == 0 { |
| 366 |
return |
| 367 |
} |
| 368 |
|
| 369 |
|
| 370 |
Failf("empty struct on wire %#v has field mismatch with type %q", encoded, obj.Type().Name()) |
| 371 |
} |
| 372 |
|
| 373 |
|
| 374 |
rte := ds.types.Lookup(typeID(encoded.TypeID), obj.Type()) |
| 375 |
ods.typ = typeID(encoded.TypeID) |
| 376 |
|
| 377 |
|
| 378 |
od := objectDecoder{ |
| 379 |
ds: ds, |
| 380 |
ods: ods, |
| 381 |
rte: rte, |
| 382 |
encoded: encoded, |
| 383 |
} |
| 384 |
ds.stats.start(ods.typ) |
| 385 |
defer ds.stats.done() |
| 386 |
if sl, ok := obj.Addr().Interface().(SaverLoader); ok { |
| 387 |
|
| 388 |
|
| 389 |
sl.StateLoad(Source{internal: od}) |
| 390 |
} |
| 391 |
} |
| 392 |
|
| 393 |
|
| 394 |
func (ds *decodeState) decodeMap(ods *objectDecodeState, obj reflect.Value, encoded *wire.Map) { |
| 395 |
if obj.IsNil() { |
| 396 |
|
| 397 |
obj.Set(reflect.MakeMap(obj.Type())) |
| 398 |
} |
| 399 |
for i := 0; i < len(encoded.Keys); i++ { |
| 400 |
|
| 401 |
kv := reflect.New(obj.Type().Key()).Elem() |
| 402 |
vv := reflect.New(obj.Type().Elem()).Elem() |
| 403 |
ds.decodeObject(ods, kv, encoded.Keys[i]) |
| 404 |
ds.decodeObject(ods, vv, encoded.Values[i]) |
| 405 |
ds.waitObject(ods, encoded.Keys[i], nil) |
| 406 |
ds.waitObject(ods, encoded.Values[i], nil) |
| 407 |
|
| 408 |
|
| 409 |
obj.SetMapIndex(kv, vv) |
| 410 |
} |
| 411 |
} |
| 412 |
|
| 413 |
|
| 414 |
func (ds *decodeState) decodeArray(ods *objectDecodeState, obj reflect.Value, encoded *wire.Array) { |
| 415 |
if len(encoded.Contents) != obj.Len() { |
| 416 |
Failf("mismatching array length expect=%d, actual=%d", obj.Len(), len(encoded.Contents)) |
| 417 |
} |
| 418 |
|
| 419 |
for i := 0; i < len(encoded.Contents); i++ { |
| 420 |
ds.decodeObject(ods, obj.Index(i), encoded.Contents[i]) |
| 421 |
ds.waitObject(ods, encoded.Contents[i], nil) |
| 422 |
} |
| 423 |
} |
| 424 |
|
| 425 |
|
| 426 |
func (ds *decodeState) findType(t wire.TypeSpec) reflect.Type { |
| 427 |
switch x := t.(type) { |
| 428 |
case wire.TypeID: |
| 429 |
typ := ds.types.LookupType(typeID(x)) |
| 430 |
rte := ds.types.Lookup(typeID(x), typ) |
| 431 |
return rte.LocalType |
| 432 |
case *wire.TypeSpecPointer: |
| 433 |
return reflect.PtrTo(ds.findType(x.Type)) |
| 434 |
case *wire.TypeSpecArray: |
| 435 |
return reflect.ArrayOf(int(x.Count), ds.findType(x.Type)) |
| 436 |
case *wire.TypeSpecSlice: |
| 437 |
return reflect.SliceOf(ds.findType(x.Type)) |
| 438 |
case *wire.TypeSpecMap: |
| 439 |
return reflect.MapOf(ds.findType(x.Key), ds.findType(x.Value)) |
| 440 |
default: |
| 441 |
|
| 442 |
Failf("unknown type %#v", t) |
| 443 |
} |
| 444 |
panic("unreachable") |
| 445 |
} |
| 446 |
|
| 447 |
|
| 448 |
func (ds *decodeState) decodeInterface(ods *objectDecodeState, obj reflect.Value, encoded *wire.Interface) { |
| 449 |
if _, ok := encoded.Type.(wire.TypeSpecNil); ok { |
| 450 |
|
| 451 |
|
| 452 |
ds.decodeObject(ods, obj, encoded.Value) |
| 453 |
return |
| 454 |
} |
| 455 |
|
| 456 |
|
| 457 |
typ := ds.findType(encoded.Type) |
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
origObj := obj |
| 464 |
obj = reflect.New(typ).Elem() |
| 465 |
defer origObj.Set(obj) |
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
ds.decodeObject(ods, obj, encoded.Value) |
| 470 |
} |
| 471 |
|
| 472 |
|
| 473 |
func isFloatEq(x float64, y float64) bool { |
| 474 |
switch { |
| 475 |
case math.IsNaN(x): |
| 476 |
return math.IsNaN(y) |
| 477 |
case math.IsInf(x, 1): |
| 478 |
return math.IsInf(y, 1) |
| 479 |
case math.IsInf(x, -1): |
| 480 |
return math.IsInf(y, -1) |
| 481 |
default: |
| 482 |
return x == y |
| 483 |
} |
| 484 |
} |
| 485 |
|
| 486 |
|
| 487 |
func isComplexEq(x complex128, y complex128) bool { |
| 488 |
return isFloatEq(real(x), real(y)) && isFloatEq(imag(x), imag(y)) |
| 489 |
} |
| 490 |
|
| 491 |
|
| 492 |
func (ds *decodeState) decodeObject(ods *objectDecodeState, obj reflect.Value, encoded wire.Object) { |
| 493 |
switch x := encoded.(type) { |
| 494 |
case wire.Nil: |
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
case *wire.Ref: |
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
if id := objectID(x.Root); id == 0 { |
| 503 |
return |
| 504 |
} |
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
if obj.Kind() == reflect.Map { |
| 509 |
v := ds.register(x, obj.Type()) |
| 510 |
if v.IsNil() { |
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
v.Set(reflect.MakeMap(v.Type())) |
| 516 |
} |
| 517 |
obj.Set(v) |
| 518 |
return |
| 519 |
} |
| 520 |
|
| 521 |
|
| 522 |
v := ds.register(x, obj.Type().Elem()) |
| 523 |
obj.Set(reflectValueRWAddr(v)) |
| 524 |
case wire.Bool: |
| 525 |
obj.SetBool(bool(x)) |
| 526 |
case wire.Int: |
| 527 |
obj.SetInt(int64(x)) |
| 528 |
if obj.Int() != int64(x) { |
| 529 |
Failf("signed integer truncated from %v to %v", int64(x), obj.Int()) |
| 530 |
} |
| 531 |
case wire.Uint: |
| 532 |
obj.SetUint(uint64(x)) |
| 533 |
if obj.Uint() != uint64(x) { |
| 534 |
Failf("unsigned integer truncated from %v to %v", uint64(x), obj.Uint()) |
| 535 |
} |
| 536 |
case wire.Float32: |
| 537 |
obj.SetFloat(float64(x)) |
| 538 |
case wire.Float64: |
| 539 |
obj.SetFloat(float64(x)) |
| 540 |
if !isFloatEq(obj.Float(), float64(x)) { |
| 541 |
Failf("floating point number truncated from %v to %v", float64(x), obj.Float()) |
| 542 |
} |
| 543 |
case *wire.Complex64: |
| 544 |
obj.SetComplex(complex128(*x)) |
| 545 |
case *wire.Complex128: |
| 546 |
obj.SetComplex(complex128(*x)) |
| 547 |
if !isComplexEq(obj.Complex(), complex128(*x)) { |
| 548 |
Failf("complex number truncated from %v to %v", complex128(*x), obj.Complex()) |
| 549 |
} |
| 550 |
case *wire.String: |
| 551 |
obj.SetString(string(*x)) |
| 552 |
case *wire.Slice: |
| 553 |
|
| 554 |
if id := objectID(x.Ref.Root); id == 0 { |
| 555 |
return |
| 556 |
} |
| 557 |
|
| 558 |
|
| 559 |
typ := reflect.ArrayOf(int(x.Capacity), obj.Type().Elem()) |
| 560 |
v := ds.register(&x.Ref, typ) |
| 561 |
obj.Set(reflectValueRWSlice3(v, 0, int(x.Length), int(x.Capacity))) |
| 562 |
case *wire.Array: |
| 563 |
ds.decodeArray(ods, obj, x) |
| 564 |
case *wire.Struct: |
| 565 |
ds.decodeStruct(ods, obj, x) |
| 566 |
case *wire.Map: |
| 567 |
ds.decodeMap(ods, obj, x) |
| 568 |
case *wire.Interface: |
| 569 |
ds.decodeInterface(ods, obj, x) |
| 570 |
default: |
| 571 |
|
| 572 |
Failf("unknown object %#v for %q", encoded, obj.Type().Name()) |
| 573 |
} |
| 574 |
} |
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
func (ds *decodeState) Load(obj reflect.Value) { |
| 580 |
ds.stats.init() |
| 581 |
defer ds.stats.fini(func(id typeID) string { |
| 582 |
return ds.types.LookupName(id) |
| 583 |
}) |
| 584 |
|
| 585 |
|
| 586 |
rootOds := &objectDecodeState{ |
| 587 |
id: 1, |
| 588 |
obj: obj, |
| 589 |
} |
| 590 |
ds.objectsByID = append(ds.objectsByID, rootOds) |
| 591 |
ds.pending.PushBack(rootOds) |
| 592 |
|
| 593 |
|
| 594 |
numObjects, object, err := ReadHeader(ds.r) |
| 595 |
if err != nil { |
| 596 |
Failf("header error: %w", err) |
| 597 |
} |
| 598 |
if !object { |
| 599 |
Failf("object missing") |
| 600 |
} |
| 601 |
|
| 602 |
|
| 603 |
var ( |
| 604 |
encoded wire.Object |
| 605 |
ods *objectDecodeState |
| 606 |
id objectID |
| 607 |
tid = typeID(1) |
| 608 |
) |
| 609 |
if err := safely(func() { |
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
for i := uint64(0); i < numObjects; { |
| 615 |
|
| 616 |
encoded = wire.Load(ds.r) |
| 617 |
switch we := encoded.(type) { |
| 618 |
case *wire.Type: |
| 619 |
ds.types.Register(we) |
| 620 |
tid++ |
| 621 |
encoded = nil |
| 622 |
continue |
| 623 |
case wire.Uint: |
| 624 |
id = objectID(we) |
| 625 |
i++ |
| 626 |
|
| 627 |
encoded = wire.Load(ds.r) |
| 628 |
ods = ds.lookup(id) |
| 629 |
if ods != nil { |
| 630 |
|
| 631 |
ds.decodeObject(ods, ods.obj, encoded) |
| 632 |
} else { |
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
ds.deferred[id] = encoded |
| 637 |
} |
| 638 |
|
| 639 |
ods = nil |
| 640 |
encoded = nil |
| 641 |
default: |
| 642 |
Failf("wanted type or object ID, got %#v", encoded) |
| 643 |
} |
| 644 |
} |
| 645 |
}); err != nil { |
| 646 |
|
| 647 |
|
| 648 |
if ods != nil { |
| 649 |
Failf("error decoding object ID %d (%T) from %#v: %w", id, ods.obj.Interface(), encoded, err) |
| 650 |
} else if encoded != nil { |
| 651 |
Failf("error decoding from %#v: %w", encoded, err) |
| 652 |
} else { |
| 653 |
Failf("general decoding error: %w", err) |
| 654 |
} |
| 655 |
} |
| 656 |
|
| 657 |
|
| 658 |
numDeferred := 0 |
| 659 |
for id, encoded := range ds.deferred { |
| 660 |
numDeferred++ |
| 661 |
if s, ok := encoded.(*wire.Struct); ok && s.TypeID != 0 { |
| 662 |
typ := ds.types.LookupType(typeID(s.TypeID)) |
| 663 |
log.Warningf("unused deferred object: ID %d, type %v", id, typ) |
| 664 |
} else { |
| 665 |
log.Warningf("unused deferred object: ID %d, %#v", id, encoded) |
| 666 |
} |
| 667 |
} |
| 668 |
if numDeferred != 0 { |
| 669 |
Failf("still had %d deferred objects", numDeferred) |
| 670 |
} |
| 671 |
|
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
|
| 679 |
|
| 680 |
if err := safely(func() { |
| 681 |
for ds.pending.Back() != nil { |
| 682 |
thisCycle := false |
| 683 |
for ods = ds.pending.Back(); ods != nil; { |
| 684 |
if ds.checkComplete(ods) { |
| 685 |
thisCycle = true |
| 686 |
break |
| 687 |
} |
| 688 |
ods = ods.Prev() |
| 689 |
} |
| 690 |
if !thisCycle { |
| 691 |
break |
| 692 |
} |
| 693 |
} |
| 694 |
}); err != nil { |
| 695 |
Failf("error executing callbacks for %#v: %w", ods.obj.Interface(), err) |
| 696 |
} |
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
if ods := ds.pending.Front(); ods != nil { |
| 701 |
|
| 702 |
cycle := ods.findCycle() |
| 703 |
var buf bytes.Buffer |
| 704 |
buf.WriteString("dependency cycle: {") |
| 705 |
for i, cycleOS := range cycle { |
| 706 |
if i > 0 { |
| 707 |
buf.WriteString(" => ") |
| 708 |
} |
| 709 |
fmt.Fprintf(&buf, "%q", cycleOS.obj.Type()) |
| 710 |
} |
| 711 |
buf.WriteString("}") |
| 712 |
Failf("incomplete graph: %s", string(buf.Bytes())) |
| 713 |
} |
| 714 |
} |
| 715 |
|
| 716 |
|
| 717 |
|
| 718 |
|
| 719 |
|
| 720 |
|
| 721 |
func ReadHeader(r wire.Reader) (length uint64, object bool, err error) { |
| 722 |
|
| 723 |
err = safely(func() { |
| 724 |
length = wire.LoadUint(r) |
| 725 |
}) |
| 726 |
if err != nil { |
| 727 |
|
| 728 |
if sErr, ok := err.(*ErrState); ok { |
| 729 |
return 0, false, sErr.Unwrap() |
| 730 |
} |
| 731 |
} |
| 732 |
|
| 733 |
|
| 734 |
object = length&objectFlag != 0 |
| 735 |
length &^= objectFlag |
| 736 |
return |
| 737 |
} |