Skip to content

Commit

Permalink
++
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilipDeegan committed Dec 8, 2024
1 parent 48948bd commit e8bf861
Show file tree
Hide file tree
Showing 11 changed files with 356 additions and 190 deletions.
6 changes: 5 additions & 1 deletion .github/workflows/build_nix.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
name: ubuntu-latest

concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
cancel-in-progress: true

on:
push:
branches: [ master ]
Expand All @@ -16,4 +20,4 @@ jobs:
run: |
curl -Lo mkn https://github.com/mkn/mkn/releases/download/latest/mkn_nix
chmod +x mkn
KLOG=3 ./mkn clean build run -dtOp test -a "-std=c++17 -fPIC"
KLOG=3 ./mkn clean build run -dtOp test -a "-std=c++20 -fPIC"
6 changes: 5 additions & 1 deletion .github/workflows/build_osx.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
name: macos-latest

concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
cancel-in-progress: true

on:
push:
branches: [ master ]
Expand All @@ -16,4 +20,4 @@ jobs:
run: |
curl -Lo mkn https://github.com/mkn/mkn/releases/download/latest/mkn_arm_osx
chmod +x mkn
KLOG=3 ./mkn clean build run -dtOp test -a "-std=c++17 -fPIC"
KLOG=3 ./mkn clean build run -dtOp test -a "-std=c++20 -fPIC"
6 changes: 5 additions & 1 deletion .github/workflows/build_win.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
name: windows-latest

concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
cancel-in-progress: true

on:
push:
branches: [ master ]
Expand All @@ -23,4 +27,4 @@ jobs:
run: | # /bin/link interferes with cl/link.exe
bash -c "rm /bin/link"
bash -c "curl -Lo mkn.exe https://github.com/mkn/mkn/releases/download/latest/mkn.exe"
bash -c 'KLOG=3 ./mkn clean build run -dtKOp test -a "-EHsc -std:c++17"'
bash -c 'KLOG=3 ./mkn clean build run -dtKOp test -a "-EHsc -std:c++20"'
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# mkn.kul

**Cross platform C++ wrapper for threading/process management/io/logging/signal handling and stack walking.**
**Cross platform C++20 wrapper for threading/process management/io/logging/signal handling and stack walking.**

Supported Architectures/Operating Systems:

Expand Down
180 changes: 3 additions & 177 deletions inc/mkn/kul/alloc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,182 +31,8 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#ifndef _MKN_KUL_ALLOC_HPP_
#define _MKN_KUL_ALLOC_HPP_

#include <new>
#include <cstdint>
#include <cstddef>
#include <cstdlib>
#include <type_traits>

#include <limits>
#include <stdlib.h> // posix_memalign
#include <sys/mman.h> // madvise

