vector.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  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 "libgearman/vector.h"
  41. #include <cstdlib>
  42. #include <cstdio>
  43. #include <cstring>
  44. #include <memory>
  45. #include "util/memory.h"
  46. using namespace org::tangent;
  47. #ifndef __INTEL_COMPILER
  48. # pragma GCC diagnostic ignored "-Wformat-nonliteral"
  49. # pragma GCC diagnostic ignored "-Wformat-security"
  50. #endif
  51. inline static bool _string_check(gearman_vector_st *string, const size_t need)
  52. {
  53. assert_msg(string, "Programmer error, _string_check() was passed a null gearman_vector_st");
  54. if (string)
  55. {
  56. if (need and need > size_t(string->current_size - size_t(string->end - string->string)))
  57. {
  58. size_t current_offset= size_t(string->end - string->string);
  59. /* This is the block multiplier. To keep it larger and surive division errors we must round it up */
  60. size_t adjust= (need - size_t(string->current_size - size_t(string->end - string->string))) / GEARMAN_VECTOR_BLOCK_SIZE;
  61. adjust++;
  62. size_t new_size= sizeof(char) * size_t((adjust * GEARMAN_VECTOR_BLOCK_SIZE) + string->current_size);
  63. /* Test for overflow */
  64. if (new_size < need)
  65. {
  66. return false;
  67. }
  68. char* new_value= static_cast<char *>(realloc(string->string, new_size));
  69. if (new_value == NULL)
  70. {
  71. return false;
  72. }
  73. string->string= new_value;
  74. string->end= string->string + current_offset;
  75. string->current_size+= (GEARMAN_VECTOR_BLOCK_SIZE * adjust);
  76. }
  77. return true;
  78. }
  79. return false;
  80. }
  81. void gearman_vector_st::init()
  82. {
  83. current_size= 0;
  84. end= string= NULL;
  85. }
  86. gearman_vector_st *gearman_string_create(gearman_vector_st *self, const char *str, const size_t str_size)
  87. {
  88. if (str_size and str == NULL)
  89. {
  90. assert_msg(str, "Programmer error, gearman_string_clear() was passed a null string, but str_size > 0");
  91. return NULL;
  92. }
  93. if (str == NULL)
  94. {
  95. return NULL;
  96. }
  97. self= gearman_string_create(self, str_size);
  98. assert_vmsg(self, "Programmer error, gearman_string_create() returned a null gearman_vector_st() requesting a reserve of %u", uint32_t(str_size));
  99. if (self)
  100. {
  101. if ((self->store(str, str_size) == false))
  102. {
  103. assert_vmsg(self, "Programmer error, gearman_string_append() returned false while trying to append a string of %u length", uint32_t(str_size));
  104. gearman_string_free(self);
  105. return NULL;
  106. }
  107. }
  108. return self;
  109. }
  110. gearman_vector_st::gearman_vector_st(const size_t reserve_) :
  111. end(NULL),
  112. string(NULL),
  113. current_size(0)
  114. {
  115. if (reserve_)
  116. {
  117. _string_check(this, reserve_ +1);
  118. }
  119. }
  120. gearman_vector_st *gearman_string_create(gearman_vector_st *self, const size_t reserve_)
  121. {
  122. /* Saving malloc calls :) */
  123. if (self == NULL)
  124. {
  125. self= new (std::nothrow) gearman_vector_st(reserve_);
  126. assert_vmsg(self, "Programmer error, new gearman_vector_st() failed reserve: %u", uint32_t(reserve_));
  127. if (self == NULL)
  128. {
  129. return NULL;
  130. }
  131. gearman_set_allocated(self, true);
  132. }
  133. else
  134. {
  135. self->clear();
  136. self->resize(reserve_);
  137. }
  138. gearman_set_initialized(self, true);
  139. assert_vmsg(reserve_ <= self->capacity(), "Programmer error, capacity: %u reserve: %u", uint32_t(self->capacity()), uint32_t(reserve_));
  140. if (reserve_ > self->capacity())
  141. {
  142. gearman_string_free(self);
  143. return NULL;
  144. }
  145. return self;
  146. }
  147. gearman_vector_st *gearman_string_clone(const gearman_vector_st *self)
  148. {
  149. gearman_vector_st *clone= NULL;
  150. if (self)
  151. {
  152. clone= gearman_string_create(NULL, gearman_string_length(self));
  153. if (clone)
  154. {
  155. if (self->size())
  156. {
  157. if (clone->store(*self) == false)
  158. {
  159. gearman_string_free(clone);
  160. return NULL;
  161. }
  162. }
  163. }
  164. }
  165. return clone;
  166. }
  167. bool gearman_vector_st::append_character(const char character)
  168. {
  169. if (_string_check(this, 1 +1) == false) // Null terminate
  170. {
  171. return false;
  172. }
  173. *end= character;
  174. end++;
  175. *end= 0;
  176. return true;
  177. }
  178. bool gearman_string_append_character(gearman_vector_st *string_, const char character)
  179. {
  180. assert_msg(string_, "Programmer error, gearman_string_append_character() was passed a null gearman_vector_st");
  181. if (string_)
  182. {
  183. return string_->append_character(character);
  184. }
  185. return false;
  186. }
  187. bool gearman_string_append(gearman_vector_st *string,
  188. const char *value, size_t length)
  189. {
  190. assert_msg(string, "Programmer error, gearman_string_append() was passed a null gearman_vector_st");
  191. if (string)
  192. {
  193. string->append(value, length);
  194. }
  195. return false;
  196. }
  197. char *gearman_string_c_copy(gearman_vector_st *string)
  198. {
  199. char *c_ptr= NULL;
  200. if (gearman_string_length(string) == 0)
  201. {
  202. c_ptr= static_cast<char *>(malloc((gearman_string_length(string) +1) * sizeof(char)));
  203. if (c_ptr)
  204. {
  205. memcpy(c_ptr, gearman_string_value(string), gearman_string_length(string));
  206. c_ptr[gearman_string_length(string)]= 0;
  207. }
  208. }
  209. return c_ptr;
  210. }
  211. void gearman_string_clear(gearman_vector_st *string)
  212. {
  213. assert_msg(string, "Programmer error, gearman_string_clear() was passed a null gearman_vector_st");
  214. string->clear();
  215. }
  216. bool gearman_vector_st::store(const char* arg_, const size_t arg_length_)
  217. {
  218. clear();
  219. return append(arg_, arg_length_);
  220. }
  221. bool gearman_vector_st::store(const gearman_vector_st& vec)
  222. {
  223. clear();
  224. return append(vec.value(), vec.size());
  225. }
  226. bool gearman_vector_st::append(const char* arg_, const size_t arg_length_)
  227. {
  228. if (_string_check(this, arg_length_ +1) == false)
  229. {
  230. return false;
  231. }
  232. memcpy(end, arg_, arg_length_);
  233. end+= arg_length_;
  234. *end= 0; // Add a NULL
  235. return true;
  236. }
  237. int gearman_vector_st::vec_printf(const char *format__, ...)
  238. {
  239. clear();
  240. if (format__)
  241. {
  242. va_list args;
  243. va_start(args, format__);
  244. int required_size= vec_size_printf(format__, args);
  245. va_end(args);
  246. va_start(args, format__);
  247. int actual_size= vec_ptr_printf(required_size, format__, args);
  248. va_end(args);
  249. return actual_size;
  250. }
  251. return -1;
  252. }
  253. int gearman_vector_st::vec_append_printf(const char *format__, ...)
  254. {
  255. if (format__)
  256. {
  257. va_list args;
  258. va_start(args, format__);
  259. int required_size= vec_size_printf(format__, args);
  260. va_end(args);
  261. va_start(args, format__);
  262. int actual_size= vec_ptr_printf(required_size, format__, args);
  263. va_end(args);
  264. return actual_size;
  265. }
  266. return -1;
  267. }
  268. int gearman_vector_st::vec_size_printf(const char *format__, va_list args__)
  269. {
  270. int required_size= vsnprintf(NULL, 0, format__, args__);
  271. if (required_size)
  272. {
  273. required_size++;
  274. }
  275. return required_size;
  276. }
  277. int gearman_vector_st::vec_ptr_printf(const int required_size, const char *format__, va_list args__)
  278. {
  279. if (required_size > 0)
  280. {
  281. int actual_size= 0;
  282. if (required_size > 0 and reserve(required_size + size()))
  283. {
  284. actual_size= vsnprintf(end, capacity() - size(), format__, args__);
  285. assert(required_size == actual_size +1);
  286. end+= actual_size;
  287. }
  288. return actual_size;
  289. }
  290. return -1;
  291. }
  292. gearman_vector_st::~gearman_vector_st()
  293. {
  294. if (string)
  295. {
  296. void* tmp_ptr= string;
  297. util::free__(tmp_ptr);
  298. }
  299. }
  300. bool gearman_vector_st::resize(const size_t size_)
  301. {
  302. if (size_ == 0)
  303. {
  304. void* tmp_ptr= string;
  305. util::free__(tmp_ptr);
  306. init();
  307. }
  308. else if (size_ > capacity())
  309. {
  310. return reserve(size_);
  311. }
  312. else if (size_ < capacity())
  313. {
  314. size_t final_size= (size_ < size()) ? size_ : size();
  315. char* new_value= static_cast<char *>(realloc(string, size_ +1));
  316. if (new_value == NULL)
  317. {
  318. return false;
  319. }
  320. string= new_value;
  321. end= string +final_size;
  322. current_size= size_ +1;
  323. string[final_size]= 0;
  324. }
  325. return true;
  326. }
  327. void gearman_string_free(gearman_vector_st*& string)
  328. {
  329. if (string)
  330. {
  331. if (gearman_is_allocated(string))
  332. {
  333. delete string;
  334. string= NULL;
  335. return;
  336. }
  337. assert(gearman_is_allocated(string) == false);
  338. string->resize(0);
  339. gearman_set_initialized(string, false);
  340. }
  341. }
  342. bool gearman_string_reserve(gearman_vector_st *string, size_t need_)
  343. {
  344. if (string)
  345. {
  346. return string->reserve(need_);
  347. }
  348. return false;
  349. }
  350. size_t gearman_vector_st::size() const
  351. {
  352. assert(end >= string);
  353. return size_t(end -string);
  354. }
  355. gearman_string_t gearman_vector_st::take()
  356. {
  357. if (size())
  358. {
  359. gearman_string_t passable= { string, size() };
  360. init();
  361. return passable;
  362. }
  363. static gearman_string_t ret= {0, 0};
  364. return ret;
  365. }
  366. bool gearman_vector_st::reserve(const size_t need_)
  367. {
  368. if (need_)
  369. {
  370. return _string_check(this, need_ +1);
  371. }
  372. // Let _string_check handle the behavior of zero
  373. return _string_check(this, need_);
  374. }
  375. size_t gearman_string_length(const gearman_vector_st *self)
  376. {
  377. if (self)
  378. {
  379. return self->size();
  380. }
  381. return 0;
  382. }
  383. const char *gearman_string_value(const gearman_vector_st *self)
  384. {
  385. if (self)
  386. {
  387. return self->string;
  388. }
  389. return NULL;
  390. }
  391. gearman_string_t gearman_string(const gearman_vector_st *self)
  392. {
  393. assert(self);
  394. gearman_string_t passable= { gearman_string_value(self), gearman_string_length(self) };
  395. return passable;
  396. }
  397. gearman_string_t gearman_string_take_string(gearman_vector_st *self)
  398. {
  399. assert(self);
  400. if (self)
  401. {
  402. return self->take();
  403. }
  404. static gearman_string_t ret= {0, 0};
  405. return ret;
  406. }