home *** CD-ROM | disk | FTP | other *** search
/ Borland Programmer's Resource / Borland_Programmers_Resource_CD_1995.iso / code / stl / defalloc.h < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-19  |  4.3 KB  |  158 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. inline void* operator new(size_t, void* p) {return p;}
  27.  
  28. /*
  29.  * the following template function is replaced by the following two functions
  30.  * due to the fact that the Borland compiler doesn't change prediff_t type
  31.  * to type long when compile with -ml or -mh.
  32.  
  33. template <class T>
  34. inline T* allocate(ptrdiff_t size, T*) {
  35.     set_new_handler(0);
  36.     T* tmp = (T*)(::operator new((size_t)(size * sizeof(T))));
  37.     if (tmp == 0) {
  38.     cerr << "out of memory" << endl; 
  39.     exit(1);
  40.     }
  41.     return tmp;
  42. }
  43. */
  44.  
  45. template <class T>
  46. inline T* allocate(int size, T*) {
  47.     set_new_handler(0);
  48.     T* tmp = (T*)(::operator new((unsigned int)(size * sizeof(T))));
  49.     if (tmp == 0) {
  50.     cerr << "out of memory" << endl; 
  51.     exit(1);
  52.     }
  53.     return tmp;
  54. }
  55.  
  56. template <class T>
  57. inline T* allocate(long size, T*) {
  58.     set_new_handler(0);
  59.     T* tmp = (T*)(::operator new((unsigned long)(size * sizeof(T))));
  60.     if (tmp == 0) {
  61.     cerr << "out of memory" << endl; 
  62.     exit(1);
  63.     }
  64.     return tmp;
  65. }
  66.  
  67. template <class T>
  68. inline void deallocate(T* buffer) {
  69.     ::operator delete(buffer);
  70. }
  71.  
  72. template <class T>
  73. inline void destroy(T* pointer) {
  74.     pointer->~T();
  75. }
  76.  
  77. inline void destroy(char*) {}
  78. inline void destroy(unsigned char*) {}
  79. inline void destroy(short*) {}
  80. inline void destroy(unsigned short*) {}
  81. inline void destroy(int*) {}
  82. inline void destroy(unsigned int*) {}
  83. inline void destroy(long*) {}
  84. inline void destroy(unsigned long*) {}
  85. inline void destroy(float*) {}
  86. inline void destroy(double*) {}
  87. inline void destroy(char**) {}
  88. inline void destroy(unsigned char**) {}
  89. inline void destroy(short**) {}
  90. inline void destroy(unsigned short**) {}
  91. inline void destroy(int**) {}
  92. inline void destroy(unsigned int**) {}
  93. inline void destroy(long**) {}
  94. inline void destroy(unsigned long**) {}
  95. inline void destroy(float**) {}
  96. inline void destroy(double**) {}
  97.  
  98. inline void destroy(char*, char*) {}
  99. inline void destroy(unsigned char*, unsigned char*) {}
  100. inline void destroy(short*, short*) {}
  101. inline void destroy(unsigned short*, unsigned short*) {}
  102. inline void destroy(int*, int*) {}
  103. inline void destroy(unsigned int*, unsigned int*) {}
  104. inline void destroy(long*, long*) {}
  105. inline void destroy(unsigned long*, unsigned long*) {}
  106. inline void destroy(float*, float*) {}
  107. inline void destroy(double*, double*) {}
  108. inline void destroy(char**, char**) {}
  109. inline void destroy(unsigned char**, unsigned char**) {}
  110. inline void destroy(short**, short**) {}
  111. inline void destroy(unsigned short**, unsigned short**) {}
  112. inline void destroy(int**, int**) {}
  113. inline void destroy(unsigned int**, unsigned int**) {}
  114. inline void destroy(long**, long**) {}
  115. inline void destroy(unsigned long**, unsigned long**) {}
  116. inline void destroy(float**, float**) {}
  117. inline void destroy(double**, double**) {}
  118.  
  119. template <class T1, class T2>
  120. inline void construct(T1* p, const T2& value) {
  121.     new (p) T1(value);
  122. }
  123.  
  124. template <class T>
  125. class allocator {
  126. public:
  127.     typedef T value_type;
  128.     typedef T* pointer;
  129.     typedef const T* const_pointer;
  130.     typedef T& reference;
  131.     typedef const T& const_reference;
  132.     typedef size_t size_type;
  133.     typedef ptrdiff_t difference_type;
  134.     pointer allocate(size_type n) { 
  135.     return ::allocate((difference_type)n, (pointer)0);
  136.     }
  137.     void deallocate(pointer p) { ::deallocate(p); }
  138.     pointer address(reference x) { return (pointer)&x; }
  139.     const_pointer const_address(const_reference x) { 
  140.     return (const_pointer)&x; 
  141.     }
  142.     size_type init_page_size() { 
  143.     return max(size_type(1), size_type(4096/sizeof(T))); 
  144.     }
  145.     size_type max_size() const { 
  146.     return max(size_type(1), size_type(UINT_MAX/sizeof(T))); 
  147.     }
  148. };
  149.  
  150. class allocator<void> {
  151. public:
  152.     typedef void* pointer;
  153. };
  154.  
  155.  
  156.  
  157. #endif
  158.