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

Memstuff #1164

Merged
merged 22 commits into from
May 1, 2022
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
d389eda
Un-fake a couple of matches in memory manip functions
EllipticEllipsis Jan 17, 2022
14c1ea0
Document fmodf
EllipticEllipsis Jan 17, 2022
ddd272e
Un-fake a couple of matches in memory manip functions
EllipticEllipsis Jan 17, 2022
c4f3126
Document fmodf
EllipticEllipsis Jan 17, 2022
71c356f
Rename functions and files
EllipticEllipsis Feb 28, 2022
b1dcb3b
Document memmove, memsets, memcpys
EllipticEllipsis Feb 28, 2022
c160a55
Format
EllipticEllipsis Feb 28, 2022
97f7dad
Sort out some missing sizeofs
EllipticEllipsis Feb 28, 2022
e239738
Name fmodf
EllipticEllipsis Feb 28, 2022
46d2c8c
Rename local variables
EllipticEllipsis Mar 1, 2022
266ea99
Merge remote-tracking branch 'upstream/master' into memstuff
EllipticEllipsis Mar 1, 2022
6101e8c
size_t
EllipticEllipsis Mar 1, 2022
4a78ba0
Use COBRA_SHADOW_TEX_SIZE
EllipticEllipsis Mar 6, 2022
05cdded
Review
EllipticEllipsis Mar 9, 2022
3a78e96
Merge remote-tracking branch 'upstream/master' into memstuff
EllipticEllipsis Apr 18, 2022
b73d8e2
Merge remote-tracking branch 'upstream/master' into memstuff
EllipticEllipsis Apr 28, 2022
4119d5c
Merge branch 'memstuff' of github.com:EllipticEllipsis/oot into memstuff
EllipticEllipsis Apr 30, 2022
005ae65
Merge branch 'memstuff' of github.com:EllipticEllipsis/oot into memstuff
EllipticEllipsis Apr 30, 2022
25a21a2
Tweak the Doxyfile to remove @brief requirement
EllipticEllipsis May 1, 2022
9234dc4
Roman's review
EllipticEllipsis May 1, 2022
a2f952b
Fix a bug comment
EllipticEllipsis May 1, 2022
a2b3ff9
Change fmodf
EllipticEllipsis May 1, 2022
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
2 changes: 1 addition & 1 deletion Doxyfile
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ SHORT_NAMES = NO
# description.)
# The default value is: NO.

JAVADOC_AUTOBRIEF = NO
JAVADOC_AUTOBRIEF = YES

# If the QT_AUTOBRIEF tag is set to YES then doxygen will interpret the first
# line (until the first dot) of a Qt-style comment as the brief description. If
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,9 @@ build/src/code/fault.o: OPTFLAGS := -O2 -g3
build/src/code/fault_drawer.o: CFLAGS += -trapuv
build/src/code/fault_drawer.o: OPTFLAGS := -O2 -g3
build/src/code/ucode_disas.o: OPTFLAGS := -O2 -g3
build/src/code/code_801068B0.o: OPTFLAGS := -g
build/src/code/code_80106860.o: OPTFLAGS := -g
build/src/code/fmodf.o: OPTFLAGS := -g
build/src/code/__osMemset.o: OPTFLAGS := -g
build/src/code/__osMemmove.o: OPTFLAGS := -g

