Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
ASP
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
j2-tulloch
ASP
Commits
eb891da6
Commit
eb891da6
authored
1 year ago
by
j2-tulloch
Browse files
Options
Downloads
Patches
Plain Diff
Upload bumpall.hpp
parent
b3ec1717
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
Worksheet2/bumpall.hpp
+40
-0
40 additions, 0 deletions
Worksheet2/bumpall.hpp
with
40 additions
and
0 deletions
Worksheet2/bumpall.hpp
0 → 100644
+
40
−
0
View file @
eb891da6
#include
<cstddef>
#include
<iostream>
class
BumpAllocator
{
char
*
start
;
// Pointer to the start of the allocated memory block
char
*
current
;
// Pointer to the current position in the memory block
size_t
size
;
// Total size of the memory block
public:
// Constructor for the BumpAllocator
BumpAllocator
(
size_t
size
)
:
size
(
size
)
{
start
=
new
char
[
size
];
// Allocate a memory block of the given size
current
=
start
;
// Initialize the current pointer to the start of the block
}
void
*
allocate
(
size_t
allocSize
)
{
// Check if there is enough space left in the block for the requested size
if
(
current
+
allocSize
-
start
>
size
)
{
//std::cout << "Not enough space in the block for allocation!" << std::endl;
return
nullptr
;
// Return nullptr if there is not enough space
}
void
*
alloc
=
current
;
// Store the current position to return
current
+=
allocSize
;
// "Bump" the current pointer forward by the requested size
//std::cout << "Memory allocated successfully!" << std::endl;
return
alloc
;
// Return the original current position
}
// Function to reset the allocator
void
reset
()
{
current
=
start
;
// Reset the current pointer back to the start of the block
//std::cout << "Allocator reset!" << std::endl;
}
// Destructor for the BumpAllocator
~
BumpAllocator
()
{
delete
[]
start
;
// Deallocate the memory block when the allocator is destroyed
//std::cout << "Memory block deallocated!" << std::endl;
}
};
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment