MemoryPool - SWTube/Darkest-Cave GitHub Wiki
- defined in header
"Memory/MemoryPool.h"
The class cave::MemoryPool
provides support for memory allocation and management in the engine.
Type | Definition |
---|---|
cave::MemoryPool |
cave::MemoryPool |
Name | Description |
---|---|
(constructor) | constructs a cave::MemoryPool (public member function) |
(destructor) | destroys the memory pool, deallocating internal data blocks (public member function) |
- Capacity
Name | Description |
---|---|
GetFreeMemorySize |
returns the available memory size (public member function) |
GetCurrentStorage |
returns the size of memory being used (public member function) |
GetMaxNumDataBlocks |
returns the maximum number of data blocks (public member function) |
GetPoolSize |
returns the size of the pool (public member function) |
- Operations
Name | Description |
---|---|
Allocate |
returns a memory of given size (public member function) |
Deallocate |
retrieves the memory of given size (public member function) |
MemoryPool(size_t maxPoolSize);
Constructs new memory pool according to the given pool size.
Parameters | Description |
---|---|
maxPoolSize |
maximum size of the pool |
Linear in smallest exponent of 2 that is larger than the pool size.
#include "Memory/MemoryPool.h"
int main()
{
{
cave::MemoryPool memoryPool(1024ul);
}
return 0;
}
constexpr size_t GetFreeMemorySize() const;
- Returns the size of free memory in pool
(none)
Size of available memories in bytes
Constant
- Samples/Main.cpp
#include "Memory/MemoryPool.h"
int main()
{
cave::MemoryPool memoryPool(1024ul);
assert(memoryPool.GetFreeMemorySize() == 1024ul);
void* oneByte = memoryPool.Allocate(1);
assert(memoryPool.GetFreeMemorySize() == 1023ul);
return 0;
}
size_t GetCurrentStorage() const;
- Returns the size of memory being used
(none)
Size of memory being used
Linear to number of data blocks
- Samples/Main.cpp
#include "Memory/MemoryPool.h"
int main()
{
cave::MemoryPool memoryPool(1024ul);
assert(memoryPool.GetFreeMemorySize() == 1024ul);
return 0;
}
bool RemoveAt(size_t index);
Removes specified characters from the string.
Removes the character at index
Parameters | Description |
---|---|
index |
index to the character to remove |
true
if removal was successful, false
if otherwise.
#include "String/String.h"
int main()
{
cave::String s = "This is an example";
std::cout << s << '\n';
s.RemoveAt(0); // Remove 'T'
std::cout << s << '\n';
return 0;
}
Output:
This is an example
his is an example
void Append(const char* s);
Appends additional characters to the string.
Appends the null-terminated character string pointed to by s
. The length of the string is determined by the first null character using strlen(s)
.
Parameters | Description |
---|---|
s |
pointer to the character string to append |
*this
Linear in size of s
#include "String/String.h
int main()
{
cave::String str = "string";
const char* cPtr = "C-string";
const char cArr[] = "Two and one";
cave::String output;
// Append a whole C-string
output.append(cPtr);
std::cout << output << '\n';
return 0;
}
Output:
C-string
String& operator+=(const String& str); // (1)
String& operator+=(const char* s); // (2)
Appends additional characters to the string.
- Appends string
str
- Appends the null-terminated character string pointed to by
s
Parameters | Description |
---|---|
str |
string to append |
s |
pointer a null-terminated character string to append |
*this
- Linear in size of
str
- Linear in size of
s
#include "String/String.h"
int main()
{
cave::String str;
std::cout << str << '\n'; // empty string
str += "This";
std::cout << str << '\n';
str += cave::String(" is a");
std::cout << str << '\n';
str += {' ','s','t','r','i','n','g','.'};
std::cout << str << '\n';
}
Output:
This
This is a
This is a string.
String operator+(const String& rhs) const;
Returns a string containing characters from itself followed by the characters from rhs
.
Parameters | Description |
---|---|
rhs |
string |
A string containing the characters from itself followed by the characters from rhs
.
#include <iostream>
#include "String/String.h"
int main()
{
cave::String s1 = "Hello";
cave::String s2 = "world";
std::cout << s1 + ' ' + s2 + "!\n";
}
Output:
Hello world!
bool operator==(const String& rhs) const;
Compares the contents the string with another string.
- Two strings are equal if both the size of itself and
rhs
are equal and each character of itself has equivalent character inrhs
at the same position.
Compares itself with another String
objects
Parameters | Description |
---|---|
rhs |
string whose contents to compare |
true
if the corresponding comparison holds, false
otherwise.
Linear in the size of the strings.
int32_t IndexOf(const char* s);
Finds the first substring equal to the given character sequence.
Finds the first substring equal to the character string pointed to by s
. The length of the string is determined by the first null character using strlen(s)
Parameters | Description |
---|---|
s |
pointer to a character string to search for |
Position of the first character of the found substring or NPOS
if no such substring is found.
#include <iostream>
#include "String/String.h"
void Print(size_t n, cave::String const &s)
{
if (n == -1)
{
std::cout << "not found\n";
}
else
{
std::cout << "found" << &s.GetCString()[n] << '\n';
}
}
int main()
{
size_t n;
cave::String const s = "This is a string";
// search from beginning of string
n = s.IndexOf("is");
Print(n, s);
}
Output:
found: is is a string
int32_t LastIndexOf(const char* s);
Finds the last substring equal to the given character sequence.
Finds the last substring equal to the character string pointed to by s
. The length of the string is determined by the first null character using strlen(s)
.
Parameters | Description |
---|---|
s |
pointer to a character string to search for |
Position of the first character of the found substring or NPOS
if no such substring is found. Note that this is an offset from the string of the string, not the end.
If searching for an empty string(str.GetLength()
is zero), NPOS
is always returned.
#include <iostream>
#include "String/String.h"
void Print(size_t n, cave::String const &s)
{
if (n == -1)
{
std::cout << "not found\n";
}
else
{
std::cout << "found: \"" << &s.GetCString()[n] << "\" at " << n << '\n';
}
}
int main()
{
cave::size_t n;
cave::String const s = "This is a string";
// search backwards from end of string
n = s.LastIndexOf("is");
Print(n, s);
}
Output:
found: "is a string" at 5
static constexpr size_t NPOS = -1ul;
This is a special value equal to the maximum value representable by the type size_t
. The exact meaning depends on context, but it is generally used either as end of string indicator by functions that expects a string index or as the error indicator by the functions that return a string index.
Although the definition uses -1
, size_t
is an unsigned integer type, and the value of NPOS
is the largest positive value it can hold, due to signed-to-unsigned implicit conversion. This is a portable way to specify the largest value of any unsigned type.
#include <iostream>
#include "String/String.h"
int main()
{
// string search functions return npos if nothing is found
cave::String s = "test";
if(s.IndexOf("a") == cave::String::NPOS)
{
std::cout << "no \"a\" in \"test\"\n";
}
}
Output:
no "a" in "test"
std::ostream& operator<<(std::ostream& os, const cave::String& str);
Behaves as a FormattedOutputFunction. After constructing and checking the sentry object, determines the output format padding as follows:
- If
str.GetLength()
is not less thanos.width()
, uses the range[0, str.GetLength())
as-is - Otherwise, if (
os.flags()
&ios_base::adjustfield
) ==ios_base::left
, placesos.width() - str.GetLength()
copies of theos.fill()
character after the character sequence. - Otherwise, places
os.width() - str.size()
copies of theos.fill()
character before the character sequence.
Then stored each character from the resulting sequence (the contents of str
plus padding) to the output stream os
as if by calling os.rdbuf()->sputn(seq, n)
, where n=std::max(os.width(), str.GetLength())
Finally, calls os.width(0)
to cancel the effects of std::setw
, if any.
Parameters | Description |
---|---|
os |
a character output stream |
is |
a character input stream |
str |
the string to be inserted |
os
#include <iostream>
#include <sstream>
#include "String/String.h"
int main()
{
cave::String greeting = "Hello, whirled!";
std::cout << greeting << '\n';
}
Output:
Hello, whirled!