From 964687d1b4277a9e4c6a98c54b48b172b379487b Mon Sep 17 00:00:00 2001 From: augustin64 Date: Fri, 19 May 2023 17:43:56 +0200 Subject: [PATCH] mem management:avoid potential infinite allocation --- src/common/memory_management.c | 3 +-- src/common/memory_management.cu | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/src/common/memory_management.c b/src/common/memory_management.c index 45d7d9c..afd59a3 100644 --- a/src/common/memory_management.c +++ b/src/common/memory_management.c @@ -136,8 +136,7 @@ void* allocate_memory(int nb_elements, size_t size, Memory* mem) { //printf("Mémoire disponible: %ld. Nécessaire: %ld\n", mem->size - ((intptr_t)mem->cursor - (intptr_t)mem->start), nb_elements*size); // Sinon on continue sur l'élément suivant de la liste if (!mem->next) { - //! WARNING: May cause Infinite allocations when trying to allocate more than MEMORY_BLOCK size at once that is not naturally aligned (CUDA only) - mem->next = create_memory_block(MEMORY_BLOCK < nb_elements*size ? nb_elements*size : MEMORY_BLOCK); + mem->next = create_memory_block(MEMORY_BLOCK < (nb_elements+1)*size ? (nb_elements+1)*size : MEMORY_BLOCK); } return allocate_memory(nb_elements, size, mem->next); } diff --git a/src/common/memory_management.cu b/src/common/memory_management.cu index 45d7d9c..afd59a3 100644 --- a/src/common/memory_management.cu +++ b/src/common/memory_management.cu @@ -136,8 +136,7 @@ void* allocate_memory(int nb_elements, size_t size, Memory* mem) { //printf("Mémoire disponible: %ld. Nécessaire: %ld\n", mem->size - ((intptr_t)mem->cursor - (intptr_t)mem->start), nb_elements*size); // Sinon on continue sur l'élément suivant de la liste if (!mem->next) { - //! WARNING: May cause Infinite allocations when trying to allocate more than MEMORY_BLOCK size at once that is not naturally aligned (CUDA only) - mem->next = create_memory_block(MEMORY_BLOCK < nb_elements*size ? nb_elements*size : MEMORY_BLOCK); + mem->next = create_memory_block(MEMORY_BLOCK < (nb_elements+1)*size ? (nb_elements+1)*size : MEMORY_BLOCK); } return allocate_memory(nb_elements, size, mem->next); }