Use calloc instead of malloc + memset; remove useless memset

This commit is contained in:
Tatsuhiro Tsujikawa 2014-03-13 23:21:04 +09:00
parent 1be8d1b797
commit aefc0d1ebb
3 changed files with 14 additions and 22 deletions

View File

@ -67,12 +67,12 @@ void nghttp2_frame_headers_init(nghttp2_headers *frame,
uint8_t flags, int32_t stream_id, int32_t pri, uint8_t flags, int32_t stream_id, int32_t pri,
nghttp2_nv *nva, size_t nvlen) nghttp2_nv *nva, size_t nvlen)
{ {
memset(frame, 0, sizeof(nghttp2_headers));
nghttp2_frame_set_hd(&frame->hd, 0, NGHTTP2_HEADERS, flags, stream_id); nghttp2_frame_set_hd(&frame->hd, 0, NGHTTP2_HEADERS, flags, stream_id);
frame->pri = pri; frame->padlen = 0;
frame->nva = nva; frame->nva = nva;
frame->nvlen = nvlen; frame->nvlen = nvlen;
frame->cat = NGHTTP2_HCAT_REQUEST; frame->cat = NGHTTP2_HCAT_REQUEST;
frame->pri = pri;
} }
void nghttp2_frame_headers_free(nghttp2_headers *frame) void nghttp2_frame_headers_free(nghttp2_headers *frame)
@ -83,7 +83,6 @@ void nghttp2_frame_headers_free(nghttp2_headers *frame)
void nghttp2_frame_priority_init(nghttp2_priority *frame, int32_t stream_id, void nghttp2_frame_priority_init(nghttp2_priority *frame, int32_t stream_id,
int32_t pri) int32_t pri)
{ {
memset(frame, 0, sizeof(nghttp2_priority));
nghttp2_frame_set_hd(&frame->hd, 4, NGHTTP2_PRIORITY, NGHTTP2_FLAG_NONE, nghttp2_frame_set_hd(&frame->hd, 4, NGHTTP2_PRIORITY, NGHTTP2_FLAG_NONE,
stream_id); stream_id);
frame->pri = pri; frame->pri = pri;
@ -96,7 +95,6 @@ void nghttp2_frame_rst_stream_init(nghttp2_rst_stream *frame,
int32_t stream_id, int32_t stream_id,
nghttp2_error_code error_code) nghttp2_error_code error_code)
{ {
memset(frame, 0, sizeof(nghttp2_rst_stream));
nghttp2_frame_set_hd(&frame->hd, 4, NGHTTP2_RST_STREAM, NGHTTP2_FLAG_NONE, nghttp2_frame_set_hd(&frame->hd, 4, NGHTTP2_RST_STREAM, NGHTTP2_FLAG_NONE,
stream_id); stream_id);
frame->error_code = error_code; frame->error_code = error_code;
@ -109,7 +107,6 @@ void nghttp2_frame_rst_stream_free(nghttp2_rst_stream *frame)
void nghttp2_frame_settings_init(nghttp2_settings *frame, uint8_t flags, void nghttp2_frame_settings_init(nghttp2_settings *frame, uint8_t flags,
nghttp2_settings_entry *iv, size_t niv) nghttp2_settings_entry *iv, size_t niv)
{ {
memset(frame, 0, sizeof(nghttp2_settings));
nghttp2_frame_set_hd(&frame->hd, niv * NGHTTP2_FRAME_SETTINGS_ENTRY_LENGTH, nghttp2_frame_set_hd(&frame->hd, niv * NGHTTP2_FRAME_SETTINGS_ENTRY_LENGTH,
NGHTTP2_SETTINGS, flags, 0); NGHTTP2_SETTINGS, flags, 0);
frame->niv = niv; frame->niv = niv;
@ -126,11 +123,11 @@ void nghttp2_frame_push_promise_init(nghttp2_push_promise *frame,
int32_t promised_stream_id, int32_t promised_stream_id,
nghttp2_nv *nva, size_t nvlen) nghttp2_nv *nva, size_t nvlen)
{ {
memset(frame, 0, sizeof(nghttp2_push_promise));
nghttp2_frame_set_hd(&frame->hd, 0, NGHTTP2_PUSH_PROMISE, flags, stream_id); nghttp2_frame_set_hd(&frame->hd, 0, NGHTTP2_PUSH_PROMISE, flags, stream_id);
frame->promised_stream_id = promised_stream_id; frame->padlen = 0;
frame->nva = nva; frame->nva = nva;
frame->nvlen = nvlen; frame->nvlen = nvlen;
frame->promised_stream_id = promised_stream_id;
} }
void nghttp2_frame_push_promise_free(nghttp2_push_promise *frame) void nghttp2_frame_push_promise_free(nghttp2_push_promise *frame)
@ -141,10 +138,11 @@ void nghttp2_frame_push_promise_free(nghttp2_push_promise *frame)
void nghttp2_frame_ping_init(nghttp2_ping *frame, uint8_t flags, void nghttp2_frame_ping_init(nghttp2_ping *frame, uint8_t flags,
const uint8_t *opaque_data) const uint8_t *opaque_data)
{ {
memset(frame, 0, sizeof(nghttp2_ping));
nghttp2_frame_set_hd(&frame->hd, 8, NGHTTP2_PING, flags, 0); nghttp2_frame_set_hd(&frame->hd, 8, NGHTTP2_PING, flags, 0);
if(opaque_data) { if(opaque_data) {
memcpy(frame->opaque_data, opaque_data, sizeof(frame->opaque_data)); memcpy(frame->opaque_data, opaque_data, sizeof(frame->opaque_data));
} else {
memset(frame->opaque_data, 0, sizeof(frame->opaque_data));
} }
} }
@ -155,7 +153,6 @@ void nghttp2_frame_goaway_init(nghttp2_goaway *frame, int32_t last_stream_id,
nghttp2_error_code error_code, nghttp2_error_code error_code,
uint8_t *opaque_data, size_t opaque_data_len) uint8_t *opaque_data, size_t opaque_data_len)
{ {
memset(frame, 0, sizeof(nghttp2_goaway));
nghttp2_frame_set_hd(&frame->hd, 8+opaque_data_len, NGHTTP2_GOAWAY, nghttp2_frame_set_hd(&frame->hd, 8+opaque_data_len, NGHTTP2_GOAWAY,
NGHTTP2_FLAG_NONE, 0); NGHTTP2_FLAG_NONE, 0);
frame->last_stream_id = last_stream_id; frame->last_stream_id = last_stream_id;
@ -174,7 +171,6 @@ void nghttp2_frame_window_update_init(nghttp2_window_update *frame,
int32_t stream_id, int32_t stream_id,
int32_t window_size_increment) int32_t window_size_increment)
{ {
memset(frame, 0, sizeof(nghttp2_window_update));
nghttp2_frame_set_hd(&frame->hd, 4, NGHTTP2_WINDOW_UPDATE, flags, stream_id); nghttp2_frame_set_hd(&frame->hd, 4, NGHTTP2_WINDOW_UPDATE, flags, stream_id);
frame->window_size_increment = window_size_increment; frame->window_size_increment = window_size_increment;
} }
@ -206,10 +202,11 @@ void nghttp2_frame_private_data_init(nghttp2_private_data *frame,
int32_t stream_id, int32_t stream_id,
const nghttp2_data_provider *data_prd) const nghttp2_data_provider *data_prd)
{ {
memset(frame, 0, sizeof(nghttp2_private_data));
/* At this moment, the length of DATA frame is unknown */ /* At this moment, the length of DATA frame is unknown */
nghttp2_frame_set_hd(&frame->hd, 0, NGHTTP2_DATA, flags, stream_id); nghttp2_frame_set_hd(&frame->hd, 0, NGHTTP2_DATA, flags, stream_id);
frame->data_prd = *data_prd; frame->data_prd = *data_prd;
frame->padlen = 0;
frame->eof = 0;
} }
void nghttp2_frame_private_data_free(nghttp2_private_data *frame) void nghttp2_frame_private_data_free(nghttp2_private_data *frame)

View File

@ -31,12 +31,13 @@
int nghttp2_map_init(nghttp2_map *map) int nghttp2_map_init(nghttp2_map *map)
{ {
map->tablelen = INITIAL_TABLE_LENGTH; map->tablelen = INITIAL_TABLE_LENGTH;
map->table = malloc(sizeof(nghttp2_map_entry*) * map->tablelen); map->table = calloc(map->tablelen, sizeof(nghttp2_map_entry*));
if(map->table == NULL) { if(map->table == NULL) {
return NGHTTP2_ERR_NOMEM; return NGHTTP2_ERR_NOMEM;
} }
memset(map->table, 0, sizeof(nghttp2_map_entry*) * map->tablelen);
map->size = 0; map->size = 0;
return 0; return 0;
} }
@ -118,11 +119,11 @@ static int resize(nghttp2_map *map, size_t new_tablelen)
{ {
size_t i; size_t i;
nghttp2_map_entry **new_table; nghttp2_map_entry **new_table;
new_table = malloc(sizeof(nghttp2_map_entry*) * new_tablelen); new_table = calloc(new_tablelen, sizeof(nghttp2_map_entry*));
if(new_table == NULL) { if(new_table == NULL) {
return NGHTTP2_ERR_NOMEM; return NGHTTP2_ERR_NOMEM;
} }
memset(new_table, 0, sizeof(nghttp2_map_entry*) * new_tablelen);
for(i = 0; i < map->tablelen; ++i) { for(i = 0; i < map->tablelen; ++i) {
nghttp2_map_entry *entry; nghttp2_map_entry *entry;
for(entry = map->table[i]; entry;) { for(entry = map->table[i]; entry;) {

View File

@ -229,12 +229,11 @@ static int nghttp2_session_new(nghttp2_session **session_ptr,
{ {
int rv; int rv;
*session_ptr = malloc(sizeof(nghttp2_session)); *session_ptr = calloc(1, sizeof(nghttp2_session));
if(*session_ptr == NULL) { if(*session_ptr == NULL) {
rv = NGHTTP2_ERR_NOMEM; rv = NGHTTP2_ERR_NOMEM;
goto fail_session; goto fail_session;
} }
memset(*session_ptr, 0, sizeof(nghttp2_session));
/* next_stream_id is initialized in either /* next_stream_id is initialized in either
nghttp2_session_client_new2 or nghttp2_session_server_new2 */ nghttp2_session_client_new2 or nghttp2_session_server_new2 */
@ -302,11 +301,6 @@ static int nghttp2_session_new(nghttp2_session **session_ptr,
nghttp2_active_outbound_item_reset(&(*session_ptr)->aob); nghttp2_active_outbound_item_reset(&(*session_ptr)->aob);
memset((*session_ptr)->remote_settings, 0,
sizeof((*session_ptr)->remote_settings));
memset((*session_ptr)->local_settings, 0,
sizeof((*session_ptr)->local_settings));
init_settings((*session_ptr)->remote_settings); init_settings((*session_ptr)->remote_settings);
init_settings((*session_ptr)->local_settings); init_settings((*session_ptr)->local_settings);