Date: Sun, 12 Oct 2008 00:44:27 +0000 (UTC) From: Xin LI <delphij@FreeBSD.org> To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r183770 - head/usr.sbin/nscd Message-ID: <200810120044.m9C0iRUt031013@svn.freebsd.org>
next in thread | raw e-mail | index | archive | help
Author: delphij Date: Sun Oct 12 00:44:27 2008 New Revision: 183770 URL: http://svn.freebsd.org/changeset/base/183770 Log: Reduce code duplication: use calloc instead of allocing and memset afterward. Approved by: bushman Modified: head/usr.sbin/nscd/agent.c head/usr.sbin/nscd/cachelib.c head/usr.sbin/nscd/cacheplcs.c head/usr.sbin/nscd/config.c head/usr.sbin/nscd/hashtable.h head/usr.sbin/nscd/mp_rs_query.c head/usr.sbin/nscd/mp_ws_query.c head/usr.sbin/nscd/nscd.c head/usr.sbin/nscd/nscdcli.c head/usr.sbin/nscd/query.c Modified: head/usr.sbin/nscd/agent.c ============================================================================== --- head/usr.sbin/nscd/agent.c Sat Oct 11 21:59:03 2008 (r183769) +++ head/usr.sbin/nscd/agent.c Sun Oct 12 00:44:27 2008 (r183770) @@ -60,9 +60,8 @@ init_agent_table() struct agent_table *retval; TRACE_IN(init_agent_table); - retval = (struct agent_table *)malloc(sizeof(struct agent_table)); + retval = (struct agent_table *)calloc(1, sizeof(struct agent_table)); assert(retval != NULL); - memset(retval, 0, sizeof(struct agent_table)); TRACE_OUT(init_agent_table); return (retval); Modified: head/usr.sbin/nscd/cachelib.c ============================================================================== --- head/usr.sbin/nscd/cachelib.c Sat Oct 11 21:59:03 2008 (r183769) +++ head/usr.sbin/nscd/cachelib.c Sun Oct 12 00:44:27 2008 (r183770) @@ -479,18 +479,15 @@ init_cache(struct cache_params const *pa TRACE_IN(init_cache); assert(params != NULL); - retval = (struct cache_ *)malloc(sizeof(struct cache_)); + retval = (struct cache_ *)calloc(1, sizeof(struct cache_)); assert(retval != NULL); - memset(retval, 0, sizeof(struct cache_)); assert(params != NULL); memcpy(&retval->params, params, sizeof(struct cache_params)); - retval->entries = (struct cache_entry_ **)malloc( + retval->entries = (struct cache_entry_ **)calloc(1, sizeof(struct cache_entry_ *) * INITIAL_ENTRIES_CAPACITY); assert(retval->entries != NULL); - memset(retval->entries, 0, sizeof(sizeof(struct cache_entry_ *) - * INITIAL_ENTRIES_CAPACITY)); retval->entries_capacity = INITIAL_ENTRIES_CAPACITY; retval->entries_size = 0; @@ -541,12 +538,10 @@ register_cache_entry(struct cache_ *the_ new_capacity = the_cache->entries_capacity + ENTRIES_CAPACITY_STEP; - new_entries = (struct cache_entry_ **)malloc( + new_entries = (struct cache_entry_ **)calloc(1, sizeof(struct cache_entry_ *) * new_capacity); assert(new_entries != NULL); - memset(new_entries, 0, sizeof(struct cache_entry_ *) * - new_capacity); memcpy(new_entries, the_cache->entries, sizeof(struct cache_entry_ *) * the_cache->entries_size); @@ -559,21 +554,18 @@ register_cache_entry(struct cache_ *the_ switch (params->entry_type) { case CET_COMMON: - new_common_entry = (struct cache_common_entry_ *)malloc( + new_common_entry = (struct cache_common_entry_ *)calloc(1, sizeof(struct cache_common_entry_)); assert(new_common_entry != NULL); - memset(new_common_entry, 0, sizeof(struct cache_common_entry_)); memcpy(&new_common_entry->common_params, params, sizeof(struct common_cache_entry_params)); new_common_entry->params = (struct cache_entry_params *)&new_common_entry->common_params; - new_common_entry->common_params.entry_name = (char *)malloc( + new_common_entry->common_params.entry_name = (char *)calloc(1, entry_name_size+1); assert(new_common_entry->common_params.entry_name != NULL); - memset(new_common_entry->common_params.entry_name, 0, - entry_name_size + 1); strncpy(new_common_entry->common_params.entry_name, params->entry_name, entry_name_size); new_common_entry->name = @@ -588,11 +580,9 @@ register_cache_entry(struct cache_ *the_ else policies_size = 2; - new_common_entry->policies = (struct cache_policy_ **)malloc( + new_common_entry->policies = (struct cache_policy_ **)calloc(1, sizeof(struct cache_policy_ *) * policies_size); assert(new_common_entry->policies != NULL); - memset(new_common_entry->policies, 0, - sizeof(struct cache_policy_ *) * policies_size); new_common_entry->policies_size = policies_size; new_common_entry->policies[0] = init_cache_fifo_policy(); @@ -618,21 +608,18 @@ register_cache_entry(struct cache_ *the_ (struct cache_entry_ *)new_common_entry; break; case CET_MULTIPART: - new_mp_entry = (struct cache_mp_entry_ *)malloc( + new_mp_entry = (struct cache_mp_entry_ *)calloc(1, sizeof(struct cache_mp_entry_)); assert(new_mp_entry != NULL); - memset(new_mp_entry, 0, sizeof(struct cache_mp_entry_)); memcpy(&new_mp_entry->mp_params, params, sizeof(struct mp_cache_entry_params)); new_mp_entry->params = (struct cache_entry_params *)&new_mp_entry->mp_params; - new_mp_entry->mp_params.entry_name = (char *)malloc( + new_mp_entry->mp_params.entry_name = (char *)calloc(1, entry_name_size+1); assert(new_mp_entry->mp_params.entry_name != NULL); - memset(new_mp_entry->mp_params.entry_name, 0, - entry_name_size + 1); strncpy(new_mp_entry->mp_params.entry_name, params->entry_name, entry_name_size); new_mp_entry->name = new_mp_entry->mp_params.entry_name; @@ -925,10 +912,9 @@ open_cache_mp_write_session(struct cache return (NULL); } - retval = (struct cache_mp_write_session_ *)malloc( + retval = (struct cache_mp_write_session_ *)calloc(1, sizeof(struct cache_mp_write_session_)); assert(retval != NULL); - memset(retval, 0, sizeof(struct cache_mp_write_session_)); TAILQ_INIT(&retval->items); retval->parent_entry = mp_entry; @@ -961,10 +947,9 @@ cache_mp_write(struct cache_mp_write_ses return (-1); } - new_item = (struct cache_mp_data_item_ *)malloc( + new_item = (struct cache_mp_data_item_ *)calloc(1, sizeof(struct cache_mp_data_item_)); assert(new_item != NULL); - memset(new_item, 0, sizeof(struct cache_mp_data_item_)); new_item->value = (char *)malloc(data_size); assert(new_item->value != NULL); @@ -1065,10 +1050,9 @@ open_cache_mp_read_session(struct cache_ } } - retval = (struct cache_mp_read_session_ *)malloc( + retval = (struct cache_mp_read_session_ *)calloc(1, sizeof(struct cache_mp_read_session_)); assert(retval != NULL); - memset(retval, 0, sizeof(struct cache_mp_read_session_)); retval->parent_entry = mp_entry; retval->current_item = TAILQ_FIRST( Modified: head/usr.sbin/nscd/cacheplcs.c ============================================================================== --- head/usr.sbin/nscd/cacheplcs.c Sat Oct 11 21:59:03 2008 (r183769) +++ head/usr.sbin/nscd/cacheplcs.c Sun Oct 12 00:44:27 2008 (r183770) @@ -82,10 +82,9 @@ cache_queue_policy_create_item() struct cache_queue_policy_item_ *retval; TRACE_IN(cache_queue_policy_create_item); - retval = (struct cache_queue_policy_item_ *)malloc( + retval = (struct cache_queue_policy_item_ *)calloc(1, sizeof(struct cache_queue_policy_item_)); assert(retval != NULL); - memset(retval, 0, sizeof(struct cache_queue_policy_item_)); TRACE_OUT(cache_queue_policy_create_item); return ((struct cache_policy_item_ *)retval); @@ -193,10 +192,9 @@ init_cache_queue_policy(void) struct cache_queue_policy_ *retval; TRACE_IN(init_cache_queue_policy); - retval = (struct cache_queue_policy_ *)malloc( + retval = (struct cache_queue_policy_ *)calloc(1, sizeof(struct cache_queue_policy_)); assert(retval != NULL); - memset(retval, 0, sizeof(struct cache_queue_policy_)); retval->parent_data.create_item_func = cache_queue_policy_create_item; retval->parent_data.destroy_item_func = cache_queue_policy_destroy_item; @@ -334,10 +332,9 @@ cache_lfu_policy_create_item(void) struct cache_lfu_policy_item_ *retval; TRACE_IN(cache_lfu_policy_create_item); - retval = (struct cache_lfu_policy_item_ *)malloc( + retval = (struct cache_lfu_policy_item_ *)calloc(1, sizeof(struct cache_lfu_policy_item_)); assert(retval != NULL); - memset(retval, 0, sizeof(struct cache_lfu_policy_item_)); TRACE_OUT(cache_lfu_policy_create_item); return ((struct cache_policy_item_ *)retval); @@ -539,10 +536,9 @@ init_cache_lfu_policy() struct cache_lfu_policy_ *retval; TRACE_IN(init_cache_lfu_policy); - retval = (struct cache_lfu_policy_ *)malloc( + retval = (struct cache_lfu_policy_ *)calloc(1, sizeof(struct cache_lfu_policy_)); assert(retval != NULL); - memset(retval, 0, sizeof(struct cache_lfu_policy_)); retval->parent_data.create_item_func = cache_lfu_policy_create_item; retval->parent_data.destroy_item_func = cache_lfu_policy_destroy_item; Modified: head/usr.sbin/nscd/config.c ============================================================================== --- head/usr.sbin/nscd/config.c Sat Oct 11 21:59:03 2008 (r183769) +++ head/usr.sbin/nscd/config.c Sun Oct 12 00:44:27 2008 (r183770) @@ -119,10 +119,9 @@ create_configuration_entry(const char *n assert(negative_params != NULL); assert(mp_params != NULL); - retval = (struct configuration_entry *)malloc( + retval = (struct configuration_entry *)calloc(1, sizeof(struct configuration_entry)); assert(retval != NULL); - memset(retval, 0, sizeof(struct configuration_entry)); res = pthread_mutex_init(&retval->positive_cache_lock, NULL); if (res != 0) { @@ -162,9 +161,8 @@ create_configuration_entry(const char *n sizeof(struct mp_cache_entry_params)); size = strlen(name); - retval->name = (char *)malloc(size + 1); + retval->name = (char *)calloc(1, size + 1); assert(retval->name != NULL); - memset(retval->name, 0, size + 1); memcpy(retval->name, name, size); memcpy(&retval->common_query_timeout, common_timeout, @@ -268,12 +266,10 @@ add_configuration_entry(struct configura struct configuration_entry **new_entries; config->entries_capacity *= 2; - new_entries = (struct configuration_entry **)malloc( + new_entries = (struct configuration_entry **)calloc(1, sizeof(struct configuration_entry *) * config->entries_capacity); assert(new_entries != NULL); - memset(new_entries, 0, sizeof(struct configuration_entry *) * - config->entries_capacity); memcpy(new_entries, config->entries, sizeof(struct configuration_entry *) * config->entries_size); @@ -514,17 +510,14 @@ init_configuration(void) struct configuration *retval; TRACE_IN(init_configuration); - retval = (struct configuration *)malloc(sizeof(struct configuration)); + retval = (struct configuration *)calloc(1, sizeof(struct configuration)); assert(retval != NULL); - memset(retval, 0, sizeof(struct configuration)); retval->entries_capacity = INITIAL_ENTRIES_CAPACITY; - retval->entries = (struct configuration_entry **)malloc( + retval->entries = (struct configuration_entry **)calloc(1, sizeof(struct configuration_entry *) * retval->entries_capacity); assert(retval->entries != NULL); - memset(retval->entries, 0, sizeof(struct configuration_entry *) * - retval->entries_capacity); pthread_rwlock_init(&retval->rwlock, NULL); @@ -544,15 +537,13 @@ fill_configuration_defaults(struct confi free(config->socket_path); len = strlen(DEFAULT_SOCKET_PATH); - config->socket_path = (char *)malloc(len + 1); + config->socket_path = (char *)calloc(1, len + 1); assert(config->socket_path != NULL); - memset(config->socket_path, 0, len + 1); memcpy(config->socket_path, DEFAULT_SOCKET_PATH, len); len = strlen(DEFAULT_PIDFILE_PATH); - config->pidfile_path = (char *)malloc(len + 1); + config->pidfile_path = (char *)calloc(1, len + 1); assert(config->pidfile_path != NULL); - memset(config->pidfile_path, 0, len + 1); memcpy(config->pidfile_path, DEFAULT_PIDFILE_PATH, len); config->socket_mode = S_IFSOCK | S_IRUSR | S_IWUSR | Modified: head/usr.sbin/nscd/hashtable.h ============================================================================== --- head/usr.sbin/nscd/hashtable.h Sat Oct 11 21:59:03 2008 (r183769) +++ head/usr.sbin/nscd/hashtable.h Sun Oct 12 00:44:27 2008 (r183770) @@ -75,9 +75,7 @@ typedef int hashtable_index_t; #define HASHTABLE_INIT(table, type, field, _entries_size) \ do { \ hashtable_index_t var; \ - (table)->entries = (void *)malloc( \ - sizeof(*(table)->entries) * (_entries_size)); \ - memset((table)->entries, 0, \ + (table)->entries = (void *)calloc(1, \ sizeof(*(table)->entries) * (_entries_size)); \ (table)->entries_size = (_entries_size); \ for (var = 0; var < HASHTABLE_ENTRIES_COUNT(table); ++var) {\ Modified: head/usr.sbin/nscd/mp_rs_query.c ============================================================================== --- head/usr.sbin/nscd/mp_rs_query.c Sat Oct 11 21:59:03 2008 (r183769) +++ head/usr.sbin/nscd/mp_rs_query.c Sun Oct 12 00:44:27 2008 (r183770) @@ -115,11 +115,9 @@ on_mp_read_session_request_read1(struct return (-1); } - c_mp_rs_request->entry = (char *)malloc( + c_mp_rs_request->entry = (char *)calloc(1, c_mp_rs_request->entry_length + 1); assert(c_mp_rs_request->entry != NULL); - memset(c_mp_rs_request->entry, 0, - c_mp_rs_request->entry_length + 1); qstate->kevent_watermark = c_mp_rs_request->entry_length; qstate->process_func = on_mp_read_session_request_read2; Modified: head/usr.sbin/nscd/mp_ws_query.c ============================================================================== --- head/usr.sbin/nscd/mp_ws_query.c Sat Oct 11 21:59:03 2008 (r183769) +++ head/usr.sbin/nscd/mp_ws_query.c Sun Oct 12 00:44:27 2008 (r183770) @@ -121,11 +121,9 @@ on_mp_write_session_request_read1(struct return (-1); } - c_mp_ws_request->entry = (char *)malloc( + c_mp_ws_request->entry = (char *)calloc(1, c_mp_ws_request->entry_length + 1); assert(c_mp_ws_request->entry != NULL); - memset(c_mp_ws_request->entry, 0, - c_mp_ws_request->entry_length + 1); qstate->kevent_watermark = c_mp_ws_request->entry_length; qstate->process_func = on_mp_write_session_request_read2; @@ -376,9 +374,8 @@ on_mp_write_session_write_request_read1( return (-1); } - write_request->data = (char *)malloc(write_request->data_size); + write_request->data = (char *)calloc(1, write_request->data_size); assert(write_request->data != NULL); - memset(write_request->data, 0, write_request->data_size); qstate->kevent_watermark = write_request->data_size; qstate->process_func = on_mp_write_session_write_request_read2; Modified: head/usr.sbin/nscd/nscd.c ============================================================================== --- head/usr.sbin/nscd/nscd.c Sat Oct 11 21:59:03 2008 (r183769) +++ head/usr.sbin/nscd/nscd.c Sun Oct 12 00:44:27 2008 (r183770) @@ -163,9 +163,8 @@ init_runtime_env(struct configuration *c struct runtime_env *retval; TRACE_IN(init_runtime_env); - retval = (struct runtime_env *)malloc(sizeof(struct runtime_env)); + retval = (struct runtime_env *)calloc(1, sizeof(struct runtime_env)); assert(retval != NULL); - memset(retval, 0, sizeof(struct runtime_env)); retval->sockfd = socket(PF_LOCAL, SOCK_STREAM, 0); @@ -408,10 +407,9 @@ process_socket_event(struct kevent *even if (qstate->io_buffer != NULL) free(qstate->io_buffer); - qstate->io_buffer = (char *)malloc( + qstate->io_buffer = (char *)calloc(1, qstate->kevent_watermark); assert(qstate->io_buffer != NULL); - memset(qstate->io_buffer, 0, qstate->kevent_watermark); qstate->io_buffer_p = qstate->io_buffer; qstate->io_buffer_size = qstate->kevent_watermark; @@ -829,10 +827,8 @@ main(int argc, char *argv[]) } if (s_configuration->threads_num > 1) { - threads = (pthread_t *)malloc(sizeof(pthread_t) * + threads = (pthread_t *)calloc(1, sizeof(pthread_t) * s_configuration->threads_num); - memset(threads, 0, sizeof(pthread_t) * - s_configuration->threads_num); for (i = 0; i < s_configuration->threads_num; ++i) { thread_args = (struct processing_thread_args *)malloc( sizeof(struct processing_thread_args)); Modified: head/usr.sbin/nscd/nscdcli.c ============================================================================== --- head/usr.sbin/nscd/nscdcli.c Sat Oct 11 21:59:03 2008 (r183769) +++ head/usr.sbin/nscd/nscdcli.c Sun Oct 12 00:44:27 2008 (r183770) @@ -201,9 +201,8 @@ open_nscd_connection__(struct nscd_conne } fcntl(client_socket, F_SETFL, O_NONBLOCK); - retval = malloc(sizeof(struct nscd_connection_)); + retval = calloc(1, sizeof(struct nscd_connection_)); assert(retval != NULL); - memset(retval, 0, sizeof(struct nscd_connection_)); retval->sockfd = client_socket; Modified: head/usr.sbin/nscd/query.c ============================================================================== --- head/usr.sbin/nscd/query.c Sat Oct 11 21:59:03 2008 (r183769) +++ head/usr.sbin/nscd/query.c Sun Oct 12 00:44:27 2008 (r183770) @@ -332,27 +332,21 @@ on_write_request_read1(struct query_stat return (-1); } - write_request->entry = (char *)malloc( + write_request->entry = (char *)calloc(1, write_request->entry_length + 1); assert(write_request->entry != NULL); - memset(write_request->entry, 0, - write_request->entry_length + 1); - write_request->cache_key = (char *)malloc( + write_request->cache_key = (char *)calloc(1, write_request->cache_key_size + qstate->eid_str_length); assert(write_request->cache_key != NULL); memcpy(write_request->cache_key, qstate->eid_str, qstate->eid_str_length); - memset(write_request->cache_key + qstate->eid_str_length, 0, - write_request->cache_key_size); if (write_request->data_size != 0) { - write_request->data = (char *)malloc( + write_request->data = (char *)calloc(1, write_request->data_size); assert(write_request->data != NULL); - memset(write_request->data, 0, - write_request->data_size); } qstate->kevent_watermark = write_request->entry_length + @@ -611,19 +605,16 @@ on_read_request_read1(struct query_state return (-1); } - read_request->entry = (char *)malloc( + read_request->entry = (char *)calloc(1, read_request->entry_length + 1); assert(read_request->entry != NULL); - memset(read_request->entry, 0, read_request->entry_length + 1); - read_request->cache_key = (char *)malloc( + read_request->cache_key = (char *)calloc(1, read_request->cache_key_size + qstate->eid_str_length); assert(read_request->cache_key != NULL); memcpy(read_request->cache_key, qstate->eid_str, qstate->eid_str_length); - memset(read_request->cache_key + qstate->eid_str_length, 0, - read_request->cache_key_size); qstate->kevent_watermark = read_request->entry_length + read_request->cache_key_size; @@ -936,11 +927,9 @@ on_transform_request_read1(struct query_ return (-1); } - transform_request->entry = (char *)malloc( + transform_request->entry = (char *)calloc(1, transform_request->entry_length + 1); assert(transform_request->entry != NULL); - memset(transform_request->entry, 0, - transform_request->entry_length + 1); qstate->process_func = on_transform_request_read2; } else @@ -1228,9 +1217,8 @@ init_query_state(int sockfd, size_t keve struct query_state *retval; TRACE_IN(init_query_state); - retval = (struct query_state *)malloc(sizeof(struct query_state)); + retval = (struct query_state *)calloc(1, sizeof(struct query_state)); assert(retval != NULL); - memset(retval, 0, sizeof(struct query_state)); retval->sockfd = sockfd; retval->kevent_filter = EVFILT_READ;
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200810120044.m9C0iRUt031013>