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
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Hywon woong Jang
1801_OS_assignment4
Commits
6d1309f5
Commit
6d1309f5
authored
6 years ago
by
Hywon woong Jang
Browse files
Options
Downloads
Patches
Plain Diff
Ver3.0 Final complete edition
parent
9d5aec22
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
+106
-30
106 additions, 30 deletions
alloc.c
alloc.h
+5
-0
5 additions, 0 deletions
alloc.h
main.c
+62
-1
62 additions, 1 deletion
main.c
with
173 additions
and
31 deletions
alloc.c
+
106
−
30
View file @
6d1309f5
...
...
@@ -5,8 +5,6 @@ unsigned int n_link;
unsigned
int
stacked_link
;
meta
*
meta_header
[
100
];
char
Algorithm
;
void
*
m_malloc
(
size_t
size
)
{
unsigned
int
new_size
;
...
...
@@ -33,11 +31,11 @@ void * m_malloc(size_t size)
}
else
{
printf
(
"dlrjakwsi?
\n
"
);
//
printf("dlrjakwsi?\n");
return
temp
+
1
;
}
// printf(" ") ;
return
sbrk
(
size
);
// return address to pointer
return
sbrk
(
new_
size
);
// return address to pointer
}
...
...
@@ -67,6 +65,7 @@ void * m_realloc(void *ptr , size_t size)
if
(
temp
->
size
==
new_size
)
return
ptr
;
//***********************************************/
/*
if(META.Tail == temp)
{
if(temp -> size > new_size)
...
...
@@ -76,7 +75,7 @@ void * m_realloc(void *ptr , size_t size)
return ptr;
}
}
*/
if
(
(
int
)
temp
->
size
-
(
int
)
new_size
>=
20
)
{
split_block
(
temp
,
new_size
);
// split and add meta
...
...
@@ -136,8 +135,9 @@ void * re_allocate(void * ptr, meta * temp, size_t new_size )
unsigned
int
save_size
;
save_size
=
temp
->
size
;
save_temp
=
ptr
;
m_free
(
ptr
);
//
m_free(ptr);
ptr
=
m_malloc
(
(
size_t
)
new_size
);
m_free
(
save_temp
);
memmove
(
ptr
,
(
void
*
)
save_temp
,
save_size
);
return
ptr
;
...
...
@@ -223,7 +223,7 @@ void * m_free( void *ptr)
}
else
if
(
temp
->
next
==
NULL
&&
temp
->
prev
==
NULL
)
{
printf
(
"durldhsi??
\n
"
);
//
printf("durldhsi??\n");
META
.
Head
=
NULL
;
META
.
Tail
=
NULL
;
brk
(
NULL
);
...
...
@@ -265,24 +265,102 @@ void set_link(void)
}
meta
*
search_and_split
(
size_t
size
)
{
int
max
,
min
;
max
=
0
;
min
=
0
;
meta
*
temp
=
NULL
;
temp
=
META
.
Head
;
int
temp_num
=
0
;
unsigned
int
diff
;
unsigned
int
save
;
int
i
=
0
;
while
(
temp
!=
NULL
)
{
if
(
temp
->
free
==
1
&&
temp
->
size
>=
size
)
{
// printf("nonono...\n");
diff
=
temp
->
size
-
size
;
if
(
Algorithm
==
'F'
)
{
if
(
diff
>=
20
)
return
allocate
(
diff
,
size
,
temp
);
else
{
temp
->
free
=
0
;
// temp -> size = size;
return
temp
;
}
}
else
if
(
Algorithm
==
'W'
)
{
if
(
temp
->
size
>
max
)
{
max
=
temp
->
size
;
temp_num
=
i
;
}
}
else
if
(
Algorithm
==
'B'
)
{
if
(
min
==
0
)
min
=
temp
->
size
;
if
(
min
>
temp
->
size
)
{
min
=
temp
->
size
;
printf
(
"min : %d
\n
"
,
min
);
temp_num
=
i
;
}
}
}
temp
=
temp
->
next
;
i
++
;
}
if
(
Algorithm
==
'W'
)
{
if
(
max
==
0
)
return
NULL
;
if
(
max
!=
0
)
{
temp
=
choose_meta
(
temp_num
);
diff
=
temp
->
size
-
size
;
return
allocate
(
diff
,
size
,
temp
);
}
}
else
if
(
Algorithm
==
'B'
)
{
// printf("B\n");
if
(
min
==
0
)
{
// printf("???\n");
return
NULL
;
}
else
if
(
min
!=
0
)
{
temp
=
choose_meta
(
temp_num
);
printf
(
"temp_num : %d
\n
"
,
temp_num
);
diff
=
temp
->
size
-
size
;
return
allocate
(
diff
,
size
,
temp
);
}
}
return
NULL
;
}
meta
*
allocate
(
int
diff
,
int
size
,
meta
*
temp
)
{
meta
*
save
=
NULL
;
save
=
temp
;
// printf("B allocate\n");
if
(
diff
>=
20
)
{
save
=
temp
;
meta_header
[
n_link
]
=
save
+
16
+
(
unsigned
int
)
size
;
meta_header
[
n_link
]
->
free
=
1
;
meta_header
[
n_link
]
->
next
=
temp
->
next
;
meta_header
[
n_link
]
->
next
->
prev
=
meta_header
[
n_link
];
temp
->
next
=
meta_header
[
n_link
];
...
...
@@ -293,26 +371,24 @@ meta * search_and_split(size_t size)
temp
->
size
=
size
;
n_link
++
;
return
temp
;
}
else
{
// printf("temp = %p\n",temp)
;
temp
->
free
=
0
;
return
temp
;
}
}
meta
*
choose_meta
(
int
num
)
{
meta
*
temp
=
NULL
;
temp
=
META
.
Head
;
for
(
int
i
=
0
;
i
<
num
;
i
++
)
{
temp
=
temp
->
next
;
}
return
NULL
;
return
temp
;
}
void
print_link
(
void
)
{
meta
*
temp
=
NULL
;
...
...
This diff is collapsed.
Click to expand it.
alloc.h
+
5
−
0
View file @
6d1309f5
...
...
@@ -14,11 +14,16 @@ typedef struct meta_manage {
meta
*
Head
;
meta
*
Tail
;
}
manage
;
char
Algorithm
;
manage
META
;
void
*
m_malloc
(
size_t
size
);
meta
*
choose_meta
(
int
num
);
meta
*
allocate
(
int
diff
,
int
size
,
meta
*
temp
);
void
*
m_realloc
(
void
*
ptr
,
size_t
size
);
void
*
m_free
(
void
*
ptr
);
...
...
This diff is collapsed.
Click to expand it.
main.c
+
62
−
1
View file @
6d1309f5
#include
"alloc.h"
int
main
()
{
int
num
;
char
*
str
[
100
];
char
move
[
4096
];
// enough size;
meta
*
temp
=
NULL
;
char
*
save
=
NULL
;
char
flag
;
int
size
;
int
sequence
;
int
n
=
0
;
int
i
=
0
;
scanf
(
"%d %c"
,
&
num
,
&
Algorithm
);
for
(
i
=
0
;
i
<
num
;
i
++
)
{
// printf("here?\n");
scanf
(
" %c"
,
&
flag
);
if
(
flag
==
'r'
)
{
scanf
(
" %d %d"
,
&
sequence
,
&
size
);
temp
=
choose_meta
(
sequence
);
temp
=
m_realloc
(
temp
+
1
,
size
);
}
else
if
(
flag
==
's'
)
{
// printf("asdf\n");
scanf
(
" %[^
\n
]"
,
move
);
// printf("size : %d\n", strlen(move));
str
[
n
]
=
m_malloc
(
strlen
(
move
)
+
1
);
strcpy
(
str
[
n
++
],
move
);
// printf("strlen : %d\n" ,strlen(str[n -1]));
}
else
if
(
flag
==
'f'
)
{
scanf
(
" %d"
,
&
sequence
);
// printf("sequence = %d \n",sequence);
temp
=
choose_meta
(
sequence
);
m_free
(
temp
+
1
);
}
else
if
(
flag
==
'e'
)
{
scanf
(
" %d"
,
&
size
);
m_malloc
(
size
);
}
}
temp
=
META
.
Head
;
i
=
0
;
printf
(
"
\n
"
);
while
(
temp
!=
NULL
)
{
save
=
temp
+
1
;
printf
(
"%d %d"
,
temp
->
free
,
temp
->
size
);
if
(
temp
->
free
==
0
)
printf
(
" %s
\n
"
,
save
);
else
printf
(
"
\n
"
);
temp
=
temp
->
next
;
}
// printf("\n");
// print_link();
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