Search Results for 'allocation'

1 POSTS

Have you got stucked in the development while aiming to implement a modern and very efficient alorithm? Here's a start up point before getting to implement them.

Simple Memory Allocation Algorithms

Memory allocation is a process that assigns chunks of memory upon request of the various processes running on a machine. Today, there are several well known efficient algorithms used on high-end operating systems but their mathematical models are quite difficult to comprehend from the first time by most amateur operating system developers. That’s why most of the simple operating systems available on the amateur websites use less efficient memory allocation algorithms but highly comprehensible and very educative.

This article focuses on three simple algorithms you may want to experiment on your osdev projects.

The Simplest Algorithm

The complexity of an algorithm is given by the number of steps, their iteration and their timing. Since we are talking about the simplest one, it will have the fastest memory release function: it will do nothing!

The allocator will just store the address of the next free available memory block and allocate requested chunks continuously. A brief implementation follows:

Extremely inefficient, but stable, no different block will overlap to cause memory sharing violations. Since the memory will be consumed after a certain number of allocations, we must implement a mechanism to re-enter the released blocks back in a list of available locations.

The “First Fit” Algorithm

The first fit algorithm basically extends the previous one by implementing a list of free blocks. When the allocator will receive a request of X bytes, it will lookup the list for an available block to fit X bytes. Since aiming to find a perfect match my yield negative results we will allocate memory for the X bytes in the first block equal or greater than X.

Of course, this will lead to an undesired effect: memory fragmentation. Larger block will be split into smaller ones to fit the requested amounts of memory and we may end up with the impossibility of serving a process requesting Y bytes although the sum of all free, but small, blocks is way greater than Y.

There is enough room for optimization for the above code, especially for the management of the two lists. Chained links are optimal and sorting them when the CPU is idle is a good idea, especially if followed by testing if any blocks are adjacent in order to be merged.

The “Buddy” Algorithm

The buddy algorithm is an optimized version of the “first fit” allocator. It will only allocate blocks of certain sizes, maintaining lists for each series of blocks of a specific size. The permitted sizes are often powers of two, aiming to match blocks of basic types which have sizes of powers of two and because splitting a lager block in two will result a pair of permitted blocks of inferior permitted size. Some implementations use other series of sizes, such as the Fibonacci sequence.

The main advantage compared to “first fit” is speed. We know exactly where to lookup for free blocks, and if we don’t find one we go straight to the next list, of larger blocks. But as its ancestor, it suffers from memory fragmentation.

Let’s suppose a process requests a block of 7 bytes. The allocator will automatically lookup the list of 8 bytes blocks and assign one. There will be a one byte loss in the allocated block. But what if the process then requests a block of 9 bytes? It will look up the 16 bytes block list and return one block. The loss will consist in 7 unused bytes.

That may not sound very tragic, but multiply the amounts with 1K or 1MB to get the figures quite closed to the reality. The lost memory space is common known as slack space. Fragmentation can be reduced by making the block sizes closer together.