Main Page   Class Hierarchy   Data Structures   File List   Data Fields   Globals  

pdyn_alloc.C

Go to the documentation of this file.
00001 
00002        //=======================================================//    _\|/_
00003       //  __  _____           ___                    ___       //      /|\
00004      //  /      |      ^     |   \  |         ^     |   \     //          _\|/_
00005     //   \__    |     / \    |___/  |        / \    |___/    //            /|\
00006    //       \   |    /___\   |  \   |       /___\   |   \   // _\|/_
00007   //     ___/   |   /     \  |   \  |____  /     \  |___/  //   /|\     
00008  //                                                       //            _\|/_
00009 //=======================================================//              /|\
00010 
00014 
00015 // Externally visible functions:
00016 //
00017 //      new_pdyn
00018 //      new_pdyn2
00019 //      alloc_pdyn
00020 
00021 #include "pdyn.h"
00022 
00023 // Note: pdyn_mem is the base node of the pdyn management scheme; if it
00024 // is non-NULL, we are doing our own memory management.
00025 
00026 static int  n_pdyn_mem = 0;             // total number of available nodes
00027 
00028 // Static arrays (pdyn, flag, pointer), length n_pdyn_mem, index i.
00029 // Could be orgainzed as an array of structures, but this seems simpler.
00030 
00031 static pdyn *pdyn_mem  = NULL;          // array of all nodes
00032 static bool *pdyn_free = NULL;          // true iff node #i is available
00033 static int  *pdyn_ptr  = NULL;          // pointer into pdyn_alloc
00034 
00035 // Management array (pointer into the static array of nodes), index j.
00036 
00037 static int  n_alloc = 0;                // number of allocated nodes
00038 static int  *pdyn_alloc = NULL;         // nodes 0 to n_alloc-1 are allocated,
00039                                         // the remainder are free
00040 
00041 // Pointers are such that       pdyn_alloc[pdyn_ptr[i]] = i
00042 //                              pdyn_ptr[pdyn_alloc[j]] = j
00043 
00044 void dealloc_all()
00045 {
00046     // Quick dealloc; don't clear stories or bases (careful!).
00047 
00048     n_alloc = 0;
00049     for (int i = 0; i < n_pdyn_mem; i++) {
00050         pdyn_free[i] = true;
00051         pdyn_ptr[i] = i;
00052         pdyn_alloc[i] = i;
00053     }
00054 }
00055 
00056 bool initialize_pdyn(int n,
00057                      hbpfp the_hbpfp,   // default = new_hydrobase
00058                      sbpfp the_sbpfp,   // default = new_starbase
00059                      bool use_stories)  // default = true
00060 {
00061     // Create a block of n contiguous pdyn objects, with associated
00062     // pointers and management arrays.
00063 
00064     n_pdyn_mem = 0;
00065     pdyn_mem = new pdyn[n](the_hbpfp, the_sbpfp, use_stories);
00066     pdyn_free = new bool[n];
00067     pdyn_ptr = new int[n];
00068     pdyn_alloc = new int[n];
00069 
00070     if (pdyn_mem && pdyn_free && pdyn_ptr && pdyn_alloc) {
00071         n_pdyn_mem = n;
00072         dealloc_all();
00073             
00074         cerr << "initialize_pdyn: created " << n
00075              << " pdyn elements; pdyn_mem = " << pdyn_mem << endl;
00076 
00077         return true;
00078 
00079     } else
00080         return false;
00081 }
00082 
00083 // alloc_pdyn: replacement for new pdyn once static array has 
00084 //            been built (arguments from pdyn constructor)
00085 
00086 pdyn *alloc_pdyn(hbpfp the_hbpfp,       // default = new_hydrobase
00087                  sbpfp the_sbpfp,       // default = new_starbase
00088                  bool use_stories,      // default = true
00089                  bool reinit)           // default = true
00090 {
00091     if (pdyn_mem) {
00092 
00093         // Must create/delete *bases and stories as necessary.  Also, must
00094         // do all (node/dyn/...) initialization ourselves if reinit not set.
00095 
00096         // Get the next free node.  Should extend the array on overflow...
00097 
00098         if (n_alloc >= n_pdyn_mem) err_exit("pdyn array overflow");
00099 
00100         // Next available node is i = pdyn_alloc[n_alloc].  Allocate
00101         // it and adjust all pointers accordingly.
00102 
00103         int i = pdyn_alloc[n_alloc];
00104         pdyn_free[i] = false;
00105         pdyn_ptr[i] = n_alloc++;
00106 
00107         pdyn *p = pdyn_mem + i;
00108 
00109         if (reinit) {
00110 
00111             // Initialize p using the same functions as the *dyn constructors.
00112 
00113             p->pdyn_init();
00114             p->dyn_init();
00115             p->node_init();
00116 
00117             // Optional (must delete if not wanted):
00118 
00119             if (!use_stories)
00120                 p->rmstory();
00121             else
00122                 p->node_set_stories(use_stories);
00123 
00124             if (!the_hbpfp)
00125                 p->rmhydrobase();
00126             else
00127                 p->node_set_hbase(the_hbpfp);
00128 
00129             if (!the_sbpfp)
00130                 p->rmstarbase();
00131             else
00132                 p->node_set_sbase(the_sbpfp);
00133         }
00134 
00135         return p;
00136 
00137     } else
00138 
00139         // The standard constructor:
00140 
00141         return new pdyn(the_hbpfp, the_sbpfp, use_stories);
00142 }
00143 
00144 // dealloc_pdyn: replacement for delete pdyn once static array has
00145 //               been built
00146 
00147 void dealloc_pdyn(pdyn *p)
00148 {
00149     if (p) {
00150 
00151         if (pdyn_mem) {
00152 
00153             // Return p to the free list.  Clean up as though we were
00154             // deleting the node, although this may not actually be necessary.
00155 
00156             int i = p - pdyn_mem;               // easy computation of index!
00157             pdyn_free[i] = true;
00158             int j = pdyn_ptr[i];
00159 
00160             // Location j in the management array is now free.
00161             // Reorder the array to place the free slot at the end:
00162             // swap elements j and n_alloc-1, then reduce n_alloc.
00163 
00164             int itmp = pdyn_alloc[n_alloc-1];
00165 
00166             pdyn_alloc[--n_alloc] = i;
00167             pdyn_ptr[i] = n_alloc;
00168 
00169             pdyn_alloc[j] = itmp;
00170             pdyn_ptr[itmp] = j;
00171 
00172             // cerr << "deleting " << p << " = " << p->format_label()
00173             //      << ",  i = " << i << endl;
00174 
00175             // pdyn destructor (no action):
00176 
00177             // dyn destructor:
00178 
00179             p->rmkepler();
00180 
00181             // node destructor:
00182 
00183             p->set_invalid();
00184             p->clear_name();
00185             if (p->is_root()) p->set_root(NULL);
00186             p->rmstory();
00187             p->rmhydrobase();
00188             p->rmstarbase();
00189 
00190         } else
00191 
00192             // The standard destructor:
00193 
00194             delete p;
00195     }
00196 }
00197 
00198 // new_pdyn: used by the pdyn version of get_node()
00199 
00200 node * new_pdyn(hbpfp the_hbpfp,
00201                 sbpfp the_sbpfp,
00202                 bool use_stories)       // default = true
00203 {
00204     return (node *) new pdyn(the_hbpfp, the_sbpfp, use_stories);
00205 }
00206 
00207 // new_pdyn2: replacement for new_pdyn using our own memory management
00208 
00209 node * new_pdyn2(hbpfp the_hbpfp,
00210                  sbpfp the_sbpfp,
00211                  bool use_stories)      // default = true
00212 {
00213     return alloc_pdyn(the_hbpfp, the_sbpfp, use_stories);
00214 }

Generated at Sun Feb 24 09:57:11 2002 for STARLAB by doxygen1.2.6 written by Dimitri van Heesch, © 1997-2001