1
0
Fork 0
mirror of https://github.com/ossrs/srs.git synced 2025-03-09 15:49:59 +00:00

research st: refine skt.

This commit is contained in:
winlin 2014-11-06 13:55:06 +08:00
parent 22710db911
commit 3377df0bea

View file

@ -46,7 +46,6 @@
#include <sys/mman.h> #include <sys/mman.h>
#include "common.h" #include "common.h"
/* How much space to leave between the stacks, at each end */ /* How much space to leave between the stacks, at each end */
#define REDZONE _ST_PAGE_SIZE #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_stack_t *_st_stack_new(int stack_size)
{ {
_st_clist_t *qp; _st_clist_t *qp;
_st_stack_t *ts; _st_stack_t *ts;
int extra; int extra;
for (qp = _st_free_stacks.next; qp != &_st_free_stacks; qp = qp->next) { for (qp = _st_free_stacks.next; qp != &_st_free_stacks; qp = qp->next) {
ts = _ST_THREAD_STACK_PTR(qp); ts = _ST_THREAD_STACK_PTR(qp);
if (ts->stk_size >= stack_size) { if (ts->stk_size >= stack_size) {
/* Found a stack that is big enough */ /* Found a stack that is big enough */
ST_REMOVE_LINK(&ts->links); ST_REMOVE_LINK(&ts->links);
_st_num_free_stacks--; _st_num_free_stacks--;
ts->links.next = NULL; ts->links.next = NULL;
ts->links.prev = NULL; ts->links.prev = NULL;
return ts; return ts;
}
} }
}
/* Make a new thread stack object. */ /* Make a new thread stack object. */
if ((ts = (_st_stack_t *)calloc(1, sizeof(_st_stack_t))) == NULL) if ((ts = (_st_stack_t *)calloc(1, sizeof(_st_stack_t))) == NULL) {
return NULL; return NULL;
extra = _st_randomize_stacks ? _ST_PAGE_SIZE : 0; }
ts->vaddr_size = stack_size + 2*REDZONE + extra; extra = _st_randomize_stacks ? _ST_PAGE_SIZE : 0;
ts->vaddr = _st_new_stk_segment(ts->vaddr_size); ts->vaddr_size = stack_size + 2*REDZONE + extra;
if (!ts->vaddr) { ts->vaddr = _st_new_stk_segment(ts->vaddr_size);
free(ts); if (!ts->vaddr) {
return NULL; free(ts);
} return NULL;
ts->stk_size = stack_size; }
ts->stk_bottom = ts->vaddr + REDZONE; ts->stk_size = stack_size;
ts->stk_top = ts->stk_bottom + stack_size; ts->stk_bottom = ts->vaddr + REDZONE;
ts->stk_top = ts->stk_bottom + stack_size;
#ifdef DEBUG #ifdef DEBUG
mprotect(ts->vaddr, REDZONE, PROT_NONE); mprotect(ts->vaddr, REDZONE, PROT_NONE);
mprotect(ts->stk_top + extra, REDZONE, PROT_NONE); mprotect(ts->stk_top + extra, REDZONE, PROT_NONE);
#endif #endif
if (extra) { if (extra) {
long offset = (random() % extra) & ~0xf; long offset = (random() % extra) & ~0xf;
ts->stk_bottom += offset; ts->stk_bottom += offset;
ts->stk_top += offset; ts->stk_top += offset;
} }
return ts; return ts;
} }
/* /*
* Free the stack for the current thread * Free the stack for the current thread
*/ */
void _st_stack_free(_st_stack_t *ts) void _st_stack_free(_st_stack_t *ts)
{ {
if (!ts) if (!ts) {
return; return;
}
/* Put the stack on the free list */ /* Put the stack on the free list */
ST_APPEND_LINK(&ts->links, _st_free_stacks.prev); ST_APPEND_LINK(&ts->links, _st_free_stacks.prev);
_st_num_free_stacks++; _st_num_free_stacks++;
} }
static char *_st_new_stk_segment(int size) static char *_st_new_stk_segment(int size)
{ {
#ifdef MALLOC_STACK #ifdef MALLOC_STACK
void *vaddr = malloc(size); void *vaddr = malloc(size);
#else #else
static int zero_fd = -1; static int zero_fd = -1;
int mmap_flags = MAP_PRIVATE; int mmap_flags = MAP_PRIVATE;
void *vaddr; 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;
}
#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 #endif
vaddr = mmap(NULL, size, PROT_READ | PROT_WRITE, mmap_flags, zero_fd, 0); return (char *)vaddr;
if (vaddr == (void *)MAP_FAILED)
return NULL;
#endif /* MALLOC_STACK */
return (char *)vaddr;
} }
/* Not used */ /* Not used */
#if 0 #if 0
void _st_delete_stk_segment(char *vaddr, int size) void _st_delete_stk_segment(char *vaddr, int size)
{ {
#ifdef MALLOC_STACK #ifdef MALLOC_STACK
free(vaddr); free(vaddr);
#else #else
(void) munmap(vaddr, size); (void) munmap(vaddr, size);
#endif #endif
} }
#endif #endif
int st_randomize_stacks(int on) int st_randomize_stacks(int on)
{ {
int wason = _st_randomize_stacks; int wason = _st_randomize_stacks;
_st_randomize_stacks = on; _st_randomize_stacks = on;
if (on) if (on) {
srandom((unsigned int) st_utime()); srandom((unsigned int) st_utime());
}
return wason; return wason;
} }