From: Brian Aker Date: Wed, 14 Nov 2007 07:46:52 +0000 (-0800) Subject: Removed more then a handfull of memset() calls. X-Git-Tag: 0.10~12 X-Git-Url: https://git.m6w6.name/?a=commitdiff_plain;h=46f12a5edfa0fc0382a9d25355b15d30f857b138;p=awesomized%2Flibmemcached Removed more then a handfull of memset() calls. Also reworked multiget to not need to call malloc(). --- diff --git a/include/memcached.h b/include/memcached.h index 29195de0..7cb057d2 100644 --- a/include/memcached.h +++ b/include/memcached.h @@ -105,6 +105,7 @@ struct memcached_server_st { unsigned int cursor_active; char write_buffer[MEMCACHED_MAX_BUFFER]; size_t write_buffer_offset; + char *write_ptr; char read_buffer[MEMCACHED_MAX_BUFFER]; size_t read_buffer_length; char *read_ptr; diff --git a/lib/memcached_auto.c b/lib/memcached_auto.c index 61ad9125..f2ed0b8f 100644 --- a/lib/memcached_auto.c +++ b/lib/memcached_auto.c @@ -30,8 +30,6 @@ static memcached_return memcached_auto(memcached_st *ptr, if (rc != MEMCACHED_SUCCESS) return rc; - memset(buffer, 0, MEMCACHED_DEFAULT_COMMAND_SIZE); - rc= memcached_response(ptr, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, server_key); /* diff --git a/lib/memcached_flush.c b/lib/memcached_flush.c index f40c1a6b..af2303ca 100644 --- a/lib/memcached_flush.c +++ b/lib/memcached_flush.c @@ -16,7 +16,6 @@ memcached_return memcached_flush(memcached_st *ptr, time_t expiration) if (rc != MEMCACHED_SUCCESS) rc= MEMCACHED_SOME_ERRORS; - memset(buffer, 0, MEMCACHED_DEFAULT_COMMAND_SIZE); for (x= 0; x < ptr->number_of_hosts; x++) { if (expiration) diff --git a/lib/memcached_get.c b/lib/memcached_get.c index cbe94926..db626f6f 100644 --- a/lib/memcached_get.c +++ b/lib/memcached_get.c @@ -12,7 +12,6 @@ static memcached_return memcached_value_fetch(memcached_st *ptr, char *key, size char *string_ptr; char *end_ptr; - memset(buffer, 0, MEMCACHED_DEFAULT_COMMAND_SIZE); end_ptr= buffer + MEMCACHED_DEFAULT_COMMAND_SIZE; *flags= 0; @@ -32,7 +31,6 @@ static memcached_return memcached_value_fetch(memcached_st *ptr, char *key, size /* We load the key */ if (load_key) { - memset(key, 0, MEMCACHED_MAX_KEY); *key_length= 0; for (; end_ptr > string_ptr && *string_ptr != ' '; string_ptr++) @@ -216,7 +214,6 @@ memcached_return memcached_mget(memcached_st *ptr, { unsigned int x; memcached_return rc= MEMCACHED_NOTFOUND; - char *cursor_key_exec; LIBMEMCACHED_MEMCACHED_MGET_START(); ptr->cursor_server= 0; @@ -226,41 +223,40 @@ memcached_return memcached_mget(memcached_st *ptr, if (ptr->number_of_hosts == 0) return MEMCACHED_NO_SERVERS; - cursor_key_exec= (char *)malloc(sizeof(char) * ptr->number_of_hosts); - memset(cursor_key_exec, 0, sizeof(char) * ptr->number_of_hosts); - for (x= 0; x < number_of_keys; x++) { unsigned int server_key; server_key= memcached_generate_hash(ptr, keys[x], key_length[x]); - if (cursor_key_exec[server_key] == 0) + if (ptr->hosts[server_key].cursor_active == 0) { rc= memcached_connect(ptr, server_key); if ((memcached_io_write(ptr, server_key, "get ", 4, 0)) == -1) { - memcached_quit(ptr); + memcached_quit_server(ptr, server_key); rc= MEMCACHED_SOME_ERRORS; - break; + continue; } + ptr->hosts[server_key].cursor_active= 1; } if ((memcached_io_write(ptr, server_key, keys[x], key_length[x], 0)) == -1) { - memcached_quit(ptr); + ptr->hosts[server_key].cursor_active = 0; + memcached_quit_server(ptr, server_key); rc= MEMCACHED_SOME_ERRORS; - break; + continue; } if ((memcached_io_write(ptr, server_key, " ", 1, 0)) == -1) { - memcached_quit(ptr); + ptr->hosts[server_key].cursor_active = 0; + memcached_quit_server(ptr, server_key); rc= MEMCACHED_SOME_ERRORS; - break; + continue; } - cursor_key_exec[server_key]= 1; } @@ -269,24 +265,17 @@ memcached_return memcached_mget(memcached_st *ptr, */ for (x= 0; x < ptr->number_of_hosts; x++) { - if (cursor_key_exec[x]) + if (ptr->hosts[x].cursor_active == 1) { /* We need to doo something about non-connnected hosts in the future */ if ((memcached_io_write(ptr, x, "\r\n", 2, 1)) == -1) { - memcached_quit(ptr); + memcached_quit_server(ptr, x); rc= MEMCACHED_SOME_ERRORS; - break; } - - ptr->hosts[x].cursor_active= 1; } - else - ptr->hosts[x].cursor_active= 0; } - free(cursor_key_exec); - LIBMEMCACHED_MEMCACHED_MGET_END(); return rc; } @@ -312,18 +301,28 @@ char *memcached_fetch(memcached_st *ptr, char *key, size_t *key_length, *value_length= memcached_string_length(result_buffer); if (*error == MEMCACHED_NOTFOUND) + { + ptr->hosts[ptr->cursor_server].cursor_active = 0; ptr->cursor_server++; + } else if (*error == MEMCACHED_END && *value_length == 0) + { return NULL; + } else if (*error == MEMCACHED_END) { WATCHPOINT_ASSERT(0); /* If this happens we have somehow messed up the fetch */ + *value_length= 0; return NULL; } else if (*error != MEMCACHED_SUCCESS) + { return NULL; + } else + { return memcached_string_c_copy(result_buffer); + } } @@ -352,18 +351,27 @@ memcached_result_st *memcached_fetch_result(memcached_st *ptr, 1, ptr->cursor_server); if (*error == MEMCACHED_NOTFOUND) + { + ptr->hosts[ptr->cursor_server].cursor_active = 0; ptr->cursor_server++; + } else if (*error == MEMCACHED_END && memcached_string_length((memcached_string_st *)(&result->value)) == 0) + { return NULL; + } else if (*error == MEMCACHED_END) { WATCHPOINT_ASSERT(0); /* If this happens we have somehow messed up the fetch */ return NULL; } else if (*error != MEMCACHED_SUCCESS) + { return NULL; + } else + { return result; + } } diff --git a/lib/memcached_hosts.c b/lib/memcached_hosts.c index f16052b3..d5ec5606 100644 --- a/lib/memcached_hosts.c +++ b/lib/memcached_hosts.c @@ -15,6 +15,7 @@ static void host_reset(memcached_server_st *host, char *new_hostname, unsigned i host->fd= -1; host->type= type; host->read_ptr= host->read_buffer; + host->write_ptr= host->write_buffer; } memcached_return memcached_server_push(memcached_st *ptr, memcached_server_st *list) @@ -123,7 +124,6 @@ static memcached_return server_add(memcached_st *ptr, char *hostname, if (!new_hostname) return MEMCACHED_MEMORY_ALLOCATION_FAILURE; - memset(new_hostname, 0, strlen(hostname)+1); memcpy(new_hostname, hostname, strlen(hostname)); host_reset(&ptr->hosts[ptr->number_of_hosts], new_hostname, port, type); ptr->number_of_hosts++; diff --git a/lib/memcached_io.c b/lib/memcached_io.c index c0215ac0..06ac2e9b 100644 --- a/lib/memcached_io.c +++ b/lib/memcached_io.c @@ -93,7 +93,11 @@ ssize_t memcached_io_write(memcached_st *ptr, unsigned int server_key, for (x= 0; x < length; x++) { - ptr->hosts[server_key].write_buffer[ptr->hosts[server_key].write_buffer_offset]= buffer[x]; + if (ptr->hosts[server_key].write_ptr == 0) + ptr->hosts[server_key].write_ptr= ptr->hosts[server_key].write_buffer; + WATCHPOINT_ASSERT(ptr->hosts[server_key].write_ptr); + *ptr->hosts[server_key].write_ptr= buffer[x]; + ptr->hosts[server_key].write_ptr++; ptr->hosts[server_key].write_buffer_offset++; if (ptr->hosts[server_key].write_buffer_offset == MEMCACHED_MAX_BUFFER) @@ -103,6 +107,7 @@ ssize_t memcached_io_write(memcached_st *ptr, unsigned int server_key, sent_length= memcached_io_flush(ptr, server_key); WATCHPOINT_ASSERT(sent_length == MEMCACHED_MAX_BUFFER); + ptr->hosts[server_key].write_ptr= ptr->hosts[server_key].write_buffer; ptr->hosts[server_key].write_buffer_offset= 0; } } @@ -142,14 +147,10 @@ ssize_t memcached_io_flush(memcached_st *ptr, unsigned int server_key) sent_length= 0; if (ptr->hosts[server_key].type == MEMCACHED_CONNECTION_UDP) { - - sent_length= sendto(ptr->hosts[server_key].fd, write_ptr, write_length, - 0, 0, 0); - /* - rc = sendto(sd, argv[i], strlen(argv[i])+1, 0, - (struct sockaddr *) &remoteServAddr, - sizeof(remoteServAddr)); - */ + sent_length= sendto(ptr->hosts[server_key].fd, + write_ptr, write_length, 0, + (struct sockaddr *)&ptr->hosts[server_key].servAddr, + sizeof(struct sockaddr)); } else { @@ -182,6 +183,7 @@ ssize_t memcached_io_flush(memcached_st *ptr, unsigned int server_key) WATCHPOINT_ASSERT(write_length == 0); WATCHPOINT_ASSERT(return_length == ptr->hosts[server_key].write_buffer_offset); + ptr->hosts[server_key].write_ptr= ptr->hosts[server_key].write_buffer; ptr->hosts[server_key].write_buffer_offset= 0; return return_length; @@ -192,6 +194,7 @@ ssize_t memcached_io_flush(memcached_st *ptr, unsigned int server_key) */ void memcached_io_reset(memcached_st *ptr, unsigned int server_key) { + ptr->hosts[server_key].write_ptr= ptr->hosts[server_key].write_buffer; ptr->hosts[server_key].write_buffer_offset= 0; memcached_quit(ptr); } diff --git a/lib/memcached_parse.c b/lib/memcached_parse.c index 069593e5..52154215 100644 --- a/lib/memcached_parse.c +++ b/lib/memcached_parse.c @@ -29,16 +29,17 @@ memcached_server_st *memcached_servers_parse(char *server_strings) char *ptr; port= 0; - memset(buffer, 0, HUGE_STRING_LEN); if (string) { memcpy(buffer, begin_ptr, string - begin_ptr); + buffer[(unsigned int)(string - begin_ptr)]= 0; begin_ptr= string+1; } else { size_t length= strlen(begin_ptr); memcpy(buffer, begin_ptr, length); + buffer[length]= 0; begin_ptr= end_ptr; } diff --git a/lib/memcached_quit.c b/lib/memcached_quit.c index e51692fc..a0cc6172 100644 --- a/lib/memcached_quit.c +++ b/lib/memcached_quit.c @@ -30,6 +30,7 @@ void memcached_quit_server(memcached_st *ptr, unsigned int server_key) ptr->hosts[server_key].write_buffer_offset= 0; ptr->hosts[server_key].read_buffer_length= 0; ptr->hosts[server_key].read_ptr= ptr->hosts[server_key].read_buffer; + ptr->hosts[server_key].write_ptr= ptr->hosts[server_key].write_buffer; } ptr->connected--; diff --git a/lib/memcached_response.c b/lib/memcached_response.c index 19fa6400..b90d861d 100644 --- a/lib/memcached_response.c +++ b/lib/memcached_response.c @@ -18,7 +18,6 @@ memcached_return memcached_response(memcached_st *ptr, unsigned int max_messages; - memset(buffer, 0, buffer_length); send_length= 0; max_messages= memcached_server_response_count(ptr, server_key); diff --git a/lib/memcached_stats.c b/lib/memcached_stats.c index cbfdef35..a2dc17a3 100644 --- a/lib/memcached_stats.c +++ b/lib/memcached_stats.c @@ -152,9 +152,6 @@ char *memcached_stat_get_value(memcached_st *ptr, memcached_stat_st *stat, char buffer[SMALL_STRING_LEN]; *error= MEMCACHED_SUCCESS; - - memset(buffer, 0, SMALL_STRING_LEN); - if (!memcmp("pid", key, strlen("pid"))) snprintf(buffer, SMALL_STRING_LEN,"%u", stat->pid); else if (!memcmp("uptime", key, strlen("uptime"))) diff --git a/lib/memcached_string.c b/lib/memcached_string.c index 2bbda3fa..42f3c326 100644 --- a/lib/memcached_string.c +++ b/lib/memcached_string.c @@ -27,10 +27,6 @@ memcached_return memcached_string_check(memcached_string_st *string, size_t need string->end= string->string + current_offset; string->current_size+= (string->block_size * adjust); - - /* We zero the block structure we just realloced */ - memset((string->string + current_offset), 0, - sizeof(char) * string->block_size); } return MEMCACHED_SUCCESS;