winlin

research st: refine skt.

@@ -46,7 +46,6 @@ @@ -46,7 +46,6 @@
46 #include <sys/mman.h> 46 #include <sys/mman.h>
47 #include "common.h" 47 #include "common.h"
48 48
49 -  
50 /* How much space to leave between the stacks, at each end */ 49 /* How much space to leave between the stacks, at each end */
51 #define REDZONE _ST_PAGE_SIZE 50 #define REDZONE _ST_PAGE_SIZE
52 51
@@ -75,8 +74,9 @@ _st_stack_t *_st_stack_new(int stack_size) @@ -75,8 +74,9 @@ _st_stack_t *_st_stack_new(int stack_size)
75 } 74 }
76 75
77 /* Make a new thread stack object. */ 76 /* Make a new thread stack object. */
78 - if ((ts = (_st_stack_t *)calloc(1, sizeof(_st_stack_t))) == NULL) 77 + if ((ts = (_st_stack_t *)calloc(1, sizeof(_st_stack_t))) == NULL) {
79 return NULL; 78 return NULL;
  79 + }
80 extra = _st_randomize_stacks ? _ST_PAGE_SIZE : 0; 80 extra = _st_randomize_stacks ? _ST_PAGE_SIZE : 0;
81 ts->vaddr_size = stack_size + 2*REDZONE + extra; 81 ts->vaddr_size = stack_size + 2*REDZONE + extra;
82 ts->vaddr = _st_new_stk_segment(ts->vaddr_size); 82 ts->vaddr = _st_new_stk_segment(ts->vaddr_size);
@@ -103,21 +103,20 @@ _st_stack_t *_st_stack_new(int stack_size) @@ -103,21 +103,20 @@ _st_stack_t *_st_stack_new(int stack_size)
103 return ts; 103 return ts;
104 } 104 }
105 105
106 -  
107 /* 106 /*
108 * Free the stack for the current thread 107 * Free the stack for the current thread
109 */ 108 */
110 void _st_stack_free(_st_stack_t *ts) 109 void _st_stack_free(_st_stack_t *ts)
111 { 110 {
112 - if (!ts) 111 + if (!ts) {
113 return; 112 return;
  113 + }
114 114
115 /* Put the stack on the free list */ 115 /* Put the stack on the free list */
116 ST_APPEND_LINK(&ts->links, _st_free_stacks.prev); 116 ST_APPEND_LINK(&ts->links, _st_free_stacks.prev);
117 _st_num_free_stacks++; 117 _st_num_free_stacks++;
118 } 118 }
119 119
120 -  
121 static char *_st_new_stk_segment(int size) 120 static char *_st_new_stk_segment(int size)
122 { 121 {
123 #ifdef MALLOC_STACK 122 #ifdef MALLOC_STACK
@@ -127,28 +126,29 @@ static char *_st_new_stk_segment(int size) @@ -127,28 +126,29 @@ static char *_st_new_stk_segment(int size)
127 int mmap_flags = MAP_PRIVATE; 126 int mmap_flags = MAP_PRIVATE;
128 void *vaddr; 127 void *vaddr;
129 128
130 -#if defined (MD_USE_SYSV_ANON_MMAP) 129 + #if defined (MD_USE_SYSV_ANON_MMAP)
131 if (zero_fd < 0) { 130 if (zero_fd < 0) {
132 - if ((zero_fd = open("/dev/zero", O_RDWR, 0)) < 0) 131 + if ((zero_fd = open("/dev/zero", O_RDWR, 0)) < 0) {
133 return NULL; 132 return NULL;
  133 + }
134 fcntl(zero_fd, F_SETFD, FD_CLOEXEC); 134 fcntl(zero_fd, F_SETFD, FD_CLOEXEC);
135 } 135 }
136 -#elif defined (MD_USE_BSD_ANON_MMAP) 136 + #elif defined (MD_USE_BSD_ANON_MMAP)
137 mmap_flags |= MAP_ANON; 137 mmap_flags |= MAP_ANON;
138 -#else  
139 -#error Unknown OS  
140 -#endif 138 + #else
  139 + #error Unknown OS
  140 + #endif
141 141
142 vaddr = mmap(NULL, size, PROT_READ | PROT_WRITE, mmap_flags, zero_fd, 0); 142 vaddr = mmap(NULL, size, PROT_READ | PROT_WRITE, mmap_flags, zero_fd, 0);
143 - if (vaddr == (void *)MAP_FAILED) 143 + if (vaddr == (void *)MAP_FAILED) {
144 return NULL; 144 return NULL;
  145 + }
145 146
146 -#endif /* MALLOC_STACK */ 147 +#endif
147 148
148 return (char *)vaddr; 149 return (char *)vaddr;
149 } 150 }
150 151
151 -  
152 /* Not used */ 152 /* Not used */
153 #if 0 153 #if 0
154 void _st_delete_stk_segment(char *vaddr, int size) 154 void _st_delete_stk_segment(char *vaddr, int size)
@@ -166,8 +166,9 @@ int st_randomize_stacks(int on) @@ -166,8 +166,9 @@ int st_randomize_stacks(int on)
166 int wason = _st_randomize_stacks; 166 int wason = _st_randomize_stacks;
167 167
168 _st_randomize_stacks = on; 168 _st_randomize_stacks = on;
169 - if (on) 169 + if (on) {
170 srandom((unsigned int) st_utime()); 170 srandom((unsigned int) st_utime());
  171 + }
171 172
172 return wason; 173 return wason;
173 } 174 }