object_heap.c revision 7e8d39a9d261ff6b5256d7cf9c7a127947b2b2a5
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 <stdlib.h>
28#include <string.h>
29
30#include "psb_def.h"
31
32#define LAST_FREE    -1
33#define ALLOCATED    -2
34#define SUSPENDED    -3
35
36/*
37 * Expands the heap
38 * Return 0 on success, -1 on error
39 */
40static int object_heap_expand( object_heap_p heap )
41{
42    int i;
43    int malloc_error = FALSE;
44    object_base_p *new_heap_index;
45    int next_free;
46    int new_heap_size = heap->heap_size + heap->heap_increment;
47
48    new_heap_index = (object_base_p *) realloc( heap->heap_index, new_heap_size * sizeof(object_base_p) );
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) malloc(heap->object_size);
58        heap->heap_index[i] = obj;
59        if (NULL == obj)
60        {
61            malloc_error = TRUE;
62            continue; /* Clean up after the loop is completely done */
63        }
64        obj->id = i + heap->id_offset;
65        obj->next_free = next_free;
66        next_free = i;
67    }
68
69    if (malloc_error)
70    {
71        /* Clean up the mess */
72        for(i = new_heap_size; i-- > heap->heap_size; )
73        {
74            if (heap->heap_index[i])
75            {
76                free(heap->heap_index[i]);
77            }
78        }
79        /* heap->heap_index is left as is */
80        return -1; /* Out of memory */
81    }
82    heap->next_free = next_free;
83    heap->heap_size = new_heap_size;
84    return 0; /* Success */
85}
86
87/*
88 * Return 0 on success, -1 on error
89 */
90int object_heap_init( object_heap_p heap, int object_size, int id_offset)
91{
92    heap->object_size = object_size;
93    heap->id_offset = id_offset & OBJECT_HEAP_OFFSET_MASK;
94    heap->heap_size = 0;
95    heap->heap_increment = 16;
96    heap->heap_index = NULL;
97    heap->next_free = LAST_FREE;
98    return object_heap_expand(heap);
99}
100
101/*
102 * Allocates an object
103 * Returns the object ID on success, returns -1 on error
104 */
105int object_heap_allocate( object_heap_p heap )
106{
107    object_base_p obj;
108    if ( LAST_FREE == heap->next_free )
109    {
110        if( -1 == object_heap_expand( heap ) )
111        {
112            return -1; /* Out of memory */
113        }
114    }
115    ASSERT( heap->next_free >= 0 );
116
117    obj = heap->heap_index[heap->next_free];
118    heap->next_free = obj->next_free;
119    obj->next_free = ALLOCATED;
120    return obj->id;
121}
122
123/*
124 * Lookup an object by object ID
125 * Returns a pointer to the object on success, returns NULL on error
126 */
127object_base_p object_heap_lookup( object_heap_p heap, int id )
128{
129    object_base_p obj;
130    if ( (id < heap->id_offset) || (id > (heap->heap_size+heap->id_offset)) )
131    {
132        return NULL;
133    }
134    id &= OBJECT_HEAP_ID_MASK;
135    obj = heap->heap_index[id];
136
137    /* Check if the object has in fact been allocated */
138    if ( obj->next_free != ALLOCATED )
139    {
140        return NULL;
141    }
142    return obj;
143}
144
145/*
146 * Iterate over all objects in the heap.
147 * Returns a pointer to the first object on the heap, returns NULL if heap is empty.
148 */
149object_base_p object_heap_first( object_heap_p heap, object_heap_iterator *iter )
150{
151    *iter = -1;
152    return object_heap_next( heap, iter );
153}
154
155/*
156 * Iterate over all objects in the heap.
157 * Returns a pointer to the next object on the heap, returns NULL if heap is empty.
158 */
159object_base_p object_heap_next( object_heap_p heap, object_heap_iterator *iter )
160{
161    object_base_p obj;
162    int i = *iter + 1;
163    while ( i < heap->heap_size)
164    {
165        obj = heap->heap_index[i];
166        if ( (obj->next_free == ALLOCATED) || (obj->next_free == SUSPENDED) )
167        {
168            *iter = i;
169            return obj;
170        }
171        i++;
172    }
173    *iter = i;
174    return NULL;
175}
176
177
178
179/*
180 * Frees an object
181 */
182void object_heap_free( object_heap_p heap, object_base_p obj )
183{
184    /* Don't complain about NULL pointers */
185    if (NULL != obj)
186    {
187        /* Check if the object has in fact been allocated */
188        ASSERT(( obj->next_free == ALLOCATED ) || ( obj->next_free == SUSPENDED ));
189
190        obj->next_free = heap->next_free;
191        heap->next_free = obj->id & OBJECT_HEAP_ID_MASK;
192    }
193}
194
195/*
196 * Destroys a heap, the heap must be empty.
197 */
198void object_heap_destroy( object_heap_p heap )
199{
200    object_base_p obj;
201    int i;
202    for (i = 0; i < heap->heap_size; i++)
203    {
204        /* Check if object is not still allocated */
205        obj = heap->heap_index[i];
206        ASSERT( obj->next_free != ALLOCATED );
207        ASSERT( obj->next_free != SUSPENDED );
208        /* Free object itself */
209        free( obj );
210    }
211    free(heap->heap_index);
212    heap->heap_size = 0;
213    heap->heap_index = NULL;
214    heap->next_free = LAST_FREE;
215}
216
217/*
218 * Suspend an object
219 * Suspended objects can not be looked up
220 */
221void object_heap_suspend_object( object_base_p obj, int suspend)
222{
223    if (suspend)
224    {
225        ASSERT( obj->next_free == ALLOCATED );
226        obj->next_free = SUSPENDED;
227    }
228    else
229    {
230        ASSERT( obj->next_free == SUSPENDED );
231        obj->next_free = ALLOCATED;
232    }
233}
234