patterncppMinor
Memory Management Code
Viewed 0 times
managementcodememory
Problem
For a project I had to do a memory management problem without creating any custom classes or templates using the provided
I implemented all functions and created two extra,
```
const int poolSize = 65536;
char pool[poolSize];
int SizeConversion(int);
int GetSize(int, int);
// Initialize set up any data needed to manage the memory pool
void initializeMemoryManager(void)
{
//Verify that the pool is preset to null
for (int x = 0; x = (256 * 256)) {
onIllegalOperation("Allocation Size too Large");
return nullptr;
}
else {
int first = 0;
int cur = 0;
//Find the first location the data can be stored
for (int x = 0; x = aSize + 2) {
break;
}
}
//Define the allocated data for display purposes to 'A'
for (int x = first + 2; x i) {
((char)aPointer + i) = '\0';
i++;
}
//Deallocated Allocation size parameters
((char)aPointer - 1) = '\0';
((char)aPointer - 2) = '\0';
}
}
// Will scan the memory pool and return the total free space remaining
int freeRemaining(void)
{
int remaining = 0;
for (int x = 0; x largest)
largest = cur;
}
else {
cur = 0;
//Get the integer value of the two size bytes
int sr = (int)pool[x];
int sl = (int)pool[x + 1];
//Jump from current index to end of allocation then continue
x = x + 1 + GetSize(sl, sr);
}
}
return largest;
}
//Scans the memory pool and return the smallest free space remaining
int smallestFree(void)
{
int smallest = poolSize;
int cur = 0;
for (int x = 0; x < poolSize; x++) {
if (pool[x] == '\0') {
cur++;
if (cur
pool array and poolSize. I implemented all functions and created two extra,
SizeConversion and GetSize. My main question is this: is there a better way to implement this and is the code that I have clean and efficient?```
const int poolSize = 65536;
char pool[poolSize];
int SizeConversion(int);
int GetSize(int, int);
// Initialize set up any data needed to manage the memory pool
void initializeMemoryManager(void)
{
//Verify that the pool is preset to null
for (int x = 0; x = (256 * 256)) {
onIllegalOperation("Allocation Size too Large");
return nullptr;
}
else {
int first = 0;
int cur = 0;
//Find the first location the data can be stored
for (int x = 0; x = aSize + 2) {
break;
}
}
//Define the allocated data for display purposes to 'A'
for (int x = first + 2; x i) {
((char)aPointer + i) = '\0';
i++;
}
//Deallocated Allocation size parameters
((char)aPointer - 1) = '\0';
((char)aPointer - 2) = '\0';
}
}
// Will scan the memory pool and return the total free space remaining
int freeRemaining(void)
{
int remaining = 0;
for (int x = 0; x largest)
largest = cur;
}
else {
cur = 0;
//Get the integer value of the two size bytes
int sr = (int)pool[x];
int sl = (int)pool[x + 1];
//Jump from current index to end of allocation then continue
x = x + 1 + GetSize(sl, sr);
}
}
return largest;
}
//Scans the memory pool and return the smallest free space remaining
int smallestFree(void)
{
int smallest = poolSize;
int cur = 0;
for (int x = 0; x < poolSize; x++) {
if (pool[x] == '\0') {
cur++;
if (cur
Solution
You have a pool:
The problem with allocating pools like this (via char array). Is that the language gives you no guarantees about the alignment of
The maximum size of the pool is
Note: We need this prefix size even when the block is allocated. Also this means there is a two byte additional overhead when allocating blocks.
Also each free block has to know the next block. Again the next free block has a number of in the range
Note: This data is only used when the object has been freed. So it can be used by the application as part of the data space. But it means that that the minimum block size that will be allocated is 2 (so 0/1 sized blocks will not be optimal).
const int poolSize = 65536;
char pool[poolSize];The problem with allocating pools like this (via char array). Is that the language gives you no guarantees about the alignment of
pool. If on the other hand you used a std::vector pool(poolSize); then the language guarantees that the &pool[0] is aligned for all types of size poolSize or smaller (see requirements for dynamically allocated memory).The maximum size of the pool is
65536 so this can be represented by 2 bytes. So every block will keep a two byte prefix of its length so that when we de-allocate it we know how many bytes have been added back into the system.Note: We need this prefix size even when the block is allocated. Also this means there is a two byte additional overhead when allocating blocks.
Also each free block has to know the next block. Again the next free block has a number of in the range
[0-65535] so it can be represented as two bytes.Note: This data is only used when the object has been freed. So it can be used by the application as part of the data space. But it means that that the minimum block size that will be allocated is 2 (so 0/1 sized blocks will not be optimal).
Code Snippets
const int poolSize = 65536;
char pool[poolSize];Context
StackExchange Code Review Q#121911, answer score: 3
Revisions (0)
No revisions yet.