X-Git-Url: http://plrg.eecs.uci.edu/git/?p=c11tester.git;a=blobdiff_plain;f=mymemory.h;h=87ee0e92b5bc863b344302a8ea6888571b21be76;hp=56e690436d1e573da00bf287e6aff2388f476c0c;hb=7f6f38735411f44357208a952278a419454b52b2;hpb=f3ef22bef8d339c7d45b7d7232cdcf183a0b7776 diff --git a/mymemory.h b/mymemory.h index 56e69043..87ee0e92 100644 --- a/mymemory.h +++ b/mymemory.h @@ -9,28 +9,43 @@ /** MEMALLOC declares the allocators for a class to allocate * memory in the non-snapshotting heap. */ - #define MEMALLOC \ void * operator new(size_t size) { \ - return MYMALLOC(size);\ - }\ + return model_malloc(size); \ + } \ void operator delete(void *p, size_t size) { \ - MYFREE( p ); \ - }\ + model_free(p); \ + } \ void * operator new[](size_t size) { \ - return MYMALLOC(size);\ - }\ - void operator delete[](void *p, size_t size) {\ - MYFREE(p);\ + return model_malloc(size); \ + } \ + void operator delete[](void *p, size_t size) { \ + model_free(p); \ } /** SNAPSHOTALLOC declares the allocators for a class to allocate * memory in the snapshotting heap. */ +#define SNAPSHOTALLOC \ + void * operator new(size_t size) { \ + return snapshot_malloc(size); \ + } \ + void operator delete(void *p, size_t size) { \ + snapshot_free(p); \ + } \ + void * operator new[](size_t size) { \ + return snapshot_malloc(size); \ + } \ + void operator delete[](void *p, size_t size) { \ + snapshot_free(p); \ + } -#define SNAPSHOTALLOC +void *model_malloc(size_t size); +void *model_calloc(size_t count, size_t size); +void model_free(void *ptr); -void *MYMALLOC(size_t size); -void MYFREE(void *ptr); +void * snapshot_malloc(size_t size); +void * snapshot_calloc(size_t count, size_t size); +void snapshot_free(void *ptr); void system_free( void * ptr ); void *system_malloc( size_t size ); @@ -45,88 +60,87 @@ void *system_malloc( size_t size ); * This software is provided "as is" without express or implied * warranty, and with no claim as to its suitability for any purpose. */ - template - class MyAlloc { - public: - // type definitions - typedef T value_type; - typedef T* pointer; - typedef const T* const_pointer; - typedef T& reference; - typedef const T& const_reference; - typedef size_t size_type; - typedef size_t difference_type; - - // rebind allocator to type U - template - struct rebind { - typedef MyAlloc other; - }; - - // return address of values - pointer address (reference value) const { - return &value; - } - const_pointer address (const_reference value) const { - return &value; - } - - /* constructors and destructor - * - nothing to do because the allocator has no state - */ - MyAlloc() throw() { - } - MyAlloc(const MyAlloc&) throw() { - } - template - MyAlloc (const MyAlloc&) throw() { - } - ~MyAlloc() throw() { - } - - // return maximum number of elements that can be allocated - size_type max_size () const throw() { - return std::numeric_limits::max() / sizeof(T); - } - - // allocate but don't initialize num elements of type T - pointer allocate (size_type num, const void* = 0) { - pointer p = ( pointer )MYMALLOC( num * sizeof( T ) ); - return p; - } - - // initialize elements of allocated storage p with value value - void construct (pointer p, const T& value) { - // initialize memory with placement new - new((void*)p)T(value); - } - - // destroy elements of initialized storage p - void destroy (pointer p) { - // destroy objects by calling their destructor - p->~T(); - } - - // deallocate storage p of deleted elements - void deallocate (pointer p, size_type num) { - MYFREE((void*)p); - } - }; +class ModelAlloc { + public: + // type definitions + typedef T value_type; + typedef T* pointer; + typedef const T* const_pointer; + typedef T& reference; + typedef const T& const_reference; + typedef size_t size_type; + typedef size_t difference_type; + + // rebind allocator to type U + template + struct rebind { + typedef ModelAlloc other; + }; + + // return address of values + pointer address(reference value) const { + return &value; + } + const_pointer address(const_reference value) const { + return &value; + } + + /* constructors and destructor + * - nothing to do because the allocator has no state + */ + ModelAlloc() throw() { + } + ModelAlloc(const ModelAlloc&) throw() { + } + template + ModelAlloc(const ModelAlloc&) throw() { + } + ~ModelAlloc() throw() { + } + + // return maximum number of elements that can be allocated + size_type max_size() const throw() { + return std::numeric_limits::max() / sizeof(T); + } + + // allocate but don't initialize num elements of type T + pointer allocate(size_type num, const void * = 0) { + pointer p = (pointer)model_malloc(num * sizeof(T)); + return p; + } + + // initialize elements of allocated storage p with value value + void construct(pointer p, const T& value) { + // initialize memory with placement new + new((void*)p)T(value); + } + + // destroy elements of initialized storage p + void destroy(pointer p) { + // destroy objects by calling their destructor + p->~T(); + } + + // deallocate storage p of deleted elements + void deallocate(pointer p, size_type num) { + model_free((void*)p); + } +}; /** Return that all specializations of this allocator are interchangeable. */ - template - bool operator== (const MyAlloc&, - const MyAlloc&) throw() { - return true; - } +template +bool operator== (const ModelAlloc&, + const ModelAlloc&) throw() { + return true; +} /** Return that all specializations of this allocator are interchangeable. */ - template - bool operator!= (const MyAlloc&, - const MyAlloc&) throw() { - return false; - } +template +bool operator!= (const ModelAlloc&, + const ModelAlloc&) throw() { + return false; +} #ifdef __cplusplus extern "C" {