src/stack.c

Mon, 14 May 2018 18:12:14 +0200

author
Mike Becker <universe@uap-core.de>
date
Mon, 14 May 2018 18:12:14 +0200
changeset 308
d6f580621512
parent 301
0f83916c1639
permissions
-rw-r--r--

renames ucx_strcmp() to ucx_cmp_str()

13
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1 /*
103
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 95
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
13
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
3 *
259
2f5dea574a75 modules documentation
Mike Becker <universe@uap-core.de>
parents: 251
diff changeset
4 * Copyright 2017 Mike Becker, Olaf Wintermann All rights reserved.
103
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 95
diff changeset
5 *
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 95
diff changeset
6 * Redistribution and use in source and binary forms, with or without
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 95
diff changeset
7 * modification, are permitted provided that the following conditions are met:
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 95
diff changeset
8 *
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 95
diff changeset
9 * 1. Redistributions of source code must retain the above copyright
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 95
diff changeset
10 * notice, this list of conditions and the following disclaimer.
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 95
diff changeset
11 *
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 95
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 95
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 95
diff changeset
14 * documentation and/or other materials provided with the distribution.
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 95
diff changeset
15 *
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 95
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 95
diff changeset
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 95
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 95
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 95
diff changeset
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 95
diff changeset
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 95
diff changeset
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 95
diff changeset
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 95
diff changeset
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 95
diff changeset
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 95
diff changeset
26 * POSSIBILITY OF SUCH DAMAGE.
13
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
27 */
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
28
251
fae240d633fc changes source directory structure in preperation for autotools rollout
Mike Becker <universe@uap-core.de>
parents: 250
diff changeset
29 #include "ucx/stack.h"
fae240d633fc changes source directory structure in preperation for autotools rollout
Mike Becker <universe@uap-core.de>
parents: 250
diff changeset
30
13
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
31 #include <string.h>
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
32
185
a48428642b4e added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents: 182
diff changeset
33 static size_t ucx_stack_align(size_t n) {
a48428642b4e added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents: 182
diff changeset
34 int align = n % sizeof(void*);
a48428642b4e added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents: 182
diff changeset
35 if (align) {
a48428642b4e added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents: 182
diff changeset
36 n += sizeof(void*) - align;
a48428642b4e added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents: 182
diff changeset
37 }
a48428642b4e added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents: 182
diff changeset
38 return n;
a48428642b4e added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents: 182
diff changeset
39 }
a48428642b4e added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents: 182
diff changeset
40
a48428642b4e added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents: 182
diff changeset
41 void ucx_stack_init(UcxStack *stack, char* space, size_t size) {
a48428642b4e added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents: 182
diff changeset
42 stack->size = size - size % sizeof(void*);
a48428642b4e added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents: 182
diff changeset
43 stack->space = space;
a48428642b4e added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents: 182
diff changeset
44 stack->top = NULL;
182
998bf7c643b4 merged sstrcat function
Mike Becker <universe@uap-core.de>
parents: 177
diff changeset
45
185
a48428642b4e added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents: 182
diff changeset
46 stack->allocator.pool = stack;
a48428642b4e added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents: 182
diff changeset
47 stack->allocator.malloc = (ucx_allocator_malloc) ucx_stack_malloc;
a48428642b4e added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents: 182
diff changeset
48 stack->allocator.calloc = (ucx_allocator_calloc) ucx_stack_calloc;
a48428642b4e added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents: 182
diff changeset
49 stack->allocator.realloc = (ucx_allocator_realloc) ucx_stack_realloc;
a48428642b4e added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents: 182
diff changeset
50 stack->allocator.free = (ucx_allocator_free) ucx_stack_free;
13
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
51 }
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
52
182
998bf7c643b4 merged sstrcat function
Mike Becker <universe@uap-core.de>
parents: 177
diff changeset
53 void *ucx_stack_malloc(UcxStack *stack, size_t n) {
998bf7c643b4 merged sstrcat function
Mike Becker <universe@uap-core.de>
parents: 177
diff changeset
54
185
a48428642b4e added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents: 182
diff changeset
55 if (ucx_stack_avail(stack) < ucx_stack_align(n)) {
158
81d580042da1 Added allocator to mempool struct + fixed suncc.mk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 141
diff changeset
56 return NULL;
135
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 131
diff changeset
57 } else {
185
a48428642b4e added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents: 182
diff changeset
58 char *prev = stack->top;
a48428642b4e added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents: 182
diff changeset
59 if (stack->top) {
a48428642b4e added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents: 182
diff changeset
60 stack->top += ucx_stack_align(ucx_stack_topsize(stack));
a48428642b4e added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents: 182
diff changeset
61 } else {
a48428642b4e added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents: 182
diff changeset
62 stack->top = stack->space;
a48428642b4e added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents: 182
diff changeset
63 }
182
998bf7c643b4 merged sstrcat function
Mike Becker <universe@uap-core.de>
parents: 177
diff changeset
64
185
a48428642b4e added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents: 182
diff changeset
65 ((struct ucx_stack_metadata*)stack->top)->prev = prev;
a48428642b4e added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents: 182
diff changeset
66 ((struct ucx_stack_metadata*)stack->top)->size = n;
a48428642b4e added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents: 182
diff changeset
67 stack->top += sizeof(struct ucx_stack_metadata);
182
998bf7c643b4 merged sstrcat function
Mike Becker <universe@uap-core.de>
parents: 177
diff changeset
68
185
a48428642b4e added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents: 182
diff changeset
69 return stack->top;
15
2dc4c688c262 fixed mpool compiler warnings
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
70 }
13
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
71 }
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
72
182
998bf7c643b4 merged sstrcat function
Mike Becker <universe@uap-core.de>
parents: 177
diff changeset
73 void *ucx_stack_calloc(UcxStack *stack, size_t nelem, size_t elsize) {
998bf7c643b4 merged sstrcat function
Mike Becker <universe@uap-core.de>
parents: 177
diff changeset
74 void *mem = ucx_stack_malloc(stack, nelem*elsize);
998bf7c643b4 merged sstrcat function
Mike Becker <universe@uap-core.de>
parents: 177
diff changeset
75 memset(mem, 0, nelem*elsize);
998bf7c643b4 merged sstrcat function
Mike Becker <universe@uap-core.de>
parents: 177
diff changeset
76 return mem;
13
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
77 }
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
78
182
998bf7c643b4 merged sstrcat function
Mike Becker <universe@uap-core.de>
parents: 177
diff changeset
79 void *ucx_stack_realloc(UcxStack *stack, void *ptr, size_t n) {
185
a48428642b4e added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents: 182
diff changeset
80 if (ptr == stack->top) {
a48428642b4e added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents: 182
diff changeset
81 if (stack->size - (stack->top - stack->space) < ucx_stack_align(n)) {
a48428642b4e added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents: 182
diff changeset
82 return NULL;
a48428642b4e added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents: 182
diff changeset
83 } else {
a48428642b4e added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents: 182
diff changeset
84 ((struct ucx_stack_metadata*)stack->top - 1)->size = n;
a48428642b4e added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents: 182
diff changeset
85 return ptr;
a48428642b4e added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents: 182
diff changeset
86 }
182
998bf7c643b4 merged sstrcat function
Mike Becker <universe@uap-core.de>
parents: 177
diff changeset
87 } else {
185
a48428642b4e added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents: 182
diff changeset
88 if (ucx_stack_align(((struct ucx_stack_metadata*)ptr - 1)->size) <
a48428642b4e added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents: 182
diff changeset
89 ucx_stack_align(n)) {
182
998bf7c643b4 merged sstrcat function
Mike Becker <universe@uap-core.de>
parents: 177
diff changeset
90 void *nptr = ucx_stack_malloc(stack, n);
998bf7c643b4 merged sstrcat function
Mike Becker <universe@uap-core.de>
parents: 177
diff changeset
91 if (nptr) {
185
a48428642b4e added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents: 182
diff changeset
92 memcpy(nptr, ptr, n);
182
998bf7c643b4 merged sstrcat function
Mike Becker <universe@uap-core.de>
parents: 177
diff changeset
93 ucx_stack_free(stack, ptr);
185
a48428642b4e added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents: 182
diff changeset
94
182
998bf7c643b4 merged sstrcat function
Mike Becker <universe@uap-core.de>
parents: 177
diff changeset
95 return nptr;
998bf7c643b4 merged sstrcat function
Mike Becker <universe@uap-core.de>
parents: 177
diff changeset
96 } else {
998bf7c643b4 merged sstrcat function
Mike Becker <universe@uap-core.de>
parents: 177
diff changeset
97 return NULL;
14
b78e174b6814 Fixed realloc
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 13
diff changeset
98 }
182
998bf7c643b4 merged sstrcat function
Mike Becker <universe@uap-core.de>
parents: 177
diff changeset
99 } else {
185
a48428642b4e added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents: 182
diff changeset
100 ((struct ucx_stack_metadata*)ptr - 1)->size = n;
182
998bf7c643b4 merged sstrcat function
Mike Becker <universe@uap-core.de>
parents: 177
diff changeset
101 return ptr;
13
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
102 }
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
103 }
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
104 }
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
105
182
998bf7c643b4 merged sstrcat function
Mike Becker <universe@uap-core.de>
parents: 177
diff changeset
106 void ucx_stack_free(UcxStack *stack, void *ptr) {
185
a48428642b4e added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents: 182
diff changeset
107 if (ptr == stack->top) {
a48428642b4e added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents: 182
diff changeset
108 stack->top = ((struct ucx_stack_metadata*) stack->top - 1)->prev;
182
998bf7c643b4 merged sstrcat function
Mike Becker <universe@uap-core.de>
parents: 177
diff changeset
109 } else {
185
a48428642b4e added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents: 182
diff changeset
110 struct ucx_stack_metadata *next = (struct ucx_stack_metadata*)(
a48428642b4e added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents: 182
diff changeset
111 (char*)ptr +
a48428642b4e added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents: 182
diff changeset
112 ucx_stack_align(((struct ucx_stack_metadata*) ptr - 1)->size)
a48428642b4e added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents: 182
diff changeset
113 );
a48428642b4e added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents: 182
diff changeset
114 next->prev = ((struct ucx_stack_metadata*) ptr - 1)->prev;
113
8693d7874773 added mempool allocator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
115 }
8693d7874773 added mempool allocator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
116 }
8693d7874773 added mempool allocator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
117
185
a48428642b4e added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents: 182
diff changeset
118 void ucx_stack_popn(UcxStack *stack, void *dest, size_t n) {
a48428642b4e added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents: 182
diff changeset
119 if (ucx_stack_empty(stack)) {
a48428642b4e added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents: 182
diff changeset
120 return;
a48428642b4e added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents: 182
diff changeset
121 }
a48428642b4e added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents: 182
diff changeset
122
301
0f83916c1639 documentation for the UcxStack
Mike Becker <universe@uap-core.de>
parents: 259
diff changeset
123 if (dest) {
0f83916c1639 documentation for the UcxStack
Mike Becker <universe@uap-core.de>
parents: 259
diff changeset
124 size_t len = ucx_stack_topsize(stack);
0f83916c1639 documentation for the UcxStack
Mike Becker <universe@uap-core.de>
parents: 259
diff changeset
125 if (len > n) {
0f83916c1639 documentation for the UcxStack
Mike Becker <universe@uap-core.de>
parents: 259
diff changeset
126 len = n;
0f83916c1639 documentation for the UcxStack
Mike Becker <universe@uap-core.de>
parents: 259
diff changeset
127 }
0f83916c1639 documentation for the UcxStack
Mike Becker <universe@uap-core.de>
parents: 259
diff changeset
128
0f83916c1639 documentation for the UcxStack
Mike Becker <universe@uap-core.de>
parents: 259
diff changeset
129 memcpy(dest, stack->top, len);
185
a48428642b4e added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents: 182
diff changeset
130 }
a48428642b4e added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents: 182
diff changeset
131
a48428642b4e added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents: 182
diff changeset
132 ucx_stack_free(stack, stack->top);
13
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
133 }
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
134
185
a48428642b4e added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents: 182
diff changeset
135 size_t ucx_stack_avail(UcxStack *stack) {
a48428642b4e added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents: 182
diff changeset
136 size_t avail = ((stack->top ? (stack->size
a48428642b4e added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents: 182
diff changeset
137 - (stack->top - stack->space)
a48428642b4e added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents: 182
diff changeset
138 - ucx_stack_align(ucx_stack_topsize(stack)))
a48428642b4e added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents: 182
diff changeset
139 : stack->size));
a48428642b4e added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents: 182
diff changeset
140
a48428642b4e added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents: 182
diff changeset
141 if (avail > sizeof(struct ucx_stack_metadata)) {
a48428642b4e added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents: 182
diff changeset
142 return avail - sizeof(struct ucx_stack_metadata);
a48428642b4e added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents: 182
diff changeset
143 } else {
a48428642b4e added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents: 182
diff changeset
144 return 0;
a48428642b4e added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents: 182
diff changeset
145 }
13
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
146 }
301
0f83916c1639 documentation for the UcxStack
Mike Becker <universe@uap-core.de>
parents: 259
diff changeset
147
0f83916c1639 documentation for the UcxStack
Mike Becker <universe@uap-core.de>
parents: 259
diff changeset
148 void *ucx_stack_push(UcxStack *stack, size_t n, const void *data) {
0f83916c1639 documentation for the UcxStack
Mike Becker <universe@uap-core.de>
parents: 259
diff changeset
149 void *space = ucx_stack_malloc(stack, n);
0f83916c1639 documentation for the UcxStack
Mike Becker <universe@uap-core.de>
parents: 259
diff changeset
150 if (space) {
0f83916c1639 documentation for the UcxStack
Mike Becker <universe@uap-core.de>
parents: 259
diff changeset
151 memcpy(space, data, n);
0f83916c1639 documentation for the UcxStack
Mike Becker <universe@uap-core.de>
parents: 259
diff changeset
152 }
0f83916c1639 documentation for the UcxStack
Mike Becker <universe@uap-core.de>
parents: 259
diff changeset
153 return space;
0f83916c1639 documentation for the UcxStack
Mike Becker <universe@uap-core.de>
parents: 259
diff changeset
154 }
0f83916c1639 documentation for the UcxStack
Mike Becker <universe@uap-core.de>
parents: 259
diff changeset
155
0f83916c1639 documentation for the UcxStack
Mike Becker <universe@uap-core.de>
parents: 259
diff changeset
156 void *ucx_stack_pusharr(UcxStack *stack,
0f83916c1639 documentation for the UcxStack
Mike Becker <universe@uap-core.de>
parents: 259
diff changeset
157 size_t nelem, size_t elsize, const void *data) {
0f83916c1639 documentation for the UcxStack
Mike Becker <universe@uap-core.de>
parents: 259
diff changeset
158
0f83916c1639 documentation for the UcxStack
Mike Becker <universe@uap-core.de>
parents: 259
diff changeset
159 // skip the memset by using malloc
0f83916c1639 documentation for the UcxStack
Mike Becker <universe@uap-core.de>
parents: 259
diff changeset
160 void *space = ucx_stack_malloc(stack, nelem*elsize);
0f83916c1639 documentation for the UcxStack
Mike Becker <universe@uap-core.de>
parents: 259
diff changeset
161 if (space) {
0f83916c1639 documentation for the UcxStack
Mike Becker <universe@uap-core.de>
parents: 259
diff changeset
162 memcpy(space, data, nelem*elsize);
0f83916c1639 documentation for the UcxStack
Mike Becker <universe@uap-core.de>
parents: 259
diff changeset
163 }
0f83916c1639 documentation for the UcxStack
Mike Becker <universe@uap-core.de>
parents: 259
diff changeset
164 return space;
0f83916c1639 documentation for the UcxStack
Mike Becker <universe@uap-core.de>
parents: 259
diff changeset
165 }

mercurial