• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

c-ares / c-ares / #53

27 Sep 2023 12:43PM UTC coverage: 88.055% (+0.02%) from 88.04%
#53

push

travis-ci

bradh352
fix #206 by allowing NULL to be passed to socket function callbacks to use defaults

21 of 21 new or added lines in 1 file covered. (100.0%)

5057 of 5743 relevant lines covered (88.06%)

11801.46 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

85.9
/src/lib/ares__htable_asvp.c
1
/* Copyright (C) 2023 by Brad House
2
 *
3
 * Permission to use, copy, modify, and distribute this
4
 * software and its documentation for any purpose and without
5
 * fee is hereby granted, provided that the above copyright
6
 * notice appear in all copies and that both that copyright
7
 * notice and this permission notice appear in supporting
8
 * documentation, and that the name of M.I.T. not be used in
9
 * advertising or publicity pertaining to distribution of the
10
 * software without specific, written prior permission.
11
 * M.I.T. makes no representations about the suitability of
12
 * this software for any purpose.  It is provided "as is"
13
 * without express or implied warranty.
14
 *
15
 * SPDX-License-Identifier: MIT
16
 */
17
#include "ares_setup.h"
18
#include "ares.h"
19
#include "ares_private.h"
20
#include "ares__htable.h"
21
#include "ares__htable_asvp.h"
22

23

24
struct ares__htable_asvp {
25
  ares__htable_asvp_val_free_t free_val;
26
  ares__htable_t              *hash;
27
};
28

29

30
typedef struct {
31
  ares_socket_t        key;
32
  void                *val;
33
  ares__htable_asvp_t *parent;
34
} ares__htable_asvp_bucket_t;
35

36

37
void ares__htable_asvp_destroy(ares__htable_asvp_t *htable)
333✔
38
{
39
  if (htable == NULL)
333✔
40
    return;
35✔
41

42
  ares__htable_destroy(htable->hash);
298✔
43
  ares_free(htable);
298✔
44
}
45

46

47
static unsigned int hash_func(const void *key, unsigned int seed)
2,697✔
48
{
49
  const ares_socket_t *arg = key;
2,697✔
50
  return ares__htable_hash_FNV1a((const unsigned char *)arg, sizeof(*arg),
2,697✔
51
                                 seed);
52
}
53

54

55
static const void *bucket_key(const void *bucket)
2,538✔
56
{
57
  const ares__htable_asvp_bucket_t *arg = bucket;
2,538✔
58
  return &arg->key;
2,538✔
59
}
60

61

62
static void bucket_free(void *bucket)
264✔
63
{
64
  ares__htable_asvp_bucket_t *arg = bucket;
264✔
65

66
  if (arg->parent->free_val)
264✔
67
    arg->parent->free_val(arg->val);
×
68

69
  ares_free(arg);
264✔
70
}
264✔
71

72

73
static unsigned int key_eq(const void *key1, const void *key2)
2,262✔
74
{
75
  const ares_socket_t *k1 = key1;
2,262✔
76
  const ares_socket_t *k2 = key2;
2,262✔
77

78
  if (*k1 == *k2)
2,262✔
79
    return 1;
2,262✔
80

81
  return 0;
×
82
}
83

84

85
ares__htable_asvp_t *ares__htable_asvp_create(
306✔
86
    ares__htable_asvp_val_free_t val_free)
87
{
88
  ares__htable_asvp_t *htable = ares_malloc(sizeof(*htable));
306✔
89
  if (htable == NULL)
306✔
90
    goto fail;
3✔
91

92
  htable->hash = ares__htable_create(hash_func,
303✔
93
                                     bucket_key,
94
                                     bucket_free,
95
                                     key_eq);
96
  if (htable->hash == NULL)
303✔
97
    goto fail;
4✔
98

99
  htable->free_val = val_free;
299✔
100

101
  return htable;
299✔
102

103
fail:
7✔
104
  if (htable) {
7✔
105
    ares__htable_destroy(htable->hash);
4✔
106
    ares_free(htable);
4✔
107
  }
108
  return NULL;
7✔
109
}
110

111

112
unsigned int ares__htable_asvp_insert(ares__htable_asvp_t *htable,
266✔
113
                                      ares_socket_t key, void *val)
114
{
115
  ares__htable_asvp_bucket_t *bucket = NULL;
266✔
116

117
  if (htable == NULL)
266✔
118
    goto fail;
×
119

120
  bucket = ares_malloc(sizeof(*bucket));
266✔
121
  if (bucket == NULL)
266✔
122
    goto fail;
2✔
123

124
  bucket->parent = htable;
264✔
125
  bucket->key    = key;
264✔
126
  bucket->val    = val;
264✔
127

128
  if (!ares__htable_insert(htable->hash, bucket))
264✔
129
    goto fail;
×
130

131
  return 1;
264✔
132

133
fail:
2✔
134
  if (bucket) {
2✔
135
    ares_free(bucket);
×
136
  }
137
  return 0;
2✔
138
}
139

140

141
unsigned int ares__htable_asvp_get(ares__htable_asvp_t *htable,
2,156✔
142
                                   ares_socket_t key, void **val)
143
{
144
  ares__htable_asvp_bucket_t *bucket = NULL;
2,156✔
145

146
  if (val)
2,156✔
147
    *val = NULL;
2,156✔
148

149
  if (htable == NULL)
2,156✔
150
    return 0;
×
151

152
  bucket = ares__htable_get(htable->hash, &key);
2,156✔
153
  if (bucket == NULL)
2,156✔
154
    return 0;
158✔
155

156
  if (val)
1,998✔
157
    *val = bucket->val;
1,998✔
158
  return 1;
1,998✔
159
}
160

161

162
void *ares__htable_asvp_get_direct(ares__htable_asvp_t *htable,
2,156✔
163
                                   ares_socket_t key)
164
{
165
  void *val = NULL;
2,156✔
166
  ares__htable_asvp_get(htable, key, &val);
2,156✔
167
  return val;
2,156✔
168
}
169

170

171
unsigned int ares__htable_asvp_remove(ares__htable_asvp_t *htable,
264✔
172
                                      ares_socket_t key)
173
{
174
  if (htable == NULL)
264✔
175
    return 0;
×
176

177
  return ares__htable_remove(htable->hash, &key);
264✔
178
}
179

180

181
size_t ares__htable_asvp_num_keys(ares__htable_asvp_t *htable)
×
182
{
183
  if (htable == NULL)
×
184
    return 0;
×
185
  return ares__htable_num_keys(htable->hash);
×
186
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc