Skip to content
Snippets Groups Projects
Commit d61316c5 authored by Martin Larralde's avatar Martin Larralde
Browse files

Fix array reallocation formula causing segfaults in `nhmmer` (#62)

parent 824962da
No related branches found
No related tags found
No related merge requests found
Pipeline #56809 passed
......@@ -3,11 +3,6 @@ cdef inline size_t new_capacity(size_t capacity, size_t length) nogil:
Compute a new capacity for a buffer reallocation.
This is how CPython computes the allocation sizes for the array storing
the references for a `list` object.
"""
cdef size_t new_cap = (<size_t> capacity + (capacity >> 3) + 6) & ~(<size_t> 3)
if (capacity - length) > (new_cap - capacity):
new_cap = (<size_t> capacity + 3) & ~(<size_t> 3)
return new_cap
\ No newline at end of file
return (<size_t> capacity + (capacity >> 3) + 6) & ~(<size_t> 3)
from libc.stdint cimport int64_t, uint64_t
from libc.stdlib cimport calloc, malloc, realloc, free
from libc.stdio cimport printf
cimport libeasel
from libhmmer.p7_tophits cimport P7_TOPHITS
......@@ -19,7 +18,7 @@ ctypedef struct ID_LENGTH_LIST:
size_t size
cdef inline ID_LENGTH_LIST* idlen_list_init(size_t size) nogil:
cdef inline ID_LENGTH_LIST* idlen_list_init(size_t size) noexcept nogil:
cdef ID_LENGTH_LIST* l = <ID_LENGTH_LIST*> malloc(sizeof(ID_LENGTH_LIST))
if l != NULL:
l.count = 0
......@@ -30,13 +29,13 @@ cdef inline ID_LENGTH_LIST* idlen_list_init(size_t size) nogil:
l = NULL
return l
cdef inline void idlen_list_destroy(ID_LENGTH_LIST* l) nogil:
cdef inline void idlen_list_destroy(ID_LENGTH_LIST* l) noexcept nogil:
if l != NULL:
if l.id_lengths != NULL:
free(l.id_lengths)
free(l)
cdef inline int idlen_list_add(ID_LENGTH_LIST* l, int64_t id, int64_t L) nogil:
cdef inline int idlen_list_add(ID_LENGTH_LIST* l, int64_t id, int64_t L) noexcept nogil:
if l.count > 0 and l.id_lengths[l.count - 1].id == id:
l.id_lengths[l.count - 1].length = L
else:
......@@ -50,7 +49,7 @@ cdef inline int idlen_list_add(ID_LENGTH_LIST* l, int64_t id, int64_t L) nogil:
l.count += 1
return libeasel.eslOK
cdef inline int idlen_list_assign(ID_LENGTH_LIST* l, P7_TOPHITS* th) nogil:
cdef inline int idlen_list_assign(ID_LENGTH_LIST* l, P7_TOPHITS* th) noexcept nogil:
cdef uint64_t i
cdef int64_t j = 0
for i in range(th.N):
......@@ -61,5 +60,5 @@ cdef inline int idlen_list_assign(ID_LENGTH_LIST* l, P7_TOPHITS* th) nogil:
th.hit[i].dcl[0].ad.L = l.id_lengths[j].length
return libeasel.eslOK
cdef inline int idlen_list_clear(ID_LENGTH_LIST* l) nogil:
cdef inline int idlen_list_clear(ID_LENGTH_LIST* l) noexcept nogil:
l.count = 0
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment