Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate string_buffer to use C++ smart pointer. #7351

Closed
wants to merge 3 commits into from

Conversation

saikat107
Copy link

  1. string_buffer class is now migrated to use smart pointers instead of manually allocated and deallocated memory.
    The buffer function decides and returns if the buffer is currently using initial buffer or the allocated buffer.
  2. Added tst_small_buffer_expand to test memory expansion of the small buffer, i.e., the test forces the buffer to go from m_initial_buffer to dynamically allocated m_buffer.

string_buffer class is now migrated to use smart pointers instead of manually allocated and deallocated memory.
The `buffer` function decides and returns if the buffer is currently using initial buffer or the allocated buffer.
@NikolajBjorner
Copy link
Contributor

alloc/dealloc use memory_manager. STL allows associating custom allocators with classes that call new/delete.
Better to have a uniqe_ptr wrapper that ensures all memory uses memory_manager.

To decide whether `m_aig_manager` should be a `std::unique_ptr`, `std::shared_ptr`, or `std::weak_ptr`, let's analyze the ownership semantics and usage pattern of `m_aig_manager` in the `aig_tactic` class.

Analysis of Ownership:

1. **Single Ownership**:
   - `m_aig_manager` is allocated within the `mk_aig_manager` struct's constructor, where it calls `alloc(aig_manager, ...)`, and deallocated within the `mk_aig_manager` destructor using `dealloc(m_owner.m_aig_manager);`.
   - `m_aig_manager` is not shared or referenced by any other part of the code outside of `aig_tactic` and `mk_aig_manager`. This suggests that `aig_manager` is owned exclusively by the `aig_tactic` class.

2. **Lifespan Management**:
   - `m_aig_manager` is created within `mk_aig_manager` and is automatically destroyed when `mk_aig_manager` goes out of scope (which happens after the tactic is applied).
   - This indicates that `m_aig_manager`'s lifespan is tightly coupled with the `aig_tactic` class or its method execution.

3. **No Sharing Needed**:
   - There is no indication that `m_aig_manager` needs to be shared across multiple owners or instances. Therefore, there’s no need for `std::shared_ptr`.
   - Since there’s no sharing, `std::weak_ptr` is also unnecessary.

Conclusion:

Given the analysis, **`std::unique_ptr`** is the most appropriate choice for managing `m_aig_manager`. It ensures that the memory is properly managed (automatically deleted when the pointer goes out of scope), and it enforces the exclusive ownership semantics that are currently implied by the code.
@nunoplopes
Copy link
Collaborator

Plus the changes to string_buffer remove several of optimizations that it has. I don't see the point of this PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants