Toggle navigation
Toggle navigation
此项目
正在载入...
Sign in
胡斌
/
srs
转到一个项目
Toggle navigation
项目
群组
代码片段
帮助
Toggle navigation pinning
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Network
Create a new issue
Builds
Commits
Authored by
winlin
2014-11-06 13:55:06 +0800
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
3377df0bea2a96e3d2ffd649c9dd7a3cbd2ff434
3377df0b
1 parent
22710db9
research st: refine skt.
隐藏空白字符变更
内嵌
并排对比
正在显示
1 个修改的文件
包含
83 行增加
和
82 行删除
trunk/research/st/stk.c
trunk/research/st/stk.c
查看文件 @
3377df0
...
...
@@ -46,7 +46,6 @@
#include <sys/mman.h>
#include "common.h"
/* How much space to leave between the stacks, at each end */
#define REDZONE _ST_PAGE_SIZE
...
...
@@ -58,116 +57,118 @@ static char *_st_new_stk_segment(int size);
_st_stack_t
*
_st_stack_new
(
int
stack_size
)
{
_st_clist_t
*
qp
;
_st_stack_t
*
ts
;
int
extra
;
for
(
qp
=
_st_free_stacks
.
next
;
qp
!=
&
_st_free_stacks
;
qp
=
qp
->
next
)
{
ts
=
_ST_THREAD_STACK_PTR
(
qp
);
if
(
ts
->
stk_size
>=
stack_size
)
{
/* Found a stack that is big enough */
ST_REMOVE_LINK
(
&
ts
->
links
);
_st_num_free_stacks
--
;
ts
->
links
.
next
=
NULL
;
ts
->
links
.
prev
=
NULL
;
return
ts
;
_st_clist_t
*
qp
;
_st_stack_t
*
ts
;
int
extra
;
for
(
qp
=
_st_free_stacks
.
next
;
qp
!=
&
_st_free_stacks
;
qp
=
qp
->
next
)
{
ts
=
_ST_THREAD_STACK_PTR
(
qp
);
if
(
ts
->
stk_size
>=
stack_size
)
{
/* Found a stack that is big enough */
ST_REMOVE_LINK
(
&
ts
->
links
);
_st_num_free_stacks
--
;
ts
->
links
.
next
=
NULL
;
ts
->
links
.
prev
=
NULL
;
return
ts
;
}
}
}
/* Make a new thread stack object. */
if
((
ts
=
(
_st_stack_t
*
)
calloc
(
1
,
sizeof
(
_st_stack_t
)))
==
NULL
)
return
NULL
;
extra
=
_st_randomize_stacks
?
_ST_PAGE_SIZE
:
0
;
ts
->
vaddr_size
=
stack_size
+
2
*
REDZONE
+
extra
;
ts
->
vaddr
=
_st_new_stk_segment
(
ts
->
vaddr_size
);
if
(
!
ts
->
vaddr
)
{
free
(
ts
);
return
NULL
;
}
ts
->
stk_size
=
stack_size
;
ts
->
stk_bottom
=
ts
->
vaddr
+
REDZONE
;
ts
->
stk_top
=
ts
->
stk_bottom
+
stack_size
;
/* Make a new thread stack object. */
if
((
ts
=
(
_st_stack_t
*
)
calloc
(
1
,
sizeof
(
_st_stack_t
)))
==
NULL
)
{
return
NULL
;
}
extra
=
_st_randomize_stacks
?
_ST_PAGE_SIZE
:
0
;
ts
->
vaddr_size
=
stack_size
+
2
*
REDZONE
+
extra
;
ts
->
vaddr
=
_st_new_stk_segment
(
ts
->
vaddr_size
);
if
(
!
ts
->
vaddr
)
{
free
(
ts
);
return
NULL
;
}
ts
->
stk_size
=
stack_size
;
ts
->
stk_bottom
=
ts
->
vaddr
+
REDZONE
;
ts
->
stk_top
=
ts
->
stk_bottom
+
stack_size
;
#ifdef DEBUG
mprotect
(
ts
->
vaddr
,
REDZONE
,
PROT_NONE
);
mprotect
(
ts
->
stk_top
+
extra
,
REDZONE
,
PROT_NONE
);
mprotect
(
ts
->
vaddr
,
REDZONE
,
PROT_NONE
);
mprotect
(
ts
->
stk_top
+
extra
,
REDZONE
,
PROT_NONE
);
#endif
if
(
extra
)
{
long
offset
=
(
random
()
%
extra
)
&
~
0xf
;
ts
->
stk_bottom
+=
offset
;
ts
->
stk_top
+=
offset
;
}
return
ts
;
if
(
extra
)
{
long
offset
=
(
random
()
%
extra
)
&
~
0xf
;
ts
->
stk_bottom
+=
offset
;
ts
->
stk_top
+=
offset
;
}
return
ts
;
}
/*
* Free the stack for the current thread
*/
void
_st_stack_free
(
_st_stack_t
*
ts
)
{
if
(
!
ts
)
return
;
/* Put the stack on the free list */
ST_APPEND_LINK
(
&
ts
->
links
,
_st_free_stacks
.
prev
);
_st_num_free_stacks
++
;
if
(
!
ts
)
{
return
;
}
/* Put the stack on the free list */
ST_APPEND_LINK
(
&
ts
->
links
,
_st_free_stacks
.
prev
);
_st_num_free_stacks
++
;
}
static
char
*
_st_new_stk_segment
(
int
size
)
{
#ifdef MALLOC_STACK
void
*
vaddr
=
malloc
(
size
);
#else
static
int
zero_fd
=
-
1
;
int
mmap_flags
=
MAP_PRIVATE
;
void
*
vaddr
;
#if defined (MD_USE_SYSV_ANON_MMAP)
if
(
zero_fd
<
0
)
{
if
((
zero_fd
=
open
(
"/dev/zero"
,
O_RDWR
,
0
))
<
0
)
return
NULL
;
fcntl
(
zero_fd
,
F_SETFD
,
FD_CLOEXEC
);
}
#elif defined (MD_USE_BSD_ANON_MMAP)
mmap_flags
|=
MAP_ANON
;
void
*
vaddr
=
malloc
(
size
);
#else
#error Unknown OS
static
int
zero_fd
=
-
1
;
int
mmap_flags
=
MAP_PRIVATE
;
void
*
vaddr
;
#if defined (MD_USE_SYSV_ANON_MMAP)
if
(
zero_fd
<
0
)
{
if
((
zero_fd
=
open
(
"/dev/zero"
,
O_RDWR
,
0
))
<
0
)
{
return
NULL
;
}
fcntl
(
zero_fd
,
F_SETFD
,
FD_CLOEXEC
);
}
#elif defined (MD_USE_BSD_ANON_MMAP)
mmap_flags
|=
MAP_ANON
;
#else
#error Unknown OS
#endif
vaddr
=
mmap
(
NULL
,
size
,
PROT_READ
|
PROT_WRITE
,
mmap_flags
,
zero_fd
,
0
);
if
(
vaddr
==
(
void
*
)
MAP_FAILED
)
{
return
NULL
;
}
#endif
vaddr
=
mmap
(
NULL
,
size
,
PROT_READ
|
PROT_WRITE
,
mmap_flags
,
zero_fd
,
0
);
if
(
vaddr
==
(
void
*
)
MAP_FAILED
)
return
NULL
;
#endif
/* MALLOC_STACK */
return
(
char
*
)
vaddr
;
return
(
char
*
)
vaddr
;
}
/* Not used */
#if 0
void _st_delete_stk_segment(char *vaddr, int size)
{
#ifdef MALLOC_STACK
free(vaddr);
free(vaddr);
#else
(void) munmap(vaddr, size);
(void) munmap(vaddr, size);
#endif
}
#endif
int
st_randomize_stacks
(
int
on
)
{
int
wason
=
_st_randomize_stacks
;
_st_randomize_stacks
=
on
;
if
(
on
)
srandom
((
unsigned
int
)
st_utime
());
return
wason
;
int
wason
=
_st_randomize_stacks
;
_st_randomize_stacks
=
on
;
if
(
on
)
{
srandom
((
unsigned
int
)
st_utime
());
}
return
wason
;
}
...
...
请
注册
或
登录
后发表评论