1 /*
2  * Copyright © 2008 Chris Wilson <chris@chris-wilson.co.uk>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it either under the terms of the GNU Lesser General Public
6  * License version 2.1 as published by the Free Software Foundation
7  * (the "LGPL") or, at your option, under the terms of the Mozilla
8  * Public License Version 1.1 (the "MPL"). If you do not alter this
9  * notice, a recipient may use your version of this file under either
10  * the MPL or the LGPL.
11  *
12  * You should have received a copy of the LGPL along with this library
13  * in the file COPYING-LGPL-2.1; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA
15  * You should have received a copy of the MPL along with this library
16  * in the file COPYING-MPL-1.1
17  *
18  * The contents of this file are subject to the Mozilla Public License
19  * Version 1.1 (the "License"); you may not use this file except in
20  * compliance with the License. You may obtain a copy of the License at
21  * http://www.mozilla.org/MPL/
22  *
23  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
24  * OF ANY KIND, either express or implied. See the LGPL or the MPL for
25  * the specific language governing rights and limitations.
26  *
27  * The Original Code is the cairo graphics library.
28  *
29  * The Initial Developer of the Original Code is Chris Wilson.
30  *
31  * Contributor(s):
32  *	Chris Wilson <chris@chris-wilson.co.uk>
33  */
34 
35 #include "config.h"
36 
37 #include "cairo-script-private.h"
38 
39 #include <limits.h> /* INT_MAX */
40 #include <string.h>
41 
42 csi_status_t
_csi_stack_init(csi_t * ctx,csi_stack_t * stack,csi_integer_t size)43 _csi_stack_init (csi_t *ctx, csi_stack_t *stack, csi_integer_t size)
44 {
45     csi_status_t status = CSI_STATUS_SUCCESS;
46 
47     stack->len = 0;
48     stack->size = size;
49     /* assert ((unsigned) size < INT_MAX / sizeof (csi_object_t)); */
50     stack->objects = _csi_alloc (ctx, size * sizeof (csi_object_t));
51     if (_csi_unlikely (stack->objects == NULL))
52 	status = _csi_error (CSI_STATUS_NO_MEMORY);
53 
54     return status;
55 }
56 
57 void
_csi_stack_fini(csi_t * ctx,csi_stack_t * stack)58 _csi_stack_fini (csi_t *ctx, csi_stack_t *stack)
59 {
60     csi_integer_t n;
61 
62     for (n = 0; n < stack->len; n++)
63 	csi_object_free (ctx, &stack->objects[n]);
64 
65     _csi_free (ctx, stack->objects);
66 }
67 
68 csi_status_t
_csi_stack_roll(csi_t * ctx,csi_stack_t * stack,csi_integer_t mod,csi_integer_t n)69 _csi_stack_roll (csi_t *ctx,
70 		 csi_stack_t *stack,
71 		 csi_integer_t mod, csi_integer_t n)
72 {
73     csi_object_t stack_copy[128];
74     csi_object_t *copy;
75     csi_integer_t last, i, len;
76 
77     switch (mod) { /* special cases */
78     case 1:
79 	last = stack->len - 1;
80 	stack_copy[0] = stack->objects[last];
81 	for (i = last; --n; i--)
82 	    stack->objects[i] = stack->objects[i-1];
83 	stack->objects[i] = stack_copy[0];
84 	return CSI_STATUS_SUCCESS;
85     case -1:
86 	last = stack->len - 1;
87 	stack_copy[0] = stack->objects[i = last - n + 1];
88 	for (; --n; i++)
89 	    stack->objects[i] = stack->objects[i+1];
90 	stack->objects[i] = stack_copy[0];
91 	return CSI_STATUS_SUCCESS;
92     }
93 
94     /* fall back to a copy */
95     if (n > ARRAY_LENGTH (stack_copy)) {
96 	if (_csi_unlikely ((unsigned) n > INT_MAX / sizeof (csi_object_t)))
97 	    return _csi_error (CSI_STATUS_NO_MEMORY);
98 	copy = _csi_alloc (ctx, n * sizeof (csi_object_t));
99 	if (copy == NULL)
100 	    return _csi_error (CSI_STATUS_NO_MEMORY);
101     } else
102 	copy = stack_copy;
103 
104     i = stack->len - n;
105     memcpy (copy, stack->objects + i, n * sizeof (csi_object_t));
106     mod = -mod;
107     if (mod < 0)
108 	mod += n;
109     last = mod;
110     for (len = n; n--; i++) {
111 	stack->objects[i] = copy[last];
112 	if (++last == len)
113 	    last = 0;
114     }
115 
116     if (copy != stack_copy)
117 	_csi_free (ctx, copy);
118 
119     return CSI_STATUS_SUCCESS;
120 }
121 
122 csi_status_t
_csi_stack_grow(csi_t * ctx,csi_stack_t * stack,csi_integer_t cnt)123 _csi_stack_grow (csi_t *ctx, csi_stack_t *stack, csi_integer_t cnt)
124 {
125     csi_integer_t newsize;
126     csi_object_t *newstack;
127 
128     if (_csi_likely (cnt <= stack->size))
129 	return CSI_STATUS_SUCCESS;
130     if (_csi_unlikely ((unsigned) cnt >= INT_MAX / sizeof (csi_object_t)))
131 	return _csi_error (CSI_STATUS_NO_MEMORY);
132 
133     newsize = stack->size;
134     do {
135 	newsize *= 2;
136     } while (newsize <= cnt);
137 
138     newstack = _csi_realloc (ctx,
139 			     stack->objects,
140 			     newsize * sizeof (csi_object_t));
141     if (_csi_unlikely (newstack == NULL))
142 	return _csi_error (CSI_STATUS_NO_MEMORY);
143 
144     stack->objects = newstack;
145     stack->size  = newsize;
146 
147     return CSI_STATUS_SUCCESS;
148 }
149 
150 csi_status_t
_csi_stack_push_internal(csi_t * ctx,csi_stack_t * stack,const csi_object_t * obj)151 _csi_stack_push_internal (csi_t *ctx, csi_stack_t *stack,
152 			  const csi_object_t *obj)
153 {
154     csi_status_t status;
155 
156     status = _csi_stack_grow (ctx, stack, stack->size + 1);
157     if (_csi_unlikely (status))
158 	return status;
159 
160     stack->objects[stack->len++] = *obj;
161     return CSI_STATUS_SUCCESS;
162 }
163 
164 csi_object_t *
_csi_stack_peek(csi_stack_t * stack,csi_integer_t i)165 _csi_stack_peek (csi_stack_t *stack, csi_integer_t i)
166 {
167     if (_csi_unlikely (stack->len < i))
168 	return NULL;
169 
170     return &stack->objects[stack->len - i -1];
171 }
172 
173 void
_csi_stack_pop(csi_t * ctx,csi_stack_t * stack,csi_integer_t count)174 _csi_stack_pop (csi_t *ctx, csi_stack_t *stack, csi_integer_t count)
175 {
176     if (_csi_unlikely (stack->len < count))
177 	count = stack->len;
178 
179     while (count--)
180 	csi_object_free (ctx, &stack->objects[--stack->len]);
181 }
182 
183 csi_status_t
_csi_stack_exch(csi_stack_t * stack)184 _csi_stack_exch (csi_stack_t *stack)
185 {
186     csi_object_t tmp;
187     csi_integer_t n;
188 
189     if (_csi_unlikely (stack->len < 2))
190 	return _csi_error (CSI_STATUS_INVALID_SCRIPT);
191 
192     n = stack->len - 1;
193     tmp = stack->objects[n];
194     stack->objects[n] = stack->objects[n - 1];
195     stack->objects[n - 1] = tmp;
196 
197     return CSI_STATUS_SUCCESS;
198 }
199