Merge Mark's test code.
[m6w6/libmemcached] / lib / memcached.c
1 /*
2 Memcached library
3 */
4 #include <memcached.h>
5
6 memcached_st *memcached_init(memcached_st *ptr)
7 {
8 if (!ptr)
9 {
10 ptr= (memcached_st *)malloc(sizeof(memcached_st));
11
12 if (!ptr)
13 return NULL; /* MEMCACHED_MEMORY_ALLOCATION_FAILURE */
14
15 memset(ptr, 0, sizeof(memcached_st));
16 ptr->is_allocated= MEMCACHED_ALLOCATED;
17 }
18 else
19 {
20 memset(ptr, 0, sizeof(memcached_st));
21 }
22
23 return ptr;
24 }
25
26 void memcached_deinit(memcached_st *ptr)
27 {
28 unsigned int x;
29
30 if (ptr->hosts)
31 {
32 for (x= 0; x < ptr->number_of_hosts; x++)
33 {
34 if (ptr->hosts[x].fd > 0)
35 close(ptr->hosts[x].fd);
36
37 free(ptr->hosts[x].hostname);
38 }
39
40 free(ptr->hosts);
41 }
42
43 if (ptr->is_allocated == MEMCACHED_ALLOCATED)
44 free(ptr);
45 else
46 memset(ptr, 0, sizeof(memcached_st));
47 }