src/ucx/stack.c

branch
config
changeset 254
4784c14aa639
parent 135
471e28cca288
equal deleted inserted replaced
253:ddfead6ea863 254:4784c14aa639
1 /* 1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 * 3 *
4 * Copyright 2016 Olaf Wintermann. All rights reserved. 4 * Copyright 2017 Mike Becker, Olaf Wintermann All rights reserved.
5 * 5 *
6 * Redistribution and use in source and binary forms, with or without 6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met: 7 * modification, are permitted provided that the following conditions are met:
8 * 8 *
9 * 1. Redistributions of source code must retain the above copyright 9 * 1. Redistributions of source code must retain the above copyright
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE. 26 * POSSIBILITY OF SUCH DAMAGE.
27 */ 27 */
28 28
29 #include "stack.h" 29 #include "ucx/stack.h"
30
30 #include <string.h> 31 #include <string.h>
31 32
32 static size_t ucx_stack_align(size_t n) { 33 static size_t ucx_stack_align(size_t n) {
33 int align = n % sizeof(void*); 34 int align = n % sizeof(void*);
34 if (align) { 35 if (align) {
117 void ucx_stack_popn(UcxStack *stack, void *dest, size_t n) { 118 void ucx_stack_popn(UcxStack *stack, void *dest, size_t n) {
118 if (ucx_stack_empty(stack)) { 119 if (ucx_stack_empty(stack)) {
119 return; 120 return;
120 } 121 }
121 122
122 size_t len = ucx_stack_topsize(stack); 123 if (dest) {
123 if (len > n) { 124 size_t len = ucx_stack_topsize(stack);
124 len = n; 125 if (len > n) {
126 len = n;
127 }
128
129 memcpy(dest, stack->top, len);
125 } 130 }
126
127 memcpy(dest, stack->top, len);
128 131
129 ucx_stack_free(stack, stack->top); 132 ucx_stack_free(stack, stack->top);
130 } 133 }
131 134
132 size_t ucx_stack_avail(UcxStack *stack) { 135 size_t ucx_stack_avail(UcxStack *stack) {
139 return avail - sizeof(struct ucx_stack_metadata); 142 return avail - sizeof(struct ucx_stack_metadata);
140 } else { 143 } else {
141 return 0; 144 return 0;
142 } 145 }
143 } 146 }
147
148 void *ucx_stack_push(UcxStack *stack, size_t n, const void *data) {
149 void *space = ucx_stack_malloc(stack, n);
150 if (space) {
151 memcpy(space, data, n);
152 }
153 return space;
154 }
155
156 void *ucx_stack_pusharr(UcxStack *stack,
157 size_t nelem, size_t elsize, const void *data) {
158
159 // skip the memset by using malloc
160 void *space = ucx_stack_malloc(stack, nelem*elsize);
161 if (space) {
162 memcpy(space, data, nelem*elsize);
163 }
164 return space;
165 }

mercurial