Skip to content
GitLab
Explore
Projects
Groups
Topics
Snippets
Projects
Groups
Topics
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Han zheli
halOS
Commits
d02dcec8
Commit
d02dcec8
authored
3 years ago
by
liyeshen
Browse files
Options
Download
Patches
Plain Diff
Update umalloc.c
parent
37ad6179
patch-7
1 merge request
!6
Patch 7
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
user/umalloc.c
+4
-52
user/umalloc.c
with
4 additions
and
52 deletions
+4
-52
user/umalloc.c
+
4
−
52
View file @
d02dcec8
#include
"kernel/libs/types.h"
#include
"kernel/fat32/stat.h"
#include
"../kernel/libs/types.h"
#include
"user.h"
#include
"kernel/libs/param.h"
#include
"
../
kernel/libs/param.h"
#include
"../kernel/fat32/stat.h"
// Memory allocator by Kernighan and Ritchie,
// The C programming Language, 2nd ed. Section 8.7.
typedef
long
Align
;
union
header
{
...
...
@@ -18,14 +16,13 @@ union header {
typedef
union
header
Header
;
static
Header
base
;
//
static Header base;
static
Header
*
freep
;
void
free
(
void
*
ap
)
{
Header
*
bp
,
*
p
;
bp
=
(
Header
*
)
ap
-
1
;
for
(
p
=
freep
;
!
(
bp
>
p
&&
bp
<
p
->
s
.
ptr
);
p
=
p
->
s
.
ptr
)
if
(
p
>=
p
->
s
.
ptr
&&
(
bp
>
p
||
bp
<
p
->
s
.
ptr
))
...
...
@@ -43,48 +40,3 @@ free(void *ap)
freep
=
p
;
}
static
Header
*
morecore
(
uint
nu
)
{
char
*
p
;
Header
*
hp
;
if
(
nu
<
4096
)
nu
=
4096
;
p
=
sbrk
(
nu
*
sizeof
(
Header
));
if
(
p
==
(
char
*
)
-
1
)
return
0
;
hp
=
(
Header
*
)
p
;
hp
->
s
.
size
=
nu
;
free
((
void
*
)(
hp
+
1
));
return
freep
;
}
void
*
malloc
(
uint
nbytes
)
{
Header
*
p
,
*
prevp
;
uint
nunits
;
nunits
=
(
nbytes
+
sizeof
(
Header
)
-
1
)
/
sizeof
(
Header
)
+
1
;
if
((
prevp
=
freep
)
==
0
){
base
.
s
.
ptr
=
freep
=
prevp
=
&
base
;
base
.
s
.
size
=
0
;
}
for
(
p
=
prevp
->
s
.
ptr
;
;
prevp
=
p
,
p
=
p
->
s
.
ptr
){
if
(
p
->
s
.
size
>=
nunits
){
if
(
p
->
s
.
size
==
nunits
)
prevp
->
s
.
ptr
=
p
->
s
.
ptr
;
else
{
p
->
s
.
size
-=
nunits
;
p
+=
p
->
s
.
size
;
p
->
s
.
size
=
nunits
;
}
freep
=
prevp
;
return
(
void
*
)(
p
+
1
);
}
if
(
p
==
freep
)
if
((
p
=
morecore
(
nunits
))
==
0
)
return
0
;
}
}
This diff is collapsed.
Click to expand it.
Write
Preview
Supports
Markdown
0%
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
Menu
Explore
Projects
Groups
Topics
Snippets