vector.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * Copyright (C) 2005 iptelorg GmbH
  3. *
  4. * This file is part of ser, a free SIP server.
  5. *
  6. * ser is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version
  10. *
  11. * For a license to use the ser software under conditions
  12. * other than those described here, or to purchase support for this
  13. * software, please contact iptel.org by e-mail at the following addresses:
  14. * [email protected]
  15. *
  16. * ser is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  24. */
  25. #include <cds/memory.h>
  26. #include <cds/vector.h>
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. int vector_add(vector_t *vector, void *element)
  31. {
  32. if (vector->element_count >= vector->allocated_count) {
  33. void *new_data;
  34. int new_size = vector->allocated_count + vector->allocation_count;
  35. if (new_size <= vector->allocated_count) return -1;
  36. new_data = (void *)cds_malloc(vector->element_size * new_size);
  37. if (!new_data) return -1;
  38. if (vector->data) {
  39. memcpy(new_data, vector->data,
  40. vector->element_size * vector->allocated_count);
  41. cds_free(vector->data);
  42. }
  43. vector->data = new_data;
  44. vector->allocated_count = new_size;
  45. }
  46. memcpy(vector->data + (vector->element_count * vector->element_size),
  47. element, vector->element_size);
  48. vector->element_count++;
  49. return 0;
  50. }
  51. int vector_remove(vector_t *vector, int index)
  52. {
  53. int cnt;
  54. if (index >= vector->element_count) return -1;
  55. cnt = vector->element_count - index - 1;
  56. if (cnt > 0) {
  57. memmove(vector->data + (index * vector->element_size),
  58. vector->data + ((index + 1) * vector->element_size),
  59. cnt * vector->element_size);
  60. }
  61. vector->element_count--;
  62. return 0;
  63. }
  64. int vector_get(vector_t *vector, int index, void *element_dst)
  65. {
  66. if (index >= vector->element_count) return -1;
  67. memcpy(element_dst, vector->data + (index * vector->element_size), vector->element_size);
  68. return 0;
  69. }
  70. void* vector_get_ptr(vector_t *vector, int index)
  71. {
  72. if (index >= vector->element_count) return NULL;
  73. else return vector->data + (index * vector->element_size);
  74. }
  75. void vector_destroy(vector_t *vector)
  76. {
  77. if (vector) {
  78. if (vector->data) cds_free(vector->data);
  79. vector->data = NULL;
  80. vector->allocation_count = 0;
  81. vector->element_count = 0;
  82. }
  83. }
  84. int vector_init(vector_t *vector, int element_size, int allocation_count)
  85. {
  86. if (!vector) return -1;
  87. vector->element_size = element_size;
  88. vector->element_count = 0;
  89. vector->allocation_count = allocation_count;
  90. vector->data = (void *)cds_malloc(element_size * allocation_count);
  91. if (!vector->data) {
  92. vector->allocated_count = 0;
  93. return -1;
  94. }
  95. else {
  96. vector->allocated_count = allocation_count;
  97. return 0;
  98. }
  99. }
  100. int vector_test(void)
  101. {
  102. /*TODO: do tests for vector_t - all "methods" must be called */
  103. return 0;
  104. }