defalloc.h

00001 /*
00002  *
00003  * Copyright (c) 1994
00004  * Hewlett-Packard Company
00005  *
00006  * Permission to use, copy, modify, distribute and sell this software
00007  * and its documentation for any purpose is hereby granted without fee,
00008  * provided that the above copyright notice appear in all copies and
00009  * that both that copyright notice and this permission notice appear
00010  * in supporting documentation.  Hewlett-Packard Company makes no
00011  * representations about the suitability of this software for any
00012  * purpose.  It is provided "as is" without express or implied warranty.
00013  *
00014  */
00015 
00016 // Inclusion of this file is DEPRECATED.  This is the original HP
00017 // default allocator.  It is provided only for backward compatibility.
00018 // This file WILL BE REMOVED in a future release.
00019 //
00020 // DO NOT USE THIS FILE unless you have an old container implementation
00021 // that requires an allocator with the HP-style interface.  
00022 //
00023 // Standard-conforming allocators have a very different interface.  The
00024 // standard default allocator is declared in the header <memory>.
00025 
00026 #ifndef DEFALLOC_H
00027 #define DEFALLOC_H
00028 
00029 #ifndef UNDER_CE
00030 #include <new.h>
00031 #include <stddef.h>
00032 #else
00033 #include <wce_defs.h>
00034 #endif /* UNDER_CE */
00035 #include <stdlib.h>
00036 #include <limits.h>
00037 #ifndef UNDER_CE
00038 #include <iostream.h>
00039 #endif /* UNDER_CE */
00040 #include <algobase.h>
00041 
00042 template <class T>
00043 inline T* allocate(ptrdiff_t size, T*) {
00044 #ifndef UNDER_CE
00045     set_new_handler(0);
00046 #endif
00047     T* tmp = (T*)(::operator new((size_t)(size * sizeof(T))));
00048     if (tmp == 0) {
00049 #ifndef UNDER_CE
00050         cerr << "out of memory" << endl; 
00051         exit(1);
00052 #else
00053     __THROW_BAD_ALLOC;
00054 #endif
00055     }
00056     return tmp;
00057 }
00058 
00059 template <class T>
00060 inline void deallocate(T* buffer) {
00061     ::operator delete(buffer);
00062 }
00063 
00064 template <class T>
00065 class allocator {
00066 public:
00067     typedef T value_type;
00068     typedef T* pointer;
00069     typedef const T* const_pointer;
00070     typedef T& reference;
00071     typedef const T& const_reference;
00072     typedef size_t size_type;
00073     typedef ptrdiff_t difference_type;
00074     pointer allocate(size_type n) { 
00075         return ::allocate((difference_type)n, (pointer)0);
00076     }
00077     void deallocate(pointer p) { ::deallocate(p); }
00078     pointer address(reference x) { return (pointer)&x; }
00079     const_pointer const_address(const_reference x) { 
00080         return (const_pointer)&x; 
00081     }
00082     size_type init_page_size() { 
00083         return max(size_type(1), size_type(4096/sizeof(T))); 
00084     }
00085     size_type max_size() const { 
00086         return max(size_type(1), size_type(UINT_MAX/sizeof(T))); 
00087     }
00088 };
00089 
00090 class allocator<void> {
00091 public:
00092     typedef void* pointer;
00093 };
00094 
00095 
00096 
00097 #endif

Generated on Mon Jun 5 10:20:43 2006 for Intelligence.kdevelop by  doxygen 1.4.6