home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / libg++-2.7.1-bin.lha / lib / g++-include / faralloc.h < prev    next >
C/C++ Source or Header  |  1996-10-12  |  4KB  |  121 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 FARALLOC_H
  17. #define FARALLOC_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. template <class T>
  27. inline random_access_iterator_tag iterator_category(const T __far *) {
  28.     return random_access_iterator_tag();
  29. }
  30.  
  31. template <class T>
  32. inline T* value_type(const T __far *) { return (T*)(0); }
  33.  
  34. template <class T>
  35. inline long* distance_type(const T __far*) { return (long*)(0); }
  36.  
  37. inline void destroy(char __far *) {}
  38. inline void destroy(unsigned char __far *) {}
  39. inline void destroy(short __far *) {}
  40. inline void destroy(unsigned short __far *) {}
  41. inline void destroy(int __far *) {}
  42. inline void destroy(unsigned int __far *) {}
  43. inline void destroy(long __far *) {}
  44. inline void destroy(unsigned long __far *) {}
  45. inline void destroy(float __far *) {}
  46. inline void destroy(double __far *) {}
  47.  
  48. inline void destroy(char __far *, char __far *) {}
  49. inline void destroy(unsigned char __far *, unsigned char __far *) {}
  50. inline void destroy(short __far *, short __far *) {}
  51. inline void destroy(unsigned short __far *, unsigned short __far *) {}
  52. inline void destroy(int __far *, int __far *) {}
  53. inline void destroy(unsigned int __far *, unsigned int __far *) {}
  54. inline void destroy(long __far *, long __far *) {}
  55. inline void destroy(unsigned long __far *, unsigned long __far *) {}
  56. inline void destroy(float __far *, float __far *) {}
  57. inline void destroy(double __far *, double __far *) {}
  58.  
  59. inline void __far * operator new(size_t, void __far *p) { return p; }
  60.  
  61. template <class T>
  62. inline T __far * allocate(long size, T __far * p) {
  63.     set_new_handler(0);
  64.     T __far * tmp = 
  65.         (T __far *)(::operator new((unsigned long)(size * sizeof(T))));
  66.     if (tmp == 0) {
  67.     cerr << "out of memory" << endl; 
  68.     exit(1);
  69.     }
  70.     return tmp;
  71. }
  72.  
  73. template <class T>
  74. inline void deallocate(T __far * buffer) {
  75.     ::operator delete(buffer);
  76. }
  77.  
  78. template <class T1, class T2>
  79. inline void construct( T1 __far *p, const T2& value )
  80. {
  81.     new(p)T1(value);
  82. }
  83.  
  84. template <class T>
  85. inline void destroy( T __far * pointer ) {
  86.     pointer->~T();
  87. }
  88.  
  89. template <class T>
  90. class far_allocator {
  91. public:
  92.     typedef T value_type;
  93.     typedef T __far * pointer;
  94.     typedef const T __far * const_pointer;
  95.     typedef T __far & reference;
  96.     typedef const T __far & const_reference;
  97.     typedef unsigned long size_type;
  98.     typedef long difference_type;
  99.     pointer allocate(size_type n) {
  100.         return ::allocate((difference_type)n, (pointer)0);
  101.     }
  102.     void deallocate(pointer p) { ::deallocate(p); }
  103.     pointer address(reference x) { return (pointer)&x; }
  104.     const_pointer const_address(const_reference x) { 
  105.     return (const_pointer)&x; 
  106.     }
  107.     size_type init_page_size() { 
  108.     return max(size_type(1), size_type(4096/sizeof(T))); 
  109.     }
  110.     size_type max_size() const { 
  111.     return max(size_type(1), size_type(ULONG_MAX/sizeof(T))); 
  112.     }
  113. };
  114.  
  115. class far_allocator<void> {
  116. public:
  117.     typedef void __far * pointer;
  118. };
  119.  
  120. #endif
  121.