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

taosdata / TDengine / #3562

20 Dec 2024 09:57AM UTC coverage: 26.655% (-32.2%) from 58.812%
#3562

push

travis-ci

web-flow
Merge pull request #29229 from taosdata/enh/TS-5749-3.0

enh: seperate tsdb async tasks to different thread pools

21498 of 109421 branches covered (19.65%)

Branch coverage included in aggregate %.

66 of 96 new or added lines in 7 files covered. (68.75%)

39441 existing lines in 157 files now uncovered.

35007 of 102566 relevant lines covered (34.13%)

53922.97 hits per line

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

46.44
/include/util/tarray2.h
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 "talgo.h"
17

18
#ifndef _TD_UTIL_TARRAY2_H_
19
#define _TD_UTIL_TARRAY2_H_
20

21
#ifdef __cplusplus
22
extern "C" {
23
#endif
24

25
// a: a
26
// e: element
27
// ep: element pointer
28
// cmp: compare function
29
// idx: index
30
// cb: callback function
31

32
#define TARRAY2(TYPE) \
33
  struct {            \
34
    int32_t size;     \
35
    int32_t capacity; \
36
    TYPE   *data;     \
37
  }
38

39
typedef void (*TArray2Cb)(void *);
40

41
#define TARRAY2_SIZE(a)       ((a)->size)
42
#define TARRAY2_CAPACITY(a)   ((a)->capacity)
43
#define TARRAY2_DATA(a)       ((a)->data)
44
#define TARRAY2_GET(a, i)     ((a)->data[i])
45
#define TARRAY2_GET_PTR(a, i) ((a)->data + i)
46
#define TARRAY2_FIRST(a)      ((a)->data[0])
47
#define TARRAY2_LAST(a)       ((a)->data[(a)->size - 1])
48
#define TARRAY2_DATA_LEN(a)   ((a)->size * sizeof(((a)->data[0])))
49

50
static FORCE_INLINE int32_t tarray2_make_room(void *arr, int32_t expSize, int32_t eleSize) {
51
  TARRAY2(void) *a = arr;
83,070✔
52

53
  int32_t capacity = (a->capacity > 0) ? (a->capacity << 1) : 32;
83,070✔
54
  while (capacity < expSize) {
83,070!
55
    capacity <<= 1;
×
56
  }
57
  void *p = taosMemoryRealloc(a->data, capacity * eleSize);
83,070!
58
  if (p == NULL) return terrno;
83,050!
59
  a->capacity = capacity;
83,050✔
60
  a->data = p;
83,050✔
61
  return 0;
83,050✔
62
}
63

64
static FORCE_INLINE int32_t tarray2InsertBatch(void *arr, int32_t idx, const void *elePtr, int32_t numEle,
65
                                               int32_t eleSize) {
66
  TARRAY2(uint8_t) *a = arr;
106,865✔
67

68
  int32_t ret = 0;
106,865✔
69
  if (a->size + numEle > a->capacity) {
106,865!
70
    ret = tarray2_make_room(a, a->size + numEle, eleSize);
166,120!
71
  }
72
  if (ret == 0) {
106,845!
73
    if (idx < a->size) {
106,816!
UNCOV
74
      (void)memmove(a->data + (idx + numEle) * eleSize, a->data + idx * eleSize, (a->size - idx) * eleSize);
×
75
    }
76
    (void)memcpy(a->data + idx * eleSize, elePtr, numEle * eleSize);
106,816✔
77
    a->size += numEle;
106,816✔
78
  }
79
  return ret;
106,845✔
80
}
81

82
static FORCE_INLINE void *tarray2Search(void *arr, const void *elePtr, int32_t eleSize, __compar_fn_t compar,
83
                                        int32_t flag) {
84
  TARRAY2(void) *a = arr;
121✔
85
  return taosbsearch(elePtr, a->data, a->size, eleSize, compar, flag);
121✔
86
}
87

88
static FORCE_INLINE int32_t tarray2SearchIdx(void *arr, const void *elePtr, int32_t eleSize, __compar_fn_t compar,
89
                                             int32_t flag) {
90
  TARRAY2(void) *a = arr;
88✔
91
  void *p = taosbsearch(elePtr, a->data, a->size, eleSize, compar, flag);
42✔
92
  if (p == NULL) {
88!
93
    return -1;
78✔
94
  } else {
95
    return (int32_t)(((uint8_t *)p - (uint8_t *)a->data) / eleSize);
10✔
96
  }
97
}
98

99
static FORCE_INLINE int32_t tarray2SortInsert(void *arr, const void *elePtr, int32_t eleSize, __compar_fn_t compar) {
100
  TARRAY2(void) *a = arr;
78✔
101
  int32_t idx = tarray2SearchIdx(arr, elePtr, eleSize, compar, TD_GT);
78✔
102
  return tarray2InsertBatch(arr, idx < 0 ? a->size : idx, elePtr, 1, eleSize);
156!
103
}
104

105
#define TARRAY2_INIT_EX(a, size_, capacity_, data_) \
106
  do {                                              \
107
    (a)->size = (size_);                            \
108
    (a)->capacity = (capacity_);                    \
109
    (a)->data = (data_);                            \
110
  } while (0)
111

112
#define TARRAY2_INIT(a) TARRAY2_INIT_EX(a, 0, 0, NULL)
113

114
#define TARRAY2_CLEAR(a, cb)                    \
115
  do {                                          \
116
    if ((cb) && (a)->size > 0) {                \
117
      TArray2Cb cb_ = (TArray2Cb)(cb);          \
118
      for (int32_t i = 0; i < (a)->size; ++i) { \
119
        (void)cb_((a)->data + i);               \
120
      }                                         \
121
    }                                           \
122
    (a)->size = 0;                              \
123
  } while (0)
124

125
#define TARRAY2_DESTROY(a, cb)   \
126
  do {                           \
127
    TARRAY2_CLEAR(a, cb);        \
128
    if ((a)->data) {             \
129
      taosMemoryFree((a)->data); \
130
      (a)->data = NULL;          \
131
    }                            \
132
    (a)->capacity = 0;           \
133
  } while (0)
134

135
#define TARRAY2_INSERT_PTR(a, idx, ep) tarray2InsertBatch(a, idx, ep, 1, sizeof((a)->data[0]))
136
#define TARRAY2_APPEND_PTR(a, ep)      tarray2InsertBatch(a, (a)->size, ep, 1, sizeof((a)->data[0]))
137
#define TARRAY2_APPEND_BATCH(a, ep, n) tarray2InsertBatch(a, (a)->size, ep, n, sizeof((a)->data[0]))
138
#define TARRAY2_APPEND(a, e)           TARRAY2_APPEND_PTR(a, &(e))
139

140
// return (TYPE *)
141
#define TARRAY2_SEARCH(a, ep, cmp, flag) tarray2Search(a, ep, sizeof(((a)->data[0])), (__compar_fn_t)cmp, flag)
142

143
#define TARRAY2_SEARCH_IDX(a, ep, cmp, flag) tarray2SearchIdx(a, ep, sizeof(((a)->data[0])), (__compar_fn_t)cmp, flag)
144

145
#define TARRAY2_SORT_INSERT(a, e, cmp)    tarray2SortInsert(a, &(e), sizeof(((a)->data[0])), (__compar_fn_t)cmp)
146
#define TARRAY2_SORT_INSERT_P(a, ep, cmp) tarray2SortInsert(a, ep, sizeof(((a)->data[0])), (__compar_fn_t)cmp)
147

148
#define TARRAY2_REMOVE(a, idx, cb)                                                                             \
149
  do {                                                                                                         \
150
    if ((idx) < (a)->size) {                                                                                   \
151
      if (cb) {                                                                                                \
152
        TArray2Cb cb_ = (TArray2Cb)(cb);                                                                       \
153
        cb_((a)->data + (idx));                                                                                \
154
      }                                                                                                        \
155
      if ((idx) < (a)->size - 1) {                                                                             \
156
        (void)memmove((a)->data + (idx), (a)->data + (idx) + 1, sizeof((*(a)->data)) * ((a)->size - (idx)-1)); \
157
      }                                                                                                        \
158
      (a)->size--;                                                                                             \
159
    }                                                                                                          \
160
  } while (0)
161

162
#define TARRAY2_FOREACH(a, e)         for (int32_t __i = 0; __i < (a)->size && ((e) = (a)->data[__i], 1); __i++)
163
#define TARRAY2_FOREACH_REVERSE(a, e) for (int32_t __i = (a)->size - 1; __i >= 0 && ((e) = (a)->data[__i], 1); __i--)
164
#define TARRAY2_FOREACH_PTR(a, ep)    for (int32_t __i = 0; __i < (a)->size && ((ep) = &(a)->data[__i], 1); __i++)
165
#define TARRAY2_FOREACH_PTR_REVERSE(a, ep) \
166
  for (int32_t __i = (a)->size - 1; __i >= 0 && ((ep) = &(a)->data[__i], 1); __i--)
167

168
#define TARRAY2_SORT(a, cmp)                                                    \
169
  do {                                                                          \
170
    if ((a)->size > 1) {                                                        \
171
      taosSort((a)->data, (a)->size, sizeof((a)->data[0]), (__compar_fn_t)cmp); \
172
    }                                                                           \
173
  } while (0)
174

175
#ifdef __cplusplus
176
}
177
#endif
178

179
#endif /*_TD_UTIL_TARRAY2_H_*/
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