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
9e485bb8
Commit
9e485bb8
authored
6 months ago
by
j2-pelczar
Browse files
Options
Downloads
Patches
Plain Diff
Upload New File
parent
43a39eac
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_2_unit_tests/bump_allocator.hpp
+59
-0
59 additions, 0 deletions
worksheet_two/task_2_unit_tests/bump_allocator.hpp
with
59 additions
and
0 deletions
worksheet_two/task_2_unit_tests/bump_allocator.hpp
0 → 100644
+
59
−
0
View file @
9e485bb8
#ifndef BUMP_ALLOCATOR_HPP
#define BUMP_ALLOCATOR_HPP
#include
<cstddef>
#include
<iostream>
class
BumpAllocator
{
private:
char
*
memory
;
char
*
next
;
std
::
size_t
capacity
;
std
::
size_t
used
;
public:
explicit
BumpAllocator
(
std
::
size_t
size
)
:
memory
(
new
char
[
size
]),
next
(
memory
),
capacity
(
size
),
used
(
0
)
{}
~
BumpAllocator
()
{
delete
[]
memory
;
}
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
;
}
void
dealloc
()
{
if
(
used
>
0
)
{
--
used
;
if
(
used
==
0
)
{
next
=
memory
;
}
}
else
{
std
::
cerr
<<
"Error: No allocations to deallocate.
\n
"
;
}
}
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