Debian Codegraph @

src/vendor/azure-uamqp-c/deps/azure-c-shared-utility/pal/tickcounter.c

raw
1 // Copyright (c) Microsoft. All rights reserved.
2 // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3
4 #include <stdlib.h>
5 #include "azure_c_shared_utility/gballoc.h"
6
7 #include <stdint.h>
8 #include <time.h>
9 #include "azure_c_shared_utility/tickcounter.h"
10 #include "azure_c_shared_utility/optimize_size.h"
11 #include "azure_c_shared_utility/xlogging.h"
12
13 #define INVALID_TIME_VALUE (time_t)(-1)
14 typedef struct TICK_COUNTER_INSTANCE_TAG
15 {
16 time_t init_time_value;
17 tickcounter_ms_t current_ms;
18 } TICK_COUNTER_INSTANCE;
19
20 TICK_COUNTER_HANDLE tickcounter_create(void)
21 {
22 TICK_COUNTER_INSTANCE* result = (TICK_COUNTER_INSTANCE*)malloc(sizeof(TICK_COUNTER_INSTANCE));
23 if (result != NULL)
24 {
25 result->init_time_value = time(NULL);
26 if (result->init_time_value == INVALID_TIME_VALUE)
27 {
28 LogError("tickcounter failed: time return INVALID_TIME.");
29 free(result);
30 result = NULL;
31 }
32 else
33 {
34 result->current_ms = 0;
35 }
36 }
37 return result;
38 }
39
40 void tickcounter_destroy(TICK_COUNTER_HANDLE tick_counter)
41 {
42 if (tick_counter != NULL)
43 {
44 free(tick_counter);
45 }
46 }
47
48 int tickcounter_get_current_ms(TICK_COUNTER_HANDLE tick_counter, tickcounter_ms_t * current_ms)
49 {
50 int result;
51
52 if (tick_counter == NULL || current_ms == NULL)
53 {
54 LogError("tickcounter failed: Invalid Arguments.");
55 result = MU_FAILURE;
56 }
57 else
58 {
59 time_t time_value = time(NULL);
60 if (time_value == INVALID_TIME_VALUE)
61 {
62 result = MU_FAILURE;
63 }
64 else
65 {
66 TICK_COUNTER_INSTANCE* tick_counter_instance = (TICK_COUNTER_INSTANCE*)tick_counter;
67 tick_counter_instance->current_ms = (tickcounter_ms_t)(difftime(time_value, tick_counter_instance->init_time_value) * 1000);
68 *current_ms = tick_counter_instance->current_ms;
69 result = 0;
70 }
71 }
72
73 return result;
74 }