From: Trond Norbye Date: Fri, 23 Jan 2009 09:41:32 +0000 (+0100) Subject: replace memcached_readline with memcached_io_readline X-Git-Tag: 0.26~5 X-Git-Url: https://git.m6w6.name/?a=commitdiff_plain;h=704c4c319482776ad5566e1e3ec69c31a651fb51;p=m6w6%2Flibmemcached replace memcached_readline with memcached_io_readline Moved the readline function into the io layer and use the buffer directly instead of calling memcached_io_read for each byte --- diff --git a/libmemcached/memcached_io.c b/libmemcached/memcached_io.c index 9a11556b..ffbd6806 100644 --- a/libmemcached/memcached_io.c +++ b/libmemcached/memcached_io.c @@ -409,3 +409,48 @@ memcached_return memcached_safe_read(memcached_server_st *ptr, return MEMCACHED_SUCCESS; } + +memcached_return memcached_io_readline(memcached_server_st *ptr, + char *buffer_ptr, + size_t size) +{ + bool line_complete= false; + int total_nr= 0; + + while (!line_complete) + { + if (ptr->read_buffer_length == 0) + { + /* + * We don't have any data in the buffer, so let's fill the read + * buffer. Call the standard read function to avoid duplicating + * the logic. + */ + if (memcached_io_read(ptr, buffer_ptr, 1) != 1) + return MEMCACHED_UNKNOWN_READ_FAILURE; + + if (*buffer_ptr == '\n') + line_complete= true; + + ++buffer_ptr; + ++total_nr; + } + + /* Now let's look in the buffer and copy as we go! */ + while (ptr->read_buffer_length && total_nr < size && !line_complete) + { + *buffer_ptr = *ptr->read_ptr; + if (*buffer_ptr == '\n') + line_complete = true; + --ptr->read_buffer_length; + ++ptr->read_ptr; + ++total_nr; + ++buffer_ptr; + } + + if (total_nr == size) + return MEMCACHED_PROTOCOL_ERROR; + } + + return MEMCACHED_SUCCESS; +} diff --git a/libmemcached/memcached_io.h b/libmemcached/memcached_io.h index 3da8af75..1bed7959 100644 --- a/libmemcached/memcached_io.h +++ b/libmemcached/memcached_io.h @@ -9,6 +9,10 @@ ssize_t memcached_io_write(memcached_server_st *ptr, void memcached_io_reset(memcached_server_st *ptr); ssize_t memcached_io_read(memcached_server_st *ptr, void *buffer, size_t length); +/* Read a line (terminated by '\n') into the buffer */ +memcached_return memcached_io_readline(memcached_server_st *ptr, + char *buffer_ptr, + size_t size); memcached_return memcached_io_close(memcached_server_st *ptr); /* Read n bytes of data from the server and store them in dta */ memcached_return memcached_safe_read(memcached_server_st *ptr, diff --git a/libmemcached/memcached_response.c b/libmemcached/memcached_response.c index 5c44e5f2..08e664f6 100644 --- a/libmemcached/memcached_response.c +++ b/libmemcached/memcached_response.c @@ -67,40 +67,11 @@ memcached_return memcached_response(memcached_server_st *ptr, return memcached_read_one_response(ptr, buffer, buffer_length, result); } -static memcached_return memcached_readline(memcached_server_st *ptr, - char *buffer, - size_t size) -{ - bool line_complete= false; - char *buffer_ptr= buffer; - int total_nr=0; - - while (!line_complete) - { - if (memcached_io_read(ptr, buffer_ptr, 1) != 1) - return MEMCACHED_UNKNOWN_READ_FAILURE; - - WATCHPOINT_ASSERT(*buffer_ptr != '\0'); - if (*buffer_ptr == '\n') - line_complete=true; - else - { - ++buffer_ptr; - ++total_nr; - } - - if (total_nr == size) - return MEMCACHED_PROTOCOL_ERROR; - } - - return MEMCACHED_SUCCESS; -} - static memcached_return textual_read_one_response(memcached_server_st *ptr, char *buffer, size_t buffer_length, memcached_result_st *result) { - memcached_return rc=memcached_readline(ptr, buffer, buffer_length); + memcached_return rc= memcached_io_readline(ptr, buffer, buffer_length); if (rc != MEMCACHED_SUCCESS) return rc;