Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
malloc
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
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
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
Daniel Shchur
malloc
Commits
38f51c32
Commit
38f51c32
authored
5 years ago
by
DocQuantum
Browse files
Options
Downloads
Patches
Plain Diff
Implemented realloc -- 83% performance
parent
dc005608
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
mm.c
+13
-17
13 additions, 17 deletions
mm.c
with
13 additions
and
17 deletions
mm.c
+
13
−
17
View file @
38f51c32
...
...
@@ -245,8 +245,6 @@ void *mm_realloc(void *ptr, size_t size)
* contents of the new block are identical to the first 4 bytes of the old block.
*/
return
NULL
;
if
(
ptr
==
NULL
&&
size
>
0
){
return
mm_malloc
(
size
);
}
else
if
(
ptr
!=
NULL
&&
size
==
0
)
{
...
...
@@ -256,30 +254,25 @@ void *mm_realloc(void *ptr, size_t size)
size_t
oldSize
=
GET_SIZE
(
HDRP
(
ptr
));
/* Adjust block size to include overhead and alignment reqs. */
if
(
size
<=
W
SIZE
)
{
size
=
W
SIZE
+
OVERHEAD
;
if
(
size
<=
D
SIZE
)
{
size
=
MIN
SIZE
;
}
else
{
size
=
DSIZE
*
((
size
+
OVERHEAD
+
DSIZE
-
1
)
/
DSIZE
);
}
// if sizes are the same or the difference is less than a
D
SIZE, no changes needed
if
(
oldSize
==
size
||
(
oldSize
-
size
<
=
W
SIZE
))
{
// if sizes are the same or the difference is less than a
MIN
SIZE, no changes needed
if
(
oldSize
==
size
||
(
oldSize
-
size
<
MIN
SIZE
))
{
return
ptr
;
}
else
{
// If size is less than old size, shrink and free end
if
(
size
<
oldSize
)
{
if
((
oldSize
-
size
)
>=
DSIZE
)
{
PUT
(
HDRP
(
ptr
),
PACK
(
size
,
0
));
PUT
(
FTRP
(
ptr
),
PACK
(
size
,
0
));
PUT
(
HDRP
(
NEXT_BLKP
(
ptr
)),
PACK
(
oldSize
-
size
,
1
));
PUT
(
FTRP
(
NEXT_BLKP
(
ptr
)),
PACK
(
oldSize
-
size
,
1
));
}
else
{
PUT
(
HDRP
(
ptr
),
PACK
(
oldSize
,
0
));
PUT
(
FTRP
(
ptr
),
PACK
(
oldSize
,
0
));
}
add_to_list
(
NEXT_BLKP
(
ptr
));
return
ptr
;
}
...
...
@@ -287,13 +280,16 @@ void *mm_realloc(void *ptr, size_t size)
else
if
(
size
>
oldSize
)
{
size_t
nextSize
=
GET_SIZE
(
HDRP
(
NEXT_BLKP
(
ptr
)))
+
oldSize
;
if
(
GET_ALLOC
(
HDRP
(
NEXT_BLKP
(
ptr
)))
&&
nextSize
>=
size
){
if
((
nextSize
-
size
)
>=
DSIZE
)
{
if
((
nextSize
-
size
)
>=
MINSIZE
)
{
remove_from_list
(
NEXT_BLKP
(
ptr
));
PUT
(
HDRP
(
ptr
),
PACK
(
size
,
0
));
PUT
(
FTRP
(
ptr
),
PACK
(
size
,
0
));
PUT
(
HDRP
(
NEXT_BLKP
(
ptr
)),
PACK
(
nextSize
-
size
,
1
));
PUT
(
FTRP
(
NEXT_BLKP
(
ptr
)),
PACK
(
nextSize
-
size
,
1
));
add_to_list
(
NEXT_BLKP
(
ptr
));
}
else
{
remove_from_list
(
NEXT_BLKP
(
ptr
));
PUT
(
HDRP
(
ptr
),
PACK
(
nextSize
,
0
));
PUT
(
FTRP
(
ptr
),
PACK
(
nextSize
,
0
));
}
...
...
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