-
Notifications
You must be signed in to change notification settings - Fork 46
/
posix-compat.c
71 lines (65 loc) · 1.5 KB
/
posix-compat.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/* posix-compat.c -- POSIX functions for libsubstitute to use
*
* Copyright (C) 2016 Yifan Lu
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
#include <psp2kern/types.h>
#include <psp2kern/kernel/sysmem.h>
#include <stdlib.h>
#include <string.h>
/** From `patches.c`. Assumes it is initialized before first `malloc` call! */
extern SceUID g_patch_pool;
/**
* @brief Allocates heap memory from default pool
*
* @param[in] size The size
*
* @return See `malloc()`
*/
void *malloc(size_t size) {
void *ptr;
ptr = ksceKernelAllocHeapMemory(g_patch_pool, size + sizeof(size_t));
if (ptr) {
*(size_t *)ptr = size;
ptr = (char *)ptr + sizeof(size_t);
}
return ptr;
}
/**
* @brief Frees heap memory from default pool
*
* @param ptr The pointer
*/
void free(void *ptr) {
ksceKernelFreeHeapMemory(g_patch_pool, (char *)ptr - sizeof(size_t));
}
/**
* @brief Reallocates memory to new size
*
* @param ptr The pointer
* @param[in] size The size
*
* @return See `realloc()`
*/
void *realloc(void *ptr, size_t size) {
void *dup;
size_t oldsize;
dup = malloc(size);
if (dup) {
oldsize = *(size_t *)((char *)ptr - sizeof(size_t));
if (oldsize > size) {
oldsize = size;
}
memcpy(dup, ptr, oldsize);
free(ptr);
}
return dup;
}
/**
* @brief Crashes kernel with unrecoverable error
*/
void __attribute__((naked)) abort(void) {
asm ("bkpt #0");
}