Skip to content

Commit b019860

Browse files
committed
Support free the stack, #38.
1 parent 843e74b commit b019860

File tree

11 files changed

+41
-14
lines changed

11 files changed

+41
-14
lines changed

Makefile

+3
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,9 @@ endif
191191
#
192192
# make EXTRA_CFLAGS=-DDEBUG_STATS
193193
#
194+
# or cache the stack and reuse it:
195+
# make EXTRA_CFLAGS=-DMD_CACHE_STACK
196+
#
194197
# or enable the coverage for utest:
195198
# make UTEST_FLAGS="-fprofile-arcs -ftest-coverage"
196199
#

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ The branch [srs](https://github.com/ossrs/state-threads/tree/srs) was patched an
119119
- [x] Check capability for backtrack.
120120
- [x] Support set specifics for any thread.
121121
- [x] Support st_destroy to free resources for asan.
122+
- [x] Support free the stack, [#38](https://github.com/ossrs/state-threads/issues/38).
122123
- [ ] System: Support sendmmsg for UDP, [#12](https://github.com/ossrs/state-threads/issues/12).
123124

124125
## GDB Tools

stk.c

+29-6
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,16 @@ __thread int _st_num_free_stacks = 0;
5757
__thread int _st_randomize_stacks = 0;
5858

5959
static char *_st_new_stk_segment(int size);
60+
static void _st_delete_stk_segment(char *vaddr, int size);
6061

6162
_st_stack_t *_st_stack_new(int stack_size)
6263
{
6364
_st_clist_t *qp;
6465
_st_stack_t *ts;
6566
int extra;
66-
67+
68+
/* If cache stack, we try to use stack from the cache list. */
69+
#ifdef MD_CACHE_STACK
6770
for (qp = _st_free_stacks.next; qp != &_st_free_stacks; qp = qp->next) {
6871
ts = _ST_THREAD_STACK_PTR(qp);
6972
if (ts->stk_size >= stack_size) {
@@ -75,11 +78,34 @@ _st_stack_t *_st_stack_new(int stack_size)
7578
return ts;
7679
}
7780
}
81+
#endif
82+
83+
extra = _st_randomize_stacks ? _ST_PAGE_SIZE : 0;
84+
/* If not cache stack, we will free all stack in the list, which contains the stack to be freed.
85+
* Note that we should never directly free it at _st_stack_free, because it is still be used,
86+
* and will cause crash. */
87+
#ifndef MD_CACHE_STACK
88+
for (qp = _st_free_stacks.next; qp != &_st_free_stacks;) {
89+
ts = _ST_THREAD_STACK_PTR(qp);
90+
/* Before qp is freed, move to next one, because the qp will be freed when free the ts. */
91+
qp = qp->next;
92+
93+
ST_REMOVE_LINK(&ts->links);
94+
_st_num_free_stacks--;
95+
96+
#if defined(DEBUG) && !defined(MD_NO_PROTECT)
97+
mprotect(ts->vaddr, REDZONE, PROT_READ | PROT_WRITE);
98+
mprotect(ts->stk_top + extra, REDZONE, PROT_READ | PROT_WRITE);
99+
#endif
100+
101+
_st_delete_stk_segment(ts->vaddr, ts->vaddr_size);
102+
free(ts);
103+
}
104+
#endif
78105

79106
/* Make a new thread stack object. */
80107
if ((ts = (_st_stack_t *)calloc(1, sizeof(_st_stack_t))) == NULL)
81108
return NULL;
82-
extra = _st_randomize_stacks ? _ST_PAGE_SIZE : 0;
83109
ts->vaddr_size = stack_size + 2*REDZONE + extra;
84110
ts->vaddr = _st_new_stk_segment(ts->vaddr_size);
85111
if (!ts->vaddr) {
@@ -114,7 +140,7 @@ void _st_stack_free(_st_stack_t *ts)
114140
{
115141
if (!ts)
116142
return;
117-
143+
118144
/* Put the stack on the free list */
119145
ST_APPEND_LINK(&ts->links, _st_free_stacks.prev);
120146
_st_num_free_stacks++;
@@ -152,8 +178,6 @@ static char *_st_new_stk_segment(int size)
152178
}
153179

154180

155-
/* Not used */
156-
#if 0
157181
void _st_delete_stk_segment(char *vaddr, int size)
158182
{
159183
#ifdef MALLOC_STACK
@@ -162,7 +186,6 @@ void _st_delete_stk_segment(char *vaddr, int size)
162186
(void) munmap(vaddr, size);
163187
#endif
164188
}
165-
#endif
166189

167190
int st_randomize_stacks(int on)
168191
{

tools/backtrace/backtrace.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* SPDX-License-Identifier: MIT */
2-
/* Copyright (c) 2013-2022 Winlin */
2+
/* Copyright (c) 2013-2024 The SRS Authors */
33

44
#ifdef __linux__
55
#define _GNU_SOURCE

tools/helloworld/helloworld.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* SPDX-License-Identifier: MIT */
2-
/* Copyright (c) 2013-2022 Winlin */
2+
/* Copyright (c) 2013-2024 The SRS Authors */
33

44
#include <stdio.h>
55

tools/porting/porting.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* SPDX-License-Identifier: MIT */
2-
/* Copyright (c) 2013-2022 Winlin */
2+
/* Copyright (c) 2013-2024 The SRS Authors */
33

44
#include <stdio.h>
55
#include <setjmp.h>

tools/verify/verify.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* SPDX-License-Identifier: MIT */
2-
/* Copyright (c) 2013-2022 Winlin */
2+
/* Copyright (c) 2013-2024 The SRS Authors */
33

44
#include <stdio.h>
55

utest/st_utest.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* SPDX-License-Identifier: MIT */
2-
/* Copyright (c) 2013-2022 Winlin */
2+
/* Copyright (c) 2013-2024 The SRS Authors */
33

44
#include <st_utest.hpp>
55

utest/st_utest.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* SPDX-License-Identifier: MIT */
2-
/* Copyright (c) 2013-2022 Winlin */
2+
/* Copyright (c) 2013-2024 The SRS Authors */
33

44
#ifndef ST_UTEST_PUBLIC_HPP
55
#define ST_UTEST_PUBLIC_HPP

utest/st_utest_coroutines.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* SPDX-License-Identifier: MIT */
2-
/* Copyright (c) 2013-2022 Winlin */
2+
/* Copyright (c) 2013-2024 The SRS Authors */
33

44
#include <st_utest.hpp>
55

utest/st_utest_tcp.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* SPDX-License-Identifier: MIT */
2-
/* Copyright (c) 2013-2022 Winlin */
2+
/* Copyright (c) 2013-2024 The SRS Authors */
33

44
#include <st_utest.hpp>
55

0 commit comments

Comments
 (0)