r17677 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r17676‎ | r17677 | r17678 >
Date:18:57, 14 November 2006
Author:river
Status:old
Tags:
Comment:
flalloc, ptalloc should free cache at exit.
imstring op= double free
Modified paths:
  • /trunk/willow/src/include/flalloc.h (modified) (history)
  • /trunk/willow/src/include/ptalloc.h (modified) (history)
  • /trunk/willow/src/include/wbackend.h (modified) (history)
  • /trunk/willow/src/include/wconfig.h (modified) (history)
  • /trunk/willow/src/include/whttp_header.h (modified) (history)
  • /trunk/willow/src/include/willow.h (modified) (history)
  • /trunk/willow/src/include/wnet.h (modified) (history)
  • /trunk/willow/src/include/wthread.h (modified) (history)
  • /trunk/willow/src/willow/wbackend.cc (modified) (history)
  • /trunk/willow/src/willow/wcache.cc (modified) (history)
  • /trunk/willow/src/willow/wconfig.cc (modified) (history)
  • /trunk/willow/src/willow/whttp.cc (modified) (history)
  • /trunk/willow/src/willow/willow.cc (modified) (history)
  • /trunk/willow/src/willow/wnet.cc (modified) (history)

Diff [purge]

Index: trunk/willow/src/include/wconfig.h
@@ -32,6 +32,10 @@
3333 int port;
3434 int group;
3535 wnet::socket *sock;
 36+
 37+ ~listener() {
 38+ delete sock;
 39+ }
3640 };
3741 extern vector<listener *> listeners;
3842 extern map<wsocket *, int> lsn2group;
Index: trunk/willow/src/include/wthread.h
@@ -223,11 +223,13 @@
224224 return ~v1.v;
225225 }
226226
227 -template<typename T>
 227+void tss_null_dtor(void *);
 228+
 229+template<typename T, void dtor (void *) = tss_null_dtor>
228230 struct tss {
229231 mutable pthread_key_t _key;
230232 tss() {
231 - pthread_key_create(&_key, NULL);
 233+ pthread_key_create(&_key, dtor);
232234 }
233235 T const& operator* (void) const {
234236 return *(T *)pthread_getspecific(_key);
Index: trunk/willow/src/include/ptalloc.h
@@ -47,13 +47,24 @@
4848 };
4949
5050 struct pttsswrap {
 51+ static void pttsswrapdtor(void *p) {
 52+ pta_block **pt = (pta_block **)p, *n = *pt, *o;
 53+ while (o = n) {
 54+ n = n->next;
 55+ free(o);
 56+ }
 57+ delete pt;
 58+ }
 59+
5160 pttsswrap() {
52 - pthread_key_create(&key, NULL);
 61+ pthread_key_create(&key, pttsswrapdtor);
5362 }
5463 pthread_key_t key;
5564 };
5665
57 -extern tss<vector<pta_block *> > ptfreelist;
 66+void ptdealloc(void *);
 67+
 68+extern tss<vector<pta_block *>, ptdealloc> ptfreelist;
5869 extern pttsswrap pttssw;
5970
6071 template<typename T>
@@ -106,6 +117,7 @@
107118 size_t sz = sizeof(T) * n;
108119 int exp = ilog2(sz) + 1;
109120 void *ret;
 121+
110122 if (!ptfreelist)
111123 ptfreelist = new vector<pta_block *>;
112124 vector<pta_block *> &fl = *ptfreelist;
@@ -119,7 +131,7 @@
120132 return (pointer) ret;
121133 }
122134 /* no, need a new block */
123 - ret = new char[2 << (exp - 1)];
 135+ ret = new char[2 << exp];
124136 return (pointer) ret;
125137 }
126138
@@ -127,6 +139,7 @@
128140 size_t sz = sizeof(T) * n;
129141 int exp = ilog2(sz) + 1;
130142 pta_block *ptb = get_ptb();
 143+
