-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstl_alloc.h
More file actions
68 lines (56 loc) · 1.65 KB
/
Copy pathstl_alloc.h
File metadata and controls
68 lines (56 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
//
// Created by sakura on 2020/5/10.
//
#ifndef SAKURA_STL_STL_ALLOC_H
#define SAKURA_STL_STL_ALLOC_H
#include <cstddef>
#include <cstdlib>
#include <cstdio>
#include "stl_config.h"
#include "stl_construct.h"
__STL_BEGIN_NAMESPACE
template<int __inst>
class __malloc_alloc_template {
public:
static void *allocate(size_t __n) {
void *__result = malloc(__n);
if (0 == __result) {
fprintf(stderr, "out of memory\n");
exit(1);
}
return __result;
}
static void deallocate(void *__p, size_t /* __n */) {
free(__p);
}
static void *reallocate(void *__p, size_t/* old_sz */, size_t __new_sz) {
void *__result = realloc(__p, __new_sz);
if (0 == __result) {
fprintf(stderr, "out of memory\n");
exit(1);
}
return __result;
}
};
typedef __malloc_alloc_template<0> malloc_alloc;
template<class _Tp, class _Alloc>
class simple_alloc {
public:
static _Tp *allocate(size_t __n) {
return 0 == __n ? 0 : (_Tp *) _Alloc::allocate(__n * sizeof(_Tp));
}
static _Tp *allocate(void) {
return (_Tp *) _Alloc::allocate(sizeof(_Tp));
}
static void deallocate(_Tp *__p, size_t __n) {
if (0 != __n) {
_Alloc::deallocate(__p, __n * sizeof(_Tp));
}
}
static void deallocate(_Tp *__p) {
_Alloc::deallocate(__p, sizeof(_Tp));
}
};
typedef malloc_alloc alloc;
__STL_END_NAMESPACE
#endif //SAKURA_STL_STL_ALLOC_H