OSDN Git Service

1900f8cdd289a4085045e7f8e48f227a2a68e925
[android-x86/hardware-intel-common-libva.git] / i965_drv_video / object_heap.c
1 /*
2  * Copyright (c) 2007 Intel Corporation. All Rights Reserved.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the
6  * "Software"), to deal in the Software without restriction, including
7  * without limitation the rights to use, copy, modify, merge, publish,
8  * distribute, sub license, and/or sell copies of the Software, and to
9  * permit persons to whom the Software is furnished to do so, subject to
10  * the following conditions:
11  * 
12  * The above copyright notice and this permission notice (including the
13  * next paragraph) shall be included in all copies or substantial portions
14  * of the Software.
15  * 
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
19  * IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
20  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */
24
25 #include "object_heap.h"
26
27 #include "assert.h"
28 #include <stdio.h>
29 #include <string.h>
30 #include <stdlib.h>
31
32 #define ASSERT  assert
33
34 #define LAST_FREE       -1
35 #define ALLOCATED       -2
36
37 /*
38  * Expands the heap
39  * Return 0 on success, -1 on error
40  */
41 static int object_heap_expand( object_heap_p heap )
42 {
43     int i;
44     void *new_heap_index;
45     int next_free;
46     int new_heap_size = heap->heap_size + heap->heap_increment;
47     
48     new_heap_index = (void *) realloc( heap->heap_index, new_heap_size * heap->object_size );
49     if ( NULL == new_heap_index )
50     {
51         return -1; /* Out of memory */
52     }
53     memset(new_heap_index + heap->heap_size*heap->object_size, 0, heap->heap_increment * new_heap_size);
54     heap->heap_index = new_heap_index;
55     next_free = heap->next_free;
56     for(i = new_heap_size; i-- > heap->heap_size; )
57     {
58         object_base_p obj = (object_base_p) (heap->heap_index + i * heap->object_size);
59         obj->id = i + heap->id_offset;
60         obj->next_free = next_free;
61         next_free = i;
62     }
63     heap->next_free = next_free;
64     heap->heap_size = new_heap_size;
65     return 0; /* Success */
66 }
67
68 /*
69  * Return 0 on success, -1 on error
70  */
71 int object_heap_init( object_heap_p heap, int object_size, int id_offset)
72 {
73     heap->object_size = object_size;
74     heap->id_offset = id_offset & OBJECT_HEAP_OFFSET_MASK;
75     heap->heap_size = 0;
76     heap->heap_increment = 16;
77     heap->heap_index = NULL;
78     heap->next_free = LAST_FREE;
79     return object_heap_expand(heap);
80 }
81
82 /*
83  * Allocates an object
84  * Returns the object ID on success, returns -1 on error
85  */
86 int object_heap_allocate( object_heap_p heap )
87 {
88     object_base_p obj;
89     if ( LAST_FREE == heap->next_free )
90     {
91         if( -1 == object_heap_expand( heap ) )
92         {
93             return -1; /* Out of memory */
94         }
95     }
96     ASSERT( heap->next_free >= 0 );
97     
98     obj = (object_base_p) (heap->heap_index + heap->next_free * heap->object_size);
99     heap->next_free = obj->next_free;
100     obj->next_free = ALLOCATED;
101     return obj->id;
102 }
103
104 /*
105  * Lookup an object by object ID
106  * Returns a pointer to the object on success, returns NULL on error
107  */
108 object_base_p object_heap_lookup( object_heap_p heap, int id )
109 {
110     object_base_p obj;
111     if ( (id < heap->id_offset) || (id > (heap->heap_size+heap->id_offset)) )
112     {
113         return NULL;
114     }
115     id &= OBJECT_HEAP_ID_MASK;
116     obj = (object_base_p) (heap->heap_index + id * heap->object_size);
117
118         /* Check if the object has in fact been allocated */
119         if ( obj->next_free != ALLOCATED )
120     {
121         return NULL;
122     }
123     return obj;
124 }
125
126 /*
127  * Iterate over all objects in the heap.
128  * Returns a pointer to the first object on the heap, returns NULL if heap is empty.
129  */
130 object_base_p object_heap_first( object_heap_p heap, object_heap_iterator *iter )
131 {
132     *iter = -1;
133     return object_heap_next( heap, iter );
134 }
135
136 /*
137  * Iterate over all objects in the heap.
138  * Returns a pointer to the next object on the heap, returns NULL if heap is empty.
139  */
140 object_base_p object_heap_next( object_heap_p heap, object_heap_iterator *iter )
141 {
142     object_base_p obj;
143     int i = *iter + 1;
144     while ( i < heap->heap_size)
145     {
146         obj = (object_base_p) (heap->heap_index + i * heap->object_size);
147         if (obj->next_free == ALLOCATED)
148         {
149             *iter = i;
150             return obj;
151         }
152         i++;
153     }
154     *iter = i;
155     return NULL;
156 }
157
158
159
160 /*
161  * Frees an object
162  */
163 void object_heap_free( object_heap_p heap, object_base_p obj )
164 {
165     /* Don't complain about NULL pointers */
166     if (NULL != obj)
167     {
168         /* Check if the object has in fact been allocated */
169         ASSERT( obj->next_free == ALLOCATED );
170     
171         obj->next_free = heap->next_free;
172         heap->next_free = obj->id & OBJECT_HEAP_ID_MASK;
173     }
174 }
175
176 /*
177  * Destroys a heap, the heap must be empty.
178  */
179 void object_heap_destroy( object_heap_p heap )
180 {
181     object_base_p obj;
182     int i;
183     /* Check if heap is empty */
184     for (i = 0; i < heap->heap_size; i++)
185     {
186         /* Check if object is not still allocated */
187         obj = (object_base_p) (heap->heap_index + i * heap->object_size);
188         ASSERT( obj->next_free != ALLOCATED );
189     }
190     free(heap->heap_index);
191     heap->heap_size = 0;
192     heap->heap_index = NULL;
193     heap->next_free = LAST_FREE;
194 }