{
ptr= (memcached_st *)calloc(1, sizeof(memcached_st));
- if (!ptr)
+ if (! ptr)
+ {
return NULL; /* MEMCACHED_MEMORY_ALLOCATION_FAILURE */
+ }
- ptr->is_allocated= true;
+ ptr->options.is_allocated= true;
}
else
{
memset(ptr, 0, sizeof(memcached_st));
}
+ ptr->options.is_initialized= true;
+
memcached_set_memory_allocators(ptr, NULL, NULL, NULL, NULL);
result_ptr= memcached_result_create(ptr, &ptr->result);
ptr->io_msg_watermark= 500;
ptr->io_bytes_watermark= 65 * 1024;
+ WATCHPOINT_ASSERT_INITIALIZED(&ptr->result);
+ WATCHPOINT_ASSERT_INITIALIZED(&ptr->hashkit);
+
return ptr;
}
if (ptr->continuum)
ptr->call_free(ptr, ptr->continuum);
- if (ptr->is_allocated)
+ if (memcached_is_allocated(ptr))
+ {
ptr->call_free(ptr, ptr);
+ }
else
- memset(ptr, 0, sizeof(memcached_st));
+ {
+ ptr->options.is_initialized= false;
+ }
}
/*
if (source == NULL)
return memcached_create(clone);
- if (clone && clone->is_allocated)
+ if (clone && memcached_is_allocated(clone))
{
return NULL;
}
};
struct memcached_st {
- uint8_t purging;
- bool is_allocated;
+ struct {
+ bool is_allocated:1;
+ bool is_initialized:1;
+ bool is_purging:1;
+ } options;
uint8_t distribution;
uint8_t hash;
uint32_t continuum_points_counter;
LIBMEMCACHED_LOCAL
memcached_return run_distribution(memcached_st *ptr);
+
+#define memcached_is_allocated(__object) ((__object)->options.is_allocated)
+#define memcached_is_initialized(__object) ((__object)->options.is_initialized)
+
#ifdef __cplusplus
}
#endif
}
/* We have completed reading data */
- if (result->is_allocated)
+ if (memcached_is_allocated(result))
+ {
memcached_result_free(result);
+ }
else
+ {
memcached_string_reset(&result->value);
+ }
return NULL;
}
uint32_t x;
memcached_return ret= MEMCACHED_SUCCESS;
- if (ptr->root->purging || /* already purging */
+ if (ptr->root->options.is_purging || /* already purging */
(memcached_server_response_count(ptr) < ptr->root->io_msg_watermark &&
ptr->io_bytes_sent < ptr->root->io_bytes_watermark) ||
(ptr->io_bytes_sent >= ptr->root->io_bytes_watermark &&
/* memcached_io_write and memcached_response may call memcached_purge
so we need to be able stop any recursion.. */
- ptr->root->purging= 1;
+ ptr->root->options.is_purging= true;
WATCHPOINT_ASSERT(ptr->fd != -1);
/* Force a flush of the buffer to ensure that we don't have the n-1 pending
requests buffered up.. */
if (memcached_io_write(ptr, NULL, 0, 1) == -1)
{
- ptr->root->purging= 0;
+ ptr->root->options.is_purging= true;
return MEMCACHED_WRITE_FAILURE;
}
WATCHPOINT_ASSERT(ptr->fd != -1);
memcached_result_free(result_ptr);
ptr->root->poll_timeout= timeo;
}
- ptr->root->purging= 0;
+ ptr->root->options.is_purging= false;
return ret;
}
memcached_result_st *memcached_result_create(memcached_st *memc,
memcached_result_st *ptr)
{
+ WATCHPOINT_ASSERT(memc && memc->options.is_initialized);
+
/* Saving malloc calls :) */
if (ptr)
+ {
memset(ptr, 0, sizeof(memcached_result_st));
+ }
else
{
ptr= memc->call_malloc(memc, sizeof(memcached_result_st));
if (ptr == NULL)
return NULL;
- ptr->is_allocated= true;
+ ptr->options.is_allocated= true;
}
+ ptr->options.is_initialized= true;
+
ptr->root= memc;
memcached_string_create(memc, &ptr->value, 0);
+ WATCHPOINT_ASSERT_INITIALIZED(&ptr->value);
WATCHPOINT_ASSERT(ptr->value.string == NULL);
return ptr;
memcached_string_free(&ptr->value);
- if (ptr->is_allocated)
+ if (memcached_is_allocated(ptr))
+ {
free(ptr);
+ }
+ else
+ {
+ ptr->options.is_initialized= false;
+ }
}
#endif
struct memcached_result_st {
+ struct {
+ bool is_allocated:1;
+ bool is_initialized:1;
+ } options;
uint32_t flags;
- bool is_allocated;
time_t expiration;
memcached_st *root;
size_t key_length;
if (!ptr)
return NULL; /* MEMCACHED_MEMORY_ALLOCATION_FAILURE */
- ptr->is_allocated= true;
+ ptr->options.is_allocated= true;
}
else
+ {
memset(ptr, 0, sizeof(memcached_server_st));
+ }
ptr->root= memc;
if (ptr->address_info)
freeaddrinfo(ptr->address_info);
- if (ptr->is_allocated)
+
+ if (memcached_is_allocated(ptr))
+ {
ptr->root->call_free(ptr->root, ptr);
+ }
else
+ {
memset(ptr, 0, sizeof(memcached_server_st));
+ }
}
/*
#endif
struct memcached_server_st {
- bool is_allocated;
+ struct {
+ bool is_allocated:1;
+ } options;
bool sockaddr_inited;
uint16_t count;
unsigned int cursor_active;
static memcached_return set_data(memcached_stat_st *memc_stat, char *key, char *value)
{
- if(strlen(key) < 1)
+ if (strlen(key) < 1)
{
WATCHPOINT_STRING(key);
return MEMCACHED_UNKNOWN_STAT_KEY;
{
memcached_return rc;
memcached_st memc;
+ memcached_st *memc_ptr;
- memcached_create(&memc);
+ memc_ptr= memcached_create(&memc);
+ WATCHPOINT_ASSERT(memc_ptr);
memcached_server_add(&memc, hostname, port);
#include "common.h"
-memcached_return memcached_string_check(memcached_string_st *string, size_t need)
+inline static memcached_return _string_check(memcached_string_st *string, size_t need)
{
if (need && need > (size_t)(string->current_size - (size_t)(string->end - string->string)))
{
return MEMCACHED_SUCCESS;
}
-memcached_string_st *memcached_string_create(memcached_st *ptr, memcached_string_st *string, size_t initial_size)
+memcached_string_st *memcached_string_create(memcached_st *memc, memcached_string_st *string, size_t initial_size)
{
memcached_return rc;
/* Saving malloc calls :) */
if (string)
+ {
+ WATCHPOINT_ASSERT(memc->options.is_safe && string->options.is_initialized == false);
+
memset(string, 0, sizeof(memcached_string_st));
+ }
else
{
- string= ptr->call_calloc(ptr, 1, sizeof(memcached_string_st));
+ string= memc->call_calloc(memc, 1, sizeof(memcached_string_st));
if (string == NULL)
+ {
return NULL;
- string->is_allocated= true;
+ }
+
+ string->options.is_allocated= true;
}
string->block_size= MEMCACHED_BLOCK_SIZE;
- string->root= ptr;
+ string->root= memc;
- rc= memcached_string_check(string, initial_size);
+ rc= _string_check(string, initial_size);
if (rc != MEMCACHED_SUCCESS)
{
- ptr->call_free(ptr, string);
+ memc->call_free(memc, string);
return NULL;
}
+ string->options.is_initialized= true;
+
WATCHPOINT_ASSERT(string->string == string->end);
return string;
{
memcached_return rc;
- rc= memcached_string_check(string, 1);
+ rc= _string_check(string, 1);
if (rc != MEMCACHED_SUCCESS)
return rc;
{
memcached_return rc;
- rc= memcached_string_check(string, length);
+ rc= _string_check(string, length);
if (rc != MEMCACHED_SUCCESS)
return rc;
return;
if (ptr->string)
+ {
ptr->root->call_free(ptr->root, ptr->string);
+ }
- if (ptr->is_allocated)
+ if (memcached_is_allocated(ptr))
+ {
ptr->root->call_free(ptr->root, ptr);
+ }
else
+ {
+ ptr->options.is_initialized= false;
memset(ptr, 0, sizeof(memcached_string_st));
+ }
+}
+
+memcached_return memcached_string_check(memcached_string_st *string, size_t need)
+{
+ return _string_check(string, need);
}
+
char *string;
size_t current_size;
size_t block_size;
- bool is_allocated;
+ struct {
+ bool is_allocated:1;
+ bool is_initialized:1;
+ } options;
};
#define memcached_string_length(A) (size_t)((A)->end - (A)->string)
size_t initial_size);
LIBMEMCACHED_API
memcached_return memcached_string_check(memcached_string_st *string, size_t need);
+
LIBMEMCACHED_API
char *memcached_string_c_copy(memcached_string_st *string);
+
LIBMEMCACHED_API
memcached_return memcached_string_append_character(memcached_string_st *string,
char character);
const char *value, size_t length);
LIBMEMCACHED_API
memcached_return memcached_string_reset(memcached_string_st *string);
+
LIBMEMCACHED_API
void memcached_string_free(memcached_string_st *string);
#define WATCHPOINT_ERRNO(A) fprintf(stderr, "\nWATCHPOINT %s:%d (%s) %s\n", __FILE__, __LINE__,__func__, strerror(A));fflush(stdout);
#define WATCHPOINT_ASSERT_PRINT(A,B,C) if(!(A)){fprintf(stderr, "\nWATCHPOINT ASSERT %s:%d (%s) ", __FILE__, __LINE__,__func__);fprintf(stderr, (B),(C));fprintf(stderr,"\n");fflush(stdout);}assert((A));
#define WATCHPOINT_ASSERT(A) assert((A));
+#define WATCHPOINT_ASSERT_INITIALIZED(A) (memcached_is_initialized((A));
+
#else
+
#define WATCHPOINT
#define WATCHPOINT_ERROR(A)
#define WATCHPOINT_IFERROR(A)
#define WATCHPOINT_ERRNO(A)
#define WATCHPOINT_ASSERT_PRINT(A,B,C)
#define WATCHPOINT_ASSERT(A)
+#define WATCHPOINT_ASSERT_INITIALIZED(A)
#endif /* DEBUG */
memcached_result_st *result_ptr;
result_ptr= memcached_result_create(memc, &result);
- test_truth(result.is_allocated == false);
+ test_truth(result.options.is_allocated == false);
+ test_truth(memcached_is_initialized(&result) == true);
test_truth(result_ptr);
+ test_truth(result_ptr == &result);
+
memcached_result_free(&result);
+ test_truth(result.options.is_allocated == false);
+ test_truth(memcached_is_initialized(&result) == false);
+
return TEST_SUCCESS;
}
static test_return_t result_alloc(memcached_st *memc)
{
- memcached_result_st *result;
+ memcached_result_st *result_ptr;
- result= memcached_result_create(memc, NULL);
- test_truth(result);
- memcached_result_free(result);
+ result_ptr= memcached_result_create(memc, NULL);
+ test_truth(result_ptr);
+ test_truth(result_ptr->options.is_allocated == true);
+ test_truth(memcached_is_initialized(result_ptr) == true);
+ memcached_result_free(result_ptr);
return TEST_SUCCESS;
}
memcached_string_st *string_ptr;
string_ptr= memcached_string_create(memc, &string, 0);
- test_truth(string.is_allocated == false);
+ test_truth(string.options.is_initialized == true);
test_truth(string_ptr);
+
+ /* The following two better be the same! */
+ test_truth(memcached_is_allocated(string_ptr) == false);
+ test_truth(memcached_is_allocated(&string) == false);
+ test_truth(&string == string_ptr);
+
+ test_truth(string.options.is_initialized == true);
+ test_truth(memcached_is_initialized(&string) == true);
memcached_string_free(&string);
+ test_truth(memcached_is_initialized(&string) == false);
return TEST_SUCCESS;
}
string= memcached_string_create(memc, NULL, 0);
test_truth(string);
+ test_truth(memcached_is_allocated(string) == true);
+ test_truth(memcached_is_initialized(string) == true);
memcached_string_free(string);
return TEST_SUCCESS;
string= memcached_string_create(memc, NULL, 1024);
test_truth(string);
+ test_truth(memcached_is_allocated(string) == true);
+ test_truth(memcached_is_initialized(string) == true);
memcached_string_free(string);
return TEST_SUCCESS;
string= memcached_string_create(memc, NULL, 100);
test_truth(string);
+ test_truth(memcached_is_allocated(string) == true);
+ test_truth(memcached_is_initialized(string) == true);
for (x= 0; x < 1024; x++)
{
rc= memcached_string_append(string, buffer, SMALL_STRING_LEN);
test_truth(rc == MEMCACHED_SUCCESS);
}
+ test_truth(memcached_is_allocated(string) == true);
memcached_string_free(string);
return TEST_SUCCESS;
string= memcached_string_create(memc, NULL, 100);
test_truth(string);
+ test_truth(memcached_is_allocated(string) == true);
+ test_truth(memcached_is_initialized(string) == true);
for (x= 0; x < 1024; x++)
{
}
rc= memcached_string_append(string, buffer, SIZE_MAX);
test_truth(rc == MEMCACHED_MEMORY_ALLOCATION_FAILURE);
+ test_truth(memcached_is_allocated(string) == true);
memcached_string_free(string);
return TEST_SUCCESS;