2 +--------------------------------------------------------------------+
3 | libmemcached - C/C++ Client Library for memcached |
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted under the terms of the BSD license. |
7 | You should have received a copy of the license in a bundled file |
8 | named LICENSE; in case you did not receive a copy you can review |
9 | the terms online at: https://opensource.org/licenses/BSD-3-Clause |
10 +--------------------------------------------------------------------+
11 | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ |
12 | Copyright (c) 2020 Michael Wallner <mike@php.net> |
13 +--------------------------------------------------------------------+
16 #include "mem_config.h"
26 #include "generator.h"
30 /* Use this for string generation */
31 static const char ALPHANUMERICS
[] = "0123456789ABCDEFGHIJKLMNOPQRSTWXYZabcdefghijklmnopqrstuvwxyz";
33 #define ALPHANUMERICS_SIZE (sizeof(ALPHANUMERICS) - 1)
35 static size_t get_alpha_num(void) {
36 return (size_t) random() % ALPHANUMERICS_SIZE
;
39 void get_random_string(char *buffer
, size_t size
) {
40 char *buffer_ptr
= buffer
;
43 *buffer_ptr
++ = ALPHANUMERICS
[get_alpha_num()];
45 *buffer_ptr
++ = ALPHANUMERICS
[get_alpha_num()];
48 void pairs_free(pairs_st
*pairs
) {
53 /* We free until we hit the null pair we stores during creation */
54 for (uint32_t x
= 0; pairs
[x
].key
; x
++) {
64 pairs_st
*pairs_generate(uint64_t number_of
, size_t value_length
) {
65 pairs_st
*pairs
= (pairs_st
*) calloc((size_t) number_of
+ 1, sizeof(pairs_st
));
71 for (uint64_t x
= 0; x
< number_of
; x
++) {
72 pairs
[x
].key
= (char *) calloc(KEY_BYTES
, sizeof(char));
74 if (pairs
[x
].key
== NULL
)
77 get_random_string(pairs
[x
].key
, KEY_BYTES
);
78 pairs
[x
].key_length
= KEY_BYTES
;
81 pairs
[x
].value
= (char *) calloc(value_length
, sizeof(char));
83 if (pairs
[x
].value
== NULL
)
86 get_random_string(pairs
[x
].value
, value_length
);
87 pairs
[x
].value_length
= value_length
;
89 pairs
[x
].value
= NULL
;
90 pairs
[x
].value_length
= 0;
96 std::cerr
<< "Memory Allocation failure in pairs_generate." << std::endl
;