Showing the changes to src/vm/sys/Sysmod.cpp (1 of 1 file). Show all
Unified · Split
| @@ -118,22 +118,27 @@ namespace · 117 unmodified lines | ||
| 118 | 118 | } |
| 119 | 119 | } |
| 120 | 120 | |
| 121 | − static vm_alloc_t * create(vm_fd_t *f) | |
| 121 | + // The ref count of the supplied vm_fd_t instance is updated. | |
| 122 | + static vm_alloc_t * create_new(vm_fd_t *vm_fd) | |
| 122 | 123 | { |
| 124 | + assert(vm_fd != nullptr); | |
| 125 | + assert(vm_fd->get_ref_count() == 1 && "Should be a new FD"); | |
| 126 | + | |
| 123 | 127 | auto alloc = vm_alloc_t::allocate(T_FD); |
| 124 | 128 | auto fd_impl = ::new(alloc->get_allocation())Sys_FD_Impl{}; |
| 125 | 129 | |
| 126 | − fd_impl->fd = disvm::runtime::sys::create_fd_record(f); | |
| 127 | − fd_impl->impl = f; | |
| 130 | + fd_impl->fd = disvm::runtime::sys::create_fd_record(vm_fd); | |
| 131 | + fd_impl->impl = vm_fd; | |
| 132 | + vm_fd->add_ref(); | |
| 128 | 133 | |
| 129 | 134 | return alloc; |
| 130 | 135 | } |
| 131 | 136 | |
| 132 | − static vm_alloc_t * try_create(word_t fd) | |
| 137 | + // The ref count of the supplied vm_fd_t instance is _not_ altered. | |
| 138 | + static vm_alloc_t * create_existing(word_t fd, vm_fd_t *vm_fd) | |
| 133 | 139 | { |
| 134 | − auto vm_fd = disvm::runtime::sys::fetch_fd_record(fd); | |
| 135 | − if (vm_fd == nullptr) | |
| 136 | − return nullptr; | |
| 140 | + assert(vm_fd != nullptr); | |
| 141 | + assert(fd >= 0); | |
| 137 | 142 | |
| 138 | 143 | auto alloc = vm_alloc_t::allocate(T_FD); |
| 139 | 144 | auto fd_impl = ::new(alloc->get_allocation())Sys_FD_Impl{}; |
| @@ -148,6 +153,7 @@ namespace · 8 unmodified lines | ||
| 148 | 153 | ~Sys_FD_Impl() |
| 149 | 154 | { |
| 150 | 155 | disvm::runtime::sys::drop_fd_record(fd); |
| 156 | + disvm::runtime::dec_ref_count_and_free(impl); | |
| 151 | 157 | disvm::debug::assign_debug_pointer(&impl); |
| 152 | 158 | } |
| 153 | 159 | |
| @@ -381,7 +387,7 @@ Sys_create(vm_registers_t &r, vm_t &vm) · 227 unmodified lines | ||
| 381 | 387 | if (vm_fd == nullptr) |
| 382 | 388 | return; |
| 383 | 389 | |
| 384 | − auto fd_alloc = Sys_FD_Impl::create(vm_fd); | |
| 390 | + auto fd_alloc = Sys_FD_Impl::create_new(vm_fd); | |
| 385 | 391 | *fp.ret = fd_alloc->get_allocation(); |
| 386 | 392 | |
| 387 | 393 | if (disvm::debug::is_component_tracing_enabled<component_trace_t::builtin>()) |
| @@ -480,9 +486,12 @@ Sys_fildes(vm_registers_t &r, vm_t &vm) · 92 unmodified lines | ||
| 480 | 486 | dec_ref_count_and_free(vm_alloc_t::from_allocation(*fp.ret)); |
| 481 | 487 | *fp.ret = nullptr; |
| 482 | 488 | |
| 483 | − auto fd_alloc = Sys_FD_Impl::try_create(fp.fd); | |
| 484 | − if (fd_alloc != nullptr) | |
| 489 | + auto vm_fd = disvm::runtime::sys::fetch_fd_record(fp.fd); | |
| 490 | + if (vm_fd != nullptr) | |
| 491 | + { | |
| 492 | + auto fd_alloc = Sys_FD_Impl::create_existing(fp.fd, vm_fd); | |
| 485 | 493 | *fp.ret = fd_alloc->get_allocation(); |
| 494 | + } | |
| 486 | 495 | } |
| 487 | 496 | |
| 488 | 497 | void |
| @@ -580,7 +589,7 @@ Sys_open(vm_registers_t &r, vm_t &vm) · 91 unmodified lines | ||
| 580 | 589 | if (vm_fd == nullptr) |
| 581 | 590 | return; |
| 582 | 591 | |
| 583 | − auto fd_alloc = Sys_FD_Impl::create(vm_fd); | |
| 592 | + auto fd_alloc = Sys_FD_Impl::create_new(vm_fd); | |
| 584 | 593 | *fp.ret = fd_alloc->get_allocation(); |
| 585 | 594 | |
| 586 | 595 | if (disvm::debug::is_component_tracing_enabled<component_trace_t::builtin>()) |
| @@ -631,6 +640,7 @@ Sys_print(vm_registers_t &r, vm_t &vm) · 44 unmodified lines | ||
| 631 | 640 | throw vm_system_exception{ "stdout not available" }; |
| 632 | 641 | |
| 633 | 642 | *fp.ret = printf_to_fd(vm, *std_output, *str, &fp.vargs, fp_base); |
| 643 | + disvm::runtime::dec_ref_count_and_free(std_output); | |
| 634 | 644 | } |
| 635 | 645 | |
| 636 | 646 | void |