Skip to content
This repository was archived by the owner on Oct 13, 2020. It is now read-only.

Commit 9904492

Browse files
author
Christian Hergert
committed
util: make bson_next_power_of_two() take a size_t.
1 parent 2fc0f1a commit 9904492

File tree

5 files changed

+55
-13
lines changed

5 files changed

+55
-13
lines changed

src/bson/bson-json.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ _bson_json_buf_ensure (bson_json_buf_t *buf, /* IN */
295295
if (buf->n_bytes < len) {
296296
bson_free (buf->buf);
297297

298-
buf->n_bytes = bson_next_power_of_two ((uint32_t)len);
298+
buf->n_bytes = bson_next_power_of_two (len);
299299
buf->buf = bson_malloc (buf->n_bytes);
300300
}
301301
}

src/bson/bson-string.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ bson_string_new (const char *str) /* IN */
6363
ret->alloc = ret->len + 1;
6464

6565
if (!bson_is_power_of_two (ret->alloc)) {
66-
ret->alloc = bson_next_power_of_two (ret->alloc);
66+
ret->alloc = bson_next_power_of_two ((size_t)ret->alloc);
6767
}
6868

6969
BSON_ASSERT (ret->alloc >= 1);
@@ -151,7 +151,7 @@ bson_string_append (bson_string_t *string, /* IN */
151151
if ((string->alloc - string->len - 1) < len) {
152152
string->alloc += len;
153153
if (!bson_is_power_of_two (string->alloc)) {
154-
string->alloc = bson_next_power_of_two (string->alloc);
154+
string->alloc = bson_next_power_of_two ((size_t)string->alloc);
155155
}
156156
string->str = bson_realloc (string->str, string->alloc);
157157
}
@@ -306,7 +306,7 @@ bson_string_truncate (bson_string_t *string, /* IN */
306306
}
307307

308308
if (!bson_is_power_of_two (alloc)) {
309-
alloc = bson_next_power_of_two (alloc);
309+
alloc = bson_next_power_of_two ((size_t)alloc);
310310
}
311311

312312
string->str = bson_realloc (string->str, alloc);

src/bson/bson-types.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -488,15 +488,18 @@ BSON_STATIC_ASSERT (sizeof (bson_error_t) == 512);
488488
*
489489
* Returns: The next power of 2 from @v.
490490
*/
491-
static BSON_INLINE uint32_t
492-
bson_next_power_of_two (uint32_t v)
491+
static BSON_INLINE size_t
492+
bson_next_power_of_two (size_t v)
493493
{
494494
v--;
495495
v |= v >> 1;
496496
v |= v >> 2;
497497
v |= v >> 4;
498498
v |= v >> 8;
499499
v |= v >> 16;
500+
#if BSON_WORD_SIZE == 64
501+
v |= v >> 32;
502+
#endif
500503
v++;
501504

502505
return v;

src/bson/bson.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ static const uint8_t gZero;
8686

8787
static bool
8888
_bson_impl_inline_grow (bson_impl_inline_t *impl, /* IN */
89-
uint32_t size) /* IN */
89+
size_t size) /* IN */
9090
{
9191
bson_impl_alloc_t *alloc = (bson_impl_alloc_t *)impl;
9292
uint8_t *data;
@@ -96,7 +96,7 @@ _bson_impl_inline_grow (bson_impl_inline_t *impl, /* IN */
9696
BSON_ASSERT (!(impl->flags & BSON_FLAG_RDONLY));
9797
BSON_ASSERT (!(impl->flags & BSON_FLAG_CHILD));
9898

99-
if ((impl->len + size) <= sizeof impl->data) {
99+
if (((size_t)impl->len + size) <= sizeof impl->data) {
100100
return true;
101101
}
102102

@@ -143,25 +143,25 @@ _bson_impl_inline_grow (bson_impl_inline_t *impl, /* IN */
143143

144144
static bool
145145
_bson_impl_alloc_grow (bson_impl_alloc_t *impl, /* IN */
146-
uint32_t size) /* IN */
146+
size_t size) /* IN */
147147
{
148-
uint32_t req;
148+
size_t req;
149149

150150
BSON_ASSERT (impl);
151151

152152
/*
153153
* Determine how many bytes we need for this document in the buffer
154154
* including necessary trailing bytes for parent documents.
155155
*/
156-
req = (uint32_t)(impl->offset + impl->len + size + impl->depth);
156+
req = (impl->offset + impl->len + size + impl->depth);
157157

158158
if (req <= *impl->buflen) {
159159
return true;
160160
}
161161

162162
req = bson_next_power_of_two (req);
163163

164-
if ((int32_t)req <= INT32_MAX && impl->realloc) {
164+
if ((req <= INT32_MAX) && impl->realloc) {
165165
*impl->buf = impl->realloc (*impl->buf, req, impl->realloc_func_ctx);
166166
*impl->buflen = req;
167167
return true;
@@ -2005,7 +2005,7 @@ bson_copy_to (const bson_t *src,
20052005
}
20062006

20072007
data = _bson_data (src);
2008-
len = bson_next_power_of_two (src->len);
2008+
len = bson_next_power_of_two ((size_t)src->len);
20092009

20102010
adst = (bson_impl_alloc_t *)dst;
20112011
adst->flags = BSON_FLAG_STATIC;

tests/test-bson.c

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1356,6 +1356,43 @@ test_bson_has_field (void)
13561356
}
13571357

13581358

1359+
static void
1360+
test_next_power_of_two (void)
1361+
{
1362+
size_t s;
1363+
1364+
s = 3;
1365+
s = bson_next_power_of_two (s);
1366+
assert (s == 4);
1367+
1368+
s = 4;
1369+
s = bson_next_power_of_two (s);
1370+
assert (s == 4);
1371+
1372+
s = 33;
1373+
s = bson_next_power_of_two (s);
1374+
assert (s == 64);
1375+
1376+
s = 91;
1377+
s = bson_next_power_of_two (s);
1378+
assert (s == 128);
1379+
1380+
#if BSON_WORD_SIZE == 64
1381+
s = 4294967296LL;
1382+
s = bson_next_power_of_two (s);
1383+
assert (s == 4294967296LL);
1384+
1385+
s = 4294967297LL;
1386+
s = bson_next_power_of_two (s);
1387+
assert (s == 8589934592LL);
1388+
1389+
s = 17179901952LL;
1390+
s = bson_next_power_of_two (s);
1391+
assert (s == 34359738368LL);
1392+
#endif
1393+
}
1394+
1395+
13591396
void
13601397
test_bson_install (TestSuite *suite)
13611398
{
@@ -1409,4 +1446,6 @@ test_bson_install (TestSuite *suite)
14091446
TestSuite_Add (suite, "/bson/clear", test_bson_clear);
14101447
TestSuite_Add (suite, "/bson/destroy_with_steal", test_bson_destroy_with_steal);
14111448
TestSuite_Add (suite, "/bson/has_field", test_bson_has_field);
1449+
1450+
TestSuite_Add (suite, "/util/next_power_of_two", test_next_power_of_two);
14121451
}

0 commit comments

Comments
 (0)