Malloc_Allocator.h

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 //==========================================================================
00004 /**
00005  *  @file   Malloc_Allocator.h
00006  *
00007  *  Malloc_Allocator.h,v 4.13 2006/05/05 07:39:41 jwillemsen Exp
00008  *
00009  *  @author Based on code that formerly existed in another ACE file.
00010  */
00011 //==========================================================================
00012 
00013 #ifndef ACE_MALLOC_ALLOCATOR_H
00014 #define ACE_MALLOC_ALLOCATOR_H
00015 
00016 #include /**/ "ace/pre.h"
00017 
00018 #include "ace/ACE_export.h"
00019 
00020 #if !defined (ACE_LACKS_PRAGMA_ONCE)
00021 # pragma once
00022 #endif /* ACE_LACKS_PRAGMA_ONCE */
00023 
00024 #include "ace/Malloc_Base.h"
00025 
00026 #if defined (ACE_HAS_MALLOC_STATS)
00027 #if defined (ACE_HAS_THREADS)
00028 #include "ace/Process_Mutex.h"
00029 #define ACE_PROCESS_MUTEX ACE_Process_Mutex
00030 #else
00031 #include "ace/SV_Semaphore_Simple.h"
00032 #define ACE_PROCESS_MUTEX ACE_SV_Semaphore_Simple
00033 #endif /* ACE_HAS_THREADS */
00034 
00035 #endif /* ACE_HAS_MALLOC_STATS */
00036 
00037 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
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 ACE_END_VERSIONED_NAMESPACE_DECL
00139 
00140 #if defined (__ACE_INLINE__)
00141 #include "ace/Malloc_Allocator.inl"
00142 #endif /* __ACE_INLINE__ */
00143 
00144 #include /**/ "ace/post.h"
00145 
00146 #endif /* MALLOC_ALLOCATOR_H */

Generated on Thu Nov 9 09:41:54 2006 for ACE by doxygen 1.3.6