00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #ifndef MEMORYHOOK_H
00016 #define MEMORYHOOK_H
00017
00018 #include "dtoolbase.h"
00019 #include "numeric_types.h"
00020 #include "atomicAdjust.h"
00021 #include "mutexImpl.h"
00022 #include <map>
00023
00024 class DeletedBufferChain;
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043 class EXPCL_DTOOL MemoryHook {
00044 public:
00045 MemoryHook();
00046 MemoryHook(const MemoryHook ©);
00047 virtual ~MemoryHook();
00048
00049 virtual void *heap_alloc_single(size_t size);
00050 virtual void heap_free_single(void *ptr);
00051
00052 virtual void *heap_alloc_array(size_t size);
00053 virtual void *heap_realloc_array(void *ptr, size_t size);
00054 virtual void heap_free_array(void *ptr);
00055
00056 INLINE void inc_heap(size_t size);
00057 INLINE void dec_heap(size_t size);
00058
00059 bool heap_trim(size_t pad);
00060
00061 INLINE static size_t get_memory_alignment();
00062 INLINE static size_t get_header_reserved_bytes();
00063
00064 virtual void *mmap_alloc(size_t size, bool allow_exec);
00065 virtual void mmap_free(void *ptr, size_t size);
00066 INLINE size_t get_page_size() const;
00067 INLINE size_t round_up_to_page_size(size_t size) const;
00068
00069 virtual void mark_pointer(void *ptr, size_t orig_size, ReferenceCount *ref_ptr);
00070
00071 DeletedBufferChain *get_deleted_chain(size_t buffer_size);
00072
00073 virtual void alloc_fail(size_t attempted_size);
00074
00075 private:
00076 INLINE static size_t inflate_size(size_t size);
00077 INLINE static void *alloc_to_ptr(void *alloc, size_t size);
00078 INLINE static void *ptr_to_alloc(void *ptr, size_t &size);
00079
00080 #ifdef DO_MEMORY_USAGE
00081 protected:
00082 TVOLATILE AtomicAdjust::Integer _total_heap_single_size;
00083 TVOLATILE AtomicAdjust::Integer _total_heap_array_size;
00084 TVOLATILE AtomicAdjust::Integer _requested_heap_size;
00085 TVOLATILE AtomicAdjust::Integer _total_mmap_size;
00086
00087
00088
00089 size_t _max_heap_size;
00090
00091 virtual void overflow_heap_size();
00092 #endif // DO_MEMORY_USAGE
00093
00094 private:
00095 size_t _page_size;
00096
00097 typedef map<size_t, DeletedBufferChain *> DeletedChains;
00098 DeletedChains _deleted_chains;
00099
00100 MutexImpl _lock;
00101 };
00102
00103 #include "memoryHook.I"
00104
00105 #endif