Azure IoT common library
Dependents: STM32F746_iothub_client_sample_mqtt f767zi_mqtt iothub_client_sample_amqp iothub_client_sample_http ... more
vector.c@0:fa2de1b79154, 2016-04-08 (annotated)
- Committer:
- Azure.IoT Build
- Date:
- Fri Apr 08 12:01:36 2016 -0700
- Revision:
- 0:fa2de1b79154
- Child:
- 10:1be0bc9a9deb
1.0.4
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Azure.IoT Build | 0:fa2de1b79154 | 1 | // Copyright (c) Microsoft. All rights reserved. |
Azure.IoT Build | 0:fa2de1b79154 | 2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information. |
Azure.IoT Build | 0:fa2de1b79154 | 3 | |
Azure.IoT Build | 0:fa2de1b79154 | 4 | #include <stdlib.h> |
Azure.IoT Build | 0:fa2de1b79154 | 5 | #ifdef _CRTDBG_MAP_ALLOC |
Azure.IoT Build | 0:fa2de1b79154 | 6 | #include <crtdbg.h> |
Azure.IoT Build | 0:fa2de1b79154 | 7 | #endif |
Azure.IoT Build | 0:fa2de1b79154 | 8 | #include "azure_c_shared_utility/gballoc.h" |
Azure.IoT Build | 0:fa2de1b79154 | 9 | |
Azure.IoT Build | 0:fa2de1b79154 | 10 | #include "azure_c_shared_utility/vector.h" |
Azure.IoT Build | 0:fa2de1b79154 | 11 | #include <string.h> |
Azure.IoT Build | 0:fa2de1b79154 | 12 | |
Azure.IoT Build | 0:fa2de1b79154 | 13 | |
Azure.IoT Build | 0:fa2de1b79154 | 14 | typedef struct VECTOR_TAG |
Azure.IoT Build | 0:fa2de1b79154 | 15 | { |
Azure.IoT Build | 0:fa2de1b79154 | 16 | void* storage; |
Azure.IoT Build | 0:fa2de1b79154 | 17 | size_t count; |
Azure.IoT Build | 0:fa2de1b79154 | 18 | size_t elementSize; |
Azure.IoT Build | 0:fa2de1b79154 | 19 | } VECTOR; |
Azure.IoT Build | 0:fa2de1b79154 | 20 | |
Azure.IoT Build | 0:fa2de1b79154 | 21 | VECTOR_HANDLE VECTOR_create(size_t elementSize) |
Azure.IoT Build | 0:fa2de1b79154 | 22 | { |
Azure.IoT Build | 0:fa2de1b79154 | 23 | VECTOR_HANDLE result; |
Azure.IoT Build | 0:fa2de1b79154 | 24 | |
Azure.IoT Build | 0:fa2de1b79154 | 25 | VECTOR* vec = (VECTOR*)malloc(sizeof(VECTOR)); |
Azure.IoT Build | 0:fa2de1b79154 | 26 | if (vec == NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 27 | { |
Azure.IoT Build | 0:fa2de1b79154 | 28 | result = NULL; |
Azure.IoT Build | 0:fa2de1b79154 | 29 | } |
Azure.IoT Build | 0:fa2de1b79154 | 30 | else |
Azure.IoT Build | 0:fa2de1b79154 | 31 | { |
Azure.IoT Build | 0:fa2de1b79154 | 32 | vec->storage = NULL; |
Azure.IoT Build | 0:fa2de1b79154 | 33 | vec->count = 0; |
Azure.IoT Build | 0:fa2de1b79154 | 34 | vec->elementSize = elementSize; |
Azure.IoT Build | 0:fa2de1b79154 | 35 | result = (VECTOR_HANDLE)vec; |
Azure.IoT Build | 0:fa2de1b79154 | 36 | } |
Azure.IoT Build | 0:fa2de1b79154 | 37 | return result; |
Azure.IoT Build | 0:fa2de1b79154 | 38 | } |
Azure.IoT Build | 0:fa2de1b79154 | 39 | |
Azure.IoT Build | 0:fa2de1b79154 | 40 | static void internal_VECTOR_clear(VECTOR* vec) |
Azure.IoT Build | 0:fa2de1b79154 | 41 | { |
Azure.IoT Build | 0:fa2de1b79154 | 42 | if (vec->storage != NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 43 | { |
Azure.IoT Build | 0:fa2de1b79154 | 44 | free(vec->storage); |
Azure.IoT Build | 0:fa2de1b79154 | 45 | vec->storage = NULL; |
Azure.IoT Build | 0:fa2de1b79154 | 46 | } |
Azure.IoT Build | 0:fa2de1b79154 | 47 | vec->count = 0; |
Azure.IoT Build | 0:fa2de1b79154 | 48 | } |
Azure.IoT Build | 0:fa2de1b79154 | 49 | |
Azure.IoT Build | 0:fa2de1b79154 | 50 | void VECTOR_destroy(VECTOR_HANDLE handle) |
Azure.IoT Build | 0:fa2de1b79154 | 51 | { |
Azure.IoT Build | 0:fa2de1b79154 | 52 | if (handle != NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 53 | { |
Azure.IoT Build | 0:fa2de1b79154 | 54 | VECTOR* vec = (VECTOR*)handle; |
Azure.IoT Build | 0:fa2de1b79154 | 55 | internal_VECTOR_clear(vec); |
Azure.IoT Build | 0:fa2de1b79154 | 56 | free(vec); |
Azure.IoT Build | 0:fa2de1b79154 | 57 | } |
Azure.IoT Build | 0:fa2de1b79154 | 58 | } |
Azure.IoT Build | 0:fa2de1b79154 | 59 | |
Azure.IoT Build | 0:fa2de1b79154 | 60 | /* insertion */ |
Azure.IoT Build | 0:fa2de1b79154 | 61 | int VECTOR_push_back(VECTOR_HANDLE handle, const void* elements, size_t numElements) |
Azure.IoT Build | 0:fa2de1b79154 | 62 | { |
Azure.IoT Build | 0:fa2de1b79154 | 63 | int result; |
Azure.IoT Build | 0:fa2de1b79154 | 64 | if (handle == NULL || elements == NULL || numElements == 0) |
Azure.IoT Build | 0:fa2de1b79154 | 65 | { |
Azure.IoT Build | 0:fa2de1b79154 | 66 | result = __LINE__; |
Azure.IoT Build | 0:fa2de1b79154 | 67 | } |
Azure.IoT Build | 0:fa2de1b79154 | 68 | else |
Azure.IoT Build | 0:fa2de1b79154 | 69 | { |
Azure.IoT Build | 0:fa2de1b79154 | 70 | VECTOR* vec = (VECTOR*)handle; |
Azure.IoT Build | 0:fa2de1b79154 | 71 | const size_t curSize = vec->elementSize * vec->count; |
Azure.IoT Build | 0:fa2de1b79154 | 72 | const size_t appendSize = vec->elementSize * numElements; |
Azure.IoT Build | 0:fa2de1b79154 | 73 | |
Azure.IoT Build | 0:fa2de1b79154 | 74 | void* temp = realloc(vec->storage, curSize + appendSize); |
Azure.IoT Build | 0:fa2de1b79154 | 75 | if (temp == NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 76 | { |
Azure.IoT Build | 0:fa2de1b79154 | 77 | result = __LINE__; |
Azure.IoT Build | 0:fa2de1b79154 | 78 | } |
Azure.IoT Build | 0:fa2de1b79154 | 79 | else |
Azure.IoT Build | 0:fa2de1b79154 | 80 | { |
Azure.IoT Build | 0:fa2de1b79154 | 81 | memcpy((unsigned char*)temp + curSize, elements, appendSize); |
Azure.IoT Build | 0:fa2de1b79154 | 82 | vec->storage = temp; |
Azure.IoT Build | 0:fa2de1b79154 | 83 | vec->count += numElements; |
Azure.IoT Build | 0:fa2de1b79154 | 84 | result = 0; |
Azure.IoT Build | 0:fa2de1b79154 | 85 | } |
Azure.IoT Build | 0:fa2de1b79154 | 86 | } |
Azure.IoT Build | 0:fa2de1b79154 | 87 | return result; |
Azure.IoT Build | 0:fa2de1b79154 | 88 | } |
Azure.IoT Build | 0:fa2de1b79154 | 89 | |
Azure.IoT Build | 0:fa2de1b79154 | 90 | /* removal */ |
Azure.IoT Build | 0:fa2de1b79154 | 91 | void VECTOR_erase(VECTOR_HANDLE handle, void* elements, size_t numElements) |
Azure.IoT Build | 0:fa2de1b79154 | 92 | { |
Azure.IoT Build | 0:fa2de1b79154 | 93 | if (handle != NULL && elements != NULL && numElements > 0) |
Azure.IoT Build | 0:fa2de1b79154 | 94 | { |
Azure.IoT Build | 0:fa2de1b79154 | 95 | VECTOR* vec = (VECTOR*)handle; |
Azure.IoT Build | 0:fa2de1b79154 | 96 | unsigned char* src = (unsigned char*)elements + (vec->elementSize * numElements); |
Azure.IoT Build | 0:fa2de1b79154 | 97 | unsigned char* srcEnd = (unsigned char*)vec->storage + (vec->elementSize * vec->count); |
Azure.IoT Build | 0:fa2de1b79154 | 98 | (void)memmove(elements, src, srcEnd - src); |
Azure.IoT Build | 0:fa2de1b79154 | 99 | vec->count -= numElements; |
Azure.IoT Build | 0:fa2de1b79154 | 100 | if (vec->count == 0) |
Azure.IoT Build | 0:fa2de1b79154 | 101 | { |
Azure.IoT Build | 0:fa2de1b79154 | 102 | free(vec->storage); |
Azure.IoT Build | 0:fa2de1b79154 | 103 | vec->storage = NULL; |
Azure.IoT Build | 0:fa2de1b79154 | 104 | } |
Azure.IoT Build | 0:fa2de1b79154 | 105 | else |
Azure.IoT Build | 0:fa2de1b79154 | 106 | { |
Azure.IoT Build | 0:fa2de1b79154 | 107 | vec->storage = realloc(vec->storage, (vec->elementSize * vec->count)); |
Azure.IoT Build | 0:fa2de1b79154 | 108 | } |
Azure.IoT Build | 0:fa2de1b79154 | 109 | } |
Azure.IoT Build | 0:fa2de1b79154 | 110 | } |
Azure.IoT Build | 0:fa2de1b79154 | 111 | |
Azure.IoT Build | 0:fa2de1b79154 | 112 | void VECTOR_clear(VECTOR_HANDLE handle) |
Azure.IoT Build | 0:fa2de1b79154 | 113 | { |
Azure.IoT Build | 0:fa2de1b79154 | 114 | if (handle != NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 115 | { |
Azure.IoT Build | 0:fa2de1b79154 | 116 | VECTOR* vec = (VECTOR*)handle; |
Azure.IoT Build | 0:fa2de1b79154 | 117 | internal_VECTOR_clear(vec); |
Azure.IoT Build | 0:fa2de1b79154 | 118 | } |
Azure.IoT Build | 0:fa2de1b79154 | 119 | } |
Azure.IoT Build | 0:fa2de1b79154 | 120 | |
Azure.IoT Build | 0:fa2de1b79154 | 121 | /* access */ |
Azure.IoT Build | 0:fa2de1b79154 | 122 | |
Azure.IoT Build | 0:fa2de1b79154 | 123 | void* VECTOR_element(const VECTOR_HANDLE handle, size_t index) |
Azure.IoT Build | 0:fa2de1b79154 | 124 | { |
Azure.IoT Build | 0:fa2de1b79154 | 125 | void* result = NULL; |
Azure.IoT Build | 0:fa2de1b79154 | 126 | if (handle != NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 127 | { |
Azure.IoT Build | 0:fa2de1b79154 | 128 | const VECTOR* vec = (const VECTOR*)handle; |
Azure.IoT Build | 0:fa2de1b79154 | 129 | if (index <= vec->count) |
Azure.IoT Build | 0:fa2de1b79154 | 130 | { |
Azure.IoT Build | 0:fa2de1b79154 | 131 | result = (unsigned char*)vec->storage + (vec->elementSize * index); |
Azure.IoT Build | 0:fa2de1b79154 | 132 | } |
Azure.IoT Build | 0:fa2de1b79154 | 133 | } |
Azure.IoT Build | 0:fa2de1b79154 | 134 | return result; |
Azure.IoT Build | 0:fa2de1b79154 | 135 | } |
Azure.IoT Build | 0:fa2de1b79154 | 136 | |
Azure.IoT Build | 0:fa2de1b79154 | 137 | void* VECTOR_front(const VECTOR_HANDLE handle) |
Azure.IoT Build | 0:fa2de1b79154 | 138 | { |
Azure.IoT Build | 0:fa2de1b79154 | 139 | void* result = NULL; |
Azure.IoT Build | 0:fa2de1b79154 | 140 | if (handle != NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 141 | { |
Azure.IoT Build | 0:fa2de1b79154 | 142 | const VECTOR* vec = (const VECTOR*)handle; |
Azure.IoT Build | 0:fa2de1b79154 | 143 | result = vec->storage; |
Azure.IoT Build | 0:fa2de1b79154 | 144 | } |
Azure.IoT Build | 0:fa2de1b79154 | 145 | return result; |
Azure.IoT Build | 0:fa2de1b79154 | 146 | } |
Azure.IoT Build | 0:fa2de1b79154 | 147 | |
Azure.IoT Build | 0:fa2de1b79154 | 148 | void* VECTOR_back(const VECTOR_HANDLE handle) |
Azure.IoT Build | 0:fa2de1b79154 | 149 | { |
Azure.IoT Build | 0:fa2de1b79154 | 150 | void* result = NULL; |
Azure.IoT Build | 0:fa2de1b79154 | 151 | if (handle != NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 152 | { |
Azure.IoT Build | 0:fa2de1b79154 | 153 | const VECTOR* vec = (const VECTOR*)handle; |
Azure.IoT Build | 0:fa2de1b79154 | 154 | result = (unsigned char*)vec->storage + (vec->elementSize * (vec->count - 1)); |
Azure.IoT Build | 0:fa2de1b79154 | 155 | } |
Azure.IoT Build | 0:fa2de1b79154 | 156 | return result; |
Azure.IoT Build | 0:fa2de1b79154 | 157 | } |
Azure.IoT Build | 0:fa2de1b79154 | 158 | |
Azure.IoT Build | 0:fa2de1b79154 | 159 | void* VECTOR_find_if(const VECTOR_HANDLE handle, PREDICATE_FUNCTION pred, const void* value) |
Azure.IoT Build | 0:fa2de1b79154 | 160 | { |
Azure.IoT Build | 0:fa2de1b79154 | 161 | void* result = NULL; |
Azure.IoT Build | 0:fa2de1b79154 | 162 | size_t i; |
Azure.IoT Build | 0:fa2de1b79154 | 163 | VECTOR* handleData = (VECTOR*)handle; |
Azure.IoT Build | 0:fa2de1b79154 | 164 | if (handle != NULL && pred != NULL && value != NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 165 | { |
Azure.IoT Build | 0:fa2de1b79154 | 166 | for (i = 0; i < handleData->count; ++i) |
Azure.IoT Build | 0:fa2de1b79154 | 167 | { |
Azure.IoT Build | 0:fa2de1b79154 | 168 | void* elem = (unsigned char*)handleData->storage + (handleData->elementSize * i); |
Azure.IoT Build | 0:fa2de1b79154 | 169 | if (!!pred(elem, value)) |
Azure.IoT Build | 0:fa2de1b79154 | 170 | { |
Azure.IoT Build | 0:fa2de1b79154 | 171 | result = elem; |
Azure.IoT Build | 0:fa2de1b79154 | 172 | break; |
Azure.IoT Build | 0:fa2de1b79154 | 173 | } |
Azure.IoT Build | 0:fa2de1b79154 | 174 | } |
Azure.IoT Build | 0:fa2de1b79154 | 175 | } |
Azure.IoT Build | 0:fa2de1b79154 | 176 | return result; |
Azure.IoT Build | 0:fa2de1b79154 | 177 | } |
Azure.IoT Build | 0:fa2de1b79154 | 178 | |
Azure.IoT Build | 0:fa2de1b79154 | 179 | /* capacity */ |
Azure.IoT Build | 0:fa2de1b79154 | 180 | |
Azure.IoT Build | 0:fa2de1b79154 | 181 | size_t VECTOR_size(const VECTOR_HANDLE handle) |
Azure.IoT Build | 0:fa2de1b79154 | 182 | { |
Azure.IoT Build | 0:fa2de1b79154 | 183 | size_t result = 0; |
Azure.IoT Build | 0:fa2de1b79154 | 184 | if (handle != NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 185 | { |
Azure.IoT Build | 0:fa2de1b79154 | 186 | const VECTOR* vec = (const VECTOR*)handle; |
Azure.IoT Build | 0:fa2de1b79154 | 187 | result = vec->count; |
Azure.IoT Build | 0:fa2de1b79154 | 188 | } |
Azure.IoT Build | 0:fa2de1b79154 | 189 | return result; |
Azure.IoT Build | 0:fa2de1b79154 | 190 | } |