Skip to content

Commit

Permalink
Added functionality to save and load memory loads.
Browse files Browse the repository at this point in the history
  • Loading branch information
TheLat committed Jul 9, 2015
1 parent 9ea6f7c commit 2852a21
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 4 deletions.
19 changes: 17 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ I am writing a memory manager to make life easier in C++. The goal is to create
* Handles Dynamic Arrays Of Length Zero: Yes
* Dynamic Array Resizing Updates Other Pointers To The Same Dynamic Array: Yes
* Ability To Pack Tables To Smallest Possible Length: Yes
* Ability To Start Tables At Previous Maximum Load: **No**
* Ability To Start Tables At Previous Memory Load: Yes
* Threadsafe: **No**

### Use:
Expand Down Expand Up @@ -119,5 +119,20 @@ void Pack()
```
Calling pack with no index will shrink all tables to their minimum size.

#### Memory Manager Functions
```
static mm& get()
mm::get()
```
The memory manager is a singleton and access to it can only be granted by the second line.
```
void SaveTableSizes()
```
If you would like to save the current load of the memory manager, such as before calling an extremely memory-intensive function, or if you have hit the maximum load, you can save the current load of the memory manager. Currently, you can only have one save. It may or may not be beneficial to call Pack before calling this function.
```
void LoadTableSizes()
```
If you would like to load the saved memory loads, this function will grow all memory tables to the size they were when SaveTableSizes() was called. This will not shrink tables if the current table size is larger than the saved table size.

#### Benchmarking
Data to come.
Preliminary data indicates that this is between 1.2 and 2.0 times faster than normal C++ memory allocation.
31 changes: 29 additions & 2 deletions mm.h
Original file line number Diff line number Diff line change
Expand Up @@ -393,8 +393,8 @@ class mm {
int* sizes;
int* goodIndex;
int NumTables;
void GrowTable(int& index){
int newsize = sizes[index] * 2;
void GrowTable(int& index, int max = 0){
int newsize = max <= 0 ? sizes[index] * 2 : max;
if (newsize == 0) {
newsize = INITIAL_SIZE;
}
Expand Down Expand Up @@ -554,6 +554,33 @@ class mm {
Pack(i);
}
}
void SaveTableSizes() {
FILE * out;
fopen_s(&out, "MemoryTableSizes.txt", "wb");
if (!out)
return;
fwrite((void*)&NumTables, sizeof(int), 1, out);
for (int i = 0; i < NumTables; ++i) {
fwrite((void*)&sizes[i], sizeof(int), 1, out);
}
fclose(out);
}
void LoadTableSizes() {
FILE * out;
fopen_s(&out, "MemoryTableSizes.txt", "rb");
if (!out)
return;
int temp, max;
fread((void*)&max, sizeof(int), 1, out);
if (max > NumTables)
GrowTables(max);
for (int i = 0; i < max; ++i) {
fread((void*)&temp, sizeof(int), 1, out);
if (temp > sizes[i])
GrowTable(i, temp);
}
fclose(out);
}
void Pack(int index) {
if (index >= NumTables)
return;
Expand Down

0 comments on commit 2852a21

Please sign in to comment.