Skip to content

Commit 070ee7e

Browse files
committed
Add support for rejecting values larger than the max trackable value. #126.
1 parent e33c19c commit 070ee7e

File tree

3 files changed

+14
-15
lines changed

3 files changed

+14
-15
lines changed

src/hdr_histogram.c

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -341,10 +341,10 @@ static int32_t buckets_needed_to_cover_value(int64_t value, int32_t sub_bucket_c
341341
/* ## ## ######## ## ## ####### ## ## ## */
342342

343343
int hdr_calculate_bucket_config(
344-
int64_t lowest_discernible_value,
345-
int64_t highest_trackable_value,
346-
int significant_figures,
347-
struct hdr_histogram_bucket_config* cfg)
344+
int64_t lowest_discernible_value,
345+
int64_t highest_trackable_value,
346+
int significant_figures,
347+
struct hdr_histogram_bucket_config* cfg)
348348
{
349349
int32_t sub_bucket_count_magnitude;
350350
int64_t largest_value_with_single_unit_resolution;
@@ -406,10 +406,10 @@ void hdr_init_preallocated(struct hdr_histogram* h, struct hdr_histogram_bucket_
406406
}
407407

408408
int hdr_init(
409-
int64_t lowest_discernible_value,
410-
int64_t highest_trackable_value,
411-
int significant_figures,
412-
struct hdr_histogram** result)
409+
int64_t lowest_discernible_value,
410+
int64_t highest_trackable_value,
411+
int significant_figures,
412+
struct hdr_histogram** result)
413413
{
414414
int64_t* counts;
415415
struct hdr_histogram_bucket_config cfg;
@@ -492,13 +492,12 @@ bool hdr_record_values(struct hdr_histogram* h, int64_t value, int64_t count)
492492
{
493493
int32_t counts_index;
494494

495-
if (value < 0)
495+
if (value < 0 || h->highest_trackable_value < value)
496496
{
497497
return false;
498498
}
499499

500500
counts_index = counts_index_for(h, value);
501-
502501
if (counts_index < 0 || h->counts_len <= counts_index)
503502
{
504503
return false;
@@ -514,7 +513,7 @@ bool hdr_record_values_atomic(struct hdr_histogram* h, int64_t value, int64_t co
514513
{
515514
int32_t counts_index;
516515

517-
if (value < 0)
516+
if (value < 0 || h->highest_trackable_value < value)
518517
{
519518
return false;
520519
}

test/hdr_histogram_atomic_test.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -435,8 +435,8 @@ static char* test_out_of_range_values(void)
435435
{
436436
struct hdr_histogram *h;
437437
hdr_init(1, 1000, 4, &h);
438-
mu_assert("Should successfully record value", hdr_record_value_atomic(h, 32767));
439-
mu_assert("Should not record value", !hdr_record_value_atomic(h, 32768));
438+
mu_assert("Should successfully record value", hdr_record_value_atomic(h, 1000));
439+
mu_assert("Should not record value", !hdr_record_value_atomic(h, 1001));
440440

441441
return 0;
442442
}

test/hdr_histogram_test.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -450,8 +450,8 @@ static char* test_out_of_range_values(void)
450450
{
451451
struct hdr_histogram *h;
452452
hdr_init(1, 1000, 4, &h);
453-
mu_assert("Should successfully record value", hdr_record_value(h, 32767));
454-
mu_assert("Should not record value", !hdr_record_value(h, 32768));
453+
mu_assert("Should successfully record value", hdr_record_value(h, 1000));
454+
mu_assert("Should not record value", !hdr_record_value(h, 1001));
455455

456456
return 0;
457457
}

0 commit comments

Comments
 (0)