From 34e7285b0a62e0a4daa74caecbaea2932b13c05b Mon Sep 17 00:00:00 2001 From: Sergey Sharybin Date: Fri, 4 Sep 2015 12:38:10 +0500 Subject: Cycles: Gracefully handle out-of-memory happening in device vector Currently only image loading benefits of this and will give magenta color when image manager detects it's running out of memory. This isn't ideal solution and can't handle all cases. For example, OOM killer might kill process before it realized it run out of memory, but in other cases this could prevent some crashes. Reviewers: juicyfruit, dingto Differential Revision: https://developer.blender.org/D1502 --- intern/cycles/util/util_vector.h | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) (limited to 'intern/cycles/util') diff --git a/intern/cycles/util/util_vector.h b/intern/cycles/util/util_vector.h index ee1f997721d..623436483a0 100644 --- a/intern/cycles/util/util_vector.h +++ b/intern/cycles/util/util_vector.h @@ -163,7 +163,7 @@ public: util_aligned_free(data); } - void resize(size_t newsize) + T* resize(size_t newsize) { if(newsize == 0) { clear(); @@ -171,7 +171,12 @@ public: else if(newsize != datasize) { if(newsize > capacity) { T *newdata = (T*)util_aligned_malloc(sizeof(T)*newsize, alignment); - if(data) { + if(newdata == NULL) { + /* Allocation failed, likely out of memory. */ + clear(); + return NULL; + } + else if(data) { memcpy(newdata, data, ((datasize < newsize)? datasize: newsize)*sizeof(T)); util_aligned_free(data); } @@ -180,12 +185,15 @@ public: } datasize = newsize; } + return data; } void clear() { - util_aligned_free(data); - data = NULL; + if(data != NULL) { + util_aligned_free(data); + data = NULL; + } datasize = 0; capacity = 0; } -- cgit v1.2.3