c9750231104dcfd1552d5f79330fda0bdac075e8
[awesomized/libmemcached] / clients / generator.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <stdint.h>
4 #include <string.h>
5
6 #include "generator.h"
7
8 /* Use this for string generation */
9 static const char ALPHANUMERICS[]=
10 "0123456789ABCDEFGHIJKLMNOPQRSTWXYZabcdefghijklmnopqrstuvwxyz";
11
12 #define ALPHANUMERICS_SIZE (sizeof(ALPHANUMERICS)-1)
13
14 static void get_random_string(char *buffer, size_t size)
15 {
16 char *buffer_ptr= buffer;
17
18 while (--size)
19 *buffer_ptr++= ALPHANUMERICS[random() % ALPHANUMERICS_SIZE];
20 *buffer_ptr++= ALPHANUMERICS[random() % ALPHANUMERICS_SIZE];
21 }
22
23 void pairs_free(pairs_st *pairs)
24 {
25 uint32_t x;
26
27 if (!pairs)
28 return;
29
30 /* We free until we hit the null pair we stores during creation */
31 for (x= 0; pairs[x].key; x++)
32 {
33 free(pairs[x].key);
34 free(pairs[x].value);
35 }
36
37 free(pairs);
38 }
39
40 pairs_st *pairs_generate(uint64_t number_of, size_t value_length)
41 {
42 unsigned int x;
43 pairs_st *pairs;
44
45 pairs= (pairs_st*)malloc(sizeof(pairs_st) * (number_of+1));
46
47 if (!pairs)
48 goto error;
49
50 memset(pairs, 0, sizeof(pairs_st) * (number_of+1));
51
52 for (x= 0; x < number_of; x++)
53 {
54 pairs[x].key= (char *)malloc(sizeof(char) * 100);
55 if (!pairs[x].key)
56 goto error;
57 get_random_string(pairs[x].key, 100);
58 pairs[x].key_length= 100;
59
60 pairs[x].value= (char *)malloc(sizeof(char) * value_length);
61 if (!pairs[x].value)
62 goto error;
63 get_random_string(pairs[x].value, value_length);
64 pairs[x].value_length= value_length;
65 }
66
67 return pairs;
68 error:
69 fprintf(stderr, "Memory Allocation failure in pairs_generate.\n");
70 exit(0);
71 }