Skip to content

Commit

Permalink
[add] Add the ability to change allocator at compile time
Browse files Browse the repository at this point in the history
  • Loading branch information
filipecosta90 committed Jan 30, 2021
1 parent 7f63be1 commit b8db533
Show file tree
Hide file tree
Showing 2 changed files with 28 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 Filipe Oliveira, 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

0 comments on commit b8db533

Please sign in to comment.