Skip to content

Commit 3215e86

Browse files
committed
Avoid double allocation in _bc_new_num_ex
Since the two allocations are tied together anyway, we can just use a single allocation. Moreover, this actually seemed like the intention because the bc_struct allocation already accounted for the length and scale.
1 parent f4dbe23 commit 3215e86

File tree

1 file changed

+2
-7
lines changed
  • ext/bcmath/libbcmath/src

1 file changed

+2
-7
lines changed

ext/bcmath/libbcmath/src/init.c

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,12 @@
3939
bc_num _bc_new_num_ex(size_t length, size_t scale, bool persistent)
4040
{
4141
/* PHP Change: malloc() -> pemalloc(), removed free_list code */
42-
bc_num temp = (bc_num) safe_pemalloc(1, sizeof(bc_struct) + length, scale, persistent);
42+
bc_num temp = safe_pemalloc(1, sizeof(bc_struct) + length, scale, persistent);
4343
temp->n_sign = PLUS;
4444
temp->n_len = length;
4545
temp->n_scale = scale;
4646
temp->n_refs = 1;
47-
/* PHP Change: malloc() -> pemalloc() */
48-
temp->n_ptr = (char *) safe_pemalloc(1, length, scale, persistent);
47+
temp->n_ptr = (char *) temp + sizeof(bc_struct);
4948
temp->n_value = temp->n_ptr;
5049
memset(temp->n_ptr, 0, length + scale);
5150
return temp;
@@ -61,10 +60,6 @@ void _bc_free_num_ex(bc_num *num, bool persistent)
6160
}
6261
(*num)->n_refs--;
6362
if ((*num)->n_refs == 0) {
64-
if ((*num)->n_ptr) {
65-
/* PHP Change: free() -> pefree(), removed free_list code */
66-
pefree((*num)->n_ptr, persistent);
67-
}
6863
pefree(*num, persistent);
6964
}
7065
*num = NULL;

0 commit comments

Comments
 (0)