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

« back to all changes in this revision

Viewing changes to source/blender/blenlib/intern/buffer.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:
 
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
#include "MEM_guardedalloc.h"
 
22
 
 
23
#include "BLI_buffer.h"
 
24
#include "BLI_utildefines.h"
 
25
 
 
26
#include <string.h>
 
27
 
 
28
static void *buffer_alloc(BLI_Buffer *buffer, int len)
 
29
{
 
30
        return ((buffer->flag & BLI_BUFFER_USE_CALLOC) ?
 
31
                MEM_callocN : MEM_mallocN)
 
32
                (buffer->elem_size * len, "BLI_Buffer.data");
 
33
}
 
34
 
 
35
static void *buffer_realloc(BLI_Buffer *buffer, int len)
 
36
{
 
37
        return ((buffer->flag & BLI_BUFFER_USE_CALLOC) ?
 
38
                MEM_recallocN : MEM_reallocN)
 
39
                (buffer->data, (buffer->elem_size * len));
 
40
}
 
41
 
 
42
void BLI_buffer_resize(BLI_Buffer *buffer, int new_count)
 
43
{
 
44
        if (new_count > buffer->alloc_count) {
 
45
                if (buffer->flag & BLI_BUFFER_USE_STATIC) {
 
46
                        void *orig = buffer->data;
 
47
 
 
48
                        buffer->data = buffer_alloc(buffer, new_count);
 
49
                        memcpy(buffer->data, orig, buffer->elem_size * buffer->count);
 
50
                        buffer->alloc_count = new_count;
 
51
                        buffer->flag &= ~BLI_BUFFER_USE_STATIC;
 
52
                }
 
53
                else {
 
54
                        if (buffer->alloc_count && (new_count < buffer->alloc_count * 2)) {
 
55
                                buffer->alloc_count *= 2;
 
56
                        }
 
57
                        else {
 
58
                                buffer->alloc_count = new_count;
 
59
                        }
 
60
 
 
61
                        if (buffer->data) {
 
62
                                buffer->data = buffer_realloc(buffer, buffer->alloc_count);
 
63
                        }
 
64
                        else {
 
65
                                buffer->data = buffer_alloc(buffer, buffer->alloc_count);
 
66
                        }
 
67
                }
 
68
        }
 
69
 
 
70
        buffer->count = new_count;
 
71
}
 
72
 
 
73
void BLI_buffer_free(BLI_Buffer *buffer)
 
74
{
 
75
        if ((buffer->flag & BLI_BUFFER_USE_STATIC) == 0) {
 
76
                if (buffer->data) {
 
77
                        MEM_freeN(buffer->data);
 
78
                }
 
79
        }
 
80
        memset(buffer, 0, sizeof(*buffer));
 
81
}