Skip to content
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
19 changes: 15 additions & 4 deletions Lisensor.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
["Megaton contributors"]
# C files
"packages/lib/**/*.c" = "GPL-3.0-or-later"
"packages/lib/**/*.cpp" = "GPL-3.0-or-later"
"packages/lib/**/*.hpp" = "GPL-3.0-or-later"
"packages/lib/**/*.h" = "GPL-3.0-or-later"
"packages/lib/src/*.cpp" = "GPL-3.0-or-later"
"packages/lib/src/megaton/*.s" = "GPL-3.0-or-later"
"packages/lib/src/megaton/*.c" = "GPL-3.0-or-later"
"packages/lib/src/megaton/*.cpp" = "GPL-3.0-or-later"
"packages/lib/src/nximpl/*.s" = "GPL-3.0-or-later"
"packages/lib/include/exl/**/*.h" = "GPL-3.0-or-later"
"packages/lib/include/exl/**/*.hpp" = "GPL-3.0-or-later"
"packages/lib/include/exl_armv8/**/*.h" = "GPL-3.0-or-later"
"packages/lib/include/exl_armv8/**/*.hpp" = "GPL-3.0-or-later"
"packages/lib/include/megaton/*.h" = "GPL-3.0-or-later"
"packages/lib/include/megaton/*.hpp" = "GPL-3.0-or-later"
"packages/lib/include/megaton/__internal/*.h" = "GPL-3.0-or-later"
"packages/lib/include/megaton/__internal/*.hpp" = "GPL-3.0-or-later"
"packages/lib/include/megaton/__priv/*.h" = "GPL-3.0-or-later"
"packages/lib/include/megaton/__priv/*.hpp" = "GPL-3.0-or-later"
"packages/test-mod/**/*.cpp" = "GPL-3.0-or-later"
"packages/test-mod/**/*.h" = "GPL-3.0-or-later"

Expand All @@ -17,3 +26,5 @@
# Unmodified files
# ["libnx Authors"]
# "packages/lib/src/external/libnx/*.s" = "ISC"
# ["Matthew Conte"]
# "packages/lib/include/megaton/__external/tlsf.h" = "BSD-3-Clause"
90 changes: 90 additions & 0 deletions packages/lib/include/megaton/__external/tlsf.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#ifndef INCLUDED_tlsf
#define INCLUDED_tlsf

/*
** Two Level Segregated Fit memory allocator, version 3.1.
** Written by Matthew Conte
** http://tlsf.baisoku.org
**
** Based on the original documentation by Miguel Masmano:
** http://www.gii.upv.es/tlsf/main/docs
**
** This implementation was written to the specification
** of the document, therefore no GPL restrictions apply.
**
** Copyright (c) 2006-2016, Matthew Conte
** 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 the copyright holder 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 MATTHEW CONTE 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.
*/

#include <stddef.h>

#if defined(__cplusplus)
extern "C" {
#endif

/* tlsf_t: a TLSF structure. Can contain 1 to N pools. */
/* pool_t: a block of memory that TLSF can manage. */
typedef void* tlsf_t;
typedef void* pool_t;

/* Create/destroy a memory pool. */
tlsf_t tlsf_create(void* mem);
tlsf_t tlsf_create_with_pool(void* mem, size_t bytes);
void tlsf_destroy(tlsf_t tlsf);
pool_t tlsf_get_pool(tlsf_t tlsf);

/* Add/remove memory pools. */
pool_t tlsf_add_pool(tlsf_t tlsf, void* mem, size_t bytes);
void tlsf_remove_pool(tlsf_t tlsf, pool_t pool);

/* malloc/memalign/realloc/free replacements. */
void* tlsf_malloc(tlsf_t tlsf, size_t bytes);
void* tlsf_memalign(tlsf_t tlsf, size_t align, size_t bytes);
void* tlsf_realloc(tlsf_t tlsf, void* ptr, size_t size);
void tlsf_free(tlsf_t tlsf, void* ptr);

/* Returns internal block size, not original request size */
size_t tlsf_block_size(void* ptr);

/* Overheads/limits of internal structures. */
size_t tlsf_size(void);
size_t tlsf_align_size(void);
size_t tlsf_block_size_min(void);
size_t tlsf_block_size_max(void);
size_t tlsf_pool_overhead(void);
size_t tlsf_alloc_overhead(void);

/* Debugging. */
typedef void (*tlsf_walker)(void* ptr, size_t size, int used, void* user);
void tlsf_walk_pool(pool_t pool, tlsf_walker walker, void* user);
/* Returns nonzero if any internal consistency check fails. */
int tlsf_check(tlsf_t tlsf);
int tlsf_check_pool(pool_t pool);

#if defined(__cplusplus)
};
#endif

