1
- /* **************************************************************************************
2
- * file : shared_allocator.cpp
3
- * data : 2016/03/05
4
- * author : Victor Zarubkin
5
- * contact : v.s.zarubkin@gmail.com
6
- * copyright : Copyright (C) 2016 Victor Zarubkin
7
- * :
8
- * description : This header contains implementation of shared_allocate,
9
- * : shared_deallocate and shared_size functions.
10
- * :
11
- * references : Original (and actual) version of source code can be found
12
- * : here <http://www.github.com/cas4ey/shared_allocator>.
13
- * :
14
- * license : This file is part of SharedAllocator.
15
- * :
16
- * : The MIT License (MIT)
17
- * : Permission is hereby granted, free of charge, to any person
18
- * : obtaining a copy of this software and associated documentation
19
- * : files (the "Software"), to deal in the Software without
20
- * : restriction, including without limitation the rights to use,
21
- * : copy, modify, merge, publish, distribute, sublicense, and/or sell
22
- * : copies of the Software, and to permit persons to whom the Software
23
- * : is furnished to do so, subject to the following conditions:
24
- * :
25
- * : The above copyright notice and this permission notice shall be
26
- * : included in all copies or substantial portions of the Software.
27
- * :
28
- * : THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29
- * : EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
30
- * : OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
31
- * : IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
32
- * : ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
33
- * : TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
34
- * : OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35
- ****************************************************************************************/
36
-
37
- #include < xstddef>
38
- #include < type_traits>
39
- #include < malloc.h>
40
-
41
- // ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
42
- // ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
43
-
44
- #if defined(WIN32) || defined(_WIN32) || defined(_WIN64)
45
-
46
- # define SHARED_ALLOCATOR_DLLEXPORT __declspec (dllexport)
47
-
48
- #else
49
-
50
- # define SHARED_ALLOCATOR_DLLEXPORT
51
-
52
- #endif // defined(WIN32) || defined(_WIN32) || defined(_WIN64)
53
-
54
- extern " C"
55
- {
56
- /* * \brief Allocate new memory.
57
-
58
- \param _bytesNumber Required bytes number including already preallocated size of _currentMemory (if exist).
59
- \param _currentMemory Pointer to existing memory buffer (if exist). If it's size is greater than _bytesNumber then no new memory would be allocated. */
60
- SHARED_ALLOCATOR_DLLEXPORT void * shared_allocate (size_t _bytesNumber, void * _currentMemory = nullptr )
61
- {
1
+ /* **************************************************************************************
2
+ * file : shared_allocator.cpp
3
+ * data : 2016/03/05
4
+ * author : Victor Zarubkin
5
+ * contact : v.s.zarubkin@gmail.com
6
+ * copyright : Copyright (C) 2016 Victor Zarubkin
7
+ * :
8
+ * description : This header contains implementation of shared_allocate,
9
+ * : shared_deallocate and shared_size functions.
10
+ * :
11
+ * references : Original (and actual) version of source code can be found
12
+ * : here <http://www.github.com/cas4ey/shared_allocator>.
13
+ * :
14
+ * license : This file is part of SharedAllocator.
15
+ * :
16
+ * : The MIT License (MIT)
17
+ * : Permission is hereby granted, free of charge, to any person
18
+ * : obtaining a copy of this software and associated documentation
19
+ * : files (the "Software"), to deal in the Software without
20
+ * : restriction, including without limitation the rights to use,
21
+ * : copy, modify, merge, publish, distribute, sublicense, and/or sell
22
+ * : copies of the Software, and to permit persons to whom the Software
23
+ * : is furnished to do so, subject to the following conditions:
24
+ * :
25
+ * : The above copyright notice and this permission notice shall be
26
+ * : included in all copies or substantial portions of the Software.
27
+ * :
28
+ * : THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29
+ * : EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
30
+ * : OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
31
+ * : IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
32
+ * : ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
33
+ * : TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
34
+ * : OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35
+ ****************************************************************************************/
36
+
37
+ #include < type_traits>
38
+ #include < malloc.h>
39
+
40
+ // ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
41
+ // ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
42
+
43
+ #if defined(WIN32) || defined(_WIN32) || defined(_WIN64)
44
+
45
+ # define SHARED_ALLOCATOR_DLLEXPORT __declspec (dllexport)
46
+
47
+ #else
48
+
49
+ # define SHARED_ALLOCATOR_DLLEXPORT
50
+
51
+ #endif // defined(WIN32) || defined(_WIN32) || defined(_WIN64)
52
+
53
+ extern " C"
54
+ {
55
+ /* * \brief Allocate new memory.
56
+
57
+ \param _bytesNumber Required bytes number including already preallocated size of _currentMemory (if exist).
58
+ \param _currentMemory Pointer to existing memory buffer (if exist). If it's size is greater than _bytesNumber then no new memory would be allocated. */
59
+ SHARED_ALLOCATOR_DLLEXPORT void * shared_allocate (size_t _bytesNumber, void * _currentMemory = nullptr )
60
+ {
62
61
if (_currentMemory == nullptr )
63
62
{
64
63
size_t * newMemory = reinterpret_cast <size_t *>(malloc (_bytesNumber + sizeof (size_t )));
@@ -74,39 +73,39 @@ extern "C"
74
73
75
74
size_t * newMemory = reinterpret_cast <size_t *>(realloc (pMemory, _bytesNumber + sizeof (size_t )));
76
75
*newMemory = _bytesNumber;
77
- return static_cast <void *>(newMemory + 1 );
78
- }
79
-
80
- /* * \brief Deallocate memory.
81
-
82
- \param _currentMemory Pointer to existing memory buffer to be deallocated.
83
-
84
- \warning Pointer to _currentMemory after this operation will be invalid. */
85
- SHARED_ALLOCATOR_DLLEXPORT void shared_deallocate (void * _currentMemory)
86
- {
76
+ return static_cast <void *>(newMemory + 1 );
77
+ }
78
+
79
+ /* * \brief Deallocate memory.
80
+
81
+ \param _currentMemory Pointer to existing memory buffer to be deallocated.
82
+
83
+ \warning Pointer to _currentMemory after this operation will be invalid. */
84
+ SHARED_ALLOCATOR_DLLEXPORT void shared_deallocate (void * _currentMemory)
85
+ {
87
86
if (_currentMemory != nullptr )
88
87
{
89
88
size_t * pMemory = reinterpret_cast <size_t *>(_currentMemory) - 1 ;
90
- free (pMemory);
91
- }
92
- }
93
-
94
- /* * \brief Returns memory size in bytes.
95
-
96
- \param _currentMemory Pointer to existing memory buffer. */
97
- SHARED_ALLOCATOR_DLLEXPORT size_t shared_size (const void * _currentMemory)
98
- {
89
+ free (pMemory);
90
+ }
91
+ }
92
+
93
+ /* * \brief Returns memory size in bytes.
94
+
95
+ \param _currentMemory Pointer to existing memory buffer. */
96
+ SHARED_ALLOCATOR_DLLEXPORT size_t shared_size (const void * _currentMemory)
97
+ {
99
98
if (_currentMemory != nullptr )
100
99
{
101
- return *(reinterpret_cast <const size_t *>(_currentMemory) - 1 );
102
- }
103
-
104
- return 0 ;
105
- }
106
-
107
- }
108
-
109
- #undef SHARED_ALLOCATOR_DLLEXPORT
110
-
111
- // ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
112
- // ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
100
+ return *(reinterpret_cast <const size_t *>(_currentMemory) - 1 );
101
+ }
102
+
103
+ return 0 ;
104
+ }
105
+
106
+ }
107
+
108
+ #undef SHARED_ALLOCATOR_DLLEXPORT
109
+
110
+ // ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
111
+ // ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
0 commit comments