next up previous contents
Next: 2.4.9 Allocators used in Up: 2.4 System resources Previous: 2.4.7 Resource revocation

   
2.4.8 Allocation statistics

Statistics are maintained by bookkeeping allocators (BKAllocators). Every system allocator will inherit from this class. Concrete children must also implement the resource allocation interface.

Note how locking facilities are missing. The resource containing the allocator should use its own locking facilities for concurrency control.

<Off bookkeeping allocator. >= (U->)
// A Bookkeeping allocator.
//
class off_BKAllocator : public off_Allocator {
private:
  natural_t b_ntotal;          //  # of nodes in the allocator.
  natural_t b_nfree;           //  # of free nodes
  natural_t b_nalloc;          //  # of allocated nodes
  natural_t b_maxalloc;        //  Maximum # of ever allocated nodes
  natural_t b_nallocrq;        //  # of alloc requests
  natural_t b_nfreerq;         //  # of free requests
protected:
  // Reset the number of free nodes. 
  void resize(natural_t l) {
    b_nfree += l;
    b_ntotal+= l;
    assert( b_ntotal == b_nalloc + b_nfree);
  }
  // Allocator bookkeeping methods.
  void allocate(void);
  void deallocate(void);

public:
  // Allows declarations of uninitialized bookkeeping allocators.
  off_BKAllocator(void){;}

  // Initialize statistics
  off_BKAllocator(off_Exhausted *r, natural_t size=0): 
    off_Allocator(r)
  { 
     b_nalloc=b_maxalloc=b_nallocrq=b_nfreerq=0;
     b_ntotal=b_nfree=size;
     assert( b_ntotal == b_nalloc + b_nfree);
  }

  // Get some statistics 
  inline natural_t get_nfree(void) const {return b_nfree;}
  inline natural_t get_nalloc(void) const {return b_nalloc;}
  inline natural_t get_maxalloc(void) const {return b_maxalloc;}
  inline natural_t get_num_allocrq(void) const {return b_nallocrq;}
  inline natural_t get_num_freerq(void) const {return b_nfreerq;}
  inline natural_t get_length(void) const {return b_ntotal;}
  

};

Defines off_BKAllocator (links are to index).

<Off bookkeeping allocator dependencies. >= (U->)
#include <assert.h>             // for assert
#include <flux/types.h>         // for boolean_t natural_t et al.
#include <klib/Allocator.h>     // for off_Allocator

<off_BKAllocator::allocate and deallocate implementation. >= (U->)
// Allocator bookkeeping methods.
void off_BKAllocator::allocate(void)
{
  b_nfree--; b_nalloc++; b_nallocrq++; 
  if (b_maxalloc < b_nalloc)
    b_maxalloc = b_nalloc;
  assert( b_ntotal == b_nalloc + b_nfree);
}
void off_BKAllocator::deallocate(void)
{ 
  b_nfree++; b_nalloc--; b_nfreerq++;
  assert( b_ntotal == b_nalloc + b_nfree);
}

\subsubsection{Allocation statistics \cpp{} source files}

The bookkeeping allocator is defined in klib/BKAllocator.h and klib/BKAllocator.C.

<BKAllocator.h*>=
<Read the literate code instead warning. >
#ifndef __OFF_BK_ALLOCATOR_H
#define __OFF_BK_ALLOCATOR_H 1

<Off bookkeeping allocator dependencies. >
#ifdef __KERNEL__
<Off bookkeeping allocator. >
#endif // __KERNEL__

#endif // __OFF_BK_ALLOCATOR_H

<BKAllocator.C*>=
<Read the literate code instead warning. >

#include <klib/BKAllocator.h>   // Exported interface. 

<off_BKAllocator::allocate and deallocate implementation. >



Francisco J. Ballesteros
1998-05-25