xref: /haiku/headers/cpp/defalloc.h (revision 820dca4df6c7bf955c46e8f6521b9408f50b2900)
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 // Inclusion of this file is DEPRECATED.  This is the original HP
17 // default allocator.  It is provided only for backward compatibility.
18 // This file WILL BE REMOVED in a future release.
19 //
20 // DO NOT USE THIS FILE unless you have an old container implementation
21 // that requires an allocator with the HP-style interface.
22 //
23 // Standard-conforming allocators have a very different interface.  The
24 // standard default allocator is declared in the header <memory>.
25 
26 #ifndef DEFALLOC_H
27 #define DEFALLOC_H
28 
29 #include <new.h>
30 #include <stddef.h>
31 #include <stdlib.h>
32 #include <limits.h>
33 #if (defined(__BEOS__) || defined(__HAIKU__))
34 # include <stdio.h>
35 #else
36 #include <iostream.h>
37 #endif
38 #include <algobase.h>
39 
40 
41 template <class T>
42 inline T* allocate(ptrdiff_t size, T*) {
43     set_new_handler(0);
44     T* tmp = (T*)(::operator new((size_t)(size * sizeof(T))));
45     if (tmp == 0) {
46 #if (defined(__BEOS__) || defined(__HAIKU__))
47 	fprintf(stderr, "out of memory\n");
48 #else
49 	cerr << "out of memory" << endl;
50 #endif
51 	exit(1);
52     }
53     return tmp;
54 }
55 
56 
57 template <class T>
58 inline void deallocate(T* buffer) {
59     ::operator delete(buffer);
60 }
61 
62 template <class T>
63 class allocator {
64 public:
65     typedef T value_type;
66     typedef T* pointer;
67     typedef const T* const_pointer;
68     typedef T& reference;
69     typedef const T& const_reference;
70     typedef size_t size_type;
71     typedef ptrdiff_t difference_type;
72     pointer allocate(size_type n) {
73 	return ::allocate((difference_type)n, (pointer)0);
74     }
75     void deallocate(pointer p) { ::deallocate(p); }
76     pointer address(reference x) { return (pointer)&x; }
77     const_pointer const_address(const_reference x) {
78 	return (const_pointer)&x;
79     }
80     size_type init_page_size() {
81 	return max(size_type(1), size_type(4096/sizeof(T)));
82     }
83     size_type max_size() const {
84 	return max(size_type(1), size_type(UINT_MAX/sizeof(T)));
85     }
86 };
87 
88 class allocator<void> {
89 public:
90     typedef void* pointer;
91 };
92 
93 
94 
95 #endif
96