~ubuntu-branches/ubuntu/trusty/blender/trusty

« back to all changes in this revision

Viewing changes to source/blender/blenlib/intern/BLI_mempool.c

  • Committer: Package Import Robot
  • Author(s): Jeremy Bicha
  • Date: 2013-03-06 12:08:47 UTC
  • mfrom: (1.5.1) (14.1.8 experimental)
  • Revision ID: package-import@ubuntu.com-20130306120847-frjfaryb2zrotwcg
Tags: 2.66a-1ubuntu1
* Resynchronize with Debian (LP: #1076930, #1089256, #1052743, #999024,
  #1122888, #1147084)
* debian/control:
  - Lower build-depends on libavcodec-dev since we're not
    doing the libav9 transition in Ubuntu yet

Show diffs side-by-side

added added

removed removed

Lines of Context:
27
27
 
28
28
/** \file blender/blenlib/intern/BLI_mempool.c
29
29
 *  \ingroup bli
30
 
 */
31
 
 
32
 
/*
 
30
 *
33
31
 * Simple, fast memory allocator for allocating many elements of the same size.
34
32
 */
35
33
 
45
43
#include <string.h>
46
44
#include <stdlib.h>
47
45
 
48
 
/* note: copied from BKE_utildefines.h, don't use here because we're in BLI */
 
46
/* note: copied from BLO_blend_defs.h, don't use here because we're in BLI */
49
47
#ifdef __BIG_ENDIAN__
50
48
/* Big Endian */
51
 
#  define MAKE_ID(a,b,c,d) ( (int)(a)<<24 | (int)(b)<<16 | (c)<<8 | (d) )
 
49
#  define MAKE_ID(a, b, c, d) ( (int)(a) << 24 | (int)(b) << 16 | (c) << 8 | (d) )
52
50
#else
53
51
/* Little Endian */
54
 
#  define MAKE_ID(a,b,c,d) ( (int)(d)<<24 | (int)(c)<<16 | (b)<<8 | (a) )
 
52
#  define MAKE_ID(a, b, c, d) ( (int)(d) << 24 | (int)(c) << 16 | (b) << 8 | (a) )
55
53
#endif
56
54
 
57
55
#define FREEWORD MAKE_ID('f', 'r', 'e', 'e')
58
56
 
 
57
/* currently totalloc isnt used */
 
58
// #define USE_TOTALLOC
 
59
 
59
60
typedef struct BLI_freenode {
60
61
        struct BLI_freenode *next;
61
62
        int freeword; /* used to identify this as a freed node */
112
113
        pool->pchunk = pchunk;
113
114
        pool->csize = esize * pchunk;
114
115
        pool->chunks.first = pool->chunks.last = NULL;
 
116
        pool->totalloc = 0;
115
117
        pool->totused = 0;
116
118
 
117
119
        maxchunks = totelem / pchunk + 1;
161
163
                        }
162
164
                }
163
165
 
164
 
                /* set the end of this chunks memoryy to the new tail for next iteration */
 
166
                /* set the end of this chunks memory to the new tail for next iteration */
165
167
                lasttail = curnode;
166
 
 
 
168
#ifdef USE_TOTALLOC
167
169
                pool->totalloc += pool->pchunk;
 
170
#endif
168
171
        }
169
172
        /* terminate the list */
170
173
        curnode->next = NULL;
215
218
                        }
216
219
                }
217
220
                curnode->next = NULL; /* terminate the list */
218
 
 
 
221
#ifdef USE_TOTALLOC
219
222
                pool->totalloc += pool->pchunk;
 
223
#endif
220
224
        }
221
225
 
222
226
        retval = pool->free;
237
241
        return retval;
238
242
}
239
243
 
240
 
/* doesnt protect against double frees, don't be stupid! */
 
244
/**
 
245
 * Free an element from the mempool.
 
246
 *
 
247
 * \note doesnt protect against double frees, don't be stupid!
 
248
 */
