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