Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

Malloc_Allocator.h

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 //==========================================================================
00004 /**
00005  *  @file   Malloc_Allocator.h
00006  *
00007  *  $Id: Malloc_Allocator.h,v 1.1.1.2 2003/02/21 18:36:32 chad Exp $
00008  * 
00009  *  @author Priyanka Gontla <pgontla@ece.uci.edu>
00010  *  @author Based on code that formerly existed in another ACE file.
00011  */
00012 //==========================================================================
00013 
00014 #ifndef ACE_MALLOC_ALLOCATOR_H
00015 #define ACE_MALLOC_ALLOCATOR_H
00016 
00017 #include "ace/pre.h"
00018 
00019 #include "ace/OS.h"
00020 
00021 #if !defined (ACE_LACKS_PRAGMA_ONCE)
00022 # pragma once
00023 #endif /* ACE_LACKS_PRAGMA_ONCE */
00024 
00025 #include "ace/Malloc_Base.h"
00026 #include "ace/Log_Msg.h"
00027 
00028 #if defined (ACE_HAS_MALLOC_STATS)
00029 #include "ace/Synch_T.h"
00030 #if defined (ACE_HAS_THREADS)
00031 #define ACE_PROCESS_MUTEX ACE_Process_Mutex
00032 #else
00033 #include "ace/SV_Semaphore_Simple.h"
00034 #define ACE_PROCESS_MUTEX ACE_SV_Semaphore_Simple
00035 #endif /* ACE_HAS_THREADS */
00036 
00037 #endif /* ACE_HAS_MALLOC_STATS */
00038 
00039 /**
00040  * @class ACE_New_Allocator
00041  *
00042  * @brief Defines a class that provided a simple implementation of
00043  * memory allocation.
00044  *
00045  * This class uses the new/delete operators to allocate and free up
00046  * memory.  Please note that the only methods that are supported are
00047  * <malloc>, <calloc>, and <free>. All other methods are no-ops that
00048  * return -1 and set <errno> to <ENOTSUP>.  If you require this
00049  * functionality, please use: ACE_Allocator_Adapter <ACE_Malloc
00050  * <ACE_LOCAL_MEMORY_POOL, MUTEX> >, which will allow you to use the
00051  * added functionality of bind/find/etc. while using the new/delete
00052  * operators.  
00053  */
00054 class ACE_Export ACE_New_Allocator : public ACE_Allocator
00055 {
00056 public:
00057   /// These methods are defined.
00058   virtual void *malloc (size_t nbytes);
00059   virtual void *calloc (size_t nbytes, char initial_value = '\0');
00060   virtual void *calloc (size_t n_elem, size_t elem_size, char initial_value = '\0');
00061   virtual void free (void *ptr);
00062 
00063   /// These methods are no-ops.
00064   virtual int remove (void);
00065   virtual int bind (const char *name, void *pointer, int duplicates = 0);
00066   virtual int trybind (const char *name, void *&pointer);
00067   virtual int find (const char *name, void *&pointer);
00068   virtual int find (const char *name);
00069   virtual int unbind (const char *name);
00070   virtual int unbind (const char *name, void *&pointer);
00071   virtual int sync (ssize_t len = -1, int flags = MS_SYNC);
00072   virtual int sync (void *addr, size_t len, int flags = MS_SYNC);
00073   virtual int protect (ssize_t len = -1, int prot = PROT_RDWR);
00074   virtual int protect (void *addr, size_t len, int prot = PROT_RDWR);
00075 #if defined (ACE_HAS_MALLOC_STATS)
00076   virtual void print_stats (void) const;
00077 #endif /* ACE_HAS_MALLOC_STATS */
00078   virtual void dump (void) const;
00079 
00080 private:
00081   // DO NOT ADD ANY STATE (DATA MEMBERS) TO THIS CLASS!!!!  See the
00082   // <ACE_Allocator::instance> implementation for explanation.
00083 };
00084 
00085 /**
00086  * @class ACE_Static_Allocator_Base
00087  *
00088  * @brief Defines a class that provided a highly optimized memory
00089  * management scheme for allocating memory statically.
00090  *
00091  * This class manages a fixed-size <POOL_SIZE> of memory.  Every
00092  * time <malloc>/<calloc> is called, it simply moves an internal
00093  * index forward and returns a pointer to the requested chunk.
00094  * All memory is allocated statically (typically via the
00095  * <ACE_Static_Allocator> template) and <free> is a no-op.  This
00096  * behavior is useful for use-cases where all the memory
00097  * allocation needs are known in advance and no deletions ever
00098  * occur.
00099  */
00100 class ACE_Export ACE_Static_Allocator_Base : public ACE_Allocator
00101 {
00102 public:
00103   ACE_Static_Allocator_Base (char *buffer, size_t size);
00104   virtual void *malloc (size_t nbytes);
00105   virtual void *calloc (size_t nbytes, char initial_value = '\0');
00106   virtual void *calloc (size_t n_elem, size_t elem_size, char initial_value = '\0');
00107   virtual void free (void *ptr);
00108   virtual int remove (void);
00109   virtual int bind (const char *name, void *pointer, int duplicates = 0);
00110   virtual int trybind (const char *name, void *&pointer);
00111   virtual int find (const char *name, void *&pointer);
00112   virtual int find (const char *name);
00113   virtual int unbind (const char *name);
00114   virtual int unbind (const char *name, void *&pointer);
00115   virtual int sync (ssize_t len = -1, int flags = MS_SYNC);
00116   virtual int sync (void *addr, size_t len, int flags = MS_SYNC);
00117   virtual int protect (ssize_t len = -1, int prot = PROT_RDWR);
00118   virtual int protect (void *addr, size_t len, int prot = PROT_RDWR);
00119 #if defined (ACE_HAS_MALLOC_STATS)
00120   virtual void print_stats (void) const;
00121 #endif /* ACE_HAS_MALLOC_STATS */
00122   virtual void dump (void) const;
00123 
00124 protected:
00125   /// Don't allow direct instantiations of this class.
00126   ACE_Static_Allocator_Base (void);
00127 
00128   /// Pointer to the buffer.
00129   char *buffer_;
00130 
00131   /// Size of the buffer.
00132   size_t size_;
00133 
00134   /// Pointer to the current offset in the <buffer_>.
00135   size_t offset_;
00136 };
00137 
00138 #if defined (__ACE_INLINE__)
00139 #include "ace/Malloc_Allocator.i"
00140 #endif /* __ACE_INLINE__ */
00141 
00142 #include "ace/post.h"
00143 
00144 #endif /* MALLOC_ALLOCATOR_H */

Generated on Mon Jun 16 11:20:04 2003 for ACE by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002