2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4 * Copyright 2017 Mike Becker, Olaf Wintermann All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
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
26 * POSSIBILITY OF SUCH DAMAGE.
32 * Default stack memory allocation implementation.
35 * @author Olaf Wintermann
42 #include "allocator.h"
50 * UCX stack structure.
53 /** UcxAllocator based on this stack */
54 UcxAllocator allocator;
59 /** Pointer to the bottom of the stack */
62 /** Pointer to the top of the stack */
67 * Metadata for each UCX stack element.
69 struct ucx_stack_metadata {
71 * Location of the previous element (<code>NULL</code> if this is the first)
75 /** Size of this element */
80 * Initializes UcxStack structure with memory.
82 * @param stack a pointer to an uninitialized stack structure
83 * @param space the memory area that shall be managed
84 * @param size size of the memory area
85 * @return a new UcxStack structure
87 void ucx_stack_init(UcxStack *stack, char* space, size_t size);
90 * Allocates stack memory.
92 * @param stack a pointer to the stack
93 * @param n amount of memory to allocate
94 * @return a pointer to the allocated memory or <code>NULL</code> on stack
96 * @see ucx_allocator_malloc()
98 void *ucx_stack_malloc(UcxStack *stack, size_t n);
101 * Allocates memory with #ucx_stack_malloc() and copies the specified data if
102 * the allocation was successful.
104 * @param stack a pointer to the stack
105 * @param n amount of memory to allocate
106 * @param data a pointer to the data to copy
107 * @return a pointer to the allocated memory
108 * @see ucx_stack_malloc
110 void *ucx_stack_push(UcxStack *stack, size_t n, const void *data);
113 * Allocates an array of stack memory
115 * The content of the allocated memory is set to zero.
117 * @param stack a pointer to the stack
118 * @param nelem amount of elements to allocate
119 * @param elsize amount of memory per element
120 * @return a pointer to the allocated memory
121 * @see ucx_allocator_calloc()
123 void *ucx_stack_calloc(UcxStack *stack, size_t nelem, size_t elsize);
126 * Allocates memory with #ucx_stack_calloc() and copies the specified data if
127 * the allocation was successful.
129 * @param stack a pointer to the stack
130 * @param nelem amount of elements to allocate
131 * @param elsize amount of memory per element
132 * @param data a pointer to the data
133 * @return a pointer to the allocated memory
134 * @see ucx_stack_calloc
136 void *ucx_stack_pusharr(UcxStack *stack,
137 size_t nelem, size_t elsize, const void *data);
140 * Reallocates memory on the stack.
142 * Shrinking memory is always safe. Extending memory can be very expensive.
144 * @param stack the stack
145 * @param ptr a pointer to the memory that shall be reallocated
146 * @param n the new size of the memory
147 * @return a pointer to the new location of the memory
148 * @see ucx_allocator_realloc()
150 void *ucx_stack_realloc(UcxStack *stack, void *ptr, size_t n);
153 * Frees memory on the stack.
155 * Freeing stack memory behaves in a special way.
157 * If the element, that should be freed, is the top most element of the stack,
158 * it is removed from the stack. Otherwise it is marked as freed. Marked
159 * elements are removed, when they become the top most elements of the stack.
161 * @param stack a pointer to the stack
162 * @param ptr a pointer to the memory that shall be freed
164 void ucx_stack_free(UcxStack *stack, void *ptr);
168 * Returns the size of the top most element.
169 * @param stack a pointer to the stack
170 * @return the size of the top most element
172 #define ucx_stack_topsize(stack) ((stack)->top ? ((struct ucx_stack_metadata*)\
173 (stack)->top - 1)->size : 0)
176 * Removes the top most element from the stack and copies the content to <code>
177 * dest</code>, if specified.
179 * Use #ucx_stack_topsize()# to get the amount of memory that must be available
180 * at the location of <code>dest</code>.
182 * @param stack a pointer to the stack
183 * @param dest the location where the contents shall be written to, or <code>
184 * NULL</code>, if the element shall only be removed.
185 * @see ucx_stack_free
186 * @see ucx_stack_popn
188 #define ucx_stack_pop(stack, dest) ucx_stack_popn(stack, dest, (size_t)-1)
191 * Removes the top most element from the stack and copies the content to <code>
194 * This function copies at most <code>n</code> bytes to the destination, but
195 * the element is always freed as a whole.
196 * If the element was larger than <code>n</code>, the remaining data is lost.
198 * @param stack a pointer to the stack
199 * @param dest the location where the contents shall be written to
200 * @param n copies at most n bytes to <code>dest</code>
203 void ucx_stack_popn(UcxStack *stack, void *dest, size_t n);
206 * Returns the remaining available memory on the specified stack.
208 * @param stack a pointer to the stack
209 * @return the remaining available memory
211 size_t ucx_stack_avail(UcxStack *stack);
214 * Checks, if the stack is empty.
216 * @param stack a pointer to the stack
217 * @return nonzero, if the stack is empty, zero otherwise
219 #define ucx_stack_empty(stack) (!(stack)->top)
222 * Computes a recommended size for the stack memory area. Note, that
223 * reallocations have not been taken into account, so you might need to reserve
224 * twice as much memory to allow many reallocations.
226 * @param size the approximate payload
227 * @param elems the approximate count of element allocations
228 * @return a recommended size for the stack space based on the information
231 #define ucx_stack_dim(size, elems) (size+sizeof(struct ucx_stack_metadata) * \
239 #endif /* UCX_STACK_H */