An algorithm for an array free list of contiguous resource blocks

Source: Internet
Author: User
Tags array definition file system printf resource

How to manage idle resources, it is obvious to organize into a two-way linked list, called Freelist, and then each time from the list to remove one, release the time to put back. The best strategy for reducing fragmentation is to prioritize the recently released one, if you could consider merging, like a partner system, that would be great. This article gives a generic resource allocation algorithm that maps resources to an integer ID, based entirely on an array without memory management. There is no need to allocate structure bodies.

When organizing a linked list, memory management consumes a lot of work, and the forward pointer and back pointer modifications must have these pointers. A typical data structure is the List_head architecture of the Linux kernel. However, static bitmap-like resources are not suitable for list_head, because such resources themselves can be mapped to a continuous number of natural count ID, more typical is the disk free block, the allocation of contiguous memory block.

Since the resource location is continuous, it can be represented by a continuous number of natural numbers, and all resources can be represented as an array-an arraya of IDs mapped to natural numbers.

Next we need another array to represent the free list, recorded as Arrayb.

Next, then, is the construction of the Arrayb ... The size of the Arrayb equals the size of the Arraya plus 1, the extra element can be used as a fixed point, it will not be allocated. Arrayb element size is Arraya array size occupies twice times the number of bytes, is to store two index in an element, such as array size can be expressed in 8-bit data, that is, up to 256 elements, then arrayb elements should be 2*8 so large, for example:

Array size: short-up to 65,536 elements

Arraya array definition: int Arraya[max]; Max Max 65536

Arrayb array Definition: int arrayb[];int type two short

Structural body visualization Representation Arrayb:

#define NUM    8
struct Freehl {short
        high_preindex;//Represents the index short low_nextindex in the previous Arraya array
        ;  Represents the index in the latter Arraya array
};
struct Freehl  freelist[num+1];

The equivalent of splitting the arrayb into two halves.

You can then link operations in a contiguous array space. In fact, this array represents the Freelist and the Prev,next freelist of the pointer is consistent, the array subscript is also a pointer, but in the array representation of the freelist, using the relative pointer offset, expressed as subscript!

Here is an algorithm implementation problem, very simple. After assigning a index in freelist, it is necessary to modify its forward index and forward index, which is reversed to the release process and the allocation process. The code is as follows:

 short data[num];//is for Arraya struct Freehl freelist[num+1];/is for Arrayb//Represents a next assigned index; unsigned int position = 0;
/Global one-time initialization, note that if the serialization to the file,//Can not initialize again, should be from the file deserialization to initialize.
        void List_init () {int i = 0, j =-1;
                for (; i < num+1; i++) {Freelist[i].high_preindex = (i + NUM)% (num+1);
        Freelist[i].low_nextindex = (i + 1)% (num+1);
} position = 0;
        }//allocating interface int Nalloc () {int ret_index =-1, Next_index =-1, Pre_index =-1;
    Ret_index = position;
    Saves the forward index Next_index = Freelist[position].low_nextindex to which the index is currently assigned;
    Save the index pre_index = freelist[position].high_preindex;
        Assigns the current index//http://www.bianceng.cn position = Next_index;
        if (Ret_index = = Next_index) {return-1;
    //Update the index Freelist[freelist[ret_index].high_preindex].low_nextindex = Next_index to the index before the current index; Update the forward index freelist[to index after the current indexFreelist[ret_index].low_nextindex].high_preindex = Pre_index;
return ret_index;
    }//free interface int nfree (unsigned int id) {int pos_pre =-1, Pos_next =-1;
    Save the next index to assign to index, reduce fragmentation and more easily hit cache pos_pre = Freelist[position].high_preindex;
        Releases the element freelist[pos_pre].low_nextindex = ID of index as ID;
        Freelist[id].high_preindex = Pos_pre;
    Freelist[id].low_nextindex = position;
The next index to be assigned is the index position = ID just released; }

Here is a test:

int main (int argc, char **argv)
{
        list_init ();
        int i = 0;
        printf ("begin\n");
        for (; i < num+1; i++) {
                printf ("\n%d \", Nalloc ());
        }
        Nfree (5);
        Nfree (0);
        Nfree (7);
        for (i = 0; i < num+1 i++) {
                printf ("\n%d \", Nalloc ());
        }
        printf ("\nend\n");
}

Where is this code used? As I said before, where is the occasion where resources can be expressed as sequential IDs? In the authoring of a UNIX file system, I said that the free I node and free block allocation algorithm is not good, and the above method can be used, the effect is better, that is, a little bit of work in each allocation/release time, you can avoid at a certain point of time to do a lot of accumulated heavy work. This algorithm eliminates the traversal operation, and the cost is to occupy a bit of contiguous address space.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.