Skip to content

Commit

Permalink
Refactored utils.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
Wouter Tichelaar committed May 25, 2024
1 parent ea175d5 commit dbde6ca
Showing 1 changed file with 22 additions and 65 deletions.
87 changes: 22 additions & 65 deletions src/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <vector>
#include <sys/time.h>
#include "utils.hpp"

Expand All @@ -10,88 +11,44 @@
#ifdef _WIN32
#include <windows.h>
#include <malloc.h>
#include <vector>
typedef DWORD thread_ret_t;

class LargeArrayManager {
public:
LargeArrayManager(size_t size) {
arr.reserve(size);
arr.resize(size); // Allocate and resize to the desired size
}

char* getPointer() {
return arr.data();
}

private:
std::vector<char> arr;
};

char* createBuffer(size_t size) {
LargeArrayManager* manager = new LargeArrayManager(size);
return manager->getPointer();
}
#else
#include <sys/mman.h>
#endif

char* allocateAndLockMemory(size_t size) {
char* buffer = createBuffer(size);
void* gracefullyAllocateBuffer(size_t size){
std::vector<char>* arr = new std::vector<char>();
arr->reserve(size);
arr->resize(size);
char* buffer = arr->data();
std::memset(buffer, 0, size);
return buffer;
}

static int check_align(size_t align)
{
for (size_t i = sizeof(void *); i != 0; i *= 2)
if (align == i)
return 0;
return EINVAL;
}

int posix_memalign(void **ptr, size_t align, size_t size) {
if (check_align(align)) {
fprintf(stderr, "posix_memalign: alignment %zu is not valid\n", align);
return EINVAL;
}

int saved_errno = errno;
void *p = _aligned_malloc(size, align);
if (p == NULL) {
fprintf(stderr, "posix_memalign: _aligned_malloc failed\n");
errno = saved_errno;
return ENOMEM;
}

*ptr = p;
return 0;
#ifdef _WIN32
char* newBuffer(size_t size) {
char* ptr = (char*)gracefullyAllocateBuffer(size);
return ptr;
}

#else
#include <sys/mman.h>

char* allocateAndLockMemory(size_t size) {
char* newBuffer(size_t size) {
char* ptr;
bool useGraceful = false;

if (posix_memalign((void**)&ptr, BUFFER_ALIGNMENT, size) != 0) {
fprintf(stderr, "error: posix_memalign failed\n");
exit(EXIT_FAILURE);
useGraceful = true;
}
else if (mlock(ptr, size) != 0){
useGraceful = true;
}
if (mlock(ptr, size) != 0) {
fprintf(stderr, "🚧 Cannot allocate %zu bytes directly in RAM\n", size);

if(useGraceful){
ptr = (char*)gracefullyAllocateBuffer(size);
}

return ptr;
}
#endif

char* newBuffer(size_t size) {
char* buffer = (char*)allocateAndLockMemory(size);
if (buffer == NULL || buffer == nullptr) {
fprintf(stderr, "🚧 Cannot allocate %zu bytes directly in RAM\n", size);
exit(EXIT_FAILURE);
}
return buffer;
}

unsigned long timeMs() {
struct timeval te;
gettimeofday(&te, NULL);
Expand Down

0 comments on commit dbde6ca

Please sign in to comment.