Modified Hashkit class (turned it into an actual class).
[m6w6/libmemcached] / libmemcached / string.c
1 /* LibMemcached
2 * Copyright (C) 2006-2009 Brian Aker
3 * All rights reserved.
4 *
5 * Use and distribution licensed under the BSD license. See
6 * the COPYING file in the parent directory for full text.
7 *
8 * Summary: String structure used for libmemcached.
9 *
10 */
11
12 #include "common.h"
13
14 inline static memcached_return_t _string_check(memcached_string_st *string, size_t need)
15 {
16 if (need && need > (size_t)(string->current_size - (size_t)(string->end - string->string)))
17 {
18 size_t current_offset= (size_t) (string->end - string->string);
19 char *new_value;
20 size_t adjust;
21 size_t new_size;
22
23 /* This is the block multiplier. To keep it larger and surive division errors we must round it up */
24 adjust= (need - (size_t)(string->current_size - (size_t)(string->end - string->string))) / MEMCACHED_BLOCK_SIZE;
25 adjust++;
26
27 new_size= sizeof(char) * (size_t)((adjust * MEMCACHED_BLOCK_SIZE) + string->current_size);
28 /* Test for overflow */
29 if (new_size < need)
30 return MEMCACHED_MEMORY_ALLOCATION_FAILURE;
31
32 new_value= libmemcached_realloc(string->root, string->string, new_size);
33
34 if (new_value == NULL)
35 return MEMCACHED_MEMORY_ALLOCATION_FAILURE;
36
37 string->string= new_value;
38 string->end= string->string + current_offset;
39
40 string->current_size+= (MEMCACHED_BLOCK_SIZE * adjust);
41 }
42
43 return MEMCACHED_SUCCESS;
44 }
45
46 static inline void _init_string(memcached_string_st *self)
47 {
48 self->current_size= 0;
49 self->end= self->string= NULL;
50 }
51
52 memcached_string_st *memcached_string_create(const memcached_st *memc, memcached_string_st *self, size_t initial_size)
53 {
54 memcached_return_t rc;
55
56 WATCHPOINT_ASSERT(memc);
57
58 /* Saving malloc calls :) */
59 if (self)
60 {
61 WATCHPOINT_ASSERT(self->options.is_initialized == false);
62
63 self->options.is_allocated= false;
64 }
65 else
66 {
67 self= libmemcached_malloc(memc, sizeof(memcached_string_st));
68
69 if (self == NULL)
70 {
71 return NULL;
72 }
73
74 self->options.is_allocated= true;
75 }
76 self->root= memc;
77
78 _init_string(self);
79
80 rc= _string_check(self, initial_size);
81 if (rc != MEMCACHED_SUCCESS)
82 {
83 libmemcached_free(memc, self);
84 return NULL;
85 }
86
87 self->options.is_initialized= true;
88
89 WATCHPOINT_ASSERT(self->string == self->end);
90
91 return self;
92 }
93
94 memcached_return_t memcached_string_append_character(memcached_string_st *string,
95 char character)
96 {
97 memcached_return_t rc;
98
99 rc= _string_check(string, 1);
100
101 if (rc != MEMCACHED_SUCCESS)
102 return rc;
103
104 *string->end= character;
105 string->end++;
106
107 return MEMCACHED_SUCCESS;
108 }
109
110 memcached_return_t memcached_string_append(memcached_string_st *string,
111 const char *value, size_t length)
112 {
113 memcached_return_t rc;
114
115 rc= _string_check(string, length);
116
117 if (rc != MEMCACHED_SUCCESS)
118 return rc;
119
120 WATCHPOINT_ASSERT(length <= string->current_size);
121 WATCHPOINT_ASSERT(string->string);
122 WATCHPOINT_ASSERT(string->end >= string->string);
123
124 memcpy(string->end, value, length);
125 string->end+= length;
126
127 return MEMCACHED_SUCCESS;
128 }
129
130 char *memcached_string_c_copy(memcached_string_st *string)
131 {
132 char *c_ptr;
133
134 if (memcached_string_length(string) == 0)
135 return NULL;
136
137 c_ptr= libmemcached_malloc(string->root, (memcached_string_length(string)+1) * sizeof(char));
138
139 if (c_ptr == NULL)
140 return NULL;
141
142 memcpy(c_ptr, memcached_string_value(string), memcached_string_length(string));
143 c_ptr[memcached_string_length(string)]= 0;
144
145 return c_ptr;
146 }
147
148 memcached_return_t memcached_string_reset(memcached_string_st *string)
149 {
150 string->end= string->string;
151
152 return MEMCACHED_SUCCESS;
153 }
154
155 void memcached_string_free(memcached_string_st *ptr)
156 {
157 if (ptr == NULL)
158 return;
159
160 if (ptr->string)
161 {
162 libmemcached_free(ptr->root, ptr->string);
163 }
164
165 if (memcached_is_allocated(ptr))
166 {
167 libmemcached_free(ptr->root, ptr);
168 }
169 else
170 {
171 ptr->options.is_initialized= false;
172 }
173 }
174
175 memcached_return_t memcached_string_check(memcached_string_st *string, size_t need)
176 {
177 return _string_check(string, need);
178 }
179