src/ucx/stack.h

Sun, 21 Jan 2018 14:10:59 +0100

author
Mike Becker <universe@uap-core.de>
date
Sun, 21 Jan 2018 14:10:59 +0100
changeset 273
9c1591b3c4a4
parent 259
2f5dea574a75
child 301
0f83916c1639
permissions
-rw-r--r--

fixes return value for multiplication with zero in ucx_szmul

103
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 69
diff changeset
1 /*
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 69
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 69
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: 69
diff changeset
5 *
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 69
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: 69
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: 69
diff changeset
8 *
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 69
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: 69
diff changeset
10 * notice, this list of conditions and the following disclaimer.
13
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
11 *
103
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 69
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: 69
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: 69
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: 69
diff changeset
15 *
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 69
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: 69
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: 69
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: 69
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: 69
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: 69
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: 69
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: 69
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: 69
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: 69
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: 69
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
135
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
29 /**
182
998bf7c643b4 merged sstrcat function
Mike Becker <universe@uap-core.de>
parents: 177
diff changeset
30 * @file stack.h
135
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
31 *
182
998bf7c643b4 merged sstrcat function
Mike Becker <universe@uap-core.de>
parents: 177
diff changeset
32 * Default stack memory allocation implementation.
135
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
33 *
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
34 * @author Mike Becker
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
35 * @author Olaf Wintermann
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
36 */
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
37
182
998bf7c643b4 merged sstrcat function
Mike Becker <universe@uap-core.de>
parents: 177
diff changeset
38 #ifndef UCX_STACK_H
998bf7c643b4 merged sstrcat function
Mike Becker <universe@uap-core.de>
parents: 177
diff changeset
39 #define UCX_STACK_H
13
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
40
259
2f5dea574a75 modules documentation
Mike Becker <universe@uap-core.de>
parents: 251
diff changeset
41 #include "ucx.h"
2f5dea574a75 modules documentation
Mike Becker <universe@uap-core.de>
parents: 251
diff changeset
42 #include "allocator.h"
38
35f67a8ef875 added missing stddef includes
Mike Becker <universe@uap-core.de>
parents: 16
diff changeset
43
13
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
44 #ifdef __cplusplus
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
45 extern "C" {
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
46 #endif
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
47
135
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
48
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
49 /**
182
998bf7c643b4 merged sstrcat function
Mike Becker <universe@uap-core.de>
parents: 177
diff changeset
50 * UCX stack structure.
135
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
51 */
182
998bf7c643b4 merged sstrcat function
Mike Becker <universe@uap-core.de>
parents: 177
diff changeset
52 typedef struct {
998bf7c643b4 merged sstrcat function
Mike Becker <universe@uap-core.de>
parents: 177
diff changeset
53 /** UcxAllocator based on this stack */
998bf7c643b4 merged sstrcat function
Mike Becker <universe@uap-core.de>
parents: 177
diff changeset
54 UcxAllocator allocator;
998bf7c643b4 merged sstrcat function
Mike Becker <universe@uap-core.de>
parents: 177
diff changeset
55
998bf7c643b4 merged sstrcat function
Mike Becker <universe@uap-core.de>
parents: 177
diff changeset
56 /** Stack size. */
998bf7c643b4 merged sstrcat function
Mike Becker <universe@uap-core.de>
parents: 177
diff changeset
57 size_t size;
998bf7c643b4 merged sstrcat function
Mike Becker <universe@uap-core.de>
parents: 177
diff changeset
58
998bf7c643b4 merged sstrcat function
Mike Becker <universe@uap-core.de>
parents: 177
diff changeset
59 /** Pointer to the bottom of the stack */
998bf7c643b4 merged sstrcat function
Mike Becker <universe@uap-core.de>
parents: 177
diff changeset
60 char *space;
998bf7c643b4 merged sstrcat function
Mike Becker <universe@uap-core.de>
parents: 177
diff changeset
61
998bf7c643b4 merged sstrcat function
Mike Becker <universe@uap-core.de>
parents: 177
diff changeset
62 /** Pointer to the top of the stack */
998bf7c643b4 merged sstrcat function
Mike Becker <universe@uap-core.de>
parents: 177
diff changeset
63 char *top;
998bf7c643b4 merged sstrcat function
Mike Becker <universe@uap-core.de>
parents: 177
diff changeset
64 } UcxStack;
135
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
65
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
66 /**
185
a48428642b4e added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents: 182
diff changeset
67 * Metadata for each UCX stack element.
a48428642b4e added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents: 182
diff changeset
68 */
a48428642b4e added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents: 182
diff changeset
69 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
70 /**
a48428642b4e added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents: 182
diff changeset
71 * Location of the previous element (<code>NULL</code> if this is the first)
a48428642b4e added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents: 182
diff changeset
72 */
a48428642b4e added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents: 182
diff changeset
73 char *prev;
a48428642b4e added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents: 182
diff changeset
74
a48428642b4e added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents: 182
diff changeset
75 /** Size of this element */
a48428642b4e added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents: 182
diff changeset
76 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
77 };
a48428642b4e added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents: 182
diff changeset
78
a48428642b4e added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents: 182
diff changeset
79 /**
a48428642b4e added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents: 182
diff changeset
80 * Initializes UcxStack structure with memory.
135
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
81 *
185
a48428642b4e added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents: 182
diff changeset
82 * @param stack a pointer to an uninitialized stack structure
182
998bf7c643b4 merged sstrcat function
Mike Becker <universe@uap-core.de>
parents: 177
diff changeset
83 * @param space the memory area that shall be managed
998bf7c643b4 merged sstrcat function
Mike Becker <universe@uap-core.de>
parents: 177
diff changeset
84 * @param size size of the memory area
998bf7c643b4 merged sstrcat function
Mike Becker <universe@uap-core.de>
parents: 177
diff changeset
85 * @return a new UcxStack structure
135
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
86 */
185
a48428642b4e added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents: 182
diff changeset
87 void ucx_stack_init(UcxStack *stack, char* space, size_t size);
13
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
88
135
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
89 /**
182
998bf7c643b4 merged sstrcat function
Mike Becker <universe@uap-core.de>
parents: 177
diff changeset
90 * Allocates stack memory.
141
c466e2a6cbd0 added mempool clamp + some minor fixes
Mike Becker <universe@uap-core.de>
parents: 135
diff changeset
91 *
182
998bf7c643b4 merged sstrcat function
Mike Becker <universe@uap-core.de>
parents: 177
diff changeset
92 * @param stack a pointer to the stack
135
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
93 * @param n amount of memory to allocate
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
94 * @return a pointer to the allocated memory
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
95 * @see ucx_allocator_malloc()
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
96 */
182
998bf7c643b4 merged sstrcat function
Mike Becker <universe@uap-core.de>
parents: 177
diff changeset
97 void *ucx_stack_malloc(UcxStack *stack, size_t n);
998bf7c643b4 merged sstrcat function
Mike Becker <universe@uap-core.de>
parents: 177
diff changeset
98
135
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
99 /**
182
998bf7c643b4 merged sstrcat function
Mike Becker <universe@uap-core.de>
parents: 177
diff changeset
100 * Alias for #ucx_stack_malloc().
998bf7c643b4 merged sstrcat function
Mike Becker <universe@uap-core.de>
parents: 177
diff changeset
101 * @param stack a pointer to the stack
998bf7c643b4 merged sstrcat function
Mike Becker <universe@uap-core.de>
parents: 177
diff changeset
102 * @param n amount of memory to allocate
998bf7c643b4 merged sstrcat function
Mike Becker <universe@uap-core.de>
parents: 177
diff changeset
103 * @return a pointer to the allocated memory
998bf7c643b4 merged sstrcat function
Mike Becker <universe@uap-core.de>
parents: 177
diff changeset
104 * @see ucx_stack_malloc
998bf7c643b4 merged sstrcat function
Mike Becker <universe@uap-core.de>
parents: 177
diff changeset
105 */
188
63f87e2884c1 fixed doc
Mike Becker <universe@uap-core.de>
parents: 185
diff changeset
106 #define ucx_stack_push(stack, n) ucx_stack_malloc(stack, n)
182
998bf7c643b4 merged sstrcat function
Mike Becker <universe@uap-core.de>
parents: 177
diff changeset
107
998bf7c643b4 merged sstrcat function
Mike Becker <universe@uap-core.de>
parents: 177
diff changeset
108 /**
998bf7c643b4 merged sstrcat function
Mike Becker <universe@uap-core.de>
parents: 177
diff changeset
109 * Allocates an array of stack memory
135
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
110 *
182
998bf7c643b4 merged sstrcat function
Mike Becker <universe@uap-core.de>
parents: 177
diff changeset
111 * The content of the allocated memory is set to zero.
135
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
112 *
182
998bf7c643b4 merged sstrcat function
Mike Becker <universe@uap-core.de>
parents: 177
diff changeset
113 * @param stack a pointer to the stack
135
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
114 * @param nelem amount of elements to allocate
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
115 * @param elsize amount of memory per element
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
116 * @return a pointer to the allocated memory
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
117 * @see ucx_allocator_calloc()
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
118 */
182
998bf7c643b4 merged sstrcat function
Mike Becker <universe@uap-core.de>
parents: 177
diff changeset
119 void *ucx_stack_calloc(UcxStack *stack, size_t nelem, size_t elsize);
146
aa376dba1ba8 fixed documentation for netbeans parser + added sstrprefix() and sstrsuffix()
Mike Becker <universe@uap-core.de>
parents: 141
diff changeset
120
135
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
121 /**
182
998bf7c643b4 merged sstrcat function
Mike Becker <universe@uap-core.de>
parents: 177
diff changeset
122 * Alias for #ucx_stack_calloc().
135
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
123 *
182
998bf7c643b4 merged sstrcat function
Mike Becker <universe@uap-core.de>
parents: 177
diff changeset
124 * @param stack a pointer to the stack
188
63f87e2884c1 fixed doc
Mike Becker <universe@uap-core.de>
parents: 185
diff changeset
125 * @param n amount of elements to allocate
182
998bf7c643b4 merged sstrcat function
Mike Becker <universe@uap-core.de>
parents: 177
diff changeset
126 * @param elsize amount of memory per element
998bf7c643b4 merged sstrcat function
Mike Becker <universe@uap-core.de>
parents: 177
diff changeset
127 * @return a pointer to the allocated memory
998bf7c643b4 merged sstrcat function
Mike Becker <universe@uap-core.de>
parents: 177
diff changeset
128 * @see ucx_stack_calloc
998bf7c643b4 merged sstrcat function
Mike Becker <universe@uap-core.de>
parents: 177
diff changeset
129 */
188
63f87e2884c1 fixed doc
Mike Becker <universe@uap-core.de>
parents: 185
diff changeset
130 #define ucx_stack_pusharr(stack,n,elsize) ucx_stack_calloc(stack,n,elssize)
182
998bf7c643b4 merged sstrcat function
Mike Becker <universe@uap-core.de>
parents: 177
diff changeset
131
998bf7c643b4 merged sstrcat function
Mike Becker <universe@uap-core.de>
parents: 177
diff changeset
132 /**
998bf7c643b4 merged sstrcat function
Mike Becker <universe@uap-core.de>
parents: 177
diff changeset
133 * Reallocates memory on the stack.
141
c466e2a6cbd0 added mempool clamp + some minor fixes
Mike Becker <universe@uap-core.de>
parents: 135
diff changeset
134 *
182
998bf7c643b4 merged sstrcat function
Mike Becker <universe@uap-core.de>
parents: 177
diff changeset
135 * Shrinking memory is always safe. Extending memory can be very expensive.
998bf7c643b4 merged sstrcat function
Mike Becker <universe@uap-core.de>
parents: 177
diff changeset
136 *
998bf7c643b4 merged sstrcat function
Mike Becker <universe@uap-core.de>
parents: 177
diff changeset
137 * @param stack the stack
135
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
138 * @param ptr a pointer to the memory that shall be reallocated
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
139 * @param n the new size of the memory
182
998bf7c643b4 merged sstrcat function
Mike Becker <universe@uap-core.de>
parents: 177
diff changeset
140 * @return a pointer to the new location of the memory
135
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
141 * @see ucx_allocator_realloc()
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
142 */
182
998bf7c643b4 merged sstrcat function
Mike Becker <universe@uap-core.de>
parents: 177
diff changeset
143 void *ucx_stack_realloc(UcxStack *stack, void *ptr, size_t n);
146
aa376dba1ba8 fixed documentation for netbeans parser + added sstrprefix() and sstrsuffix()
Mike Becker <universe@uap-core.de>
parents: 141
diff changeset
144
135
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
145 /**
182
998bf7c643b4 merged sstrcat function
Mike Becker <universe@uap-core.de>
parents: 177
diff changeset
146 * Frees memory on the stack.
135
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
147 *
182
998bf7c643b4 merged sstrcat function
Mike Becker <universe@uap-core.de>
parents: 177
diff changeset
148 * Freeing stack memory behaves in a special way.
135
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
149 *
182
998bf7c643b4 merged sstrcat function
Mike Becker <universe@uap-core.de>
parents: 177
diff changeset
150 * If the element, that should be freed, is the top most element of the stack,
998bf7c643b4 merged sstrcat function
Mike Becker <universe@uap-core.de>
parents: 177
diff changeset
151 * it is removed from the stack. Otherwise it is marked as freed. Marked
998bf7c643b4 merged sstrcat function
Mike Becker <universe@uap-core.de>
parents: 177
diff changeset
152 * elements are removed, when they become the top most elements of the stack.
135
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
153 *
182
998bf7c643b4 merged sstrcat function
Mike Becker <universe@uap-core.de>
parents: 177
diff changeset
154 * @param stack a pointer to the stack
135
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
155 * @param ptr a pointer to the memory that shall be freed
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
156 */
182
998bf7c643b4 merged sstrcat function
Mike Becker <universe@uap-core.de>
parents: 177
diff changeset
157 void ucx_stack_free(UcxStack *stack, void *ptr);
998bf7c643b4 merged sstrcat function
Mike Becker <universe@uap-core.de>
parents: 177
diff changeset
158
13
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
159
135
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
160 /**
182
998bf7c643b4 merged sstrcat function
Mike Becker <universe@uap-core.de>
parents: 177
diff changeset
161 * Returns the size of the top most element.
998bf7c643b4 merged sstrcat function
Mike Becker <universe@uap-core.de>
parents: 177
diff changeset
162 * @param stack a pointer to the stack
998bf7c643b4 merged sstrcat function
Mike Becker <universe@uap-core.de>
parents: 177
diff changeset
163 * @return the size of the top most element
998bf7c643b4 merged sstrcat function
Mike Becker <universe@uap-core.de>
parents: 177
diff changeset
164 */
185
a48428642b4e added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents: 182
diff changeset
165 #define ucx_stack_topsize(stack) ((stack)->top ? ((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
166 (stack)->top - 1)->size : 0)
182
998bf7c643b4 merged sstrcat function
Mike Becker <universe@uap-core.de>
parents: 177
diff changeset
167
998bf7c643b4 merged sstrcat function
Mike Becker <universe@uap-core.de>
parents: 177
diff changeset
168 /**
998bf7c643b4 merged sstrcat function
Mike Becker <universe@uap-core.de>
parents: 177
diff changeset
169 * Removes the top most element from the stack and copies the content to <code>
998bf7c643b4 merged sstrcat function
Mike Becker <universe@uap-core.de>
parents: 177
diff changeset
170 * dest</code>, if specified.
135
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
171 *
182
998bf7c643b4 merged sstrcat function
Mike Becker <universe@uap-core.de>
parents: 177
diff changeset
172 * Use #ucx_stack_topsize()# to get the amount of memory that must be available
998bf7c643b4 merged sstrcat function
Mike Becker <universe@uap-core.de>
parents: 177
diff changeset
173 * at the location of <code>dest</code>.
135
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
174 *
182
998bf7c643b4 merged sstrcat function
Mike Becker <universe@uap-core.de>
parents: 177
diff changeset
175 * @param stack a pointer to the stack
998bf7c643b4 merged sstrcat function
Mike Becker <universe@uap-core.de>
parents: 177
diff changeset
176 * @param dest the location where the contents shall be written to, or <code>
998bf7c643b4 merged sstrcat function
Mike Becker <universe@uap-core.de>
parents: 177
diff changeset
177 * NULL</code>, if the element shall only be removed.
998bf7c643b4 merged sstrcat function
Mike Becker <universe@uap-core.de>
parents: 177
diff changeset
178 * @see ucx_stack_free
998bf7c643b4 merged sstrcat function
Mike Becker <universe@uap-core.de>
parents: 177
diff changeset
179 * @see ucx_stack_popn
135
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
180 */
217
e056e4e0b08e removed dependency to stdint.h in stack header
Mike Becker <universe@uap-core.de>
parents: 192
diff changeset
181 #define ucx_stack_pop(stack, dest) ucx_stack_popn(stack, dest, (size_t)-1)
13
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
182
135
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
183 /**
182
998bf7c643b4 merged sstrcat function
Mike Becker <universe@uap-core.de>
parents: 177
diff changeset
184 * Removes the top most element from the stack and copies the content to <code>
998bf7c643b4 merged sstrcat function
Mike Becker <universe@uap-core.de>
parents: 177
diff changeset
185 * dest</code>.
135
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
186 *
182
998bf7c643b4 merged sstrcat function
Mike Becker <universe@uap-core.de>
parents: 177
diff changeset
187 * In contrast to #ucx_stack_pop() the <code>dest</code> pointer <code>MUST
998bf7c643b4 merged sstrcat function
Mike Becker <universe@uap-core.de>
parents: 177
diff changeset
188 * NOT</code> be <code>NULL</code>.
135
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
189 *
182
998bf7c643b4 merged sstrcat function
Mike Becker <universe@uap-core.de>
parents: 177
diff changeset
190 * @param stack a pointer to the stack
998bf7c643b4 merged sstrcat function
Mike Becker <universe@uap-core.de>
parents: 177
diff changeset
191 * @param dest the location where the contents shall be written to
998bf7c643b4 merged sstrcat function
Mike Becker <universe@uap-core.de>
parents: 177
diff changeset
192 * @param n copies at most n elements to <code>dest</code>
998bf7c643b4 merged sstrcat function
Mike Becker <universe@uap-core.de>
parents: 177
diff changeset
193 * @see ucx_stack_pop
135
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
194 */
182
998bf7c643b4 merged sstrcat function
Mike Becker <universe@uap-core.de>
parents: 177
diff changeset
195 void ucx_stack_popn(UcxStack *stack, void *dest, size_t n);
135
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
196
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
197 /**
182
998bf7c643b4 merged sstrcat function
Mike Becker <universe@uap-core.de>
parents: 177
diff changeset
198 * Returns the remaining available memory on the specified stack.
135
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
199 *
182
998bf7c643b4 merged sstrcat function
Mike Becker <universe@uap-core.de>
parents: 177
diff changeset
200 * @param stack a pointer to the stack
998bf7c643b4 merged sstrcat function
Mike Becker <universe@uap-core.de>
parents: 177
diff changeset
201 * @return the remaining available memory
135
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
202 */
185
a48428642b4e added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents: 182
diff changeset
203 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
204
a48428642b4e added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents: 182
diff changeset
205 /**
a48428642b4e added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents: 182
diff changeset
206 * Checks, if the stack is empty.
a48428642b4e added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents: 182
diff changeset
207 *
a48428642b4e added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents: 182
diff changeset
208 * @param stack a pointer to the stack
a48428642b4e added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents: 182
diff changeset
209 * @return nonzero, if the stack is empty, zero otherwise
a48428642b4e added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents: 182
diff changeset
210 */
a48428642b4e added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents: 182
diff changeset
211 #define ucx_stack_empty(stack) (!(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
212
a48428642b4e added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents: 182
diff changeset
213 /**
a48428642b4e added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents: 182
diff changeset
214 * Computes a recommended size for the stack memory area. Note, that
a48428642b4e added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents: 182
diff changeset
215 * reallocations have not been taken into account, so you might need to reserve
a48428642b4e added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents: 182
diff changeset
216 * twice as much memory to allow many reallocations.
a48428642b4e added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents: 182
diff changeset
217 *
a48428642b4e added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents: 182
diff changeset
218 * @param size the approximate payload
a48428642b4e added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents: 182
diff changeset
219 * @param elems the approximate count of element allocations
a48428642b4e added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents: 182
diff changeset
220 * @return a recommended size for the stack space based on the information
a48428642b4e added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents: 182
diff changeset
221 * provided
a48428642b4e added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents: 182
diff changeset
222 */
a48428642b4e added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents: 182
diff changeset
223 #define ucx_stack_dim(size, elems) (size+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
224 (elems + 1))
182
998bf7c643b4 merged sstrcat function
Mike Becker <universe@uap-core.de>
parents: 177
diff changeset
225
13
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
226
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
227 #ifdef __cplusplus
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
228 }
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
229 #endif
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
230
182
998bf7c643b4 merged sstrcat function
Mike Becker <universe@uap-core.de>
parents: 177
diff changeset
231 #endif /* UCX_STACK_H */
13
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
232

mercurial