30 #include <oc_system_cfg.h> 41 #define _________________________________________VARIABLES_SECTION_________________________________________________________________________ 43 static oC_UInt_t NextPid = 0;
44 static oC_UInt_t SomeProcessToDelete=
false;
49 .
Name =
"Process Manager" ,
54 .Name =
"Process Manager" ,
55 .Module = oC_Module_ProcessMan ,
56 .TurnOnFunction = oC_ProcessMan_TurnOn ,
57 .TurnOffFunction = oC_ProcessMan_TurnOff ,
58 .RequiredModules = { oC_Module_ThreadMan }
61 #undef _________________________________________VARIABLES_SECTION_________________________________________________________________________ 68 #define _________________________________________INTERFACE_FUNCTIONS_SECTION________________________________________________________________ 72 oC_ErrorCode_t oC_ProcessMan_TurnOn(
void )
74 oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
78 Processes = oC_List_New(&
Allocator,AllocationFlags_NoWait);
82 SomeProcessToDelete =
false;
83 DeleteDeamonThread =
NULL;
84 DeleteDeamonProcess =
NULL;
86 errorCode = oC_ErrorCode_None;
91 errorCode = oC_ErrorCode_AllocationError;
100 oC_ErrorCode_t oC_ProcessMan_TurnOff(
void )
102 oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
107 errorCode = oC_ErrorCode_None;
109 oC_List_Foreach(Processes,process)
111 if(oC_Process_Delete(&process) ==
false)
113 errorCode = oC_ErrorCode_CannotDeleteProcess;
116 if(oC_List_Delete(Processes,AllocationFlags_CanWaitForever) ==
false)
118 errorCode = oC_ErrorCode_ReleaseError;
126 oC_ErrorCode_t oC_ProcessMan_AddProcess(
oC_Process_t Process )
128 oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
132 && oC_AssignErrorCodeIfFalse(&errorCode , oC_Process_IsCorrect(Process) , oC_ErrorCode_ObjectNotCorrect)
135 bool added = oC_List_PushBack(Processes,Process,&
Allocator);
139 errorCode = oC_ErrorCode_None;
143 errorCode = oC_ErrorCode_CannotAddObjectToList;
152 oC_ErrorCode_t oC_ProcessMan_RemoveProcess(
oC_Process_t Process )
154 oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
158 && oC_AssignErrorCodeIfFalse(&errorCode , oC_Process_IsCorrect(Process) , oC_ErrorCode_ObjectNotCorrect)
161 bool contains = oC_List_Contains(Processes,Process);
165 bool removed = oC_List_RemoveAll(Processes,Process);
169 errorCode = oC_ErrorCode_None;
173 errorCode = oC_ErrorCode_CannotRemoveObjectFromList;
178 errorCode = oC_ErrorCode_ObjectNotFoundOnList;
188 bool oC_ProcessMan_ContainsProcess(
oC_Process_t Process )
190 bool contains =
false;
193 oC_SaveIfFalse(
"ProcessMan",
oC_Module_IsTurnedOn(oC_Module_ProcessMan) , oC_ErrorCode_ModuleNotStartedYet )
194 && oC_SaveIfFalse(
"Process" , oC_Process_IsCorrect(Process) , oC_ErrorCode_ObjectNotCorrect )
197 contains = oC_List_Contains(Processes,Process);
205 oC_ErrorCode_t oC_ProcessMan_GetList( oC_List(
oC_Process_t) List )
207 oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
213 errorCode = oC_ErrorCode_None;
217 oC_List_Foreach(Processes,process)
219 bool added = oC_List_PushBack(List,process,&
Allocator);
223 errorCode = oC_ErrorCode_CannotAddObjectToList;
234 oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
238 oC_AssignErrorCodeIfFalse(&errorCode , oC_List_IsCorrect(List) , oC_ErrorCode_ListNotCorrect)
241 errorCode = oC_ErrorCode_None;
245 oC_List_Foreach(Processes,process)
247 if(oC_Process_GetUser(process) == User)
249 bool added = oC_List_PushBack(List,process,&
Allocator);
253 errorCode = oC_ErrorCode_CannotAddObjectToList;
269 oC_Thread_t currentThread = oC_ThreadMan_GetCurrentThread();
271 oC_List_Foreach(Processes,process)
273 if(oC_Process_ContainsThread(process,currentThread))
275 currentProcess = process;
281 return currentProcess;
286 oC_Process_t oC_ProcessMan_GetProcess(
const char * Name )
292 oC_List_Foreach(Processes,process)
294 if(strcmp(oC_Process_GetName(process),Name) == 0)
296 processToReturn = process;
302 return processToReturn;
307 oC_Process_t oC_ProcessMan_GetProcessById( oC_UInt_t PID )
313 oC_List_Foreach(Processes,process)
315 if(oC_Process_GetPid(process) == PID)
317 processToReturn = process;
323 return processToReturn;
328 Allocator_t oC_ProcessMan_GetCurrentAllocator(
void )
334 oC_IntMan_EnterCriticalSection();
335 oC_Process_t currentProcess = oC_ProcessMan_GetCurrentProcess();
336 oC_IntMan_ExitCriticalSection();
340 oC_IntMan_EnterCriticalSection();
341 allocator = oC_Process_GetAllocator(currentProcess);
342 oC_IntMan_ExitCriticalSection();
355 if(oC_Thread_IsCorrect(Thread))
357 oC_List_Foreach(Processes,proc)
359 if(oC_Process_ContainsThread(proc,Thread))
368 oC_SaveError(
"Process manager" , oC_ErrorCode_ObjectNotCorrect);
376 void * oC_ProcessMan_RawAllocate( oC_UInt_t Size ,
const char * Function, uint32_t LineNumber , AllocationFlags_t Flags )
378 void * address =
NULL;
382 oC_Process_t currentProcess = oC_ProcessMan_GetCurrentProcess();
386 oC_HeapMap_t heapMap = oC_Process_GetHeapMap(currentProcess);
400 bool oC_ProcessMan_RawFree(
void * Address , oC_UInt_t Size )
402 bool released =
false;
406 oC_Process_t currentProcess = oC_ProcessMan_GetCurrentProcess();
410 oC_HeapMap_t heapMap = oC_Process_GetHeapMap(currentProcess);
423 void * oC_ProcessMan_Allocate( oC_UInt_t Size ,
const char * Function, uint32_t LineNumber , AllocationFlags_t Flags )
425 void * address =
NULL;
429 Allocator_t allocator = oC_ProcessMan_GetCurrentAllocator();
441 bool oC_ProcessMan_Free(
void * Address , AllocationFlags_t Flags )
443 bool released =
false;
455 void * oC_ProcessMan_SmartAllocate( oC_UInt_t Size,
const char * Function, uint32_t LineNumber , AllocationFlags_t Flags )
457 void * address =
NULL;
461 oC_UInt_t realRawAllocationSize = Size + (Size/8) + ((Size%8) > 0) ? 1 : 0;
464 if(realAllocationSize >= realRawAllocationSize)
466 address = oC_ProcessMan_RawAllocate(Size,Function,LineNumber,Flags);
470 address = oC_ProcessMan_Allocate(Size,Function,LineNumber,Flags);
479 bool oC_ProcessMan_SmartFree(
void * Address , oC_UInt_t Size , AllocationFlags_t Flags )
481 bool released =
false;
485 released = oC_ProcessMan_RawFree(Address,Size);
489 released = oC_ProcessMan_Free(Address,Flags);
498 void * oC_ProcessMan_KernelSmartAllocate( oC_UInt_t Size ,
Allocator_t Allocator ,
const char * Function, uint32_t LineNumber , AllocationFlags_t Flags )
500 void * address = oC_ProcessMan_SmartAllocate(Size,Function,LineNumber,Flags);
504 address = _kmalloc(Size,Allocator,Function,LineNumber,Flags,0);
512 bool oC_ProcessMan_KernelSmartFree(
void * Address , oC_UInt_t Size , AllocationFlags_t Flags )
514 bool released = oC_ProcessMan_SmartFree(Address,Size,Flags);
516 if(released ==
false)
518 released = kfree(Address,Flags);
526 void oC_ProcessMan_DeleteDeamon(
void * Parameter )
528 oC_Thread_t currentThread = oC_ThreadMan_GetCurrentThread();
532 oC_IntMan_EnterCriticalSection();
533 SomeProcessToDelete =
false;
534 if(oC_Thread_SetBlocked(currentThread,&SomeProcessToDelete,oC_Thread_Unblock_WhenEqual,
true,oC_Thread_UnblockMask_All,oC_hour(1))==
false)
536 kdebuglog(oC_LogType_Error ,
"DeleteDeamon: cannot block current thread\n");
538 oC_IntMan_ExitCriticalSection();
540 while(oC_Thread_IsBlocked(currentThread));
542 oC_Thread_SetUnblocked(currentThread);
544 oC_List_Foreach(Processes,process)
546 if(oC_Process_IsKilled(process))
549 oC_ErrorCode_t errorCode = oC_ProcessMan_RemoveProcess(process);
551 if(oC_ErrorOccur(errorCode))
553 oC_SaveError(
"DeleteDeamon: Cannot delete process from the list: " , errorCode );
556 if(oC_Process_Delete(&process)==
false)
558 oC_SaveError(oC_Process_GetName(copy),oC_ErrorCode_CannotDeleteProcess);
567 oC_ErrorCode_t oC_ProcessMan_RunDeleteDaemon(
void )
569 oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
573 oC_AssignErrorCodeIfFalse(&errorCode , oC_Thread_IsCorrect(DeleteDeamonThread) ==
false , oC_ErrorCode_DaemonThreadAlreadyExists ) &&
574 oC_AssignErrorCodeIfFalse(&errorCode , oC_Process_IsCorrect(DeleteDeamonProcess)==
false , oC_ErrorCode_DaemonProcessAlreadyExists)
577 DeleteDeamonProcess = oC_Process_New(oC_Process_Priority_SystemHandlerDeamon,
"delete daemon",oC_UserMan_GetRootUser(),0,
NULL,
NULL,
NULL);
579 if(oC_Process_IsCorrect(DeleteDeamonProcess))
581 DeleteDeamonThread = oC_Thread_New(oC_Process_Priority_SystemHandlerDeamon,CFG_BYTES_DELETE_DEAMON_STACK_SIZE,DeleteDeamonProcess,
"delete daemon",oC_ProcessMan_DeleteDeamon,
NULL);
583 if(oC_Thread_IsCorrect(DeleteDeamonThread))
585 if(oC_Thread_Run(DeleteDeamonThread))
587 errorCode = oC_ProcessMan_AddProcess(DeleteDeamonProcess);
591 if(oC_Process_Delete(&DeleteDeamonProcess)==
false)
593 oC_SaveError(
"Cannot delete daemon process: ",oC_ErrorCode_ReleaseError);
596 if(oC_Thread_Delete(&DeleteDeamonThread)==
false)
598 oC_SaveError(
"Cannot delete daemon thread: ",oC_ErrorCode_ReleaseError);
601 errorCode = oC_ErrorCode_CannotRunThread;
606 if(oC_Process_Delete(&DeleteDeamonProcess)==
false)
608 oC_SaveError(
"Cannot delete daemon process: ",oC_ErrorCode_ReleaseError);
610 errorCode = oC_ErrorCode_CannotCreateThread;
615 errorCode = oC_ErrorCode_CannotCreateProcess;
625 void oC_ProcessMan_ActivateDeleteDeamon(
void )
629 SomeProcessToDelete =
true;
635 oC_UInt_t oC_ProcessMan_GetNextPid(
void )
645 oC_List_Foreach(Processes,process)
647 if(oC_Process_GetPid(process)==pid)
661 void oC_ProcessMan_KillAllZombies(
void )
663 oC_List_Foreach(Processes,process)
665 if(oC_Process_GetState(process) == oC_Process_State_Zombie)
667 oC_Process_Kill(process);
673 #undef _________________________________________INTERFACE_FUNCTIONS_SECTION________________________________________________________________
bool oC_MEM_LLD_SetMemoryAccessMode(oC_MEM_LLD_MemoryAccessMode_t Mode)
Change current memory access mode.
identifier for allocations
void * oC_MemMan_RawAllocate(oC_HeapMap_t Map, oC_UInt_t Size, const char *Function, uint32_t LineNumber, AllocationFlags_t Flags)
allow to allocate memory in heap map
The file with interface for process manager.
bool oC_MemMan_RawFree(oC_HeapMap_t Map, void *Address, oC_UInt_t Size)
release memory in heap map
File with interface for user system manager.
void * oC_MemMan_Allocate(oC_UInt_t Size, Allocator_t Allocator, const char *Function, uint32_t LineNumber, AllocationFlags_t Flags, oC_UInt_t Alignment)
allocates memory on heap
oC_UInt_t oC_MemMan_GetAllocationBlockSize(void)
returns size of block needed for allocation
The file with interface for the module library.
static bool oC_Module_IsTurnedOn(oC_Module_t Module)
checks if the module is turned on
Memory controller is configured to allow for memory access for regions that can be accessible by a pr...
The file with interface for interrupt manager.
bool oC_MemMan_Free(void *Address, AllocationFlags_t Flags)
release allocated memory
static void oC_Module_TurnOn(oC_Module_t Module)
sets module as turned on
static bool oC_Module_TurnOffVerification(oC_ErrorCode_t *outErrorCode, oC_Module_t Module)
verify if module is turned off
static const oC_Allocator_t Allocator
static bool oC_Module_TurnOnVerification(oC_ErrorCode_t *outErrorCode, oC_Module_t Module)
verify if module is turned on
The file with interface for Thread Manager.
#define NULL
pointer to a zero
static void oC_Module_TurnOff(oC_Module_t Module)
sets module as turned off