Skip to content

Commit 1aab9db

Browse files
Merge pull request #4 from RedisBloom/td.malloc
Add the ability to change the T-Digest allocator at compile time.
2 parents 7f63be1 + f5abcef commit 1aab9db

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
lines changed

src/td_malloc.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* Adaptive histogram based on something like streaming k-means crossed with Q-digest.
3+
* The implementation is a direct descendent of MergingDigest
4+
* https://github.com/tdunning/t-digest/
5+
*
6+
* Copyright (c) 2021 Redis Labs, All rights reserved.
7+
*
8+
* Allocator selection.
9+
*
10+
* This file is used in order to change the t-digest allocator at compile time.
11+
* Just define the following defines to what you want to use. Also add
12+
* the include of your alternate allocator if needed (not needed in order
13+
* to use the default libc allocator). */
14+
15+
#ifndef TD_ALLOC_H
16+
#define TD_ALLOC_H
17+
#define __td_malloc malloc
18+
#define __td_realloc realloc
19+
#define __td_free free
20+
#endif

src/tdigest.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44
#include <math.h>
55
#include "tdigest.h"
66

7+
#ifndef TD_MALLOC_INCLUDE
8+
#define TD_MALLOC_INCLUDE "td_malloc.h"
9+
#endif
10+
11+
#include TD_MALLOC_INCLUDE
12+
713
void bbzero(void *to, size_t count) { memset(to, 0, count); }
814

915
static bool is_very_small(double val) { return !(val > .000000001 || val < -.000000001); }
@@ -56,10 +62,10 @@ static td_histogram_t *td_init(double compression, size_t buf_size, char *buf) {
5662

5763
td_histogram_t *td_new(double compression) {
5864
size_t memsize = td_required_buf_size(compression);
59-
return td_init(compression, memsize, (char *)(malloc(memsize)));
65+
return td_init(compression, memsize, (char *)(__td_malloc(memsize)));
6066
}
6167

62-
void td_free(td_histogram_t *h) { free((void *)(h)); }
68+
void td_free(td_histogram_t *h) { __td_free((void *)(h)); }
6369

6470
void td_merge(td_histogram_t *into, td_histogram_t *from) {
6571
td_compress(into);

src/tdigest.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* The implementation is a direct descendent of MergingDigest
77
* https://github.com/tdunning/t-digest/
88
*
9+
* Copyright (c) 2021 Redis Labs, All rights reserved.
910
* Copyright (c) 2018 Andrew Werner, All rights reserved.
1011
*
1112
* The special characteristics of this algorithm are:

0 commit comments

Comments
 (0)