| 354 | } |
| 355 | |
| 356 | static rt_varea_t _merge_surrounding(rt_aspace_t aspace, rt_varea_t operand, |
| 357 | struct _mapping_property *prop) |
| 358 | { |
| 359 | int again; |
| 360 | rt_err_t error; |
| 361 | int can_merge_fw; |
| 362 | int can_merge_bw; |
| 363 | rt_varea_t neighbour; |
| 364 | char *operand_start; |
| 365 | size_t operand_size; |
| 366 | rt_err_t (*on_varea_merge)(struct rt_varea *to, struct rt_varea *from); |
| 367 | |
| 368 | if (operand->mem_obj && operand->mem_obj->on_varea_merge) |
| 369 | { |
| 370 | on_varea_merge = operand->mem_obj->on_varea_merge; |
| 371 | do { |
| 372 | operand_start = operand->start; |
| 373 | operand_size = operand->size; |
| 374 | LOG_D("search op_start=%p,op_size=0x%lx", operand_start, operand_size); |
| 375 | |
| 376 | /* find a compatible neighbour if any and setup direction */ |
| 377 | can_merge_fw = can_merge_bw = 0; |
| 378 | neighbour = _aspace_bst_search(aspace, operand_start - 1); |
| 379 | if (!neighbour || !_compatible(neighbour, operand_size, prop)) |
| 380 | { |
| 381 | neighbour = _aspace_bst_search(aspace, operand_start + operand_size); |
| 382 | if (neighbour && _compatible(neighbour, operand_size, prop)) |
| 383 | can_merge_bw = 1; |
| 384 | } |
| 385 | else |
| 386 | can_merge_fw = 1; |
| 387 | |
| 388 | if (can_merge_fw || can_merge_bw) |
| 389 | { |
| 390 | /* merge operand with its predecessor or successor */ |
| 391 | if (can_merge_fw) |
| 392 | { |
| 393 | error = _migrate_and_release_varea(aspace, neighbour, operand, on_varea_merge); |
| 394 | operand = neighbour; |
| 395 | } |
| 396 | else |
| 397 | error = _migrate_and_release_varea(aspace, operand, neighbour, on_varea_merge); |
| 398 | |
| 399 | if (error == RT_EOK) |
| 400 | again = 1; |
| 401 | } |
| 402 | else |
| 403 | again = 0; |
| 404 | |
| 405 | } while (again); |
| 406 | } |
| 407 | return operand; |
| 408 | } |
| 409 | |
| 410 | /** |
| 411 | * Brief: expand and merge surrounding until not possible and |
no test coverage detected