Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
jerrrrry
infinilm
Commits
6fcbb9ec
Commit
6fcbb9ec
authored
Jun 26, 2025
by
wooway777
Browse files
Removed a few redundant operations
parent
afab9951
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
8 additions
and
19 deletions
+8
-19
src/allocator.hpp
src/allocator.hpp
+0
-1
src/allocator/memory_allocator.cpp
src/allocator/memory_allocator.cpp
+8
-18
No files found.
src/allocator.hpp
View file @
6fcbb9ec
...
@@ -43,7 +43,6 @@ private:
...
@@ -43,7 +43,6 @@ private:
void
*
allocateNewRegion
(
size_t
size
);
void
*
allocateNewRegion
(
size_t
size
);
void
tryCoalesce
(
const
Block
&
block
);
void
tryCoalesce
(
const
Block
&
block
);
void
*
alignPointer
(
void
*
ptr
)
const
;
size_t
_alignment
;
size_t
_alignment
;
std
::
vector
<
void
*>
_base_regions
;
std
::
vector
<
void
*>
_base_regions
;
...
...
src/allocator/memory_allocator.cpp
View file @
6fcbb9ec
...
@@ -19,11 +19,6 @@ MemoryPool::~MemoryPool() {
...
@@ -19,11 +19,6 @@ MemoryPool::~MemoryPool() {
}
}
}
}
void
*
MemoryPool
::
alignPointer
(
void
*
ptr
)
const
{
return
reinterpret_cast
<
void
*>
(
(
reinterpret_cast
<
uintptr_t
>
(
ptr
)
+
_alignment
-
1
)
&
~
(
_alignment
-
1
));
}
void
*
MemoryPool
::
alloc
(
size_t
size
)
{
void
*
MemoryPool
::
alloc
(
size_t
size
)
{
if
(
size
==
0
)
{
if
(
size
==
0
)
{
return
nullptr
;
return
nullptr
;
...
@@ -48,29 +43,25 @@ void *MemoryPool::alloc(size_t size) {
...
@@ -48,29 +43,25 @@ void *MemoryPool::alloc(size_t size) {
_all_blocks
.
erase
(
block_it
);
_all_blocks
.
erase
(
block_it
);
// Align the pointer within the block
// Align the pointer within the block
void
*
aligned_ptr
=
alignPointer
(
block
.
ptr
);
size_t
alignment_padding
=
reinterpret_cast
<
char
*>
(
block
.
ptr
)
-
reinterpret_cast
<
char
*>
(
block
.
ptr
);
size_t
alignment_padding
=
reinterpret_cast
<
char
*>
(
aligned_ptr
)
-
reinterpret_cast
<
char
*>
(
block
.
ptr
);
// Calculate remaining space after allocation
// Calculate remaining space after allocation
const
size_t
remaining
=
block
.
size
-
aligned_size
-
alignment_padding
;
const
size_t
remaining
=
block
.
size
-
aligned_size
-
alignment_padding
;
// Create allocated block
// Create allocated block
Block
alloc_block
(
block
.
base
,
aligned_
ptr
,
aligned_size
,
false
);
Block
alloc_block
(
block
.
base
,
block
.
ptr
,
aligned_size
,
false
);
auto
alloc_it
=
_all_blocks
.
insert
(
alloc_block
).
first
;
auto
alloc_it
=
_all_blocks
.
insert
(
alloc_block
).
first
;
_ptr_to_block
[
aligned_
ptr
]
=
alloc_it
;
_ptr_to_block
[
block
.
ptr
]
=
alloc_it
;
// Split remaining space if it's large enough
// Split remaining space if it's large enough
if
(
remaining
>=
_alignment
)
{
if
(
remaining
>=
_alignment
)
{
void
*
rem_ptr
=
static_cast
<
char
*>
(
aligned_
ptr
)
+
aligned_size
;
void
*
rem_ptr
=
static_cast
<
char
*>
(
block
.
ptr
)
+
aligned_size
;
Block
rem_block
(
block
.
base
,
rem_ptr
,
remaining
,
true
);
Block
rem_block
(
block
.
base
,
rem_ptr
,
remaining
,
true
);
auto
rem_it
=
_all_blocks
.
insert
(
rem_block
).
first
;
auto
rem_it
=
_all_blocks
.
insert
(
rem_block
).
first
;
_free_blocks
.
emplace
(
remaining
,
rem_it
);
_free_blocks
.
emplace
(
remaining
,
rem_it
);
}
else
{
// If remaining space is too small, include it in the allocated block
alloc_block
.
size
+=
remaining
;
}
}
return
aligned_
ptr
;
return
block
.
ptr
;
}
}
void
MemoryPool
::
release
(
void
*
ptr
)
{
void
MemoryPool
::
release
(
void
*
ptr
)
{
...
@@ -99,15 +90,14 @@ void *MemoryPool::allocateNewRegion(size_t size) {
...
@@ -99,15 +90,14 @@ void *MemoryPool::allocateNewRegion(size_t size) {
_base_regions
.
push_back
(
ptr
);
_base_regions
.
push_back
(
ptr
);
// Align the pointer within the allocated region
// Align the pointer within the allocated region
void
*
aligned_ptr
=
alignPointer
(
ptr
);
size_t
alignment_padding
=
reinterpret_cast
<
char
*>
(
ptr
)
-
reinterpret_cast
<
char
*>
(
ptr
);
size_t
alignment_padding
=
reinterpret_cast
<
char
*>
(
aligned_ptr
)
-
reinterpret_cast
<
char
*>
(
ptr
);
size_t
usable_size
=
size
-
alignment_padding
;
size_t
usable_size
=
size
-
alignment_padding
;
Block
new_block
(
ptr
,
aligned_
ptr
,
usable_size
,
true
);
Block
new_block
(
ptr
,
ptr
,
usable_size
,
true
);
auto
it
=
_all_blocks
.
insert
(
new_block
).
first
;
auto
it
=
_all_blocks
.
insert
(
new_block
).
first
;
_free_blocks
.
emplace
(
usable_size
,
it
);
_free_blocks
.
emplace
(
usable_size
,
it
);
return
aligned_
ptr
;
return
ptr
;
}
}
void
MemoryPool
::
tryCoalesce
(
const
Block
&
block
)
{
void
MemoryPool
::
tryCoalesce
(
const
Block
&
block
)
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment