Skip to content

Commit 900fcee

Browse files
Tony LiuTony Liu
authored andcommitted
use ngx_calloc to replace ngx_pcalloc to avoid memory leak
#53
1 parent 970fe48 commit 900fcee

6 files changed

+30
-33
lines changed

src/http/ngx_http_accounting_module.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ ngx_http_accounting_process_init(ngx_cycle_t *cycle)
139139
}
140140

141141
if (amcf->current == NULL) {
142-
if (ngx_traffic_accounting_period_create(cycle->pool, amcf) != NGX_OK)
142+
if (ngx_traffic_accounting_period_create(amcf) != NGX_OK)
143143
return NGX_ERROR;
144144
}
145145

@@ -206,7 +206,7 @@ worker_process_alarm_handler(ngx_event_t *ev)
206206

207207
amcf = ngx_http_cycle_get_module_main_conf(ngx_cycle, ngx_http_accounting_module);
208208

209-
ngx_traffic_accounting_period_rotate(amcf->current->pool, amcf);
209+
ngx_traffic_accounting_period_rotate(amcf);
210210
ngx_traffic_accounting_period_rbtree_iterate(amcf->previous,
211211
worker_process_export_metrics,
212212
amcf->previous->created_at,
@@ -243,10 +243,10 @@ ngx_http_accounting_request_handler(ngx_http_request_t *r)
243243

244244
amcf = ngx_http_get_module_main_conf(r, ngx_http_accounting_module);
245245

246-
metrics = ngx_traffic_accounting_period_fetch_metrics(amcf->current, accounting_id);
246+
metrics = ngx_traffic_accounting_period_fetch_metrics(amcf->current, accounting_id, amcf->log);
247247
if (metrics == NULL) { return NGX_ERROR; }
248248

249-
if (ngx_traffic_accounting_metrics_init(metrics, amcf->current->pool, ngx_http_statuses_len) == NGX_ERROR)
249+
if (ngx_traffic_accounting_metrics_init(metrics, ngx_http_statuses_len, amcf->log) == NGX_ERROR)
250250
return NGX_ERROR;
251251

252252
amcf->current->updated_at = ngx_timeofday();

src/ngx_traffic_accounting.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,21 +38,19 @@ typedef struct {
3838
ngx_rbtree_t rbtree;
3939
ngx_rbtree_node_t sentinel;
4040

41-
ngx_pool_t *pool;
42-
4341
ngx_time_t *created_at;
4442
ngx_time_t *updated_at;
4543
} ngx_traffic_accounting_period_t;
4644

47-
ngx_int_t ngx_traffic_accounting_metrics_init(ngx_traffic_accounting_metrics_t *metrics, ngx_pool_t *pool, size_t len);
45+
ngx_int_t ngx_traffic_accounting_metrics_init(ngx_traffic_accounting_metrics_t *metrics, size_t len, ngx_log_t *log);
4846

4947
ngx_int_t ngx_traffic_accounting_period_init(ngx_traffic_accounting_period_t *period);
50-
void ngx_traffic_accounting_period_insert(ngx_traffic_accounting_period_t *period, ngx_str_t *name);
48+
void ngx_traffic_accounting_period_insert(ngx_traffic_accounting_period_t *period, ngx_str_t *name, ngx_log_t *log);
5149
void ngx_traffic_accounting_period_insert_metrics(ngx_traffic_accounting_period_t *period, ngx_traffic_accounting_metrics_t *metrics);
5250
void ngx_traffic_accounting_period_delete(ngx_traffic_accounting_period_t *period, ngx_str_t *name);
5351
void ngx_traffic_accounting_period_delete_metrics(ngx_traffic_accounting_period_t *period, ngx_traffic_accounting_metrics_t *metrics);
5452
ngx_traffic_accounting_metrics_t * ngx_traffic_accounting_period_lookup_metrics(ngx_traffic_accounting_period_t *period, ngx_str_t *name);
55-
ngx_traffic_accounting_metrics_t * ngx_traffic_accounting_period_fetch_metrics(ngx_traffic_accounting_period_t *period, ngx_str_t *name);
53+
ngx_traffic_accounting_metrics_t * ngx_traffic_accounting_period_fetch_metrics(ngx_traffic_accounting_period_t *period, ngx_str_t *name, ngx_log_t *log);
5654

5755
typedef ngx_int_t (*ngx_traffic_accounting_period_iterate_func)(void *val, void *para1, void *para2);
5856

src/ngx_traffic_accounting_module.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,14 @@
99

1010

1111
ngx_int_t
12-
ngx_traffic_accounting_period_create(ngx_pool_t *pool, ngx_traffic_accounting_main_conf_t *amcf)
12+
ngx_traffic_accounting_period_create(ngx_traffic_accounting_main_conf_t *amcf)
1313
{
1414
ngx_traffic_accounting_period_t *period;
1515

16-
period = ngx_pcalloc(pool, sizeof(ngx_traffic_accounting_period_t));
16+
period = ngx_calloc(sizeof(ngx_traffic_accounting_period_t), amcf->log);
1717
if (period == NULL)
1818
return NGX_ERROR;
1919

20-
period->pool = pool;
2120
ngx_traffic_accounting_period_init(period);
2221

2322
period->created_at = ngx_timeofday();
@@ -28,11 +27,11 @@ ngx_traffic_accounting_period_create(ngx_pool_t *pool, ngx_traffic_accounting_ma
2827
}
2928

3029
ngx_int_t
31-
ngx_traffic_accounting_period_rotate(ngx_pool_t *pool, ngx_traffic_accounting_main_conf_t *amcf)
30+
ngx_traffic_accounting_period_rotate(ngx_traffic_accounting_main_conf_t *amcf)
3231
{
33-
ngx_pfree(pool, amcf->previous);
32+
ngx_free(amcf->previous);
3433

3534
amcf->previous = amcf->current;
3635

37-
return ngx_traffic_accounting_period_create(pool, amcf);
36+
return ngx_traffic_accounting_period_create(amcf);
3837
}

src/ngx_traffic_accounting_module.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ ngx_str_t * ngx_traffic_accounting_get_accounting_id(void *entry, ngx_get_loc_co
4646
ngx_get_indexed_variable_pt get_indexed_variable);
4747

4848

49-
ngx_int_t ngx_traffic_accounting_period_create(ngx_pool_t *pool, ngx_traffic_accounting_main_conf_t *amcf);
50-
ngx_int_t ngx_traffic_accounting_period_rotate(ngx_pool_t *pool, ngx_traffic_accounting_main_conf_t *amcf);
49+
ngx_int_t ngx_traffic_accounting_period_create(ngx_traffic_accounting_main_conf_t *amcf);
50+
ngx_int_t ngx_traffic_accounting_period_rotate(ngx_traffic_accounting_main_conf_t *amcf);
5151

5252

5353
#endif /* _NGX_TRAFFIC_ACCOUNTING_MODULE_H_INCLUDED_ */

src/ngx_traffic_accounting_period_metrics.c

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,17 @@
1010
static void ngx_traffic_accounting_period_insert_value(ngx_rbtree_node_t *temp, ngx_rbtree_node_t *node, ngx_rbtree_node_t *sentinel);
1111

1212
ngx_int_t
13-
ngx_traffic_accounting_metrics_init(ngx_traffic_accounting_metrics_t *metrics, ngx_pool_t *pool, size_t len)
13+
ngx_traffic_accounting_metrics_init(ngx_traffic_accounting_metrics_t *metrics, size_t len, ngx_log_t *log)
1414
{
1515
if (metrics->nr_status == NULL) {
16-
metrics->nr_status = ngx_pcalloc(pool, sizeof(ngx_uint_t) * len);
16+
metrics->nr_status = ngx_calloc(sizeof(ngx_uint_t) * len, log);
1717

1818
if (metrics->nr_status == NULL)
1919
return NGX_ERROR;
2020
}
2121

2222
if (metrics->nr_upstream_status == NULL) {
23-
metrics->nr_upstream_status = ngx_pcalloc(pool, sizeof(ngx_uint_t) * len);
23+
metrics->nr_upstream_status = ngx_calloc(sizeof(ngx_uint_t) * len, log);
2424

2525
if (metrics->nr_upstream_status == NULL)
2626
return NGX_ERROR;
@@ -39,14 +39,14 @@ ngx_traffic_accounting_period_init(ngx_traffic_accounting_period_t *period)
3939
}
4040

4141
void
42-
ngx_traffic_accounting_period_insert(ngx_traffic_accounting_period_t *period, ngx_str_t *name)
42+
ngx_traffic_accounting_period_insert(ngx_traffic_accounting_period_t *period, ngx_str_t *name, ngx_log_t *log)
4343
{
4444
ngx_traffic_accounting_metrics_t *metrics;
4545

46-
metrics = ngx_pcalloc(period->pool, sizeof(ngx_traffic_accounting_metrics_t));
46+
metrics = ngx_calloc(sizeof(ngx_traffic_accounting_metrics_t), log);
4747

4848
void *data;
49-
data = ngx_pcalloc(period->pool, name->len+1);
49+
data = ngx_calloc(name->len+1, log);
5050
ngx_memcpy(data, name->data, name->len);
5151

5252
metrics->name.data = data;
@@ -82,7 +82,7 @@ void
8282
ngx_traffic_accounting_period_delete_metrics(ngx_traffic_accounting_period_t *period, ngx_traffic_accounting_metrics_t *metrics)
8383
{
8484
ngx_rbtree_delete(&period->rbtree, &metrics->rbnode);
85-
ngx_pfree(period->pool, metrics);
85+
ngx_free(metrics);
8686
}
8787

8888
ngx_traffic_accounting_metrics_t *
@@ -124,15 +124,15 @@ ngx_traffic_accounting_period_lookup_metrics(ngx_traffic_accounting_period_t *pe
124124
}
125125

126126
ngx_traffic_accounting_metrics_t *
127-
ngx_traffic_accounting_period_fetch_metrics(ngx_traffic_accounting_period_t *period, ngx_str_t *name)
127+
ngx_traffic_accounting_period_fetch_metrics(ngx_traffic_accounting_period_t *period, ngx_str_t *name, ngx_log_t *log)
128128
{
129129
ngx_traffic_accounting_metrics_t *n;
130130

131131
n = ngx_traffic_accounting_period_lookup_metrics(period, name);
132132
if (n != NULL)
133133
return n;
134134

135-
ngx_traffic_accounting_period_insert(period, name);
135+
ngx_traffic_accounting_period_insert(period, name, log);
136136

137137
return ngx_traffic_accounting_period_lookup_metrics(period, name);
138138
}
@@ -158,9 +158,9 @@ ngx_traffic_accounting_period_rbtree_iterate(ngx_traffic_accounting_period_t *pe
158158
if (rc == NGX_DONE) {
159159
/* NGX_DONE -> destroy node */
160160
ngx_rbtree_delete(rbtree, node);
161-
ngx_pfree(period->pool, n->nr_status);
162-
ngx_pfree(period->pool, n->nr_upstream_status);
163-
ngx_pfree(period->pool, n);
161+
ngx_free(n->nr_status);
162+
ngx_free(n->nr_upstream_status);
163+
ngx_free(n);
164164

165165
goto done;
166166
}

src/stream/ngx_stream_accounting_module.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ ngx_stream_accounting_process_init(ngx_cycle_t *cycle)
136136
}
137137

138138
if (amcf->current == NULL) {
139-
if (ngx_traffic_accounting_period_create(cycle->pool, amcf) != NGX_OK)
139+
if (ngx_traffic_accounting_period_create(amcf) != NGX_OK)
140140
return NGX_ERROR;
141141
}
142142

@@ -203,7 +203,7 @@ worker_process_alarm_handler(ngx_event_t *ev)
203203

204204
amcf = ngx_stream_cycle_get_module_main_conf(ngx_cycle, ngx_stream_accounting_module);
205205

206-
ngx_traffic_accounting_period_rotate(amcf->current->pool, amcf);
206+
ngx_traffic_accounting_period_rotate(amcf);
207207
ngx_traffic_accounting_period_rbtree_iterate(amcf->previous,
208208
worker_process_export_metrics,
209209
amcf->previous->created_at,
@@ -240,10 +240,10 @@ ngx_stream_accounting_session_handler(ngx_stream_session_t *s)
240240

241241
amcf = ngx_stream_get_module_main_conf(s, ngx_stream_accounting_module);
242242

243-
metrics = ngx_traffic_accounting_period_fetch_metrics(amcf->current, accounting_id);
243+
metrics = ngx_traffic_accounting_period_fetch_metrics(amcf->current, accounting_id, amcf->log);
244244
if (metrics == NULL) { return NGX_ERROR; }
245245

246-
if (ngx_traffic_accounting_metrics_init(metrics, amcf->current->pool, ngx_stream_statuses_len) == NGX_ERROR)
246+
if (ngx_traffic_accounting_metrics_init(metrics, ngx_stream_statuses_len, amcf->log) == NGX_ERROR)
247247
return NGX_ERROR;
248248

249249
amcf->current->updated_at = ngx_timeofday();

0 commit comments

Comments
 (0)