Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
1
1801_OS_assignment4
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
김기훈
1801_OS_assignment4
Commits
36358c94
Commit
36358c94
authored
7 years ago
by
김기훈
Browse files
Options
Downloads
Patches
Plain Diff
last submit_assignment4
parent
8be41b8b
No related branches found
No related tags found
No related merge requests found
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
alloc.c
+135
-0
135 additions, 0 deletions
alloc.c
alloc.h
+9
-1
9 additions, 1 deletion
alloc.h
main
+0
-0
0 additions, 0 deletions
main
main.c
+53
-1
53 additions, 1 deletion
main.c
with
197 additions
and
2 deletions
alloc.c
+
135
−
0
View file @
36358c94
#include
"alloc.h"
#include
"alloc.h"
#include
<unistd.h>
#include
<memory.h>
#include
<stdio.h>
void
*
m_malloc
(
size_t
size
);
void
m_free
(
void
*
ptr
);
void
*
m_realloc
(
void
*
ptr
,
size_t
size
);
meta
*
meta1
=
NULL
;
meta
*
meta2
=
NULL
;
int
mode
=
-
1
;
void
select_mode
(
char
cr
){
if
(
cr
==
'F'
){
mode
=
1
;
}
else
if
(
cr
==
'B'
){
mode
=
2
;
}
else
if
(
cr
==
'W'
){
mode
=
3
;
}
}
meta
*
find_block
(
size_t
size2
){
meta
*
temp
=
meta1
;
meta
*
block
=
NULL
;
while
(
temp
!=
0
){
if
(
temp
->
free
!=
0
&&
temp
->
size
>=
size2
){
if
(
mode
==
1
){
return
temp
;
}
else
if
(
mode
==
2
){
if
(
block
==
0
||
(
block
->
size
-
size2
>
temp
->
size
-
size2
)){
block
=
temp
;
}
}
else
if
(
mode
==
3
){
if
(
block
==
0
||
block
->
size
<
temp
->
size
){
block
=
temp
;
}
}
}
temp
=
temp
->
next
;
}
return
block
;
}
void
*
m_malloc
(
size_t
size
){
if
(
mode
==-
1
||
size
<=
0
){
return
NULL
;
}
if
(
meta1
==
NULL
){
meta1
=
sbrk
(
size
+
sizeof
(
meta
));
meta1
->
prev
=
NULL
;
meta1
->
next
=
NULL
;
meta1
->
free
=
0
;
meta1
->
size
=
size
;
meta2
=
meta1
;
return
meta1
+
sizeof
(
meta
);
}
meta
*
block
=
find_block
(
size
);
if
(
block
==
0
){
block
=
sbrk
(
size
+
sizeof
(
meta
));
block
->
prev
=
meta2
;
block
->
next
=
NULL
;
block
->
free
=
0
;
block
->
size
=
size
;
meta2
->
next
=
block
;
meta2
=
block
;
return
block
+
sizeof
(
meta
);
}
block
->
free
=
0
;
if
(
block
->
size
-
size
>
sizeof
(
meta
)){
block
->
size
=
size
;
meta
*
block2
=
block
+
sizeof
(
meta
)
+
size
;
block2
->
prev
=
block
;
block2
->
next
=
block
->
next
;
block2
->
free
=
1
;
block2
->
size
=
block
->
size
-
(
size
+
sizeof
(
meta
));
block
->
next
=
block2
;
}
return
block
+
sizeof
(
meta
);
}
void
m_free
(
void
*
ptr
){
meta
*
temp
=
(
meta
*
)
ptr
-
sizeof
(
meta
);
temp
->
free
=
1
;
if
(
temp
->
prev
!=
0
&&
temp
->
prev
->
free
!=
0
){
temp
->
next
->
prev
=
temp
->
prev
;
temp
->
prev
->
next
=
temp
->
next
;
temp
->
prev
->
size
+=
temp
->
size
;
temp
=
temp
->
prev
;
}
if
(
temp
->
next
!=
0
&&
temp
->
next
->
free
!=
0
){
temp
->
size
+=
temp
->
next
->
size
;
if
(
temp
->
next
==
meta2
){
meta2
=
temp
;
}
temp
->
next
=
temp
->
next
->
next
;
if
(
temp
->
next
->
next
!=
0
){
temp
->
next
->
next
->
prev
=
temp
;
}
}
while
(
meta2
!=
0
&&
meta2
->
free
!=
0
){
meta2
=
meta2
->
prev
;
if
(
meta2
!=
0
){
meta2
->
next
=
NULL
;
}
else
{
meta1
=
NULL
;
}
brk
(
meta2
);
}
}
void
*
m_realloc
(
void
*
ptr
,
size_t
size
){
meta
*
temp
=
(
meta
*
)
ptr
-
sizeof
(
meta
);
if
(
temp
->
size
>=
size
){
return
ptr
;
}
void
*
new_ptr
=
m_malloc
(
size
);
memcpy
(
new_ptr
,
ptr
,
temp
->
size
);
m_free
(
ptr
);
return
new_ptr
;
}
This diff is collapsed.
Click to expand it.
alloc.h
+
9
−
1
View file @
36358c94
#ifndef _ALLOC_H_
#ifndef _ALLOC_H_
#define _ALLOC_H_
#define _ALLOC_H_
#include
<unistd.h>
typedef
struct
meta_struct
{
typedef
struct
meta_struct
{
struct
meta_struct
*
prev
;
struct
meta_struct
*
next
;
int
free
;
size_t
size
;
}
meta
;
}
meta
;
meta
*
meta1
;
meta
*
meta2
;
#endif
#endif
This diff is collapsed.
Click to expand it.
main
+
0
−
0
View file @
36358c94
No preview for this file type
This diff is collapsed.
Click to expand it.
main.c
+
53
−
1
View file @
36358c94
#include
"alloc.h"
#include
"alloc.h"
#include
<string.h>
#include
<stdio.h>
#include
<memory.h>
void
*
arr1
[
512
];
int
temp
;
int
main
()
int
main
()
{
{
int
num1
;
int
cr
;
scanf
(
"%d %c"
,
&
num1
,
&
cr
);
select_mode
(
cr
);
while
(
num1
!=
-
1
){
char
class
;
scanf
(
"%c"
,
&
class
);
if
(
class
==
's'
){
char
arr2
[
512
]
=
{};
int
num2
=
0
;
while
(
1
){
char
cr2
=
getchar
();
if
(
cr2
==
'\n'
){
break
;
}
arr2
[
num2
]
=
cr2
;
num2
++
;
}
arr1
[
temp
]
=
m_malloc
(
num2
);
memcpy
(
arr1
[
temp
],
arr2
,
strlen
(
arr2
));
temp
++
;
}
else
if
(
class
==
'f'
){
int
num2
;
int
num3
;
scanf
(
"%d %d"
,
&
num2
,
&
num3
);
arr1
[
num2
]
=
m_realloc
(
arr1
[
num2
],
num3
);
}
else
if
(
class
==
'r'
){
int
num3
;
scanf
(
"%d"
,
&
num3
);
arr1
[
temp
]
=
m_malloc
(
num3
);
temp
++
;
}
num1
--
;
}
meta
*
temp2
=
meta1
;
while
(
temp2
!=
0
){
printf
(
"%d %d"
,
temp2
->
free
,
temp2
->
size
);
if
(
temp2
->
free
==
0
){
printf
(
"%s
\n
"
,
(
char
*
)(
temp2
+
sizeof
(
meta
)));
}
temp2
=
temp2
->
next
;
}
return
0
;
return
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