#include "BLI_arithb.h"
#include "BLI_blenlib.h"
+#include "BLI_threads.h"
-#include "MEM_guardedalloc.h"
#include "IMB_imbuf.h"
#include "IMB_imbuf_types.h"
static CompBuf *alloc_compbuf(int sizex, int sizey, int type, int alloc)
{
- CompBuf *cbuf= MEM_callocN(sizeof(CompBuf), "compbuf");
+ CompBuf *cbuf= MEM_callocT(sizeof(CompBuf), "compbuf");
cbuf->x= sizex;
cbuf->y= sizey;
cbuf->type= type;
if(alloc) {
if(cbuf->type==CB_RGBA)
- cbuf->rect= MEM_mallocN(4*sizeof(float)*sizex*sizey, "compbuf RGBA rect");
+ cbuf->rect= MEM_mallocT(4*sizeof(float)*sizex*sizey, "compbuf RGBA rect");
else
- cbuf->rect= MEM_mallocN(sizeof(float)*sizex*sizey, "compbuf Fac rect");
+ cbuf->rect= MEM_mallocT(sizeof(float)*sizex*sizey, "compbuf Fac rect");
cbuf->malloc= 1;
}
cbuf->disprect.xmin= 0;
void free_compbuf(CompBuf *cbuf)
{
if(cbuf->malloc && cbuf->rect)
- MEM_freeN(cbuf->rect);
- MEM_freeN(cbuf);
+ MEM_freeT(cbuf->rect);
+ MEM_freeT(cbuf);
}
void print_compbuf(char *str, CompBuf *cbuf)
CompBuf *outbuf, *zbuf=NULL;
if(rr->rectf)
- MEM_freeN(rr->rectf);
+ MEM_freeT(rr->rectf);
outbuf= alloc_compbuf(rr->rectx, rr->recty, CB_RGBA, 1);
if(in[1]->data==NULL)
if(in[2]->data) {
if(rr->rectz)
- MEM_freeN(rr->rectz);
+ MEM_freeT(rr->rectz);
zbuf= alloc_compbuf(rr->rectx, rr->recty, CB_VAL, 1);
composit1_pixel_processor(node, zbuf, in[2]->data, in[2]->vec, do_copy_value);
rr->rectz= zbuf->rect;
n = 2 * rad + 1;
- gausstab = (float *) MEM_mallocN(n * sizeof(float), "gauss");
+ gausstab = (float *) MEM_mallocT(n * sizeof(float), "gauss");
sum = 0.0f;
for (i = -rad; i <= rad; i++) {
n = 2 * rad + 1;
- bloomtab = (float *) MEM_mallocN(n * sizeof(float), "bloom");
+ bloomtab = (float *) MEM_mallocT(n * sizeof(float), "bloom");
for (i = -rad; i <= rad; i++) {
val = pow(1.0 - fabs((float)i)/((float)rad), 4.0);
}
/* vertical */
- MEM_freeN(gausstab);
+ MEM_freeT(gausstab);
rad = ceil(blury);
if(rad>imgy/2)
}
free_compbuf(work);
- MEM_freeN(gausstab);
+ MEM_freeT(gausstab);
}
/* reference has to be mapped 0-1, and equal in size */
rady= 1;
x= MAX2(radx, rady);
- maintabs= MEM_mallocN(x*sizeof(void *), "gauss array");
+ maintabs= MEM_mallocT(x*sizeof(void *), "gauss array");
for(i= 0; i<x; i++)
maintabs[i]= make_bloomtab(i+1);
x= MAX2(radx, rady);
for(i= 0; i<x; i++)
- MEM_freeN(maintabs[i]);
- MEM_freeN(maintabs);
+ MEM_freeT(maintabs[i]);
+ MEM_freeT(maintabs);
}
rady= 1;
x= MAX2(radx, rady);
- maintabs= MEM_mallocN(x*sizeof(void *), "gauss array");
+ maintabs= MEM_mallocT(x*sizeof(void *), "gauss array");
for(i= 0; i<x; i++)
maintabs[i]= make_gausstab(i+1);
x= MAX2(radx, rady);
for(i= 0; i<x; i++)
- MEM_freeN(maintabs[i]);
- MEM_freeN(maintabs);
+ MEM_freeT(maintabs[i]);
+ MEM_freeT(maintabs);
}
void BLI_init_threads (ListBase *threadbase, int (*do_thread)(void *), int tot);
int BLI_available_threads(ListBase *threadbase);
int BLI_available_thread_index(ListBase *threadbase);
-void BLI_insert_thread (ListBase *threadbase, void *callerdata);
+void BLI_insert_thread (ListBase *threadbase, void *callerdata);
void BLI_remove_thread (ListBase *threadbase, void *callerdata);
void BLI_end_threads (ListBase *threadbase);
-
+
+ /* threadsafe version of MEM_malloc and friends */
+void *MEM_mallocT(int len, char *name);
+void *MEM_callocT(int len, char *name);
+void MEM_freeT(void *poin);
#endif
BLI_end_threads(&lb);
************************************************ */
+static SDL_mutex *_malloc_lock= NULL;
/* just a max for security reasons */
#define RE_MAX_THREAD 8
int avail;
} ThreadSlot;
-static ThreadSlot threadslots[RE_MAX_THREAD];
-
void BLI_init_threads(ListBase *threadbase, int (*do_thread)(void *), int tot)
{
int a;
BLI_addtail(threadbase, tslot);
tslot->do_thread= do_thread;
}
+
+ _malloc_lock = SDL_CreateMutex();
}
/* amount of available threads */
}
}
BLI_freelistN(threadbase);
+
+ if(_malloc_lock) SDL_DestroyMutex(_malloc_lock);
+ _malloc_lock= NULL;
+}
+
+/* ***************** Thread safe MEM_malloc/calloc/free ************************** */
+
+void *MEM_mallocT(int len, char *name)
+{
+ void *mem;
+ if(_malloc_lock) SDL_mutexP(_malloc_lock);
+ mem= MEM_mallocN(len, name);
+ if(_malloc_lock) SDL_mutexV(_malloc_lock);
+ return mem;
+}
+void *MEM_callocT(int len, char *name)
+{
+ void *mem;
+ if(_malloc_lock) SDL_mutexP(_malloc_lock);
+ mem= MEM_callocN(len, name);
+ if(_malloc_lock) SDL_mutexV(_malloc_lock);
+ return mem;
}
+void MEM_freeT(void *poin)
+{
+ if(_malloc_lock) SDL_mutexP(_malloc_lock);
+ MEM_freeN(poin);
+ if(_malloc_lock) SDL_mutexV(_malloc_lock);
+}
+
/* eof */