Skip to content

Commit

Permalink
Merge pull request #4 from RedisBloom/td.malloc
Browse files Browse the repository at this point in the history
Add the ability to change the T-Digest allocator at compile time.
  • Loading branch information
filipecosta90 authored Feb 1, 2021
2 parents 7f63be1 + f5abcef commit 1aab9db
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
20 changes: 20 additions & 0 deletions src/td_malloc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* Adaptive histogram based on something like streaming k-means crossed with Q-digest.
* The implementation is a direct descendent of MergingDigest
* https://github.com/tdunning/t-digest/
*
* Copyright (c) 2021 Redis Labs, All rights reserved.
*
* Allocator selection.
*
* This file is used in order to change the t-digest allocator at compile time.
* Just define the following defines to what you want to use. Also add
* the include of your alternate allocator if needed (not needed in order
* to use the default libc allocator). */

#ifndef TD_ALLOC_H
#define TD_ALLOC_H
#define __td_malloc malloc
#define __td_realloc realloc
#define __td_free free
#endif
10 changes: 8 additions & 2 deletions src/tdigest.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
#include <math.h>
#include "tdigest.h"

#ifndef TD_MALLOC_INCLUDE
#define TD_MALLOC_INCLUDE "td_malloc.h"
#endif

#include TD_MALLOC_INCLUDE

void bbzero(void *to, size_t count) { memset(to, 0, count); }

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

td_histogram_t *td_new(double compression) {
size_t memsize = td_required_buf_size(compression);
return td_init(compression, memsize, (char *)(malloc(memsize)));
return td_init(compression, memsize, (char *)(__td_malloc(memsize)));
}

void td_free(td_histogram_t *h) { free((void *)(h)); }
void td_free(td_histogram_t *h) { __td_free((void *)(h)); }

void td_merge(td_histogram_t *into, td_histogram_t *from) {
td_compress(into);
Expand Down
1 change: 1 addition & 0 deletions src/tdigest.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* The implementation is a direct descendent of MergingDigest
* https://github.com/tdunning/t-digest/
*
* Copyright (c) 2021 Redis Labs, All rights reserved.
* Copyright (c) 2018 Andrew Werner, All rights reserved.
*
* The special characteristics of this algorithm are:
Expand Down

0 comments on commit 1aab9db

Please sign in to comment.