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
Admin message
During summer vacation, Gitlab will be restart frequently. Use it carefully.
Show more breadcrumbs
kwanju
1801_OS_assignment4
Commits
38fc25af
Commit
38fc25af
authored
Jun 14, 2018
by
kwanju
Browse files
Options
Downloads
Patches
Plain Diff
input.txt 구동 성공, input2.txt 에러
parent
78eb4ef5
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
alloc.c
+61
-28
61 additions, 28 deletions
alloc.c
main.c
+88
-1
88 additions, 1 deletion
main.c
with
149 additions
and
29 deletions
alloc.c
+
61
−
28
View file @
38fc25af
...
...
@@ -3,9 +3,8 @@
#include
<stdlib.h>
#include
<stdbool.h>
#define MSIZE
sizeof(meta)-4
#define MSIZE
16
void
*
total
=
0
;
void
*
base
=
0
;
void
*
end
=
0
;
void
*
cur
=
0
;
...
...
@@ -56,32 +55,41 @@ meta* search(void* ptr, size_t size){//current pointer
void
*
m_malloc
(
size_t
size
){
if
(
base
==
0
){
end
=
cur
=
total
=
base
=
sbrk
(
0
);
//sbrk 주소반환
end
=
cur
=
base
=
sbrk
(
0
);
//sbrk 주소반환
}
if
((
size
%
4
)
!=
0
)
size
=
(
size
/
4
+
1
)
*
4
;
meta
*
target
=
search
(
total
,
size
);
//find chunk
if
(
target
==
0
){
meta
*
new
=
total
;
// printf("m1\n");
meta
*
target
=
search
(
end
,
size
);
//find chunk
//printf("m2\n");
if
(
target
==
0
||
(
!
target
->
next
&&
target
->
size
<
size
)
){
meta
*
new
=
end
;
meta
*
now
=
cur
;
//printf("m3\n");
sbrk
(
end
+
size
+
MSIZE
);
// printf("m4\n");
sbrk
(
total
+
size
+
MSIZE
);
now
->
next
=
new
;
new
->
prev
=
now
;
if
(
total
=
cur
)
//printf("m4.1\n");
if
(
end
==
cur
)
new
->
prev
=
0
;
else
{
// printf("m4.2\n");
now
->
next
=
new
;
new
->
prev
=
cur
;
}
new
->
next
=
0
;
new
->
free
=
0
;
// printf("m5\n");
new
->
size
=
size
;
// printf("m5.1\n");
// if(target != -1) target->next = new;
cur
=
new
;
total
=
total
+
size
+
MSIZE
;
//printf("m6\n");
end
=
end
+
size
+
MSIZE
;
return
new
->
data
;
}
else
{
//printf("m4\n");
m_realloc
(
target
->
data
,
size
);
}
return
target
->
data
;
...
...
@@ -91,51 +99,65 @@ void m_free(void* ptr){
meta
*
target
;
target
=
ptr
-
MSIZE
;
target
->
free
=
1
;
if
(
target
->
prev
!=
NULL
){
//printf("free error\n");
if
(
target
->
prev
!=
-
1
){
if
(
target
->
prev
->
free
==
1
){
//이전이 비어있으면 합침
target
->
prev
->
size
=
target
->
prev
->
size
+
target
->
size
+
MSIZE
;
target
->
prev
->
next
=
target
->
next
;
target
->
next
->
prev
=
target
->
prev
;
}
if
(
target
->
next
==
NULL
)
//prev는 있지만 next가 없을때
if
(
!
target
->
next
)
//prev는 있지만 next가 없을때
end
=
end
-
target
->
size
+
MSIZE
;
target
->
prev
->
next
=
NULL
;
}
if
(
target
->
next
!=
NULL
){
//next가 있을때
if
(
target
->
next
!=
-
1
){
//next가 있을때
if
(
target
->
next
->
free
==
1
){
//다음이 비어있으면 합침
target
->
next
->
size
=
target
->
next
->
size
+
target
->
size
+
MSIZE
;
target
->
next
->
prev
=
target
->
prev
;
target
->
prev
->
next
=
target
->
next
;
}
}
if
(
!
target
->
next
&&
!
target
->
prev
)
//둘다 없을때
end
=
brk
(
base
);
}
void
*
m_realloc
(
void
*
ptr
,
size_t
size
){
meta
*
old
=
ptr
-
MSIZE
;
//meta 제외 사이
// printf("re1\n");
if
(
size
%
4
!=
0
){
size
=
(
size
/
4
+
1
)
*
4
;
//4배수 맞추기
}
if
(
old
->
size
==
size
)
return
ptr
;
if
(
old
->
next
!=
NULL
){
// printf("re1-1, %d\n", old->size);
if
(
old
->
size
==
size
){
//printf("re1.2\n");
return
ptr
;
}
// printf("re1.3\n");
if
(
old
->
next
){
// printf("re2\n");
if
(
old
->
next
->
free
==
1
){
// printf("re3\n");
old
->
size
=
old
->
size
+
old
->
next
->
size
+
MSIZE
;
old
->
next
=
old
->
next
->
next
;
old
->
next
->
prev
=
old
;
// printf("re4\n");
}
}
else
if
(
old
->
size
<
size
){
// printf("re5\n");
old
->
free
=
1
;
meta
*
new
=
m_malloc
(
size
);
memcpy
(
new
,
ptr
,
size
);
strcpy
(
new
,
ptr
);
//printf("re6\n");
return
new
;
}
else
if
(
old
->
size
<
size
+
MSIZE
){
// printf("re7\n");
return
ptr
;
}
else
{
meta
*
new
=
old
+
size
+
MSIZE
;
// printf("re8\n");
meta
*
new
=
(
int
)
old
+
size
+
MSIZE
;
new
->
free
=
1
;
new
->
next
=
old
->
next
;
...
...
@@ -150,3 +172,14 @@ void* m_realloc(void* ptr, size_t size){
return
old
->
data
;
}
}
void
print_all
(){
meta
*
start
=
base
;
while
(
start
){
printf
(
"%d %d "
,
start
->
free
,
start
->
size
);
if
(
start
->
free
!=
1
)
printf
(
"%s"
,
start
->
data
);
printf
(
"
\n
"
);
start
=
start
->
next
;
}
}
This diff is collapsed.
Click to expand it.
main.c
+
88
−
1
View file @
38fc25af
...
...
@@ -2,10 +2,97 @@
#include
<stdio.h>
#include
<stdlib.h>
#include
<stdint.h>
#include
<string.h>
extern
int
fit
;
char
*
tok
;
int
main
()
int
main
(
int
argc
,
char
*
argv
[])
{
if
(
argc
!=
2
){
//인자 에러체크
printf
(
"input error argc: %d
\n
"
,
argc
);
exit
(
1
);
}
FILE
*
file
;
char
buffer
[
1024
];
char
*
tok
;
if
(
!
(
file
=
fopen
(
argv
[
1
],
"r"
))){
//file open & error check
printf
(
"file open error
\n
"
);
exit
(
1
);
}
fgets
(
buffer
,
sizeof
(
buffer
),
file
);
// printf("11111\n");
tok
=
strtok
(
buffer
,
" "
);
int
buf_num
=
atoi
(
buffer
);
//숫자 문자열->정수형, 개수
if
(
!
buf_num
){
printf
(
"buf error
\n
"
);
exit
(
1
);
}
tok
=
strtok
(
NULL
,
" "
);
//FIT 0 1 2(first best worst)
if
(
tok
[
0
]
==
'F'
){
fit
=
0
;
//first
}
else
if
(
tok
[
0
]
==
'B'
){
fit
=
1
;
//best
}
else
if
(
tok
[
0
]
==
'W'
){
fit
=
2
;
//worst
}
else
{
printf
(
"fit type error
\n
"
);
exit
(
1
);
}
// printf("2222\n");
//-----------------------------------------------------------//
char
**
words
=
(
char
**
)
malloc
(
sizeof
(
char
*
)
*
buf_num
);
int
j
=
0
;
int
length
;
int
index
;
int
num
;
char
*
tmp
;
for
(
int
i
=
0
;
i
<
buf_num
;
i
++
){
//buf_num만큼 반복작업
// printf("3333\n");
memset
(
buffer
,
" "
,
sizeof
(
buffer
));
//memset
fgets
(
buffer
,
sizeof
(
buffer
),
file
);
// printf("4444\n");
if
((
tmp
=
strchr
(
buffer
,
'\n'
))
!=
NULL
)
*
tmp
=
'\0'
;
//문자열 맨뒤의 \n 제거
if
(
buffer
[
0
]
==
's'
){
//string
// printf("s\n");
length
=
strlen
(
buffer
+
2
);
words
[
j
]
=
m_malloc
(
length
+
1
);
strcpy
(
words
[
j
++
],
buffer
+
2
);
}
else
if
(
buffer
[
0
]
==
'f'
){
//free
m_free
(
words
[
atoi
(
buffer
+
2
)
/*index*/
]);
}
else
if
(
buffer
[
0
]
==
'r'
){
//realloc
printf
(
"r
\n
"
);
tok
=
strtok
(
buffer
+
2
,
" "
);
//쪼개고
index
=
atoi
(
tok
);
//n번째
tok
=
strtok
(
NULL
,
" "
);
length
=
atoi
(
tok
);
//m byte만큼 할당
// printf("%d, %d\n", index, length);
m_realloc
(
buffer
[
index
],
length
);
}
else
if
(
buffer
[
0
]
==
'e'
){
//e = allocate mem sapce
buffer
[
j
++
]
=
m_malloc
(
atoi
(
buffer
+
2
)
/*index*/
);
}
else
{
//error check
printf
(
"error!
\n
"
);
exit
(
1
);
}
}
//---------------------------------------------------------------//
//printf("asdad\n");
print_all
();
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