Allow user override of dynamic memory allocation functions throughout libnx

This commit is contained in:
fincs 2021-01-21 20:17:37 +01:00
parent d813bef54b
commit 076657fd31
No known key found for this signature in database
GPG Key ID: 62C7609ADA219C60
23 changed files with 116 additions and 102 deletions

View File

@ -1,11 +1,11 @@
#include <string.h> #include <string.h>
#include <malloc.h>
#include <math.h> #include <math.h>
#include "libapplet_internal.h" #include "libapplet_internal.h"
#include "applets/swkbd.h" #include "applets/swkbd.h"
#include "services/vi.h" #include "services/vi.h"
#include "runtime/hosversion.h" #include "runtime/hosversion.h"
#include "runtime/util/utf.h" #include "runtime/util/utf.h"
#include "../runtime/alloc.h"
static Result _swkbdGetReplies(SwkbdInline* s); static Result _swkbdGetReplies(SwkbdInline* s);
@ -85,7 +85,7 @@ Result swkbdCreate(SwkbdConfig* c, s32 max_dictwords) {
c->workbuf_size = (c->workbuf_size + 0xfff) & ~0xfff; c->workbuf_size = (c->workbuf_size + 0xfff) & ~0xfff;
} }
c->workbuf = (u8*)memalign(0x1000, c->workbuf_size); c->workbuf = (u8*)__libnx_aligned_alloc(0x1000, c->workbuf_size);
if (c->workbuf==NULL) rc = MAKERESULT(Module_Libnx, LibnxError_OutOfMemory); if (c->workbuf==NULL) rc = MAKERESULT(Module_Libnx, LibnxError_OutOfMemory);
if (R_SUCCEEDED(rc)) memset(c->workbuf, 0, c->workbuf_size); if (R_SUCCEEDED(rc)) memset(c->workbuf, 0, c->workbuf_size);
@ -93,7 +93,7 @@ Result swkbdCreate(SwkbdConfig* c, s32 max_dictwords) {
} }
void swkbdClose(SwkbdConfig* c) { void swkbdClose(SwkbdConfig* c) {
free(c->workbuf); __libnx_free(c->workbuf);
memset(c, 0, sizeof(SwkbdConfig)); memset(c, 0, sizeof(SwkbdConfig));
} }
@ -279,14 +279,14 @@ Result swkbdShow(SwkbdConfig* c, char* out_string, size_t out_string_size) {
memset(&storage, 0, sizeof(AppletStorage)); memset(&storage, 0, sizeof(AppletStorage));
memset(&customizedDictionarySet_storage, 0, sizeof(customizedDictionarySet_storage)); memset(&customizedDictionarySet_storage, 0, sizeof(customizedDictionarySet_storage));
strbuf = (u16*)malloc(strbuf_size+2); strbuf = (u16*)__libnx_alloc(strbuf_size+2);
if (strbuf==NULL) rc = MAKERESULT(Module_Libnx, LibnxError_OutOfMemory); if (strbuf==NULL) rc = MAKERESULT(Module_Libnx, LibnxError_OutOfMemory);
if (strbuf) memset(strbuf, 0, strbuf_size+2); if (strbuf) memset(strbuf, 0, strbuf_size+2);
if (R_FAILED(rc)) return rc; if (R_FAILED(rc)) return rc;
rc = appletCreateLibraryApplet(&holder, AppletId_LibraryAppletSwkbd, LibAppletMode_AllForeground); rc = appletCreateLibraryApplet(&holder, AppletId_LibraryAppletSwkbd, LibAppletMode_AllForeground);
if (R_FAILED(rc)) { if (R_FAILED(rc)) {
free(strbuf); __libnx_free(strbuf);
return rc; return rc;
} }
@ -354,7 +354,7 @@ Result swkbdShow(SwkbdConfig* c, char* out_string, size_t out_string_size) {
appletStorageCloseTmem(&storage); appletStorageCloseTmem(&storage);
appletStorageCloseTmem(&customizedDictionarySet_storage); appletStorageCloseTmem(&customizedDictionarySet_storage);
free(strbuf); __libnx_free(strbuf);
return rc; return rc;
} }
@ -412,18 +412,18 @@ Result swkbdInlineCreate(SwkbdInline* s) {
swkbdInlineSetUtf8Mode(s, true); swkbdInlineSetUtf8Mode(s, true);
s->interactive_tmpbuf_size = 0x1000; s->interactive_tmpbuf_size = 0x1000;
s->interactive_tmpbuf = (u8*)malloc(s->interactive_tmpbuf_size); s->interactive_tmpbuf = (u8*)__libnx_alloc(s->interactive_tmpbuf_size);
if (s->interactive_tmpbuf==NULL) rc = MAKERESULT(Module_Libnx, LibnxError_OutOfMemory); if (s->interactive_tmpbuf==NULL) rc = MAKERESULT(Module_Libnx, LibnxError_OutOfMemory);
if (R_SUCCEEDED(rc)) memset(s->interactive_tmpbuf, 0, s->interactive_tmpbuf_size); if (R_SUCCEEDED(rc)) memset(s->interactive_tmpbuf, 0, s->interactive_tmpbuf_size);
if (R_SUCCEEDED(rc)) { if (R_SUCCEEDED(rc)) {
s->interactive_strbuf_size = 0x1001; s->interactive_strbuf_size = 0x1001;
s->interactive_strbuf = (char*)malloc(s->interactive_strbuf_size); s->interactive_strbuf = (char*)__libnx_alloc(s->interactive_strbuf_size);
if (s->interactive_strbuf==NULL) rc = MAKERESULT(Module_Libnx, LibnxError_OutOfMemory); if (s->interactive_strbuf==NULL) rc = MAKERESULT(Module_Libnx, LibnxError_OutOfMemory);
if (R_SUCCEEDED(rc)) memset(s->interactive_strbuf, 0, s->interactive_strbuf_size); if (R_SUCCEEDED(rc)) memset(s->interactive_strbuf, 0, s->interactive_strbuf_size);
if (R_FAILED(rc)) { if (R_FAILED(rc)) {
free(s->interactive_tmpbuf); __libnx_free(s->interactive_tmpbuf);
s->interactive_tmpbuf = NULL; s->interactive_tmpbuf = NULL;
} }
} }
@ -470,10 +470,10 @@ Result swkbdInlineClose(SwkbdInline* s) {
appletHolderClose(&s->holder); appletHolderClose(&s->holder);
} }
free(s->interactive_tmpbuf); __libnx_free(s->interactive_tmpbuf);
s->interactive_tmpbuf = NULL; s->interactive_tmpbuf = NULL;
s->interactive_tmpbuf_size = 0; s->interactive_tmpbuf_size = 0;
free(s->interactive_strbuf); __libnx_free(s->interactive_strbuf);
s->interactive_strbuf = NULL; s->interactive_strbuf = NULL;
s->interactive_strbuf_size = 0; s->interactive_strbuf_size = 0;

