summaryrefslogtreecommitdiff
path: root/src/object_heap.c
blob: 615cd38f7936182fdc73bc1bf4c9c44222197bec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/*
 *  object_heap.c - Management of VA-API resources
 *
 *  libva-driver-vdpau (C) 2009-2011 Splitted-Desktop Systems
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 */

#include "sysdeps.h"
#include <pthread.h>
#include "object_heap.h"

/* This code is:
 * Copyright (c) 2007 Intel Corporation. All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sub license, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 * 
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial portions
 * of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
 * IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#define LAST_FREE	-1
#define ALLOCATED	-2

/*
 * Expands the heap
 * Return 0 on success, -1 on error
 */
static int object_heap_expand( object_heap_p heap )
{
    int i;
    void *new_heap_index;
    int next_free;
    int new_heap_size = heap->heap_size + heap->heap_increment;
    int bucket_index = new_heap_size / heap->heap_increment - 1;

    if (bucket_index >= heap->num_buckets) {
        int new_num_buckets = heap->num_buckets + 8;
        void **new_bucket;

        new_bucket = realloc(heap->bucket, new_num_buckets * sizeof(void *));
        if (NULL == new_bucket) {
            return -1;
        }

        heap->num_buckets = new_num_buckets;
        heap->bucket = new_bucket;
    }

    new_heap_index = (void *) malloc( heap->heap_increment * heap->object_size );
    if ( NULL == new_heap_index )
    {
        return -1; /* Out of memory */
    }

    heap->bucket[bucket_index] = new_heap_index;
    next_free = heap->next_free;
    for(i = new_heap_size; i-- > heap->heap_size; )
    {
        object_base_p obj = (object_base_p) (new_heap_index + (i - heap->heap_size) * heap->object_size);
        obj->id = i + heap->id_offset;
        obj->next_free = next_free;
        next_free = i;
    }
    heap->next_free = next_free;
    heap->heap_size = new_heap_size;
    return 0; /* Success */
}

/*
 * Return 0 on success, -1 on error
 */
int object_heap_init( object_heap_p heap, int object_size, int id_offset)
{
    pthread_mutex_init(&heap->mutex, NULL);
    heap->object_size = object_size;
    heap->id_offset = id_offset & OBJECT_HEAP_OFFSET_MASK;
    heap->heap_size = 0;
    heap->heap_increment = 16;
    heap->next_free = LAST_FREE;
    heap->num_buckets = 0;
    heap->bucket = NULL;
    return object_heap_expand(heap);
}

/*
 * Allocates an object
 * Returns the object ID on success, returns -1 on error
 */
static int object_heap_allocate_unlocked( object_heap_p heap )
{
    object_base_p obj;
    int bucket_index, obj_index;

    if ( LAST_FREE == heap->next_free )
    {
        if( -1 == object_heap_expand( heap ) )
        {
            return -1; /* Out of memory */
        }
    }
    ASSERT( heap->next_free >= 0 );

    bucket_index = heap->next_free / heap->heap_increment;
    obj_index = heap->next_free % heap->heap_increment;

    obj = (object_base_p) (heap->bucket[bucket_index] + obj_index * heap->object_size);
    heap->next_free = obj->next_free;
    obj->next_free = ALLOCATED;
    return obj->id;
}

int object_heap_allocate( object_heap_p heap )
{
    int ret;

    pthread_mutex_lock(&heap->mutex);
    ret = object_heap_allocate_unlocked(heap);
    pthread_mutex_unlock(&heap->mutex);
    return ret;
}

/*
 * Lookup an object by object ID
 * Returns a pointer to the object on success, returns NULL on error
 */
static object_base_p object_heap_lookup_unlocked( object_heap_p heap, int id )
{
    object_base_p obj;
    int bucket_index, obj_index;

    if ( (id < heap->id_offset) || (id > (heap->heap_size+heap->id_offset)) )
    {
        return NULL;
    }
    id &= OBJECT_HEAP_ID_MASK;
    bucket_index = id / heap->heap_increment;
    obj_index = id % heap->heap_increment;
    obj = (object_base_p) (heap->bucket[bucket_index] + obj_index * heap->object_size);

	/* Check if the object has in fact been allocated */
	if ( obj->next_free != ALLOCATED )
    {
        return NULL;
    }
    return obj;
}

object_base_p object_heap_lookup( object_heap_p heap, int id )
{
    object_base_p obj;

    pthread_mutex_lock(&heap->mutex);
    obj = object_heap_lookup_unlocked(heap, id);
    pthread_mutex_unlock(&heap->mutex);
    return obj;
}

/*
 * Iterate over all objects in the heap.
 * Returns a pointer to the first object on the heap, returns NULL if heap is empty.
 */
object_base_p object_heap_first( object_heap_p heap, object_heap_iterator *iter )
{
    *iter = -1;
    return object_heap_next( heap, iter );
}

/*
 * Iterate over all objects in the heap.
 * Returns a pointer to the next object on the heap, returns NULL if heap is empty.
 */
static object_base_p
object_heap_next_unlocked( object_heap_p heap, object_heap_iterator *iter )
{
    object_base_p obj;
    int bucket_index, obj_index;
    int i = *iter + 1;

    while ( i < heap->heap_size)
    {
        bucket_index = i / heap->heap_increment;
        obj_index = i % heap->heap_increment;

        obj = (object_base_p) (heap->bucket[bucket_index] + obj_index * heap->object_size);
        if (obj->next_free == ALLOCATED)
        {
            *iter = i;
            return obj;
        }
        i++;
    }
    *iter = i;
    return NULL;
}

object_base_p
object_heap_next( object_heap_p heap, object_heap_iterator *iter )
{
    object_base_p obj;

    pthread_mutex_lock(&heap->mutex);
    obj = object_heap_next_unlocked(heap, iter);
    pthread_mutex_unlock(&heap->mutex);
    return obj;
}

/*
 * Frees an object
 */
static void
object_heap_free_unlocked( object_heap_p heap, object_base_p obj )
{
    /* Check if the object has in fact been allocated */
    ASSERT( obj->next_free == ALLOCATED );
    
    obj->next_free = heap->next_free;
    heap->next_free = obj->id & OBJECT_HEAP_ID_MASK;
}

void
object_heap_free( object_heap_p heap, object_base_p obj )
{
    if (!obj)
        return;
    pthread_mutex_lock(&heap->mutex);
    object_heap_free_unlocked(heap, obj);
    pthread_mutex_unlock(&heap->mutex);
}

/*
 * Destroys a heap, the heap must be empty.
 */
void object_heap_destroy( object_heap_p heap )
{
    object_base_p obj;
    int bucket_index, obj_index, i;

    /* Check if heap is empty */
    for (i = 0; i < heap->heap_size; i++)
    {
        /* Check if object is not still allocated */
        bucket_index = i / heap->heap_increment;
        obj_index = i % heap->heap_increment;
        obj = (object_base_p) (heap->bucket[bucket_index] + obj_index * heap->object_size);
        ASSERT( obj->next_free != ALLOCATED );
    }

    for (i = 0; i < heap->heap_size / heap->heap_increment; i++) {
        free(heap->bucket[i]);
    }

    pthread_mutex_destroy(&heap->mutex);

    free(heap->bucket);
    heap->bucket = NULL;
    heap->heap_size = 0;
    heap->next_free = LAST_FREE;
}