Events, Memory Management
Events, Memory Management 1. Call back, message pumps 2. Call backs vs messages 3. Memory management
Callback Program registers and event handler program that is called whenever the event occurs. The handler executes its code and the program returns to it state (instruction pointer) prior to the event. Method used by glut for example. Windows calls event handlers Windows Procedures
Message Pumps Program queries system for event (messages), interprets message and call appropriate function using switch. Both Windows and Linux have system level message system, called message queues in both. System Level message queues probably do not have appropriate features
Messages vs Callback Callback require virtual functions: Void Explosion::Update(){ //... GameObjectCollection damagedobjects; g_world.queryobjectsinsphere( GetDamageSphere(),damagedObjects); for each object in damagedobjects){ object.onexplosion();
Messages vs Callback Callbacks require objects to inherit from a common virtual base class with a virtual function for each event type. This makes adding new events, say in a script impossible. Better to have event class and have each object handle event directly as a message.
Messages vs Callback Message Pump can deliver messages to object in order of importance. Message Pump can hold message for a fixed time. Message Pump requires more complex code than Callbacks.
Memory Management Game often use a memory pool instead of the system heap for dynamic memory allocation. Can implement many pools to group objects of similar type and to avoid scattering objects of the same type throughout a general heap, so as to prevent cache misses. Give programmers a handle on memory sizes required so as to set minimum memory requirements.
Memory Management Memory management techniques same as covered in Operating System Sequential fit method. Buddy method Both have free nodes list, used list. Doubly linked list is used for detecting memory leaks
Sequential Fit Allocate: Find a suitable free node: Split off enough to hold data Add node to used list; keep remainder in free list. Return pointer to created used node.
Sequential Fit Deallocate: Add node to used list; keep remainder in free list. Coalesce with left right nodes if possible
Buddy System In a buddy system, the allocator will only allocate blocks of certain sizes, and has many free lists, one for each permitted size. The permitted sizes are usually either powers of two, or form a Fibonacci sequence, such that any block except the smallest can be divided into two smaller blocks of permitted sizes. When blocks are recycled, there may be some attempt to merge adjacent blocks into ones of a larger permitted size (coalescence). To make this easier, the free lists may be stored in order of address. The main advantage of the buddy system is that coalescence is cheap because the "buddy" of any free block can be calculated from its address.
Buddy System Internal Fragmentation:
Buddy System For example, an allocator in a binary buddy system might have sizes of 16, 32, 64,... 64 kb. It might start off with a single block of 64 kb. If the application requests a block of 8 kb, the allocator would check its 8 kb free list and find no free blocks of that size. It would then split the 64 kb block into two block of 32 kb, split one of them into two blocks of 16 kb, and split one of them into two blocks of 8 kb. The allocator would then return one of the 8 kb blocks to the application and keep the remaining three blocks of 8 kb, 16 kb, and 32 kb on the appropriate free lists. If the application then requested a block of 10 kb, the allocator would round this request up to 16 kb, and return the 16 kb block from its free list, wasting 6 kb in the process. A Fibonacci buddy system might use block sizes 16, 32, 48, 80, 128, 208,... bytes, such that each size is the sum of the two preceding sizes. When splitting a block from one free list, the two parts get added to the two preceding free lists. A buddy system can work very well or very badly, depending on how the chosen sizes interact with typical requests for memory and what the pattern of returned blocks is. The rounding typically leads to a significant amount of wasted memory, which is called internal fragmentation. This can be reduced by making the permitted block sizes closer together.
Types of Sequential Fit Allocation: First fit Choose the first free block that fits the data request. Best Fit Try to find exact fitting block. Keep a pointer to best fitting block seen. Others optimal fit, worst fit. Deallocate: Memory location Find free block before and after. Increasing size,decreasing size lists
Memory Management In reviewing game memory management methods, I learned a trick I didn't know: how to construct a double linked list with only one pointer in each node! OGRE Memory Class Boot Memory Management
XOR Trick C=Current node. N = next node. P=Previous node. Store P xor N in Current node. Moving down the list (P xor N) xor P =N. Moving up the list (P xor N) xor N =P.