Fix from Trond for deprecation
[m6w6/libmemcached] / clients / generator.c
1 #include "libmemcached/common.h"
2
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <stdint.h>
6 #include <string.h>
7
8 #include "generator.h"
9
10 /* Use this for string generation */
11 static const char ALPHANUMERICS[]=
12 "0123456789ABCDEFGHIJKLMNOPQRSTWXYZabcdefghijklmnopqrstuvwxyz";
13
14 #define ALPHANUMERICS_SIZE (sizeof(ALPHANUMERICS)-1)
15
16 static void get_random_string(char *buffer, size_t size)
17 {
18 char *buffer_ptr= buffer;
19
20 while (--size)
21 *buffer_ptr++= ALPHANUMERICS[random() % ALPHANUMERICS_SIZE];
22 *buffer_ptr++= ALPHANUMERICS[random() % ALPHANUMERICS_SIZE];
23 }
24
25 void pairs_free(pairs_st *pairs)
26 {
27 uint32_t x;
28
29 if (!pairs)
30 return;
31
32 /* We free until we hit the null pair we stores during creation */
33 for (x= 0; pairs[x].key; x++)
34 {
35 free(pairs[x].key);
36 free(pairs[x].value);
37 }
38
39 free(pairs);
40 }
41
42 pairs_st *pairs_generate(uint64_t number_of, size_t value_length)
43 {
44 unsigned int x;
45 pairs_st *pairs;
46
47 pairs= (pairs_st*)calloc(number_of + 1, sizeof(pairs_st));
48
49 if (!pairs)
50 goto error;
51
52 for (x= 0; x < number_of; x++)
53 {
54 pairs[x].key= (char *)calloc(100, sizeof(char));
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 *)calloc(value_length, sizeof(char));
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 }