Lab 6: C++ Fixed-size Memory Manager

  1. Overload new and delete operators in C++.
  2. Download memmgr-0.0.1.tar.gz.
  3. Ungzip and untar memmgr-0.0.1.tar.gz and compile the code.
      $ gzip -cd memmgr-0.0.1.tar.gz | tar xvf -
      $ cd memmgr-0.0.1
      $ configure
      $ make
      
  4. Edit mem_mgr.h and fill in the code at places marked as XXX.
    #ifndef MEM_MGR_H
    #define MEM_MGR_H
    
    #include <stdio.h>
    #include <assert.h>
    
    class MemMgr {
    public:
        int iSize;
        int iCount;
        void *pvList;
        void *pvFree;
    
    public:
        MemMgr(int iSize, int iCount) :
            iSize(iSize),
            iCount(iCount),
            pvList(NULL),
            pvFree(NULL) {
            assert(iSize >= sizeof(void *));
        }
        ~MemMgr(void) {
            while (pvList) {
                char *p = (char *)pvList;
                pvList = *(void **)pvList;
                delete []p;
            }
        }
        void *malloc(void) {
            // XXX: write your code here
            return (NULL);
        }
        void free(void *p) {
            // XXX: write your code here
        }
    };
    
    #endif // MEM_MGR_H
      
  5. Test your code by running main and main_mm with count # 1000, 10000, 100000, 1000000, 2000000, 3000000, 4000000, 5000000, 6000000, 7000000, 8000000, 9000000, 10000000. Keep track the user time returned by the command time.
      $ time main 1000000
      1.040u 0.012s 0:01.04 100.9%    0+0k 0+0io 0pf+0w
      $  time main_mm 1000000
      0.980u 0.004s 0:01.00 98.0%     0+0k 0+0io 0pf+0w
      
  6. Keep the user time in a text file, say, time.txt. For example:
      1000     ...    ...
      ...
      1000000  1.040  0.980
      ...
      
  7. Plot the data using gnuplot.
      $ gnuplot
      gnuplot> plot 'time.txt' using 1:2 with lines title "Without MM",\
                    'time.txt' using 1:3 with lines title "With MM"
      
  8. Explain the graph.