OWLNext    7.0
Borland's Object Windows Library for the modern age
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
17using namespace std;
18
19//namespace owl {
20
21//
22// Make a duplicate of a 0-terminated string using new tchar[]
23//
24_OWLFUNC(char*)
25strnewdup(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*)
43wcscpy(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)
55wcslen(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*)
69strnewdup(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
83namespace owl {
84
85//
86// internal structure __TFixedAlloc
87//
88/// \cond
89struct __TFixedAlloc
91: public TLocalObject
92#endif
93{
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
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//
153static __TFixedAlloc& GetFixedAlloc()
154{
155#if defined(BI_MULTI_THREAD_RTL)
157 return fixedAlloc.Get();
158#else
160 return fixedAlloc;
161#endif
162};
163
164namespace
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//
177TTmpBufferBase::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//
200TTmpBufferBase::~TTmpBufferBase()
201{
202 if(Index >= 0)
203 GetFixedAlloc().GetAlloc(Index)->Free(Buffer);
204 else
205 delete[] (uint8*)Buffer;
206}
207
208void* TTmpBufferBase::operator new(size_t /*sz*/) noexcept
209{
210 return nullptr;
211}
212
213
214} // OWL namespace
215//////////////////////////////
#define PRECONDITION(condition)
Definition checks.h:227
size_t __stdcall wcslen(const wchar_t *str)
wchar_t *__stdcall wcscpy(wchar_t *dst, const wchar_t *src)
char * strnewdup(const char *s, size_t allocSize)
Definition memory.cpp:25
Reliable platform independent header for common memory and string functions.
Object Windows Library (OWLNext Core)
Definition animctrl.h:22
TMMemBlocks< TStandardAllocator > TStandardBlocks
Definition template.h:484
General definitions used by all ObjectWindows programs.
#define _OWLFUNC(p)
Definition defs.h:341
#define COUNTOF(s)
Array element count Important: Only use this with an argument of array type.
Definition defs.h:376
Definition of container classes used and made available by OWL.