From: John Hodge Date: Sat, 6 Aug 2011 00:37:58 +0000 (+0800) Subject: Kernel - Cleaning up unneeded malloc() calls X-Git-Tag: rel0.10~8 X-Git-Url: https://git.ucc.asn.au/?a=commitdiff_plain;h=0f8226381e0a19e8e8e11eafbf4589532e45d430;p=tpg%2Facess2.git Kernel - Cleaning up unneeded malloc() calls - Also debug cleanups - Constness fix in RingBuffer --- diff --git a/Kernel/adt.c b/Kernel/adt.c index 0f116f3c..77c8d995 100644 --- a/Kernel/adt.c +++ b/Kernel/adt.c @@ -1,8 +1,13 @@ /* + * Acess2 Kernel + * + * adt.c + * - Complex data type code */ #include #include + // === CODE === // --- Ring Buffers --- tRingBuffer *RingBuffer_Create(size_t Space) @@ -29,11 +34,13 @@ size_t RingBuffer_Read(void *Dest, tRingBuffer *Buffer, size_t Length) memcpy(Dest, &Buffer->Data[Buffer->Start], Length); } Buffer->Start += Length; + if( Buffer->Start > Buffer->Space ) + Buffer->Start -= Buffer->Space; Buffer->Length -= Length; return Length; } -size_t RingBuffer_Write(tRingBuffer *Buffer, void *Source, size_t Length) +size_t RingBuffer_Write(tRingBuffer *Buffer, const void *Source, size_t Length) { size_t bufEnd = Buffer->Start + Buffer->Length; size_t endSpace = Buffer->Space - bufEnd; diff --git a/Kernel/arch/x86/lib.c b/Kernel/arch/x86/lib.c index a3f98be5..0d493bb1 100644 --- a/Kernel/arch/x86/lib.c +++ b/Kernel/arch/x86/lib.c @@ -15,6 +15,7 @@ // === IMPRORTS === #if TRACE_LOCKS extern struct sShortSpinlock glDebug_Lock; +extern struct sShortSpinlock glThreadListLock; #endif extern int GetCPUNum(void); @@ -94,6 +95,14 @@ void SHORTLOCK(struct sShortSpinlock *Lock) } #endif + #if TRACE_LOCKS + if( Lock != &glDebug_Lock && Lock != &glThreadListLock ) + { + //Log_Log("LOCK", "%p locked by %p", Lock, __builtin_return_address(0)); + Debug("%p obtaining %p (Called by %p)", __builtin_return_address(0), Lock, __builtin_return_address(1)); + } + #endif + // Wait for another CPU to release while(v) { // CMPXCHG: @@ -124,10 +133,11 @@ void SHORTLOCK(struct sShortSpinlock *Lock) #endif #if TRACE_LOCKS - if( Lock != &glDebug_Lock ) + if( Lock != &glDebug_Lock && Lock != &glThreadListLock ) { //Log_Log("LOCK", "%p locked by %p", Lock, __builtin_return_address(0)); - LogF("Lock %p locked by %p\n", Lock, __builtin_return_address(0)); + //Debug("Lock %p locked by %p\t%p", Lock, __builtin_return_address(0), __builtin_return_address(1)); + Debug("got it"); } #endif } @@ -145,10 +155,10 @@ void SHORTREL(struct sShortSpinlock *Lock) #endif #if TRACE_LOCKS - if( Lock != &glDebug_Lock ) + if( Lock != &glDebug_Lock && Lock != &glThreadListLock ) { //Log_Log("LOCK", "%p released by %p", Lock, __builtin_return_address(0)); - LogF("Lock %p released by %p\n", Lock, __builtin_return_address(0)); + Debug("Lock %p released by %p\t%p", Lock, __builtin_return_address(0), __builtin_return_address(1)); } #endif diff --git a/Kernel/arch/x86/proc.c b/Kernel/arch/x86/proc.c index 3ae79bf8..89335291 100644 --- a/Kernel/arch/x86/proc.c +++ b/Kernel/arch/x86/proc.c @@ -382,7 +382,7 @@ void ArchThreads_Init(void) // Create Per-Process Data Block if( !MM_Allocate(MM_PPD_CFG) ) { - Panic("OOM - No space for initiali Per-Process Config"); + Panic("OOM - No space for initial Per-Process Config"); } // Change Stacks diff --git a/Kernel/debug.c b/Kernel/debug.c index 76ed7cfb..e66b9a9f 100644 --- a/Kernel/debug.c +++ b/Kernel/debug.c @@ -148,7 +148,6 @@ void Debug(const char *Fmt, ...) va_start(args, Fmt); Debug_DbgOnlyFmt(Fmt, args); va_end(args); - Debug_PutCharDebug('\r'); Debug_PutCharDebug('\n'); #if LOCK_DEBUG_OUTPUT SHORTREL(&glDebug_Lock); @@ -169,7 +168,6 @@ void Log(const char *Fmt, ...) va_start(args, Fmt); Debug_Fmt(Fmt, args); va_end(args); - Debug_Putchar('\r'); Debug_Putchar('\n'); #if LOCK_DEBUG_OUTPUT @@ -188,7 +186,6 @@ void Warning(const char *Fmt, ...) va_start(args, Fmt); Debug_Fmt(Fmt, args); va_end(args); - Debug_Putchar('\r'); Debug_Putchar('\n'); #if LOCK_DEBUG_OUTPUT @@ -210,7 +207,6 @@ void Panic(const char *Fmt, ...) va_start(args, Fmt); Debug_Fmt(Fmt, args); va_end(args); - Debug_Putchar('\r'); Debug_Putchar('\n'); Threads_Dump(); @@ -289,7 +285,7 @@ void Debug_Enter(const char *FuncName, const char *ArgTypes, ...) } va_end(args); - Debug_Putchar(')'); Debug_Putchar('\r'); Debug_Putchar('\n'); + Debug_Putchar(')'); Debug_Putchar('\n'); #if LOCK_DEBUG_OUTPUT SHORTREL(&glDebug_Lock); @@ -317,7 +313,6 @@ void Debug_Log(const char *FuncName, const char *Fmt, ...) Debug_Fmt(Fmt, args); va_end(args); - Debug_Putchar('\r'); Debug_Putchar('\n'); #if LOCK_DEBUG_OUTPUT @@ -353,7 +348,6 @@ void Debug_Leave(const char *FuncName, char RetType, ...) // No Return if(RetType == '-') { - Debug_Putchar('\r'); Debug_Putchar('\n'); #if LOCK_DEBUG_OUTPUT SHORTREL(&glDebug_Lock); @@ -373,7 +367,6 @@ void Debug_Leave(const char *FuncName, char RetType, ...) // Extended (64-Bit) case 'X': Debug_Fmt("0x%llx", args); break; } - Debug_Putchar('\r'); Debug_Putchar('\n'); va_end(args); diff --git a/Kernel/heap.c b/Kernel/heap.c index 262053b0..a9e9931f 100644 --- a/Kernel/heap.c +++ b/Kernel/heap.c @@ -212,9 +212,10 @@ void *Heap_Allocate(const char *File, int Line, size_t __Bytes) head->File = File; head->Line = Line; head->ValidSize = __Bytes; + head->AllocateTime = now(); Mutex_Release(&glHeap); // Release spinlock #if DEBUG_TRACE - Log("[Heap ] Malloc'd %p (%i bytes), returning to %p", head->Data, head->Size, __builtin_return_address(0)); + Debug("[Heap ] Malloc'd %p (%i bytes), returning to %p", head->Data, head->Size, __builtin_return_address(0)); #endif return head->Data; } @@ -247,10 +248,11 @@ void *Heap_Allocate(const char *File, int Line, size_t __Bytes) best->Magic = MAGIC_USED; // Mark block as used best->File = File; best->Line = Line; - head->ValidSize = __Bytes; + best->ValidSize = __Bytes; + best->AllocateTime = now(); Mutex_Release(&glHeap); // Release spinlock #if DEBUG_TRACE - Log("[Heap ] Malloc'd %p (%i bytes), returning to %p", best->Data, best->Size, __builtin_return_address(0)); + Debug("[Heap ] Malloc'd %p (%i bytes), returning to %s:%i", best->Data, best->Size, File, Line); #endif return best->Data; } @@ -271,11 +273,11 @@ void *Heap_Allocate(const char *File, int Line, size_t __Bytes) best->Magic = MAGIC_USED; // Mark block as used best->File = File; best->Line = Line; + best->AllocateTime = now(); Mutex_Release(&glHeap); // Release spinlock #if DEBUG_TRACE - Log_Debug("Heap", "newhead(%p)->Size = 0x%x", newhead, newhead->Size); - Log_Debug("Heap", "Malloc'd %p (0x%x bytes), returning to %s:%i", + Debug("[Heap ] Malloc'd %p (0x%x bytes), returning to %s:%i", best->Data, best->Size, File, Line); #endif return best->Data; @@ -287,18 +289,17 @@ void *Heap_Allocate(const char *File, int Line, size_t __Bytes) */ void Heap_Deallocate(void *Ptr) { - tHeapHead *head; + tHeapHead *head = (void*)( (Uint)Ptr - sizeof(tHeapHead) ); tHeapFoot *foot; - #if DEBUG_TRACE - Log_Log("Heap", "free: Ptr = %p", Ptr); - Log_Log("Heap", "free: Returns to %p", __builtin_return_address(0)); - #endif - // INVLPTR is returned from Heap_Allocate when the allocation // size is zero. if( Ptr == INVLPTR ) return; + #if DEBUG_TRACE + Debug("[Heap ] free: %p freed by %p (%i old)", Ptr, __builtin_return_address(0), now()-head->AllocateTime); + #endif + // Alignment Check if( (Uint)Ptr & (sizeof(Uint)-1) ) { Log_Warning("Heap", "free - Passed a non-aligned address (%p)", Ptr); @@ -568,7 +569,7 @@ void Heap_Dump(void) return ; #if !VERBOSE_DUMP - Log_Log("Heap", "%p (0x%llx): 0x%08lx %i %4C", + Log_Log("Heap", "%p (%P): 0x%08lx %i %4C", head, MM_GetPhysAddr((Uint)head), head->Size, head->ValidSize, &head->Magic); Log_Log("Heap", "%p %4C", foot->Head, &foot->Magic); if(head->File) { @@ -587,7 +588,7 @@ void Heap_Dump(void) head = foot->Head; while( (tVAddr)head >= (tVAddr)badHead ) { - Log_Log("Heap", "%p (0x%llx): 0x%08lx %i %4C", + Log_Log("Heap", "%p (%P): 0x%08lx %i %4C", head, MM_GetPhysAddr((Uint)head), head->Size, head->ValidSize, &head->Magic); Log_Log("Heap", "%p %4C", foot->Head, &foot->Magic); if(head->File) @@ -667,11 +668,13 @@ void Heap_Stats(void) // Print the block info? #if 1 if( head->Magic == MAGIC_FREE ) - Log_Debug("Heap", "%p (0x%llx) - 0x%x free", + Log_Debug("Heap", "%p (%P) - 0x%x free", head->Data, MM_GetPhysAddr((tVAddr)&head->Data), head->Size); else - Log_Debug("Heap", "%p (0x%llx) - 0x%x (%i) Owned by %s:%i", - head->Data, MM_GetPhysAddr((tVAddr)&head->Data), head->Size, head->ValidSize, head->File, head->Line); + Log_Debug("Heap", "%p (%P) - 0x%x (%i) Owned by %s:%i (%lli ms old)", + head->Data, MM_GetPhysAddr((tVAddr)&head->Data), head->Size, head->ValidSize, head->File, head->Line, + now() - head->AllocateTime + ); #endif } diff --git a/Kernel/include/adt.h b/Kernel/include/adt.h index 17f23876..91a8ae5f 100644 --- a/Kernel/include/adt.h +++ b/Kernel/include/adt.h @@ -38,7 +38,7 @@ extern size_t RingBuffer_Read(void *Dest, tRingBuffer *Buffer, size_t Length); * \param Length Provided number of bytes * \return Number of bytes written */ -extern size_t RingBuffer_Write(tRingBuffer *Buffer, void *Source, size_t Length); +extern size_t RingBuffer_Write(tRingBuffer *Buffer, const void *Source, size_t Length); /** * \} */ diff --git a/Kernel/include/heap_int.h b/Kernel/include/heap_int.h index 56739bd1..12a709b3 100644 --- a/Kernel/include/heap_int.h +++ b/Kernel/include/heap_int.h @@ -12,6 +12,7 @@ typedef struct { const char *File; int Line; Uint Magic; + tTime AllocateTime; char Data[]; } tHeapHead; diff --git a/Kernel/include/vfs_ramfs.h b/Kernel/include/vfs_ramfs.h index 5bb7f319..8bbc8b58 100644 --- a/Kernel/include/vfs_ramfs.h +++ b/Kernel/include/vfs_ramfs.h @@ -9,7 +9,7 @@ typedef struct sRamFS_File { struct sRamFS_File *Next; struct sRamFS_File *Parent; - char *Name; + char Name[32]; tVFS_Node Node; union { struct sRamFS_File *FirstChild; diff --git a/Kernel/threads.c b/Kernel/threads.c index 3ebc4dc6..c4169fec 100644 --- a/Kernel/threads.c +++ b/Kernel/threads.c @@ -20,7 +20,7 @@ #define SCHED_RR_SIM 2 // Single Queue Round Robin #define SCHED_RR_PRI 3 // Multi Queue Round Robin // Set scheduler type -#define SCHEDULER_TYPE SCHED_LOTTERY +#define SCHEDULER_TYPE SCHED_RR_PRI // === CONSTANTS === #define DEFAULT_QUANTUM 10 @@ -1474,7 +1474,6 @@ int Semaphore_Wait(tSemaphore *Sem, int MaxToTake) else taken = Sem->Value; Sem->Value -= taken; - SHORTREL( &Sem->Protector ); } else { diff --git a/Kernel/vfs/fs/root.c b/Kernel/vfs/fs/root.c index 4c3c6a27..22e60d40 100644 --- a/Kernel/vfs/fs/root.c +++ b/Kernel/vfs/fs/root.c @@ -77,6 +77,9 @@ int Root_MkNod(tVFS_Node *Node, const char *Name, Uint Flags) ENTER("pNode sName xFlags", Node, Name, Flags); + if(strlen(Name) + 1 > sizeof(child->Name)); + LEAVE_RET('i', 0); + // Find last child, while we're at it, check for duplication for( ; child; prev = child, child = child->Next ) { @@ -89,7 +92,6 @@ int Root_MkNod(tVFS_Node *Node, const char *Name, Uint Flags) child = Root_int_AllocFile(); memset(child, 0, sizeof(tRamFS_File)); - child->Name = malloc(strlen(Name)+1); strcpy(child->Name, Name); child->Parent = parent; diff --git a/Kernel/vfs/open.c b/Kernel/vfs/open.c index 03d8b2b1..ed79ba11 100644 --- a/Kernel/vfs/open.c +++ b/Kernel/vfs/open.c @@ -61,13 +61,14 @@ char *VFS_GetAbsPath(const char *Path) // Check if the path is already absolute if(Path[0] == '/') { - ret = malloc(pathLen + 1); + ret = malloc(chrootLen + pathLen + 1); if(!ret) { Log_Warning("VFS", "VFS_GetAbsPath: malloc() returned NULL"); return NULL; } - strcpy(ret, Path); - } else { + strcpy(ret + chrootLen, Path); + } + else { if(cwd == NULL) { cwd = "/"; cwdLen = 1; @@ -76,15 +77,15 @@ char *VFS_GetAbsPath(const char *Path) cwdLen = strlen(cwd); } // Prepend the current directory - ret = malloc( cwdLen + 1 + pathLen + 1 ); - strcpy(ret, cwd); + ret = malloc(chrootLen + cwdLen + 1 + pathLen + 1 ); + strcpy(ret+chrootLen, cwd); ret[cwdLen] = '/'; - strcpy(&ret[cwdLen+1], Path); + strcpy(ret+chrootLen+cwdLen+1, Path); //Log("ret = '%s'", ret); } // Parse Path - pathComps[iPos++] = tmpStr = ret+1; + pathComps[iPos++] = tmpStr = ret+chrootLen+1; while(*tmpStr) { if(*tmpStr++ == '/') @@ -132,7 +133,7 @@ char *VFS_GetAbsPath(const char *Path) pathComps[iPos2] = NULL; // Build New Path - iPos2 = 1; iPos = 0; + iPos2 = chrootLen + 1; iPos = 0; ret[0] = '/'; while(pathComps[iPos]) { @@ -149,17 +150,12 @@ char *VFS_GetAbsPath(const char *Path) ret[iPos2-1] = 0; else ret[iPos2] = 0; - - + // Prepend the chroot - tmpStr = malloc(chrootLen + strlen(ret) + 1); - strcpy( tmpStr, chroot ); - strcpy( tmpStr+chrootLen, ret ); - free(ret); - ret = tmpStr; + memcpy( ret, chroot, chrootLen ); LEAVE('s', ret); - //Log("VFS_GetAbsPath: RETURN '%s'", ret); +// Log_Debug("VFS", "VFS_GetAbsPath: RETURN '%s'", ret); return ret; }