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
56e46ac1
Commit
56e46ac1
authored
7 years ago
by
최민정
Browse files
Options
Downloads
Patches
Plain Diff
finish
parent
81530590
No related branches found
No related tags found
No related merge requests found
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
alloc.c
+156
-0
156 additions, 0 deletions
alloc.c
alloc.h
+15
-1
15 additions, 1 deletion
alloc.h
input.txt
+7
-0
7 additions, 0 deletions
input.txt
main
+0
-0
0 additions, 0 deletions
main
main.c
+44
-2
44 additions, 2 deletions
main.c
with
222 additions
and
3 deletions
alloc.c
+
156
−
0
View file @
56e46ac1
#include
"alloc.h"
#include
<stdio.h>
#include
<stdint.h>
#include
<string.h>
void
*
m_malloc
(
uint32_t
size
){
meta
*
temp
;
if
(
size
%
4
!=
0
)
size
+=
(
4
-
size
%
4
);
if
(
head
==
NULL
){
temp
=
sbrk
(
size
);
temp
->
next_memory
=
NULL
;
temp
->
prev_memory
=
NULL
;
temp
->
size
=
size
;
temp
->
free
=
0
;
strcpy
(
temp
->
comment
,
tmp_comment
);
temp
->
fit
=
fit
;
head
=
temp
;
first
=
temp
;
}
else
{
meta
*
prev
=
NULL
;
meta
*
next
=
NULL
;
meta
*
tmp_head
=
head
;
while
(
1
){
if
(
tmp_head
->
next_memory
==
NULL
){
if
(
prev
==
NULL
){
prev
=
tmp_head
;
}
break
;
}
else
if
(
tmp_head
->
next_memory
->
free
==
1
&&
tmp_head
->
next_memory
->
size
>
size
){
if
(
prev
==
NULL
){
prev
=
tmp_head
;
if
(
fit
==
"F"
)
break
;
}
else
{
if
(
fit
==
"B"
)
prev
=
(
prev
->
size
<
tmp_head
->
next_memory
->
size
)
?
prev
:
tmp_head
;
else
if
(
fit
==
"W"
)
prev
=
(
prev
->
size
>
tmp_head
->
next_memory
->
size
)
?
prev
:
tmp_head
;
}
}
tmp_head
=
tmp_head
->
next_memory
;
}
tmp_head
=
prev
->
next_memory
;
next
=
tmp_head
->
next_memory
;
if
(
tmp_head
->
size
==
size
){
prev
->
next_memory
->
free
=
0
;
}
else
{
temp
->
prev_memory
=
prev
;
temp
->
next_memory
=
tmp_head
;
temp
->
size
=
size
;
temp
->
free
=
0
;
strcpy
(
temp
->
comment
,
tmp_comment
);
temp
->
fit
=
fit
;
tmp_head
->
size
-=
size
;
tmp_head
->
prev_memory
=
temp
;
prev
->
next_memory
=
temp
;
}
}
}
void
m_realoc
(
void
*
p
,
uint32_t
size
){
if
(
size
%
4
!=
0
)
size
+=
(
4
-
size
%
4
);
int
index
=
*
(
int
*
)
p
;
int
count
=
0
;
meta
*
temp
=
head
;
while
(
1
){
if
(
count
==
index
)
break
;
temp
=
temp
->
next_memory
;
if
(
temp
->
free
==
0
)
count
++
;
}
if
(
temp
->
next_memory
==
NULL
){
temp
->
size
=
size
;
}
else
if
(
size
<
(
temp
->
next_memory
->
size
)
+
temp
->
size
){
temp
->
next_memory
->
size
-=
(
size
-
temp
->
size
);
temp
->
size
=
size
;
}
else
if
(
size
=
(
temp
->
next_memory
->
size
)
+
temp
->
size
){
temp
->
size
=
size
;
temp
->
next_memory
=
temp
->
next_memory
->
next_memory
;
}
else
m_malloc
(
size
);
}
void
m_free
(
void
*
ptr
){
int
index
=
*
(
int
*
)
ptr
;
int
count
=
0
;
meta
*
temp
=
head
;
int
size
=
head
->
size
;
int
break_point
=
0
;
while
(
1
){
if
(
count
==
index
)
break
;
temp
=
temp
->
next_memory
;
if
(
temp
->
free
==
0
)
count
++
;
}
while
(
1
){
if
(
temp
->
next_memory
==
NULL
){
if
(
temp
->
next_memory
->
free
==
1
){
size
+=
temp
->
next_memory
->
size
;
temp
->
next_memory
=
temp
->
next_memory
->
next_memory
;
}
else
break
;
}
else
break
;
}
while
(
1
){
if
(
temp
->
prev_memory
==
NULL
){
if
(
temp
->
prev_memory
->
free
==
1
){
size
+=
temp
->
prev_memory
->
size
;
temp
->
prev_memory
=
temp
->
prev_memory
->
prev_memory
;
}
else
break
;
}
else
break
;
}
temp
->
free
=
1
;
strcpy
(
temp
->
comment
,
" "
);
if
(
index
==
0
)
head
==
temp
->
next_memory
;
}
void
read
(){
meta
*
temp
=
first
;
while
(
1
){
if
(
temp
->
next_memory
==
NULL
)
break
;
printf
(
"%d %d %s
\n
"
,
temp
->
free
,
temp
->
size
,
temp
->
comment
);
}
}
This diff is collapsed.
Click to expand it.
alloc.h
+
15
−
1
View file @
56e46ac1
#ifndef _ALLOC_H_
#define _ALLOC_H_
typedef
struct
meta_struct
{
#include
<stdint.h>
typedef
struct
meta_struct
{
struct
meta_struct
*
prev_memory
;
struct
meta_struct
*
next_memory
;
uint32_t
free
;
uint32_t
size
;
char
*
comment
;
char
fit
;
}
meta
;
char
fit
;
char
*
tmp_comment
;
meta
*
head
;
meta
*
first
;
#endif
This diff is collapsed.
Click to expand it.
input.txt
0 → 100644
+
7
−
0
View file @
56e46ac1
3 f
S THink like a man of action and act like man of thought.
s Courage is very important. LIke a muscle, it is strngthened by use.
s Life is the art if drqwing sufficient conclusions from insufficientpremises.
2 F
f 0
r 1
This diff is collapsed.
Click to expand it.
main
0 → 100755
+
0
−
0
View file @
56e46ac1
File added
This diff is collapsed.
Click to expand it.
main.c
+
44
−
2
View file @
56e46ac1
#include
"alloc.h"
int
main
()
{
#include
<string.h>
#include
<stdio.h>
#include
<stdlib.h>
int
main
(
int
argc
,
char
*
argv
[]){
uint32_t
size_index
,
size_data
;
char
type_fit
,
type_data
;
char
temp
[
256
];
int
num
,
index
;
FILE
*
fp
=
fopen
(
"input.txt"
,
"rb"
);
if
(
fp
==
NULL
){
perror
(
"Can't open the file"
);
exit
(
1
);
}
else
{
while
(
1
){
if
(
EOF
==
fscanf
(
fp
,
"%d %c"
,
size_index
,
type_fit
))
break
;
for
(
int
i
=
0
;
i
<
size_index
;
i
++
){
fscanf
(
fp
,
"%c"
,
type_data
);
if
(
type_data
==
"f"
){
fscanf
(
fp
,
"%d"
,
index
);
m_free
(
index
);
}
else
if
(
type_data
==
"r"
){
fscanf
(
fp
,
"%d %d"
,
index
,
size_data
);
m_realoc
(
index
,
size_data
);
}
else
if
(
type_data
==
"s"
){
fscanf
(
fp
,
"%s"
,
temp
);
if
(
temp
!=
NULL
){
strcpy
(
tmp_comment
,
temp
);
size_data
=
strlen
(
tmp_comment
);
}
m_malloc
(
size_data
);
}
}
read
();
}
}
fclose
(
fp
);
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