Skip to content

Commit

Permalink
Code clean
Browse files Browse the repository at this point in the history
  • Loading branch information
jonforums committed Nov 27, 2011
1 parent 539967d commit 8da03fa
Showing 1 changed file with 26 additions and 25 deletions.
51 changes: 26 additions & 25 deletions samples/mem_alloc.cpp
@@ -1,14 +1,13 @@
/* Copyright (c) 2011, Jon Maken
* License: 3-clause BSD
* Revision: 11/26/2011 12:00:08 PM
* Revision: 11/27/2011 6:36:22 PM
*
* build recipes (Windows):
* cl /nologo /O2 /EHsc mem_alloc.cpp
* g++ -Wall -O3 -s -o mem_alloc.exe mem_alloc.cpp
*/

// TODO
// * throw runtime exceptions on memory allocation failures
// * change to DLL to be called by runner exe

#include <ios>
Expand All @@ -27,6 +26,8 @@ const int BUF_SIZE = 32767;

int main(int argc, char* argv[])
{
using namespace std;

wchar_t* buf = NULL;
double rv = 0.0;

Expand All @@ -37,55 +38,55 @@ int main(int argc, char* argv[])
t_us.start();
buf = static_cast<wchar_t*>(malloc(BUF_SIZE));
rv = t_us.stop();
if (buf == NULL) return(-1);
std::cout << std::left
<< std::setw(18)
<< "malloc() time: "
<< rv << std::endl;
if (buf == NULL) throw runtime_error("[ERROR] malloc() failed.");
cout << left
<< setw(18)
<< "malloc() time: "
<< rv << endl;

// CRT free() - dependent upon previous malloc()
t_us.start();
free(static_cast<void*>(buf));
rv = t_us.stop();
buf = NULL;
std::cout << std::left
<< std::setw(18)
<< "free() time: "
<< rv << std::endl;
cout << left
<< setw(18)
<< "free() time: "
<< rv << endl;

// CRT calloc()
t_us.start();
buf = static_cast<wchar_t*>(calloc(BUF_SIZE, sizeof(char)));
rv = t_us.stop();
std::cout << std::left
<< std::setw(18)
<< "calloc() time: "
<< rv << std::endl;
cout << left
<< setw(18)
<< "calloc() time: "
<< rv << endl;
free(static_cast<void*>(buf));
buf = NULL;

#if defined(_WIN32)
// Win32 HeapAlloc()
HANDLE heap = ::HeapCreate(0, 2*BUF_SIZE, 4*BUF_SIZE);
if (heap == NULL) return(-1);
if (heap == NULL) throw runtime_error("[ERROR] HeapCreate() failed.");
t_us.start();
buf = static_cast<wchar_t*>(::HeapAlloc(heap, 0, BUF_SIZE));
rv = t_us.stop();
if (buf == NULL) return(-1);
std::cout << std::left
<< std::setw(18)
<< "HeapAlloc() time: "
<< rv << std::endl;
if (buf == NULL) throw runtime_error("[ERROR] HeapAlloc() failed.");
cout << left
<< setw(18)
<< "HeapAlloc() time: "
<< rv << endl;

// Win32 HeapFree() - dependent upon previous HeapAlloc()
t_us.start();
::HeapFree(heap, 0, buf);
rv = t_us.stop();
buf = NULL;
std::cout << std::left
<< std::setw(18)
<< "HeapFree() time: "
<< rv << std::endl;
cout << left
<< setw(18)
<< "HeapFree() time: "
<< rv << endl;
::HeapDestroy(heap);
#endif

Expand Down

0 comments on commit 8da03fa

Please sign in to comment.