#endif
24 changes: 3 additions & 21 deletions packages/lib/include/megaton/__internal/alloc.h
Original file line number Diff line number Diff line change
@@ -1,29 +1,11 @@
// SPDX-License-Identifier: GPL-3.0-or-later
// Copyright (c) 2025-2026 Megaton contributors

// this file declares some constants that are used in the allocator and in init
#pragma once

#include <switch/types.h>
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

do we still need this header if it's just a single define?

#define BSS_ALLOC_SIZE 0x20000

namespace nn::mem {
class StandardAllocator {
public:
StandardAllocator();
StandardAllocator(void* address, size_t size);
StandardAllocator(void* address, size_t size, bool enableCache);

~StandardAllocator() {
if (mIsInitialized) {
Finalize();
}
}

void Initialize(void* address, size_t size);
void* Allocate(size_t size);
void* Allocate(size_t size, size_t alignment);
void Free(void* address);
bool mIsInitialized;
void Finalize();
};
namespace megaton::alloc {
void init_allocator();
}
54 changes: 20 additions & 34 deletions packages/lib/src/alloc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,50 +2,36 @@
// Copyright (c) 2025-2026 Megaton contributors

#include <megaton/__internal/alloc.h>
// #include <toolkit/tcp.hpp>
#include <nn/os/os_Mutex.h>
#include <mutex>

#include <megaton/__internal/tlsf.h>

#define BSS_ALLOC_SIZE 0x20000
static u8 bss_alloc[BSS_ALLOC_SIZE] = {0};
static nn::os::Mutex mem_mutex = nn::os::Mutex(true /* recursive */);
static pool_t allocator;

nn::mem::StandardAllocator& allocator() {
static nn::mem::StandardAllocator instance(bss_alloc, sizeof(bss_alloc));
return instance;
namespace megaton::alloc {
void init_allocator() {
allocator = tlsf_create_with_pool(bss_alloc, BSS_ALLOC_SIZE);
}
}

extern "C" u8 *sys_malloc(u64 size, u64 align) {
void *ptr = allocator().Allocate(size, align);
if (ptr != nullptr) {
return (u8 *)ptr;
}
return nullptr;
std::lock_guard<nn::os::Mutex> lock(mem_mutex);
void* ptr = tlsf_memalign(allocator, align, size);
return (u8*) ptr;
}

extern "C" void sys_free(u8 *ptr, u64 size, u64 align) {
// botw::tcp::sendf("calling free on ptr %d with size %d and alignment
// %d\n",
// ptr, size, align);
allocator().Free(ptr);
// botw::tcp::sendf("successfully freed memory\n");
std::lock_guard<nn::os::Mutex> lock(mem_mutex);
tlsf_free(allocator, ptr);
}

extern "C" u8 *sys_realloc(u8 *ptr, u64 size, u64 align, u64 new_size) {
// botw::tcp::sendf(
// "calling realloc on ptr %p from size %d to size %d with alignment\n",
// ptr, size, new_size, align);
if (new_size == 0) {
sys_free(ptr, size, align);
return nullptr;
}
u8 *new_ptr = sys_malloc(new_size, align);
if (new_ptr == nullptr) {
return new_ptr;
}
for (u64 i = 0; i < new_size; i++) {
if (i >= size) {
new_ptr[i] = 0;
} else {
new_ptr[i] = ptr[i];
}
}
sys_free(ptr, size, align);
return new_ptr;
void* new_ptr = nullptr;
std::lock_guard<nn::os::Mutex> lock(mem_mutex);
new_ptr = tlsf_realloc(allocator, ptr, new_size);
return (u8*) new_ptr;
}
4 changes: 3 additions & 1 deletion packages/lib/src/megaton/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <megaton/hook.h>
#include <megaton/module_layout.h>
#include <megaton/patch.h>
#include <megaton/__internal/alloc.h>

extern "C" {
/**
Expand Down Expand Up @@ -51,7 +52,8 @@ void __megaton_lib_init() {
}

void __megaton_librs_init() {
return;
megaton::alloc::init_allocator();
return;
}
// TODO: this can probably be removed with rtld/reloc
void __megaton_rtld_init() {}
Expand Down
25 changes: 25 additions & 0 deletions packages/lib/src/os_Mutex.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// SPDX-License-Identifier: GPL-3.0-or-later
// Copyright (c) 2026 Megaton contributors

#include <nn/os/os_Mutex.h>

// these symbols are wrong in the header and shouldn't exist
// might need to be fixed

// this is required to generate the correct destructor for some reason
// otherwise a different symbol is generated and it doesn't match with
// what usage sites expect
namespace nn::os {
Mutex::~Mutex() {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Why does this need to be implemented?

nn::os::FinalizeMutex(&this->m_Mutex);
}

// these are required for std::lock_guard
void Mutex::lock() {
this->Lock();
}

void Mutex::unlock() {
this->Unlock();
}
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Why does these need to be implemented? they should already be defined in the SDK

}
Loading
Loading