~brianaker/libmemcached/1164440

« back to all changes in this revision

Viewing changes to src/generator.c

  • Committer: patg@patg.net
  • Date: 2008-05-05 03:32:10 UTC
  • mfrom: (317.1.71)
  • Revision ID: patg@patg.net-20080505033210-yxv0us02t8lb44dy
Merge 477:75823cad36b7 with -r 476

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
  if (!pairs)
27
 
    return;
28
 
 
29
 
  /* We free until we hit the null pair we stores during creation */
30
 
  for (x= 0; pairs[x].key; x++)
31
 
  {
32
 
    free(pairs[x].key);
33
 
    free(pairs[x].value);
34
 
  }
35
 
 
36
 
  free(pairs);
37
 
}
38
 
 
39
 
pairs_st *pairs_generate(unsigned long long number_of, size_t value_length)
40
 
{
41
 
  unsigned int x;
42
 
  pairs_st *pairs;
43
 
 
44
 
  pairs= (pairs_st*)malloc(sizeof(pairs_st) * (number_of+1));
45
 
 
46
 
  if (!pairs)
47
 
    goto error;
48
 
 
49
 
  memset(pairs, 0, sizeof(pairs_st) * (number_of+1));
50
 
 
51
 
  for (x= 0; x < number_of; x++)
52
 
  {
53
 
    pairs[x].key= (char *)malloc(sizeof(char) * 100);
54
 
    if (!pairs[x].key)
55
 
      goto error;
56
 
    get_random_string(pairs[x].key, 100);
57
 
    pairs[x].key_length= 100;
58
 
 
59
 
    pairs[x].value= (char *)malloc(sizeof(char) * value_length);
60
 
    if (!pairs[x].value)
61
 
      goto error;
62
 
    get_random_string(pairs[x].value, value_length);
63
 
    pairs[x].value_length= value_length;
64
 
  }
65
 
 
66
 
  return pairs;
67
 
error:
68
 
    fprintf(stderr, "Memory Allocation failure in pairs_generate.\n");
69
 
    exit(0);
70
 
}