131144 if (!ptfreelist)
132145 ptfreelist = new vector<pta_block *>;
133146 vector<pta_block *> &fl = *ptfreelist;
Index: trunk/willow/src/include/flalloc.h
@@ -18,9 +18,20 @@
1919 #include "wthread.h"
2020
2121 template<typename T>
 22+void
 23+flalloc_dtor(void *p)
 24+{
 25+T *n = (T *)p, *o;
 26+ while (o = n) {
 27+ n = n->_freelist_next;
 28+ ::operator delete(o);
 29+ }
 30+}
 31+
 32+template<typename T>
2233 struct freelist_allocator {
2334 T *_freelist_next;
24 -static tss<T> _freelist;
 35+static tss<T, flalloc_dtor<T> > _freelist;
2536
2637 void *operator new(std::size_t size) {
2738 if (_freelist) {
@@ -46,6 +57,6 @@
4758 };
4859
4960 template<typename T>
50 -tss<T> freelist_allocator<T>::_freelist;
 61+tss<T, flalloc_dtor<T> > freelist_allocator<T>::_freelist;
5162
5263 #endif
Index: trunk/willow/src/include/willow.h
@@ -183,6 +183,12 @@
184184 bool operator!= (basic_imstring const &) const;
185185
186186 private:
 187+ template<typename charT2, typename allocator2>
 188+ basic_imstring(basic_imstring<charT2, allocator2> const &);
 189+ template<typename charT2, typename allocator2>
 190+ basic_imstring &
 191+ operator=(basic_imstring<charT2, allocator2> const &);
 192+
187193 charT *_buf, *_end;
188194 size_type _len;
189195
@@ -200,6 +206,8 @@
201207
202208 template<typename charT, typename allocator>
203209 basic_imstring<charT, allocator>::basic_imstring(charT const *str)
 210+ : _buf(NULL)
 211+ , _len(0)
204212 {
205213 reserve(strlen(str));
206214 _end = _buf + _len;
@@ -208,6 +216,8 @@
209217
210218 template<typename charT, typename allocator>
211219 basic_imstring<charT, allocator>::basic_imstring(charT const *str, size_type len)
 220+ : _buf(NULL)
 221+ , _len(0)
212222 {
213223 reserve(len);
214224 _end = _buf + _len;
@@ -217,6 +227,8 @@
218228
219229 template<typename charT, typename allocator>
220230 basic_imstring<charT, allocator>::basic_imstring(basic_imstring const &o)
 231+ : _buf(NULL)
 232+ , _len(0)
221233 {
222234 reserve(o._len);
223235 _end = _buf + _len;
@@ -260,7 +272,18 @@
261273 if (this == &o)
262274 return *this;
263275
264 - _alloc.deallocate(_buf, _len);
 276+ if (_buf) {
 277+ _alloc.deallocate(_buf, _len);
 278+ _buf = NULL;
 279+ _len = 0;
 280+ }
 281+
 282+ if (!o._buf) {
 283+ _buf = 0;
 284+ _len = 0;
 285+ return *this;
 286+ }
 287+
265288 reserve(o._len);
266289 _end = _buf + _len;
267290 memcpy(_buf, o._buf, _len + 1);
@@ -271,6 +294,8 @@
272295 template<typename Sallocator>
273296 basic_imstring<charT, allocator>::basic_imstring(
274297 basic_string<charT, char_traits<charT>, Sallocator> const &s)
 298+ : _buf(NULL)
 299+ , _len(0)
275300 {
276301 reserve(s.size());
277302 _end = _buf + _len;
@@ -284,6 +309,8 @@
285310 basic_imstring<charT, allocator>::reserve(
286311 basic_imstring<charT, allocator>::size_type s)
287312 {
 313+ if (_buf)
 314+ _alloc.deallocate(_buf, _len);
288315 _buf = _alloc.allocate(s + 1);
289316 _len = s;
290317 _end = _buf;
Index: trunk/willow/src/include/wnet.h
@@ -47,7 +47,7 @@
4848 #include "willow.h"
4949 #include "polycaller.h"
5050
51 -struct client_data;
 51+extern bool wnet_exit;
5252
5353 namespace wnet {
5454 struct addrlist;
@@ -145,7 +145,6 @@
146146 extern char current_time_str[];
147147 extern char current_time_short[];
148148 extern time_t current_time;
149 -extern int wnet_exit;
150149
151150 void wnet_add_accept_wakeup (wnet::socket *);
152151 void wnet_set_time (void);
Index: trunk/willow/src/include/wbackend.h
@@ -96,6 +96,7 @@
9797
9898 struct backend_pool {
9999 backend_pool(string const &name, lb_type);
 100+ ~backend_pool();
100101
101102 void add (string const &, int, int);
102103 backend_list *get_list (imstring const & url, imstring const &host);
Index: trunk/willow/src/include/whttp_header.h
@@ -186,6 +186,7 @@
187187 , _http_reqtype(REQTYPE_INVALID)
188188 , _no_keepalive(false)
189189 , _force_keepalive(false)
 190+ , _follow_redirect(false)
190191 , _eof(false)
191192 {
192193 _flags.f_chunked = 0;
Index: trunk/willow/src/willow/wconfig.cc
@@ -443,6 +443,7 @@
444444 config.keepalive_max = 0;
445445 config.max_redirects = 1;
446446 config.use_dio = false;
 447+ config.x_follow = false;
447448
448449 conf.set(*t);
449450 whttp_reconfigure();
Index: trunk/willow/src/willow/whttp.cc
@@ -172,6 +172,9 @@
173173 imstring _request_host;
174174 imstring _request_path;
175175 int _nredir;
 176+
 177+private:
 178+ httpcllr(const httpcllr &);
176179 };
177180
178181 httpcllr::httpcllr(wsocket *s, int gr)
@@ -673,10 +676,13 @@
674677 % config.nthreads);
675678 for (int i = 0; i < config.nthreads; ++i) {
676679 http_thread *t = new http_thread;
 680+ pthread_attr_t attr;
 681+ pthread_attr_init(&attr);
 682+ pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
677683 t->sv = wnet::socket::socketpair(st_dgram);
678684 wnet_add_accept_wakeup(t->sv.first);
679685 threads.push_back(t);
680 - pthread_create(&t->thr, NULL, client_thread, t);
 686+ pthread_create(&t->thr, &attr, client_thread, t);
681687 }
682688 whttp_header_init();
683689 }
@@ -731,13 +737,20 @@
732738 sv.second->readback(polycaller<wsocket *, int>(*this,
733739 &http_thread::accept_wakeup), 0);
734740 event_base_loop(evb, 0);
735 - wlog(WLOG_ERROR, format("event_base_loop: %e"));
736 - exit(1);
 741+ delete merge_ev;
 742+ delete stats.tcur;
 743+ return;
737744 }
738745
739746 static void
740747 stats_merge(int, short, void *)
741748 {
 749+timeval tv = {0, 0};
 750+ if (wnet_exit) {
 751+ event_base_loopexit(evb, &tv);
 752+ return;
 753+ }
 754+
742755 { HOLDING(stats.cur_lock);
743756 stats.cur.n_httpreq_ok += stats.tcur->n_httpreq_ok;
744757 stats.tcur->n_httpreq_ok = 0;
Index: trunk/willow/src/willow/wnet.cc
@@ -56,7 +56,7 @@
5757
5858 static void secondly_sched(void);
5959
60 -int wnet_exit;
 60+bool wnet_exit;
6161 vector<wsocket *> awaks;
6262 int cawak;
6363
@@ -583,12 +583,17 @@
584584 void
585585 sig_exit(int sig, short what, void *d)
586586 {
587 - exit(0);
 587+ wnet_exit = true;
588588 }
589589
590590 void
591591 ioloop_t::run(void)
592592 {
593 - event_base_loop(evb, 0);
594 - perror("event_base_loop");
 593+ while (!wnet_exit) {
 594+ event_base_loop(evb, EVLOOP_ONCE);
 595+ }
 596+
 597+size_t i;
 598+ for (i = 0; i < listeners.size(); ++i)
 599+ delete listeners[i];
595600 }
Index: trunk/willow/src/willow/willow.cc
@@ -34,18 +34,38 @@
3535
3636 static const char *progname;
3737
38 -tss<vector<pta_block *> > ptfreelist;
 38+tss<vector<pta_block *>, ptdealloc> ptfreelist;
3939 pttsswrap pttssw;
 40+event checkexit_ev;
 41+timeval checkexit_tv;
4042
4143 #define min(x,y) ((x) < (y) ? (x) : (y))
4244
43 -static void
44 -sig_exit(int)
 45+static void checkexit_sched(void);
 46+
 47+static void
 48+checkexit_update(int, short, void *)
4549 {
46 - wnet_exit = 1;
 50+timeval tv = {0, 0};
 51+ if (wnet_exit) {
 52+ event_del(&checkexit_ev);
 53+ event_base_loopexit(evb, &tv);
 54+ return;
 55+ }
 56+ checkexit_sched();
4757 }
4858
4959 static void
 60+checkexit_sched(void)
 61+{
 62+ checkexit_tv.tv_usec = 0;
 63+ checkexit_tv.tv_sec = 1;
 64+ evtimer_set(&checkexit_ev, checkexit_update, NULL);
 65+ event_base_set(evb, &checkexit_ev);
 66+ event_add(&checkexit_ev, &checkexit_tv);
 67+}
 68+
 69+static void
5070 usage(void)
5171 {
5272 fprintf(stderr, "usage: %s [-hfzv] [-D cond[=value]]\n"
@@ -59,6 +79,28 @@
6080 , progname);
6181 }
6282
 83+void
 84+tss_null_dtor(void *)
 85+{
 86+}
 87+
 88+void
 89+ptdealloc(void *p)
 90+{
 91+vector<pta_block *> *v = (vector<pta_block *> *)p;
 92+std::cout<<"ptdealloc\n";
 93+ for (vector<pta_block *>::iterator it = v->begin(), end = v->end();
 94+ it != end; ++it) {
 95+ pta_block *n = *it, *o;
 96+ while (o = n) {
 97+ n = n->next;
 98+ delete [] (char *)o->addr;
 99+ free(o);
 100+ }
 101+ }
 102+ delete v;
 103+}
 104+
63105 int
64106 main(int argc, char *argv[])
65107 {
@@ -152,23 +194,23 @@
153195
154196 make_event_base();
155197 ioloop = new ioloop_t;
 198+ checkexit_sched();
156199 whttp_init();
157200 wcache_init(1);
158201 stats_init();
159202
160 - (void)signal(SIGINT, sig_exit);
161 - (void)signal(SIGTERM, sig_exit);
162 -
163203 wlog(WLOG_NOTICE, "running");
164204
165205 if (!config.foreground)
166206 daemon(0, 0);
167207
168208 ioloop->run();
 209+ wlog(WLOG_NOTICE, "shutting down");
169210 wlog_close();
170211 wcache_shutdown();
171212 whttp_shutdown();
172 -
 213+
 214+ pthread_exit(NULL);
173215 return EXIT_SUCCESS;
174216 }
175217
Index: trunk/willow/src/willow/wbackend.cc
@@ -85,6 +85,7 @@
8686 % addr % it->straddr());
8787 }
8888
 89+ delete list;
8990 _carp_calc();
9091 }
9192
@@ -190,6 +191,12 @@
191192 _carp_recalc(url, host, lbt);
192193 }
193194
 195+backend_pool::~backend_pool(void)
 196+{
 197+ for (int i = 0; i < backends.size(); ++i)
 198+ delete backends[i];
 199+}
 200+
194201 backend_list *
195202 backend_pool::get_list(imstring const &url, imstring const &host)
196203 {
Index: trunk/willow/src/willow/wcache.cc
@@ -275,6 +275,8 @@
276276 int stlen;
277277 struct cache_object *obj;
278278
 279+ if (!config.caches)
 280+ return;
279281 stlen = strlen(config.caches[0].dir) + 1 + 5 + 1;
280282 if ((stpath = (char *)wmalloc(stlen)) == NULL)
281283 outofmemory();