src/ucx/stack.h

Tue, 27 Dec 2016 14:02:28 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Tue, 27 Dec 2016 14:02:28 +0100
changeset 132
e9afb5387007
parent 99
b9a6af0ae41a
child 135
471e28cca288
permissions
-rw-r--r--

fixes broken error response

99
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1 /*
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
3 *
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
4 * Copyright 2015 Olaf Wintermann. All rights reserved.
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
5 *
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
6 * Redistribution and use in source and binary forms, with or without
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
7 * modification, are permitted provided that the following conditions are met:
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
8 *
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
9 * 1. Redistributions of source code must retain the above copyright
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
10 * notice, this list of conditions and the following disclaimer.
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
11 *
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
14 * documentation and/or other materials provided with the distribution.
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
15 *
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
26 * POSSIBILITY OF SUCH DAMAGE.
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
27 */
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
28
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
29 /**
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
30 * @file stack.h
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
31 *
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
32 * Default stack memory allocation implementation.
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
33 *
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
34 * @author Mike Becker
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
35 * @author Olaf Wintermann
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
36 */
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
37
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
38 #ifndef UCX_STACK_H
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
39 #define UCX_STACK_H
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
40
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
41 #include "ucx.h"
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
42 #include <stdint.h>
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
43 #include "allocator.h"
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
44
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
45 #ifdef __cplusplus
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
46 extern "C" {
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
47 #endif
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
48
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
49
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
50 /**
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
51 * UCX stack structure.
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
52 */
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
53 typedef struct {
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
54 /** UcxAllocator based on this stack */
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
55 UcxAllocator allocator;
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
56
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
57 /** Stack size. */
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
58 size_t size;
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
59
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
60 /** Pointer to the bottom of the stack */
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
61 char *space;
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
62
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
63 /** Pointer to the top of the stack */
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
64 char *top;
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
65 } UcxStack;
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
66
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
67 /**
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
68 * Metadata for each UCX stack element.
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
69 */
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
70 struct ucx_stack_metadata {
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
71 /**
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
72 * Location of the previous element (<code>NULL</code> if this is the first)
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
73 */
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
74 char *prev;
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
75
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
76 /** Size of this element */
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
77 size_t size;
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
78 };
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
79
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
80 /**
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
81 * Initializes UcxStack structure with memory.
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
82 *
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
83 * @param stack a pointer to an uninitialized stack structure
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
84 * @param space the memory area that shall be managed
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
85 * @param size size of the memory area
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
86 * @return a new UcxStack structure
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
87 */
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
88 void ucx_stack_init(UcxStack *stack, char* space, size_t size);
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
89
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
90 /**
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
91 * Allocates stack memory.
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
92 *
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
93 * @param stack a pointer to the stack
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
94 * @param n amount of memory to allocate
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
95 * @return a pointer to the allocated memory
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
96 * @see ucx_allocator_malloc()
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
97 */
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
98 void *ucx_stack_malloc(UcxStack *stack, size_t n);
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
99
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
100 /**
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
101 * Alias for #ucx_stack_malloc().
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
102 * @param stack a pointer to the stack
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
103 * @param n amount of memory to allocate
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
104 * @return a pointer to the allocated memory
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
105 * @see ucx_stack_malloc
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
106 */
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
107 #define ucx_stack_push(stack, n) ucx_stack_malloc(stack, n)
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
108
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
109 /**
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
110 * Allocates an array of stack memory
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
111 *
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
112 * The content of the allocated memory is set to zero.
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
113 *
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
114 * @param stack a pointer to the stack
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
115 * @param nelem amount of elements to allocate
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
116 * @param elsize amount of memory per element
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
117 * @return a pointer to the allocated memory
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
118 * @see ucx_allocator_calloc()
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
119 */
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
120 void *ucx_stack_calloc(UcxStack *stack, size_t nelem, size_t elsize);
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
121
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
122 /**
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
123 * Alias for #ucx_stack_calloc().
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
124 *
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
125 * @param stack a pointer to the stack
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
126 * @param n amount of elements to allocate
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
127 * @param elsize amount of memory per element
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
128 * @return a pointer to the allocated memory
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
129 * @see ucx_stack_calloc
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
130 */
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
131 #define ucx_stack_pusharr(stack,n,elsize) ucx_stack_calloc(stack,n,elssize)
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
132
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
133 /**
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
134 * Reallocates memory on the stack.
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
135 *
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
136 * Shrinking memory is always safe. Extending memory can be very expensive.
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
137 *
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
138 * @param stack the stack
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
139 * @param ptr a pointer to the memory that shall be reallocated
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
140 * @param n the new size of the memory
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
141 * @return a pointer to the new location of the memory
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
142 * @see ucx_allocator_realloc()
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
143 */
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
144 void *ucx_stack_realloc(UcxStack *stack, void *ptr, size_t n);
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
145
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
146 /**
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
147 * Frees memory on the stack.
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
148 *
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
149 * Freeing stack memory behaves in a special way.
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
150 *
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
151 * If the element, that should be freed, is the top most element of the stack,
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
152 * it is removed from the stack. Otherwise it is marked as freed. Marked
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
153 * elements are removed, when they become the top most elements of the stack.
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
154 *
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
155 * @param stack a pointer to the stack
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
156 * @param ptr a pointer to the memory that shall be freed
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
157 */
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
158 void ucx_stack_free(UcxStack *stack, void *ptr);
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
159
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
160
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
161 /**
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
162 * Returns the size of the top most element.
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
163 * @param stack a pointer to the stack
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
164 * @return the size of the top most element
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
165 */
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
166 #define ucx_stack_topsize(stack) ((stack)->top ? ((struct ucx_stack_metadata*)\
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
167 (stack)->top - 1)->size : 0)
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
168
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
169 /**
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
170 * Removes the top most element from the stack and copies the content to <code>
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
171 * dest</code>, if specified.
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
172 *
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
173 * Use #ucx_stack_topsize()# to get the amount of memory that must be available
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
174 * at the location of <code>dest</code>.
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
175 *
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
176 * @param stack a pointer to the stack
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
177 * @param dest the location where the contents shall be written to, or <code>
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
178 * NULL</code>, if the element shall only be removed.
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
179 * @see ucx_stack_free
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
180 * @see ucx_stack_popn
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
181 */
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
182 #define ucx_stack_pop(stack, dest) ucx_stack_popn(stack, dest, SIZE_MAX)
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
183
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
184 /**
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
185 * Removes the top most element from the stack and copies the content to <code>
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
186 * dest</code>.
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
187 *
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
188 * In contrast to #ucx_stack_pop() the <code>dest</code> pointer <code>MUST
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
189 * NOT</code> be <code>NULL</code>.
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
190 *
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
191 * @param stack a pointer to the stack
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
192 * @param dest the location where the contents shall be written to
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
193 * @param n copies at most n elements to <code>dest</code>
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
194 * @see ucx_stack_pop
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
195 */
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
196 void ucx_stack_popn(UcxStack *stack, void *dest, size_t n);
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
197
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
198 /**
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
199 * Returns the remaining available memory on the specified stack.
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
200 *
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
201 * @param stack a pointer to the stack
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
202 * @return the remaining available memory
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
203 */
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
204 size_t ucx_stack_avail(UcxStack *stack);
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
205
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
206 /**
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
207 * Checks, if the stack is empty.
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
208 *
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
209 * @param stack a pointer to the stack
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
210 * @return nonzero, if the stack is empty, zero otherwise
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
211 */
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
212 #define ucx_stack_empty(stack) (!(stack)->top)
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
213
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
214 /**
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
215 * Computes a recommended size for the stack memory area. Note, that
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
216 * reallocations have not been taken into account, so you might need to reserve
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
217 * twice as much memory to allow many reallocations.
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
218 *
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
219 * @param size the approximate payload
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
220 * @param elems the approximate count of element allocations
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
221 * @return a recommended size for the stack space based on the information
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
222 * provided
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
223 */
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
224 #define ucx_stack_dim(size, elems) (size+sizeof(struct ucx_stack_metadata) * \
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
225 (elems + 1))
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
226
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
227
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
228 #ifdef __cplusplus
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
229 }
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
230 #endif
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
231
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
232 #endif /* UCX_STACK_H */
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
233

mercurial