OWLNext
7.0
Borland's Object Windows Library for the modern age
Toggle main menu visibility
Loading...
Searching...
No Matches
memory.cpp
Go to the documentation of this file.
1
//----------------------------------------------------------------------------
2
// Borland Services Library
3
// Copyright (c) 1993, 1996 by Borland International, All Rights Reserved
4
//
5
/// \file
6
/// Implementation of memory manipulation functions
7
//----------------------------------------------------------------------------
8
#include <
owl/pch.h
>
9
#include <
owl/defs.h
>
10
#include <
owl/private/memory.h
>
11
#include <
owl/template.h
>
12
13
#if defined(__BORLANDC__)
14
# pragma option -w-ccc
// Disable "Condition is always true/false"
15
#endif
16
17
using namespace
std;
18
19
//namespace owl {
20
21
//
22
// Make a duplicate of a 0-terminated string using new tchar[]
23
//
24
_OWLFUNC
(
char
*)
25
strnewdup
(const
char
* s,
size_t
allocSize)
26
{
27
if
(!s)
28
s =
""
;
29
size_t
alloc = std::max(strlen(s)+1, allocSize);
30
return
strcpy(
new
char
[alloc], s);
31
}
32
33
//
34
// Make a duplicate of a 0-terminated string using new tchar[]
35
//
36
37
//
38
# if defined(__CYGWIN__)
//|| defined (WINELIB), GCC 4.x already provides these functions
39
//
40
// Wide string copy function.
41
//
42
_OWLFUNC
(
wchar_t
*)
43
wcscpy
(
wchar_t
* dst,
const
wchar_t
* src)
44
{
45
wchar_t
* p = dst;
46
while
((*p++ = *src++) != 0)
47
;
48
return
dst;
49
}
50
51
//
52
// Wide string length function.
53
//
54
_OWLFUNC
(
size_t
)
55
wcslen
(
const
wchar_t
* str)
56
{
57
const
wchar_t
* p = str;
58
for
(; *p; p++)
59
;
60
return
p - str;
61
}
62
63
# endif
//if defined(__CYGWIN__)
64
65
//
66
// Make a duplicate of a wide 0-terminated string using new wchar_t[]
67
//
68
_OWLFUNC
(
wchar_t
*)
69
strnewdup
(const
wchar_t
* s,
size_t
allocSize)
70
{
71
if
(!s)
72
s = L
""
;
73
size_t
alloc = std::max(
static_cast<
size_t
>
(
::wcslen
(s))+1, allocSize);
74
return ::wcscpy(
new
wchar_t
[alloc], s);
75
}
76
77
78
/////////////////////////////////////////////////////////////////////////
79
#if defined(BI_MULTI_THREAD_RTL)
80
# include <
owl/thread.h
>
81
#endif
82
83
namespace
owl
{
84
85
//
86
// internal structure __TFixedAlloc
87
//
88
/// \cond
89
struct
__TFixedAlloc
90
#if defined(BI_MULTI_THREAD_RTL)
91
:
public
TLocalObject
92
#endif
93
{
94
__TFixedAlloc();
95
~__TFixedAlloc()
96
{
97
}
98
99
TStandardBlocks
* GetAlloc(
int
index)
100
{
101
PRECONDITION
(
this
);
102
PRECONDITION
(index < 12);
103
PRECONDITION
(index >= 0);
104
return
Alloc[index];
105
}
106
107
TStandardBlocks
Alloc16;
108
TStandardBlocks
Alloc32;
109
TStandardBlocks
Alloc64;
110
TStandardBlocks
Alloc128;
111
TStandardBlocks
Alloc256;
112
TStandardBlocks
Alloc512;
113
TStandardBlocks
Alloc1024;
114
TStandardBlocks
Alloc2048;
115
TStandardBlocks
Alloc4096;
116
TStandardBlocks
Alloc8192;
117
TStandardBlocks
Alloc16384;
118
TStandardBlocks
Alloc32768;
119
120
TStandardBlocks
* Alloc[12];
121
};
122
/// \endcond
123
//
124
__TFixedAlloc::__TFixedAlloc()
125
:
126
Alloc16(16u,6),
127
Alloc32(32u,6),
128
Alloc64(64u,6),
129
Alloc128(128u,4),
130
Alloc256(256u,4),
131
Alloc512(512u,4),
132
Alloc1024(1024u,3),
133
Alloc2048(2048u,3),
134
Alloc4096(4096u,3),
135
Alloc8192(8192u,2),
136
Alloc16384(16384u,2),
137
Alloc32768(32768u,2)
138
{
139
Alloc[0] = &Alloc16;
140
Alloc[1] = &Alloc32;
141
Alloc[2] = &Alloc64;
142
Alloc[3] = &Alloc128;
143
Alloc[4] = &Alloc256;
144
Alloc[5] = &Alloc512;
145
Alloc[6] = &Alloc1024;
146
Alloc[7] = &Alloc2048;
147
Alloc[8] = &Alloc4096;
148
Alloc[9] = &Alloc8192;
149
Alloc[10] = &Alloc16384;
150
Alloc[11] = &Alloc32768;
151
}
152
//
153
static
__TFixedAlloc& GetFixedAlloc()
154
{
155
#if defined(BI_MULTI_THREAD_RTL)
156
static
TTlsContainer<__TFixedAlloc> fixedAlloc;
157
return
fixedAlloc.Get();
158
#else
159
static
__TFixedAlloc fixedAlloc;
160
return
fixedAlloc;
161
#endif
162
};
163
164
namespace
165
{
166
//
167
// Ensure singleton initialization at start-up (single-threaded, safe).
168
//
169
__TFixedAlloc& InitFixedAlloc = GetFixedAlloc();
170
}
171
172
///////////////////////
173
//
174
// class TTmpBufferBase
175
// ~~~~~ ~~~~~~~~~~~~~~
176
//
177
TTmpBufferBase::TTmpBufferBase
(
int
size)
178
{
179
PRECONDITION
(size);
180
181
static
int
szArray[] = {
182
16u,32u,64u,128u,256u,512u,1024u,2048u,4096u,8192u,16384u,32768u
183
};
184
185
int
index;
186
for
(index = 0; index < static_cast<int>(
COUNTOF
(szArray)); index++){
187
if
(szArray[index] > size)
188
break
;
189
}
190
if
(index <
static_cast<
int
>
(
COUNTOF
(szArray))){
191
Buffer
= GetFixedAlloc().GetAlloc(index)->Allocate(szArray[index]);
192
Index
= index;
193
}
194
else
{
195
Buffer
= (
void
*)
new
uint8
[size];
196
Index
= -1;
197
}
198
}
199
//
200
TTmpBufferBase::~TTmpBufferBase
()
201
{
202
if
(
Index
>= 0)
203
GetFixedAlloc().GetAlloc(
Index
)->Free(
Buffer
);
204
else
205
delete
[] (
uint8
*)
Buffer
;
206
}
207
208
void
* TTmpBufferBase::operator
new
(
size_t
/*sz*/
)
noexcept
209
{
210
return
nullptr
;
211
}
212
213
214
}
// OWL namespace
215
//////////////////////////////
PRECONDITION
#define PRECONDITION(condition)
Definition
checks.h:227
owl::TTmpBufferBase::TTmpBufferBase
TTmpBufferBase(int size)
Definition
memory.cpp:177
owl::TTmpBufferBase::Buffer
void * Buffer
Definition
memory.h:291
owl::TTmpBufferBase::Index
int Index
Definition
memory.h:292
owl::TTmpBufferBase::~TTmpBufferBase
~TTmpBufferBase()
Definition
memory.cpp:200
wcslen
size_t __stdcall wcslen(const wchar_t *str)
wcscpy
wchar_t *__stdcall wcscpy(wchar_t *dst, const wchar_t *src)
pch.h
memory.h
Reliable platform independent header for common memory and string functions.
strnewdup
char * strnewdup(const char *s, size_t minAllocSize=0)
Definition
memory.cpp:25
owl
Object Windows Library (OWLNext Core).
Definition
animctrl.h:22
owl::TStandardBlocks
TMMemBlocks< TStandardAllocator > TStandardBlocks
Definition
template.h:526
owl::uint8
unsigned char uint8
Definition
number.h:32
defs.h
General definitions used by all ObjectWindows programs.
_OWLFUNC
#define _OWLFUNC(p)
Definition
defs.h:341
COUNTOF
#define COUNTOF(s)
Array element count Important: Only use this with an argument of array type.
Definition
defs.h:376
template.h
Definition of container classes used and made available by OWL.
thread.h
source
owlcore
memory.cpp
Generated by
1.18.0