X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=clients%2Fgenerator.c;h=80b398b251df3a73b0f476f0439a91553f8951b1;hb=3ec0260542c2f66cf2668323e44fa3b55b97138b;hp=6a3b5103f084412e5b9d9dac077afa28c4f24a48;hpb=6a0e41c00d8ef3e8814e3b82257694a5692b95c7;p=awesomized%2Flibmemcached diff --git a/clients/generator.c b/clients/generator.c index 6a3b5103..80b398b2 100644 --- a/clients/generator.c +++ b/clients/generator.c @@ -1,5 +1,19 @@ +/* LibMemcached + * Copyright (C) 2006-2009 Brian Aker + * All rights reserved. + * + * Use and distribution licensed under the BSD license. See + * the COPYING file in the parent directory for full text. + * + * Summary: + * + */ + +#include "config.h" + #include #include +#include #include #include "generator.h" @@ -10,27 +24,33 @@ static const char ALPHANUMERICS[]= #define ALPHANUMERICS_SIZE (sizeof(ALPHANUMERICS)-1) +static size_t get_alpha_num(void) +{ + return (size_t)random() % ALPHANUMERICS_SIZE; +} + static void get_random_string(char *buffer, size_t size) { char *buffer_ptr= buffer; while (--size) - *buffer_ptr++= ALPHANUMERICS[random() % ALPHANUMERICS_SIZE]; - *buffer_ptr++= ALPHANUMERICS[random() % ALPHANUMERICS_SIZE]; + *buffer_ptr++= ALPHANUMERICS[get_alpha_num()]; + *buffer_ptr++= ALPHANUMERICS[get_alpha_num()]; } void pairs_free(pairs_st *pairs) { uint32_t x; - if (!pairs) + if (! pairs) return; /* We free until we hit the null pair we stores during creation */ for (x= 0; pairs[x].key; x++) { free(pairs[x].key); - free(pairs[x].value); + if (pairs[x].value) + free(pairs[x].value); } free(pairs); @@ -41,26 +61,32 @@ pairs_st *pairs_generate(uint64_t number_of, size_t value_length) unsigned int x; pairs_st *pairs; - pairs= (pairs_st*)malloc(sizeof(pairs_st) * (number_of+1)); + pairs= (pairs_st*)calloc((size_t)number_of + 1, sizeof(pairs_st)); if (!pairs) goto error; - memset(pairs, 0, sizeof(pairs_st) * (number_of+1)); - for (x= 0; x < number_of; x++) { - pairs[x].key= (char *)malloc(sizeof(char) * 100); + pairs[x].key= (char *)calloc(100, sizeof(char)); if (!pairs[x].key) goto error; get_random_string(pairs[x].key, 100); pairs[x].key_length= 100; - pairs[x].value= (char *)malloc(sizeof(char) * value_length); - if (!pairs[x].value) - goto error; - get_random_string(pairs[x].value, value_length); - pairs[x].value_length= value_length; + if (value_length) + { + pairs[x].value= (char *)calloc(value_length, sizeof(char)); + if (!pairs[x].value) + goto error; + get_random_string(pairs[x].value, value_length); + pairs[x].value_length= value_length; + } + else + { + pairs[x].value= NULL; + pairs[x].value_length= 0; + } } return pairs;