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

taosdata / TDengine / #4875

09 Dec 2025 01:22AM UTC coverage: 64.472% (-0.2%) from 64.623%
#4875

push

travis-ci

guanshengliang
fix: temporarily disable memory leak detection for UDF tests (#33856)

162014 of 251293 relevant lines covered (64.47%)

104318075.66 hits per line

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

85.0
/source/dnode/vnode/src/vnd/vnodeHash.c
1
/*
2
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
3
 *
4
 * This program is free software: you can use, redistribute, and/or modify
5
 * it under the terms of the GNU Affero General Public License, version 3
6
 * or later ("AGPL"), as published by the Free Software Foundation.
7
 *
8
 * This program is distributed in the hope that it will be useful, but WITHOUT
9
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10
 * FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * You should have received a copy of the GNU Affero General Public License
13
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14
 */
15

16
#include "vnodeHash.h"
17

18
#define VNODE_HASH_DEFAULT_NUM_BUCKETS 1024
19

20
typedef struct SVHashEntry SVHashEntry;
21

22
struct SVHashEntry {
23
  SVHashEntry* next;
24
  void*        obj;
25
};
26

27
static void vHashRehash(SVHashTable* ht, uint32_t newNumBuckets) {
13,496✔
28
  SVHashEntry** newBuckets = (SVHashEntry**)taosMemoryCalloc(newNumBuckets, sizeof(SVHashEntry*));
13,496✔
29
  if (newBuckets == NULL) {
13,496✔
30
    return;
×
31
  }
32

33
  for (int32_t i = 0; i < ht->numBuckets; i++) {
20,743,352✔
34
    SVHashEntry* entry = ht->buckets[i];
20,729,856✔
35
    while (entry != NULL) {
31,088,036✔
36
      SVHashEntry* next = entry->next;
10,358,180✔
37
      uint32_t     bucketIndex = ht->hash(entry->obj) % newNumBuckets;
10,358,180✔
38
      entry->next = newBuckets[bucketIndex];
10,358,180✔
39
      newBuckets[bucketIndex] = entry;
10,358,180✔
40
      entry = next;
10,358,180✔
41
    }
42
  }
43

44
  taosMemoryFree(ht->buckets);
13,496✔
45
  ht->buckets = newBuckets;
13,496✔
46
  ht->numBuckets = newNumBuckets;
13,496✔
47

48
  return;
13,496✔
49
}
50

51
int32_t vHashInit(SVHashTable** ht, uint32_t (*hash)(const void*), int32_t (*compare)(const void*, const void*)) {
9,726,909✔
52
  if (ht == NULL || hash == NULL || compare == NULL) {
9,726,909✔
53
    return TSDB_CODE_INVALID_PARA;
×
54
  }
55

56
  (*ht) = (SVHashTable*)taosMemoryMalloc(sizeof(SVHashTable));
9,726,909✔
57
  if (*ht == NULL) {
9,726,506✔
58
    return terrno;
×
59
  }
60

61
  (*ht)->hash = hash;
9,726,002✔
62
  (*ht)->compare = compare;
9,726,909✔
63
  (*ht)->numEntries = 0;
9,725,617✔
64
  (*ht)->numBuckets = VNODE_HASH_DEFAULT_NUM_BUCKETS;
9,726,909✔
65
  (*ht)->buckets = (SVHashEntry**)taosMemoryCalloc((*ht)->numBuckets, sizeof(SVHashEntry*));
9,725,045✔
66
  if ((*ht)->buckets == NULL) {
9,726,909✔
67
    taosMemoryFree(*ht);
×
68
    return terrno;
×
69
  }
70

71
  return 0;
9,726,909✔
72
}
73

74
void vHashDestroy(SVHashTable** ht) {
9,726,909✔
75
  if (ht == NULL) {
9,726,909✔
76
    return;
×
77
  }
78

79
  if (*ht) {
9,726,909✔
80
    taosMemoryFree((*ht)->buckets);
9,726,909✔
81
    taosMemoryFree(*ht);
9,726,909✔
82
    (*ht) = NULL;
9,726,909✔
83
  }
84
}
85

86
int32_t vHashPut(SVHashTable* ht, void* obj) {
22,678,992✔
87
  if (ht == NULL || obj == NULL) {
22,678,992✔
88
    return TSDB_CODE_INVALID_PARA;
×
89
  }
90

91
  uint32_t bucketIndex = ht->hash(obj) % ht->numBuckets;
22,681,260✔
92
  for (SVHashEntry* entry = ht->buckets[bucketIndex]; entry != NULL; entry = entry->next) {
29,126,708✔
93
    if (ht->compare(entry->obj, obj) == 0) {
6,445,008✔
94
      return TSDB_CODE_DUP_KEY;
×
95
    }
96
  }
97

98
  if (ht->numEntries >= ht->numBuckets) {
22,677,757✔
99
    vHashRehash(ht, ht->numBuckets * 2);
6,748✔
100
    bucketIndex = ht->hash(obj) % ht->numBuckets;
6,748✔
101
  }
102

103
  SVHashEntry* entry = (SVHashEntry*)taosMemoryMalloc(sizeof(SVHashEntry));
22,679,375✔
104
  if (entry == NULL) {
22,681,219✔
105
    return terrno;
×
106
  }
107
  entry->obj = obj;
22,681,219✔
108
  entry->next = ht->buckets[bucketIndex];
22,681,836✔
109
  ht->buckets[bucketIndex] = entry;
22,679,910✔
110
  ht->numEntries++;
22,679,533✔
111

112
  return 0;
22,680,140✔
113
}
114

115
int32_t vHashGet(SVHashTable* ht, const void* obj, void** retObj) {
90,517,370✔
116
  if (ht == NULL || obj == NULL || retObj == NULL) {
90,517,370✔
117
    return TSDB_CODE_INVALID_PARA;
×
118
  }
119

120
  uint32_t bucketIndex = ht->hash(obj) % ht->numBuckets;
90,529,169✔
121
  for (SVHashEntry* entry = ht->buckets[bucketIndex]; entry != NULL; entry = entry->next) {
103,985,941✔
122
    if (ht->compare(entry->obj, obj) == 0) {
81,713,766✔
123
      *retObj = entry->obj;
68,260,615✔
124
      return 0;
68,262,013✔
125
    }
126
  }
127

128
  *retObj = NULL;
22,254,270✔
129
  return TSDB_CODE_NOT_FOUND;
22,253,448✔
130
}
131

132
int32_t vHashDrop(SVHashTable* ht, const void* obj) {
22,682,644✔
133
  if (ht == NULL || obj == NULL) {
22,682,644✔
134
    return TSDB_CODE_INVALID_PARA;
×
135
  }
136

137
  uint32_t bucketIndex = ht->hash(obj) % ht->numBuckets;
22,683,244✔
138
  for (SVHashEntry** entry = &ht->buckets[bucketIndex]; *entry != NULL; entry = &(*entry)->next) {
25,420,649✔
139
    if (ht->compare((*entry)->obj, obj) == 0) {
25,421,277✔
140
      SVHashEntry* tmp = *entry;
22,683,244✔
141
      *entry = (*entry)->next;
22,683,244✔
142
      taosMemoryFree(tmp);
22,683,244✔
143
      ht->numEntries--;
22,683,244✔
144
      if (ht->numBuckets > VNODE_HASH_DEFAULT_NUM_BUCKETS && ht->numEntries < ht->numBuckets / 4) {
22,683,244✔
145
        vHashRehash(ht, ht->numBuckets / 2);
6,748✔
146
      }
147
      return 0;
22,683,244✔
148
    }
149
  }
150

151
  return TSDB_CODE_NOT_FOUND;
×
152
}
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

© 2026 Coveralls, Inc