OSDN Git Service

Test write permission
[android-x86/hardware-intel-common-libva.git] / libva / dummy_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     heap->heap_index = new_heap_index;
54     next_free = heap->next_free;
55     for(i = new_heap_size; i-- > heap->heap_size; )
56     {
57         object_base_p obj = (object_base_p) (heap->heap_index + i * heap->object_size);
58         obj->id = i + heap->id_offset;
59         obj->next_free = next_free;
60         next_free = i;
61     }
62     heap->next_free = next_free;
63     heap->heap_size = new_heap_size;
64     return 0; /* Success */
65 }
66
67 /*
68  * Return 0 on success, -1 on error
69  */
70 int object_heap_init( object_heap_p heap, int object_size, int id_offset)
71 {
72     heap->object_size = object_size;
73     heap->id_offset = id_offset & OBJECT_HEAP_OFFSET_MASK;
74     heap->heap_size = 0;
75     heap->heap_increment = 16;
76     heap->heap_index = NULL;
77     heap->next_free = LAST_FREE;
78     return object_heap_expand(heap);
79 }
80
81 /*
82  * Allocates an object
83  * Returns the object ID on success, returns -1 on error
84  */
85 int object_heap_allocate( object_heap_p heap )
86 {
87     object_base_p obj;
88     if ( LAST_FREE == heap->next_free )
89     {
90         if( -1 == object_heap_expand( heap ) )
91         {
92             return -1; /* Out of memory */
93         }
94     }
95     ASSERT( heap->next_free >= 0 );
96     
97     obj = (object_base_p) (heap->heap_index + heap->next_free * heap->object_size);
98     heap->next_free = obj->next_free;
99     obj->next_free = ALLOCATED;
100     return obj->id;
101 }
102
103 /*
104  * Lookup an object by object ID
105  * Returns a pointer to the object on success, returns NULL on error
106  */
107 object_base_p object_heap_lookup( object_heap_p heap, int id )
108 {
109     object_base_p obj;
110     if ( (id < heap->id_offset) || (id > (heap->heap_size+heap->id_offset)) )
111     {
112         return NULL;
113     }
114     id &= OBJECT_HEAP_ID_MASK;
115     obj = (object_base_p) (heap->heap_index + id * heap->object_size);
116
117         /* Check if the object has in fact been allocated */
118         if ( obj->next_free != ALLOCATED )
119     {
120         return NULL;
121     }
122     return obj;
123 }
124
125 /*
126  * Iterate over all objects in the heap.
127  * Returns a pointer to the first object on the heap, returns NULL if heap is empty.
128  */
129 object_base_p object_heap_first( object_heap_p heap, object_heap_iterator *iter )
130 {
131     *iter = -1;
132     return object_heap_next( heap, iter );
133 }
134
135 /*
136  * Iterate over all objects in the heap.
137  * Returns a pointer to the next object on the heap, returns NULL if heap is empty.
138  */
139 object_base_p object_heap_next( object_heap_p heap, object_heap_iterator *iter )
140 {
141     object_base_p obj;
142     int i = *iter + 1;
143     while ( i < heap->heap_size)
144     {
145         obj = (object_base_p) (heap->heap_index + i * heap->object_size);
146         if (obj->next_free == ALLOCATED)
147         {
148             *iter = i;
149             return obj;
150         }
151         i++;
152     }
153     *iter = i;
154     return NULL;
155 }
156
157
158
159 /*
160  * Frees an object
161  */
162 void object_heap_free( object_heap_p heap, object_base_p obj )
163 {
164     /* Don't complain about NULL pointers */
165     if (NULL != obj)
166     {
167         /* Check if the object has in fact been allocated */
168         ASSERT( obj->next_free == ALLOCATED );
169     
170         obj->next_free = heap->next_free;
171         heap->next_free = obj->id & OBJECT_HEAP_ID_MASK;
172     }
173 }
174
175 /*
176  * Destroys a heap, the heap must be empty.
177  */
178 void object_heap_destroy( object_heap_p heap )
179 {
180     object_base_p obj;
181     int i;
182     /* Check if heap is empty */
183     for (i = 0; i < heap->heap_size; i++)
184     {
185         /* Check if object is not still allocated */
186         obj = (object_base_p) (heap->heap_index + i * heap->object_size);
187         ASSERT( obj->next_free != ALLOCATED );
188     }
189     free(heap->heap_index);
190     heap->heap_size = 0;
191     heap->heap_index = NULL;
192     heap->next_free = LAST_FREE;
193 }