Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
Advanced systems programming project one
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
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
fork proxy
Advanced systems programming project one
Commits
61b725fc
Commit
61b725fc
authored
6 months ago
by
j2-pelczar
Browse files
Options
Downloads
Patches
Plain Diff
Upload New File
parent
09499ab7
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
worksheet_two/task_1_bump_allocator/bump_allocator.hpp
+59
-0
59 additions, 0 deletions
worksheet_two/task_1_bump_allocator/bump_allocator.hpp
with
59 additions
and
0 deletions
worksheet_two/task_1_bump_allocator/bump_allocator.hpp
0 → 100644
+
59
−
0
View file @
61b725fc
#ifndef BUMP_ALLOCATOR_HPP
#define BUMP_ALLOCATOR_HPP
#include
<cstddef>
// For std::size_t
#include
<iostream>
class
BumpAllocator
{
private:
char
*
memory
;
// Start of the memory chunk
char
*
next
;
// Current bump pointer
std
::
size_t
capacity
;
// Total capacity of the allocator
std
::
size_t
used
;
// Number of allocations made
public:
// Constructor: initializes the heap with a given size
explicit
BumpAllocator
(
std
::
size_t
size
)
:
memory
(
new
char
[
size
]),
next
(
memory
),
capacity
(
size
),
used
(
0
)
{}
// Destructor: frees the allocated memory
~
BumpAllocator
()
{
delete
[]
memory
;
}
// Allocate memory for N objects of type T
template
<
typename
T
>
T
*
alloc
(
std
::
size_t
count
=
1
)
{
std
::
size_t
requiredBytes
=
count
*
sizeof
(
T
);
if
(
next
+
requiredBytes
>
memory
+
capacity
)
{
std
::
cerr
<<
"Out of memory: cannot allocate "
<<
requiredBytes
<<
" bytes.
\n
"
;
return
nullptr
;
}
T
*
allocation
=
reinterpret_cast
<
T
*>
(
next
);
next
+=
requiredBytes
;
++
used
;
return
allocation
;
}
// Deallocate memory (only works when all allocations are freed)
void
dealloc
()
{
if
(
used
>
0
)
{
--
used
;
if
(
used
==
0
)
{
next
=
memory
;
// Reset the bump pointer
}
}
else
{
std
::
cerr
<<
"Error: No allocations to deallocate.
\n
"
;
}
}
// Display allocator status
void
printStatus
()
const
{
std
::
cout
<<
"Allocator status:
\n
"
;
std
::
cout
<<
" Capacity: "
<<
capacity
<<
" bytes
\n
"
;
std
::
cout
<<
" Used: "
<<
(
next
-
memory
)
<<
" bytes
\n
"
;
std
::
cout
<<
" Allocations: "
<<
used
<<
"
\n
"
;
}
};
#endif
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