Exemplo n.º 1
0
        internal static void Collect()
        {
            SortObjects();

            // unmark objects
            int count = mAllocatedObjectCount;

            for (int i = 0; i < count; i++)
            {
                mAllocatedObjectSize[i] &= 0x7FFFFFFF;
            }

            // trace stack
            var threads     = Scheduler.SystemProcess.Threads;
            int threadcount = threads.Count;

            for (int i = 0; i < threadcount; i++)
            {
                var  thread  = threads[i];
                uint limit   = thread.StackTop;
                uint pointer = thread.StackCurrent;
                while (pointer < limit)
                {
                    MarkObject(*(uint *)pointer);
                    pointer += 4;
                }
            }

            // trace global
            uint start = Native.GlobalVarStart();
            uint end   = Native.GlobalVarEnd();

            while (start < end)
            {
                MarkObject(*(uint *)start);
                start += 4;
            }

            // free unmarked objects
            int  index       = 0;
            uint MemoryFreed = 0;

            for (int i = 0; i < count; i++)
            {
                if ((mAllocatedObjectSize[i] & (1U << 31)) != 0)
                {
                    mAllocatedObjects[index]    = mAllocatedObjects[i];
                    mAllocatedObjectSize[index] = mAllocatedObjectSize[i];
                    index++;
                }
                else
                {
                    MemoryFreed += mAllocatedObjectSize[i];
                    Heap.Free(mAllocatedObjects[i], mAllocatedObjectSize[i]);
                }
            }

            mAllocatedObjectCount = index;
            mMemoryUsage         -= MemoryFreed;

            Debug.Write("[GC]\tMemory Freed: %d\n", MemoryFreed);
        }