unsigned short AxisSweep3::AddHandle(const SimdPoint3& aabbMin,const SimdPoint3& aabbMax, void* pOwner,short int collisionFilterGroup,short int collisionFilterMask) { // quantize the bounds unsigned short min[3], max[3]; Quantize(min, aabbMin, 0); Quantize(max, aabbMax, 1); // allocate a handle unsigned short handle = AllocHandle(); assert(handle!= 0xcdcd); Handle* pHandle = GetHandle(handle); pHandle->m_handleId = handle; //pHandle->m_pOverlaps = 0; pHandle->m_clientObject = pOwner; pHandle->m_collisionFilterGroup = collisionFilterGroup; pHandle->m_collisionFilterMask = collisionFilterMask; // compute current limit of edge arrays int limit = m_numHandles * 2; // insert new edges just inside the max boundary edge for (int axis = 0; axis < 3; axis++) { m_pHandles[0].m_maxEdges[axis] += 2; m_pEdges[axis][limit + 1] = m_pEdges[axis][limit - 1]; m_pEdges[axis][limit - 1].m_pos = min[axis]; m_pEdges[axis][limit - 1].m_handle = handle; m_pEdges[axis][limit].m_pos = max[axis]; m_pEdges[axis][limit].m_handle = handle; pHandle->m_minEdges[axis] = limit - 1; pHandle->m_maxEdges[axis] = limit; } // now sort the new edges to their correct position SortMinDown(0, pHandle->m_minEdges[0], false); SortMaxDown(0, pHandle->m_maxEdges[0], false); SortMinDown(1, pHandle->m_minEdges[1], false); SortMaxDown(1, pHandle->m_maxEdges[1], false); SortMinDown(2, pHandle->m_minEdges[2], true); SortMaxDown(2, pHandle->m_maxEdges[2], true); //PrintAxis(1); return handle; }
OdbcHandleBase<HandleType>::OdbcHandleBase(SQLSMALLINT handle_type, SQLHANDLE parent_handle, const char *type_base_name, HandleGetAttrFunc get_attr_function, HandleSetAttrFunc set_attr_function) :handle_type_(handle_type) ,parent_handle_(parent_handle) ,this_handle_(0) ,get_attr_function_name_("::SQLGet" + std::string(type_base_name) + "Attr") ,set_attr_function_name_("::SQLSet" + std::string(type_base_name) + "Attr") ,get_attr_function_(get_attr_function) ,set_attr_function_(set_attr_function) { // CODE NOTE: Check handle_type // CODE NOTE: Check parent handle AllocHandle(handle_type_, parent_handle_, &this_handle_); }
SYSCALL int AddInterruptHandler (int irq) { struct ISRHandler *isr_handler; struct Process *current; int handle; current = GetCurrentProcess(); if (!(current->flags & PROCF_ALLOW_IO)) return privilegeErr; if (free_handle_cnt < 1 || free_isr_handler_cnt < 1) return resourceErr; DisablePreemption(); handle = AllocHandle(); isr_handler = LIST_HEAD (&free_isr_handler_list); LIST_REM_HEAD (&free_isr_handler_list, isr_handler_entry); free_isr_handler_cnt --; isr_handler->irq = irq; isr_handler->handle = handle; SetObject (current, handle, HANDLE_TYPE_ISR, isr_handler); DisableInterrupts(); LIST_ADD_TAIL (&isr_handler_list[isr_handler->irq], isr_handler, isr_handler_entry); irq_handler_cnt[irq] ++; if (irq_handler_cnt[irq] == 1) UnmaskInterrupt(irq); EnableInterrupts(); return handle; }
/*! ****************************************************************************** @Function PVRSRVAllocSubHandle @Description Allocate a subhandle @Input phHandle - location for new subhandle pvData - pointer to resource to be associated with the subhandle eType - the type of resource hParent - parent handle @Output phHandle - points to new subhandle @Return Error code or PVRSRV_OK ******************************************************************************/ PVRSRV_ERROR PVRSRVAllocSubHandle(PVRSRV_HANDLE_BASE *psBase, IMG_HANDLE *phHandle, IMG_VOID *pvData, PVRSRV_HANDLE_TYPE eType, PVRSRV_HANDLE_ALLOC_FLAG eFlag, IMG_HANDLE hParent) { HANDLE_DATA *psPHandleData = IMG_NULL; HANDLE_DATA *psCHandleData = IMG_NULL; IMG_HANDLE hParentKey; IMG_HANDLE hHandle; PVRSRV_ERROR eError; *phHandle = IMG_NULL; /* PVRSRV_HANDLE_TYPE_NONE is reserved for internal use */ PVR_ASSERT(eType != PVRSRV_HANDLE_TYPE_NONE); PVR_ASSERT(gpsHandleFuncs); if (psBase == IMG_NULL) { PVR_DPF((PVR_DBG_ERROR, "PVRSRVAllocSubHandle: Missing handle base")); eError = PVRSRV_ERROR_INVALID_PARAMS; goto err; } hParentKey = TEST_FLAG(eFlag, PVRSRV_HANDLE_ALLOC_FLAG_PRIVATE) ? hParent : IMG_NULL; /* Lookup the parent handle */ eError = GetHandleData(psBase, &psPHandleData, hParent, PVRSRV_HANDLE_TYPE_NONE); if (eError != PVRSRV_OK) { PVR_DPF((PVR_DBG_ERROR, "PVRSRVAllocSubHandle: Failed to get parent handle structure")); goto err; } if (!TEST_FLAG(eFlag, PVRSRV_HANDLE_ALLOC_FLAG_MULTI)) { /* See if there is already a handle for this data pointer */ hHandle = FindHandle(psBase, pvData, eType, hParentKey); if (hHandle != IMG_NULL) { eError = GetHandleData(psBase, &psCHandleData, hHandle, eType); if (eError != PVRSRV_OK) { PVR_DPF((PVR_DBG_ERROR, "PVRSRVAllocSubHandle: Lookup of existing handle failed")); goto err; } PVR_ASSERT(hParentKey != IMG_NULL && ParentHandle(psCHandleData) == hParent); /* * If the client is willing to share a handle, the * existing handle is marked as shareable, and the * existing handle has the same parent, return the * existing handle. */ if (TEST_FLAG(psCHandleData->eFlag & eFlag, PVRSRV_HANDLE_ALLOC_FLAG_SHARED) && ParentHandle(psCHandleData) == hParent) { psCHandleData->ui32Refs++; *phHandle = hHandle; eError = PVRSRV_OK; goto err; } eError = PVRSRV_ERROR_HANDLE_NOT_SHAREABLE; goto err; } } eError = AllocHandle(psBase, &hHandle, pvData, eType, eFlag, hParentKey); if (eError != PVRSRV_OK) { goto err; } eError = GetHandleData(psBase, &psCHandleData, hHandle, PVRSRV_HANDLE_TYPE_NONE); if (eError != PVRSRV_OK) { PVR_DPF((PVR_DBG_ERROR, "PVRSRVAllocSubHandle: Failed to get parent handle structure")); /* If we were able to allocate the handle then there should be no reason why we can't also get it's handle structure. Otherwise something has gone badly wrong. */ PVR_ASSERT(eError == PVRSRV_OK); goto err; } /* * Get the parent handle structure again, in case the handle * structure has moved (depending on the implementation * of AllocHandle). */ eError = GetHandleData(psBase, &psPHandleData, hParent, PVRSRV_HANDLE_TYPE_NONE); if (eError != PVRSRV_OK) { PVR_DPF((PVR_DBG_ERROR, "PVRSRVAllocSubHandle: Failed to get parent handle structure")); FreeHandle(psBase, hHandle, eType, IMG_NULL); goto err; } eError = AdoptChild(psBase, psPHandleData, psCHandleData); if (eError != PVRSRV_OK) { PVR_DPF((PVR_DBG_ERROR, "PVRSRVAllocSubHandle: Parent handle failed to adopt subhandle")); FreeHandle(psBase, hHandle, eType, IMG_NULL); goto err; } *phHandle = hHandle; eError = PVRSRV_OK; err: return eError; }
/*! ****************************************************************************** @Function PVRSRVAllocHandle @Description Allocate a handle @Input phHandle - location for new handle pvData - pointer to resource to be associated with the handle eType - the type of resource @Output phHandle - points to new handle @Return Error code or PVRSRV_OK ******************************************************************************/ PVRSRV_ERROR PVRSRVAllocHandle(PVRSRV_HANDLE_BASE *psBase, IMG_HANDLE *phHandle, IMG_VOID *pvData, PVRSRV_HANDLE_TYPE eType, PVRSRV_HANDLE_ALLOC_FLAG eFlag) { IMG_HANDLE hHandle; PVRSRV_ERROR eError; *phHandle = IMG_NULL; /* PVRSRV_HANDLE_TYPE_NONE is reserved for internal use */ PVR_ASSERT(eType != PVRSRV_HANDLE_TYPE_NONE); PVR_ASSERT(gpsHandleFuncs); if (psBase == IMG_NULL) { PVR_DPF((PVR_DBG_ERROR, "PVRSRVAllocHandle: Missing handle base")); eError = PVRSRV_ERROR_INVALID_PARAMS; goto exit_AllocHandle; } if (!TEST_FLAG(eFlag, PVRSRV_HANDLE_ALLOC_FLAG_MULTI)) { /* See if there is already a handle for this data pointer */ hHandle = FindHandle(psBase, pvData, eType, IMG_NULL); if (hHandle != IMG_NULL) { HANDLE_DATA *psHandleData = IMG_NULL; eError = GetHandleData(psBase, &psHandleData, hHandle, eType); if (eError != PVRSRV_OK) { PVR_DPF((PVR_DBG_ERROR, "PVRSRVAllocHandle: Lookup of existing handle failed (%s)", PVRSRVGetErrorStringKM(eError))); goto exit_AllocHandle; } /* * If the client is willing to share a handle, and the * existing handle is marked as shareable, return the * existing handle. */ if (TEST_FLAG(psHandleData->eFlag & eFlag, PVRSRV_HANDLE_ALLOC_FLAG_SHARED)) { psHandleData->ui32Refs++; *phHandle = hHandle; eError = PVRSRV_OK; goto exit_AllocHandle; } eError = PVRSRV_ERROR_HANDLE_NOT_SHAREABLE; goto exit_AllocHandle; } } eError = AllocHandle(psBase, phHandle, pvData, eType, eFlag, IMG_NULL); exit_AllocHandle: return eError; }
_Use_decl_annotations_ int WINAPI WinMain(HINSTANCE instance, HINSTANCE prevInstance, LPSTR commandLine, int commandShow) { UMSpinLock spinLock; UMCriticalSection cs; CRITICAL_SECTION cs2; LARGE_INTEGER freq, start, stop, osCS, myCS; HANDLE threads[64]; UMSpinLockInitialize(&spinLock); UMCriticalSectionInitialize(&cs, _countof(threads)); InitializeCriticalSection(&cs2); for (uint32_t i = 0; i < _countof(threads); ++i) { threads[i] = CreateThread(NULL, 0, ThreadProcSpinLock, &spinLock, 0, NULL); assert(threads[i] != NULL); } WaitForMultipleObjects(_countof(threads), threads, TRUE, INFINITE); uint32_t value = buffer[0]; for (uint32_t i = 0; i < _countof(buffer); ++i) { if (buffer[i] != value) { OutputDebugString(L"\n\nERROR ERROR\n"); assert(false); } } QueryPerformanceFrequency(&freq); QueryPerformanceCounter(&start); for (uint32_t i = 0; i < _countof(threads); ++i) { CloseHandle(threads[i]); threads[i] = CreateThread(NULL, 0, ThreadProcCS, &cs, 0, NULL); assert(threads[i] != NULL); } WaitForMultipleObjects(_countof(threads), threads, TRUE, INFINITE); QueryPerformanceCounter(&stop); myCS.QuadPart = stop.QuadPart - start.QuadPart; value = buffer[0]; for (uint32_t i = 0; i < _countof(buffer); ++i) { if (buffer[i] != value) { OutputDebugString(L"\n\nERROR ERROR\n"); assert(false); } } QueryPerformanceCounter(&start); for (uint32_t i = 0; i < _countof(threads); ++i) { CloseHandle(threads[i]); threads[i] = CreateThread(NULL, 0, ThreadProcOSCS, &cs2, 0, NULL); assert(threads[i] != NULL); } WaitForMultipleObjects(_countof(threads), threads, TRUE, INFINITE); QueryPerformanceCounter(&stop); osCS.QuadPart = stop.QuadPart - start.QuadPart; value = buffer[0]; for (uint32_t i = 0; i < _countof(buffer); ++i) { if (buffer[i] != value) { OutputDebugString(L"\n\nERROR ERROR\n"); assert(false); } } for (uint32_t i = 0; i < _countof(threads); ++i) { CloseHandle(threads[i]); } UMCriticalSectionDestroy(&cs); typedef struct { uint32_t X; float F; } Foo; Foo f; Foo* g; Handle h; char buffer[] = "Hello, World"; wchar_t buffer2[100]; char buffer3[100]; GameObject go1; GameObject go2; AabbNode* root = NULL; go1.Signature = SIGNATURE; go2.Signature = SIGNATURE; AabbNodeStartup(GetGameObjectBounds); go1.Node = AabbNodeInsert(&root, go1.Mins, go1.Maxs, &go1); go2.Node = AabbNodeInsert(&root, go2.Mins, go2.Maxs, &go2); // AabbNodeRemove(&root, root); AabbNodeRemove(&root, go1.Node); AabbNodeRemove(&root, go2.Node); UNREFERENCED_PARAMETER(instance); UNREFERENCED_PARAMETER(prevInstance); UNREFERENCED_PARAMETER(commandLine); UNREFERENCED_PARAMETER(commandShow); f.X = 3; f.F = -234.8f; if (!HandleTableStartup()) { return -1; } h = AllocHandle(&f, 1); if (h == InvalidHandle) { return -2; } g = (Foo*)GetObjectFromHandle(h); if (g == 0) { return -3; } FreeHandle(h); HandleTableShutdown(); if (!ConvertCharToWChar(buffer, buffer2, 100)) { return -3; } if (!ConvertWCharToChar(buffer2, buffer3, 100)) { return -4; } return 0; }
SYSCALL int Spawn (struct SpawnArgs *user_sa, void **user_segments, int segment_cnt) { struct Process *proc, *current; void *segments[NSPAWNSEGMENTS]; struct Segment *vs_ptrs[NSPAWNSEGMENTS]; int h; int t; struct SpawnArgs sa; current = GetCurrentProcess(); if (!(current->flags & PROCF_FILESYS)) return privilegeErr; if (segment_cnt < 1 || segment_cnt > NSPAWNSEGMENTS) return paramErr; CopyIn (&sa, user_sa, sizeof sa); CopyIn (segments, user_segments, sizeof (void *) * segment_cnt); if (free_handle_cnt < (1 + 3 + 1) || free_process_cnt < 1) return resourceErr; if (sa.namespace_handle == -1) return paramErr; if (FindHandle (current, sa.namespace_handle) == NULL) return paramErr; if (SegmentFindMultiple (vs_ptrs, segments, segment_cnt) < 0) return paramErr; DisablePreemption(); h = AllocHandle(); proc = AllocProcess(); proc->handle = h; proc->flags = sa.flags & ~PROCF_SYSTEMMASK; proc->namespace_handle = sa.namespace_handle; handle_table[sa.namespace_handle].owner = proc; handle_table[sa.namespace_handle].flags |= HANDLEF_GRANTED_ONCE; proc->sighangup_handle = AllocHandle(); SetObject (proc, proc->sighangup_handle, HANDLE_TYPE_SYSTEMEVENT, NULL); handle_table[proc->sighangup_handle].flags |= HANDLEF_GRANTED_ONCE; proc->sigterm_handle = AllocHandle(); SetObject (proc, proc->sigterm_handle, HANDLE_TYPE_PROCESS, NULL); handle_table[proc->sigterm_handle].flags |= HANDLEF_GRANTED_ONCE; current->argv = sa.argv; current->argc = sa.argc; current->envv = sa.envv; current->envc = sa.envc; ArchAllocProcess(proc, sa.entry, sa.stack_top); SetObject (current, h, HANDLE_TYPE_PROCESS, proc); for (t=0; t < segment_cnt; t++) { PmapRemoveRegion (vs_ptrs[t]); vs_ptrs[t]->owner = proc; } proc->state = PROC_STATE_READY; SchedReady(proc); return h; }
struct Process *CreateProcess (void (*entry)(void), void *stack, int policy, int priority, bits32_t flags, struct CPU *cpu) { int handle; struct Process *proc; proc = AllocProcess(); handle = AllocHandle(); KASSERT (handle >= 0); SetObject (proc, handle, HANDLE_TYPE_PROCESS, proc); proc->handle = handle; proc->exit_status = 0; proc->flags = flags; // FIXME: Move into AllocProcess ???????????????? LIST_INIT (&proc->pending_handle_list); LIST_INIT (&proc->close_handle_list); PmapInit (proc); proc->task_state.cpu = cpu; proc->task_state.flags = 0; proc->task_state.pc = (uint32)entry; proc->task_state.r0 = 0; if (proc->flags & PROCF_DAEMON) { proc->task_state.cpsr = cpsr_dnm_state | SYS_MODE | CPSR_DEFAULT_BITS; } else { proc->task_state.cpsr = cpsr_dnm_state | USR_MODE | CPSR_DEFAULT_BITS; } proc->task_state.r1 = 0; proc->task_state.r2 = 0; proc->task_state.r3 = 0; proc->task_state.r4 = 0; proc->task_state.r5 = 0; proc->task_state.r6 = 0; proc->task_state.r7 = 0; proc->task_state.r8 = 0; proc->task_state.r9 = 0; proc->task_state.r10 = 0; proc->task_state.r11 = 0; proc->task_state.r12 = 0; proc->task_state.sp = (uint32)stack; proc->task_state.lr = 0; if (policy == SCHED_RR || policy == SCHED_FIFO) { proc->quanta_used = 0; proc->sched_policy = policy; proc->tickets = priority; SchedReady (proc); } else if (policy == SCHED_OTHER) { proc->quanta_used = 0; proc->sched_policy = policy; proc->tickets = priority; proc->stride = STRIDE1 / proc->tickets; proc->remaining = proc->stride; proc->pass = global_pass; SchedReady (proc); } else if (policy == SCHED_IDLE) { cpu->idle_process = proc; } return proc; }