Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More cleanup + OS X #202

Merged
merged 5 commits into from
Aug 24, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
more cleanup
  • Loading branch information
timkpaine committed Aug 21, 2018
commit 421d6ac65e27e70eddb153a62ece2a3d7174fbe6
2 changes: 1 addition & 1 deletion packages/perspective/src/cpp/base_impl_linux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*
*/

#ifndef WIN32
#ifdef __linux__
#include <perspective/first.h>
#include <perspective/base.h>

Expand Down
25 changes: 25 additions & 0 deletions packages/perspective/src/cpp/base_impl_osx.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/******************************************************************************
*
* Copyright (c) 2017, the Perspective Authors.
*
* This file is part of the Perspective library, distributed under the terms of
* the Apache License 2.0. The full license can be found in the LICENSE file.
*
*/

#ifdef __APPLE__
#include <perspective/first.h>
#include <perspective/base.h>

namespace perspective
{

t_str
get_error_str()
{
// handled by perror
return t_str();
}

} // end namespace perspective
#endif
2 changes: 1 addition & 1 deletion packages/perspective/src/cpp/compat_impl_linux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*
*/

#ifndef WIN32
#ifdef __linux__

#include <perspective/first.h>
#include <perspective/compat.h>
Expand Down
239 changes: 239 additions & 0 deletions packages/perspective/src/cpp/compat_impl_osx.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,239 @@
/******************************************************************************
*
* Copyright (c) 2017, the Perspective Authors.
*
* This file is part of the Perspective library, distributed under the terms of
* the Apache License 2.0. The full license can be found in the LICENSE file.
*
*/

#ifdef __APPLE__

#include <perspective/first.h>
#include <perspective/compat.h>
#include <perspective/raii.h>
#include <perspective/raw_types.h>
#include <perspective/utils.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/mman.h>
#include <sys/resource.h>
#include <sys/stat.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>

namespace perspective
{
static void map_file_internal_(const t_str& fname,
t_fflag fflag,
t_fflag fmode,
t_fflag creation_disposition,
t_fflag mprot,
t_fflag mflag,
t_bool is_read,
t_uindex size,
t_rfmapping& out);

t_uindex
file_size(t_handle h)
{
struct stat st;
t_rcode rcode = fstat(h, &st);
PSP_VERBOSE_ASSERT(rcode == 0, "Error in stat");
return st.st_size;
}

void
close_file(t_handle h)
{
t_rcode rcode = close(h);
PSP_VERBOSE_ASSERT(rcode == 0, "Error closing file.");
}

void
flush_mapping(void* base, t_uindex len)
{
t_rcode rcode = msync(base, len, MS_SYNC);
PSP_VERBOSE_ASSERT(rcode != -1, "Error in msync");
}

t_rfmapping::~t_rfmapping()
{
t_rcode rcode = munmap(m_base, m_size);
PSP_VERBOSE_ASSERT(rcode == 0, "munmap failed.");

rcode = close(m_fd);
PSP_VERBOSE_ASSERT(rcode == 0, "Error closing file.");
}

static void
map_file_internal_(const t_str& fname,
t_fflag fflag,
t_fflag fmode,
t_fflag creation_disposition,
t_fflag mprot,
t_fflag mflag,
t_bool is_read,
t_uindex size,
t_rfmapping& out)
{
t_file_handle fh(open(fname.c_str(), fflag, fmode));

PSP_VERBOSE_ASSERT(fh.valid(), "Error opening file");

if (is_read)
{
size = file_size(fh.value());
}
else
{
t_index rcode = ftruncate(fh.value(), size);
PSP_VERBOSE_ASSERT(rcode >= 0, "ftruncate failed.");
}

void* ptr = mmap(0, size, mprot, mflag, fh.value(), 0);

PSP_VERBOSE_ASSERT(ptr != MAP_FAILED, "error in mmap");

t_handle fd = fh.value();
fh.release();

out.m_fd = fd;
out.m_base = ptr;
out.m_size = size;
}

void
map_file_read(const t_str& fname, t_rfmapping& out)
{
map_file_internal_(fname,
O_RDONLY,
S_IRUSR,
0, // no disposition
PROT_READ,
MAP_SHARED,
true,
0,
out);
}

void
map_file_write(const t_str& fname, t_uindex size, t_rfmapping& out)
{
return map_file_internal_(fname,
O_RDWR | O_TRUNC | O_CREAT,
S_IRUSR | S_IWUSR,
0, // no disposition
PROT_WRITE | PROT_READ,
MAP_SHARED,
false,
size,
out);
}

t_int64
psp_curtime()
{
struct timespec t;
t_int32 rcode = clock_gettime(CLOCK_MONOTONIC, &t);
PSP_VERBOSE_ASSERT(rcode == 0, "Failure in clock_gettime");
t_int64 ns = t.tv_nsec + t.tv_sec * 1000000000;
return ns;
}

t_int64
get_page_size()
{
static t_int64 pgsize = getpagesize();
return pgsize;
}

t_int64
psp_curmem()
{
static t_float64 multiplier = getpagesize() / 1024000.;

struct t_statm
{
long int m_size, m_resident, m_share, m_text, m_lib, m_data,
m_dt;
};

t_statm result;

const char* statm_path = "/proc/self/statm";

FILE* f = fopen(statm_path, "r");
if (!f)
{
perror(statm_path);
abort();
}

PSP_VERBOSE_ASSERT(fscanf(f,
"%ld %ld %ld %ld %ld %ld %ld",
&result.m_size,
&result.m_resident,
&result.m_share,
&result.m_text,
&result.m_lib,
&result.m_data,
&result.m_dt) == 7,
"Failed to read memory size");
fclose(f);
return result.m_resident * multiplier;
}

void
set_thread_name(std::thread& thr, const t_str& name)
{
#ifdef PSP_PARALLEL_FOR
auto handle = thr.native_handle();
pthread_setname_np(name.c_str());
#endif
}

void
set_thread_name(const t_str& name)
{
// prctl(PR_SET_NAME, name.c_str(), 0, 0, 0);
PSP_COMPLAIN_AND_ABORT("Not implemented");
}

void
rmfile(const t_str& fname)
{
unlink(fname.c_str());
}

void
launch_proc(const t_str& cmdline)
{
PSP_COMPLAIN_AND_ABORT("Not implemented");
}

t_str
cwd()
{
PSP_COMPLAIN_AND_ABORT("Not implemented");
return "";
}

void*
psp_dbg_malloc(size_t size)
{
PSP_COMPLAIN_AND_ABORT("Not implemented");
return nullptr;
}

void
psp_dbg_free(void* mem)
{
PSP_COMPLAIN_AND_ABORT("Not implemented");
}

} // end namespace perspective
#endif

