osx compile files

This commit is contained in:
Jacob Dufault 2017-03-21 10:06:22 -07:00
parent cd4d63dd9c
commit 528d778d9a
3 changed files with 21 additions and 14 deletions

View File

@ -1,6 +1,8 @@
#include "buffer.h"
#include <cstdlib>
#include <mutex>
#include <thread>
#include "platform.h"
#include "../utils.h"
@ -116,4 +118,4 @@ TEST_CASE("lock") {
}
}
TEST_SUITE_END();
TEST_SUITE_END();

View File

@ -1,7 +1,7 @@
#if defined(__linux__) || defined(__APPLE__)
#include "platform.h"
#include "utils.h"
#include "../utils.h"
#include <cassert>
#include <string>
@ -64,17 +64,19 @@ int checked(int result, const char* expr) {
struct PlatformSharedMemoryLinux : public PlatformSharedMemory {
std::string name_;
size_t size_;
int fd_;
PlatformSharedMemoryLinux(const std::string& name) : name_(name) {
std::cerr << "PlatformSharedMemoryLinux name=" << name << std::endl;
PlatformSharedMemoryLinux(const std::string& name, size_t size)
: name_(name), size_(size) {
std::cerr << "PlatformSharedMemoryLinux name=" << name << ", size=" << size << std::endl;
// Try to create shared memory but only if it does not already exist. Since
// we created the memory, we need to initialize it.
fd_ = shm_open(name_.c_str(), O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
if (fd_ >= 0) {
std::cerr << "Calling ftruncate fd_=" << fd_ << std::endl;
CHECKED(ftruncate(fd_, shmem_size));
CHECKED(ftruncate(fd_, size));
}
// Otherwise, we just open existing shared memory. We don't need to
@ -86,19 +88,20 @@ struct PlatformSharedMemoryLinux : public PlatformSharedMemory {
}
// Map the shared memory to an address.
shared =
CHECKED(mmap(nullptr /*kernel assigned starting address*/, shmem_size,
data =
CHECKED(mmap(nullptr /*kernel assigned starting address*/, size,
PROT_READ | PROT_WRITE, MAP_SHARED, fd_, 0 /*offset*/));
capacity = size;
std::cout << "Open shared memory name=" << name << ", fd=" << fd_
<< ", shared=" << shared << std::endl;
<< ", shared=" << data << ", capacity=" << capacity << std::endl;
}
~PlatformSharedMemoryLinux() override {
CHECKED(munmap(shared, shmem_size));
CHECKED(munmap(data, size_));
CHECKED(shm_unlink(name_.c_str()));
shared = nullptr;
data = nullptr;
}
};
@ -116,8 +119,8 @@ std::unique_ptr<PlatformScopedMutexLock> CreatePlatformScopedMutexLock(
}
std::unique_ptr<PlatformSharedMemory> CreatePlatformSharedMemory(
const std::string& name) {
const std::string& name, size_t size) {
std::string name2 = "/" + name;
return MakeUnique<PlatformSharedMemoryLinux>(name2);
return MakeUnique<PlatformSharedMemoryLinux>(name2, size);
}
#endif
#endif

View File

@ -1,5 +1,7 @@
#pragma once
#include <cstddef>
// Points to a generic block of memory that can be resized. This class owns
// and has the only pointer to the underlying memory buffer.
struct ResizableBuffer {
@ -18,4 +20,4 @@ struct ResizableBuffer {
private:
size_t capacity_;
};
};