diff options
author | iphydf <iphydf@users.noreply.github.com> | 2018-01-13 16:50:32 +0000 |
---|---|---|
committer | iphydf <iphydf@users.noreply.github.com> | 2018-01-13 23:55:08 +0000 |
commit | e775b5533b7137b18edcd6e414fd461d07018425 (patch) | |
tree | cab52d97c0e02d5d2f17b8db1da3ab3e800fb3ce /toxcore/ping_array.c | |
parent | 1e1efec34ad820fc65dcf98581a0d8f6fa1873a8 (diff) |
Make Ping_Array a module-private type.
Diffstat (limited to 'toxcore/ping_array.c')
-rw-r--r-- | toxcore/ping_array.c | 105 |
1 files changed, 64 insertions, 41 deletions
diff --git a/toxcore/ping_array.c b/toxcore/ping_array.c index ea3e5101..eafedc77 100644 --- a/toxcore/ping_array.c +++ b/toxcore/ping_array.c | |||
@@ -30,6 +30,55 @@ | |||
30 | #include "crypto_core.h" | 30 | #include "crypto_core.h" |
31 | #include "util.h" | 31 | #include "util.h" |
32 | 32 | ||
33 | |||
34 | typedef struct { | ||
35 | void *data; | ||
36 | uint32_t length; | ||
37 | uint64_t time; | ||
38 | uint64_t ping_id; | ||
39 | } Ping_Array_Entry; | ||
40 | |||
41 | struct Ping_Array { | ||
42 | Ping_Array_Entry *entries; | ||
43 | |||
44 | uint32_t last_deleted; /* number representing the next entry to be deleted. */ | ||
45 | uint32_t last_added; /* number representing the last entry to be added. */ | ||
46 | uint32_t total_size; /* The length of entries */ | ||
47 | uint32_t timeout; /* The timeout after which entries are cleared. */ | ||
48 | }; | ||
49 | |||
50 | /* Initialize a Ping_Array. | ||
51 | * size represents the total size of the array and should be a power of 2. | ||
52 | * timeout represents the maximum timeout in seconds for the entry. | ||
53 | * | ||
54 | * return 0 on success. | ||
55 | * return -1 on failure. | ||
56 | */ | ||
57 | Ping_Array *ping_array_new(uint32_t size, uint32_t timeout) | ||
58 | { | ||
59 | if (size == 0 || timeout == 0) { | ||
60 | return NULL; | ||
61 | } | ||
62 | |||
63 | Ping_Array *empty_array = (Ping_Array *)calloc(1, sizeof(Ping_Array)); | ||
64 | |||
65 | if (empty_array == NULL) { | ||
66 | return NULL; | ||
67 | } | ||
68 | |||
69 | empty_array->entries = (Ping_Array_Entry *)calloc(size, sizeof(Ping_Array_Entry)); | ||
70 | |||
71 | if (empty_array->entries == NULL) { | ||
72 | free(empty_array); | ||
73 | return NULL; | ||
74 | } | ||
75 | |||
76 | empty_array->last_deleted = empty_array->last_added = 0; | ||
77 | empty_array->total_size = size; | ||
78 | empty_array->timeout = timeout; | ||
79 | return empty_array; | ||
80 | } | ||
81 | |||
33 | static void clear_entry(Ping_Array *array, uint32_t index) | 82 | static void clear_entry(Ping_Array *array, uint32_t index) |
34 | { | 83 | { |
35 | free(array->entries[index].data); | 84 | free(array->entries[index].data); |
@@ -39,6 +88,20 @@ static void clear_entry(Ping_Array *array, uint32_t index) | |||
39 | array->entries[index].ping_id = 0; | 88 | array->entries[index].ping_id = 0; |
40 | } | 89 | } |
41 | 90 | ||
91 | /* Free all the allocated memory in a Ping_Array. | ||
92 | */ | ||
93 | void ping_array_kill(Ping_Array *array) | ||
94 | { | ||
95 | while (array->last_deleted != array->last_added) { | ||
96 | uint32_t index = array->last_deleted % array->total_size; | ||
97 | clear_entry(array, index); | ||
98 | ++array->last_deleted; | ||
99 | } | ||
100 | |||
101 | free(array->entries); | ||
102 | free(array); | ||
103 | } | ||
104 | |||
42 | /* Clear timed out entries. | 105 | /* Clear timed out entries. |
43 | */ | 106 | */ |
44 | static void ping_array_clear_timedout(Ping_Array *array) | 107 | static void ping_array_clear_timedout(Ping_Array *array) |
@@ -101,7 +164,7 @@ uint64_t ping_array_add(Ping_Array *array, const uint8_t *data, uint32_t length) | |||
101 | * return length of data copied on success. | 164 | * return length of data copied on success. |
102 | * return -1 on failure. | 165 | * return -1 on failure. |
103 | */ | 166 | */ |
104 | int ping_array_check(uint8_t *data, uint32_t length, Ping_Array *array, uint64_t ping_id) | 167 | int32_t ping_array_check(Ping_Array *array, uint8_t *data, size_t length, uint64_t ping_id) |
105 | { | 168 | { |
106 | if (ping_id == 0) { | 169 | if (ping_id == 0) { |
107 | return -1; | 170 | return -1; |
@@ -130,43 +193,3 @@ int ping_array_check(uint8_t *data, uint32_t length, Ping_Array *array, uint64_t | |||
130 | clear_entry(array, index); | 193 | clear_entry(array, index); |
131 | return len; | 194 | return len; |
132 | } | 195 | } |
133 | |||
134 | /* Initialize a Ping_Array. | ||
135 | * size represents the total size of the array and should be a power of 2. | ||
136 | * timeout represents the maximum timeout in seconds for the entry. | ||
137 | * | ||
138 | * return 0 on success. | ||
139 | * return -1 on failure. | ||
140 | */ | ||
141 | int ping_array_init(Ping_Array *empty_array, uint32_t size, uint32_t timeout) | ||
142 | { | ||
143 | if (size == 0 || timeout == 0 || empty_array == NULL) { | ||
144 | return -1; | ||
145 | } | ||
146 | |||
147 | empty_array->entries = (Ping_Array_Entry *)calloc(size, sizeof(Ping_Array_Entry)); | ||
148 | |||
149 | if (empty_array->entries == NULL) { | ||
150 | return -1; | ||
151 | } | ||
152 | |||
153 | empty_array->last_deleted = empty_array->last_added = 0; | ||
154 | empty_array->total_size = size; | ||
155 | empty_array->timeout = timeout; | ||
156 | return 0; | ||
157 | } | ||
158 | |||
159 | /* Free all the allocated memory in a Ping_Array. | ||
160 | */ | ||
161 | void ping_array_free_all(Ping_Array *array) | ||
162 | { | ||
163 | while (array->last_deleted != array->last_added) { | ||
164 | uint32_t index = array->last_deleted % array->total_size; | ||
165 | clear_entry(array, index); | ||
166 | ++array->last_deleted; | ||
167 | } | ||
168 | |||
169 | free(array->entries); | ||
170 | array->entries = NULL; | ||
171 | } | ||
172 | |||