Skip to content

Commit 7062216

Browse files
committed
init repository
1 parent bc97e9d commit 7062216

File tree

3 files changed

+132
-169
lines changed

3 files changed

+132
-169
lines changed

2_STL_allocator/2_2_1_defalloc.h

Lines changed: 35 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*
1+
/*
22
*
33
* Copyright (c) 1994
44
* Hewlett-Packard Company
@@ -15,13 +15,12 @@
1515

1616
// Inclusion of this file is DEPRECATED. This is the original HP
1717
// default allocator. It is provided only for backward compatibility.
18-
// This file WILL BE REMOVED in a future release.
1918
//
2019
// 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>.
20+
// that requires an allocator with the HP-style interface. SGI STL
21+
// uses a different allocator interface. SGI-style allocators are not
22+
// parametrized with respect to the object type; they traffic in void *
23+
// pointers. This file is not included by any other SGI STL header.
2524

2625
#ifndef DEFALLOC_H
2726
#define DEFALLOC_H
@@ -33,57 +32,61 @@
3332
#include <iostream.h>
3433
#include <algobase.h>
3534

36-
3735
template <class T>
38-
inline T* allocate(ptrdiff_t size, T*) {
36+
inline T *allocate(ptrdiff_t size, T *)
37+
{
3938
set_new_handler(0);
40-
T* tmp = (T*)(::operator new((size_t)(size * sizeof(T))));
41-
if (tmp == 0) {
42-
cerr << "out of memory" << endl;
43-
exit(1);
39+
T *tmp = (T *)(::operator new((size_t)(size * sizeof(T))));
40+
if (tmp == 0)
41+
{
42+
cerr << "out of memory" << endl;
43+
exit(1);
4444
}
4545
return tmp;
4646
}
4747

48-
4948
template <class T>
50-
inline void deallocate(T* buffer) {
49+
inline void deallocate(T *buffer)
50+
{
5151
::operator delete(buffer);
5252
}
5353

5454
template <class T>
55-
class allocator {
55+
class allocator
56+
{
5657
public:
5758
typedef T value_type;
58-
typedef T* pointer;
59-
typedef const T* const_pointer;
60-
typedef T& reference;
61-
typedef const T& const_reference;
59+
typedef T *pointer;
60+
typedef const T *const_pointer;
61+
typedef T &reference;
62+
typedef const T &const_reference;
6263
typedef size_t size_type;
6364
typedef ptrdiff_t difference_type;
64-
pointer allocate(size_type n) {
65-
return ::allocate((difference_type)n, (pointer)0);
65+
pointer allocate(size_type n)
66+
{
67+
return ::allocate((difference_type)n, (pointer)0);
6668
}
6769
void deallocate(pointer p) { ::deallocate(p); }
6870
pointer address(reference x) { return (pointer)&x; }
69-
const_pointer const_address(const_reference x) {
70-
return (const_pointer)&x;
71+
const_pointer const_address(const_reference x)
72+
{
73+
return (const_pointer)&x;
7174
}
72-
size_type init_page_size() {
73-
return max(size_type(1), size_type(4096/sizeof(T)));
75+
size_type init_page_size()
76+
{
77+
return max(size_type(1), size_type(4096 / sizeof(T)));
7478
}
75-
size_type max_size() const {
76-
return max(size_type(1), size_type(UINT_MAX/sizeof(T)));
79+
size_type max_size() const
80+
{
81+
return max(size_type(1), size_type(UINT_MAX / sizeof(T)));
7782
}
7883
};
7984

80-
8185
// 特化版本(Specialization),为什么无需加上template<>
82-
class allocator<void> {
86+
class allocator<void>
87+
{
8388
public:
84-
typedef void* pointer;
89+
typedef void *pointer;
8590
};
8691

87-
88-
8992
#endif
Lines changed: 92 additions & 135 deletions
Original file line numberDiff line numberDiff line change
@@ -1,135 +1,92 @@
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-
* Copyright (c) 1996,1997
16-
* Silicon Graphics Computer Systems, Inc.
17-
*
18-
* Permission to use, copy, modify, distribute and sell this software
19-
* and its documentation for any purpose is hereby granted without fee,
20-
* provided that the above copyright notice appear in all copies and
21-
* that both that copyright notice and this permission notice appear
22-
* in supporting documentation. Silicon Graphics makes no
23-
* representations about the suitability of this software for any
24-
* purpose. It is provided "as is" without express or implied warranty.
25-
*/
26-
27-
/* NOTE: This is an internal header file, included by other STL headers.
28-
* You should not attempt to use it directly.
29-
*/
30-
31-
#ifndef __SGI_STL_INTERNAL_CONSTRUCT_H
32-
#define __SGI_STL_INTERNAL_CONSTRUCT_H
33-
34-
#include <new.h> // 使用placement new
35-
36-
__STL_BEGIN_NAMESPACE
37-
38-
// construct and destroy. These functions are not part of the C++ standard,
39-
// and are provided for backward compatibility with the HP STL. We also
40-
// provide internal names _Construct and _Destroy that can be used within
41-
// the library, so that standard-conforming pieces don't have to rely on
42-
// non-standard extensions.
43-
44-
// Internal names
45-
46-
template <class _T1, class _T2>
47-
inline void _Construct(_T1* __p, const _T2& __value) {
48-
new ((void*) __p) _T1(__value); // placement new; 唤起T1::T1(value)
49-
}
50-
51-
template <class _T1>
52-
inline void _Construct(_T1* __p) {
53-
new ((void*) __p) _T1();
54-
}
55-
56-
57-
// 第一个版本,接受一个参数
58-
template <class _Tp>
59-
inline void _Destroy(_Tp* __pointer) {
60-
__pointer->~_Tp(); // 调用dtor ~T()
61-
}
62-
63-
64-
65-
// 如果元素value type 有 non-trivial destructor
66-
template <class _ForwardIterator>
67-
void
68-
__destroy_aux(_ForwardIterator __first, _ForwardIterator __last, __false_type)
69-
{
70-
for ( ; __first != __last; ++__first)
71-
destroy(&*__first);
72-
}
73-
74-
// 如果元素value type 有 trivial destructor
75-
template <class _ForwardIterator>
76-
inline void __destroy_aux(_ForwardIterator, _ForwardIterator, __true_type) {}
77-
78-
79-
// 判断元素value type是否有trivial destructor
80-
template <class _ForwardIterator, class _Tp>
81-
inline void
82-
__destroy(_ForwardIterator __first, _ForwardIterator __last, _Tp*)
83-
{
84-
typedef typename __type_traits<_Tp>::has_trivial_destructor
85-
_Trivial_destructor;
86-
__destroy_aux(__first, __last, _Trivial_destructor());
87-
}
88-
89-
90-
// 第二个版本,接受两个迭代器,函数设法找出元素类别,进而利用 __type_traits<>使用最佳措施
91-
template <class _ForwardIterator>
92-
inline void _Destroy(_ForwardIterator __first, _ForwardIterator __last) {
93-
__destroy(__first, __last, __VALUE_TYPE(__first));
94-
}
95-
96-
// destroy()特化版
97-
inline void _Destroy(char*, char*) {}
98-
inline void _Destroy(int*, int*) {}
99-
inline void _Destroy(long*, long*) {}
100-
inline void _Destroy(float*, float*) {}
101-
inline void _Destroy(double*, double*) {}
102-
#ifdef __STL_HAS_WCHAR_T
103-
inline void _Destroy(wchar_t*, wchar_t*) {}
104-
#endif /* __STL_HAS_WCHAR_T */
105-
106-
// --------------------------------------------------
107-
// Old names from the HP STL.
108-
109-
template <class _T1, class _T2>
110-
inline void construct(_T1* __p, const _T2& __value) {
111-
_Construct(__p, __value);
112-
}
113-
114-
template <class _T1>
115-
inline void construct(_T1* __p) {
116-
_Construct(__p);
117-
}
118-
119-
template <class _Tp>
120-
inline void destroy(_Tp* __pointer) {
121-
_Destroy(__pointer);
122-
}
123-
124-
template <class _ForwardIterator>
125-
inline void destroy(_ForwardIterator __first, _ForwardIterator __last) {
126-
_Destroy(__first, __last);
127-
}
128-
129-
__STL_END_NAMESPACE
130-
131-
#endif /* __SGI_STL_INTERNAL_CONSTRUCT_H */
132-
133-
// Local Variables:
134-
// mode:C++
135-
// End:
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+
* Copyright (c) 1996,1997
16+
* Silicon Graphics Computer Systems, Inc.
17+
*
18+
* Permission to use, copy, modify, distribute and sell this software
19+
* and its documentation for any purpose is hereby granted without fee,
20+
* provided that the above copyright notice appear in all copies and
21+
* that both that copyright notice and this permission notice appear
22+
* in supporting documentation. Silicon Graphics makes no
23+
* representations about the suitability of this software for any
24+
* purpose. It is provided "as is" without express or implied warranty.
25+
*/
26+
27+
/* NOTE: This is an internal header file, included by other STL headers.
28+
* You should not attempt to use it directly.
29+
*/
30+
31+
#ifndef __SGI_STL_INTERNAL_CONSTRUCT_H
32+
#define __SGI_STL_INTERNAL_CONSTRUCT_H
33+
34+
#include <new.h>
35+
36+
__STL_BEGIN_NAMESPACE
37+
38+
template <class T1, class T2>
39+
inline void construct(T1 *p, const T2 &value)
40+
{
41+
// placement new,调用 T1::T1(value)
42+
new (p) T1(value);
43+
}
44+
45+
// destory 第一个版本,接受一个指针
46+
template <class T>
47+
inline void destroy(T *pointer)
48+
{
49+
pointer->~T();
50+
}
51+
52+
// dsetory 第二版本,接收两个迭代器。此函数设法找出元素的数值型别
53+
// 进而利用 __type_traits<> 求取最适当措施
54+
template <class ForwardIterator>
55+
inline void destroy(ForwardIterator first, ForwardIterator last)
56+
{
57+
__destroy(first, last, value_type(first));
58+
}
59+
60+
// 判断元素的数值型别(value type) 是否有 trivial destructor
61+
template <class ForwardIterator, class T>
62+
inline void __destroy(ForwardIterator first, ForwardIterator last, T *)
63+
{
64+
typedef typename __type_traits<T>::has_trivial_destructor trivial_destructor;
65+
__destroy_aux(first, last, trivial_destructor());
66+
}
67+
68+
69+
// 如果元素的数值型别(value type) 有 non-trivial destructor
70+
template <class ForwardIterator>
71+
inline void
72+
__destroy_aux(ForwardIterator first, ForwardIterator last, __false_type)
73+
{
74+
for (; first < last; ++first)
75+
destroy(&*first);
76+
}
77+
78+
// 如果元素的数值型别(value type) 有 trivial destructor
79+
template <class ForwardIterator>
80+
inline void __destroy_aux(ForwardIterator, ForwardIterator, __true_type) {}
81+
82+
// 以下是 destroy 第二版针对迭代器为 char* 和 wchar_t* 的特化版
83+
inline void destroy(char *, char *) {}
84+
inline void destroy(wchar_t *, wchar_t *) {}
85+
86+
__STL_END_NAMESPACE
87+
88+
#endif /* __SGI_STL_INTERNAL_CONSTRUCT_H */
89+
90+
// Local Variables:
91+
// mode:C++
92+
// End:

2_STL_allocator/README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
# 空间配置器(allocator)
22

3+
## 2.1 空间配置器标准接口
34
`2_1_1jjalloc.h`:实现了一个简单的空间配置器,实现了书上 p46 页的接口,实际上内部分配内存时调用了全局的 operator new 函数,构造对象时调用了 placement new 函数
45

56
`2_1_1jjalloc.cpp`:测试`2_1_1jjalloc.h`中的空间配置器
67

7-
`2_1_1_defalloc.h`:HP的 allocator 实现,SGI 中的空间配置器默认使用 alloc,并不使用这个 allocator,这里提供仅仅是为了回溯兼容
8+
[allocate函数中set_new_handle函数理解](https://blog.csdn.net/qq_14982047/article/details/50732568)
89

9-
[allocate函数中set_new_handle函数理解](https://blog.csdn.net/qq_14982047/article/details/50732568)
10+
## 2.2 具备次配置力的SGI空间配置器
11+
12+
`2_2_1_defalloc.h`:HP的 allocator 实现,SGI 中的空间配置器默认使用 alloc,并不使用这个 allocator,这里提供仅仅是为了回溯兼容

0 commit comments

Comments
 (0)