#pragma once
+#ifdef __cplusplus
+struct memcached_st;
+struct memcached_stat_st;
+struct memcached_analysis_st;
+struct memcached_result_st;
+struct memcached_array_st;
+struct memcached_error_t;
+
+// All of the flavors of memcache_server_st
+struct memcached_server_st;
+typedef const struct memcached_server_st *memcached_server_instance_st;
+typedef struct memcached_server_st *memcached_server_list_st;
+
+struct memcached_callback_st;
+
+// The following two structures are internal, and never exposed to users.
+struct memcached_string_st;
+struct memcached_string_t;
+struct memcached_continuum_item_st;
+
+#else
+
typedef struct memcached_st memcached_st;
typedef struct memcached_stat_st memcached_stat_st;
typedef struct memcached_analysis_st memcached_analysis_st;
typedef struct memcached_string_t memcached_string_t;
typedef struct memcached_continuum_item_st memcached_continuum_item_st;
+#endif
+
#ifdef __cplusplus
extern "C" {
if (self->allocators.malloc != _libmemcached_malloc)
{
void *ret = _libmemcached_malloc(self, nelem * size, context);
- if (not ret)
+ if (ret == NULL)
{
memset(ret, 0, nelem * size);
}
return;
}
- memcached_error_t *error= (struct memcached_error_t *)libmemcached_malloc(&memc, sizeof(struct memcached_error_t));
+ memcached_error_t *error= libmemcached_xmalloc(&memc, memcached_error_t);
if (error == NULL) // Bad business if this happens
{
return;
rc= MEMCACHED_CONNECTION_FAILURE;
}
- memcached_error_t *error= (struct memcached_error_t *)libmemcached_malloc(&memc, sizeof(struct memcached_error_t));
+ memcached_error_t *error= libmemcached_xmalloc(&memc, memcached_error_t);
if (error == NULL) // Bad business if this happens
{
return;
return NULL;
}
- memcached_error_t *error= (memcached_error_t *)libmemcached_malloc(server.root, sizeof(memcached_error_t));
+ memcached_error_t *error= libmemcached_xmalloc(server.root, memcached_error_t);
memcpy(error, server.error_messages, sizeof(memcached_error_t));
error->next= NULL;
keys, key_length, number_of_keys, mget_mode);
}
- uint32_t* hash= static_cast<uint32_t*>(libmemcached_malloc(ptr, sizeof(uint32_t) * number_of_keys));
- bool* dead_servers= static_cast<bool*>(libmemcached_calloc(ptr, memcached_server_count(ptr), sizeof(bool)));
+ uint32_t* hash= libmemcached_xvalloc(ptr, number_of_keys, uint32_t);
+ bool* dead_servers= libmemcached_xcalloc(ptr, memcached_server_count(ptr), bool);
if (hash == NULL || dead_servers == NULL)
{
memcached_continuum_item_st *new_ptr;
new_ptr= static_cast<memcached_continuum_item_st*>(libmemcached_realloc(ptr, ptr->ketama.continuum,
- sizeof(memcached_continuum_item_st) * (live_servers + MEMCACHED_CONTINUUM_ADDITION) * points_per_server));
+ (live_servers + MEMCACHED_CONTINUUM_ADDITION) * points_per_server,
+ sizeof(memcached_continuum_item_st)));
if (new_ptr == 0)
{
assert_msg(ptr, "Programmer mistake, somehow server_add() was passed a NULL memcached_st");
memcached_server_st *new_host_list= static_cast<memcached_server_st*>(libmemcached_realloc(ptr, memcached_server_list(ptr),
- sizeof(memcached_server_st) * (ptr->number_of_hosts + 1)));
+ (ptr->number_of_hosts + 1),
+ sizeof(memcached_server_st)));
if (new_host_list == NULL)
{
memcached_server_st *new_host_list;
new_host_list= static_cast<memcached_server_st*>(libmemcached_realloc(ptr, memcached_server_list(ptr),
- sizeof(memcached_server_st) * (count + memcached_server_count(ptr))));
+ (count + memcached_server_count(ptr)), sizeof(memcached_server_st)));
if (new_host_list == NULL)
{
return malloc(size);
}
+#define libmemcached_xmalloc(__memcachd_st, __type) ((__type *)libmemcached_malloc((__memcachd_st), sizeof(__type)))
-static inline void *libmemcached_realloc(const memcached_st *self, void *mem, const size_t size)
+static inline void *libmemcached_realloc(const memcached_st *self, void *mem, size_t nmemb, const size_t size)
{
if (self)
{
- return self->allocators.realloc(self, mem, size, self->allocators.context);
+ return self->allocators.realloc(self, mem, nmemb * size, self->allocators.context);
}
return realloc(mem, size);
}
+#define libmemcached_xrealloc(__memcachd_st, __mem, __nelem, __type) ((__type *)libmemcached_realloc((__memcachd_st), (__mem), (__nelem), sizeof(__type)))
+#define libmemcached_xvalloc(__memcachd_st, __nelem, __type) ((__type *)libmemcached_realloc((__memcachd_st), NULL, (__nelem), sizeof(__type)))
static inline void *libmemcached_calloc(const memcached_st *self, size_t nelem, size_t size)
{
return calloc(nelem, size);
}
+#define libmemcached_xcalloc(__memcachd_st, __nelem, __type) ((__type *)libmemcached_calloc((__memcachd_st), (__nelem), sizeof(__type)))
}
else
{
- ptr= static_cast<memcached_result_st *>(libmemcached_malloc(memc, sizeof(memcached_result_st)));
+ ptr= libmemcached_xmalloc(memc, memcached_result_st);
if (not ptr)
{
{
if (not self)
{
- self= (memcached_server_st *)libmemcached_malloc(memc, sizeof(memcached_server_st));
+ self= libmemcached_xmalloc(memc, struct memcached_server_st);
if (not self)
{
if (new_size < need)
return memcached_set_error(*string->root, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT);
- new_value= static_cast<char *>(libmemcached_realloc(string->root, string->string, new_size));
+ new_value= static_cast<char *>(libmemcached_realloc(string->root, string->string, new_size, sizeof(char)));
if (not new_value)
{
}
else
{
- self= static_cast<memcached_string_st *>(libmemcached_malloc(memc, sizeof(memcached_string_st)));
+ self= libmemcached_xmalloc(memc, memcached_string_st);
if (self == NULL)
{