build/src/libultra/libc/absf.o: OPTFLAGS := -O2 -g3
build/src/libultra/libc/sqrt.o: OPTFLAGS := -O2 -g3
Expand Down
10 changes: 5 additions & 5 deletions include/functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ void osUnmapTLBAll(void);
s32 osEPiStartDma(OSPiHandle* handle, OSIoMesg* mb, s32 direction);
const char* strchr(const char* str, s32 ch);
u32 strlen(const char* str);
void* memcpy(void* dst, const void* src, u32 size);
void* memcpy(void* dst, const void* src, size_t size);
void osInvalICache(void* vaddr, s32 nbytes);
void osCreateMesgQueue(OSMesgQueue* mq, OSMesg* msg, s32 count);
void osInvalDCache(void* vaddr, s32 nbytes);
Expand Down Expand Up @@ -831,7 +831,7 @@ void func_800645A0(GlobalContext* globalCtx, CutsceneContext* csCtx);
void Cutscene_HandleEntranceTriggers(GlobalContext* globalCtx);
void Cutscene_HandleConditionalTriggers(GlobalContext* globalCtx);
void Cutscene_SetSegment(GlobalContext* globalCtx, void* segment);
void* MemCopy(void* dest, void* src, s32 size);
void* MemCpy(void* dest, const void* src, s32 len);
void GetItem_Draw(GlobalContext* globalCtx, s16 drawId);
void SoundSource_InitAll(GlobalContext* globalCtx);
void SoundSource_UpdateAll(GlobalContext* globalCtx);
Expand Down Expand Up @@ -910,7 +910,7 @@ s32 Environment_IsForcedSequenceDisabled(void);
void Environment_PlayStormNatureAmbience(GlobalContext* globalCtx);
void Environment_StopStormNatureAmbience(GlobalContext* globalCtx);
void Environment_WarpSongLeave(GlobalContext* globalCtx);
void Lib_MemSet(u8* dest, size_t size, u8 val);
void Lib_MemSet(u8* dest, size_t len, u8 val);
f32 Math_CosS(s16 angle);
f32 Math_SinS(s16 angle);
s32 Math_ScaledStepToS(s16* pValue, s16 target, s16 step);
Expand Down Expand Up @@ -2281,8 +2281,8 @@ void guMtxF2L(MtxF* m1, Mtx* m2);
u32* osViGetCurrentFramebuffer(void);
s32 __osSpSetPc(void* pc);
f32 absf(f32);
void* func_80106860(void* ptr, s32 val, size_t size);
void* func_801068B0(void* dst, void* src, size_t size);
void* __osMemset(void* dest, s32 val, size_t len);
void* __osMemmove(void* dest, const void* src, size_t len);
void Message_UpdateOcarinaGame(GlobalContext* globalCtx);
u8 Message_ShouldAdvance(GlobalContext* globalCtx);
void Message_CloseTextbox(GlobalContext*);
Expand Down
4 changes: 2 additions & 2 deletions spec
Original file line number Diff line number Diff line change
Expand Up @@ -510,8 +510,8 @@ beginseg
include "build/src/libultra/libc/sqrt.o"
include "build/src/libultra/libc/absf.o"
include "build/src/code/fmodf.o"
include "build/src/code/code_80106860.o"
include "build/src/code/code_801068B0.o"
include "build/src/code/__osMemset.o"
include "build/src/code/__osMemmove.o"
include_data_with_rodata "build/src/code/z_message_PAL.o"
include "build/src/code/z_game_over.o"
include "build/src/code/z_construct.o"
Expand Down
26 changes: 13 additions & 13 deletions src/code/__osMalloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ void __osMallocAddBlock(Arena* arena, void* start, s32 size) {
size2 = (size - diff) & ~0xF;

if (size2 > (s32)sizeof(ArenaNode)) {
func_80106860(firstNode, BLOCK_UNINIT_MAGIC, size2); // memset
__osMemset(firstNode, BLOCK_UNINIT_MAGIC, size2);
firstNode->next = NULL;
firstNode->prev = NULL;
firstNode->size = size2 - sizeof(ArenaNode);
Expand Down Expand Up @@ -151,7 +151,7 @@ void ArenaImpl_RemoveAllBlocks(Arena* arena) {
iter = arena->head;
while (iter != NULL) {
next = ArenaImpl_GetNextBlock(iter);
func_80106860(iter, BLOCK_UNINIT_MAGIC, iter->size + sizeof(ArenaNode)); // memset
__osMemset(iter, BLOCK_UNINIT_MAGIC, iter->size + sizeof(ArenaNode));
iter = next;
}

Expand Down Expand Up @@ -228,7 +228,7 @@ void* __osMalloc_NoLockDebug(Arena* arena, u32 size, const char* file, s32 line)
ArenaImpl_SetDebugInfo(iter, file, line, arena);
alloc = (void*)((u32)iter + sizeof(ArenaNode));
if (arena->flag & FILL_ALLOCBLOCK) {
func_80106860(alloc, BLOCK_ALLOC_MAGIC, size);
__osMemset(alloc, BLOCK_ALLOC_MAGIC, size);
}

break;
Expand Down Expand Up @@ -288,7 +288,7 @@ void* __osMallocRDebug(Arena* arena, u32 size, const char* file, s32 line) {
ArenaImpl_SetDebugInfo(iter, file, line, arena);
allocR = (void*)((u32)iter + sizeof(ArenaNode));
if (arena->flag & FILL_ALLOCBLOCK) {
func_80106860(allocR, BLOCK_ALLOC_MAGIC, size);
__osMemset(allocR, BLOCK_ALLOC_MAGIC, size);
}

break;
Expand Down Expand Up @@ -339,7 +339,7 @@ void* __osMalloc_NoLock(Arena* arena, u32 size) {
ArenaImpl_SetDebugInfo(iter, NULL, 0, arena);
alloc = (void*)((u32)iter + sizeof(ArenaNode));
if (arena->flag & FILL_ALLOCBLOCK) {
func_80106860(alloc, BLOCK_ALLOC_MAGIC, size);
__osMemset(alloc, BLOCK_ALLOC_MAGIC, size);
}
break;
}
Expand Down Expand Up @@ -398,7 +398,7 @@ void* __osMallocR(Arena* arena, u32 size) {
ArenaImpl_SetDebugInfo(iter, NULL, 0, arena);
alloc = (void*)((u32)iter + sizeof(ArenaNode));
if (arena->flag & FILL_ALLOCBLOCK) {
func_80106860(alloc, BLOCK_ALLOC_MAGIC, size);
__osMemset(alloc, BLOCK_ALLOC_MAGIC, size);
}
break;
}
Expand Down Expand Up @@ -442,7 +442,7 @@ void __osFree_NoLock(Arena* arena, void* ptr) {
ArenaImpl_SetDebugInfo(node, NULL, 0, arena);

if (arena->flag & FILL_FREEBLOCK) {
func_80106860((u32)node + sizeof(ArenaNode), BLOCK_FREE_MAGIC, node->size);
__osMemset((u32)node + sizeof(ArenaNode), BLOCK_FREE_MAGIC, node->size);
}

newNext = next;
Expand All @@ -454,7 +454,7 @@ void __osFree_NoLock(Arena* arena, void* ptr) {

node->size += next->size + sizeof(ArenaNode);
if (arena->flag & FILL_FREEBLOCK) {
func_80106860(next, BLOCK_FREE_MAGIC, sizeof(ArenaNode));
__osMemset(next, BLOCK_FREE_MAGIC, sizeof(ArenaNode));
}
node->next = newNext;
next = newNext;
Expand All @@ -467,7 +467,7 @@ void __osFree_NoLock(Arena* arena, void* ptr) {
prev->next = next;
prev->size += node->size + sizeof(ArenaNode);
if (arena->flag & FILL_FREEBLOCK) {
func_80106860(node, BLOCK_FREE_MAGIC, sizeof(ArenaNode));
__osMemset(node, BLOCK_FREE_MAGIC, sizeof(ArenaNode));
}
}
}
Expand Down Expand Up @@ -512,7 +512,7 @@ void __osFree_NoLockDebug(Arena* arena, void* ptr, const char* file, s32 line) {
ArenaImpl_SetDebugInfo(node, file, line, arena);

if (arena->flag & FILL_FREEBLOCK) {
func_80106860((u32)node + sizeof(ArenaNode), BLOCK_FREE_MAGIC, node->size);
__osMemset((u32)node + sizeof(ArenaNode), BLOCK_FREE_MAGIC, node->size);
}

newNext = node->next;
Expand All @@ -524,7 +524,7 @@ void __osFree_NoLockDebug(Arena* arena, void* ptr, const char* file, s32 line) {

node->size += next->size + sizeof(ArenaNode);
if (arena->flag & FILL_FREEBLOCK) {
func_80106860(next, BLOCK_FREE_MAGIC, sizeof(ArenaNode));
__osMemset(next, BLOCK_FREE_MAGIC, sizeof(ArenaNode));
}
node->next = newNext;
next = newNext;
Expand All @@ -537,7 +537,7 @@ void __osFree_NoLockDebug(Arena* arena, void* ptr, const char* file, s32 line) {
prev->next = next;
prev->size += node->size + sizeof(ArenaNode);
if (arena->flag & FILL_FREEBLOCK) {
func_80106860(node, BLOCK_FREE_MAGIC, sizeof(ArenaNode));
__osMemset(node, BLOCK_FREE_MAGIC, sizeof(ArenaNode));
}
}
}
Expand Down Expand Up @@ -590,7 +590,7 @@ void* __osRealloc(Arena* arena, void* ptr, u32 newSize) {
}
node->next = newNext;
node->size = newSize;
func_801068B0(newNext, next, sizeof(ArenaNode)); // memcpy
__osMemmove(newNext, next, sizeof(ArenaNode));
} else {
// "Allocate a new memory block and move the contents"
osSyncPrintf("新たにメモリブロックを確保して内容を移動します\n");
Expand Down
33 changes: 33 additions & 0 deletions src/code/__osMemmove.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include "global.h"

/**
* memmove: copies `len` bytes from memory starting at `src` to memory starting at `dest`.
*
* Unlike memcpy(), the regions of memory may overlap.
*
* @param dest address of start of buffer to write to
* @param src address of start of buffer to read from
* @param len number of bytes to copy.
*
* @return dest
*/
void* __osMemmove(void* dest, const void* src, size_t len) {
u8* d = dest;
const u8* s = src;

if (d == s) {
return dest;
}
if (d < s) {
while (len--) {
*d++ = *s++;
}
} else {
d += len - 1;
s += len - 1;
while (len--) {
*d-- = *s--;
}
}
return dest;
}
21 changes: 21 additions & 0 deletions src/code/__osMemset.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include "global.h"

/**
* memset: sets `len` bytes to `val` starting at address `dest`.
*
* @see There are two other memsets in this codebase, Lib_MemSet(), MemSet()
*
* @param dest address to start at
* @param val value to write (s32, but interpreted as u8)
* @param len number of bytes to write
*
* @return dest
*/
void* __osMemset(void* dest, s32 val, size_t len) {
u8* ptr = dest;

while (len--) {
*ptr++ = val;
}
return dest;
}
2 changes: 1 addition & 1 deletion src/code/audio_heap.c
Original file line number Diff line number Diff line change
Expand Up @@ -1021,7 +1021,7 @@ void* AudioHeap_AllocPermanent(s32 tableType, s32 id, u32 size) {
gAudioContext.permanentCache[index].size = size;

//! @bug UB: missing return. "ret" is in v0 at this point, but doing an
// explicit return uses an additional register.
//! explicit return uses an additional register.
#ifdef AVOID_UB
return ret;
#endif
Expand Down
44 changes: 34 additions & 10 deletions src/code/code_80069420.c
Original file line number Diff line number Diff line change
@@ -1,23 +1,47 @@
#include "global.h"

void* MemCopy(void* dest, void* src, s32 size) {
u8* destu = (u8*)dest;
u8* srcu = (u8*)src;
/**
* memcpy: copies `len` bytes from memory starting at `src` to memory starting at `dest`. Expects the memory
* specified by `src` and `dest` to not overlap.
*
* @see libultra also has a memcpy().
*
* @param dest address of start of buffer writing to
* @param src address of start of buffer to read from
* @param len number of bytes to copy. (`s32` rather than the standard `size_t`)
*
* @return dest
*/
void* MemCpy(void* dest, const void* src, s32 len) {
u8* d = dest;
const u8* s = src;

while (size > 0) {
*destu++ = *srcu++;
size--;
while (len > 0) {
*d++ = *s++;
len--;
}

return dest;
}

void* MemSet(void* dest, s32 val, s32 size) {
u8* destu = (u8*)dest;
s32 s = size;
/**
* memset: sets `len` bytes to `val` starting at address `dest`.
*
* @see There are two other memsets in this codebase, Lib_MemSet(), __osMemset().
* This one is unused.
*
* @param dest address to start at
* @param val value to write (`s32`, but interpreted as `u8`)
* @param len number of bytes to write. (`s32` rather than the standard `size_t`)
*
* @return dest
*/
void* MemSet(void* dest, s32 val, s32 len) {
u8* d = dest;
s32 s = len;

while (s > 0) {
*destu++ = val;
*d++ = val;
s--;
}

Expand Down
12 changes: 0 additions & 12 deletions src/code/code_80106860.c

This file was deleted.

24 changes: 0 additions & 24 deletions src/code/code_801068B0.c

This file was deleted.

21 changes: 18 additions & 3 deletions src/code/fmodf.c
Original file line number Diff line number Diff line change
@@ -1,12 +1,27 @@
#include "global.h"

/**
* @brief Computes one @p x modulo @p y for floats.
EllipticEllipsis marked this conversation as resolved.
Show resolved Hide resolved
*
* Acts like the standard C fmodf except does not handle Infinity. See https://en.cppreference.com/w/c/numeric/math/fmod
* for the details. It summarizes this function as follows:
* "The floating-point remainder of the division operation x/y calculated by this function is exactly the value x - n*y,
* where n is x/y with its fractional part truncated.
*
* The returned value has the same sign as x and is less or equal to y in magnitude."
*
* @param x dividend
* @param y modulus
*
* @return f32 0.0f if y is 0.0f, or x modulo y otherwise
*/
f32 fmodf(f32 x, f32 y) {
s32 quot;
s32 n;

if (y == 0.0f) {
return 0.0f;
}
quot = x / y;
n = x / y;

return x - (quot * y);
return x - (n * y);
}
2 changes: 1 addition & 1 deletion src/code/z_camera.c
Original file line number Diff line number Diff line change
Expand Up @@ -6793,7 +6793,7 @@ void Camera_Init(Camera* camera, View* view, CollisionContext* colCtx, GlobalCon
s16 curUID;
s16 j;

func_80106860(camera, 0, sizeof(*camera));
__osMemset(camera, 0, sizeof(Camera));
if (sInitRegs) {
for (i = 0; i < sOREGInitCnt; i++) {
OREG(i) = sOREGInit[i];
Expand Down
Loading