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
271fa895
Commit
271fa895
authored
7 years ago
by
강병수
Browse files
Options
Downloads
Patches
Plain Diff
change input method and fix some bugs
parent
91a22009
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
alloc.c
+19
-19
19 additions, 19 deletions
alloc.c
alloc.h
+6
-4
6 additions, 4 deletions
alloc.h
main.c
+14
-6
14 additions, 6 deletions
main.c
with
39 additions
and
29 deletions
alloc.c
+
19
−
19
View file @
271fa895
...
@@ -3,7 +3,6 @@
...
@@ -3,7 +3,6 @@
#include
<string.h>
#include
<string.h>
#include
<limits.h>
#include
<limits.h>
#include
"alloc.h"
#include
"alloc.h"
#define align(n) (((n+3)/4)*4)
meta
*
base
;
meta
*
base
;
meta
*
last
;
meta
*
last
;
...
@@ -14,14 +13,14 @@ void *m_find(size_t size)
...
@@ -14,14 +13,14 @@ void *m_find(size_t size)
meta
*
cur
=
base
,
*
ans
=
0
;
meta
*
cur
=
base
,
*
ans
=
0
;
size_t
max
=
0
,
min
=
SIZE_T_MAX
;
size_t
max
=
0
,
min
=
SIZE_T_MAX
;
bool
find
=
false
;
int
find
=
FALSE
;
do
do
{
{
switch
(
fit_type
)
switch
(
fit_type
)
{
{
case
FIRST_FIT
:
case
FIRST_FIT
:
if
(
cur
->
free
&&
cur
->
size
>=
size
)
find
=
true
;
if
(
cur
->
free
&&
cur
->
size
>=
size
)
find
=
TRUE
;
break
;
break
;
case
BEST_FIT
:
case
BEST_FIT
:
...
@@ -48,31 +47,34 @@ void *m_find(size_t size)
...
@@ -48,31 +47,34 @@ void *m_find(size_t size)
return
!
ans
?
ans
:
ans
+
1
;
return
!
ans
?
ans
:
ans
+
1
;
}
}
void
*
m_merge
(
meta
*
cur1
,
meta
*
cur2
)
meta
*
m_merge
(
meta
*
cur1
,
meta
*
cur2
)
{
{
cur1
->
next
=
cur2
->
next
;
cur1
->
next
=
cur2
->
next
;
cur1
->
size
+=
(
cur2
->
size
+
META_SIZE
);
cur1
->
size
+=
(
cur2
->
size
+
META_SIZE
);
if
(
cur2
==
last
)
last
=
cur1
;
if
(
cur2
->
next
&&
cur2
->
next
->
free
)
if
(
cur2
->
next
&&
cur2
->
next
->
free
)
m_merge
(
cur1
,
cur2
->
next
);
m_merge
(
cur1
,
cur2
->
next
);
else
if
(
cur1
->
prev
&&
cur1
->
prev
->
free
)
else
if
(
cur1
->
prev
&&
cur1
->
prev
->
free
)
cur1
=
(
m_merge
(
cur1
->
prev
,
cur1
)
-
META_SIZE
)
;
cur1
=
m_merge
(
cur1
->
prev
,
cur1
);
return
cur1
+
1
;
return
cur1
;
}
}
void
*
m_split
(
meta
*
cur
,
size_t
size
)
meta
*
m_split
(
meta
*
cur
,
size_t
size
)
{
{
if
(
cur
->
size
<=
size
)
return
cur
+
1
;
size_t
size_sum
=
size
+
META_SIZE
;
size_t
size_sum
=
size
+
META_SIZE
;
if
(
cur
->
size
<=
size_sum
)
return
cur
;
meta
*
new_meta
=
(
void
*
)
cur
+
size_sum
;
meta
*
new_meta
=
(
void
*
)
cur
+
size_sum
;
new_meta
->
prev
=
cur
;
new_meta
->
prev
=
cur
;
new_meta
->
next
=
cur
->
next
;
new_meta
->
next
=
cur
->
next
;
new_meta
->
free
=
true
;
new_meta
->
free
=
TRUE
;
new_meta
->
size
=
cur
->
size
-
size_sum
;
new_meta
->
size
=
cur
->
size
-
size_sum
;
cur
->
size
=
size
;
cur
->
size
=
size
;
cur
->
next
=
new_meta
;
cur
->
next
=
new_meta
;
return
cur
+
1
;
return
cur
;
}
}
void
*
m_malloc
(
size_t
size
)
void
*
m_malloc
(
size_t
size
)
...
@@ -86,7 +88,7 @@ void *m_malloc(size_t size)
...
@@ -86,7 +88,7 @@ void *m_malloc(size_t size)
//if(sbrk(0)==-1) return;
//if(sbrk(0)==-1) return;
base
=
sbrk
(
0
);
base
=
sbrk
(
0
);
sbrk
((
int
)
size_sum
);
sbrk
((
int
)
size_sum
);
base
->
free
=
true
;
base
->
free
=
TRUE
;
base
->
size
=
size
;
base
->
size
=
size
;
last
=
base
;
last
=
base
;
}
}
...
@@ -101,7 +103,7 @@ void *m_malloc(size_t size)
...
@@ -101,7 +103,7 @@ void *m_malloc(size_t size)
meta
*
result_meta
=
last
;
meta
*
result_meta
=
last
;
last
=
sbrk
((
int
)
size_sum
);
last
=
sbrk
((
int
)
size_sum
);
last
->
prev
=
result_meta
;
last
->
prev
=
result_meta
;
last
->
free
=
true
;
last
->
free
=
TRUE
;
last
->
size
=
size
;
last
->
size
=
size
;
result_meta
->
next
=
last
;
result_meta
->
next
=
last
;
result
=
result_meta
+
1
;
result
=
result_meta
+
1
;
...
@@ -112,7 +114,7 @@ void *m_malloc(size_t size)
...
@@ -112,7 +114,7 @@ void *m_malloc(size_t size)
else
else
result
=
last
+
1
;
result
=
last
+
1
;
}
}
result
=
m_split
(
result
-
META_SIZE
,
size
);
result
=
m_split
(
result
-
META_SIZE
,
size
)
+
1
;
return
result
;
return
result
;
}
}
...
@@ -124,7 +126,6 @@ void *m_realloc(void *ptr, size_t size)
...
@@ -124,7 +126,6 @@ void *m_realloc(void *ptr, size_t size)
meta
*
cur_meta
=
cur
-
META_SIZE
;
meta
*
cur_meta
=
cur
-
META_SIZE
;
if
(
cur_meta
->
size
==
size
)
return
ptr
;
if
(
cur_meta
->
size
==
size
)
return
ptr
;
//TODO: case when extra size is sufficient
if
(
cur_meta
->
size
>
size
)
if
(
cur_meta
->
size
>
size
)
cur
=
m_split
(
cur_meta
,
size
);
cur
=
m_split
(
cur_meta
,
size
);
else
if
(
cur_meta
->
next
&&
cur_meta
->
next
->
free
&&
cur_meta
->
size
+
cur_meta
->
next
->
size
+
META_SIZE
>
size
)
else
if
(
cur_meta
->
next
&&
cur_meta
->
next
->
free
&&
cur_meta
->
size
+
cur_meta
->
next
->
size
+
META_SIZE
>
size
)
...
@@ -135,13 +136,13 @@ void *m_realloc(void *ptr, size_t size)
...
@@ -135,13 +136,13 @@ void *m_realloc(void *ptr, size_t size)
if
(
cur_meta
->
size
+
META_SIZE
>
size
)
if
(
cur_meta
->
size
+
META_SIZE
>
size
)
cur
=
m_split
(
cur_meta
,
size
);
cur
=
m_split
(
cur_meta
,
size
);
}
}
//nothing to merge or split
//
if
nothing to merge or split
else
else
{
{
cur
=
m_malloc
(
size
);
cur
=
m_malloc
(
size
);
cur_meta
=
cur
-
META_SIZE
;
cur_meta
=
cur
-
META_SIZE
;
memcpy
(
cur
,
ptr
,
size
);
memcpy
(
cur
,
ptr
,
size
);
cur_meta
->
free
=
false
;
cur_meta
->
free
=
FALSE
;
m_free
(
ptr
);
m_free
(
ptr
);
}
}
...
@@ -153,7 +154,7 @@ void m_free(void *ptr)
...
@@ -153,7 +154,7 @@ void m_free(void *ptr)
//if(!ptr) return 0;
//if(!ptr) return 0;
meta
*
cur
=
ptr
-
META_SIZE
;
meta
*
cur
=
ptr
-
META_SIZE
;
cur
->
free
=
true
;
cur
->
free
=
TRUE
;
if
(
cur
->
prev
&&
cur
->
prev
->
free
)
if
(
cur
->
prev
&&
cur
->
prev
->
free
)
cur
=
m_merge
(
cur
->
prev
,
cur
);
cur
=
m_merge
(
cur
->
prev
,
cur
);
...
@@ -170,7 +171,6 @@ void m_free(void *ptr)
...
@@ -170,7 +171,6 @@ void m_free(void *ptr)
//back to origin state if there are no blocks
//back to origin state if there are no blocks
else
if
(
!
cur
->
prev
&&
cur
==
last
)
else
if
(
!
cur
->
prev
&&
cur
==
last
)
base
=
last
=
NULL
;
base
=
last
=
NULL
;
//return 1;
}
}
void
*
m_travel
(
int
idx
)
void
*
m_travel
(
int
idx
)
...
...
This diff is collapsed.
Click to expand it.
alloc.h
+
6
−
4
View file @
271fa895
#include
<stdio.h>
#include
<stdio.h>
#include
<stdbool.h>
#ifndef _ALLOC_H_
#ifndef _ALLOC_H_
#define _ALLOC_H_
#define _ALLOC_H_
#define FIRST_FIT 'F'
#define FIRST_FIT 'F'
#define BEST_FIT 'B'
#define BEST_FIT 'B'
#define WORST_FIT 'W'
#define WORST_FIT 'W'
#define TRUE 1
#define FALSE 0
#define META_SIZE sizeof(struct meta_struct)
#define META_SIZE sizeof(struct meta_struct)
#define align(n) (((n+3)/4)*4)
typedef
struct
meta_struct
{
typedef
struct
meta_struct
{
struct
meta_struct
*
prev
;
struct
meta_struct
*
prev
;
struct
meta_struct
*
next
;
struct
meta_struct
*
next
;
bool
free
;
size_t
free
;
size_t
size
;
size_t
size
;
}
meta
;
}
meta
;
extern
int
fit_type
;
extern
int
fit_type
;
meta
*
m_merge
(
meta
*
cur1
,
meta
*
cur2
);
meta
*
m_split
(
meta
*
cur
,
size_t
size
);
void
*
m_find
(
size_t
size
);
void
*
m_find
(
size_t
size
);
void
*
m_merge
(
meta
*
cur1
,
meta
*
cur2
);
void
*
m_split
(
meta
*
cur
,
size_t
size
);
void
*
m_malloc
(
size_t
size
);
void
*
m_malloc
(
size_t
size
);
void
*
m_realloc
(
void
*
ptr
,
size_t
size
);
void
*
m_realloc
(
void
*
ptr
,
size_t
size
);
void
m_free
(
void
*
ptr
);
void
m_free
(
void
*
ptr
);
...
...
This diff is collapsed.
Click to expand it.
main.c
+
14
−
6
View file @
271fa895
...
@@ -2,7 +2,6 @@
...
@@ -2,7 +2,6 @@
#include
<string.h>
#include
<string.h>
#include
"alloc.h"
#include
"alloc.h"
#define BUF_MAX 1000
#define BUF_MAX 1000
#define FILE_PATH "test1.txt"
#define STRING 's'
#define STRING 's'
#define FREE 'f'
#define FREE 'f'
#define REALLOC 'r'
#define REALLOC 'r'
...
@@ -11,9 +10,15 @@ char buf[BUF_MAX+10];
...
@@ -11,9 +10,15 @@ char buf[BUF_MAX+10];
void
*
ptr
;
void
*
ptr
;
int
fit_type
;
int
fit_type
;
int
main
()
int
main
(
int
argc
,
char
*
argv
[]
)
{
{
FILE
*
fp
=
fopen
(
FILE_PATH
,
"rt"
);
if
(
argc
==
1
)
{
printf
(
"Input file name
\n
"
);
return
0
;
}
FILE
*
fp
=
fopen
(
argv
[
1
],
"r"
);
if
(
!
fp
)
if
(
!
fp
)
{
{
printf
(
"Cannot open file!
\n
"
);
printf
(
"Cannot open file!
\n
"
);
...
@@ -32,12 +37,15 @@ int main()
...
@@ -32,12 +37,15 @@ int main()
case
STRING
:
case
STRING
:
fgets
(
buf
,
BUF_MAX
,
fp
);
fgets
(
buf
,
BUF_MAX
,
fp
);
size_alloc
=
strlen
(
buf
);
size_alloc
=
strlen
(
buf
);
//organize format
if
(
buf
[
size_alloc
-
1
]
==
'\n'
)
buf
[
size_alloc
-
1
]
=
'\0'
;
buf
[
size_alloc
-
1
]
=
'\0'
;
ptr
=
m_malloc
(
size_alloc
);
ptr
=
m_malloc
(
size_alloc
);
strcpy
(
ptr
,
buf
);
strcpy
(
ptr
,
buf
);
meta
*
tmp
=
ptr
-
META_SIZE
;
meta
*
tmp
=
ptr
-
META_SIZE
;
tmp
->
free
=
false
;
tmp
->
free
=
0
;
//(meta *)(ptr-META_SIZE)->free=false;
break
;
break
;
case
FREE
:
case
FREE
:
...
...
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