32 #include <oc_system_cfg.h> 52 #define _________________________________________TYPES_SECTION______________________________________________________________________________ 57 oC_Process_Priority_t Priority;
73 #undef _________________________________________TYPES_SECTION______________________________________________________________________________ 80 #define _________________________________________PROTOTYPES_SECTION_________________________________________________________________________ 82 static void MemoryFaultHandler(
void * Address , MemoryEventFlags_t Event ,
const char * Function, uint32_t LineNumber );
84 #undef _________________________________________PROTOTYPES_SECTION_________________________________________________________________________ 92 #define _________________________________________VARIABLES_SECTION__________________________________________________________________________ 96 .EventHandler = MemoryFaultHandler ,
97 .EventFlags = MemoryEventFlags_BufferOverflow |
98 MemoryEventFlags_DataSectionOverflow |
99 MemoryEventFlags_MemoryExhausted |
100 MemoryEventFlags_PanicMemoryExhausted |
101 MemoryEventFlags_PossibleMemoryLeak
104 #undef _________________________________________VARIABLES_SECTION__________________________________________________________________________ 112 #define _________________________________________INTERFACE_FUNCTIONS_SECTION________________________________________________________________ 122 oC_User_IsCorrect(User)
125 if(InputStream !=
NULL && !oC_Stream_IsType(InputStream , oC_Stream_Type_Input))
127 oC_SaveError(Name,oC_ErrorCode_StreamTypeNotCorrect);
129 if(OutputStream !=
NULL && !oC_Stream_IsType(OutputStream , oC_Stream_Type_Output))
131 oC_SaveError(Name,oC_ErrorCode_StreamTypeNotCorrect);
133 if(ErrorStream !=
NULL && !oC_Stream_IsType(ErrorStream , oC_Stream_Type_Output))
135 oC_SaveError(Name,oC_ErrorCode_StreamTypeNotCorrect);
138 process = kmalloc(
sizeof(
struct Process_t) , &Allocator , AllocationFlags_CanWait1Second);
142 oC_Process_t currentProcess = oC_ProcessMan_GetCurrentProcess();
144 process->ErrorStream = ErrorStream;
145 process->InputStream = InputStream;
146 process->Name = Name;
147 process->OutputStream = OutputStream;
148 process->Priority = Priority;
149 process->User = User;
150 process->Allocator.
Name = Name;
152 process->Allocator.
EventFlags = MemoryEventFlags_AllErrors;
153 process->Killed =
false;
154 process->IoFlags = oC_IoFlags_Default;
155 process->HeapMap =
oC_MemMan_AllocateHeapMap(HeapMapSize,&process->Allocator,oC_FUNCTION,__LINE__,AllocationFlags_CanWait1Second);
156 process->Pwd = oC_Process_GetPwd(currentProcess);
157 process->Pid = oC_ProcessMan_GetNextPid();
158 process->StdioBuffer = kmalloc(CFG_BYTES_STDIO_BUFFER_SIZE *
sizeof(
char) , &process->Allocator , AllocationFlags_CanWait1Second);
159 process->StdioMutex = oC_Mutex_New(oC_Mutex_Type_Normal,&process->Allocator,AllocationFlags_CanWait1Second);
170 bool deleted =
false;
172 if(oC_Process_IsCorrect(*Process) && oC_Process_Kill(*Process))
174 (*Process)->ObjectControl = 0;
177 oC_ErrorCode_t errorCode = oC_VirtualFileSystem_fcloseFromProcess(*Process);
179 if(oC_ErrorOccur(errorCode))
181 oC_SaveError(
"Process delete - VFS error" , errorCode );
187 if(oC_ErrorOccur(errorCode))
189 oC_SaveError(
"Process delete - UDP error - " , errorCode );
195 if(oC_ErrorOccur(errorCode))
197 oC_SaveError(
"Process delete - ICMP error - " , errorCode );
203 if(oC_ErrorOccur(errorCode))
205 oC_SaveError(
"Process delete - TCP error - " , errorCode );
209 if(!oC_Mutex_Delete(&((*Process)->StdioMutex),AllocationFlags_CanWait1Second))
211 oC_SaveError(
"Process: Cannot delete StdioMutex",oC_ErrorCode_CannotDeleteObject);
217 oC_SaveError(
"Process delete - Error when releasing memory of allocator: " , oC_ErrorCode_ReleaseError);
221 if(kfree(*Process,AllocationFlags_CanWaitForever)==
false)
223 oC_SaveError(
"Process delete - cannot release process memory" , oC_ErrorCode_ReleaseError);
247 return oC_Process_IsCorrect(Process) && Process->Killed;
254 bool contains =
false;
256 if(oC_Process_IsCorrect(Process) && oC_Thread_IsCorrect(Thread))
268 const char * name =
"unknown";
270 if(oC_Process_IsCorrect(Process))
272 name = Process->Name;
280 oC_Process_State_t oC_Process_GetState(
oC_Process_t Process )
282 oC_Process_State_t state = oC_Process_State_Invalid;
284 if(oC_Process_IsCorrect(Process))
286 oC_IntMan_EnterCriticalSection();
287 oC_List(
oC_Thread_t) threads = oC_ThreadMan_GetList();
293 state = oC_Process_State_Killed;
297 state = oC_Process_State_Initialized;
300 uint32_t numberOfthreads = 0;
302 oC_List_Foreach(threads,thread)
304 if(oC_Process_ContainsThread(Process,thread))
310 state = oC_Process_State_Zombie;
312 else if(oC_Thread_IsBlocked(thread))
314 state = oC_Process_State_Asleep;
318 state = oC_Process_State_Run;
324 if(numberOfthreads == 0)
326 state = oC_Process_State_Zombie;
329 oC_IntMan_ExitCriticalSection();
341 if(oC_Process_IsCorrect(Process))
343 user = Process->User;
355 if(oC_Process_IsCorrect(Process))
357 stream = Process->InputStream;
369 if(oC_Process_IsCorrect(Process))
371 stream = Process->OutputStream;
383 if(oC_Process_IsCorrect(Process))
385 stream = Process->ErrorStream;
395 oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
398 oC_AssignErrorCodeIfFalse(&errorCode , oC_Process_IsCorrect(Process) , oC_ErrorCode_ProcessNotCorrect) &&
399 oC_AssignErrorCodeIfFalse(&errorCode , oC_Stream_IsCorrect(Stream) , oC_ErrorCode_StreamNotCorrect) &&
400 oC_AssignErrorCodeIfFalse(&errorCode , oC_Stream_IsType(Stream,oC_Stream_Type_Input) , oC_ErrorCode_StreamTypeNotCorrect)
403 errorCode = oC_ErrorCode_None;
404 Process->InputStream = Stream;
414 oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
417 oC_AssignErrorCodeIfFalse(&errorCode , oC_Process_IsCorrect(Process) , oC_ErrorCode_ProcessNotCorrect) &&
418 oC_AssignErrorCodeIfFalse(&errorCode , oC_Stream_IsCorrect(Stream) , oC_ErrorCode_StreamNotCorrect) &&
419 oC_AssignErrorCodeIfFalse(&errorCode , oC_Stream_IsType(Stream,oC_Stream_Type_Output) , oC_ErrorCode_StreamTypeNotCorrect)
422 errorCode = oC_ErrorCode_None;
423 Process->OutputStream= Stream;
433 oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
436 oC_AssignErrorCodeIfFalse(&errorCode , oC_Process_IsCorrect(Process) , oC_ErrorCode_ProcessNotCorrect) &&
437 oC_AssignErrorCodeIfFalse(&errorCode , oC_Stream_IsCorrect(Stream) , oC_ErrorCode_StreamNotCorrect) &&
438 oC_AssignErrorCodeIfFalse(&errorCode , oC_Stream_IsType(Stream,oC_Stream_Type_Output) , oC_ErrorCode_StreamTypeNotCorrect)
441 errorCode = oC_ErrorCode_None;
442 Process->ErrorStream = Stream;
450 oC_ErrorCode_t oC_Process_SetPriority(
oC_Process_t Process , oC_Process_Priority_t Priority )
452 oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
454 if(ErrorCondition( oC_Process_IsCorrect(Process) , oC_ErrorCode_ProcessNotCorrect ))
456 Process->Priority = Priority;
457 errorCode = oC_ErrorCode_None;
465 oC_Process_Priority_t oC_Process_GetPriority(
oC_Process_t Process )
467 return oC_Process_IsCorrect(Process) ? Process->Priority : 0;
472 oC_ErrorCode_t oC_Process_LockStdioBuffer(
oC_Process_t Process ,
char ** outStdioBuffer , oC_Time_t Timeout )
474 oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
477 ErrorCondition(oC_Process_IsCorrect(Process) ,oC_ErrorCode_ProcessNotCorrect) &&
481 if(oC_Mutex_Take(Process->StdioMutex,Timeout))
483 *outStdioBuffer = Process->StdioBuffer;
484 errorCode = oC_ErrorCode_None;
486 memset(Process->StdioBuffer,0,CFG_BYTES_STDIO_BUFFER_SIZE *
sizeof(
char));
490 errorCode = oC_ErrorCode_TimeoutError;
499 oC_ErrorCode_t oC_Process_UnlockStdioBuffer(
oC_Process_t Process )
501 oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
503 if(ErrorCondition(oC_Process_IsCorrect(Process) ,oC_ErrorCode_ProcessNotCorrect))
505 if(oC_Mutex_Give(Process->StdioMutex))
507 errorCode = oC_ErrorCode_None;
511 errorCode = oC_ErrorCode_ReleasingMutexError;
524 if(oC_Process_IsCorrect(Process))
526 oC_IntMan_EnterCriticalSection();
527 oC_List(
oC_Thread_t) threads = oC_ThreadMan_GetList();
529 foreach(threads,thread)
531 if(oC_Process_ContainsThread(Process,thread))
537 oC_IntMan_ExitCriticalSection();
547 bool allKilled =
false;
549 if(oC_Process_IsCorrect(Process))
551 oC_IntMan_EnterCriticalSection();
552 Process->Killed =
true;
555 oC_ProcessMan_ActivateDeleteDeamon();
557 oC_List(
oC_Thread_t) threads = oC_ThreadMan_GetList();
561 oC_List_Foreach(threads,thread)
563 if(oC_Process_ContainsThread(Process,thread))
565 if(oC_Thread_Cancel(&thread)==
false)
572 oC_List_Foreach(threads,thread)
574 if(oC_Process_ContainsThread(Process,thread))
576 if(oC_Thread_IsCorrect(thread))
578 oC_List_RemoveAll(threads,thread);
583 oC_IntMan_ExitCriticalSection();
596 if(oC_Process_IsCorrect(Process))
598 allocator = &Process->Allocator;
606 oC_HeapMap_t oC_Process_GetHeapMap(
oC_Process_t Process )
608 oC_HeapMap_t heapMap =
NULL;
610 if(oC_Process_IsCorrect(Process))
612 heapMap = Process->HeapMap;
620 oC_ErrorCode_t oC_Process_Sleep(
oC_Process_t Process , oC_Time_t Timeout )
622 oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
624 if(oC_AssignErrorCodeIfFalse(&errorCode , oC_Process_IsCorrect(Process) , oC_ErrorCode_ObjectNotCorrect))
626 errorCode = oC_ErrorCode_None;
628 oC_List(
oC_Thread_t) threads = oC_ThreadMan_GetList();
632 oC_List_Foreach(threads,thread)
634 oC_IntMan_EnterCriticalSection();
635 if(oC_Process_ContainsThread(Process,thread))
637 if(oC_Thread_IsBlocked(thread) ==
false)
639 oC_Thread_Sleep(thread,Timeout);
642 oC_IntMan_ExitCriticalSection();
653 oC_UInt_t oC_Process_GetHeapMapSize(
oC_Process_t Process )
657 if(oC_Process_IsCorrect(Process))
667 oC_UInt_t oC_Process_GetFreeHeapMapSize(
oC_Process_t Process )
671 if(oC_Process_IsCorrect(Process))
681 oC_UInt_t oC_Process_GetThreadsStackSize(
oC_Process_t Process )
685 if(oC_Process_IsCorrect(Process))
687 oC_List(
oC_Thread_t) threads = oC_ThreadMan_GetList();
691 oC_List_Foreach(threads,thread)
693 if(oC_Process_ContainsThread(Process,thread))
695 size += oC_Thread_GetStackSize(thread);
706 oC_Int_t oC_Process_GetFreeThreadsStackSize(
oC_Process_t Process ,
bool Current )
710 if(oC_Process_IsCorrect(Process))
712 oC_List(
oC_Thread_t) threads = oC_ThreadMan_GetList();
716 oC_List_Foreach(threads,thread)
718 if(oC_Process_ContainsThread(Process,thread))
720 oC_Int_t threadFreeSize = oC_Thread_GetFreeStackSize(thread,Current);
722 if(threadFreeSize > 0 && size >= 0)
724 size += threadFreeSize;
726 else if(threadFreeSize < size)
728 size = threadFreeSize;
740 oC_Time_t oC_Process_GetExecutionTime(
oC_Process_t Process )
742 oC_Time_t executionTime = 0;
744 if(oC_Process_IsCorrect(Process))
746 oC_List(
oC_Thread_t) threads = oC_ThreadMan_GetList();
750 oC_List_Foreach(threads,thread)
752 if(oC_Process_ContainsThread(Process,thread))
754 executionTime += oC_Thread_GetExecutionTime(thread);
760 return executionTime;
765 oC_UInt_t oC_Process_GetUsedRamSize(
oC_Process_t Process )
769 if(oC_Process_IsCorrect(Process))
779 oC_IoFlags_t oC_Process_GetIoFlags(
oC_Process_t Process )
781 oC_IoFlags_t flags = oC_IoFlags_Default;
783 if(oC_Process_IsCorrect(Process))
785 flags = Process->IoFlags;
793 oC_ErrorCode_t oC_Process_SetIoFlags(
oC_Process_t Process , oC_IoFlags_t IoFlags )
795 oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
797 if(oC_AssignErrorCodeIfFalse(&errorCode , oC_Process_IsCorrect(Process),oC_ErrorCode_ProcessNotCorrect))
799 Process->IoFlags = IoFlags;
800 errorCode = oC_ErrorCode_None;
808 oC_ErrorCode_t oC_Process_WaitForFinish(
oC_Process_t Process , oC_Time_t CheckPeriod , oC_Time_t Timeout )
810 oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
812 if(oC_AssignErrorCodeIfFalse(&errorCode , oC_Process_IsCorrect(Process),oC_ErrorCode_ProcessNotCorrect))
814 errorCode = oC_ErrorCode_None;
815 oC_Process_State_t state = oC_Process_State_Run;
816 oC_Time_t timeoutTime = oC_KTime_GetTimestamp() + Timeout;
817 while( state == oC_Process_State_Run || state == oC_Process_State_Asleep || state == oC_Process_State_Initialized )
821 state = oC_Process_GetState(Process);
823 if(oC_KTime_GetTimestamp() >= timeoutTime)
825 errorCode = oC_ErrorCode_Timeout;
841 if(oC_Process_IsCorrect(Process))
851 oC_ErrorCode_t oC_Process_SetPwd(
oC_Process_t Process ,
const char * Pwd )
853 oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
856 oC_AssignErrorCodeIfFalse(&errorCode , oC_Process_IsCorrect(Process) , oC_ErrorCode_ProcessNotCorrect) &&
857 oC_AssignErrorCodeIfFalse(&errorCode , isaddresscorrect(Pwd) , oC_ErrorCode_WrongAddress)
860 uint32_t requiredSize = 0;
861 char * newRelativePwd =
NULL;
863 errorCode = oC_VirtualFileSystem_ConvertRelativeToAbsolute(Pwd,
NULL,&requiredSize,
true);
865 if(!oC_ErrorOccur(errorCode))
867 newRelativePwd = smartalloc(requiredSize,AllocationFlags_CanWait1Second);
871 errorCode = oC_VirtualFileSystem_ConvertRelativeToAbsolute(Pwd,newRelativePwd,&requiredSize,
true);
873 if(!oC_ErrorOccur(errorCode))
875 errorCode = oC_ErrorCode_None;
877 if(oC_VirtualFileSystem_DirExists(newRelativePwd))
881 if(smartfree(Process->Pwd,strlen(Process->Pwd)+1,AllocationFlags_CanWaitForever)==
false)
883 errorCode = oC_ErrorCode_ReleaseError;
886 Process->Pwd = newRelativePwd;
890 if(smartfree(newRelativePwd,requiredSize,AllocationFlags_CanWait1Second) ==
false)
892 oC_SaveError(
"Process: Set pwd, cannot release newRelativePwd: " , oC_ErrorCode_ReleaseError);
895 errorCode = oC_ErrorCode_DirectoryNotExists;
901 if(smartfree(newRelativePwd,requiredSize,AllocationFlags_CanWait1Second) ==
false)
903 oC_SaveError(
"Process: Set pwd, cannot release newRelativePwd: " , oC_ErrorCode_ReleaseError);
909 errorCode = oC_ErrorCode_AllocationError;
923 if(oC_Process_IsCorrect(Process))
933 bool oC_Process_SetAllocationLimit(
oC_Process_t Process , oC_MemorySize_t Limit )
935 bool success =
false;
937 if(oC_Process_IsCorrect(Process))
939 Process->Allocator.
Limit = Limit;
948 bool oC_Process_SetAllocationTracking(
oC_Process_t Process,
bool Enabled )
950 bool success =
false;
952 if(oC_Process_IsCorrect(Process))
956 Process->Allocator.
EventFlags |= MemoryEventFlags_MemoryAllocated | MemoryEventFlags_MemoryReleased;
960 Process->Allocator.
EventFlags &= ~(MemoryEventFlags_MemoryAllocated | MemoryEventFlags_MemoryReleased);
968 #undef _________________________________________INTERFACE_FUNCTIONS_SECTION________________________________________________________________ 975 #define _________________________________________LOCAL_FUNCTIONS_SECTION____________________________________________________________________ 977 static void MemoryFaultHandler(
void * Address , MemoryEventFlags_t Event ,
const char * Function, uint32_t LineNumber )
979 char tempString[150] = {0};
981 sprintf(tempString,
"Error in allocation of address '%p' allocated in function %s at line %d (%d bytes)\n", Address, Function, LineNumber,
oC_MemMan_GetSizeOfAllocation(Address));
983 if(Event & MemoryEventFlags_MemoryFault )
oC_ExcHan_LogEvent(
"Memory fault flag. Reason is unknown " , tempString,
NULL,
NULL, oC_ExcHan_ExceptionType_MemoryAllocationError );
984 if(Event & MemoryEventFlags_BusFault )
oC_ExcHan_LogEvent(
"Bus fault flag. " , tempString,
NULL,
NULL, oC_ExcHan_ExceptionType_MemoryAllocationError );
985 if(Event & MemoryEventFlags_AllocationError )
oC_ExcHan_LogEvent(
"Error while allocation of memory " , tempString,
NULL,
NULL, oC_ExcHan_ExceptionType_MemoryAllocationError );
986 if(Event & MemoryEventFlags_ReleaseError )
oC_ExcHan_LogEvent(
"Error while releasing memory (address and allocation line number given in the event arguments) " , tempString,
NULL,
NULL, oC_ExcHan_ExceptionType_MemoryAllocationError );
987 if(Event & MemoryEventFlags_PossibleMemoryLeak )
oC_ExcHan_LogEvent(
"There is a possibility of memory leak " , tempString,
NULL,
NULL, oC_ExcHan_ExceptionType_MemoryAllocationError );
988 if(Event & MemoryEventFlags_BufferOverflow )
oC_ExcHan_LogEvent(
"Allocated memory at the given address achieved 'red-zone' " , tempString,
NULL,
NULL, oC_ExcHan_ExceptionType_MemoryAllocationError );
990 if(Event & MemoryEventFlags_MemoryAllocated ) kdebuglog(oC_LogType_Track,
"allocation - %s:%d (%d bytes)", Function,LineNumber,
oC_MemMan_GetSizeOfAllocation(Address));
991 if(Event & MemoryEventFlags_MemoryReleased ) kdebuglog(oC_LogType_Track,
"release - %s:%d (%d bytes)", Function,LineNumber,
oC_MemMan_GetSizeOfAllocation(Address));
992 if(Event & MemoryEventFlags_RawMemoryAllocated ) kdebuglog(oC_LogType_Track,
"allocation - %s:%d (%d bytes)", Function,LineNumber,
oC_MemMan_GetSizeOfAllocation(Address));
993 if(Event & MemoryEventFlags_RawMemoryReleased ) kdebuglog(oC_LogType_Track,
"release - %s:%d (%d bytes)", Function,LineNumber,
oC_MemMan_GetSizeOfAllocation(Address));
995 if(Event & MemoryEventFlags_ModuleTurningOff )
oC_ExcHan_LogEvent(
"The MemMan module is turning off " , tempString,
NULL,
NULL, oC_ExcHan_ExceptionType_MemoryAllocationError );
996 if(Event & MemoryEventFlags_DataSectionOverflow )
oC_ExcHan_LogEvent(
"Some buffer, that is in the data section was overwritten. " , tempString,
NULL,
NULL, oC_ExcHan_ExceptionType_MemoryAllocationError );
1000 #undef _________________________________________LOCAL_FUNCTIONS_SECTION____________________________________________________________________
#define s(time)
Number of s.
oC_ErrorCode_t oC_Udp_ReleaseAllPortsReservedBy(oC_Process_t Process, oC_Time_t Timeout)
releases all ports reserved by the process
bool oC_MemMan_IsRamAddress(const void *Address)
checks if address is in ram section
identifier for allocations
bool oC_MemMan_FreeAllMemoryOfAllocator(Allocator_t Allocator)
release all memory of allocator
The file with interface for process manager.
oC_UInt_t oC_MemMan_GetSizeOfAllocation(const void *Address)
returns size of allocation
oC_UInt_t oC_MemMan_GetHeapMapSize(oC_HeapMap_t Map)
returns size of heap map
MemoryEventHandler_t EventHandler
oC_UInt_t oC_MemMan_GetFreeHeapMapSize(oC_HeapMap_t Map)
returns size of free memory in heap map
The file with helper macros for managing objects.
bool oC_MemMan_IsDynamicAllocatedAddress(const void *Address)
checks if address is in dynamic allocated section
bool oC_MemMan_IsAddressCorrect(const void *Address)
checks if address is correct - in RAM or in FLASH
Allocator_t oC_MemMan_GetAllocatorOfAddress(const void *Address)
returns allocator of address
uint32_t oC_ObjectControl_t
stores object control value
oC_ErrorCode_t oC_Tcp_CloseProcess(oC_Process_t Process, oC_Time_t Timeout)
closes all objects related with the process
The file with list library.
The file with interface for Virtual File System.
void oC_ExcHan_LogEvent(const char *Name, char *AdditionalInfo, void *Stack, oC_Thread_t Thread, oC_ExcHan_ExceptionType_t Type)
logs system event
The file with interface for interrupt manager.
The file with interface for user system.
static oC_ObjectControl_t oC_CountObjectControl(void *ObjectPointer, oC_ObjectId_t ObjectId)
counts object control for object
static bool oC_CheckObjectControl(void *ObjectPointer, oC_ObjectId_t ObjectId, oC_ObjectControl_t ObjectControl)
checks if object control is correct
oC_UInt_t oC_MemMan_GetMemoryOfAllocatorSize(Allocator_t Allocator)
returns size of allocations per allocator
The file with interface for mutex managing.
The file with memory manager interface.
The file with interface for process mechanism.
File with interface of Exception Handler.
File with interface for ICMP.
MemoryEventFlags_t EventFlags
oC_HeapMap_t oC_MemMan_AllocateHeapMap(oC_UInt_t Size, Allocator_t Allocator, const char *Function, uint32_t LineNumber, AllocationFlags_t Flags)
allocates memory for new heap map
The file with interface for Thread Manager.
oC_ErrorCode_t oC_Icmp_ReleaseAllTypesReservedBy(oC_Process_t Process, oC_Time_t Timeout)
releases all types reserved by the process
The file with interface of kernel time module.
#define NULL
pointer to a zero