/*
+ * Acess2 Kernel
+ *
+ * adt.c
+ * - Complex data type code
*/
#include <acess.h>
#include <adt.h>
+
// === CODE ===
// --- Ring Buffers ---
tRingBuffer *RingBuffer_Create(size_t Space)
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;
// === IMPRORTS ===
#if TRACE_LOCKS
extern struct sShortSpinlock glDebug_Lock;
+extern struct sShortSpinlock glThreadListLock;
#endif
extern int GetCPUNum(void);
}
#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:
#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
}
#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
// 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
va_start(args, Fmt);
Debug_DbgOnlyFmt(Fmt, args);
va_end(args);
- Debug_PutCharDebug('\r');
Debug_PutCharDebug('\n');
#if LOCK_DEBUG_OUTPUT
SHORTREL(&glDebug_Lock);
va_start(args, Fmt);
Debug_Fmt(Fmt, args);
va_end(args);
- Debug_Putchar('\r');
Debug_Putchar('\n');
#if LOCK_DEBUG_OUTPUT
va_start(args, Fmt);
Debug_Fmt(Fmt, args);
va_end(args);
- Debug_Putchar('\r');
Debug_Putchar('\n');
#if LOCK_DEBUG_OUTPUT
va_start(args, Fmt);
Debug_Fmt(Fmt, args);
va_end(args);
- Debug_Putchar('\r');
Debug_Putchar('\n');
Threads_Dump();
}
va_end(args);
- Debug_Putchar(')'); Debug_Putchar('\r'); Debug_Putchar('\n');
+ Debug_Putchar(')'); Debug_Putchar('\n');
#if LOCK_DEBUG_OUTPUT
SHORTREL(&glDebug_Lock);
Debug_Fmt(Fmt, args);
va_end(args);
- Debug_Putchar('\r');
Debug_Putchar('\n');
#if LOCK_DEBUG_OUTPUT
// No Return
if(RetType == '-') {
- Debug_Putchar('\r');
Debug_Putchar('\n');
#if LOCK_DEBUG_OUTPUT
SHORTREL(&glDebug_Lock);
// Extended (64-Bit)
case 'X': Debug_Fmt("0x%llx", args); break;
}
- Debug_Putchar('\r');
Debug_Putchar('\n');
va_end(args);
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;
}
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;
}
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;
*/
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);
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) {
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)
// 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
}
* \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);
/**
* \}
*/
const char *File;
int Line;
Uint Magic;
+ tTime AllocateTime;
char Data[];
} tHeapHead;
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;
#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
else
taken = Sem->Value;
Sem->Value -= taken;
- SHORTREL( &Sem->Protector );
}
else
{
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 )
{
child = Root_int_AllocFile();
memset(child, 0, sizeof(tRamFS_File));
- child->Name = malloc(strlen(Name)+1);
strcpy(child->Name, Name);
child->Parent = parent;
// 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;
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++ == '/')
pathComps[iPos2] = NULL;
// Build New Path
- iPos2 = 1; iPos = 0;
+ iPos2 = chrootLen + 1; iPos = 0;
ret[0] = '/';
while(pathComps[iPos])
{
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;
}