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

« back to all changes in this revision

Viewing changes to source/blender/blenlib/BLI_buffer.h

  • 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:
 
1
/*
 
2
 * ***** BEGIN GPL LICENSE BLOCK *****
 
3
 *
 
4
 * This program is free software; you can redistribute it and/or
 
5
 * modify it under the terms of the GNU General Public License
 
6
 * as published by the Free Software Foundation; either version 2
 
7
 * of the License, or (at your option) any later version.
 
8
 *
 
9
 * This program is distributed in the hope that it will be useful,
 
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
 * GNU General Public License for more details.
 
13
 *
 
14
 * You should have received a copy of the GNU General Public License
 
15
 * along with this program; if not, write to the Free Software Foundation,
 
16
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 
17
 *
 
18
 * ***** END GPL LICENSE BLOCK *****
 
19
 */
 
20
 
 
21
#ifndef __BLI_BUFFER_H__
 
22
#define __BLI_BUFFER_H__
 
23
 
 
24
/* Note: this more or less fills same purpose as BLI_array, but makes
 
25
 * it much easier to resize the array outside of the function it was
 
26
 * declared in since */
 
27
 
 
28
/* Usage examples:
 
29
 *
 
30
 * { 
 
31
 *     BLI_buffer_declare_static(int, my_int_array, BLI_BUFFER_NOP, 32);
 
32
 *
 
33
 *     BLI_buffer_append(my_int_array, int, 42);
 
34
 *     assert(my_int_array.count == 1);
 
35
 *     assert(BLI_buffer_at(my_int_array, int, 0) == 42);
 
36
 *
 
37
 *     BLI_buffer_free(&my_int_array);
 
38
 * }
 
39
 */
 
40
 
 
41
typedef struct {
 
42
        void *data;
 
43
        const int elem_size;
 
44
        int count, alloc_count;
 
45
        int flag;
 
46
} BLI_Buffer;
 
47
 
 
48
enum {
 
49
        BLI_BUFFER_NOP        = 0,
 
50
        BLI_BUFFER_USE_STATIC = (1 << 0),
 
51
        BLI_BUFFER_USE_CALLOC = (1 << 1),  /* ensure the array is always calloc'd */
 
52
};
 
53
 
 
54
#define BLI_buffer_declare_static(type_, name_, flag_, static_count_) \
 
55
        type_ *name_ ## _static_[static_count_]; \
 
56
        BLI_Buffer name_ = { \
 
57
        /* clear the static memory if this is a calloc'd array */ \
 
58
        ((void)((flag_ & BLI_BUFFER_USE_CALLOC) ? \
 
59
                  memset(name_ ## _static_, 0, sizeof(name_ ## _static_)) : 0\
 
60
        ), /* memset-end */ \
 
61
                            name_ ## _static_), \
 
62
                            sizeof(type_), \
 
63
                            0, \
 
64
                            static_count_, \
 
65
                            BLI_BUFFER_USE_STATIC | flag_}
 
66
 
 
67
/* never use static*/
 
68
#define BLI_buffer_declare(type_, name_, flag_) \
 
69
        BLI_Buffer name_ = {NULL, \
 
70
                            sizeof(type_), \
 
71
                            0, \
 
72
                            0, \
 
73
                            flag_}
 
74
 
 
75
 
 
76
#define BLI_buffer_at(buffer_, type_, index_) ( \
 
77
        (((type_ *)(buffer_)->data)[(BLI_assert(sizeof(type_) == (buffer_)->elem_size)), index_]))
 
78
 
 
79
#define BLI_buffer_append(buffer_, type_, val_)  ( \
 
80
        BLI_buffer_resize(buffer_, (buffer_)->count + 1), \
 
81
        (BLI_buffer_at(buffer_, type_, (buffer_)->count - 1) = val_) \
 
82
)
 
83
 
 
84
/* Never decreases the amount of memory allocated */
 
85
void BLI_buffer_resize(BLI_Buffer *buffer, int new_count);
 
86
 
 
87
/* Does not free the buffer structure itself */
 
88
void BLI_buffer_free(BLI_Buffer *buffer);
 
89
 
 
90
#endif  /* __BLI_BUFFER_H__ */