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

« back to all changes in this revision

Viewing changes to source/blender/blenlib/intern/stack.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
 * Contributor(s): Nicholas Bishop
 
19
 *
 
20
 * ***** END GPL LICENSE BLOCK *****
 
21
 *
 
22
 */
 
23
 
 
24
/** \file blender/blenlib/intern/stack.c
 
25
 *  \ingroup bli
 
26
 */
 
27
 
 
28
#include <string.h>
 
29
#include <stdlib.h>  /* abort() */
 
30
 
 
31
#include "BLI_stack.h"  /* own include */
 
32
 
 
33
#include "BLI_utildefines.h"
 
34
#include "MEM_guardedalloc.h"
 
35
 
 
36
struct BLI_Stack {
 
37
        void *data;
 
38
 
 
39
        int totelem;
 
40
        int maxelem;
 
41
 
 
42
        int elem_size;
 
43
};
 
44
 
 
45
BLI_Stack *BLI_stack_new(int elem_size, const char *description)
 
46
{
 
47
        BLI_Stack *stack = MEM_callocN(sizeof(*stack), description);
 
48
 
 
49
        stack->elem_size = elem_size;
 
50
 
 
51
        return stack;
 
52
}
 
53
 
 
54
void BLI_stack_free(BLI_Stack *stack)
 
55
{
 
56
        if (stack) {
 
57
                if (stack->data)
 
58
                        MEM_freeN(stack->data);
 
59
                MEM_freeN(stack);
 
60
        }
 
61
}
 
62
 
 
63
/* Gets the last element in the stack */
 
64
#define STACK_LAST_ELEM(stack__) \
 
65
        (((char *)(stack__)->data) + \
 
66
         ((stack__)->elem_size * ((stack__)->totelem - 1)))
 
67
 
 
68
void BLI_stack_push(BLI_Stack *stack, void *src)
 
69
{
 
70
        /* Ensure stack is large enough */
 
71
        if (stack->totelem == stack->maxelem) {
 
72
                if (stack->maxelem == 0) {
 
73
                        /* Initialize stack with space for a small hardcoded
 
74
                         * number of elements */
 
75
                        stack->maxelem = 32;
 
76
                        stack->data = MEM_mallocN((stack->elem_size *
 
77
                                                   stack->maxelem), AT);
 
78
                }
 
79
                else {
 
80
                        /* Double stack size */
 
81
                        int maxelem = stack->maxelem + stack->maxelem;
 
82
                        /* Check for overflow */
 
83
                        BLI_assert(maxelem > stack->maxelem);
 
84
                        stack->data = MEM_reallocN(stack->data,
 
85
                                                   (stack->elem_size *
 
86
                                                    maxelem));
 
87
                        stack->maxelem = maxelem;
 
88
                }
 
89
        }
 
90
 
 
91
        BLI_assert(stack->totelem < stack->maxelem);
 
92
 
 
93
        /* Copy source to end of stack */
 
94
        stack->totelem++;
 
95
        memcpy(STACK_LAST_ELEM(stack), src, stack->elem_size);
 
96
}
 
97
 
 
98
void BLI_stack_pop(BLI_Stack *stack, void *dst)
 
99
{
 
100
        BLI_assert(stack->totelem > 0);
 
101
        if (stack->totelem > 0) {
 
102
                memcpy(dst, STACK_LAST_ELEM(stack), stack->elem_size);
 
103
                stack->totelem--;
 
104
        }
 
105
}
 
106
 
 
107
int BLI_stack_empty(const BLI_Stack *stack)
 
108
{
 
109
        return stack->totelem == 0;
 
110
}