blob: df805153ce29327e51e93d73842fe0d66ba099b4 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
/* LibMemcached
* Copyright (C) 2011-2012 Data Differential, http://datadifferential.com/
* 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 <mem_config.h>
#include <stdint.h>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <unistd.h>
#include "clients/generator.h"
/* Use this for string generation */
static const char ALPHANUMERICS[]=
"0123456789ABCDEFGHIJKLMNOPQRSTWXYZabcdefghijklmnopqrstuvwxyz";
#define ALPHANUMERICS_SIZE (sizeof(ALPHANUMERICS)-1)
static size_t get_alpha_num(void)
{
return (size_t)random() % ALPHANUMERICS_SIZE;
}
void get_random_string(char *buffer, size_t size)
{
char *buffer_ptr= buffer;
while (--size)
{
*buffer_ptr++= ALPHANUMERICS[get_alpha_num()];
}
*buffer_ptr++= ALPHANUMERICS[get_alpha_num()];
}
void pairs_free(pairs_st *pairs)
{
if (pairs == NULL)
{
return;
}
/* We free until we hit the null pair we stores during creation */
for (uint32_t x= 0; pairs[x].key; x++)
{
free(pairs[x].key);
if (pairs[x].value)
{
free(pairs[x].value);
}
}
free(pairs);
}
pairs_st *pairs_generate(uint64_t number_of, size_t value_length)
{
pairs_st *pairs= (pairs_st*)calloc((size_t)number_of + 1, sizeof(pairs_st));
if (pairs == NULL)
{
goto error;
}
for (uint64_t x= 0; x < number_of; x++)
{
pairs[x].key= (char *)calloc(100, sizeof(char));
if (pairs[x].key == NULL)
goto error;
get_random_string(pairs[x].key, 100);
pairs[x].key_length= 100;
if (value_length)
{
pairs[x].value= (char *)calloc(value_length, sizeof(char));
if (pairs[x].value == NULL)
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;
error:
std::cerr << "Memory Allocation failure in pairs_generate." << std::endl;
exit(EXIT_SUCCESS);
}
|