vector.cc 11 KB

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