Skip to content

Commit 6cdb790

Browse files
committed
Split into two versions for bson_malloc0 calls; alignment no longer used
1 parent cccae5d commit 6cdb790

File tree

8 files changed

+56
-20
lines changed

8 files changed

+56
-20
lines changed

src/libbson/src/bson/memory.c

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -236,13 +236,13 @@ bson_aligned_alloc0(size_t alignment /* IN */, size_t num_bytes /* IN */)
236236
*
237237
* bson_array_alloc --
238238
*
239-
* Allocates zero-filled, aligned memory for an array of objects,
240-
* checking for cases of n = 0 and integer overflow in type_size * len,
241-
* in which case NULL is returned.
239+
* Allocates memory for an array of objects, checking for cases of
240+
* n = 0 and integer overflow in type_size * len, in which case NULL
241+
* is returned.
242242
*
243243
* Parameters:
244244
* @type_size: The size of each object's type in bytes.
245-
* @len: The number of objects to allocate.
245+
* @num_elems: The number of objects to allocate.
246246
*
247247
* Returns:
248248
* A pointer if successful; otherwise abort() is called and this
@@ -255,13 +255,47 @@ bson_aligned_alloc0(size_t alignment /* IN */, size_t num_bytes /* IN */)
255255
*/
256256

257257
void *
258-
bson_array_alloc(size_t type_size /* IN */, size_t len /* IN */)
258+
bson_array_alloc(size_t type_size /* IN */, size_t num_elems /* IN */)
259259
{
260260
void *mem = NULL;
261-
size_t num_bytes = type_size * len;
261+
size_t num_bytes = type_size * num_elems;
262262

263-
if (BSON_LIKELY(num_bytes) && BSON_LIKELY(type_size == num_bytes / len)) {
264-
mem = bson_aligned_alloc0(BSON_ALIGNOF(type_size), num_bytes);
263+
if (BSON_LIKELY(num_bytes) && BSON_LIKELY(num_elems == num_bytes / type_size)) {
264+
mem = bson_malloc(num_bytes);
265+
}
266+
return mem;
267+
}
268+
269+
/*
270+
*--------------------------------------------------------------------------
271+
*
272+
* bson_array_alloc0 --
273+
*
274+
* Like bson_array_alloc() except the memory is zeroed after allocation
275+
* for convenience.
276+
*
277+
* Parameters:
278+
* @type_size: The size of each object's type in bytes.
279+
* @num_elems: The number of objects to allocate.
280+
*
281+
* Returns:
282+
* A pointer if successful; otherwise abort() is called and this
283+
* function will never return.
284+
*
285+
* Side effects:
286+
* None.
287+
*
288+
*--------------------------------------------------------------------------
289+
*/
290+
291+
void *
292+
bson_array_alloc0(size_t type_size /* IN */, size_t num_elems /* IN */)
293+
{
294+
void *mem = NULL;
295+
size_t num_bytes = type_size * num_elems;
296+
297+
if (BSON_LIKELY(num_bytes) && BSON_LIKELY(num_elems == num_bytes / type_size)) {
298+
mem = bson_malloc0(num_bytes);
265299
}
266300
return mem;
267301
}

src/libbson/src/bson/memory.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ bson_aligned_alloc(size_t alignment, size_t num_bytes);
4848
BSON_EXPORT(void *)
4949
bson_aligned_alloc0(size_t alignment, size_t num_bytes);
5050
BSON_EXPORT(void *)
51-
bson_array_alloc(size_t type_size, size_t len);
51+
bson_array_alloc(size_t type_size, size_t num_elems);
52+
BSON_EXPORT(void *)
53+
bson_array_alloc0(size_t type_size, size_t num_elems);
5254
BSON_EXPORT(void *)
5355
bson_realloc(void *mem, size_t num_bytes);
5456
BSON_EXPORT(void *)

src/libbson/src/jsonsl/jsonsl.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1054,7 +1054,7 @@ void jsonsl_jpr_match_state_init(jsonsl_t jsn,
10541054
}
10551055
jsn->jprs = (jsonsl_jpr_t *) bson_array_alloc (sizeof (jsonsl_jpr_t), njprs);
10561056
jsn->jpr_count = njprs;
1057-
jsn->jpr_root = (size_t *) bson_array_alloc (sizeof (size_t), njprs * jsn->levels_max);
1057+
jsn->jpr_root = (size_t *) bson_array_alloc0 (sizeof (size_t), njprs * jsn->levels_max);
10581058
memcpy(jsn->jprs, jprs, sizeof(jsonsl_jpr_t) * njprs);
10591059
/* Set the initial jump table values */
10601060

src/libbson/tests/test-bson-vector.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1392,7 +1392,7 @@ test_bson_vector_edge_cases_int8(void)
13921392
// Test some read and write boundaries.
13931393
{
13941394
size_t values_size = 100;
1395-
int8_t *values = bson_array_alloc(sizeof *values, values_size);
1395+
int8_t *values = bson_array_alloc0(sizeof *values, values_size);
13961396
TEST_BSON_VECTOR_EDGE_CASES_RW_COMMON(
13971397
view, max_alloc_elements, values, values_size, bson_vector_int8_view_read, bson_vector_int8_view_write);
13981398
bson_free(values);
@@ -1439,7 +1439,7 @@ test_bson_vector_edge_cases_float32(void)
14391439
// Test some read and write boundaries.
14401440
{
14411441
size_t values_size = 100;
1442-
float *values = bson_array_alloc(sizeof *values, values_size);
1442+
float *values = bson_array_alloc0(sizeof *values, values_size);
14431443
TEST_BSON_VECTOR_EDGE_CASES_RW_COMMON(
14441444
view, max_alloc_elements, values, values_size, bson_vector_float32_view_read, bson_vector_float32_view_write);
14451445
bson_free(values);
@@ -1506,7 +1506,7 @@ test_bson_vector_edge_cases_packed_bit(void)
15061506
// Only tests one length, but it's chosen to be greater than 8 and not a multiple of 8.
15071507
{
15081508
size_t values_size = 190;
1509-
bool *values = bson_array_alloc(sizeof *values, values_size);
1509+
bool *values = bson_array_alloc0(sizeof *values, values_size);
15101510
TEST_BSON_VECTOR_EDGE_CASES_RW_COMMON(view,
15111511
max_alloc_elements,
15121512
values,

src/libmongoc/src/mongoc/mongoc-cluster-sspi.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ _mongoc_cluster_sspi_new(mongoc_uri_t *uri, mongoc_stream_t *stream, const char
6565
service_ascii_len = strlen(service_ascii);
6666

6767
/* this is donated to the sspi */
68-
service = bson_array_alloc(sizeof(WCHAR), (service_ascii_len + 1));
68+
service = bson_array_alloc0(sizeof(WCHAR), (service_ascii_len + 1));
6969
service_len =
7070
MultiByteToWideChar(CP_UTF8, 0, service_ascii, (int)service_ascii_len, service, (int)service_ascii_len);
7171
service[service_len] = L'\0';
@@ -75,7 +75,7 @@ _mongoc_cluster_sspi_new(mongoc_uri_t *uri, mongoc_stream_t *stream, const char
7575
tmp_creds_len = strlen(state->sasl.pass);
7676

7777
/* this is donated to the sspi */
78-
pass = bson_array_alloc(sizeof(WCHAR), (tmp_creds_len + 1));
78+
pass = bson_array_alloc0(sizeof(WCHAR), (tmp_creds_len + 1));
7979
pass_len = MultiByteToWideChar(CP_UTF8, 0, state->sasl.pass, (int)tmp_creds_len, pass, (int)tmp_creds_len);
8080
pass[pass_len] = L'\0';
8181
}
@@ -84,7 +84,7 @@ _mongoc_cluster_sspi_new(mongoc_uri_t *uri, mongoc_stream_t *stream, const char
8484
tmp_creds_len = strlen(state->sasl.user);
8585

8686
/* this is donated to the sspi */
87-
user = bson_array_alloc(sizeof(WCHAR), (tmp_creds_len + 1));
87+
user = bson_array_alloc0(sizeof(WCHAR), (tmp_creds_len + 1));
8888
user_len = MultiByteToWideChar(CP_UTF8, 0, state->sasl.user, (int)tmp_creds_len, user, (int)tmp_creds_len);
8989
user[user_len] = L'\0';
9090
}

src/libmongoc/src/mongoc/mongoc-server-description.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -987,7 +987,7 @@ mongoc_server_description_filter_tags(const mongoc_server_description_t **descri
987987
return;
988988
}
989989

990-
sd_matched = (bool *)bson_array_alloc(sizeof(bool), description_len);
990+
sd_matched = (bool *)bson_array_alloc0(sizeof(bool), description_len);
991991

992992
bson_iter_init(&rp_tagset_iter, rp_tags);
993993

src/libmongoc/src/mongoc/mongoc-topology-background-monitoring.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ _remove_orphaned_server_monitors(mongoc_set_t *server_monitors, mongoc_set_t *se
183183

184184
/* Signal shutdown to server monitors no longer in the topology description.
185185
*/
186-
server_monitor_ids_to_remove = bson_array_alloc(sizeof(uint32_t), server_monitors->items_len);
186+
server_monitor_ids_to_remove = bson_array_alloc0(sizeof(uint32_t), server_monitors->items_len);
187187
for (size_t i = 0u; i < server_monitors->items_len; i++) {
188188
mongoc_server_monitor_t *server_monitor;
189189
uint32_t id;

src/libmongoc/src/mongoc/mongoc-topology-description.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -798,7 +798,7 @@ mongoc_topology_description_suitable_servers(mongoc_array_t *set, /* OUT */
798798
.topology_type = topology->type,
799799
.has_secondary = false,
800800
.candidates_len = 0,
801-
.candidates = bson_array_alloc(sizeof(mongoc_server_description_t *), td_servers->items_len),
801+
.candidates = bson_array_alloc0(sizeof(mongoc_server_description_t *), td_servers->items_len),
802802
};
803803

804804
/* The "effective" read mode is the read mode that we should behave for, and
@@ -2370,7 +2370,7 @@ mongoc_topology_description_get_servers(const mongoc_topology_description_t *td,
23702370
{
23712371
const mongoc_set_t *const set = mc_tpld_servers_const(BSON_ASSERT_PTR_INLINE(td));
23722372
/* enough room for all descriptions, even if some are unknown */
2373-
mongoc_server_description_t **sds = bson_array_alloc(sizeof(mongoc_server_description_t *), set->items_len);
2373+
mongoc_server_description_t **sds = bson_array_alloc0(sizeof(mongoc_server_description_t *), set->items_len);
23742374

23752375
BSON_ASSERT_PARAM(n);
23762376

0 commit comments

Comments
 (0)