Skip to content

Commit 4f1a2ff

Browse files
authored
Merge pull request #612 from hyanwong/fix-block-size-alloc
Calculate max size of memory requested
2 parents b3f3739 + 9a407cd commit 4f1a2ff

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

lib/ancestor_builder.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ ancestor_builder_alloc(
125125
ancestor_builder_t *self, size_t num_samples, size_t max_sites, int flags)
126126
{
127127
int ret = 0;
128+
unsigned long max_size = 1024 * 1024;
128129

129130
memset(self, 0, sizeof(ancestor_builder_t));
130131
if (num_samples <= 1) {
@@ -142,7 +143,13 @@ ancestor_builder_alloc(
142143
ret = TSI_ERR_NO_MEMORY;
143144
goto out;
144145
}
145-
ret = tsk_blkalloc_init(&self->allocator, 1024 * 1024);
146+
/* Pre-calculate the maximum sizes asked for in other methods when calling
147+
* tsk_blkalloc_get(&self->allocator, ...) */
148+
max_size = TSK_MAX(self->num_samples * sizeof(allele_t), max_size);
149+
/* NB: using self->max_sites below is probably overkill: the real number should be
150+
* the maximum number of focal sites in a single ancestor, usually << max_sites */
151+
max_size = TSK_MAX(self->max_sites * sizeof(tsk_id_t), max_size);
152+
ret = tsk_blkalloc_init(&self->allocator, max_size);
146153
if (ret != 0) {
147154
goto out;
148155
}

0 commit comments

Comments
 (0)