Initial Patch of Auction House bot rev. 135
[auctionmangos.git] / dep / ACE_wrappers / ace / Malloc_T.h
blob2d610eb5915c8323333a8f2d1a3dda2eb294a44f
1 // -*- C++ -*-
3 //==========================================================================
4 /**
5 * @file Malloc_T.h
7 * $Id: Malloc_T.h 80826 2008-03-04 14:51:23Z wotte $
9 * @author Douglas C. Schmidt <schmidt@cs.wustl.edu> and
10 * Irfan Pyarali <irfan@cs.wustl.edu>
12 //==========================================================================
14 #ifndef ACE_MALLOC_T_H
15 #define ACE_MALLOC_T_H
16 #include /**/ "ace/pre.h"
18 #include "ace/Malloc.h" /* Need ACE_Control_Block */
19 #include "ace/Malloc_Base.h" /* Need ACE_Allocator */
21 #if !defined (ACE_LACKS_PRAGMA_ONCE)
22 # pragma once
23 #endif /* ACE_LACKS_PRAGMA_ONCE */
25 #include "ace/Malloc_Allocator.h"
26 #include "ace/Free_List.h"
27 #include "ace/Guard_T.h"
29 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
31 /**
32 * @class ACE_Cached_Mem_Pool_Node
34 * @brief ACE_Cached_Mem_Pool_Node keeps unused memory within a free
35 * list.
37 * The length of a piece of unused memory must be greater than
38 * sizeof (void*). This makes sense because we'll waste even
39 * more memory if we keep them in a separate data structure.
40 * This class should really be placed within the ACE_Cached_Allocator
41 * class but this can't be done due to C++ compiler portability problems.
43 template <class T>
44 class ACE_Cached_Mem_Pool_Node
46 public:
47 /// Return the address of free memory.
48 T *addr (void);
50 /// Get the next ACE_Cached_Mem_Pool_Node in a list.
51 ACE_Cached_Mem_Pool_Node<T> *get_next (void);
53 /// Set the next ACE_Cached_Mem_Pool_Node.
54 void set_next (ACE_Cached_Mem_Pool_Node<T> *ptr);
56 private:
57 /**
58 * Since memory is not used when placed in a free list,
59 * we can use it to maintain the structure of free list.
60 * I was using union to hide the fact of overlapping memory
61 * usage. However, that cause problem on MSVC. So, I now turn
62 * back to hack this with casting.
64 ACE_Cached_Mem_Pool_Node<T> *next_;
67 /**
68 * @class ACE_Cached_Allocator
70 * @brief A fixed-size allocator that caches items for quicker access.
72 * This class enables caching of dynamically allocated,
73 * fixed-sized classes. Notice that the <code>sizeof (TYPE)</code>
74 * must be greater than or equal to <code> sizeof (void*) </code> for
75 * this to work properly.
77 * This class can be configured flexibly with different types of
78 * ACE_LOCK strategies that support the @a ACE_Thread_Mutex,
79 * @a ACE_Thread_Semaphore, @a ACE_Process_Mutex, and @a
80 * ACE_Process_Semaphore constructor API.
82 * @sa ACE_Dynamic_Cached_Allocator
84 template <class T, class ACE_LOCK>
85 class ACE_Cached_Allocator : public ACE_New_Allocator
87 public:
88 /// Create a cached memory pool with @a n_chunks chunks
89 /// each with sizeof (TYPE) size.
90 ACE_Cached_Allocator (size_t n_chunks);
92 /// Clear things up.
93 ~ACE_Cached_Allocator (void);
95 /**
96 * Get a chunk of memory from free list cache. Note that @a nbytes is
97 * only checked to make sure that it's less or equal to sizeof T, and is
98 * otherwise ignored since @c malloc() always returns a pointer to an
99 * item of sizeof (T).
101 void *malloc (size_t nbytes = sizeof (T));
104 * Get a chunk of memory from free list cache, giving them
105 * @a initial_value. Note that @a nbytes is only checked to make sure
106 * that it's less or equal to sizeof T, and is otherwise ignored since
107 * calloc() always returns a pointer to an item of sizeof (T).
109 virtual void *calloc (size_t nbytes,
110 char initial_value = '\0');
112 /// This method is a no-op and just returns 0 since the free list
113 /// only works with fixed sized entities.
114 virtual void *calloc (size_t n_elem,
115 size_t elem_size,
116 char initial_value = '\0');
118 /// Return a chunk of memory back to free list cache.
119 void free (void *);
121 /// Return the number of chunks available in the cache.
122 size_t pool_depth (void);
124 private:
125 /// Remember how we allocate the memory in the first place so
126 /// we can clear things up later.
127 char *pool_;
129 /// Maintain a cached memory free list.
130 ACE_Locked_Free_List<ACE_Cached_Mem_Pool_Node<T>, ACE_LOCK> free_list_;
134 * @class ACE_Dynamic_Cached_Allocator
136 * @brief A size-based allocator that caches blocks for quicker access.
138 * This class enables caching of dynamically allocated,
139 * fixed-size chunks. Notice that the <code>chunk_size</code>
140 * must be greater than or equal to <code> sizeof (void*) </code> for
141 * this to work properly.
143 * This class can be configured flexibly with different types of
144 * ACE_LOCK strategies that support the @a ACE_Thread_Mutex and @a
145 * ACE_Process_Mutex constructor API.
147 * @sa ACE_Cached_Allocator
149 template <class ACE_LOCK>
150 class ACE_Dynamic_Cached_Allocator : public ACE_New_Allocator
152 public:
153 /// Create a cached memory pool with @a n_chunks chunks
154 /// each with @a chunk_size size.
155 ACE_Dynamic_Cached_Allocator (size_t n_chunks, size_t chunk_size);
157 /// Clear things up.
158 ~ACE_Dynamic_Cached_Allocator (void);
161 * Get a chunk of memory from free list cache. Note that @a nbytes is
162 * only checked to make sure that it's less or equal to @a chunk_size,
163 * and is otherwise ignored since malloc() always returns a pointer to an
164 * item of @a chunk_size size.
166 void *malloc (size_t nbytes = 0);
169 * Get a chunk of memory from free list cache, giving them
170 * @a initial_value. Note that @a nbytes is only checked to make sure
171 * that it's less or equal to @a chunk_size, and is otherwise ignored
172 * since calloc() always returns a pointer to an item of @a chunk_size.
174 virtual void *calloc (size_t nbytes,
175 char initial_value = '\0');
177 /// This method is a no-op and just returns 0 since the free list
178 /// only works with fixed sized entities.
179 virtual void *calloc (size_t n_elem,
180 size_t elem_size,
181 char initial_value = '\0');
183 /// Return a chunk of memory back to free list cache.
184 void free (void *);
186 /// Return the number of chunks available in the cache.
187 size_t pool_depth (void);
189 private:
190 /// Remember how we allocate the memory in the first place so
191 /// we can clear things up later.
192 char *pool_;
194 /// Maintain a cached memory free list. We use @c char as template
195 /// parameter, although sizeof(char) is usually less than
196 /// sizeof(void*). Really important is that @a chunk_size
197 /// must be greater or equal to sizeof(void*).
198 ACE_Locked_Free_List<ACE_Cached_Mem_Pool_Node<char>, ACE_LOCK> free_list_;
200 /// Remember the size of our chunks.
201 size_t chunk_size_;
205 * @class ACE_Allocator_Adapter
207 * @brief This class is an adapter that allows the ACE_Allocator to
208 * use the ACE_Malloc class below.
210 template <class MALLOC>
211 class ACE_Allocator_Adapter : public ACE_Allocator
213 public:
214 // Trait.
215 typedef MALLOC ALLOCATOR;
217 #if defined (ACE_HAS_TEMPLATE_TYPEDEFS)
218 // The following code will break C++ compilers that don't support
219 // template typedefs correctly.
220 typedef const typename MALLOC::MEMORY_POOL_OPTIONS *MEMORY_POOL_OPTIONS;
221 #else
222 typedef const void *MEMORY_POOL_OPTIONS;
223 #endif /* ACE_HAS_TEMPLATE_TYPEDEFS */
225 // = Initialization.
227 * Note that @a pool_name should be located in
228 * a directory with the appropriate visibility and protection so
229 * that all processes that need to access it can do so. */
230 ACE_Allocator_Adapter (const char *pool_name = 0);
233 * Note that @a pool_name should be located in
234 * a directory with the appropriate visibility and protection so
235 * that all processes that need to access it can do so.
236 * This constructor must be inline to avoid bugs with some C++
237 * compilers. */
238 ACE_Allocator_Adapter (const char *pool_name,
239 const char *lock_name,
240 MEMORY_POOL_OPTIONS options = 0)
241 : allocator_ (ACE_TEXT_CHAR_TO_TCHAR (pool_name),
242 ACE_TEXT_CHAR_TO_TCHAR (lock_name),
243 options)
245 ACE_TRACE ("ACE_Allocator_Adapter<MALLOC>::ACE_Allocator_Adapter");
248 #if defined (ACE_HAS_WCHAR)
250 * Note that @a pool_name should be located in
251 * a directory with the appropriate visibility and protection so
252 * that all processes that need to access it can do so. */
253 ACE_Allocator_Adapter (const wchar_t *pool_name);
256 * Note that @a pool_name should be located in
257 * a directory with the appropriate visibility and protection so
258 * that all processes that need to access it can do so.
259 * This constructor must be inline to avoid bugs with some C++
260 * compilers. */
261 ACE_Allocator_Adapter (const wchar_t *pool_name,
262 const wchar_t *lock_name,
263 MEMORY_POOL_OPTIONS options = 0)
264 : allocator_ (ACE_TEXT_WCHAR_TO_TCHAR (pool_name),
265 ACE_TEXT_WCHAR_TO_TCHAR (lock_name),
266 options)
268 ACE_TRACE ("ACE_Allocator_Adapter<MALLOC>::ACE_Allocator_Adapter");
270 #endif /* ACE_HAS_WCHAR */
272 /// Destructor.
273 virtual ~ACE_Allocator_Adapter (void);
275 // = Memory Management
277 /// Allocate @a nbytes, but don't give them any initial value.
278 virtual void *malloc (size_t nbytes);
280 /// Allocate @a nbytes, giving them all an @a initial_value.
281 virtual void *calloc (size_t nbytes, char initial_value = '\0');
283 /// Allocate @a n_elem each of size @a elem_size, giving them
284 /// @a initial_value.
285 virtual void *calloc (size_t n_elem,
286 size_t elem_size,
287 char initial_value = '\0');
289 /// Free @a ptr (must have been allocated by ACE_Allocator::malloc()).
290 virtual void free (void *ptr);
292 /// Remove any resources associated with this memory manager.
293 virtual int remove (void);
295 // = Map manager like functions
298 * Associate @a name with @a pointer. If @a duplicates == 0 then do
299 * not allow duplicate @a name/pointer associations, else if
300 * @a duplicates != 0 then allow duplicate @a name/pointer
301 * assocations. Returns 0 if successfully binds (1) a previously
302 * unbound @a name or (2) @a duplicates != 0, returns 1 if trying to
303 * bind a previously bound @a name and @a duplicates == 0, else
304 * returns -1 if a resource failure occurs.
306 virtual int bind (const char *name, void *pointer, int duplicates = 0);
309 * Associate @a name with @a pointer. Does not allow duplicate
310 * name/pointer associations. Returns 0 if successfully binds
311 * (1) a previously unbound @a name, 1 if trying to bind a previously
312 * bound @a name, or returns -1 if a resource failure occurs. When
313 * this call returns, @a pointer's value will always reference the
314 * void * that @a name is associated with. Thus, if the caller needs
315 * to use @a pointer (e.g., to free it) a copy must be maintained by
316 * the caller.
318 virtual int trybind (const char *name, void *&pointer);
320 /// Locate @a name and pass out parameter via pointer. If found,
321 /// return 0, returns -1 if @a name isn't found.
322 virtual int find (const char *name, void *&pointer);
324 /// Returns 0 if the name is in the mapping and -1 if not.
325 virtual int find (const char *name);
327 /// Unbind (remove) the name from the map. Don't return the pointer
328 /// to the caller
329 virtual int unbind (const char *name);
331 /// Break any association of name. Returns the value of pointer in
332 /// case the caller needs to deallocate memory.
333 virtual int unbind (const char *name, void *&pointer);
335 // = Protection and "sync" (i.e., flushing data to backing store).
338 * Sync @a len bytes of the memory region to the backing store
339 * starting at @c this->base_addr_. If @a len == -1 then sync the
340 * whole region.
342 virtual int sync (ssize_t len = -1, int flags = MS_SYNC);
344 /// Sync @a len bytes of the memory region to the backing store
345 /// starting at @c addr_.
346 virtual int sync (void *addr, size_t len, int flags = MS_SYNC);
349 * Change the protection of the pages of the mapped region to @a prot
350 * starting at @c this->base_addr_ up to @a len bytes. If @a len == -1
351 * then change protection of all pages in the mapped region.
353 virtual int protect (ssize_t len = -1, int prot = PROT_RDWR);
355 /// Change the protection of the pages of the mapped region to @a prot
356 /// starting at @a addr up to @a len bytes.
357 virtual int protect (void *addr, size_t len, int prot = PROT_RDWR);
359 /// Returns the underlying allocator.
360 ALLOCATOR &alloc (void);
362 #if defined (ACE_HAS_MALLOC_STATS)
363 /// Dump statistics of how malloc is behaving.
364 virtual void print_stats (void) const;
365 #endif /* ACE_HAS_MALLOC_STATS */
367 /// Dump the state of the object.
368 virtual void dump (void) const;
370 private:
371 /// ALLOCATOR instance, which is owned by the adapter.
372 ALLOCATOR allocator_;
376 * @class ACE_Static_Allocator
378 * @brief Defines a class that provided a highly optimized memory
379 * management scheme for allocating memory statically.
381 * This class allocates a fixed-size @c POOL_SIZE of memory and
382 * uses the ACE_Static_Allocator_Base class implementations of
383 * malloc() and calloc() to optimize memory allocation from this
384 * pool.
386 template <size_t POOL_SIZE>
387 class ACE_Static_Allocator : public ACE_Static_Allocator_Base
389 public:
390 ACE_Static_Allocator (void)
391 : ACE_Static_Allocator_Base (pool_, POOL_SIZE)
393 // This function <{must}> be inlined!!!
396 private:
397 /// Pool contents.
398 char pool_[POOL_SIZE];
401 // Forward declaration.
402 template <ACE_MEM_POOL_1, class ACE_LOCK, class ACE_CB>
403 class ACE_Malloc_LIFO_Iterator_T;
405 // Ensure backwards compatibility...
406 #define ACE_Malloc_Iterator ACE_Malloc_LIFO_Iterator
408 // Forward declaration.
409 template <ACE_MEM_POOL_1, class ACE_LOCK, class ACE_CB>
410 class ACE_Malloc_FIFO_Iterator_T;
413 * @class ACE_Malloc_T
415 * @brief A class template that uses parameterized types to provide
416 * an extensible mechanism for encapsulating various dynamic
417 * memory management strategies.
419 * This class can be configured flexibly with different
420 * MEMORY_POOL strategies and different types of ACE_LOCK
421 * strategies that support the ACE_Thread_Mutex and ACE_Process_Mutex
422 * constructor API.
424 * Common MEMORY_POOL strategies to use with this class are:
425 * - ACE_Local_Memory_Pool
426 * - ACE_MMAP_Memory_Pool
427 * - ACE_Pagefile_Memory_Pool
428 * - ACE_Shared_Memory_Pool
429 * - ACE_Sbrk_Memory_Pool
431 * The MEMORY_POOL class must provide the following methods:
432 * - constructor (const ACE_TCHAR *pool_name)
433 * - constructor (const ACE_TCHAR *pool_name, const MEMORY_POOL_OPTIONS *options)
434 * - void dump (void) const (needed if ACE is built with ACE_HAS_DUMP defined)
435 * - void *init_acquire (size_t nbytes, size_t &rounded_bytes, int &first_time);
436 * - int release (void)
437 * - void *acquire (size_t nbytes, size_t &rounded_bytes)
438 * - void *base_addr (void)
439 * - seh_selector() (only needed on Windows)
441 * Note that the ACE_Allocator_Adapter class can be used to integrate allocator
442 * classes which do not meet the interface requirements of ACE_Malloc_T.
444 * @Note The bind() and find() methods use linear search, so
445 * it's not a good idea to use them for managing a large number of
446 * entities. If you need to manage a large number of entities, it's
447 * recommended that you bind() an ACE_Hash_Map_Manager that
448 * resides in shared memory, use find() to locate it, and then
449 * store/retrieve the entities in the hash map.
451 template <ACE_MEM_POOL_1, class ACE_LOCK, class ACE_CB>
452 class ACE_Malloc_T
454 public:
455 friend class ACE_Malloc_LIFO_Iterator_T<ACE_MEM_POOL_2, ACE_LOCK, ACE_CB>;
456 friend class ACE_Malloc_FIFO_Iterator_T<ACE_MEM_POOL_2, ACE_LOCK, ACE_CB>;
457 typedef ACE_MEM_POOL MEMORY_POOL;
458 typedef ACE_MEM_POOL_OPTIONS MEMORY_POOL_OPTIONS;
459 typedef typename ACE_CB::ACE_Name_Node NAME_NODE;
460 typedef typename ACE_CB::ACE_Malloc_Header MALLOC_HEADER;
462 // = Initialization and termination methods.
464 * Initialize ACE_Malloc. This constructor passes @a pool_name to
465 * initialize the memory pool, and uses ACE::basename() to
466 * automatically extract out the name used for the underlying lock
467 * name (if necessary).
469 * Note that @a pool_name should be located in
470 * a directory with the appropriate visibility and protection so
471 * that all processes that need to access it can do so.
473 ACE_Malloc_T (const ACE_TCHAR *pool_name = 0);
476 * Initialize ACE_Malloc. This constructor passes @a pool_name to
477 * initialize the memory pool, and uses @a lock_name to automatically
478 * extract out the name used for the underlying lock name (if
479 * necessary). In addition, @a options is passed through to
480 * initialize the underlying memory pool.
482 * Note that @a pool_name should be located in
483 * a directory with the appropriate visibility and protection so
484 * that all processes that need to access it can do so.
486 ACE_Malloc_T (const ACE_TCHAR *pool_name,
487 const ACE_TCHAR *lock_name,
488 const ACE_MEM_POOL_OPTIONS *options = 0);
491 * Initialize an ACE_Malloc with an external ACE_LOCK.
492 * This constructor passes @a pool_name and @a options to initialize
493 * the memory pool. @a lock is used as the pool lock, and must be
494 * properly set up and ready for use before being passed to this method.
496 ACE_Malloc_T (const ACE_TCHAR *pool_name,
497 const ACE_MEM_POOL_OPTIONS *options,
498 ACE_LOCK *lock);
500 #if !defined (ACE_HAS_TEMPLATE_TYPEDEFS)
501 /// This is necessary to work around template bugs with certain C++
502 /// compilers.
503 ACE_Malloc_T (const ACE_TCHAR *pool_name,
504 const ACE_TCHAR *lock_name,
505 const void *options = 0);
506 #endif /* ACE_HAS_TEMPLATE_TYPEDEFS */
508 /// Destructor
509 ~ACE_Malloc_T (void);
511 /// Get Reference counter.
512 int ref_counter (void);
514 /// Release ref counter.
515 int release (int close = 0);
517 /// Releases resources allocated by this object.
518 int remove (void);
520 // = Memory management
522 /// Allocate @a nbytes, but don't give them any initial value.
523 void *malloc (size_t nbytes);
525 /// Allocate @a nbytes, giving them @a initial_value.
526 void *calloc (size_t nbytes, char initial_value = '\0');
528 /// Allocate @a n_elem each of size @a elem_size, giving them
529 /// @a initial_value.
530 void *calloc (size_t n_elem,
531 size_t elem_size,
532 char initial_value = '\0');
534 /// Deallocate memory pointed to by @a ptr, which must have been
535 /// allocated previously by malloc().
536 void free (void *ptr);
538 /// Returns a reference to the underlying memory pool.
539 MEMORY_POOL &memory_pool (void);
541 // = Map manager like functions
544 * Associate @a name with @a pointer. If @a duplicates == 0 then do
545 * not allow duplicate name/pointer associations, else if
546 * @a duplicates != 0 then allow duplicate name/pointer
547 * assocations. Returns 0 if successfully binds (1) a previously
548 * unbound @a name or (2) @a duplicates != 0, returns 1 if trying to
549 * bind a previously bound @a name and @a duplicates == 0, else
550 * returns -1 if a resource failure occurs.
552 int bind (const char *name, void *pointer, int duplicates = 0);
555 * Associate @a name with @a pointer. Does not allow duplicate
556 * name/pointer associations. Returns 0 if successfully binds
557 * (1) a previously unbound @a name, 1 if trying to bind a previously
558 * bound @a name, or returns -1 if a resource failure occurs. When
559 * this call returns @a pointer's value will always reference the
560 * void * that @a name is associated with. Thus, if the caller needs
561 * to use @a pointer (e.g., to free it) a copy must be maintained by
562 * the caller.
564 int trybind (const char *name, void *&pointer);
566 /// Locate @a name and pass out parameter via @a pointer. If found,
567 /// return 0, returns -1 if failure occurs.
568 int find (const char *name, void *&pointer);
570 /// Returns 0 if @a name is in the mapping. -1, otherwise.
571 int find (const char *name);
574 * Unbind (remove) the name from the map. Don't return the pointer
575 * to the caller. If you want to remove all occurrences of @a name
576 * you'll need to call this method multiple times until it fails...
578 int unbind (const char *name);
581 * Unbind (remove) one association of @a name to @a pointer. Returns
582 * the value of pointer in case the caller needs to deallocate
583 * memory. If you want to remove all occurrences of @a name you'll
584 * need to call this method multiple times until it fails...
586 int unbind (const char *name, void *&pointer);
588 // = Protection and "sync" (i.e., flushing data to backing store).
591 * Sync @a len bytes of the memory region to the backing store
592 * starting at @c this->base_addr_. If @a len == -1 then sync the
593 * whole region.
595 int sync (ssize_t len = -1, int flags = MS_SYNC);
597 /// Sync @a len bytes of the memory region to the backing store
598 /// starting at @c addr_.
599 int sync (void *addr, size_t len, int flags = MS_SYNC);
602 * Change the protection of the pages of the mapped region to @a prot
603 * starting at @c this->base_addr_ up to @a len bytes. If @a len == -1
604 * then change protection of all pages in the mapped region.
606 int protect (ssize_t len = -1, int prot = PROT_RDWR);
608 /// Change the protection of the pages of the mapped region to @a prot
609 /// starting at @a addr up to @a len bytes.
610 int protect (void *addr, size_t len, int prot = PROT_RDWR);
613 * Returns a count of the number of available chunks that can hold
614 * @a size byte allocations. Function can be used to determine if you
615 * have reached a water mark. This implies a fixed amount of allocated
616 * memory.
618 * @param size The chunk size of that you would like a count of
619 * @return Function returns the number of chunks of the given size
620 * that would fit in the currently allocated memory.
622 ssize_t avail_chunks (size_t size) const;
624 #if defined (ACE_HAS_MALLOC_STATS)
625 /// Dump statistics of how malloc is behaving.
626 void print_stats (void) const;
627 #endif /* ACE_HAS_MALLOC_STATS */
629 /// Returns a pointer to the lock used to provide mutual exclusion to
630 /// an ACE_Malloc allocator.
631 ACE_LOCK &mutex (void);
633 /// Dump the state of an object.
634 void dump (void) const;
636 /// Declare the dynamic allocation hooks.
637 ACE_ALLOC_HOOK_DECLARE;
639 /// Return cb_ptr value.
640 void *base_addr (void);
643 * Bad flag. This operation should be called immediately after the
644 * construction of the Malloc object to query whether the object was
645 * constructed successfully. If not, the user should invoke @c
646 * remove and release the object (it is not usable.)
647 * @retval 0 if all is fine. non-zero if this malloc object is
648 * unuable.
650 int bad (void);
652 private:
653 /// Initialize the Malloc pool.
654 int open (void);
656 /// Associate @a name with @a pointer. Assumes that locks are held by
657 /// callers.
658 int shared_bind (const char *name,
659 void *pointer);
662 * Try to locate @a name. If found, return the associated
663 * ACE_Name_Node, else returns 0 if can't find the @a name.
664 * Assumes that locks are held by callers. Remember to cast the
665 * return value to ACE_CB::ACE_Name_Node*.
667 void *shared_find (const char *name);
669 /// Allocate memory. Assumes that locks are held by callers.
670 void *shared_malloc (size_t nbytes);
672 /// Deallocate memory. Assumes that locks are held by callers.
673 void shared_free (void *ptr);
675 /// Pointer to the control block that is stored in memory controlled
676 /// by <MEMORY_POOL>.
677 ACE_CB *cb_ptr_;
679 /// Pool of memory used by ACE_Malloc to manage its freestore.
680 MEMORY_POOL memory_pool_;
682 /// Lock that ensures mutual exclusion for the memory pool.
683 ACE_LOCK *lock_;
685 /// True if destructor should delete the lock
686 bool delete_lock_;
688 /// Keep track of failure in constructor.
689 int bad_flag_;
692 /*****************************************************************************/
695 * @class ACE_Malloc_Lock_Adapter_T
697 * @brief Template functor adapter for lock strategies used with ACE_Malloc_T.
699 * This class acts as a factory for lock strategies that have various ctor
700 * signatures. If the lock strategy's ctor takes an ACE_TCHAR* as the first
701 * and only required parameter, it will just work. Otherwise use template
702 * specialization to create a version that matches the lock strategy's ctor
703 * signature. See ACE_Process_Semaphore and ACE_Thread_Semaphore for
704 * examples.
707 /*****************************************************************************/
710 * @class ACE_Malloc_LIFO_Iterator_T
712 * @brief LIFO iterator for names stored in Malloc'd memory.
714 * This class can be configured flexibly with different types of
715 * ACE_LOCK strategies that support the @a ACE_Thread_Mutex and @a
716 * ACE_Process_Mutex constructor API.
718 * Does not support deletions while iteration is occurring.
720 template <ACE_MEM_POOL_1, class ACE_LOCK, class ACE_CB>
721 class ACE_Malloc_LIFO_Iterator_T
723 public:
724 typedef typename ACE_CB::ACE_Name_Node NAME_NODE;
725 typedef typename ACE_CB::ACE_Malloc_Header MALLOC_HEADER;
727 // = Initialization method.
728 /// If @a name = 0 it will iterate through everything else only
729 /// through those entries whose @a name match.
730 ACE_Malloc_LIFO_Iterator_T (ACE_Malloc_T<ACE_MEM_POOL_2, ACE_LOCK, ACE_CB> &malloc,
731 const char *name = 0);
733 /// Destructor.
734 ~ACE_Malloc_LIFO_Iterator_T (void);
736 // = Iteration methods.
738 /// Returns 1 when all items have been seen, else 0.
739 int done (void) const;
741 /// Pass back the next entry in the set that hasn't yet been
742 /// visited. Returns 0 when all items have been seen, else 1.
743 int next (void *&next_entry);
746 * Pass back the next entry (and the name associated with it) in
747 * the set that hasn't yet been visited. Returns 0 when all items
748 * have been seen, else 1.
750 int next (void *&next_entry,
751 const char *&name);
753 /// Move forward by one element in the set. Returns 0 when all the
754 /// items in the set have been seen, else 1.
755 int advance (void);
757 /// Dump the state of an object.
758 void dump (void) const;
760 /// Declare the dynamic allocation hooks.
761 ACE_ALLOC_HOOK_DECLARE;
763 private:
764 /// Malloc we are iterating over.
765 ACE_Malloc_T<ACE_MEM_POOL_2, ACE_LOCK, ACE_CB> &malloc_;
767 /// Keeps track of how far we've advanced...
768 NAME_NODE *curr_;
770 /// Lock Malloc for the lifetime of the iterator.
771 ACE_Read_Guard<ACE_LOCK> guard_;
773 /// Name that we are searching for.
774 const char *name_;
778 * @class ACE_Malloc_FIFO_Iterator_T
780 * @brief FIFO iterator for names stored in Malloc'd memory.
782 * This class can be configured flexibly with different types of
783 * ACE_LOCK strategies that support the @a ACE_Thread_Mutex and @a
784 * ACE_Process_Mutex constructor API.
786 * Does not support deletions while iteration is occurring.
788 template <ACE_MEM_POOL_1, class ACE_LOCK, class ACE_CB>
789 class ACE_Malloc_FIFO_Iterator_T
791 public:
792 typedef typename ACE_CB::ACE_Name_Node NAME_NODE;
793 typedef typename ACE_CB::ACE_Malloc_Header MALLOC_HEADER;
795 // = Initialization method.
796 /// If @a name = 0 it will iterate through everything else only
797 /// through those entries whose @a name match.
798 ACE_Malloc_FIFO_Iterator_T (ACE_Malloc_T<ACE_MEM_POOL_2, ACE_LOCK, ACE_CB> &malloc,
799 const char *name = 0);
801 /// Destructor.
802 ~ACE_Malloc_FIFO_Iterator_T (void);
804 // = Iteration methods.
806 /// Returns 1 when all items have been seen, else 0.
807 int done (void) const;
809 /// Pass back the next entry in the set that hasn't yet been
810 /// visited. Returns 0 when all items have been seen, else 1.
811 int next (void *&next_entry);
814 * Pass back the next entry (and the name associated with it) in
815 * the set that hasn't yet been visited. Returns 0 when all items
816 * have been seen, else 1.
818 int next (void *&next_entry,
819 const char *&name);
821 /// Move forward by one element in the set. Returns 0 when all the
822 /// items in the set have been seen, else 1.
823 int advance (void);
825 /// Go to the starting element that was inserted first. Returns 0
826 /// when there is no item in the set, else 1.
827 int start (void);
829 /// Dump the state of an object.
830 void dump (void) const;
832 /// Declare the dynamic allocation hooks.
833 ACE_ALLOC_HOOK_DECLARE;
835 private:
836 /// Malloc we are iterating over.
837 ACE_Malloc_T<ACE_MEM_POOL_2, ACE_LOCK, ACE_CB> &malloc_;
839 /// Keeps track of how far we've advanced...
840 NAME_NODE *curr_;
842 /// Lock Malloc for the lifetime of the iterator.
843 ACE_Read_Guard<ACE_LOCK> guard_;
845 /// Name that we are searching for.
846 const char *name_;
849 template <ACE_MEM_POOL_1, class ACE_LOCK>
850 class ACE_Malloc : public ACE_Malloc_T<ACE_MEM_POOL_2, ACE_LOCK, ACE_Control_Block>
852 public:
853 // = Initialization and termination methods.
855 * Initialize ACE_Malloc. This constructor passes @a pool_name to
856 * initialize the memory pool, and uses ACE::basename() to
857 * automatically extract out the name used for the underlying lock
858 * name (if necessary). Note that @a pool_name should be located in
859 * a directory with the appropriate visibility and protection so
860 * that all processes that need to access it can do so.
862 ACE_Malloc (const ACE_TCHAR *pool_name = 0);
865 * Initialize ACE_Malloc. This constructor passes @a pool_name to
866 * initialize the memory pool, and uses @a lock_name to automatically
867 * extract out the name used for the underlying lock name (if
868 * necessary). In addition, @a options is passed through to
869 * initialize the underlying memory pool. Note that @a pool_name
870 * should be located in a directory with the appropriate visibility
871 * and protection so that all processes that need to access it can
872 * do so.
874 ACE_Malloc (const ACE_TCHAR *pool_name,
875 const ACE_TCHAR *lock_name,
876 const ACE_MEM_POOL_OPTIONS *options = 0);
878 #if !defined (ACE_HAS_TEMPLATE_TYPEDEFS)
879 /// This is necessary to work around template bugs with certain C++
880 /// compilers.
881 ACE_Malloc (const ACE_TCHAR *pool_name,
882 const ACE_TCHAR *lock_name,
883 const void *options = 0);
884 #endif /* ACE_HAS_TEMPLATE_TYPEDEFS */
887 template <ACE_MEM_POOL_1, class ACE_LOCK>
888 class ACE_Malloc_LIFO_Iterator : public ACE_Malloc_LIFO_Iterator_T<ACE_MEM_POOL_2, ACE_LOCK, ACE_Control_Block>
890 public:
891 // = Initialization method.
892 /// If @a name = 0 it will iterate through everything else only
893 /// through those entries whose @a name match.
894 ACE_Malloc_LIFO_Iterator (ACE_Malloc<ACE_MEM_POOL_2, ACE_LOCK> &malloc,
895 const char *name = 0);
898 template <ACE_MEM_POOL_1, class ACE_LOCK>
899 class ACE_Malloc_FIFO_Iterator : public ACE_Malloc_FIFO_Iterator_T<ACE_MEM_POOL_2, ACE_LOCK, ACE_Control_Block>
901 public:
902 // = Initialization method.
903 /// If @a name = 0 it will iterate through everything else only
904 /// through those entries whose @a name match.
905 ACE_Malloc_FIFO_Iterator (ACE_Malloc<ACE_MEM_POOL_2, ACE_LOCK> &malloc,
906 const char *name = 0);
909 template <class ACE_LOCK>
910 class ACE_Malloc_Lock_Adapter_T
912 public:
913 ACE_LOCK * operator () (const ACE_TCHAR *myname);
916 ACE_END_VERSIONED_NAMESPACE_DECL
918 #if defined (__ACE_INLINE__)
919 #include "ace/Malloc_T.inl"
920 #endif /* __ACE_INLINE__ */
922 #if defined (ACE_TEMPLATES_REQUIRE_SOURCE)
923 #include "ace/Malloc_T.cpp"
924 #endif /* ACE_TEMPLATES_REQUIRE_SOURCE */
926 #if defined (ACE_TEMPLATES_REQUIRE_PRAGMA)
927 #pragma implementation ("Malloc_T.cpp")
928 #endif /* ACE_TEMPLATES_REQUIRE_PRAGMA */
930 #include /**/ "ace/post.h"
931 #endif /* ACE_MALLOC_H */