241
249
void BLI_mempool_free(BLI_mempool *pool, void *addr)
242
250
{
243
251
        BLI_freenode *newhead = addr;
244
252
 
245
253
        if (pool->flag & BLI_MEMPOOL_ALLOW_ITER) {
 
254
#ifndef NDEBUG
 
255
                /* this will detect double free's */
 
256
                BLI_assert(newhead->freeword != FREEWORD);
 
257
#endif
246
258
                newhead->freeword = FREEWORD;
247
259
        }
248
260
 
276
288
                }
277
289
 
278
290
                BLI_addtail(&pool->chunks, first);
 
291
#ifdef USE_TOTALLOC
279
292
                pool->totalloc = pool->pchunk;
 
293
#endif
280
294
 
281
295
                pool->free = first->data; /* start of the list */
282
296
                for (tmpaddr = first->data, i = 0; i < pool->pchunk; i++) {
295
309
 
296
310
void *BLI_mempool_findelem(BLI_mempool *pool, int index)
297
311
{
298
 
        if (!(pool->flag & BLI_MEMPOOL_ALLOW_ITER)) {
299
 
                fprintf(stderr, "%s: Error! you can't iterate over this mempool!\n", __func__);
300
 
                return NULL;
301
 
        }
302
 
        else if ((index >= 0) && (index < pool->totused)) {
 
312
        BLI_assert(pool->flag & BLI_MEMPOOL_ALLOW_ITER);
 
313
 
 
314
        if ((index >= 0) && (index < pool->totused)) {
303
315
                /* we could have some faster mem chunk stepping code inline */
304
316
                BLI_mempool_iter iter;
305
317
                void *elem;
306
318
                BLI_mempool_iternew(pool, &iter);
307
319
                for (elem = BLI_mempool_iterstep(&iter); index-- != 0; elem = BLI_mempool_iterstep(&iter)) {
308
320
                        /* do nothing */
309
 
                };
 
321
                }
310
322
                return elem;
311
323
        }
312
324
 
313
325
        return NULL;
314
326
}
315
327
 
 
328
/**
 
329
 * \param data array of pointers at least the size of 'pool->totused'
 
330
 */
 
331
void BLI_mempool_as_array(BLI_mempool *pool, void **data)
 
332
{
 
333
        BLI_mempool_iter iter;
 
334
        void *elem;
 
335
        void **p = data;
 
336
        BLI_assert(pool->flag & BLI_MEMPOOL_ALLOW_ITER);
 
337
        BLI_mempool_iternew(pool, &iter);
 
338
        for (elem = BLI_mempool_iterstep(&iter); elem; elem = BLI_mempool_iterstep(&iter)) {
 
339
                *p++ = elem;
 
340
        }
 
341
        BLI_assert((p - data) == pool->totused);
 
342
}
 
343
 
 
344
/**
 
345
 * Allocate an array from the mempool.
 
346
 */
 
347
void *BLI_mempool_as_arrayN(BLI_mempool *pool, const char *allocstr)
 
348
{
 
349
        void *data = MEM_mallocN(BLI_mempool_count(pool) * pool->esize, allocstr);
 
350
        BLI_mempool_as_array(pool, data);
 
351
        return data;
 
352
}
 
353
 
316
354
void BLI_mempool_iternew(BLI_mempool *pool, BLI_mempool_iter *iter)
317
355
{
318
 
        if (!(pool->flag & BLI_MEMPOOL_ALLOW_ITER)) {
319
 
                fprintf(stderr, "%s: Error! you can't iterate over this mempool!\n", __func__);
320
 
                iter->curchunk = NULL;
321
 
                iter->curindex = 0;
322
 
 
323
 
                return;
324
 
        }
 
356
        BLI_assert(pool->flag & BLI_MEMPOOL_ALLOW_ITER);
325
357
 
326
358
        iter->pool = pool;
327
359
        iter->curchunk = pool->chunks.first;
391
423
 
392
424
#endif
393
425
 
 
426
/**
 
427
 * Free the mempool its self (and all elements).
 
428
 */
394
429
void BLI_mempool_destroy(BLI_mempool *pool)
395
430
{
396
431
        BLI_mempool_chunk *mpchunk = NULL;