1 change: 0 additions & 1 deletion packages/perspective/src/cpp/gnode_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
#include <perspective/sym_table.h>
#ifdef PSP_PARALLEL_FOR
#include <tbb/tbb.h>
#include <perspective/config_proc.h>
#endif

namespace perspective
Expand Down
5 changes: 0 additions & 5 deletions packages/perspective/src/cpp/pool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,13 +133,8 @@ t_pool::_process_helper()
auto work_to_do = m_data_remaining.load();
if (work_to_do)
{
#ifdef PSP_ENABLE_WASM
t_update_task task(*this);
task.run();
#else
auto task = new t_update_task(*this);
ASGWidget::c_task_queue_post(task);
#endif
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/perspective/src/cpp/raii_impl_linux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*
*/

#ifndef WIN32
#ifdef __linux__
#include <perspective/first.h>
#include <perspective/base.h>
#include <perspective/raii.h>
Expand Down
57 changes: 57 additions & 0 deletions packages/perspective/src/cpp/raii_impl_osx.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/******************************************************************************
*
* Copyright (c) 2017, the Perspective Authors.
*
* This file is part of the Perspective library, distributed under the terms of
* the Apache License 2.0. The full license can be found in the LICENSE file.
*
*/

#ifdef __APPLE__
#include <perspective/first.h>
#include <perspective/base.h>
#include <perspective/raii.h>
#include <sys/mman.h>

namespace perspective
{

t_file_handle::~t_file_handle()
{
if (valid())
{
t_rcode rcode = close(m_value);
PSP_VERBOSE_ASSERT(rcode == 0, "Error closing file.");
}
}

bool
t_file_handle::valid() const
{
return m_value >= 0;
}

void
t_file_handle::release()
{
m_value = -1;
}

t_mmap_handle::~t_mmap_handle()
{
if (valid())
{
t_rcode rcode = munmap(m_value, m_len);
PSP_VERBOSE_ASSERT(rcode == 0, "munmap failed.");
}
}

bool
t_mmap_handle::valid()
{
return m_value != MAP_FAILED;
}

} // end namespace perspective

#endif
2 changes: 1 addition & 1 deletion packages/perspective/src/cpp/storage_impl_linux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*
*/

#ifndef WIN32
#ifdef __linux__

#include <perspective/first.h>
#include <perspective/base.h>
Expand Down
Loading