Skip to content

Commit a2f3201

Browse files
author
blog.s135.com
committed
内存申请失败处理。
1 parent cdecf76 commit a2f3201

1 file changed

Lines changed: 14 additions & 20 deletions

File tree

httpsqs.c

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
HTTP Simple Queue Service - httpsqs v1.4.20100713
2+
HTTP Simple Queue Service - httpsqs v1.4.20100928
33
Author: Zhang Yan (http://blog.s135.com), E-mail: [email protected]
44
This is free software, and you are welcome to modify and redistribute it under the New BSD License
55
*/
@@ -116,7 +116,7 @@ char *urldecode(char *input_str)
116116
static void show_help(void)
117117
{
118118
char *b = "--------------------------------------------------------------------------------------------------\n"
119-
"HTTP Simple Queue Service - httpsqs v" VERSION " (July 13, 2010)\n\n"
119+
"HTTP Simple Queue Service - httpsqs v" VERSION " (September 28, 2010)\n\n"
120120
"Author: Zhang Yan (http://blog.s135.com), E-mail: [email protected]\n"
121121
"This is free software, and you are welcome to modify and redistribute it under the New BSD License\n"
122122
"\n"
@@ -125,7 +125,7 @@ static void show_help(void)
125125
"-x <path> database directory (example: /opt/httpsqs/data)\n"
126126
"-t <second> timeout for an http request (default: 3)\n"
127127
"-s <second> the interval to sync updated contents to the disk (default: 5)\n"
128-
"-c <num> the maximum number of non-leaf nodes to be cached (default: 10000)\n"
128+
"-c <num> the maximum number of non-leaf nodes to be cached (default: 1024)\n"
129129
"-m <size> database memory cache size in MB (default: 100)\n"
130130
"-i <file> save PID in <file> (default: /tmp/httpsqs.pid)\n"
131131
"-d run as a daemon\n"
@@ -217,7 +217,7 @@ static int httpsqs_maxqueue(const char* httpsqs_input_name, int httpsqs_input_nu
217217
/* 设置的最大的队列数量必须大于等于”当前队列写入位置点“和”当前队列读取位置点“,并且”当前队列写入位置点“必须大于等于”当前队列读取位置点“ */
218218
if (queue_maxnum_int >= queue_put_value && queue_maxnum_int >= queue_get_value && queue_put_value >= queue_get_value) {
219219
char queue_name[300]; /* 队列名称的总长度,用户输入的队列长度少于256字节 */
220-
char *queue_maxnum = (char *)malloc(16);
220+
char queue_maxnum[16];
221221
memset(queue_name, '\0', 300);
222222
memset(queue_maxnum, '\0', 16);
223223
sprintf(queue_name, "%s:%s", httpsqs_input_name, "maxqueue");
@@ -226,7 +226,6 @@ static int httpsqs_maxqueue(const char* httpsqs_input_name, int httpsqs_input_nu
226226

227227
tcbdbsync(httpsqs_db_tcbdb); /* 实时刷新到磁盘 */
228228

229-
free(queue_maxnum);
230229
return queue_maxnum_int;
231230
}
232231

@@ -285,7 +284,7 @@ static int httpsqs_now_putpos(const char* httpsqs_input_name)
285284
int queue_put_value = 0;
286285
int queue_get_value = 0;
287286
char queue_name[300]; /* 队列名称的总长度,用户输入的队列长度少于256字节 */
288-
char *queue_input = (char *)malloc(32);
287+
char queue_input[32];
289288

290289
/* 获取最大队列数量 */
291290
maxqueue_num = httpsqs_read_maxqueue(httpsqs_input_name);
@@ -311,8 +310,7 @@ static int httpsqs_now_putpos(const char* httpsqs_input_name)
311310
} else { /* 队列写入位置点加1后的值,回写入数据库 */
312311
memset(queue_input, '\0', 32);
313312
sprintf(queue_input, "%d", queue_put_value);
314-
tcbdbput2(httpsqs_db_tcbdb, queue_name, queue_input);
315-
free(queue_input);
313+
tcbdbput2(httpsqs_db_tcbdb, queue_name, (char *)queue_input);
316314
}
317315

318316
return queue_put_value;
@@ -345,19 +343,17 @@ static int httpsqs_now_getpos(const char* httpsqs_input_name)
345343
/* 如果队列的读取值(出队列)小于队列的写入值(入队列) */
346344
} else if (queue_get_value < queue_put_value) {
347345
queue_get_value = queue_get_value + 1;
348-
char *queue_input = (char *)malloc(32);
346+
char queue_input[32];
349347
memset(queue_input, '\0', 32);
350348
sprintf(queue_input, "%d", queue_get_value);
351349
tcbdbput2(httpsqs_db_tcbdb, queue_name, queue_input);
352-
free(queue_input);
353350
/* 如果队列的读取值(出队列)大于队列的写入值(入队列),并且队列的读取值(出队列)小于最大队列数量 */
354351
} else if (queue_get_value > queue_put_value && queue_get_value < maxqueue_num) {
355352
queue_get_value = queue_get_value + 1;
356-
char *queue_input = (char *)malloc(32);
353+
char queue_input[32];
357354
memset(queue_input, '\0', 32);
358355
sprintf(queue_input, "%d", queue_get_value);
359356
tcbdbput2(httpsqs_db_tcbdb, queue_name, queue_input);
360-
free(queue_input);
361357
/* 如果队列的读取值(出队列)大于队列的写入值(入队列),并且队列的读取值(出队列)等于最大队列数量 */
362358
} else if (queue_get_value > queue_put_value && queue_get_value == maxqueue_num) {
363359
queue_get_value = 1;
@@ -400,11 +396,10 @@ void httpsqs_handler(struct evhttp_request *req, void *arg)
400396

401397
/* 返回给用户的Header头信息 */
402398
if (httpsqs_input_charset != NULL && strlen(httpsqs_input_charset) <= 40) {
403-
char *content_type = (char *)malloc(64);
399+
char content_type[64];
404400
memset(content_type, '\0', 64);
405401
sprintf(content_type, "text/plain; charset=%s", httpsqs_input_charset);
406402
evhttp_add_header(req->output_headers, "Content-Type", content_type);
407-
free(content_type);
408403
} else {
409404
evhttp_add_header(req->output_headers, "Content-Type", "text/plain");
410405
}
@@ -426,7 +421,7 @@ void httpsqs_handler(struct evhttp_request *req, void *arg)
426421
memset(queue_name, '\0', 300);
427422
sprintf(queue_name, "%s:%d", httpsqs_input_name, queue_put_value);
428423
char *httpsqs_input_postbuffer;
429-
char *buffer_data = (char *)malloc(buffer_data_len + 1);
424+
char *buffer_data = (char *)tcmalloc(buffer_data_len + 1);
430425
memset(buffer_data, '\0', buffer_data_len + 1);
431426
memcpy (buffer_data, EVBUFFER_DATA(req->input_buffer), buffer_data_len);
432427
httpsqs_input_postbuffer = urldecode(buffer_data);
@@ -449,7 +444,7 @@ void httpsqs_handler(struct evhttp_request *req, void *arg)
449444
sprintf(queue_name, "%s:%d", httpsqs_input_name, queue_put_value);
450445
buffer_data_len = strlen(httpsqs_input_data);
451446
char *httpsqs_input_postbuffer;
452-
char *buffer_data = (char *)malloc(buffer_data_len + 1);
447+
char *buffer_data = (char *)tcmalloc(buffer_data_len + 1);
453448
memset(buffer_data, '\0', buffer_data_len + 1);
454449
memcpy (buffer_data, httpsqs_input_data, buffer_data_len);
455450
httpsqs_input_postbuffer = urldecode(buffer_data);
@@ -586,7 +581,6 @@ void httpsqs_handler(struct evhttp_request *req, void *arg)
586581
/* 内存释放 */
587582
evhttp_clear_headers(&httpsqs_http_query);
588583
evbuffer_free(buf);
589-
evhttp_request_free(req);
590584
}
591585

592586
/* 信号处理 */
@@ -619,8 +613,8 @@ int main(int argc, char **argv)
619613
bool httpsqs_settings_daemon = false;
620614
int httpsqs_settings_timeout = 3; /* 单位:秒 */
621615
httpsqs_settings_syncinterval = 5; /* 单位:秒 */
622-
int httpsqs_settings_cachenonleaf = 10000; /* 缓存非叶子节点数。单位:条 */
623-
int httpsqs_settings_cacheleaf = 20000; /* 缓存叶子节点数。叶子节点缓存数为非叶子节点数的两倍。单位:条 */
616+
int httpsqs_settings_cachenonleaf = 1024; /* 缓存非叶子节点数。单位:条 */
617+
int httpsqs_settings_cacheleaf = 2048; /* 缓存叶子节点数。叶子节点缓存数为非叶子节点数的两倍。单位:条 */
624618
int httpsqs_settings_mappedmemory = 104857600; /* 单位:字节 */
625619
httpsqs_settings_pidfile = "/tmp/httpsqs.pid";
626620

@@ -682,7 +676,7 @@ int main(int argc, char **argv)
682676

683677
/* 数据表路径 */
684678
int httpsqs_settings_dataname_len = 1024;
685-
char *httpsqs_settings_dataname = (char *)malloc(httpsqs_settings_dataname_len);
679+
char *httpsqs_settings_dataname = (char *)tcmalloc(httpsqs_settings_dataname_len);
686680
memset(httpsqs_settings_dataname, '\0', httpsqs_settings_dataname_len);
687681
sprintf(httpsqs_settings_dataname, "%s/httpsqs.db", httpsqs_settings_datapath);
688682

0 commit comments

Comments
 (0)