Skip to content

Commit 7e939fb

Browse files
authored
Fix handling of additional buffer structures in case of overflow
1 parent bb2f1ec commit 7e939fb

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

driver/others/memory.c

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
7373

7474
#include "common.h"
7575

76+
#define NEW_BUFFERS 512
7677
#ifndef likely
7778
#ifdef __GNUC__
7879
#define likely(x) __builtin_expect(!!(x), 1)
@@ -2897,7 +2898,7 @@ void *blas_memory_alloc(int procpos){
28972898
#endif
28982899
position ++;
28992900

2900-
} while (position < 512+NUM_BUFFERS);
2901+
} while (position < NEW_BUFFERS + NUM_BUFFERS);
29012902
}
29022903
#if (defined(SMP) || defined(USE_LOCKING)) && !defined(USE_OPENMP)
29032904
UNLOCK_COMMAND(&alloc_lock);
@@ -3014,9 +3015,10 @@ void *blas_memory_alloc(int procpos){
30143015
fprintf(stderr,"To avoid this warning, please rebuild your copy of OpenBLAS with a larger NUM_THREADS setting\n");
30153016
fprintf(stderr,"or set the environment variable OPENBLAS_NUM_THREADS to %d or lower\n", MAX_CPU_NUMBER);
30163017
memory_overflowed=1;
3017-
new_release_info = (struct release_t*) malloc(512*sizeof(struct release_t));
3018-
newmemory = (struct newmemstruct*) malloc(512*sizeof(struct newmemstruct));
3019-
for (i = 0; i < 512; i++) {
3018+
MB;
3019+
new_release_info = (struct release_t*) malloc(NEW_BUFFERS * sizeof(struct release_t));
3020+
newmemory = (struct newmemstruct*) malloc(NEW_BUFFERS * sizeof(struct newmemstruct));
3021+
for (i = 0; i < NEW_BUFFERS; i++) {
30203022
newmemory[i].addr = (void *)0;
30213023
#if defined(WHEREAMI) && !defined(USE_OPENMP)
30223024
newmemory[i].pos = -1;
@@ -3129,12 +3131,12 @@ void blas_memory_free(void *free_area){
31293131
printf(" Position : %d\n", position);
31303132
#endif
31313133
if (unlikely(memory_overflowed && position >= NUM_BUFFERS)) {
3132-
while ((position < NUM_BUFFERS+512) && (newmemory[position-NUM_BUFFERS].addr != free_area))
3134+
while ((position < NUM_BUFFERS+NEW_BUFFERS) && (newmemory[position-NUM_BUFFERS].addr != free_area))
31333135
position++;
31343136
// arm: ensure all writes are finished before other thread takes this memory
31353137
WMB;
3136-
3137-
newmemory[position].used = 0;
3138+
if (position - NUM_BUFFERS >= NEW_BUFFERS) goto error;
3139+
newmemory[position-NUM_BUFFERS].used = 0;
31383140
#if (defined(SMP) || defined(USE_LOCKING)) && !defined(USE_OPENMP)
31393141
UNLOCK_COMMAND(&alloc_lock);
31403142
#endif
@@ -3213,7 +3215,7 @@ void blas_shutdown(void){
32133215
memory[pos].lock = 0;
32143216
}
32153217
if (memory_overflowed)
3216-
for (pos = 0; pos < 512; pos ++){
3218+
for (pos = 0; pos < NEW_BUFFERS; pos ++){
32173219
newmemory[pos].addr = (void *)0;
32183220
newmemory[pos].used = 0;
32193221
#if defined(WHEREAMI) && !defined(USE_OPENMP)

0 commit comments

Comments
 (0)