Repository: ossrs/srs
File: trunk/3rdparty/st-srs/stk.c (C)

Code before:
            ts->links.next = NULL;
            ts->links.prev = NULL;
            returnt ts;
        }
    }
#endif

    extra = _st_randomize_stacks ? _ST_PAGE_SIZE : 0;
#ifndef MD_CACHE_STACK
    for (qp = _st_free_stacks.next; qp != &_st_free_stacks;) {
        ts = _ST_THREAD_STACK_PTR(qp);
        // Before qp is freed, move to next one, because the qp will be freed when free the ts.
        qp = qp->next;

        ST_REMOVE_LINK(&ts->links);
        _st_num_free_stacks--;

#if defined(DEBUG) && !defined(MD_NO_PROTECT)
        mprotect(ts->vaddr, REDZONE, PROT_READ | PROT_WRITE);
        mprotect(ts->stk_top + extra, REDZONE, PROT_READ | PROT_WRITE);
#endif

        _st_delete_stk_segment(ts->vaddr, ts->vaddr_size);
        free(ts);
    }
#endif
    
    /* Make a new thread stack object. */
    if ((ts = (_st_stack_t *)calloc(1, sizeof(_st_stack_t))) == NULL)
        return NULL;
    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;

    /* For example, in OpenWRT, the memory at the begin minus 16B by mprotect is read-only. */
#if defined(DEBUG) && !defined(MD_NO_PROTECT)
    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;

Review comment (performance):
only rarely a few part of codes in `st-srs`, add comments to denote which `#if` to match.
```suggestion
#endif /* MD_CACHE_STACK */
```
I would suggest add comments in this way.


Code after:
            ts->links.next = NULL;
            ts->links.prev = NULL;
            return ts;
        }
    }
#endif

    extra = _st_randomize_stacks ? _ST_PAGE_SIZE : 0;
#ifndef MD_CACHE_STACK
    for (qp = _st_free_stacks.next; qp != &_st_free_stacks;) {
        ts = _ST_THREAD_STACK_PTR(qp);
        // Before qp is freed, move to next one, because the qp will be freed when free the ts.
        qp = qp->next;

        ST_REMOVE_LINK(&ts->links);
        _st_num_free_stacks--;

#if defined(DEBUG) && !defined(MD_NO_PROTECT)
        mprotect(ts->vaddr, REDZONE, PROT_READ | PROT_WRITE);
        mprotect(ts->stk_top + extra, REDZONE, PROT_READ | PROT_WRITE);
#endif

        _st_delete_stk_segment(ts->vaddr, ts->vaddr_size);
        free(ts);
    }
#endif
    
    /* Make a new thread stack object. */
    if ((ts = (_st_stack_t *)calloc(1, sizeof(_st_stack_t))) == NULL)
        return NULL;
    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;

    /* For example, in OpenWRT, the memory at the begin minus 16B by mprotect is read-only. */
#if defined(DEBUG) && !defined(MD_NO_PROTECT)
    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;