Last active
December 17, 2015 06:18
-
-
Save triffid/5563987 to your computer and use it in GitHub Desktop.
memorypool test program
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // g++ -g -DMEMDEBUG -o test test.cpp MemoryPool.cpp && ./test | |
| #include <stdio.h> | |
| #include <stdlib.h> | |
| #include "MemoryPool.h" | |
| class T1 { | |
| public: | |
| T1(){ | |
| v1 = 3; | |
| v2 = 4; | |
| }; | |
| ~T1() { | |
| printf("T1 %p destroyed: ", this); | |
| this->pr(); | |
| } | |
| void pr() { | |
| printf("%d %d\n", v1, v2); | |
| }; | |
| void a() { | |
| v1 += v2; | |
| v2 += 1; | |
| }; | |
| uint32_t v1; | |
| uint32_t v2; | |
| }; | |
| class T2 { | |
| public: | |
| T2(){}; | |
| ~T2(){ | |
| printf("T2 %p destroyed\n", this); | |
| } | |
| uint32_t v[4]; | |
| }; | |
| int main(int argc, char** argv) | |
| { | |
| void* _pool1 = malloc(16384); | |
| MemoryPool* pool1 = new MemoryPool(_pool1, 16384); | |
| printf("pool1: %p, %p\n", _pool1, pool1); | |
| T1* a1 = new(*pool1) T1(); | |
| printf("a1: %p (%d): %+d\n", a1, sizeof(T1), ((uint8_t*) a1) - ((uint8_t*) _pool1)); | |
| T1* a2 = new(*pool1) T1(); | |
| printf("a2: %p (%d): %+d\n", a2, sizeof(T1), ((uint8_t*) a2) - ((uint8_t*) _pool1)); | |
| T2* a3 = new(*pool1) T2(); | |
| printf("a3: %p (%d): %+d\n", a3, sizeof(T2), ((uint8_t*) a3) - ((uint8_t*) _pool1)); | |
| a1->pr(); a2->pr(); | |
| a1->a(); a2->a(); | |
| a1->pr(); a2->pr(); | |
| delete a1; | |
| delete a2; | |
| T2* a4 = new(*pool1) T2(); | |
| printf("a4: %p (%d): %+d\n", a4, sizeof(T2), ((uint8_t*) a4) - ((uint8_t*) _pool1)); | |
| T1* a5 = new(*pool1) T1(); | |
| printf("a5: %p (%d): %+d\n", a5, sizeof(T1), ((uint8_t*) a5) - ((uint8_t*) _pool1)); | |
| pool1->debug(); | |
| delete a5; | |
| pool1->debug(); | |
| delete a4; | |
| pool1->debug(); | |
| delete a3; | |
| pool1->debug(); | |
| delete pool1; | |
| delete _pool1; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment