Fix msvc 2013 compiler errors after the ingenious cleanup in 4ca67869cc7a.
[blender.git] / source / blender / imbuf / intern / allocimbuf.c
index d7ca381bae6aa314bb24464133834876c951cd2a..556c4beeae7a523c5252c35b529a81c3a66e921b 100644 (file)
 #include "imbuf.h"
 
 #include "MEM_guardedalloc.h"
-#include "MEM_CacheLimiterC-Api.h"
 
 #include "BLI_utildefines.h"
+#include "BLI_threads.h"
+
+static SpinLock refcounter_spin;
+
+void imb_refcounter_lock_init(void)
+{
+       BLI_spin_init(&refcounter_spin);
+}
+
+void imb_refcounter_lock_exit(void)
+{
+       BLI_spin_end(&refcounter_spin);
+}
 
 void imb_freemipmapImBuf(ImBuf *ibuf)
 {
@@ -154,10 +166,18 @@ void IMB_freezbuffloatImBuf(ImBuf *ibuf)
 void IMB_freeImBuf(ImBuf *ibuf)
 {
        if (ibuf) {
+               bool needs_free = false;
+
+               BLI_spin_lock(&refcounter_spin);
                if (ibuf->refcounter > 0) {
                        ibuf->refcounter--;
                }
                else {
+                       needs_free = true;
+               }
+               BLI_spin_unlock(&refcounter_spin);
+
+               if (needs_free) {
                        imb_freerectImBuf(ibuf);
                        imb_freerectfloatImBuf(ibuf);
                        imb_freetilesImBuf(ibuf);
@@ -177,7 +197,9 @@ void IMB_freeImBuf(ImBuf *ibuf)
 
 void IMB_refImBuf(ImBuf *ibuf)
 {
+       BLI_spin_lock(&refcounter_spin);
        ibuf->refcounter++;
+       BLI_spin_unlock(&refcounter_spin);
 }
 
 ImBuf *IMB_makeSingleUser(ImBuf *ibuf)