WIP. send a large constant string twice a second, in order to test out the transport with something indicative of our required load.
Dependencies: FXOS8700CQ NTPClient azure_umqtt_c iothub_mqtt_transport mbed-rtos mbed wolfSSL Socket lwip-eth lwip-sys lwip
Fork of FXOS8700CQ_To_Azure_IoT by
azure_c_shared_utility/singlylinkedlist.c@3:c0556ff7b8e3, 2016-12-08 (annotated)
- Committer:
- markrad
- Date:
- Thu Dec 08 00:11:40 2016 +0000
- Revision:
- 3:c0556ff7b8e3
Hack the code to get restart working
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
markrad | 3:c0556ff7b8e3 | 1 | // Copyright (c) Microsoft. All rights reserved. |
markrad | 3:c0556ff7b8e3 | 2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information. |
markrad | 3:c0556ff7b8e3 | 3 | |
markrad | 3:c0556ff7b8e3 | 4 | #include <stdlib.h> |
markrad | 3:c0556ff7b8e3 | 5 | #ifdef _CRTDBG_MAP_ALLOC |
markrad | 3:c0556ff7b8e3 | 6 | #include <crtdbg.h> |
markrad | 3:c0556ff7b8e3 | 7 | #endif |
markrad | 3:c0556ff7b8e3 | 8 | #include <stdbool.h> |
markrad | 3:c0556ff7b8e3 | 9 | |
markrad | 3:c0556ff7b8e3 | 10 | #include "azure_c_shared_utility/gballoc.h" |
markrad | 3:c0556ff7b8e3 | 11 | #include "azure_c_shared_utility/singlylinkedlist.h" |
markrad | 3:c0556ff7b8e3 | 12 | |
markrad | 3:c0556ff7b8e3 | 13 | typedef struct LIST_ITEM_INSTANCE_TAG |
markrad | 3:c0556ff7b8e3 | 14 | { |
markrad | 3:c0556ff7b8e3 | 15 | const void* item; |
markrad | 3:c0556ff7b8e3 | 16 | void* next; |
markrad | 3:c0556ff7b8e3 | 17 | } LIST_ITEM_INSTANCE; |
markrad | 3:c0556ff7b8e3 | 18 | |
markrad | 3:c0556ff7b8e3 | 19 | typedef struct SINGLYLINKEDLIST_INSTANCE_TAG |
markrad | 3:c0556ff7b8e3 | 20 | { |
markrad | 3:c0556ff7b8e3 | 21 | LIST_ITEM_INSTANCE* head; |
markrad | 3:c0556ff7b8e3 | 22 | } LIST_INSTANCE; |
markrad | 3:c0556ff7b8e3 | 23 | |
markrad | 3:c0556ff7b8e3 | 24 | SINGLYLINKEDLIST_HANDLE singlylinkedlist_create(void) |
markrad | 3:c0556ff7b8e3 | 25 | { |
markrad | 3:c0556ff7b8e3 | 26 | LIST_INSTANCE* result; |
markrad | 3:c0556ff7b8e3 | 27 | |
markrad | 3:c0556ff7b8e3 | 28 | /* Codes_SRS_LIST_01_001: [singlylinkedlist_create shall create a new list and return a non-NULL handle on success.] */ |
markrad | 3:c0556ff7b8e3 | 29 | result = (LIST_INSTANCE*)malloc(sizeof(LIST_INSTANCE)); |
markrad | 3:c0556ff7b8e3 | 30 | if (result != NULL) |
markrad | 3:c0556ff7b8e3 | 31 | { |
markrad | 3:c0556ff7b8e3 | 32 | /* Codes_SRS_LIST_01_002: [If any error occurs during the list creation, singlylinkedlist_create shall return NULL.] */ |
markrad | 3:c0556ff7b8e3 | 33 | result->head = NULL; |
markrad | 3:c0556ff7b8e3 | 34 | } |
markrad | 3:c0556ff7b8e3 | 35 | |
markrad | 3:c0556ff7b8e3 | 36 | return result; |
markrad | 3:c0556ff7b8e3 | 37 | } |
markrad | 3:c0556ff7b8e3 | 38 | |
markrad | 3:c0556ff7b8e3 | 39 | void singlylinkedlist_destroy(SINGLYLINKEDLIST_HANDLE list) |
markrad | 3:c0556ff7b8e3 | 40 | { |
markrad | 3:c0556ff7b8e3 | 41 | /* Codes_SRS_LIST_01_004: [If the list argument is NULL, no freeing of resources shall occur.] */ |
markrad | 3:c0556ff7b8e3 | 42 | if (list != NULL) |
markrad | 3:c0556ff7b8e3 | 43 | { |
markrad | 3:c0556ff7b8e3 | 44 | LIST_INSTANCE* list_instance = (LIST_INSTANCE*)list; |
markrad | 3:c0556ff7b8e3 | 45 | |
markrad | 3:c0556ff7b8e3 | 46 | while (list_instance->head != NULL) |
markrad | 3:c0556ff7b8e3 | 47 | { |
markrad | 3:c0556ff7b8e3 | 48 | LIST_ITEM_INSTANCE* current_item = list_instance->head; |
markrad | 3:c0556ff7b8e3 | 49 | list_instance->head = (LIST_ITEM_INSTANCE*)current_item->next; |
markrad | 3:c0556ff7b8e3 | 50 | free(current_item); |
markrad | 3:c0556ff7b8e3 | 51 | } |
markrad | 3:c0556ff7b8e3 | 52 | |
markrad | 3:c0556ff7b8e3 | 53 | /* Codes_SRS_LIST_01_003: [singlylinkedlist_destroy shall free all resources associated with the list identified by the handle argument.] */ |
markrad | 3:c0556ff7b8e3 | 54 | free(list_instance); |
markrad | 3:c0556ff7b8e3 | 55 | } |
markrad | 3:c0556ff7b8e3 | 56 | } |
markrad | 3:c0556ff7b8e3 | 57 | |
markrad | 3:c0556ff7b8e3 | 58 | LIST_ITEM_HANDLE singlylinkedlist_add(SINGLYLINKEDLIST_HANDLE list, const void* item) |
markrad | 3:c0556ff7b8e3 | 59 | { |
markrad | 3:c0556ff7b8e3 | 60 | LIST_ITEM_INSTANCE* result; |
markrad | 3:c0556ff7b8e3 | 61 | |
markrad | 3:c0556ff7b8e3 | 62 | /* Codes_SRS_LIST_01_006: [If any of the arguments is NULL, singlylinkedlist_add shall not add the item to the list and return NULL.] */ |
markrad | 3:c0556ff7b8e3 | 63 | if ((list == NULL) || |
markrad | 3:c0556ff7b8e3 | 64 | (item == NULL)) |
markrad | 3:c0556ff7b8e3 | 65 | { |
markrad | 3:c0556ff7b8e3 | 66 | result = NULL; |
markrad | 3:c0556ff7b8e3 | 67 | } |
markrad | 3:c0556ff7b8e3 | 68 | else |
markrad | 3:c0556ff7b8e3 | 69 | { |
markrad | 3:c0556ff7b8e3 | 70 | LIST_INSTANCE* list_instance = (LIST_INSTANCE*)list; |
markrad | 3:c0556ff7b8e3 | 71 | result = (LIST_ITEM_INSTANCE*)malloc(sizeof(LIST_ITEM_INSTANCE)); |
markrad | 3:c0556ff7b8e3 | 72 | |
markrad | 3:c0556ff7b8e3 | 73 | if (result == NULL) |
markrad | 3:c0556ff7b8e3 | 74 | { |
markrad | 3:c0556ff7b8e3 | 75 | /* Codes_SRS_LIST_01_007: [If allocating the new list node fails, singlylinkedlist_add shall return NULL.] */ |
markrad | 3:c0556ff7b8e3 | 76 | result = NULL; |
markrad | 3:c0556ff7b8e3 | 77 | } |
markrad | 3:c0556ff7b8e3 | 78 | else |
markrad | 3:c0556ff7b8e3 | 79 | { |
markrad | 3:c0556ff7b8e3 | 80 | /* Codes_SRS_LIST_01_005: [singlylinkedlist_add shall add one item to the tail of the list and on success it shall return a handle to the added item.] */ |
markrad | 3:c0556ff7b8e3 | 81 | result->next = NULL; |
markrad | 3:c0556ff7b8e3 | 82 | result->item = item; |
markrad | 3:c0556ff7b8e3 | 83 | |
markrad | 3:c0556ff7b8e3 | 84 | if (list_instance->head == NULL) |
markrad | 3:c0556ff7b8e3 | 85 | { |
markrad | 3:c0556ff7b8e3 | 86 | list_instance->head = result; |
markrad | 3:c0556ff7b8e3 | 87 | } |
markrad | 3:c0556ff7b8e3 | 88 | else |
markrad | 3:c0556ff7b8e3 | 89 | { |
markrad | 3:c0556ff7b8e3 | 90 | LIST_ITEM_INSTANCE* current = list_instance->head; |
markrad | 3:c0556ff7b8e3 | 91 | while (current->next != NULL) |
markrad | 3:c0556ff7b8e3 | 92 | { |
markrad | 3:c0556ff7b8e3 | 93 | current = (LIST_ITEM_INSTANCE*)current->next; |
markrad | 3:c0556ff7b8e3 | 94 | } |
markrad | 3:c0556ff7b8e3 | 95 | |
markrad | 3:c0556ff7b8e3 | 96 | current->next = result; |
markrad | 3:c0556ff7b8e3 | 97 | } |
markrad | 3:c0556ff7b8e3 | 98 | } |
markrad | 3:c0556ff7b8e3 | 99 | } |
markrad | 3:c0556ff7b8e3 | 100 | |
markrad | 3:c0556ff7b8e3 | 101 | return result; |
markrad | 3:c0556ff7b8e3 | 102 | } |
markrad | 3:c0556ff7b8e3 | 103 | |
markrad | 3:c0556ff7b8e3 | 104 | int singlylinkedlist_remove(SINGLYLINKEDLIST_HANDLE list, LIST_ITEM_HANDLE item) |
markrad | 3:c0556ff7b8e3 | 105 | { |
markrad | 3:c0556ff7b8e3 | 106 | int result; |
markrad | 3:c0556ff7b8e3 | 107 | |
markrad | 3:c0556ff7b8e3 | 108 | /* Codes_SRS_LIST_01_024: [If any of the arguments list or item_handle is NULL, singlylinkedlist_remove shall fail and return a non-zero value.] */ |
markrad | 3:c0556ff7b8e3 | 109 | if ((list == NULL) || |
markrad | 3:c0556ff7b8e3 | 110 | (item == NULL)) |
markrad | 3:c0556ff7b8e3 | 111 | { |
markrad | 3:c0556ff7b8e3 | 112 | result = __LINE__; |
markrad | 3:c0556ff7b8e3 | 113 | } |
markrad | 3:c0556ff7b8e3 | 114 | else |
markrad | 3:c0556ff7b8e3 | 115 | { |
markrad | 3:c0556ff7b8e3 | 116 | LIST_INSTANCE* list_instance = (LIST_INSTANCE*)list; |
markrad | 3:c0556ff7b8e3 | 117 | LIST_ITEM_INSTANCE* current_item = list_instance->head; |
markrad | 3:c0556ff7b8e3 | 118 | LIST_ITEM_INSTANCE* previous_item = NULL; |
markrad | 3:c0556ff7b8e3 | 119 | |
markrad | 3:c0556ff7b8e3 | 120 | while (current_item != NULL) |
markrad | 3:c0556ff7b8e3 | 121 | { |
markrad | 3:c0556ff7b8e3 | 122 | if (current_item == item) |
markrad | 3:c0556ff7b8e3 | 123 | { |
markrad | 3:c0556ff7b8e3 | 124 | if (previous_item != NULL) |
markrad | 3:c0556ff7b8e3 | 125 | { |
markrad | 3:c0556ff7b8e3 | 126 | previous_item->next = current_item->next; |
markrad | 3:c0556ff7b8e3 | 127 | } |
markrad | 3:c0556ff7b8e3 | 128 | else |
markrad | 3:c0556ff7b8e3 | 129 | { |
markrad | 3:c0556ff7b8e3 | 130 | list_instance->head = (LIST_ITEM_INSTANCE*)current_item->next; |
markrad | 3:c0556ff7b8e3 | 131 | } |
markrad | 3:c0556ff7b8e3 | 132 | |
markrad | 3:c0556ff7b8e3 | 133 | free(current_item); |
markrad | 3:c0556ff7b8e3 | 134 | |
markrad | 3:c0556ff7b8e3 | 135 | break; |
markrad | 3:c0556ff7b8e3 | 136 | } |
markrad | 3:c0556ff7b8e3 | 137 | previous_item = current_item; |
markrad | 3:c0556ff7b8e3 | 138 | current_item = (LIST_ITEM_INSTANCE*)current_item->next; |
markrad | 3:c0556ff7b8e3 | 139 | } |
markrad | 3:c0556ff7b8e3 | 140 | |
markrad | 3:c0556ff7b8e3 | 141 | if (current_item == NULL) |
markrad | 3:c0556ff7b8e3 | 142 | { |
markrad | 3:c0556ff7b8e3 | 143 | /* Codes_SRS_LIST_01_025: [If the item item_handle is not found in the list, then singlylinkedlist_remove shall fail and return a non-zero value.] */ |
markrad | 3:c0556ff7b8e3 | 144 | result = __LINE__; |
markrad | 3:c0556ff7b8e3 | 145 | } |
markrad | 3:c0556ff7b8e3 | 146 | else |
markrad | 3:c0556ff7b8e3 | 147 | { |
markrad | 3:c0556ff7b8e3 | 148 | /* Codes_SRS_LIST_01_023: [singlylinkedlist_remove shall remove a list item from the list and on success it shall return 0.] */ |
markrad | 3:c0556ff7b8e3 | 149 | result = 0; |
markrad | 3:c0556ff7b8e3 | 150 | } |
markrad | 3:c0556ff7b8e3 | 151 | } |
markrad | 3:c0556ff7b8e3 | 152 | |
markrad | 3:c0556ff7b8e3 | 153 | return result; |
markrad | 3:c0556ff7b8e3 | 154 | } |
markrad | 3:c0556ff7b8e3 | 155 | |
markrad | 3:c0556ff7b8e3 | 156 | LIST_ITEM_HANDLE singlylinkedlist_get_head_item(SINGLYLINKEDLIST_HANDLE list) |
markrad | 3:c0556ff7b8e3 | 157 | { |
markrad | 3:c0556ff7b8e3 | 158 | LIST_ITEM_HANDLE result; |
markrad | 3:c0556ff7b8e3 | 159 | |
markrad | 3:c0556ff7b8e3 | 160 | if (list == NULL) |
markrad | 3:c0556ff7b8e3 | 161 | { |
markrad | 3:c0556ff7b8e3 | 162 | /* Codes_SRS_LIST_01_009: [If the list argument is NULL, singlylinkedlist_get_head_item shall return NULL.] */ |
markrad | 3:c0556ff7b8e3 | 163 | result = NULL; |
markrad | 3:c0556ff7b8e3 | 164 | } |
markrad | 3:c0556ff7b8e3 | 165 | else |
markrad | 3:c0556ff7b8e3 | 166 | { |
markrad | 3:c0556ff7b8e3 | 167 | LIST_INSTANCE* list_instance = (LIST_INSTANCE*)list; |
markrad | 3:c0556ff7b8e3 | 168 | |
markrad | 3:c0556ff7b8e3 | 169 | /* Codes_SRS_LIST_01_008: [singlylinkedlist_get_head_item shall return the head of the list.] */ |
markrad | 3:c0556ff7b8e3 | 170 | /* Codes_SRS_LIST_01_010: [If the list is empty, singlylinkedlist_get_head_item_shall_return NULL.] */ |
markrad | 3:c0556ff7b8e3 | 171 | result = list_instance->head; |
markrad | 3:c0556ff7b8e3 | 172 | } |
markrad | 3:c0556ff7b8e3 | 173 | |
markrad | 3:c0556ff7b8e3 | 174 | return result; |
markrad | 3:c0556ff7b8e3 | 175 | } |
markrad | 3:c0556ff7b8e3 | 176 | |
markrad | 3:c0556ff7b8e3 | 177 | LIST_ITEM_HANDLE singlylinkedlist_get_next_item(LIST_ITEM_HANDLE item_handle) |
markrad | 3:c0556ff7b8e3 | 178 | { |
markrad | 3:c0556ff7b8e3 | 179 | LIST_ITEM_HANDLE result; |
markrad | 3:c0556ff7b8e3 | 180 | |
markrad | 3:c0556ff7b8e3 | 181 | if (item_handle == NULL) |
markrad | 3:c0556ff7b8e3 | 182 | { |
markrad | 3:c0556ff7b8e3 | 183 | /* Codes_SRS_LIST_01_019: [If item_handle is NULL then singlylinkedlist_get_next_item shall return NULL.] */ |
markrad | 3:c0556ff7b8e3 | 184 | result = NULL; |
markrad | 3:c0556ff7b8e3 | 185 | } |
markrad | 3:c0556ff7b8e3 | 186 | else |
markrad | 3:c0556ff7b8e3 | 187 | { |
markrad | 3:c0556ff7b8e3 | 188 | /* Codes_SRS_LIST_01_018: [singlylinkedlist_get_next_item shall return the next item in the list following the item item_handle.] */ |
markrad | 3:c0556ff7b8e3 | 189 | result = (LIST_ITEM_HANDLE)((LIST_ITEM_INSTANCE*)item_handle)->next; |
markrad | 3:c0556ff7b8e3 | 190 | } |
markrad | 3:c0556ff7b8e3 | 191 | |
markrad | 3:c0556ff7b8e3 | 192 | return result; |
markrad | 3:c0556ff7b8e3 | 193 | } |
markrad | 3:c0556ff7b8e3 | 194 | |
markrad | 3:c0556ff7b8e3 | 195 | const void* singlylinkedlist_item_get_value(LIST_ITEM_HANDLE item_handle) |
markrad | 3:c0556ff7b8e3 | 196 | { |
markrad | 3:c0556ff7b8e3 | 197 | const void* result; |
markrad | 3:c0556ff7b8e3 | 198 | |
markrad | 3:c0556ff7b8e3 | 199 | if (item_handle == NULL) |
markrad | 3:c0556ff7b8e3 | 200 | { |
markrad | 3:c0556ff7b8e3 | 201 | /* Codes_SRS_LIST_01_021: [If item_handle is NULL, singlylinkedlist_item_get_value shall return NULL.] */ |
markrad | 3:c0556ff7b8e3 | 202 | result = NULL; |
markrad | 3:c0556ff7b8e3 | 203 | } |
markrad | 3:c0556ff7b8e3 | 204 | else |
markrad | 3:c0556ff7b8e3 | 205 | { |
markrad | 3:c0556ff7b8e3 | 206 | /* Codes_SRS_LIST_01_020: [singlylinkedlist_item_get_value shall return the value associated with the list item identified by the item_handle argument.] */ |
markrad | 3:c0556ff7b8e3 | 207 | result = ((LIST_ITEM_INSTANCE*)item_handle)->item; |
markrad | 3:c0556ff7b8e3 | 208 | } |
markrad | 3:c0556ff7b8e3 | 209 | |
markrad | 3:c0556ff7b8e3 | 210 | return result; |
markrad | 3:c0556ff7b8e3 | 211 | } |
markrad | 3:c0556ff7b8e3 | 212 | |
markrad | 3:c0556ff7b8e3 | 213 | LIST_ITEM_HANDLE singlylinkedlist_find(SINGLYLINKEDLIST_HANDLE list, LIST_MATCH_FUNCTION match_function, const void* match_context) |
markrad | 3:c0556ff7b8e3 | 214 | { |
markrad | 3:c0556ff7b8e3 | 215 | LIST_ITEM_HANDLE result; |
markrad | 3:c0556ff7b8e3 | 216 | |
markrad | 3:c0556ff7b8e3 | 217 | if ((list == NULL) || |
markrad | 3:c0556ff7b8e3 | 218 | (match_function == NULL)) |
markrad | 3:c0556ff7b8e3 | 219 | { |
markrad | 3:c0556ff7b8e3 | 220 | /* Codes_SRS_LIST_01_012: [If the list or the match_function argument is NULL, singlylinkedlist_find shall return NULL.] */ |
markrad | 3:c0556ff7b8e3 | 221 | result = NULL; |
markrad | 3:c0556ff7b8e3 | 222 | } |
markrad | 3:c0556ff7b8e3 | 223 | else |
markrad | 3:c0556ff7b8e3 | 224 | { |
markrad | 3:c0556ff7b8e3 | 225 | LIST_INSTANCE* list_instance = (LIST_INSTANCE*)list; |
markrad | 3:c0556ff7b8e3 | 226 | LIST_ITEM_INSTANCE* current = list_instance->head; |
markrad | 3:c0556ff7b8e3 | 227 | |
markrad | 3:c0556ff7b8e3 | 228 | /* Codes_SRS_LIST_01_011: [singlylinkedlist_find shall iterate through all items in a list and return the first one that satisfies a certain match function.] */ |
markrad | 3:c0556ff7b8e3 | 229 | while (current != NULL) |
markrad | 3:c0556ff7b8e3 | 230 | { |
markrad | 3:c0556ff7b8e3 | 231 | /* Codes_SRS_LIST_01_014: [list find shall determine whether an item satisfies the match criteria by invoking the match function for each item in the list until a matching item is found.] */ |
markrad | 3:c0556ff7b8e3 | 232 | /* Codes_SRS_LIST_01_013: [The match_function shall get as arguments the list item being attempted to be matched and the match_context as is.] */ |
markrad | 3:c0556ff7b8e3 | 233 | if (match_function((LIST_ITEM_HANDLE)current, match_context) == true) |
markrad | 3:c0556ff7b8e3 | 234 | { |
markrad | 3:c0556ff7b8e3 | 235 | /* Codes_SRS_LIST_01_017: [If the match function returns true, singlylinkedlist_find shall consider that item as matching.] */ |
markrad | 3:c0556ff7b8e3 | 236 | break; |
markrad | 3:c0556ff7b8e3 | 237 | } |
markrad | 3:c0556ff7b8e3 | 238 | |
markrad | 3:c0556ff7b8e3 | 239 | /* Codes_SRS_LIST_01_016: [If the match function returns false, singlylinkedlist_find shall consider that item as not matching.] */ |
markrad | 3:c0556ff7b8e3 | 240 | current = (LIST_ITEM_INSTANCE*)current->next; |
markrad | 3:c0556ff7b8e3 | 241 | } |
markrad | 3:c0556ff7b8e3 | 242 | |
markrad | 3:c0556ff7b8e3 | 243 | if (current == NULL) |
markrad | 3:c0556ff7b8e3 | 244 | { |
markrad | 3:c0556ff7b8e3 | 245 | /* Codes_SRS_LIST_01_015: [If the list is empty, singlylinkedlist_find shall return NULL.] */ |
markrad | 3:c0556ff7b8e3 | 246 | result = NULL; |
markrad | 3:c0556ff7b8e3 | 247 | } |
markrad | 3:c0556ff7b8e3 | 248 | else |
markrad | 3:c0556ff7b8e3 | 249 | { |
markrad | 3:c0556ff7b8e3 | 250 | result = current; |
markrad | 3:c0556ff7b8e3 | 251 | } |
markrad | 3:c0556ff7b8e3 | 252 | } |
markrad | 3:c0556ff7b8e3 | 253 | |
markrad | 3:c0556ff7b8e3 | 254 | return result; |
markrad | 3:c0556ff7b8e3 | 255 | } |