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
Park SungHo
1801_OS_assignment4
Commits
558d5a7e
Commit
558d5a7e
authored
6 years ago
by
Park SungHo
Browse files
Options
Downloads
Patches
Plain Diff
2018_06_12 test 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
+25
-1
25 additions, 1 deletion
alloc.h
input.txt
+5
-0
5 additions, 0 deletions
input.txt
main
+0
-0
0 additions, 0 deletions
main
main.c
+70
-2
70 additions, 2 deletions
main.c
with
256 additions
and
3 deletions
alloc.c
+
156
−
0
View file @
558d5a7e
#include
"alloc.h"
//start
#include
<stdio.h>
#define _SYS/TYPES_H_
#include
<sys/types.h>
meta
*
search_position
(
size_t
size
);
extern
int
FIT
=
F
;
void
*
init
=
0
;
void
*
last
=
0
;
void
*
m_malloc
(
size_t
size
){
meta
*
search
;
if
(
init
==
0
){
init
=
sbrk
(
0
);
last
=
init
;
}
size_t
alloc_size
=
size
+
METASIZE
;
if
(
last
==
init
)
search
=
-
1
;
else
search
=
search_position
(
size
);
if
(
search
==
-
1
||
(
!
search
->
next
&&
(
!
search
->
free
||
search
->
free
&&
search
->
size
<
size
))){
meta
*
new
=
last
;
last
+=
alloc_size
;
if
(
sbrk
(
alloc_size
)
==
-
1
)
return
0
;
new
->
free
=
0
;
new
->
next
=
0
;
new
->
prev
=
search
;
new
->
size
=
size
;
if
(
search
!=
-
1
)
search
->
next
=
new
;
search
=
new
;
}
else
m_realloc
(
search
->
data
,
size
);
return
search
->
data
;
}
void
m_free
(
void
*
point
){
meta
*
curr
=
point
-
METASIZE
;
curr
->
free
=
1
;
if
(
curr
->
next
&&
curr
->
next
->
free
==
1
){
curr
->
size
+=
curr
->
next
->
size
+
METASIZE
;
curr
->
next
=
curr
->
next
->
next
;
}
if
(
curr
->
prev
!=
-
1
){
if
(
curr
->
prev
->
free
){
curr
=
curr
->
prev
;
curr
->
size
+=
curr
->
next
->
size
+
METASIZE
;
curr
->
next
=
curr
->
next
->
next
;
}
if
(
!
curr
->
next
){
last
-=
curr
->
size
+
METASIZE
;
curr
->
prev
->
next
=
0
;
}
}
else
if
(
!
curr
->
next
&&
!
curr
->
prev
)
last
=
brk
(
init
);
point
=
0
;
}
void
*
m_realloc
(
void
*
pointer
,
size_t
size
){
meta
*
curr
=
pointer
-
METASIZE
;
if
(
curr
->
size
==
size
)
return
pointer
;
else
if
(
curr
->
size
<
size
){
if
(
curr
->
next
&&
curr
->
next
->
free
&&
curr
->
size
+
curr
->
next
->
size
+
METASIZE
>=
size
){
curr
->
size
+=
curr
->
next
->
size
+
METASIZE
;
curr
->
next
=
curr
->
next
->
next
;
curr
->
next
->
prev
=
curr
;
if
((
curr
->
size
-
size
)
<
METASIZE
)
return
pointer
;
else
{
meta
*
new
=
(
int
)
curr
+
size
+
METASIZE
;
new
->
prev
=
curr
;
new
->
next
=
curr
->
next
;
new
->
size
=
curr
->
size
-
size
-
METASIZE
;
curr
->
next
=
new
;
curr
->
size
=
size
;
curr
->
free
=
0
;
m_free
(
new
->
data
);
return
curr
->
data
;
}
}
else
{
m_free
(
curr
->
data
);
void
*
tmp
=
m_malloc
(
size
);
strcpy
(
tmp
,
pointer
);
return
tmp
;
}
}
else
if
((
curr
->
size
-
size
)
<
METASIZE
)
return
pointer
;
else
{
meta
*
new
=
(
int
)
curr
+
size
+
METASIZE
;
new
->
prev
=
curr
;
new
->
next
=
curr
->
next
;
new
->
size
=
curr
->
size
-
size
-
METASIZE
;
curr
->
next
=
new
;
curr
->
size
=
size
;
curr
->
free
=
0
;
m_free
(
new
->
data
);
return
curr
->
data
;
}
}
meta
*
search_position
(
size_t
size
){
meta
*
fix
=
init
;
meta
*
position
=
-
1
;
switch
(
FIT
){
case
F
:
if
(
fix
==
NULL
)
break
;
while
(
1
){
if
(
fix
->
free
&&
fix
->
size
>=
size
){
position
=
fix
;
break
;
}
if
(
fix
->
next
==
NULL
){
position
=
fix
;
break
;
}
fix
=
fix
->
next
;
}
break
;
case
B
:
if
(
fix
==
NULL
)
break
;
while
(
1
){
if
(
fix
->
size
=
size
&&
fix
->
free
){
if
(
position
==
-
1
)
position
=
fix
;
else
if
(
position
->
size
>
fix
->
size
)
position
=
fix
;
}
if
(
position
==
-
1
&&
!
fix
->
next
)
position
=
fix
;
fix
=
fix
->
next
;
}
break
;
case
W
:
if
(
fix
==
NULL
)
break
;
while
(
1
){
if
(
fix
->
size
>=
size
&&
fix
->
free
){
if
(
position
==
-
1
)
position
=
fix
;
else
if
(
position
->
size
<
fix
->
size
)
position
=
fix
;
}
if
(
position
==
-
1
&&
!
fix
->
next
)
position
=
fix
;
fix
=
fix
->
next
;
}
break
;
}
return
position
;
}
void
print_alloc
(){
meta
*
curr
=
init
;
while
(
curr
){
printf
(
"free : %d / size : %d "
,
curr
->
free
,
curr
->
size
);
if
(
!
curr
->
free
)
printf
(
"%s
\n
"
,
curr
->
data
);
else
printf
(
"
\n
"
);
curr
=
curr
->
next
;
}
}
This diff is collapsed.
Click to expand it.
alloc.h
+
25
−
1
View file @
558d5a7e
#ifndef _ALLOC_H_
#define _ALLOC_H_
//start
#ifndef _STDINT_H_
#define _STDINT_H_
#include
<stdint.h>
#endif
typedef
struct
meta_struct
{
#ifndef _SYS/TYPES_H_
#define _SYS/TYPES_H_
#include
<sys/types.h>
#endif
#define F 1
#define B 2
#define W 4
#define METASIZE 16
typedef
struct
meta_struct
{
uint32_t
free
;
uint32_t
size
;
struct
meta_struct
*
prev
;
struct
meta_struct
*
next
;
char
data
[
1
];
}
meta
;
extern
int
type
;
void
*
m_malloc
(
size_t
size
);
void
m_free
(
void
*
point
);
void
*
m_realloc
(
void
*
point
,
size_t
size
);
#endif
This diff is collapsed.
Click to expand it.
input.txt
0 → 100644
+
5
−
0
View file @
558d5a7e
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 strengthened by use.
s Life is the art of drawing sufficient conclusions from insufficient premises.
This diff is collapsed.
Click to expand it.
main
0 → 100755
+
0
−
0
View file @
558d5a7e
File added
This diff is collapsed.
Click to expand it.
main.c
+
70
−
2
View file @
558d5a7e
#include
"alloc.h"
#include
<string.h>
#include
<stdio.h>
int
main
()
{
extern
int
FIT
;
int
main
(
int
argc
,
char
*
argv
[]){
if
(
argc
<
2
){
printf
(
"Please input 2 argument
\n
"
);
return
0
;
}
FILE
*
fp
=
fopen
(
argv
[
1
],
"r"
);
if
(
fp
==
NULL
)
return
0
;
char
buf
[
4096
];
char
*
token
;
fgets
(
buf
,
sizeof
(
buf
),
fp
);
token
=
strtok
(
buf
,
" "
);
int
index
=
atoi
(
token
);
if
(
index
==
0
)
return
0
;
token
=
strtok
(
NULL
,
" "
);
switch
(
token
[
0
]){
case
'F'
:
FIT
=
F
;
break
;
case
'B'
:
FIT
=
B
;
break
;
case
'W'
:
FIT
=
W
;
break
;
default:
printf
(
"FIT is in F,B,W
\n
"
);
return
0
;
}
char
**
command
=
(
char
**
)
malloc
(
sizeof
(
char
*
)
*
index
);
int
tmp
,
init
=
0
,
resize
;
for
(
int
i
=
0
;
i
<
index
;
i
++
){
memset
(
buf
,
'\0'
,
sizeof
(
buf
));
fgets
(
buf
,
sizeof
(
buf
),
fp
);
buf
[
strlen
(
buf
)
-
1
]
=
'\0'
;
switch
(
buf
[
0
]){
case
'f'
:
tmp
=
atoi
(
buf
+
2
);
m_free
(
command
[
tmp
]);
break
;
case
'e'
:
tmp
=
atoi
(
buf
+
2
);
command
[
init
++
]
=
m_malloc
(
tmp
);
break
;
case
'r'
:
token
=
strtok
(
buf
+
2
,
" "
);
tmp
=
atoi
(
token
);
token
=
strtok
(
NULL
,
" "
);
resize
=
atoi
(
token
);
m_realloc
(
command
[
tmp
],
resize
);
break
;
case
'm'
:
case
's'
:
command
[
init
]
=
m_malloc
(
strlen
(
buf
+
2
)
+
1
);
strcpy
(
command
[
init
++
],
buf
+
2
);
break
;
default:
printf
(
"command error. input f, e, r, m, s
\n
"
);
return
0
;
}
}
print_alloc
();
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