View File

@ -1,5 +1,4 @@
#include <string.h> #include <string.h>
#include <malloc.h>
#include "libapplet_internal.h" #include "libapplet_internal.h"
#include "applets/web.h" #include "applets/web.h"
#include "runtime/hosversion.h" #include "runtime/hosversion.h"

View File

@ -64,7 +64,7 @@ Result audrvCreate(AudioDriver* d, const AudioRendererConfig* config, int num_fi
size_t etc_size = _audrvGetEtcSize(config); size_t etc_size = _audrvGetEtcSize(config);
memset(d, 0, sizeof(AudioDriver)); memset(d, 0, sizeof(AudioDriver));
d->etc = (AudioDriverEtc*)malloc(etc_size); d->etc = (AudioDriverEtc*)__libnx_alloc(etc_size);
if (!d->etc) if (!d->etc)
goto _error0; goto _error0;
@ -80,12 +80,12 @@ Result audrvCreate(AudioDriver* d, const AudioRendererConfig* config, int num_fi
d->etc->sinks = (AudioDriverEtcSink*)(d->etc->mixes+config->num_mix_objs); d->etc->sinks = (AudioDriverEtcSink*)(d->etc->mixes+config->num_mix_objs);
d->etc->out_buf_size = audrenGetOutputParamSize(config); d->etc->out_buf_size = audrenGetOutputParamSize(config);
d->etc->out_buf = memalign(AUDREN_OUTPUT_PARAM_ALIGNMENT, d->etc->out_buf_size); d->etc->out_buf = __libnx_aligned_alloc(AUDREN_OUTPUT_PARAM_ALIGNMENT, d->etc->out_buf_size);
if (!d->etc->out_buf) if (!d->etc->out_buf)
goto _error1; goto _error1;
d->etc->in_buf_size = audrenGetInputParamSize(config); d->etc->in_buf_size = audrenGetInputParamSize(config);
d->etc->in_buf = memalign(AUDREN_INPUT_PARAM_ALIGNMENT, d->etc->in_buf_size); d->etc->in_buf = __libnx_aligned_alloc(AUDREN_INPUT_PARAM_ALIGNMENT, d->etc->in_buf_size);
if (!d->etc->in_buf) if (!d->etc->in_buf)
goto _error2; goto _error2;
@ -93,9 +93,9 @@ Result audrvCreate(AudioDriver* d, const AudioRendererConfig* config, int num_fi
return 0; return 0;
_error2: _error2:
free(d->etc->out_buf); __libnx_free(d->etc->out_buf);
_error1: _error1:
free(d->etc); __libnx_free(d->etc);
d->etc = NULL; d->etc = NULL;
_error0: _error0:
return MAKERESULT(Module_Libnx, LibnxError_OutOfMemory); return MAKERESULT(Module_Libnx, LibnxError_OutOfMemory);
@ -136,8 +136,8 @@ Result audrvUpdate(AudioDriver* d)
void audrvClose(AudioDriver* d) void audrvClose(AudioDriver* d)
{ {
free(d->etc->in_buf); __libnx_free(d->etc->in_buf);
free(d->etc->out_buf); __libnx_free(d->etc->out_buf);
free(d->etc); __libnx_free(d->etc);
memset(d, 0, sizeof(AudioDriver)); memset(d, 0, sizeof(AudioDriver));
} }

View File

@ -1,10 +1,10 @@
#pragma once #pragma once
#include <malloc.h>
#include <string.h> #include <string.h>
#include "types.h" #include "types.h"
#include "result.h" #include "result.h"
#include "services/audren.h" #include "services/audren.h"
#include "audio/driver.h" #include "audio/driver.h"
#include "../runtime/alloc.h"
typedef struct { typedef struct {
int next_free; int next_free;

View File

@ -11,6 +11,7 @@
#include "display/native_window.h" #include "display/native_window.h"
#include "display/framebuffer.h" #include "display/framebuffer.h"
#include "nvidia/graphic_buffer.h" #include "nvidia/graphic_buffer.h"
#include "../runtime/alloc.h"
static const NvColorFormat g_nvColorFmtTable[] = { static const NvColorFormat g_nvColorFmtTable[] = {
NvColorFormat_A8B8G8R8, // PIXEL_FORMAT_RGBA_8888 NvColorFormat_A8B8G8R8, // PIXEL_FORMAT_RGBA_8888
@ -76,7 +77,7 @@ Result framebufferCreate(Framebuffer* fb, NWindow *win, u32 width, u32 height, u
const u32 fb_size = width_aligned_bytes*height_aligned; const u32 fb_size = width_aligned_bytes*height_aligned;
const u32 buf_size = (num_fbs*fb_size + 0xFFF) &~ 0xFFF; // needs to be page aligned const u32 buf_size = (num_fbs*fb_size + 0xFFF) &~ 0xFFF; // needs to be page aligned
fb->buf = aligned_alloc(0x1000, buf_size); fb->buf = __libnx_aligned_alloc(0x1000, buf_size);
if (!fb->buf) if (!fb->buf)
rc = MAKERESULT(Module_Libnx, LibnxError_OutOfMemory); rc = MAKERESULT(Module_Libnx, LibnxError_OutOfMemory);
@ -119,10 +120,11 @@ Result framebufferMakeLinear(Framebuffer* fb)
return MAKERESULT(Module_Libnx, LibnxError_AlreadyInitialized); return MAKERESULT(Module_Libnx, LibnxError_AlreadyInitialized);
u32 height = (fb->win->height + 7) &~ 7; // GOBs are 8 rows tall u32 height = (fb->win->height + 7) &~ 7; // GOBs are 8 rows tall
fb->buf_linear = calloc(1, fb->stride*height); fb->buf_linear = __libnx_alloc(fb->stride*height);
if (!fb->buf_linear) if (!fb->buf_linear)
return MAKERESULT(Module_Libnx, LibnxError_OutOfMemory); return MAKERESULT(Module_Libnx, LibnxError_OutOfMemory);
memset(fb->buf_linear, 0, fb->stride*height);
return 0; return 0;
} }
@ -132,12 +134,12 @@ void framebufferClose(Framebuffer* fb)
return; return;
if (fb->buf_linear) if (fb->buf_linear)
free(fb->buf_linear); __libnx_free(fb->buf_linear);
if (fb->buf) { if (fb->buf) {
nwindowReleaseBuffers(fb->win); nwindowReleaseBuffers(fb->win);
nvMapClose(&fb->map); nvMapClose(&fb->map);
free(fb->buf); __libnx_free(fb->buf);
} }
memset(fb, 0, sizeof(*fb)); memset(fb, 0, sizeof(*fb));

View File

@ -1,5 +1,4 @@
// Copyright 2018 plutoo // Copyright 2018 plutoo
#include <malloc.h>
#include "types.h" #include "types.h"
#include "result.h" #include "result.h"
#include "runtime/env.h" #include "runtime/env.h"
@ -7,6 +6,7 @@
#include "kernel/svc.h" #include "kernel/svc.h"
#include "kernel/virtmem.h" #include "kernel/virtmem.h"
#include "kernel/jit.h" #include "kernel/jit.h"
#include "../runtime/alloc.h"
Result jitCreate(Jit* j, size_t size) Result jitCreate(Jit* j, size_t size)
{ {
@ -29,8 +29,7 @@ Result jitCreate(Jit* j, size_t size)
} }
size = (size + 0xFFF) &~ 0xFFF; size = (size + 0xFFF) &~ 0xFFF;
void* src_addr = __libnx_aligned_alloc(0x1000, size);
void* src_addr = memalign(0x1000, size);
if (src_addr == NULL) if (src_addr == NULL)
return MAKERESULT(Module_Libnx, LibnxError_OutOfMemory); return MAKERESULT(Module_Libnx, LibnxError_OutOfMemory);
@ -92,7 +91,7 @@ Result jitCreate(Jit* j, size_t size)
} }
if (R_FAILED(rc)) { if (R_FAILED(rc)) {
free(j->src_addr); __libnx_free(j->src_addr);
j->src_addr = NULL; j->src_addr = NULL;
} }
@ -179,7 +178,7 @@ Result jitClose(Jit* j)
if (R_SUCCEEDED(rc)) { if (R_SUCCEEDED(rc)) {
if (j->src_addr != NULL) { if (j->src_addr != NULL) {
free(j->src_addr); __libnx_free(j->src_addr);
j->src_addr = NULL; j->src_addr = NULL;
} }
} }

View File

@ -1,5 +1,4 @@
// Copyright 2017 plutoo // Copyright 2017 plutoo
#include <malloc.h>
#include "types.h" #include "types.h"
#include "result.h" #include "result.h"
#include "kernel/svc.h" #include "kernel/svc.h"

View File

@ -1,5 +1,4 @@
// Copyright 2017 plutoo // Copyright 2017 plutoo
#include <malloc.h>
#include <string.h> #include <string.h>
#include "types.h" #include "types.h"
#include "result.h" #include "result.h"
@ -11,6 +10,7 @@
#include "runtime/env.h" #include "runtime/env.h"
#include "runtime/diag.h" #include "runtime/diag.h"
#include "../internal.h" #include "../internal.h"
#include "../runtime/alloc.h"
#define USER_TLS_BEGIN 0x108 #define USER_TLS_BEGIN 0x108
#define USER_TLS_END (0x200 - sizeof(ThreadVars)) #define USER_TLS_END (0x200 - sizeof(ThreadVars))
@ -90,14 +90,6 @@ void __libnx_init_thread(void) {
getThreadVars()->thread_ptr = &g_mainThread; getThreadVars()->thread_ptr = &g_mainThread;
} }
void* __attribute__((weak)) __libnx_thread_alloc(size_t size) {
return memalign(0x1000, size);
}
void __attribute__((weak)) __libnx_thread_free(void* mem) {
free(mem);
}
Result threadCreate( Result threadCreate(
Thread* t, ThreadFunc entry, void* arg, void* stack_mem, size_t stack_sz, Thread* t, ThreadFunc entry, void* arg, void* stack_mem, size_t stack_sz,
int prio, int cpuid) int prio, int cpuid)
@ -109,7 +101,7 @@ Result threadCreate(
bool owns_stack_mem; bool owns_stack_mem;
if (stack_mem == NULL) { if (stack_mem == NULL) {
// Allocate new memory, stack then reent then tls. // Allocate new memory, stack then reent then tls.
stack_mem = __libnx_thread_alloc(((stack_sz + reent_sz + tls_sz) + 0xFFF) & ~0xFFF); stack_mem = __libnx_aligned_alloc(0x1000, stack_sz + reent_sz + tls_sz);
owns_stack_mem = true; owns_stack_mem = true;
} else { } else {
@ -190,7 +182,7 @@ Result threadCreate(
if (R_FAILED(rc)) { if (R_FAILED(rc)) {
if (owns_stack_mem) { if (owns_stack_mem) {
__libnx_thread_free(stack_mem); __libnx_free(stack_mem);
} }
} }
@ -248,7 +240,7 @@ Result threadClose(Thread* t) {
if (R_SUCCEEDED(rc)) { if (R_SUCCEEDED(rc)) {
if (t->owns_stack_mem) { if (t->owns_stack_mem) {
__libnx_thread_free(t->stack_mem); __libnx_free(t->stack_mem);
} }
svcCloseHandle(t->handle); svcCloseHandle(t->handle);
} }

View File

@ -1,12 +1,12 @@
// Copyright 2017 plutoo // Copyright 2017 plutoo
#include <string.h> #include <string.h>
#include <malloc.h>
#include "types.h" #include "types.h"
#include "result.h" #include "result.h"
#include "kernel/svc.h" #include "kernel/svc.h"
#include "kernel/tmem.h" #include "kernel/tmem.h"
#include "kernel/virtmem.h" #include "kernel/virtmem.h"
#include "runtime/diag.h" #include "runtime/diag.h"
#include "../runtime/alloc.h"
Result tmemCreate(TransferMemory* t, size_t size, Permission perm) Result tmemCreate(TransferMemory* t, size_t size, Permission perm)
{ {
@ -16,7 +16,7 @@ Result tmemCreate(TransferMemory* t, size_t size, Permission perm)
t->size = size; t->size = size;
t->perm = perm; t->perm = perm;
t->map_addr = NULL; t->map_addr = NULL;
t->src_addr = memalign(0x1000, size); t->src_addr = __libnx_aligned_alloc(0x1000, size);
if (t->src_addr == NULL) { if (t->src_addr == NULL) {
rc = MAKERESULT(Module_Libnx, LibnxError_OutOfMemory); rc = MAKERESULT(Module_Libnx, LibnxError_OutOfMemory);
@ -30,7 +30,7 @@ Result tmemCreate(TransferMemory* t, size_t size, Permission perm)
} }
if (R_FAILED(rc)) { if (R_FAILED(rc)) {
free(t->src_addr); __libnx_free(t->src_addr);
t->src_addr = NULL; t->src_addr = NULL;
} }
@ -114,7 +114,7 @@ Result tmemClose(TransferMemory* t)
} }
if (t->src_addr != NULL) { if (t->src_addr != NULL) {
free(t->src_addr); __libnx_free(t->src_addr);
} }
t->src_addr = NULL; t->src_addr = NULL;

View File

@ -5,7 +5,7 @@
#include "kernel/virtmem.h" #include "kernel/virtmem.h"
#include "kernel/random.h" #include "kernel/random.h"
#include "runtime/diag.h" #include "runtime/diag.h"
#include <stdlib.h> #include "../runtime/alloc.h"
#define SEQUENTIAL_GUARD_REGION_SIZE 0x1000 #define SEQUENTIAL_GUARD_REGION_SIZE 0x1000
#define RANDOM_MAX_ATTEMPTS 0x200 #define RANDOM_MAX_ATTEMPTS 0x200
@ -220,7 +220,7 @@ void* virtmemFindCodeMemory(size_t size, size_t guard_size) {
VirtmemReservation* virtmemAddReservation(void* mem, size_t size) { VirtmemReservation* virtmemAddReservation(void* mem, size_t size) {
if (!mutexIsLockedByCurrentThread(&g_VirtmemMutex)) return NULL; if (!mutexIsLockedByCurrentThread(&g_VirtmemMutex)) return NULL;
VirtmemReservation* rv = (VirtmemReservation*)malloc(sizeof(VirtmemReservation)); VirtmemReservation* rv = (VirtmemReservation*)__libnx_alloc(sizeof(VirtmemReservation));
if (rv) { if (rv) {
rv->region.start = (uintptr_t)mem; rv->region.start = (uintptr_t)mem;
rv->region.end = rv->region.start + size; rv->region.end = rv->region.start + size;
@ -241,4 +241,5 @@ void virtmemRemoveReservation(VirtmemReservation* rv) {
rv->prev->next = rv->next; rv->prev->next = rv->next;
else else
g_Reservations = rv->next; g_Reservations = rv->next;
__libnx_free(rv);
} }

View File

@ -1,4 +1,3 @@
#include <malloc.h>
#include "types.h" #include "types.h"
#include "result.h" #include "result.h"
#include "kernel/svc.h" #include "kernel/svc.h"

View File

@ -1,4 +1,3 @@
#include <malloc.h>
#include "types.h" #include "types.h"
#include "result.h" #include "result.h"
#include "kernel/svc.h" #include "kernel/svc.h"

View File

@ -1,4 +1,3 @@
#include <malloc.h>
#include "types.h" #include "types.h"
#include "result.h" #include "result.h"
#include "kernel/svc.h" #include "kernel/svc.h"

15
nx/source/runtime/alloc.c Normal file
View File

@ -0,0 +1,15 @@
#include "alloc.h"
#include <stdlib.h>
void* __attribute__((weak)) __libnx_alloc(size_t size) {
return malloc(size);
}
void* __attribute__((weak)) __libnx_aligned_alloc(size_t alignment, size_t size) {
size = (size + alignment - 1) &~ (alignment - 1);
return aligned_alloc(alignment, size);
}
void __attribute__((weak)) __libnx_free(void* p) {
free(p);
}

View File

@ -0,0 +1,6 @@
#pragma once
#include "types.h"
void* __libnx_alloc(size_t size);
void* __libnx_aligned_alloc(size_t alignment, size_t size);
void __libnx_free(void* p);

View File

@ -14,6 +14,7 @@
#include "runtime/env.h" #include "runtime/env.h"
#include "services/time.h" #include "services/time.h"
#include "../alloc.h"
#include "path_buf.h" #include "path_buf.h"
/*! @internal /*! @internal
@ -324,7 +325,7 @@ static int _fsdevMountDevice(const char *name, FsFileSystem fs, fsdev_fsdevice *
goto _fail; goto _fail;
device->setup = 1; device->setup = 1;
device->cwd = __nx_fsdev_support_cwd ? malloc(FS_MAX_PATH) : NULL; device->cwd = __nx_fsdev_support_cwd ? __libnx_alloc(FS_MAX_PATH) : NULL;
if(device->cwd!=NULL) if(device->cwd!=NULL)
{ {
device->cwd[0] = '/'; device->cwd[0] = '/';
@ -365,7 +366,7 @@ static int _fsdevUnmountDeviceStruct(fsdev_fsdevice *device)
strncat(name, ":", sizeof(name)-strlen(name)-1); strncat(name, ":", sizeof(name)-strlen(name)-1);
RemoveDevice(name); RemoveDevice(name);
free(device->cwd); __libnx_free(device->cwd);
fsFsClose(&device->fs); fsFsClose(&device->fs);
if(device->id == fsdev_fsdevice_cwd) if(device->id == fsdev_fsdevice_cwd)

View File

@ -14,6 +14,7 @@
#include "runtime/env.h" #include "runtime/env.h"
#include "nro.h" #include "nro.h"
#include "../alloc.h"
#include "path_buf.h" #include "path_buf.h"
typedef enum { typedef enum {
@ -240,10 +241,10 @@ static romfs_mount* romfs_alloc(void)
static void romfs_free(romfs_mount *mount) static void romfs_free(romfs_mount *mount)
{ {
free(mount->fileTable); __libnx_free(mount->fileTable);
free(mount->fileHashTable); __libnx_free(mount->fileHashTable);
free(mount->dirTable); __libnx_free(mount->dirTable);
free(mount->dirHashTable); __libnx_free(mount->dirHashTable);
_romfsResetMount(mount, mount->id); _romfsResetMount(mount, mount->id);
} }
@ -395,25 +396,25 @@ Result romfsMountCommon(const char *name, romfs_mount *mount)
if (_romfs_read(mount, 0, &mount->header, sizeof(mount->header)) != sizeof(mount->header)) if (_romfs_read(mount, 0, &mount->header, sizeof(mount->header)) != sizeof(mount->header))
goto fail_io; goto fail_io;
mount->dirHashTable = (u32*)malloc(mount->header.dirHashTableSize); mount->dirHashTable = (u32*)__libnx_alloc(mount->header.dirHashTableSize);
if (!mount->dirHashTable) if (!mount->dirHashTable)
goto fail_oom; goto fail_oom;
if (!_romfs_read_chk(mount, mount->header.dirHashTableOff, mount->dirHashTable, mount->header.dirHashTableSize)) if (!_romfs_read_chk(mount, mount->header.dirHashTableOff, mount->dirHashTable, mount->header.dirHashTableSize))
goto fail_io; goto fail_io;
mount->dirTable = malloc(mount->header.dirTableSize); mount->dirTable = __libnx_alloc(mount->header.dirTableSize);
if (!mount->dirTable) if (!mount->dirTable)
goto fail_oom; goto fail_oom;
if (!_romfs_read_chk(mount, mount->header.dirTableOff, mount->dirTable, mount->header.dirTableSize)) if (!_romfs_read_chk(mount, mount->header.dirTableOff, mount->dirTable, mount->header.dirTableSize))
goto fail_io; goto fail_io;
mount->fileHashTable = (u32*)malloc(mount->header.fileHashTableSize); mount->fileHashTable = (u32*)__libnx_alloc(mount->header.fileHashTableSize);
if (!mount->fileHashTable) if (!mount->fileHashTable)
goto fail_oom; goto fail_oom;
if (!_romfs_read_chk(mount, mount->header.fileHashTableOff, mount->fileHashTable, mount->header.fileHashTableSize)) if (!_romfs_read_chk(mount, mount->header.fileHashTableOff, mount->fileHashTable, mount->header.fileHashTableSize))
goto fail_io; goto fail_io;
mount->fileTable = malloc(mount->header.fileTableSize); mount->fileTable = __libnx_alloc(mount->header.fileTableSize);
if (!mount->fileTable) if (!mount->fileTable)
goto fail_oom; goto fail_oom;
if (!_romfs_read_chk(mount, mount->header.fileTableOff, mount->fileTable, mount->header.fileTableSize)) if (!_romfs_read_chk(mount, mount->header.fileTableOff, mount->fileTable, mount->header.fileTableSize))

View File

@ -4,7 +4,6 @@
#include <errno.h> #include <errno.h>
#include <alloca.h> #include <alloca.h>
#include <sys/iosupport.h> #include <sys/iosupport.h>
#include <malloc.h>
#include <fcntl.h> #include <fcntl.h>
#include <poll.h> #include <poll.h>
@ -19,6 +18,7 @@
#include "services/ssl.h" #include "services/ssl.h"
#include "runtime/devices/socket.h" #include "runtime/devices/socket.h"
#include "runtime/hosversion.h" #include "runtime/hosversion.h"
#include "../alloc.h"
__attribute__((weak)) size_t __nx_pollfd_sb_max_fds = 64; __attribute__((weak)) size_t __nx_pollfd_sb_max_fds = 64;
@ -308,7 +308,7 @@ int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struc
if(numfds <= __nx_pollfd_sb_max_fds) if(numfds <= __nx_pollfd_sb_max_fds)
pollinfo = (struct pollfd *)alloca(numfds * sizeof(struct pollfd)); pollinfo = (struct pollfd *)alloca(numfds * sizeof(struct pollfd));
else else
pollinfo = (struct pollfd *)malloc(numfds * sizeof(struct pollfd)); pollinfo = (struct pollfd *)__libnx_alloc(numfds * sizeof(struct pollfd));
if(pollinfo == NULL) { if(pollinfo == NULL) {
errno = ENOMEM; errno = ENOMEM;
return -1; return -1;
@ -379,7 +379,7 @@ int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struc
cleanup: cleanup:
if(numfds > __nx_pollfd_sb_max_fds) if(numfds > __nx_pollfd_sb_max_fds)
free(pollinfo); __libnx_free(pollinfo);
return rc; return rc;
} }
@ -396,7 +396,7 @@ int poll(struct pollfd *fds, nfds_t nfds, int timeout) {
if(nfds <= __nx_pollfd_sb_max_fds) if(nfds <= __nx_pollfd_sb_max_fds)
fds2 = (struct pollfd *)alloca(nfds * sizeof(struct pollfd)); fds2 = (struct pollfd *)alloca(nfds * sizeof(struct pollfd));
else else
fds2 = (struct pollfd *)malloc(nfds * sizeof(struct pollfd)); fds2 = (struct pollfd *)__libnx_alloc(nfds * sizeof(struct pollfd));
if(fds2 == NULL) { if(fds2 == NULL) {
errno = ENOMEM; errno = ENOMEM;
return -1; return -1;
@ -426,7 +426,7 @@ int poll(struct pollfd *fds, nfds_t nfds, int timeout) {
} }
if(nfds > __nx_pollfd_sb_max_fds) if(nfds > __nx_pollfd_sb_max_fds)
free(fds2); __libnx_free(fds2);
return ret; return ret;
} }
@ -629,7 +629,7 @@ int ioctl(int fd, int request, ...) {
return -1; return -1;
} }
struct bpf_program_serialized *prog_ser = (struct bpf_program_serialized *)malloc(sizeof(struct bpf_program_serialized)); struct bpf_program_serialized *prog_ser = (struct bpf_program_serialized *)__libnx_alloc(sizeof(struct bpf_program_serialized));
if(prog_ser == NULL) { if(prog_ser == NULL) {
errno = ENOMEM; errno = ENOMEM;
return -1; return -1;
@ -640,7 +640,7 @@ int ioctl(int fd, int request, ...) {
request = _IOC(request & IOC_DIRMASK, IOCGROUP(request), IOCBASECMD(request), sizeof(struct bpf_program_serialized)); request = _IOC(request & IOC_DIRMASK, IOCGROUP(request), IOCBASECMD(request), sizeof(struct bpf_program_serialized));
ret = bsdIoctl(fd, request, prog_ser); ret = bsdIoctl(fd, request, prog_ser);
free(prog_ser); __libnx_free(prog_ser);
return _socketParseBsdResult(NULL, ret); return _socketParseBsdResult(NULL, ret);
} }
default: default:
@ -967,7 +967,7 @@ static int _mmsgInitCommon(u8 **buf, size_t *alignsize, struct mmsghdr *msgvec,
} }
*alignsize = (bufsize+0xfff) & ~0xfff; *alignsize = (bufsize+0xfff) & ~0xfff;
*buf = (u8*)memalign(0x1000, *alignsize); *buf = (u8*)__libnx_aligned_alloc(0x1000, *alignsize);
if (*buf == NULL) { if (*buf == NULL) {
errno = ENOMEM; errno = ENOMEM;
return -1; return -1;
@ -1018,7 +1018,7 @@ int sendmmsg(int sockfd, struct mmsghdr *msgvec, unsigned int vlen, int flags) {
if (ret2==-1) ret = ret2; if (ret2==-1) ret = ret2;
} }
free(buf); __libnx_free(buf);
return ret; return ret;
} }
@ -1062,7 +1062,7 @@ int recvmmsg(int sockfd, struct mmsghdr *msgvec, unsigned int vlen, int flags, s
if (ret2==-1) ret = ret2; if (ret2==-1) ret = ret2;
} }
free(buf); __libnx_free(buf);
return ret; return ret;
} }

View File

@ -1,5 +1,4 @@
#include <string.h> #include <string.h>
#include <malloc.h>
#include "types.h" #include "types.h"
#include "result.h" #include "result.h"
#include "kernel/rwlock.h" #include "kernel/rwlock.h"
@ -7,6 +6,7 @@
#include "runtime/hosversion.h" #include "runtime/hosversion.h"
#include "runtime/diag.h" #include "runtime/diag.h"
#include "runtime/devices/usb_comms.h" #include "runtime/devices/usb_comms.h"
#include "../alloc.h"
#define TOTAL_INTERFACES 4 #define TOTAL_INTERFACES 4
@ -177,8 +177,8 @@ static void _usbCommsInterfaceFree(usbCommsInterface *interface)
interface->endpoint_out = NULL; interface->endpoint_out = NULL;
interface->interface = NULL; interface->interface = NULL;
free(interface->endpoint_in_buffer); __libnx_free(interface->endpoint_in_buffer);
free(interface->endpoint_out_buffer); __libnx_free(interface->endpoint_out_buffer);
interface->endpoint_in_buffer = NULL; interface->endpoint_in_buffer = NULL;
interface->endpoint_out_buffer = NULL; interface->endpoint_out_buffer = NULL;
@ -258,11 +258,11 @@ static Result _usbCommsInterfaceInit5x(u32 intf_ind, const UsbCommsInterfaceInfo
interface->initialized = 1; interface->initialized = 1;
//The buffer for PostBufferAsync commands must be 0x1000-byte aligned. //The buffer for PostBufferAsync commands must be 0x1000-byte aligned.
interface->endpoint_in_buffer = memalign(0x1000, 0x1000); interface->endpoint_in_buffer = __libnx_aligned_alloc(0x1000, 0x1000);
if (interface->endpoint_in_buffer==NULL) rc = MAKERESULT(Module_Libnx, LibnxError_OutOfMemory); if (interface->endpoint_in_buffer==NULL) rc = MAKERESULT(Module_Libnx, LibnxError_OutOfMemory);
if (R_SUCCEEDED(rc)) { if (R_SUCCEEDED(rc)) {
interface->endpoint_out_buffer = memalign(0x1000, 0x1000); interface->endpoint_out_buffer = __libnx_aligned_alloc(0x1000, 0x1000);
if (interface->endpoint_out_buffer==NULL) rc = MAKERESULT(Module_Libnx, LibnxError_OutOfMemory); if (interface->endpoint_out_buffer==NULL) rc = MAKERESULT(Module_Libnx, LibnxError_OutOfMemory);
} }
@ -360,11 +360,11 @@ static Result _usbCommsInterfaceInit1x(u32 intf_ind, const UsbCommsInterfaceInfo
interface->initialized = 1; interface->initialized = 1;
//The buffer for PostBufferAsync commands must be 0x1000-byte aligned. //The buffer for PostBufferAsync commands must be 0x1000-byte aligned.
interface->endpoint_in_buffer = memalign(0x1000, 0x1000); interface->endpoint_in_buffer = __libnx_aligned_alloc(0x1000, 0x1000);
if (interface->endpoint_in_buffer==NULL) rc = MAKERESULT(Module_Libnx, LibnxError_OutOfMemory); if (interface->endpoint_in_buffer==NULL) rc = MAKERESULT(Module_Libnx, LibnxError_OutOfMemory);
if (R_SUCCEEDED(rc)) { if (R_SUCCEEDED(rc)) {
interface->endpoint_out_buffer = memalign(0x1000, 0x1000); interface->endpoint_out_buffer = __libnx_aligned_alloc(0x1000, 0x1000);
if (interface->endpoint_out_buffer==NULL) rc = MAKERESULT(Module_Libnx, LibnxError_OutOfMemory); if (interface->endpoint_out_buffer==NULL) rc = MAKERESULT(Module_Libnx, LibnxError_OutOfMemory);
} }

View File

@ -18,6 +18,7 @@
#include "services/time.h" #include "services/time.h"
#include "runtime/diag.h" #include "runtime/diag.h"
#include "result.h" #include "result.h"
#include "alloc.h"
#define THRD_MAIN_HANDLE ((struct __pthread_t*)~(uintptr_t)0) #define THRD_MAIN_HANDLE ((struct __pthread_t*)~(uintptr_t)0)
@ -183,7 +184,7 @@ int __syscall_thread_create(struct __pthread_t **thread, void* (*func)(void*), v
if (R_FAILED(rc)) if (R_FAILED(rc))
return EPERM; return EPERM;
struct __pthread_t* t = (struct __pthread_t*)malloc(sizeof(struct __pthread_t)); struct __pthread_t* t = (struct __pthread_t*)__libnx_alloc(sizeof(struct __pthread_t));
if (!t) if (!t)
return ENOMEM; return ENOMEM;
@ -217,7 +218,7 @@ int __syscall_thread_create(struct __pthread_t **thread, void* (*func)(void*), v
_error2: _error2:
threadClose(&t->thr); threadClose(&t->thr);
_error1: _error1:
free(t); __libnx_free(t);
return ENOMEM; return ENOMEM;
} }
@ -232,7 +233,7 @@ void* __syscall_thread_join(struct __pthread_t *thread)
void* ret = thread->rc; void* ret = thread->rc;
threadClose(&thread->thr); threadClose(&thread->thr);
free(thread); __libnx_free(thread);
return ret; return ret;
} }

View File

@ -14,6 +14,7 @@
#include "services/nifm.h" #include "services/nifm.h"
#include "runtime/hosversion.h" #include "runtime/hosversion.h"
#include "runtime/resolver.h" #include "runtime/resolver.h"
#include "alloc.h"
__thread int h_errno; __thread int h_errno;
@ -126,7 +127,7 @@ static struct hostent *_resolverDeserializeHostent(const void *out_he_serialized
pos_addresses = pos; pos_addresses = pos;
pos += addrlen * nb_addresses; pos += addrlen * nb_addresses;
he = malloc( he = __libnx_alloc(
sizeof(struct hostent) sizeof(struct hostent)
+ name_size + name_size
+ 8 * (nb_aliases + 1 + nb_addresses + 1) + 8 * (nb_aliases + 1 + nb_addresses + 1)
@ -244,7 +245,7 @@ static struct addrinfo_serialized_hdr *_resolverSerializeAddrInfoList(size_t *ou
if (reqsize > g_resolverAddrInfoHintsBufferSize) if (reqsize > g_resolverAddrInfoHintsBufferSize)
return NULL; return NULL;
struct addrinfo_serialized_hdr *out = malloc(reqsize); struct addrinfo_serialized_hdr *out = __libnx_alloc(reqsize);
if (!out) if (!out)
return NULL; return NULL;
@ -268,7 +269,7 @@ static struct addrinfo *_resolverDeserializeAddrInfo(size_t *out_len, const stru
size_t subsize1 = hdr->ai_addrlen ? ntohl(hdr->ai_addrlen) : 4; size_t subsize1 = hdr->ai_addrlen ? ntohl(hdr->ai_addrlen) : 4;
size_t subsize2 = strlen((const char *)hdr + sizeof(struct addrinfo_serialized_hdr) + subsize1) + 1; size_t subsize2 = strlen((const char *)hdr + sizeof(struct addrinfo_serialized_hdr) + subsize1) + 1;
struct addrinfo_node *node = malloc(sizeof(struct addrinfo_node) + subsize2); struct addrinfo_node *node = __libnx_alloc(sizeof(struct addrinfo_node) + subsize2);
*out_len = sizeof(struct addrinfo_serialized_hdr) + subsize1 + subsize2; *out_len = sizeof(struct addrinfo_serialized_hdr) + subsize1 + subsize2;
if (!node) if (!node)
@ -348,13 +349,13 @@ static struct addrinfo *_resolverDeserializeAddrInfoList(struct addrinfo_seriali
} }
void freehostent(struct hostent *he) { void freehostent(struct hostent *he) {
free(he); __libnx_free(he);
} }
void freeaddrinfo(struct addrinfo *ai) { void freeaddrinfo(struct addrinfo *ai) {
for (struct addrinfo *node = ai, *next; node; node = next) { for (struct addrinfo *node = ai, *next; node; node = next) {
next = node->ai_next; next = node->ai_next;
free(node); __libnx_free(node);
} }
} }
@ -371,7 +372,7 @@ struct hostent *gethostbyname(const char *name) {
return NULL; return NULL;
} }
void *out_serialized = malloc(g_resolverHostByNameBufferSize); void *out_serialized = __libnx_alloc(g_resolverHostByNameBufferSize);
if (!out_serialized) { if (!out_serialized) {
h_errno = NETDB_INTERNAL; h_errno = NETDB_INTERNAL;
errno = ENOMEM; errno = ENOMEM;
@ -403,7 +404,7 @@ struct hostent *gethostbyname(const char *name) {
if (h_errno == NETDB_SUCCESS) if (h_errno == NETDB_SUCCESS)
ret = _resolverDeserializeHostent(out_serialized); ret = _resolverDeserializeHostent(out_serialized);
free(out_serialized); __libnx_free(out_serialized);
return ret; return ret;
} }
@ -426,7 +427,7 @@ struct hostent *gethostbyaddr(const void *addr, socklen_t len, int type) {
return NULL; return NULL;
} }
void *out_serialized = malloc(g_resolverHostByAddrBufferSize); void *out_serialized = __libnx_alloc(g_resolverHostByAddrBufferSize);
if (!out_serialized) { if (!out_serialized) {
h_errno = NETDB_INTERNAL; h_errno = NETDB_INTERNAL;
errno = ENOMEM; errno = ENOMEM;
@ -458,7 +459,7 @@ struct hostent *gethostbyaddr(const void *addr, socklen_t len, int type) {
if (h_errno == NETDB_SUCCESS) if (h_errno == NETDB_SUCCESS)
ret = _resolverDeserializeHostent(out_serialized); ret = _resolverDeserializeHostent(out_serialized);
free(out_serialized); __libnx_free(out_serialized);
return ret; return ret;
} }
@ -508,9 +509,9 @@ int getaddrinfo(const char *node, const char *service, const struct addrinfo *hi
} }
} }
struct addrinfo_serialized_hdr *out_serialized = malloc(g_resolverAddrInfoBufferSize); struct addrinfo_serialized_hdr *out_serialized = __libnx_alloc(g_resolverAddrInfoBufferSize);
if (!out_serialized) { if (!out_serialized) {
free(hints_serialized); __libnx_free(hints_serialized);
errno = ENOMEM; errno = ENOMEM;
return EAI_FAIL; return EAI_FAIL;
} }
@ -528,7 +529,7 @@ int getaddrinfo(const char *node, const char *service, const struct addrinfo *hi
NULL); NULL);
g_resolverResult = rc; g_resolverResult = rc;
g_resolverCancelHandle = 0; g_resolverCancelHandle = 0;
free(hints_serialized); __libnx_free(hints_serialized);
if (R_FAILED(rc)) { if (R_FAILED(rc)) {
if (R_MODULE(rc) == 21) // SM if (R_MODULE(rc) == 21) // SM
@ -548,7 +549,7 @@ int getaddrinfo(const char *node, const char *service, const struct addrinfo *hi
} }
} }
free(out_serialized); __libnx_free(out_serialized);
return ret; return ret;
} }

View File

@ -1,7 +1,7 @@
#include <string.h> #include <string.h>
#include <malloc.h>
#include "runtime/ringcon.h" #include "runtime/ringcon.h"
#include "arm/counter.h" #include "arm/counter.h"
#include "alloc.h"
static Result _ringconSetup(RingCon *c); static Result _ringconSetup(RingCon *c);
@ -62,7 +62,7 @@ Result ringconCreate(RingCon *c, HidNpadIdType id) {
memset(c, 0, sizeof(*c)); memset(c, 0, sizeof(*c));
c->workbuf_size = 0x1000; c->workbuf_size = 0x1000;
c->workbuf = memalign(0x1000, c->workbuf_size); c->workbuf = __libnx_aligned_alloc(0x1000, c->workbuf_size);
if (c->workbuf == NULL) if (c->workbuf == NULL)
rc = MAKERESULT(Module_Libnx, LibnxError_OutOfMemory); rc = MAKERESULT(Module_Libnx, LibnxError_OutOfMemory);
else else
@ -101,7 +101,7 @@ void ringconClose(RingCon *c) {
hidbusFinalize(c->handle); hidbusFinalize(c->handle);
} }
free(c->workbuf); __libnx_free(c->workbuf);
c->workbuf = 0; c->workbuf = 0;
} }

View File

@ -2,7 +2,6 @@
#include "service_guard.h" #include "service_guard.h"
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include <malloc.h>
#include <stdatomic.h> #include <stdatomic.h>
#include "kernel/shmem.h" #include "kernel/shmem.h"
#include "kernel/mutex.h" #include "kernel/mutex.h"
@ -11,6 +10,7 @@
#include "services/hid.h" #include "services/hid.h"
#include "runtime/hosversion.h" #include "runtime/hosversion.h"
#include "runtime/diag.h" #include "runtime/diag.h"
#include "../runtime/alloc.h"
static Service g_hidSrv; static Service g_hidSrv;
static Service g_hidIAppletResource; static Service g_hidIAppletResource;
@ -1410,7 +1410,7 @@ Result hidInitializeSevenSixAxisSensor(void) {
if (R_FAILED(rc)) return rc; if (R_FAILED(rc)) return rc;
} }
g_sevenSixAxisSensorBuffer = (u8*)memalign(0x1000, bufsize); g_sevenSixAxisSensorBuffer = (u8*)__libnx_aligned_alloc(0x1000, bufsize);
if (g_sevenSixAxisSensorBuffer == NULL) if (g_sevenSixAxisSensorBuffer == NULL)
return MAKERESULT(Module_Libnx, LibnxError_OutOfMemory); return MAKERESULT(Module_Libnx, LibnxError_OutOfMemory);
memset(g_sevenSixAxisSensorBuffer, 0, bufsize); memset(g_sevenSixAxisSensorBuffer, 0, bufsize);
@ -1424,7 +1424,7 @@ Result hidInitializeSevenSixAxisSensor(void) {
tmemClose(&g_sevenSixAxisSensorTmem0); tmemClose(&g_sevenSixAxisSensorTmem0);
tmemClose(&g_sevenSixAxisSensorTmem1); tmemClose(&g_sevenSixAxisSensorTmem1);
free(g_sevenSixAxisSensorBuffer); __libnx_free(g_sevenSixAxisSensorBuffer);
g_sevenSixAxisSensorBuffer = NULL; g_sevenSixAxisSensorBuffer = NULL;
} }
@ -1444,7 +1444,7 @@ Result hidFinalizeSevenSixAxisSensor(void) {
tmemClose(&g_sevenSixAxisSensorTmem0); tmemClose(&g_sevenSixAxisSensorTmem0);
tmemClose(&g_sevenSixAxisSensorTmem1); tmemClose(&g_sevenSixAxisSensorTmem1);
free(g_sevenSixAxisSensorBuffer); __libnx_free(g_sevenSixAxisSensorBuffer);
g_sevenSixAxisSensorBuffer = NULL; g_sevenSixAxisSensorBuffer = NULL;
return rc; return rc;