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

Haiku support proposal. #218

Merged
merged 2 commits into from
Jun 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 5 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,11 @@ if(NOT DEFINED SNMALLOC_ONLY_HEADER_LIBRARY)
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /Zi")
set(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} /DEBUG")
else()
add_compile_options(-fno-exceptions -fno-rtti -g -ftls-model=initial-exec -fomit-frame-pointer)
add_compile_options(-fno-exceptions -fno-rtti -g -fomit-frame-pointer)
# Static TLS model unsupported on Haiku
if (NOT CMAKE_SYSTEM_NAME MATCHES "Haiku")
add_compile_options(-ftls-model=initial-exec)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please can you leave a comment explaining this? Is there a Haiku TLS ABI doc that you can point at?

Copy link
Collaborator Author

@devnexen devnexen Jun 29, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really but found out a bunch of complains about the same issue I ve faced. Haiku at large is not too well documented, kind of small community driven os project.

endif()
if(SNMALLOC_CI_BUILD OR (${CMAKE_BUILD_TYPE} MATCHES "Debug"))
# Get better stack traces in CI and Debug.
target_link_libraries(snmalloc_lib INTERFACE "-rdynamic")
Expand Down
3 changes: 3 additions & 0 deletions src/pal/pal.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
# include "pal_apple.h"
# include "pal_freebsd.h"
# include "pal_freebsd_kernel.h"
# include "pal_haiku.h"
# include "pal_linux.h"
# include "pal_netbsd.h"
# include "pal_openbsd.h"
Expand All @@ -31,6 +32,8 @@ namespace snmalloc
PALFreeBSDKernel;
# elif defined(__FreeBSD__)
PALFreeBSD;
# elif defined(__HAIKU__)
PALHaiku;
# elif defined(__NetBSD__)
PALNetBSD;
# elif defined(__OpenBSD__)
Expand Down
84 changes: 84 additions & 0 deletions src/pal/pal_haiku.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#pragma once

#if defined(__HAIKU__)
# include "pal_posix.h"

# include <sys/mman.h>

namespace snmalloc
{
/**
* Platform abstraction layer for Haiku. This provides features for this
* system.
*/
class PALHaiku : public PALPOSIX<PALHaiku>
{
public:
/**
* Bitmap of PalFeatures flags indicating the optional features that this
* PAL supports.
*
*/
static constexpr uint64_t pal_features = PALPOSIX::pal_features;

/**
* Notify platform that we will not be needing these pages.
* Haiku does not provide madvise call per say only the posix equivalent.
*/
void notify_not_using(void* p, size_t size) noexcept
{
SNMALLOC_ASSERT(is_aligned_block<page_size>(p, size));
posix_madvise(p, size, POSIX_MADV_DONTNEED);
}

/**
* OS specific function for zeroing memory
* using MAP_NORESERVE for explicit over commit appliance.
*/
template<bool page_aligned = false>
void zero(void* p, size_t size)
{
if (page_aligned || is_aligned_block<page_size>(p, size))
{
SNMALLOC_ASSERT(is_aligned_block<page_size>(p, size));
void* r = mmap(
p,
size,
PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED | MAP_NORESERVE,
-1,
0);

if (r != MAP_FAILED)
return;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't find documentation for the Haiku mmap call. Does it guarantee that the existing mapping is left in place if the map fails?

}

bzero(p, size);
}

/**
* Reserve memory using MAP_NORESERVE for explicit
* over commit applicance.
*/
std::pair<void*, size_t> reserve_at_least(size_t size)
{
constexpr size_t min_size =
bits::is64() ? bits::one_at_bit(32) : bits::one_at_bit(28);
auto size_request = bits::max(size, min_size);

void* p = mmap(
nullptr,
size_request,
PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE,
-1,
0);

if (p == MAP_FAILED)
error("Out of memory");

return {p, size_request};
}
};
} // namespace snmalloc
#endif