CMS 3D CMS Logo

IgProfBuffer.cc

Go to the documentation of this file.
00001 #include "IgTools/IgProf/src/IgProfBuffer.h"
00002 #include <sys/mman.h>
00003 #include <memory.h>
00004 #include <errno.h>
00005 
00006 #if ! defined MAP_ANONYMOUS && defined MAP_ANON
00007 # define MAP_ANONYMOUS MAP_ANON
00008 #endif
00009 
00010 static const unsigned int MEM_POOL_SIZE = 8*1024*1024;
00011 
00013 IgProfBuffer::IgProfBuffer (void)
00014   : poolfirst_(0),
00015     poolcur_(0),
00016     freestart_(0),
00017     freeend_(0)
00018 {
00019   // Get the first pool and initialise the chain pointer to null.
00020   void *pool = allocateRaw(MEM_POOL_SIZE);
00021   poolfirst_ = poolcur_ = (void **) pool;
00022   *poolfirst_ = 0;
00023 
00024   // Mark the rest free.
00025   freestart_ = (char *) pool + sizeof(void **);
00026   freeend_ = (char *) pool + MEM_POOL_SIZE;
00027 }
00028 
00029 IgProfBuffer::~IgProfBuffer(void)
00030 {
00031   void **p = poolfirst_;
00032   while (p)
00033   {
00034     void **next = (void **) *p;
00035     munmap(p, MEM_POOL_SIZE);
00036     p = next;
00037   }
00038 }
00039 
00040 void
00041 IgProfBuffer::unallocateRaw(void *p, size_t size)
00042 {
00043   munmap(p, size);
00044 }
00045 
00046 void *
00047 IgProfBuffer::allocateRaw(size_t size)
00048 {
00049   void *data = mmap (0, size, PROT_READ | PROT_WRITE,
00050                      MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
00051   if (data != MAP_FAILED)
00052     return data;
00053   else
00054   {
00055     IgProf::debug ("Failed to allocate memory for profile buffer: %s (%d)\n",
00056                    strerror (errno), errno);
00057     abort ();
00058   }
00059 }
00060 
00061 void
00062 IgProfBuffer::allocatePool(void)
00063 {
00064   // Get a pool.  If we don't have any pools yet, make this the first
00065   // one.  If we have an existing pool, chain the new one into the
00066   // last one.  Then make this the current and last pool and mark the
00067   // memory free.  Note the pool memory is allocated all-zeroes.
00068   void **pool = (void **) allocateRaw(MEM_POOL_SIZE);
00069 
00070   if (! poolfirst_)
00071     poolfirst_ = pool;
00072 
00073   if (poolcur_)
00074     *poolcur_ = pool;
00075 
00076   poolcur_ = pool;
00077 
00078   freestart_ = (char *) pool + sizeof(void **);
00079   freeend_ = (char *) pool + MEM_POOL_SIZE;
00080 }

Generated on Tue Jun 9 17:38:08 2009 for CMSSW by  doxygen 1.5.4