Sweep: create stack allocator class
chocolacula opened this issue · 1 comments
Details
Feature: create a new class in library/include/er/ with name StackAlloc
which implements C++ 17 allocator. It should have additional template parameter size
with type size_t
which means size of bytes preallocated on stack. If T
has sizeof > size
the memory will allocate on the heap, else on stack.
Checklist
-
library/include/er/stack_alloc.h
✅ Commit37ab61b
Here's the PR! #16.
Actions (click)
- ↻ Restart Sweep
Step 1: 🔎 Searching
I found the following snippets in your repository. I will now analyze these snippets and come up with a plan.
Some code snippets I looked at (click to expand). If some file is missing from here, you can mention the path in the ticket description.
easy_reflection_cpp/library/include/er/variable/box.h
Lines 1 to 34 in 28a5b85
Step 2: ⌨️ Coding
-
library/include/er/stack_alloc.h
✅ Commit37ab61b
• Begin by including the necessary headers. This will likely include `` for the allocator, `` for placement new, and `` for `std::max`.
• Declare the `StackAlloc` class within the `er` namespace. It should be a template class with two parameters: `T` and `size`.
• The `StackAlloc` class should have the following public types, as required by the Allocator concept: `value_type`, `pointer`, `const_pointer`, `reference`, `const_reference`, `size_type`, and `difference_type`.
• Implement the `allocate` function. This function should check if the size of `T` is greater than `size`. If it is, it should use `::operator new` to allocate memory on the heap. Otherwise, it should use placement new to allocate memory on the stack.
• Implement the `deallocate` function. This function should check if the pointer passed to it points to the stack or the heap, and use the appropriate delete operator.
• Implement the `construct` and `destroy` functions. These functions should use placement new and explicit destructor invocation, respectively.
• Implement the `max_size` function. This function should return the maximum number of objects that can be allocated by the allocator.
• Implement the `address` function. This function should return the address of an object.
• Implement the `StackAlloc` copy constructor and copy assignment operator. These should be trivial, as the `StackAlloc` class does not own any resources.
• Implement the `StackAlloc` move constructor and move assignment operator. These should also be trivial, as the `StackAlloc` class does not own any resources.
• Implement the `StackAlloc` destructor. This should also be trivial, as the `StackAlloc` class does not own any resources.
Step 3: 🔁 Code Review
I have finished reviewing the code for completeness. I did not find errors for sweep/stack_alloc_class
.
.
🎉 Latest improvements to Sweep:
- Sweep can now passively improve your repository! Check out Rules to learn more.
💡 To recreate the pull request edit the issue title or description. To tweak the pull request, leave a comment on the pull request.
Join Our Discord