2 * Copyright (c) 2007 Intel Corporation. All Rights Reserved.
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:
12 * The above copyright notice and this permission notice (including the
13 * next paragraph) shall be included in all copies or substantial portions
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.
25 #include "object_heap.h"
38 * Return 0 on success, -1 on error
40 static int object_heap_expand( object_heap_p heap )
45 int new_heap_size = heap->heap_size + heap->heap_increment;
47 new_heap_index = (void *) realloc( heap->heap_index, new_heap_size * heap->object_size );
48 if ( NULL == new_heap_index )
50 return -1; /* Out of memory */
52 heap->heap_index = new_heap_index;
53 next_free = heap->next_free;
54 for(i = new_heap_size; i-- > heap->heap_size; )
56 object_base_p obj = (object_base_p) (heap->heap_index + i * heap->object_size);
57 obj->id = i + heap->id_offset;
58 obj->next_free = next_free;
61 heap->next_free = next_free;
62 heap->heap_size = new_heap_size;
63 return 0; /* Success */
67 * Return 0 on success, -1 on error
69 int object_heap_init( object_heap_p heap, int object_size, int id_offset)
71 heap->object_size = object_size;
72 heap->id_offset = id_offset & OBJECT_HEAP_OFFSET_MASK;
74 heap->heap_increment = 16;
75 heap->heap_index = NULL;
76 heap->next_free = LAST_FREE;
77 return object_heap_expand(heap);
82 * Returns the object ID on success, returns -1 on error
84 int object_heap_allocate( object_heap_p heap )
87 if ( LAST_FREE == heap->next_free )
89 if( -1 == object_heap_expand( heap ) )
91 return -1; /* Out of memory */
94 ASSERT( heap->next_free >= 0 );
96 obj = (object_base_p) (heap->heap_index + heap->next_free * heap->object_size);
97 heap->next_free = obj->next_free;
98 obj->next_free = ALLOCATED;
103 * Lookup an object by object ID
104 * Returns a pointer to the object on success, returns NULL on error
106 object_base_p object_heap_lookup( object_heap_p heap, int id )
109 if ( (id < heap->id_offset) || (id > (heap->heap_size+heap->id_offset)) )
113 id &= OBJECT_HEAP_ID_MASK;
114 obj = (object_base_p) (heap->heap_index + id * heap->object_size);
116 /* Check if the object has in fact been allocated */
117 if ( obj->next_free != ALLOCATED )
125 * Iterate over all objects in the heap.
126 * Returns a pointer to the first object on the heap, returns NULL if heap is empty.
128 object_base_p object_heap_first( object_heap_p heap, object_heap_iterator *iter )
131 return object_heap_next( heap, iter );
135 * Iterate over all objects in the heap.
136 * Returns a pointer to the next object on the heap, returns NULL if heap is empty.
138 object_base_p object_heap_next( object_heap_p heap, object_heap_iterator *iter )
142 while ( i < heap->heap_size)
144 obj = (object_base_p) (heap->heap_index + i * heap->object_size);
145 if (obj->next_free == ALLOCATED)
161 void object_heap_free( object_heap_p heap, object_base_p obj )
163 /* Don't complain about NULL pointers */
166 /* Check if the object has in fact been allocated */
167 ASSERT( obj->next_free == ALLOCATED );
169 obj->next_free = heap->next_free;
170 heap->next_free = obj->id & OBJECT_HEAP_ID_MASK;
175 * Destroys a heap, the heap must be empty.
177 void object_heap_destroy( object_heap_p heap )
181 /* Check if heap is empty */
182 for (i = 0; i < heap->heap_size; i++)
184 /* Check if object is not still allocated */
185 obj = (object_base_p) (heap->heap_index + i * heap->object_size);
186 ASSERT( obj->next_free != ALLOCATED );
188 free(heap->heap_index);
190 heap->heap_index = NULL;
191 heap->next_free = LAST_FREE;