namespace mkn::kul {

template <typename T, std::int32_t alignment = 32>
class AlignedAllocator {
using This = AlignedAllocator<T, alignment>;

public:
using pointer = T*;
using reference = T&;
using value_type = T;
using size_type = std::size_t;
using difference_type = std::ptrdiff_t;

template <typename U>
struct rebind {
using other = AlignedAllocator<U, alignment>;
};

T* allocate(std::size_t const n) const {
if (n == 0) return nullptr;

auto size = n * sizeof(T);
std::uint32_t diff = size % alignment;
if (diff > 0) diff = alignment - diff;

void* p = std::aligned_alloc(alignment, size + diff);
if (!p) throw std::bad_alloc();

return static_cast<T*>(p);
}

void deallocate(T* const p) noexcept {
if (p) std::free(p);
}
void deallocate(T* const p, std::size_t /*n*/) noexcept { // needed from std::
deallocate(p);
}

bool operator!=(This const& that) const { return !(*this == that); }

bool operator==(This const& /*that*/) const {
return true; // stateless
}
};

template <typename T>
class Allocator {
using This = Allocator<T>;

public:
using pointer = T*;
using reference = T&;
using value_type = T;
using size_type = std::size_t;
using difference_type = std::ptrdiff_t;

template <typename U>
struct rebind {
using other = Allocator<U>;
};

T* allocate(std::size_t const n) const { return static_cast<T*>(::operator new(n * sizeof(T))); }

void deallocate(T* const p) noexcept {
if (p) ::operator delete(p);
}
void deallocate(T* const p, std::size_t /*n*/) noexcept { // needed from std::
if (p) ::operator delete(p);
}
bool operator!=(This const& that) const { return !(*this == that); }
bool operator==(This const& /*that*/) const {
return true; // stateless
}
};

template <typename T>
class NonConstructingAllocator : public Allocator<T> {
using This = NonConstructingAllocator<T>;

public:
template <typename U>
struct rebind {
using other = NonConstructingAllocator<U>;
};

T* allocate(std::size_t const n) const {
// if (n == 0) return nullptr;
return static_cast<T*>(malloc(n * sizeof(T)));
}

template <typename U, typename... Args>
void construct(U* /*ptr*/, Args&&... /*args*/) {} // nothing
template <typename U>
void construct(U* /*ptr*/) noexcept(std::is_nothrow_default_constructible<U>::value) {}

bool operator!=(This const& that) const { return !(*this == that); }
bool operator==(This const& /*that*/) const {
return true; // stateless
}
};

template <typename T, std::size_t huge_page_size = 4096>
class HugePageAllocator : public Allocator<T> {
using This = HugePageAllocator<T, huge_page_size>;

public:
template <typename U>
struct rebind {
using other = HugePageAllocator<U>;
};

HugePageAllocator() = default;
template <class U>
constexpr HugePageAllocator(HugePageAllocator<U> const&) noexcept {}

T* allocate(std::size_t n) {
if (n > std::numeric_limits<std::size_t>::max() / sizeof(T)) throw std::bad_alloc();
void* p = nullptr;
posix_memalign(&p, huge_page_size, n * sizeof(T));
madvise(p, n * sizeof(T), MADV_HUGEPAGE);
if (p == nullptr) throw std::bad_alloc();
return static_cast<T*>(p);
}
void deallocate(T* p, std::size_t n) { std::free(p); }
bool operator!=(This const& that) const { return !(*this == that); }
bool operator==(This const& /*that*/) const {
return true; // stateless
}
};

template <typename T, std::size_t huge_page_size = 4096>
class NonConstructingHugePageAllocator : public NonConstructingAllocator<T> {
using This = NonConstructingHugePageAllocator<T, huge_page_size>;

public:
template <typename U>
struct rebind {
using other = NonConstructingHugePageAllocator<U>;
};

NonConstructingHugePageAllocator() = default;
template <class U>
constexpr NonConstructingHugePageAllocator(NonConstructingHugePageAllocator<U> const&) noexcept {}

T* allocate(std::size_t n) {
if (n > std::numeric_limits<std::size_t>::max() / sizeof(T)) throw std::bad_alloc();
void* p = nullptr;
posix_memalign(&p, huge_page_size, n * sizeof(T));
madvise(p, n * sizeof(T), MADV_HUGEPAGE);
if (p == nullptr) throw std::bad_alloc();
return static_cast<T*>(p);
}

void deallocate(T* p, std::size_t n) { std::free(p); }

template <typename U, typename... Args>
void construct(U* /*ptr*/, Args&&... /*args*/) {} // nothing
template <typename U>
void construct(U* /*ptr*/) noexcept(std::is_nothrow_default_constructible<U>::value) {}

bool operator!=(This const& that) const { return !(*this == that); }
bool operator==(This const& /*that*/) const {
return true; // stateless
}
};

} // namespace mkn::kul
#include "mkn/kul/alloc/base.hpp"
#include "mkn/kul/alloc/aligned.hpp" // active if available
#include "mkn/kul/alloc/huge.hpp" // active if available

#endif /*_MKN_KUL_ALLOC_HPP_*/
92 changes: 92 additions & 0 deletions inc/mkn/kul/alloc/aligned.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/**
Copyright (c) 2022, Philip Deegan.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the
distribution.
* Neither the name of Philip Deegan nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef _MKN_KUL_ALLOC_ALIGNED_HPP_
#define _MKN_KUL_ALLOC_ALIGNED_HPP_

#if __has_include(<sys/mman.h>)

#include "mkn/kul/alloc/base.hpp"

#include <new>
#include <cstddef>
#include <cstdlib>
#include <type_traits>

namespace mkn::kul {

template <typename T, std::int32_t alignment = 32>
class AlignedAllocator : public Allocator<T> {
using This = AlignedAllocator<T, alignment>;

public:
using pointer = T*;
using reference = T&;
using value_type = T;
using size_type = std::size_t;
using difference_type = std::ptrdiff_t;

template <typename U>
struct rebind {
using other = AlignedAllocator<U, alignment>;
};

T* allocate(std::size_t const n) const {
if (n == 0) return nullptr;

auto size = n * sizeof(T);
std::uint32_t diff = size % alignment;
if (diff > 0) diff = alignment - diff;

void* p = std::aligned_alloc(alignment, size + diff);
if (!p) throw std::bad_alloc();

return static_cast<T*>(p);
}

void deallocate(T* const p) noexcept {
if (p) std::free(p);
}
void deallocate(T* const p, std::size_t /*n*/) noexcept { // needed from std::
deallocate(p);
}

bool operator!=(This const& that) const { return !(*this == that); }

bool operator==(This const& /*that*/) const {
return true; // stateless
}
};

} // namespace mkn::kul

#endif // __has_include(<sys/mman.h>)

#endif /*_MKN_KUL_ALLOC_ALIGNED_HPP_*/
Loading

0 comments on commit e8bf861

Please sign in to comment.