simple_vector.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * Copyright (C) 2011-2018 Intel Corporation. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * * Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in
  12. * the documentation and/or other materials provided with the
  13. * distribution.
  14. * * Neither the name of Intel Corporation nor the names of its
  15. * contributors may be used to endorse or promote products derived
  16. * from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. *
  30. */
  31. #include <stddef.h>
  32. #include <stdlib.h>
  33. #include <string.h>
  34. #include "sgx_lfence.h"
  35. #include "simple_vector.h"
  36. //initial vector capacity when fisrt item is added to vector
  37. #define INIT_SIZE 10
  38. //max vector capacity of a vector
  39. #define MAX_SIZE 10000
  40. //init vector to all zero
  41. void vector_init(simple_vector* v)
  42. {
  43. if (v)
  44. {
  45. v->size = 0;
  46. v->capacity = 0;
  47. v->data = NULL;
  48. }
  49. }
  50. //return current number of items the vector holds
  51. uint32_t vector_size(const simple_vector* v)
  52. {
  53. if (v)
  54. return v->size;
  55. else
  56. return 0;
  57. }
  58. //push a pointer to the end of the vector
  59. //return 0 if success, return 1 if memory malloc failure.
  60. errno_t vector_push_back(simple_vector* v, const void* data)
  61. {
  62. if (v)
  63. {
  64. if (v->capacity == 0) {
  65. //first item
  66. v->data = (void**)malloc(sizeof(void*) * INIT_SIZE);
  67. if (v->data ==NULL)
  68. return 1;
  69. v->capacity = INIT_SIZE;
  70. memset(v->data, '\0', sizeof(void*) * INIT_SIZE);
  71. }
  72. else if (v->size == v->capacity) {
  73. void** new_data;
  74. if( v->capacity >= MAX_SIZE - INIT_SIZE)
  75. return 1;
  76. //increate size by INIT_SIZE
  77. new_data = (void**)realloc(v->data, sizeof(void*) *( v->capacity + INIT_SIZE));
  78. if (new_data ==NULL)
  79. return 1;
  80. memset(&new_data[v->capacity], '\0', sizeof(void*) * INIT_SIZE);
  81. v->data = new_data;
  82. v->capacity += INIT_SIZE;
  83. }
  84. //assign new item
  85. v->data[v->size] = const_cast<void*>(data);
  86. v->size++;
  87. return 0;
  88. }
  89. return 1;
  90. }
  91. //get the item pointer in the vector
  92. //return 0 if success, return 1 if index is out of range or data pointer is invalid.
  93. errno_t vector_get(const simple_vector* v, uint32_t index, void** data)
  94. {
  95. if (!v || index >= v->size || !data)
  96. return 1;
  97. //fence after boundary check
  98. sgx_lfence();
  99. *data = v->data[index];
  100. return 0;
  101. }
  102. //set the pointer in the vector
  103. //return 0 if success, return 1 if index is out of range.
  104. errno_t vector_set(simple_vector* v, uint32_t index, const void* data)
  105. {
  106. if (!v || index >= v->size)
  107. return 1;
  108. v->data[index] = const_cast<void*>(data);
  109. return 0;
  110. }
  111. //release memory used by the vector
  112. void vector_free(simple_vector* v)
  113. {
  114. if (v)
  115. {
  116. v->size = 0;
  117. v->capacity = 0;
  118. if(v->data)
  119. {
  120. free(v->data);
  121. v->data = NULL;
  122. }
  123. }
  124. }