2 This is a partial implementation for fetching/creating memcached_server_st objects.
6 memcached_server_st
*memcached_server_create(memcached_st
*memc
, memcached_server_st
*ptr
)
10 ptr
= (memcached_server_st
*)malloc(sizeof(memcached_server_st
));
13 return NULL
; /* MEMCACHED_MEMORY_ALLOCATION_FAILURE */
15 memset(ptr
, 0, sizeof(memcached_server_st
));
16 ptr
->is_allocated
= MEMCACHED_ALLOCATED
;
20 ptr
->is_allocated
= MEMCACHED_USED
;
21 memset(ptr
, 0, sizeof(memcached_server_st
));
29 void memcached_server_free(memcached_server_st
*ptr
)
31 WATCHPOINT_NUMBER(ptr
->port
);
32 WATCHPOINT_ASSERT(ptr
->is_allocated
!= MEMCACHED_NOT_ALLOCATED
);
34 memcached_quit_server(ptr
, 0);
36 if (ptr
->address_info
)
38 freeaddrinfo(ptr
->address_info
);
39 ptr
->address_info
= NULL
;
42 if (ptr
->is_allocated
== MEMCACHED_ALLOCATED
)
44 if (ptr
->root
&& ptr
->root
->call_free
)
45 ptr
->root
->call_free(ptr
->root
, ptr
);
52 If we do not have a valid object to clone from, we toss an error.
54 memcached_server_st
*memcached_server_clone(memcached_server_st
*clone
, memcached_server_st
*ptr
)
56 memcached_server_st
*new_clone
;
58 /* We just do a normal create if ptr is missing */
62 if (clone
&& clone
->is_allocated
== MEMCACHED_USED
)
68 new_clone
= memcached_server_create(ptr
->root
, clone
);
70 if (new_clone
== NULL
)
73 new_clone
->root
= ptr
->root
;
75 host_reset(new_clone
->root
, new_clone
,
76 ptr
->hostname
, ptr
->port
, ptr
->weight
,
82 memcached_return
memcached_server_cursor(memcached_st
*ptr
,
83 memcached_server_function
*callback
,
85 unsigned int number_of_callbacks
)
89 for (y
= 0; y
< ptr
->number_of_hosts
; y
++)
93 for (x
= 0; x
< number_of_callbacks
; x
++)
97 iferror
= (*callback
[x
])(ptr
, &ptr
->hosts
[y
], context
);
104 return MEMCACHED_SUCCESS
;
107 memcached_server_st
*memcached_server_by_key(memcached_st
*ptr
, const char *key
, size_t key_length
, memcached_return
*error
)
111 unlikely (key_length
== 0)
113 *error
= MEMCACHED_NO_KEY_PROVIDED
;
117 unlikely (ptr
->number_of_hosts
== 0)
119 *error
= MEMCACHED_NO_SERVERS
;
123 if ((ptr
->flags
& MEM_VERIFY_KEY
) && (memcachd_key_test((char **)&key
, &key_length
, 1) == MEMCACHED_BAD_KEY_PROVIDED
))
125 *error
= MEMCACHED_BAD_KEY_PROVIDED
;
129 server_key
= memcached_generate_hash(ptr
, key
, key_length
);
131 return memcached_server_clone(NULL
, &ptr
->hosts
[server_key
]);