From a375dd4beaabf4a8263ec0eaf6a686661e58a2b0 Mon Sep 17 00:00:00 2001 From: Brian Aker Date: Fri, 25 Mar 2011 21:48:33 -0700 Subject: [PATCH] Merge in additional changes so that we remove a usage of std::string in the core. --- libmemcached/callback.c | 28 ++------------- libmemcached/common.h | 6 ++-- libmemcached/constants.h | 8 +++-- libmemcached/include.am | 4 ++- libmemcached/options.cc | 19 ++++++---- libmemcached/options/parser.yy | 8 ++--- libmemcached/options/scanner.l | 3 -- libmemcached/prefix_key.cc | 63 ++++++++++++++++++++++++++++++++++ libmemcached/prefix_key.h | 49 ++++++++++++++++++++++++++ tests/mem_functions.c | 2 +- 10 files changed, 141 insertions(+), 49 deletions(-) create mode 100644 libmemcached/prefix_key.cc create mode 100644 libmemcached/prefix_key.h diff --git a/libmemcached/callback.c b/libmemcached/callback.c index 6d5285a5..dcb3ddeb 100644 --- a/libmemcached/callback.c +++ b/libmemcached/callback.c @@ -9,7 +9,7 @@ * */ -#include "common.h" +#include "libmemcached/common.h" #include /* @@ -24,31 +24,7 @@ memcached_return_t memcached_callback_set(memcached_st *ptr, { case MEMCACHED_CALLBACK_PREFIX_KEY: { - char *key= (char *)data; - - if (key) - { - size_t key_length= strlen(key); - - if (memcached_key_test((const char **)&key, &key_length, 1) == MEMCACHED_BAD_KEY_PROVIDED) - return memcached_set_error(ptr, MEMCACHED_BAD_KEY_PROVIDED, NULL); - - if ((key_length > MEMCACHED_PREFIX_KEY_MAX_SIZE -1)) - return memcached_set_error(ptr, MEMCACHED_KEY_TOO_BIG, NULL); - - memcached_array_free(ptr->prefix_key); - ptr->prefix_key= memcached_strcpy(ptr, (const char *)data, strlen((const char*)data)); - - if (! ptr->prefix_key) - return memcached_set_error(ptr, MEMCACHED_MEMORY_ALLOCATION_FAILURE, NULL); - } - else - { - memcached_array_free(ptr->prefix_key); - ptr->prefix_key= NULL; - } - - break; + return memcached_set_prefix_key(ptr, (char*)data, data ? strlen((char*)data) : 0); } case MEMCACHED_CALLBACK_USER_DATA: { diff --git a/libmemcached/common.h b/libmemcached/common.h index e6f845ea..c4cd0e53 100644 --- a/libmemcached/common.h +++ b/libmemcached/common.h @@ -13,8 +13,7 @@ Common include file for libmemached */ -#ifndef __LIBMEMCACHED_COMMON_H__ -#define __LIBMEMCACHED_COMMON_H__ +#pragma once #include @@ -71,6 +70,7 @@ memcached_return_t memcached_server_execute(memcached_st *ptr, #include "libmemcached/memcached/protocol_binary.h" #include "libmemcached/byteorder.h" #include "libmemcached/response.h" +#include "libmemcached/prefix_key.h" /* string value */ struct memcached_continuum_item_st @@ -217,5 +217,3 @@ static inline void *libmemcached_calloc(const memcached_st *ptr, size_t nelem, s #ifdef __cplusplus } #endif - -#endif /* __LIBMEMCACHED_COMMON_H__ */ diff --git a/libmemcached/constants.h b/libmemcached/constants.h index e01623c8..ed588875 100644 --- a/libmemcached/constants.h +++ b/libmemcached/constants.h @@ -30,7 +30,7 @@ #define MEMCACHED_VERSION_STRING_LENGTH 24 -typedef enum { +enum memcached_return_t { MEMCACHED_SUCCESS, MEMCACHED_FAILURE, MEMCACHED_HOST_LOOKUP_FAILURE, @@ -77,7 +77,11 @@ typedef enum { MEMCACHED_PARSE_ERROR, MEMCACHED_PARSE_USER_ERROR, MEMCACHED_MAXIMUM_RETURN /* Always add new error code before */ -} memcached_return_t; +}; + +#ifndef __cplusplus +typedef enum memcached_return_t memcached_return_t; +#endif typedef enum { diff --git a/libmemcached/include.am b/libmemcached/include.am index b0d07319..c290989e 100644 --- a/libmemcached/include.am +++ b/libmemcached/include.am @@ -47,11 +47,12 @@ nobase_include_HEADERS+= \ libmemcached/memcached/protocol_binary.h \ libmemcached/options.h \ libmemcached/parse.h \ - libmemcached/platform.h \ + libmemcached/prefix_key.h \ libmemcached/protocol/cache.h \ libmemcached/protocol/callback.h \ libmemcached/protocol_handler.h \ libmemcached/quit.h \ + libmemcached/platform.h \ libmemcached/result.h \ libmemcached/sasl.h \ libmemcached/server.h \ @@ -114,6 +115,7 @@ libmemcached_libmemcached_la_SOURCES+= \ libmemcached/memcached.c \ libmemcached/options.cc \ libmemcached/parse.c \ + libmemcached/prefix_key.cc \ libmemcached/purge.c \ libmemcached/quit.c \ libmemcached/response.c \ diff --git a/libmemcached/options.cc b/libmemcached/options.cc index 02b885fc..b404652e 100644 --- a/libmemcached/options.cc +++ b/libmemcached/options.cc @@ -35,7 +35,7 @@ * */ -#include "common.h" +#include "libmemcached/common.h" #include @@ -53,15 +53,20 @@ size_t memcached_parse_filename_length(memcached_st *memc) static memcached_return_t _parse_file_options(memcached_st *self, memcached_string_t *filename) { - std::string real_name(filename->c_str, filename->size); - FILE *fp= fopen(real_name.c_str(), "r"); + memcached_array_st *real_name= memcached_strcpy(self, filename->c_str, filename->size); + + if (not real_name) + return MEMCACHED_MEMORY_ALLOCATION_FAILURE; + + FILE *fp= fopen(memcached_array_string(real_name), "r"); if (! fp) { - memcached_string_t tmp; - tmp.c_str= real_name.c_str(); - tmp.size= real_name.size(); - return memcached_set_errno(self, errno, &tmp); + memcached_string_t error_message= memcached_array_to_string(real_name); + memcached_return_t rc= memcached_set_errno(self, errno, &error_message); + memcached_array_free(real_name); + return rc; } + memcached_array_free(real_name); char buffer[BUFSIZ]; memcached_return_t rc= MEMCACHED_INVALID_ARGUMENTS; diff --git a/libmemcached/options/parser.yy b/libmemcached/options/parser.yy index a8551205..adf3dc01 100644 --- a/libmemcached/options/parser.yy +++ b/libmemcached/options/parser.yy @@ -28,7 +28,6 @@ %name-prefix="libmemcached_" %parse-param { Context *context } %parse-param { yyscan_t *scanner } -%locations %pure-parser %require "2.2" %start begin @@ -38,8 +37,7 @@ #include -#include -#include +#include #include #include @@ -48,7 +46,7 @@ #pragma GCC diagnostic ignored "-Wold-style-cast" #include -int libmemcached_lex(YYSTYPE* lvalp, YYLTYPE* llocp, void* scanner); +int libmemcached_lex(YYSTYPE* lvalp, void* scanner); #define parser_abort(A, B) do { parser_abort_func((A), (B)); YYABORT; } while (0) @@ -75,7 +73,7 @@ inline void parser_abort_func(Context *context, const char *error) memcached_set_error_string(context->memc, context->rc, error_message.c_str(), error_message.size()); } -inline void libmemcached_error(YYLTYPE *locp, Context *context, yyscan_t *scanner, const char *error) +inline void libmemcached_error(Context *context, yyscan_t *scanner, const char *error) { if (not context->end()) parser_abort_func(context, error); diff --git a/libmemcached/options/scanner.l b/libmemcached/options/scanner.l index 7949e93d..b653c460 100644 --- a/libmemcached/options/scanner.l +++ b/libmemcached/options/scanner.l @@ -30,7 +30,6 @@ #include #define YY_EXTRA_TYPE Context* -#define YY_USER_ACTION yylloc->first_line = yylineno; } @@ -62,12 +61,10 @@ static void get_lex_chars(char* buffer, int& result, int max_size, Context *cont %} %option bison-bridge -%option bison-locations %option case-insensitive %option debug %option nounput %option noyywrap -%option yylineno %option outfile="libmemcached/options/scanner.cc" header-file="libmemcached/options/scanner.h" %option perf-report %option prefix="libmemcached_" diff --git a/libmemcached/prefix_key.cc b/libmemcached/prefix_key.cc new file mode 100644 index 00000000..526f5fac --- /dev/null +++ b/libmemcached/prefix_key.cc @@ -0,0 +1,63 @@ +/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: + * + * Libmemcached library + * + * Copyright (C) 2011 Data Differential, http://datadifferential.com/ + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * + * * The names of its contributors may not be used to endorse or + * promote products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include + +memcached_return_t memcached_set_prefix_key(memcached_st *self, const char *key, size_t key_length) +{ + if (key && key_length) + { + if (memcached_key_test((const char **)&key, &key_length, 1) == MEMCACHED_BAD_KEY_PROVIDED) + return memcached_set_error(self, MEMCACHED_BAD_KEY_PROVIDED, NULL); + + if ((key_length > MEMCACHED_PREFIX_KEY_MAX_SIZE -1)) + return memcached_set_error(self, MEMCACHED_KEY_TOO_BIG, NULL); + + memcached_array_free(self->prefix_key); + self->prefix_key= memcached_strcpy(self, key, key_length); + + if (! self->prefix_key) + return memcached_set_error(self, MEMCACHED_MEMORY_ALLOCATION_FAILURE, NULL); + } + else + { + memcached_array_free(self->prefix_key); + self->prefix_key= NULL; + } + + return MEMCACHED_SUCCESS; +} diff --git a/libmemcached/prefix_key.h b/libmemcached/prefix_key.h new file mode 100644 index 00000000..bd81160f --- /dev/null +++ b/libmemcached/prefix_key.h @@ -0,0 +1,49 @@ +/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: + * + * Libmemcached library + * + * Copyright (C) 2011 Data Differential, http://datadifferential.com/ + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * + * * The names of its contributors may not be used to endorse or + * promote products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#pragma once + +#ifdef __cplusplus +extern "C" { +#endif + +LIBMEMCACHED_LOCAL + memcached_return_t memcached_set_prefix_key(memcached_st *self, const char *str, size_t length); + +#ifdef __cplusplus +} +#endif diff --git a/tests/mem_functions.c b/tests/mem_functions.c index a69d35ac..c3f5c30e 100644 --- a/tests/mem_functions.c +++ b/tests/mem_functions.c @@ -845,7 +845,7 @@ static test_return_t bad_key_test(memcached_st *memc) rc= memcached_callback_set(memc_clone, MEMCACHED_CALLBACK_PREFIX_KEY, NULL); test_true(rc == MEMCACHED_SUCCESS); - char *longkey= malloc(max_keylen + 1); + char *longkey= (char *)malloc(max_keylen + 1); if (longkey != NULL) { memset(longkey, 'a', max_keylen + 1); -- 2.30.2