ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
tsdfpool.cpp
Go to the documentation of this file.
1 /*
2  * $Id$
3  *
4  * Author: David Fournier
5  * Copyright (c) 2008-2012 Regents of the University of California
6  */
11 #if defined(THREAD_SAFE)
12 #include <fvar.hpp>
13 //#undef USE_VECTOR_SHAPE_POOL
14 
15 #include <dfpool.h>
16 
17 
18 #if defined(THREAD_SAFE)
19  extern pthread_mutex_t mutex_dfpool;
20  extern pthread_key_t admb_pthread_key;
21  //pthread_mutex_t mutex_dfpool = PTHREAD_MUTEX_INITIALIZER;
22  //pthread_key_t admb_pthread_key;
23 #endif
24 
25 tsdfpool::tsdfpool(int n) : dfpool(n) {}
26 
27 //ofstream xofs("allocation");
28 typedef ts_vector_shape_pool * pts_vector_shape_pool;
29 
30 
31 #if defined(USE_VECTOR_SHAPE_POOL)
32 ts_vector_shape_pool ** ts_vector_shapex::xpool = 0;
33 #endif
34 
39  void ts_vector_shapex::shift(int min)
40  {
41  index_max=index_max-index_min+min;
42  index_min=min;
43  }
44 
45 const int max_number_threads=10;
46 
51  int get_pthread_number(void)
52  {
53  int* p_keyval = (int*)pthread_getspecific(admb_pthread_key);
54  int tmp;
55  if (p_keyval)
56  {
57  tmp=*p_keyval;
58  }
59  else
60  {
61  tmp=0;
62  }
63  return tmp;
64  }
65 
66 #if defined(USE_VECTOR_SHAPE_POOL)
67 
72 void * ts_vector_shapex::operator new(size_t n)
73 {
74  if (xpool==0)
75  {
76  pthread_mutex_lock(&mutex_dfpool);
77  if (xpool==0)
78  {
79  xpool = new pts_vector_shape_pool[max_number_threads];
80  for (int i=0;i<max_number_threads;i++)
81  {
82  xpool[i]=0;
83  }
84  pthread_mutex_unlock(&mutex_dfpool);
85  }
86  }
87 
88  int pnum=get_pthread_number();
89  if (xpool[pnum]==0)
90  {
91  xpool[pnum]=new ts_vector_shape_pool(sizeof(ts_vector_shapex));
92  }
93 # if defined(SAFE_ALL)
94  if (n != xpool[pnum]->size)
95  {
96  cerr << "incorrect size requested in dfpool" << endl;
97  ad_exit(1);
98  }
99 # endif
100  return xpool[pnum]->alloc();
101 }
102 
107  void ts_vector_shapex::operator delete(void * ptr,size_t n)
108  {
109  xpool[get_pthread_number()]->free(ptr);
110  }
111 #endif // defined(THREAD_SAFE)
112 #endif // defined(THREAD_SAFE)
exitptr ad_exit
Definition: gradstrc.cpp:53
tsdfpool(int n)
prnstream & endl(prnstream &)
#define min(a, b)
Definition: cbivnorm.cpp:188
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Description not yet available.
Definition: fvar.hpp:497
Description not yet available.
Description not yet available.
Definition: dfpool.h:56