home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / libg++-2.7.1-bin.lha / lib / g++-include / defalloc.h < prev    next >
C/C++ Source or Header  |  1996-10-12  |  5KB  |  177 lines

  1. /*
  2.  *
  3.  * Copyright (c) 1994
  4.  * Hewlett-Packard Company
  5.  *
  6.  * Permission to use, copy, modify, distribute and sell this software
  7.  * and its documentation for any purpose is hereby granted without fee,
  8.  * provided that the above copyright notice appear in all copies and
  9.  * that both that copyright notice and this permission notice appear
  10.  * in supporting documentation.  Hewlett-Packard Company makes no
  11.  * representations about the suitability of this software for any
  12.  * purpose.  It is provided "as is" without express or implied warranty.
  13.  *
  14.  */
  15.  
  16. #ifndef DEFALLOC_H
  17. #define DEFALLOC_H
  18.  
  19. #include <new.h>
  20. #include <stddef.h>
  21. #include <stdlib.h>
  22. #include <limits.h>
  23. #include <iostream.h>
  24. #include <algobase.h>
  25.  
  26. #ifndef __GNUG__
  27. inline void* operator new(size_t, void* p) {return p;}
  28. #endif
  29.  
  30. /*
  31.  * the following template function is replaced by the following two functions
  32.  * due to the fact that the Borland compiler doesn't change prediff_t type
  33.  * to type long when compile with -ml or -mh.
  34.  
  35. template <class T>
  36. inline T* allocate(ptrdiff_t size, T*) {
  37.     set_new_handler(0);
  38.     T* tmp = (T*)(::operator new((size_t)(size * sizeof(T))));
  39.     if (tmp == 0) {
  40.     cerr << "out of memory" << endl; 
  41.     exit(1);
  42.     }
  43.     return tmp;
  44. }
  45. */
  46.  
  47. template <class T>
  48. inline T* allocate(int size, T*) {
  49.     set_new_handler(0);
  50.     T* tmp = (T*)(::operator new((unsigned int)(size * sizeof(T))));
  51.     if (tmp == 0) {
  52.     cerr << "out of memory" << endl; 
  53.     exit(1);
  54.     }
  55.     return tmp;
  56. }
  57.  
  58. template <class T>
  59. inline T* allocate(long size, T*) {
  60.     set_new_handler(0);
  61.     T* tmp = (T*)(::operator new((unsigned long)(size * sizeof(T))));
  62.     if (tmp == 0) {
  63.     cerr << "out of memory" << endl; 
  64.     exit(1);
  65.     }
  66.     return tmp;
  67. }
  68.  
  69. template <class T>
  70. inline void deallocate(T* buffer) {
  71.     ::operator delete(buffer);
  72. }
  73.  
  74. template <class T>
  75. inline void destroy(T* pointer) {
  76.     pointer->~T();
  77. }
  78.  
  79. inline void destroy(char*) {}
  80. inline void destroy(unsigned char*) {}
  81. inline void destroy(short*) {}
  82. inline void destroy(unsigned short*) {}
  83. inline void destroy(int*) {}
  84. inline void destroy(unsigned int*) {}
  85. inline void destroy(long*) {}
  86. inline void destroy(unsigned long*) {}
  87. inline void destroy(float*) {}
  88. inline void destroy(double*) {}
  89. inline void destroy(char**) {}
  90. inline void destroy(unsigned char**) {}
  91. inline void destroy(short**) {}
  92. inline void destroy(unsigned short**) {}
  93. inline void destroy(int**) {}
  94. inline void destroy(unsigned int**) {}
  95. inline void destroy(long**) {}
  96. inline void destroy(unsigned long**) {}
  97. inline void destroy(float**) {}
  98. inline void destroy(double**) {}
  99.  
  100. inline void destroy(char*, char*) {}
  101. inline void destroy(unsigned char*, unsigned char*) {}
  102. inline void destroy(short*, short*) {}
  103. inline void destroy(unsigned short*, unsigned short*) {}
  104. inline void destroy(int*, int*) {}
  105. inline void destroy(unsigned int*, unsigned int*) {}
  106. inline void destroy(long*, long*) {}
  107. inline void destroy(unsigned long*, unsigned long*) {}
  108. inline void destroy(float*, float*) {}
  109. inline void destroy(double*, double*) {}
  110. inline void destroy(char**, char**) {}
  111. inline void destroy(unsigned char**, unsigned char**) {}
  112. inline void destroy(short**, short**) {}
  113. inline void destroy(unsigned short**, unsigned short**) {}
  114. inline void destroy(int**, int**) {}
  115. inline void destroy(unsigned int**, unsigned int**) {}
  116. inline void destroy(long**, long**) {}
  117. inline void destroy(unsigned long**, unsigned long**) {}
  118. inline void destroy(float**, float**) {}
  119. inline void destroy(double**, double**) {}
  120.  
  121. template <class T1, class T2>
  122. inline void construct(T1* p, const T2& value) {
  123.     new (p) T1(value);
  124. }
  125.  
  126. template <class T>
  127. class allocator {
  128. public:
  129.     typedef T value_type;
  130.     typedef T* pointer;
  131.     typedef const T* const_pointer;
  132.     typedef T& reference;
  133.     typedef const T& const_reference;
  134.     typedef size_t size_type;
  135.     typedef ptrdiff_t difference_type;
  136. #ifdef __GNUG__
  137.     static pointer allocate(size_type n) { 
  138.     return ::allocate((difference_type)n, (pointer)0);
  139.     }
  140.     static void deallocate(pointer p) { ::deallocate(p); }
  141.     static pointer address(reference x) { return (pointer)&x; }
  142.     static const_pointer const_address(const_reference x) { 
  143.     return (const_pointer)&x; 
  144.     }
  145.     static size_type init_page_size() { 
  146.     return max(size_type(1), size_type(4096/sizeof(T))); 
  147.     }
  148.     static size_type max_size() { 
  149.     return max(size_type(1), size_type(UINT_MAX/sizeof(T))); 
  150.     }
  151. #else
  152.     pointer allocate(size_type n) { 
  153.     return ::allocate((difference_type)n, (pointer)0);
  154.     }
  155.     void deallocate(pointer p) { ::deallocate(p); }
  156.     pointer address(reference x) { return (pointer)&x; }
  157.     const_pointer const_address(const_reference x) { 
  158.     return (const_pointer)&x; 
  159.     }
  160.     size_type init_page_size() { 
  161.     return max(size_type(1), size_type(4096/sizeof(T))); 
  162.     }
  163.     size_type max_size() const { 
  164.     return max(size_type(1), size_type(UINT_MAX/sizeof(T))); 
  165.     }
  166. #endif
  167. };
  168.  
  169. class allocator<void> {
  170. public:
  171.     typedef void* pointer;
  172. };
  173.  
  174.  
  175.  
  176. #endif
  177.