Skip to content

Commit

Permalink
implement Drop for deallocation
Browse files Browse the repository at this point in the history
  • Loading branch information
dbalsom committed Jul 6, 2024
1 parent 9338ccf commit 4984cbb
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@ A small library to provide bindings for the [Nuked-OPL3 library](https://github.
# Usage

Nuked-OPL3 is not a turn-key implementation of the OPL3 chip - functions such as the status register, timers and
interrupts are left as implementation details. Eventually this library will aim to provide a wrapper type that will
provide example implementations for easier integrations with emulators.
interrupts are left as implementation details.

For now, you can access the Nuked-OPL3 API via the `Opl3Chip` struct.
You can access the Nuked-OPL3 API via the `Opl3Chip` struct, if needed, but with the caveat that directly writing
registers to Nuked-OPL3 will prevent you from reading the OPL registers correctly.

If you intend to utilize `opl3-rs` in an emulator, you will probably want to use the `Opl3Device` wrapper which provides
a full, device-oriented OPL3 implementation including the status, address and data registers, plus the OPL3 timers.

# Docs

Expand All @@ -24,6 +27,9 @@ Documentation can be found on [docs.rs](https://docs.rs/opl3-rs/latest/opl3_rs/)
An example of music playback is provided in the play_tune directory under /examples.
This example uses the rodio library for audio playback and crossbeam channels for inter-thread communication.

opl3-rs was primarily built for use with the [MartyPC PC emulator](https://github.com/dbalsom/martypc). It is used to
implement an AdLib Music Card device.

# Credits

[Nuked-OPL3](https://github.com/nukeykt/Nuked-OPL3) is (C) 2013-2020 Nuke.YKT and licensed under LGPL 2.1
Expand Down
12 changes: 9 additions & 3 deletions src/docs.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
# opl3-rs

`opl3-rs` is a small library to provide bindings for the [Nuked-OPL3 library](https://github.com/nukeykt/Nuked-OPL3).

# Usage

Nuked-OPL3 is not a turn-key implementation of the OPL3 chip - functions such as the status register, timers and
interrupts are left as implementation details. Eventually this library will aim to provide a wrapper type that will
provide example implementations for easier integrations with emulators.
interrupts are left as implementation details.

For now, you can access the Nuked-OPL3 API via the `Opl3Chip` struct.
You can access the Nuked-OPL3 API via the `Opl3Chip` struct, if needed, but with the caveat that directly writing
registers to Nuked-OPL3 will prevent you from reading the OPL registers correctly.

If you intend to utilize `opl3-rs` in an emulator, you will probably want to use the `Opl3Device` wrapper which provides
a full OPL3 implementation including the status registers and timers.

# Credits

[Nuked-OPL3](https://github.com/nukeykt/Nuked-OPL3) is (C) 2013-2020 Nuke.YKT and licensed under LGPL 2.1
10 changes: 10 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,16 @@ pub struct Opl3Chip {
chip: *mut bindings::Opl3Chip,
}

impl Drop for Opl3Chip {
/// Drop the Opl3Chip instance by deallocating the memory used by the Nuked-OPL3 instance.
fn drop(&mut self) {
unsafe {
let layout = std::alloc::Layout::new::<bindings::Opl3Chip>();
std::alloc::dealloc(self.chip as *mut u8, layout);
}
}
}

impl Opl3Chip {
/// Creates a new OPL3 chip instance. The chip is initialized with the given sample rate.
/// The internal chip device is Pinned to ensure that it is not moved in memory. The Nuked-OPL3
Expand Down

0 comments on commit 4984cbb

Please sign in to comment.