| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 |
|
|---|
| 34 |
|
|---|
| 35 |
|
|---|
| 36 |
|
|---|
| 37 |
#ifndef _MEMPOOL_ALLOC_H_ |
|---|
| 38 |
#define _MEMPOOL_ALLOC_H_ |
|---|
| 39 |
|
|---|
| 40 |
#include <vjConfig.h> |
|---|
| 41 |
#include <alloc.h> |
|---|
| 42 |
#include <SharedMem/vjMemPool.h> |
|---|
| 43 |
|
|---|
| 44 |
|
|---|
| 45 |
|
|---|
| 46 |
|
|---|
| 47 |
#ifdef __STL_STATIC_TEMPLATE_MEMBER_BUG |
|---|
| 48 |
# ifdef __DECLARE_GLOBALS_HERE |
|---|
| 49 |
void (* __mempool_alloc_oom_handler)() = 0; |
|---|
| 50 |
|
|---|
| 51 |
# else |
|---|
| 52 |
extern void (* __mempool_alloc_oom_handler)(); |
|---|
| 53 |
# endif |
|---|
| 54 |
#endif |
|---|
| 55 |
|
|---|
| 56 |
template <int inst> |
|---|
| 57 |
class __vj_mempool_alloc_template |
|---|
| 58 |
{ |
|---|
| 59 |
|
|---|
| 60 |
private: |
|---|
| 61 |
|
|---|
| 62 |
static void *oom_malloc(size_t); |
|---|
| 63 |
|
|---|
| 64 |
static void *oom_realloc(void *, size_t); |
|---|
| 65 |
|
|---|
| 66 |
#ifndef __STL_STATIC_TEMPLATE_MEMBER_BUG |
|---|
| 67 |
static void (* __mempool_alloc_oom_handler)(); |
|---|
| 68 |
#endif |
|---|
| 69 |
|
|---|
| 70 |
public: |
|---|
| 71 |
|
|---|
| 72 |
static void * allocate(size_t n) |
|---|
| 73 |
{ |
|---|
| 74 |
verifyMempoolAllocated(); |
|---|
| 75 |
void *result = STLMemPool->allocate(n); |
|---|
| 76 |
if (0 == result) result = oom_malloc(n); |
|---|
| 77 |
return result; |
|---|
| 78 |
} |
|---|
| 79 |
|
|---|
| 80 |
static void deallocate(void *p, size_t ) |
|---|
| 81 |
{ |
|---|
| 82 |
STLMemPool->deallocate(p); |
|---|
| 83 |
} |
|---|
| 84 |
|
|---|
| 85 |
static void * reallocate(void *p, size_t , size_t new_sz) |
|---|
| 86 |
{ |
|---|
| 87 |
void * result = STLMemPool->reallocate(p, new_sz); |
|---|
| 88 |
if (0 == result) result = oom_realloc(p, new_sz); |
|---|
| 89 |
return result; |
|---|
| 90 |
} |
|---|
| 91 |
|
|---|
| 92 |
static void (* set_malloc_handler(void (*f)()))() |
|---|
| 93 |
{ |
|---|
| 94 |
void (* old)() = __mempool_alloc_oom_handler; |
|---|
| 95 |
__mempool_alloc_oom_handler = f; |
|---|
| 96 |
return (old); |
|---|
| 97 |
} |
|---|
| 98 |
|
|---|
| 99 |
public: |
|---|
| 100 |
|
|---|
| 101 |
static void verifyMempoolAllocated() |
|---|
| 102 |
{ |
|---|
| 103 |
if (STLMemPool == NULL) |
|---|
| 104 |
{ |
|---|
| 105 |
STLMemPool = new vjSharedPool(1024*1024); |
|---|
| 106 |
} |
|---|
| 107 |
} |
|---|
| 108 |
|
|---|
| 109 |
static vjMemPool* STLMemPool; |
|---|
| 110 |
}; |
|---|
| 111 |
|
|---|
| 112 |
MemPool* __vj_mempool_alloc_template<0>::STLMemPool = NULL; |
|---|
| 113 |
|
|---|
| 114 |
|
|---|
| 115 |
|
|---|
| 116 |
#ifndef __STL_STATIC_TEMPLATE_MEMBER_BUG |
|---|
| 117 |
template <int inst> |
|---|
| 118 |
void (* __vj_mempool_alloc_template<inst>::__mempool_alloc_oom_handler)() = 0; |
|---|
| 119 |
#endif |
|---|
| 120 |
|
|---|
| 121 |
template <int inst> |
|---|
| 122 |
void * __vj_mempool_alloc_template<inst>::oom_malloc(size_t n) |
|---|
| 123 |
{ |
|---|
| 124 |
void (* my_malloc_handler)(); |
|---|
| 125 |
void *result; |
|---|
| 126 |
|
|---|
| 127 |
for (;;) { |
|---|
| 128 |
my_malloc_handler = __mempool_alloc_oom_handler; |
|---|
| 129 |
if (0 == my_malloc_handler) { __THROW_BAD_ALLOC; } |
|---|
| 130 |
(*my_malloc_handler)(); |
|---|
| 131 |
result = malloc(n); |
|---|
| 132 |
if (result) return(result); |
|---|
| 133 |
} |
|---|
| 134 |
} |
|---|
| 135 |
|
|---|
| 136 |
template <int inst> |
|---|
| 137 |
void * __vj_mempool_alloc_template<inst>::oom_realloc(void *p, size_t n) |
|---|
| 138 |
{ |
|---|
| 139 |
void (* my_malloc_handler)(); |
|---|
| 140 |
void *result; |
|---|
| 141 |
|
|---|
| 142 |
for (;;) { |
|---|
| 143 |
my_malloc_handler = __mempool_alloc_oom_handler; |
|---|
| 144 |
if (0 == my_malloc_handler) { __THROW_BAD_ALLOC; } |
|---|
| 145 |
(*my_malloc_handler)(); |
|---|
| 146 |
result = realloc(p, n); |
|---|
| 147 |
if (result) return(result); |
|---|
| 148 |
} |
|---|
| 149 |
} |
|---|
| 150 |
|
|---|
| 151 |
typedef __vj_mempool_alloc_template<0> mempool_alloc; |
|---|
| 152 |
|
|---|
| 153 |
#endif // _MEMPOOL_ALLOC_H_ |
|---|