ADMB Documentation  11.5.3197
 All Classes Files Functions Variables Typedefs Friends Defines
tsdfpool.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id$
00003  *
00004  * Author: David Fournier
00005  * Copyright (c) 2008-2012 Regents of the University of California
00006  */
00011 #if defined(THREAD_SAFE)
00012 #include <fvar.hpp>
00013 //#undef USE_VECTOR_SHAPE_POOL
00014 
00015 #include <dfpool.h>
00016 
00017 
00018 #if defined(THREAD_SAFE)
00019   extern pthread_mutex_t mutex_dfpool;
00020   extern pthread_key_t admb_pthread_key;
00021   //pthread_mutex_t mutex_dfpool = PTHREAD_MUTEX_INITIALIZER;
00022   //pthread_key_t admb_pthread_key;
00023 #endif
00024 
00025 tsdfpool::tsdfpool(int n) : dfpool(n) {}
00026 
00027 //ofstream xofs("allocation");
00028 typedef ts_vector_shape_pool * pts_vector_shape_pool;
00029 
00030 
00031 #if defined(USE_VECTOR_SHAPE_POOL)
00032 ts_vector_shape_pool ** ts_vector_shapex::xpool = 0;
00033 #endif
00034 
00039   void ts_vector_shapex::shift(int min)
00040   {
00041     index_max=index_max-index_min+min;
00042     index_min=min;
00043   }
00044 
00045 const int max_number_threads=10;
00046 
00051   int get_pthread_number(void)
00052   {
00053     int* p_keyval = (int*)pthread_getspecific(admb_pthread_key);
00054      int tmp;
00055     if (p_keyval)
00056     {
00057       tmp=*p_keyval;
00058     }
00059     else
00060     {
00061       tmp=0;
00062     }
00063     return tmp;
00064   }
00065 
00066 #if defined(USE_VECTOR_SHAPE_POOL)
00067 
00072 void * ts_vector_shapex::operator new(size_t n)
00073 {
00074   if (xpool==0)
00075   {
00076     pthread_mutex_lock(&mutex_dfpool);
00077     if (xpool==0)
00078     {
00079       xpool = new pts_vector_shape_pool[max_number_threads];
00080       for (int i=0;i<max_number_threads;i++)
00081       {
00082         xpool[i]=0;
00083       }
00084       pthread_mutex_unlock(&mutex_dfpool);
00085     }
00086   }
00087 
00088   int pnum=get_pthread_number();
00089   if (xpool[pnum]==0)
00090   {
00091     xpool[pnum]=new ts_vector_shape_pool(sizeof(ts_vector_shapex));
00092   }
00093 # if defined(SAFE_ALL)
00094   if (n != xpool[pnum]->size)
00095   {
00096     cerr << "incorrect size requested in dfpool" << endl;
00097     ad_exit(1);
00098   }
00099 # endif
00100   return xpool[pnum]->alloc();
00101 }
00102 
00107   void ts_vector_shapex::operator delete(void * ptr,size_t n)
00108   {
00109     xpool[get_pthread_number()]->free(ptr);
00110   }
00111 #endif // defined(THREAD_SAFE)
00112 #endif // defined(THREAD_SAFE)