"Fossies" - the Fresh Open Source Software Archive

Member "muscle/html/muscle-by-example/examples/refcount/example_2_with_object_pool.cpp" (8 Jun 2019, 2598 Bytes) of package /linux/privat/muscle7.30.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 #include "system/SetupSystem.h"  // for CompleteSetupSystem
    2 #include "util/RefCount.h"
    3 #include "util/ObjectPool.h"
    4 
    5 using namespace muscle;
    6 
    7 static void PrintExampleDescription()
    8 {
    9    printf("\n");
   10    printf("This program demonstrates using a RefCountable class in conjunction with an ObjectPool to minimize object (de)allocations at runtime\n");
   11    printf("\n");
   12 }
   13 
   14 static int _myClassCounter = 0;  // let's keep track of how many MyClass objects there are
   15 
   16 /** An example of a class we want to allocate objects of from the heap,
   17   * but still avoid any risk of memory leaks.
   18   */
   19 class MyClass : public RefCountable
   20 {
   21 public:
   22    MyClass()
   23    {
   24       printf("MyClass default-constructor called, this=%p, _myClassCounter=%i\n", this, ++_myClassCounter);
   25    }
   26 
   27    MyClass(const MyClass & rhs)
   28    {
   29       printf("MyClass copy-constructor called, this=%p, rhs=%p, _myClassCounter=%i\n", this, &rhs, ++_myClassCounter);
   30    }
   31 
   32    ~MyClass()
   33    {
   34       printf("MyClass destructor called, this=%p, _myClassCounter=%i\n", this, --_myClassCounter);
   35       if (_myClassCounter == 0) printf("\nAll MyClass objects have been destroyed, yay!\n");
   36    }
   37 };
   38 DECLARE_REFTYPES(MyClass);  // defines MyClassRef and ConstMyClassRef
   39 
   40 // To avoid constant calls to new and delete, we'll set up this little "recycling program"
   41 // Note that ObjectPool uses a slab-allocator, so it will allocate a bunch of objects at
   42 // once when necessary (enough objects to fill up one 4KB page of RAM) and then dole them
   43 // out as the program needs them.
   44 static ObjectPool<MyClass> _myClassPool;
   45 
   46 int main(int argc, char ** argv)
   47 {
   48    CompleteSetupSystem css;
   49 
   50    PrintExampleDescription();
   51 
   52    printf("At top of main\n");
   53    printf("\n");
   54 
   55    // Give ownership of new MyClass objects to a MyClassRef immediately
   56    // As soon as you've done that, you don't have to worry about leaks anymore
   57    MyClassRef mc1(_myClassPool.ObtainObject());
   58    MyClassRef mc2(_myClassPool.ObtainObject());
   59 
   60    printf("\n");
   61 
   62    // Simluate a program doing stuff that needs a lot of MyClass objects at the same time
   63    for (int i=0; i<100; i+=5)
   64    {
   65       // Grab a number of MyClass objects from the ObjectPool for our use here
   66       int numObjectsNeededThisTime = i;
   67       Queue<MyClassRef> q;
   68       for (int j=0; j<i; j++) q.AddTail(MyClassRef(_myClassPool.ObtainObject()));
   69 
   70       printf("   Iteration %i of the loop is (pretending to use) %i MyClass objects\n", i, i);
   71       q.Clear();  // not strictly necessary since (q) is about to go out of scope anyway
   72    }
   73 
   74    printf("\n");
   75    printf("At bottom of main()\n");
   76    printf("\n");
   77    return 0;
   78 }