ucx
UAP Common Extensions
stack.h
Go to the documentation of this file.
1 /*
2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3  *
4  * Copyright 2017 Mike Becker, Olaf Wintermann All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
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.
15  *
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.
27  */
28 
38 #ifndef UCX_STACK_H
39 #define UCX_STACK_H
40 
41 #include "ucx.h"
42 #include "allocator.h"
43 
44 #ifdef __cplusplus
45 extern "C" {
46 #endif
47 
48 
52 typedef struct {
55 
57  size_t size;
58 
60  char *space;
61 
63  char *top;
64 } UcxStack;
65 
73  char *prev;
74 
76  size_t size;
77 };
78 
87 void ucx_stack_init(UcxStack *stack, char* space, size_t size);
88 
97 void *ucx_stack_malloc(UcxStack *stack, size_t n);
98 
106 #define ucx_stack_push(stack, n) ucx_stack_malloc(stack, n)
107 
119 void *ucx_stack_calloc(UcxStack *stack, size_t nelem, size_t elsize);
120 
130 #define ucx_stack_pusharr(stack,n,elsize) ucx_stack_calloc(stack,n,elssize)
131 
143 void *ucx_stack_realloc(UcxStack *stack, void *ptr, size_t n);
144 
157 void ucx_stack_free(UcxStack *stack, void *ptr);
158 
159 
165 #define ucx_stack_topsize(stack) ((stack)->top ? ((struct ucx_stack_metadata*)\
166  (stack)->top - 1)->size : 0)
167 
181 #define ucx_stack_pop(stack, dest) ucx_stack_popn(stack, dest, (size_t)-1)
182 
195 void ucx_stack_popn(UcxStack *stack, void *dest, size_t n);
196 
203 size_t ucx_stack_avail(UcxStack *stack);
204 
211 #define ucx_stack_empty(stack) (!(stack)->top)
212 
223 #define ucx_stack_dim(size, elems) (size+sizeof(struct ucx_stack_metadata) * \
224  (elems + 1))
225 
226 
227 #ifdef __cplusplus
228 }
229 #endif
230 
231 #endif /* UCX_STACK_H */
232 
size_t size
Stack size.
Definition: stack.h:57
UCX stack structure.
Definition: stack.h:52
void ucx_stack_free(UcxStack *stack, void *ptr)
Frees memory on the stack.
Definition: stack.c:106
Main UCX Header providing most common definitions.
void * ucx_stack_malloc(UcxStack *stack, size_t n)
Allocates stack memory.
Definition: stack.c:53
Metadata for each UCX stack element.
Definition: stack.h:69
void * ucx_stack_realloc(UcxStack *stack, void *ptr, size_t n)
Reallocates memory on the stack.
Definition: stack.c:79
char * prev
Location of the previous element (NULL if this is the first)
Definition: stack.h:73
size_t ucx_stack_avail(UcxStack *stack)
Returns the remaining available memory on the specified stack.
Definition: stack.c:133
UCX allocator data structure containing memory management functions.
Definition: allocator.h:88
char * space
Pointer to the bottom of the stack.
Definition: stack.h:60
void ucx_stack_popn(UcxStack *stack, void *dest, size_t n)
Removes the top most element from the stack and copies the content to dest.
Definition: stack.c:118
Allocator for custom memory management.
void ucx_stack_init(UcxStack *stack, char *space, size_t size)
Initializes UcxStack structure with memory.
Definition: stack.c:41
size_t size
Size of this element.
Definition: stack.h:76
void * ucx_stack_calloc(UcxStack *stack, size_t nelem, size_t elsize)
Allocates an array of stack memory.
Definition: stack.c:73
char * top
Pointer to the top of the stack.
Definition: stack.h:63
UcxAllocator allocator
UcxAllocator based on this stack.
Definition: stack.h:54