Skip to content

Commit

Permalink
Merge branch 'filipecosta90-allocator.selection'
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeb01 committed Feb 23, 2021
2 parents e20129c + 5094c64 commit 706a9e0
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 41 deletions.
3 changes: 2 additions & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ set(HDR_HISTOGRAM_PRIVATE_HEADERS
hdr_atomic.h
hdr_encoding.h
hdr_endian.h
hdr_tests.h)
hdr_tests.h
hdr_malloc.h)

set(HDR_HISTOGRAM_PUBLIC_HEADERS
hdr_histogram.h
Expand Down
16 changes: 11 additions & 5 deletions src/hdr_histogram.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@
#include "hdr_tests.h"
#include "hdr_atomic.h"

#ifndef HDR_MALLOC_INCLUDE
#define HDR_MALLOC_INCLUDE "hdr_malloc.h"
#endif

#include HDR_MALLOC_INCLUDE

/* ###### ####### ## ## ## ## ######## ###### */
/* ## ## ## ## ## ## ### ## ## ## ## */
/* ## ## ## ## ## #### ## ## ## */
Expand Down Expand Up @@ -398,16 +404,16 @@ int hdr_init(
return r;
}

counts = (int64_t*) calloc((size_t) cfg.counts_len, sizeof(int64_t));
counts = (int64_t*) hdr_calloc((size_t) cfg.counts_len, sizeof(int64_t));
if (!counts)
{
return ENOMEM;
}

histogram = (struct hdr_histogram*) calloc(1, sizeof(struct hdr_histogram));
histogram = (struct hdr_histogram*) hdr_calloc(1, sizeof(struct hdr_histogram));
if (!histogram)
{
free(counts);
hdr_free(counts);
return ENOMEM;
}

Expand All @@ -422,8 +428,8 @@ int hdr_init(
void hdr_close(struct hdr_histogram* h)
{
if (h) {
free(h->counts);
free(h);
hdr_free(h->counts);
hdr_free(h);
}
}

Expand Down
70 changes: 38 additions & 32 deletions src/hdr_histogram_log.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ typedef SSIZE_T ssize_t;

#include "hdr_endian.h"

#ifndef HDR_MALLOC_INCLUDE
#define HDR_MALLOC_INCLUDE "hdr_malloc.h"
#endif

#include HDR_MALLOC_INCLUDE

/* Private prototypes useful for the logger */
int32_t counts_index_for(const struct hdr_histogram* h, int64_t value);

Expand Down Expand Up @@ -181,7 +187,7 @@ int hdr_encode_compressed(
int32_t counts_limit = len_to_max < h->counts_len ? len_to_max : h->counts_len;

const size_t encoded_len = SIZEOF_ENCODING_FLYWEIGHT_V1 + MAX_BYTES_LEB128 * (size_t) counts_limit;
if ((encoded = (encoding_flyweight_v1_t*) calloc(encoded_len, sizeof(uint8_t))) == NULL)
if ((encoded = (encoding_flyweight_v1_t*) hdr_calloc(encoded_len, sizeof(uint8_t))) == NULL)
{
FAIL_AND_CLEANUP(cleanup, result, ENOMEM);
}
Expand Down Expand Up @@ -225,7 +231,7 @@ int hdr_encode_compressed(
dest_len = compressBound(encoded_size);
compressed_size = SIZEOF_COMPRESSION_FLYWEIGHT + dest_len;

if ((compressed = (compression_flyweight_t*) malloc(compressed_size)) == NULL)
if ((compressed = (compression_flyweight_t*) hdr_malloc(compressed_size)) == NULL)
{
FAIL_AND_CLEANUP(cleanup, result, ENOMEM);
}
Expand All @@ -242,10 +248,10 @@ int hdr_encode_compressed(
*compressed_len = SIZEOF_COMPRESSION_FLYWEIGHT + dest_len;

cleanup:
free(encoded);
hdr_free(encoded);
if (result == HDR_DEFLATE_FAIL)
{
free(compressed);
hdr_free(compressed);
}

return result;
Expand Down Expand Up @@ -409,7 +415,7 @@ static int hdr_decode_compressed_v0(
}

counts_array_len = h->counts_len * word_size;
if ((counts_array = (uint8_t*) calloc(1, (size_t) counts_array_len)) == NULL)
if ((counts_array = (uint8_t*) hdr_calloc(1, (size_t) counts_array_len)) == NULL)
{
FAIL_AND_CLEANUP(cleanup, result, ENOMEM);
}
Expand All @@ -430,11 +436,11 @@ static int hdr_decode_compressed_v0(

cleanup:
(void)inflateEnd(&strm);
free(counts_array);
hdr_free(counts_array);

if (result != 0)
{
free(h);
hdr_free(h);
}
else if (NULL == *histogram)
{
Expand All @@ -443,7 +449,7 @@ static int hdr_decode_compressed_v0(
else
{
hdr_add(*histogram, h);
free(h);
hdr_free(h);
}

return result;
Expand Down Expand Up @@ -510,7 +516,7 @@ static int hdr_decode_compressed_v1(
/* Give the temp uncompressed array a little bif of extra */
counts_array_len = counts_limit * word_size;

if ((counts_array = (uint8_t*) calloc(1, (size_t) counts_array_len)) == NULL)
if ((counts_array = (uint8_t*) hdr_calloc(1, (size_t) counts_array_len)) == NULL)
{
FAIL_AND_CLEANUP(cleanup, result, ENOMEM);
}
Expand All @@ -531,11 +537,11 @@ static int hdr_decode_compressed_v1(

cleanup:
(void)inflateEnd(&strm);
free(counts_array);
hdr_free(counts_array);

if (result != 0)
{
free(h);
hdr_free(h);
}
else if (NULL == *histogram)
{
Expand All @@ -544,7 +550,7 @@ static int hdr_decode_compressed_v1(
else
{
hdr_add(*histogram, h);
free(h);
hdr_free(h);
}

return result;
Expand Down Expand Up @@ -608,7 +614,7 @@ static int hdr_decode_compressed_v2(
/* Make sure there at least 9 bytes to read */
/* if there is a corrupt value at the end */
/* of the array we won't read corrupt data or crash. */
if ((counts_array = (uint8_t*) calloc(1, (size_t) counts_limit + 9)) == NULL)
if ((counts_array = (uint8_t*) hdr_calloc(1, (size_t) counts_limit + 9)) == NULL)
{
FAIL_AND_CLEANUP(cleanup, result, ENOMEM);
}
Expand All @@ -633,11 +639,11 @@ static int hdr_decode_compressed_v2(

cleanup:
(void)inflateEnd(&strm);
free(counts_array);
hdr_free(counts_array);

if (result != 0)
{
free(h);
hdr_free(h);
}
else if (NULL == *histogram)
{
Expand All @@ -646,7 +652,7 @@ static int hdr_decode_compressed_v2(
else
{
hdr_add(*histogram, h);
free(h);
hdr_free(h);
}

return result;
Expand Down Expand Up @@ -796,7 +802,7 @@ int hdr_log_write(
}

encoded_len = hdr_base64_encoded_len(compressed_len);
encoded_histogram = (char*) calloc(encoded_len + 1, sizeof(char));
encoded_histogram = (char*) hdr_calloc(encoded_len + 1, sizeof(char));

rc = hdr_base64_encode(
compressed_histogram, compressed_len, encoded_histogram, encoded_len);
Expand All @@ -816,8 +822,8 @@ int hdr_log_write(
}

cleanup:
free(compressed_histogram);
free(encoded_histogram);
hdr_free(compressed_histogram);
hdr_free(encoded_histogram);

return result;
}
Expand Down Expand Up @@ -848,7 +854,7 @@ int hdr_log_write_entry(
}

encoded_len = hdr_base64_encoded_len(compressed_len);
encoded_histogram = (char*) calloc(encoded_len + 1, sizeof(char));
encoded_histogram = (char*) hdr_calloc(encoded_len + 1, sizeof(char));

rc = hdr_base64_encode(
compressed_histogram, compressed_len, encoded_histogram, encoded_len);
Expand All @@ -874,8 +880,8 @@ int hdr_log_write_entry(
}

cleanup:
free(compressed_histogram);
free(encoded_histogram);
hdr_free(compressed_histogram);
hdr_free(encoded_histogram);

return result;
}
Expand Down Expand Up @@ -1067,7 +1073,7 @@ int hdr_log_read_entry(
size_t capacity = 1024;
size_t base64_len = 0;
size_t tag_offset = 0;
char* base64_histogram = calloc(capacity, sizeof(char));
char* base64_histogram = hdr_calloc(capacity, sizeof(char));
size_t compressed_len = 0;
uint8_t* compressed_histogram = NULL;
int result = -EINVAL;
Expand Down Expand Up @@ -1176,7 +1182,7 @@ int hdr_log_read_entry(
if (base64_len == capacity)
{
capacity *= 2;
base64_histogram = realloc(base64_histogram, capacity * sizeof(char));
base64_histogram = hdr_realloc(base64_histogram, capacity * sizeof(char));
if (NULL == base64_histogram)
{
FAIL_AND_CLEANUP(cleanup, result, -ENOMEM);
Expand All @@ -1196,7 +1202,7 @@ int hdr_log_read_entry(
}
while (DONE != state);

compressed_histogram = calloc(base64_len, sizeof(uint8_t));
compressed_histogram = hdr_calloc(base64_len, sizeof(uint8_t));
compressed_len = hdr_base64_decoded_len(base64_len);

result = hdr_base64_decode(
Expand All @@ -1209,8 +1215,8 @@ int hdr_log_read_entry(
result = hdr_decode_compressed(compressed_histogram, compressed_len, histogram);

cleanup:
free(base64_histogram);
free(compressed_histogram);
hdr_free(base64_histogram);
hdr_free(compressed_histogram);
return result;
}

Expand All @@ -1231,20 +1237,20 @@ int hdr_log_encode(struct hdr_histogram* histogram, char** encoded_histogram)
}

encoded_len = hdr_base64_encoded_len(compressed_len);
encoded_histogram_tmp = (char*) calloc(encoded_len + 1, sizeof(char));
encoded_histogram_tmp = (char*) hdr_calloc(encoded_len + 1, sizeof(char));

rc = hdr_base64_encode(
compressed_histogram, compressed_len, encoded_histogram_tmp, encoded_len);
if (rc != 0)
{
free(encoded_histogram_tmp);
hdr_free(encoded_histogram_tmp);
FAIL_AND_CLEANUP(cleanup, result, rc);
}

*encoded_histogram = encoded_histogram_tmp;

cleanup:
free(compressed_histogram);
hdr_free(compressed_histogram);

return result;
}
Expand All @@ -1256,7 +1262,7 @@ int hdr_log_decode(struct hdr_histogram** histogram, char* base64_histogram, siz
int result = 0;

size_t compressed_len = hdr_base64_decoded_len(base64_len);
compressed_histogram = (uint8_t*) malloc(sizeof(uint8_t)*compressed_len);
compressed_histogram = (uint8_t*) hdr_malloc(sizeof(uint8_t)*compressed_len);
memset(compressed_histogram, 0, compressed_len);

r = hdr_base64_decode(
Expand All @@ -1274,7 +1280,7 @@ int hdr_log_decode(struct hdr_histogram** histogram, char* base64_histogram, siz
}

cleanup:
free(compressed_histogram);
hdr_free(compressed_histogram);

return result;
}
Expand Down
6 changes: 6 additions & 0 deletions src/hdr_interval_recorder.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
#include "hdr_atomic.h"
#include "hdr_interval_recorder.h"

#ifndef HDR_MALLOC_INCLUDE
#define HDR_MALLOC_INCLUDE "hdr_malloc.h"
#endif

#include HDR_MALLOC_INCLUDE

int hdr_interval_recorder_init(struct hdr_interval_recorder* r)
{
r->active = r->inactive = NULL;
Expand Down
19 changes: 19 additions & 0 deletions src/hdr_malloc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* hdr_malloc.h
* Written by Filipe Oliveira and released to the public domain,
* as explained at http://creativecommons.org/publicdomain/zero/1.0/
*
* Allocator selection.
*
* This file is used in order to change the HdrHistogram 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 HDR_MALLOC_H__
#define HDR_MALLOC_H__
#define hdr_malloc malloc
#define hdr_calloc calloc
#define hdr_realloc realloc
#define hdr_free free
#endif
10 changes: 8 additions & 2 deletions src/hdr_thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,20 @@
#include <stdlib.h>
#include "hdr_thread.h"

#ifndef HDR_MALLOC_INCLUDE
#define HDR_MALLOC_INCLUDE "hdr_malloc.h"
#endif

#include HDR_MALLOC_INCLUDE

struct hdr_mutex* hdr_mutex_alloc(void)
{
return malloc(sizeof(hdr_mutex));
return hdr_malloc(sizeof(hdr_mutex));
}

void hdr_mutex_free(struct hdr_mutex* mutex)
{
free(mutex);
hdr_free(mutex);
}

#if defined(_WIN32) || defined(_WIN64) || defined(__CYGWIN__)
Expand Down
6 changes: 6 additions & 0 deletions src/hdr_writer_reader_phaser.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@

#include "hdr_writer_reader_phaser.h"

#ifndef HDR_MALLOC_INCLUDE
#define HDR_MALLOC_INCLUDE "hdr_malloc.h"
#endif

#include HDR_MALLOC_INCLUDE

static int64_t _hdr_phaser_get_epoch(int64_t* field)
{
return hdr_atomic_load_64(field);
Expand Down
2 changes: 1 addition & 1 deletion test/hdr_histogram_benchmark.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ static void BM_hdr_value_at_percentiles_given_array(benchmark::State &state) {
int64_t items_processed = 0;
for (auto _ : state) {
benchmark::DoNotOptimize(
hdr_value_at_percentiles(histogram, percentile_list, 4, &values));
hdr_value_at_percentiles(histogram, percentile_list, values, 4));
// read/write barrier
benchmark::ClobberMemory();
items_processed += 4;
Expand Down

0 comments on commit 706a9e0

Please sign in to comment.