NIM 跨平台 C++ SDK
载入中...
搜索中...
未找到
ne_stl_memory.h
浏览该文件的文档.
1#ifndef NE_STL_INCLUDENE_STL_MEMORY_H
2#define NE_STL_INCLUDENE_STL_MEMORY_H
3
4#include "ne_stl_atomic.h"
5#include "ne_stl_build.h"
6#include "ne_stl_spinlock.h"
7namespace nstd {
8
9template <class TElement>
10using element_malloc_fun_t = TElement* (*)(std::size_t /*count*/);
11
12template <class TElement>
13using element_delete_fun_t = void (*)(TElement*&);
14
15struct memory {
16 template <class TElement>
17 static TElement* safe_malloc_container_element(std::size_t count) {
18 return new TElement[count]();
19 }
20 template <class TElement>
21 static void safe_delete_container_element(TElement*& element_ptr) {
22 if (element_ptr != nullptr) {
23 delete[] element_ptr;
24 element_ptr = nullptr;
25 }
26 }
27 template <class TObject, class... TArgs>
28 static TObject* safe_new(TArgs... args) {
29 return new TObject(args...);
30 }
31 template <class TObject>
32 static void safe_delete(TObject*& object_ptr) {
33 if (object_ptr != nullptr) {
34 delete object_ptr;
35 object_ptr = nullptr;
36 }
37 }
38 template <typename TObject, typename... TArgs>
39 static TObject* new_obj(TArgs... args) {
40 TObject* ret = (TObject*)malloc(sizeof(TObject));
41 if (ret == nullptr)
42 return nullptr;
43 new (ret) TObject(args...);
44 return ret;
45 }
46 template <typename TObject>
47 static bool delete_obj(TObject*& object) {
48 if (object == nullptr)
49 return false;
50 char* src = (char*)object;
51 object->~TObject();
52 free(src);
53 object = nullptr;
54 return true;
55 }
56};
57
58template <class _Ty>
62 template <class _Ty2, class = typename std::enable_if<std::is_convertible<_Ty2*, _Ty*>::value, void>::type>
64 void operator()(_Ty* _Ptr) const {
65 static_assert(0 < sizeof(_Ty), "can't delete an incomplete type");
66 delete _Ptr;
67 }
68};
70 big_endian = 0,
71 little_endian = 1,
72};
74 static inline EM_Byte_Endian_Order value() {
75 static atomic_bool _once_flag{false};
76 static spinlock _lock;
78 if (!_once_flag) {
79 lock_guard auto_lock(_lock);
80 if (!_once_flag) {
81 static uint16_t param = 0x0001;
82 if (*(uint8_t*)(&param) == 0x01)
84 else
86 _once_flag = true;
87 }
88 }
89 return order;
90 }
91 static inline bool same_to_host(EM_Byte_Endian_Order order) { return order == value(); }
92 static inline bool is_little_endian() { return value() == EM_Byte_Endian_Order::little_endian; }
93};
94} // namespace nstd
95
96#endif // !NE_STL_INCLUDENE_STL_MEMORY_H
Definition: ne_stl_spinlock.h:61
Definition: ne_stl_spinlock.h:44
Definition: ne_stl_atomic.h:131
Definition: ne_stl_any.h:7
void(*)(TElement *&) element_delete_fun_t
Definition: ne_stl_memory.h:13
TElement *(*)(std::size_t) element_malloc_fun_t
Definition: ne_stl_memory.h:10
EM_Byte_Endian_Order
Definition: ne_stl_memory.h:69
Definition: ne_stl_memory.h:73
static bool same_to_host(EM_Byte_Endian_Order order)
Definition: ne_stl_memory.h:91
static bool is_little_endian()
Definition: ne_stl_memory.h:92
static EM_Byte_Endian_Order value()
Definition: ne_stl_memory.h:74
Definition: ne_stl_memory.h:59
default_delete< _Ty > _Myt
Definition: ne_stl_memory.h:60
default_delete()
Definition: ne_stl_memory.h:61
void operator()(_Ty *_Ptr) const
Definition: ne_stl_memory.h:64
default_delete(const default_delete< _Ty2 > &)
Definition: ne_stl_memory.h:63
Definition: ne_stl_memory.h:15
static void safe_delete(TObject *&object_ptr)
Definition: ne_stl_memory.h:32
static TObject * new_obj(TArgs... args)
Definition: ne_stl_memory.h:39
static bool delete_obj(TObject *&object)
Definition: ne_stl_memory.h:47
static TElement * safe_malloc_container_element(std::size_t count)
Definition: ne_stl_memory.h:17
static TObject * safe_new(TArgs... args)
Definition: ne_stl_memory.h:28
static void safe_delete_container_element(TElement *&element_ptr)
Definition: ne_stl_memory.h:21