SLikeNet 0.1.3

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DS_BytePool.cpp
Go to the documentation of this file.
1 /*
2 * Copyright (c) 2014, Oculus VR, Inc.
3 * All rights reserved.
4 *
5 * This source code is licensed under the BSD-style license found in the
6 * RakNet License.txt file in the licenses directory of this source tree. An additional grant
7 * of patent rights can be found in the RakNet Patents.txt file in the same directory.
8 *
9 *
10 * Modified work: Copyright (c) 2017, SLikeSoft UG (haftungsbeschränkt)
11 *
12 * This source code was modified by SLikeSoft. Modifications are licensed under the MIT-style
13 * license found in the license.txt file in the root directory of this source tree.
14 */
15
16 #include "slikenet/DS_BytePool.h"
17 #include "slikenet/assert.h"
18 #ifndef __APPLE__
19 // Use stdlib and not malloc for compatibility
20 #include <stdlib.h>
21 #endif
22
23 using namespace DataStructures;
24
25 BytePool::BytePool()
26 {
27 pool128.SetPageSize(8192*4);
28 pool512.SetPageSize(8192*4);
29 pool2048.SetPageSize(8192*4);
30 pool8192.SetPageSize(8192*4);
31 }
32 BytePool::~BytePool()
33 {
34 }
35 void BytePool::SetPageSize(int size)
36 {
37 pool128.SetPageSize(size);
38 pool512.SetPageSize(size);
39 pool2048.SetPageSize(size);
40 pool8192.SetPageSize(size);
41 }
42 unsigned char *BytePool::Allocate(int bytesWanted, const char *file, unsigned int line)
43 {
44 #ifdef _DISABLE_BYTE_POOL
45 return rakMalloc_Ex(bytesWanted, _FILE_AND_LINE_);
46 #endif
47 unsigned char *out;
48 if (bytesWanted <= 127)
49 {
50 #ifdef _THREADSAFE_BYTE_POOL
51 mutex128.Lock();
52 #endif
53 out = (unsigned char*) pool128.Allocate(file, line);
54 #ifdef _THREADSAFE_BYTE_POOL
55 mutex128.Unlock();
56 #endif
57 out[0]=0;
58 return ((unsigned char*) out)+1;
59 }
60 if (bytesWanted <= 511)
61 {
62 #ifdef _THREADSAFE_BYTE_POOL
63 mutex512.Lock();
64 #endif
65 out = (unsigned char*) pool512.Allocate(file, line);
66 #ifdef _THREADSAFE_BYTE_POOL
67 mutex512.Unlock();
68 #endif
69 out[0]=1;
70 return ((unsigned char*) out)+1;
71 }
72 if (bytesWanted <= 2047)
73 {
74 #ifdef _THREADSAFE_BYTE_POOL
75 mutex2048.Lock();
76 #endif
77 out = (unsigned char*) pool2048.Allocate(file, line);
78 #ifdef _THREADSAFE_BYTE_POOL
79 mutex2048.Unlock();
80 #endif
81 out[0]=2;
82 return ((unsigned char*) out)+1;
83 }
84 if (bytesWanted <= 8191)
85 {
86 #ifdef _THREADSAFE_BYTE_POOL
87 mutex8192.Lock();
88 #endif
89 out = (unsigned char*) pool8192.Allocate(file, line);
90 #ifdef _THREADSAFE_BYTE_POOL
91 mutex8192.Unlock();
92 #endif
93 out[0]=3;
94 return ((unsigned char*) out)+1;
95 }
96
97 out = (unsigned char*) rakMalloc_Ex(bytesWanted+1, _FILE_AND_LINE_);
98 out[0]=(unsigned char)255;
99 return out+1;
100 }
101 void BytePool::Release(unsigned char *data, const char *file, unsigned int line)
102 {
103 #ifdef _DISABLE_BYTE_POOL
104 _rakFree_Ex(data, _FILE_AND_LINE_ );
105 #endif
106 unsigned char *realData = data-1;
107 switch (realData[0])
108 {
109 case 0:
110 #ifdef _THREADSAFE_BYTE_POOL
111 mutex128.Lock();
112 #endif
113 pool128.Release((unsigned char(*)[128]) realData, file, line );
114 #ifdef _THREADSAFE_BYTE_POOL
115 mutex128.Unlock();
116 #endif
117 break;
118 case 1:
119 #ifdef _THREADSAFE_BYTE_POOL
120 mutex512.Lock();
121 #endif
122 pool512.Release((unsigned char(*)[512]) realData, file, line );
123 #ifdef _THREADSAFE_BYTE_POOL
124 mutex512.Unlock();
125 #endif
126 break;
127 case 2:
128 #ifdef _THREADSAFE_BYTE_POOL
129 mutex2048.Lock();
130 #endif
131 pool2048.Release((unsigned char(*)[2048]) realData, file, line );
132 #ifdef _THREADSAFE_BYTE_POOL
133 mutex2048.Unlock();
134 #endif
135 break;
136 case 3:
137 #ifdef _THREADSAFE_BYTE_POOL
138 mutex8192.Lock();
139 #endif
140 pool8192.Release((unsigned char(*)[8192]) realData, file, line );
141 #ifdef _THREADSAFE_BYTE_POOL
142 mutex8192.Unlock();
143 #endif
144 break;
145 case 255:
146 rakFree_Ex(realData, file, line );
147 break;
148 default:
149 RakAssert(0);
150 break;
151 }
152 }
153 void BytePool::Clear(const char *file, unsigned int line)
154 {
155 (void) file;
156 (void) line;
157
158 #ifdef _THREADSAFE_BYTE_POOL
159 pool128.Clear(file, line);
160 pool512.Clear(file, line);
161 pool2048.Clear(file, line);
162 pool8192.Clear(file, line);
163 #endif
164 }