vector.cc 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
  2. *
  3. * Gearmand String
  4. *
  5. * Copyright (C) 2011-2013 Data Differential, http://datadifferential.com/
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are
  9. * met:
  10. *
  11. * * Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * * Redistributions in binary form must reproduce the above
  15. * copyright notice, this list of conditions and the following disclaimer
  16. * in the documentation and/or other materials provided with the
  17. * distribution.
  18. *
  19. * * The names of its contributors may not be used to endorse or
  20. * promote products derived from this software without specific prior
  21. * written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  24. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  25. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  26. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  27. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  28. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  29. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  30. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  31. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  32. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  33. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  34. *
  35. */
  36. #include "gear_config.h"
  37. #include "libgearman/assert.hpp"
  38. #include "libgearman/is.hpp"
  39. #include "libgearman/vector.hpp"
  40. #include <cstdlib>
  41. #include <cstring>
  42. #include <memory>
  43. #include "util/memory.h"
  44. using namespace org::tangent;
  45. inline static bool _string_check(gearman_vector_st *string, const size_t need)
  46. {
  47. assert_msg(string, "Programmer error, _string_check() was passed a null gearman_vector_st");
  48. if (string)
  49. {
  50. if (need and need > size_t(string->current_size - size_t(string->end - string->string)))
  51. {
  52. size_t current_offset= size_t(string->end - string->string);
  53. /* This is the block multiplier. To keep it larger and surive division errors we must round it up */
  54. size_t adjust= (need - size_t(string->current_size - size_t(string->end - string->string))) / GEARMAN_VECTOR_BLOCK_SIZE;
  55. adjust++;
  56. size_t new_size= sizeof(char) * size_t((adjust * GEARMAN_VECTOR_BLOCK_SIZE) + string->current_size);
  57. /* Test for overflow */
  58. if (new_size < need)
  59. {
  60. return false;
  61. }
  62. char* new_value= static_cast<char *>(realloc(string->string, new_size));
  63. if (new_value == NULL)
  64. {
  65. return false;
  66. }
  67. string->string= new_value;
  68. string->end= string->string + current_offset;
  69. string->current_size+= (GEARMAN_VECTOR_BLOCK_SIZE * adjust);
  70. }
  71. return true;
  72. }
  73. return false;
  74. }
  75. void gearman_vector_st::init()
  76. {
  77. current_size= 0;
  78. end= string= NULL;
  79. }
  80. gearman_vector_st *gearman_string_create(gearman_vector_st *self, const char *str, size_t str_size)
  81. {
  82. assert_msg(str, "Programmer error, gearman_string_clear() was passed a null string");
  83. if (str == NULL)
  84. {
  85. return NULL;
  86. }
  87. self= gearman_string_create(self, str_size);
  88. assert_vmsg(self, "Programmer error, gearman_string_create() returned a null gearman_vector_st() requesting a reserve of %u", uint32_t(str_size));
  89. if (self)
  90. {
  91. if ((gearman_string_append(self, str, str_size) == false))
  92. {
  93. assert_vmsg(self, "Programmer error, gearman_string_append() returned false while trying to append a string of %u length", uint32_t(str_size));
  94. gearman_string_free(self);
  95. return NULL;
  96. }
  97. }
  98. return self;
  99. }
  100. gearman_vector_st::gearman_vector_st(size_t initial_size) :
  101. end(NULL),
  102. string(NULL),
  103. current_size(0)
  104. {
  105. if (initial_size)
  106. {
  107. _string_check(this, initial_size +1);
  108. }
  109. }
  110. gearman_vector_st *gearman_string_create(gearman_vector_st *self, size_t reserve_)
  111. {
  112. /* Saving malloc calls :) */
  113. if (self == NULL)
  114. {
  115. self= new (std::nothrow) gearman_vector_st(reserve_);
  116. assert_vmsg(self, "Programmer error, new gearman_vector_st() failed reserve: %u", uint32_t(reserve_));
  117. if (self == NULL)
  118. {
  119. return NULL;
  120. }
  121. gearman_set_allocated(self, true);
  122. }
  123. else
  124. {
  125. self->clear();
  126. self->resize(reserve_);
  127. }
  128. gearman_set_initialized(self, true);
  129. assert_vmsg(reserve_ <= self->capacity(), "Programmer error, capacity: %u reserve: %u", uint32_t(self->capacity()), uint32_t(reserve_));
  130. if (reserve_ > self->capacity())
  131. {
  132. gearman_string_free(self);
  133. return NULL;
  134. }
  135. return self;
  136. }
  137. gearman_vector_st *gearman_string_clone(const gearman_vector_st *self)
  138. {
  139. gearman_vector_st *clone= NULL;
  140. if (self)
  141. {
  142. clone= gearman_string_create(NULL, gearman_string_length(self));
  143. if (clone)
  144. {
  145. if (gearman_string_length(self))
  146. {
  147. if (gearman_string_append(clone, gearman_string_value(self), gearman_string_length(self)) == false)
  148. {
  149. gearman_string_free(clone);
  150. return NULL;
  151. }
  152. }
  153. }
  154. }
  155. return clone;
  156. }
  157. bool gearman_string_append_character(gearman_vector_st *string, char character)
  158. {
  159. assert_msg(string, "Programmer error, gearman_string_append_character() was passed a null gearman_vector_st");
  160. if (_string_check(string, 1 +1) == false) // Null terminate
  161. {
  162. return false;
  163. }
  164. *string->end= character;
  165. string->end++;
  166. *string->end= 0;
  167. return true;
  168. }
  169. bool gearman_string_append(gearman_vector_st *string,
  170. const char *value, size_t length)
  171. {
  172. assert_msg(string, "Programmer error, gearman_string_append() was passed a null gearman_vector_st");
  173. if (_string_check(string, length +1) == false)
  174. {
  175. return false;
  176. }
  177. memcpy(string->end, value, length);
  178. string->end+= length;
  179. *string->end= 0; // Add a NULL
  180. return true;
  181. }
  182. char *gearman_string_c_copy(gearman_vector_st *string)
  183. {
  184. char *c_ptr= NULL;
  185. if (gearman_string_length(string) == 0)
  186. {
  187. c_ptr= static_cast<char *>(malloc((gearman_string_length(string) +1) * sizeof(char)));
  188. if (c_ptr)
  189. {
  190. memcpy(c_ptr, gearman_string_value(string), gearman_string_length(string));
  191. c_ptr[gearman_string_length(string)]= 0;
  192. }
  193. }
  194. return c_ptr;
  195. }
  196. void gearman_string_clear(gearman_vector_st *string)
  197. {
  198. assert_msg(string, "Programmer error, gearman_string_clear() was passed a null gearman_vector_st");
  199. string->clear();
  200. }
  201. bool gearman_vector_st::store(const char* arg_, const size_t arg_length_)
  202. {
  203. clear();
  204. return append(arg_, arg_length_);
  205. }
  206. bool gearman_vector_st::append(const char* arg_, const size_t arg_length_)
  207. {
  208. return gearman_string_append(this, arg_, arg_length_);
  209. }
  210. gearman_vector_st::~gearman_vector_st()
  211. {
  212. if (string)
  213. {
  214. void* tmp_ptr= string;
  215. util::free__(tmp_ptr);
  216. }
  217. }
  218. void gearman_vector_st::resize(const size_t size_)
  219. {
  220. if (size_ == 0)
  221. {
  222. void* tmp_ptr= string;
  223. util::free__(tmp_ptr);
  224. init();
  225. }
  226. else if (size_ > capacity())
  227. {
  228. reserve(size_);
  229. }
  230. else if (size_ < capacity())
  231. {
  232. size_t final_size= (size_ < size()) ? size_ : size();
  233. char* new_value= static_cast<char *>(realloc(string, size_ +1));
  234. if (new_value == NULL)
  235. {
  236. return;
  237. }
  238. string= new_value;
  239. end= string +final_size;
  240. current_size= size_ +1;
  241. string[final_size]= 0;
  242. }
  243. }
  244. void gearman_string_free(gearman_vector_st *string)
  245. {
  246. if (string)
  247. {
  248. if (gearman_is_allocated(string))
  249. {
  250. delete string;
  251. return;
  252. }
  253. assert(gearman_is_allocated(string) == false);
  254. string->resize(0);
  255. gearman_set_initialized(string, false);
  256. }
  257. }
  258. bool gearman_string_reserve(gearman_vector_st *string, size_t need_)
  259. {
  260. if (need_)
  261. {
  262. return _string_check(string, need_ +1);
  263. }
  264. // Let _string_check handle the behavior of zero
  265. return _string_check(string, need_);
  266. }
  267. size_t gearman_vector_st::size() const
  268. {
  269. assert(end >= string);
  270. return size_t(end -string);
  271. }
  272. void gearman_vector_st::reserve(size_t need_)
  273. {
  274. if (need_)
  275. {
  276. _string_check(this, need_ +1);
  277. }
  278. // Let _string_check handle the behavior of zero
  279. _string_check(this, need_);
  280. }
  281. size_t gearman_string_length(const gearman_vector_st *self)
  282. {
  283. if (self)
  284. {
  285. return self->size();
  286. }
  287. return 0;
  288. }
  289. const char *gearman_string_value(const gearman_vector_st *self)
  290. {
  291. if (self)
  292. {
  293. return self->string;
  294. }
  295. return NULL;
  296. }
  297. gearman_string_t gearman_string(const gearman_vector_st *self)
  298. {
  299. assert(self);
  300. gearman_string_t passable= { gearman_string_value(self), gearman_string_length(self) };
  301. return passable;
  302. }
  303. gearman_string_t gearman_string_take_string(gearman_vector_st *self)
  304. {
  305. assert(self);
  306. if (gearman_string_length(self))
  307. {
  308. gearman_string_t passable= gearman_string(self);
  309. self->init();
  310. return passable;
  311. }
  312. static gearman_string_t ret= {0, 0};
  313. return ret;
  314. }