src/ucx/stack.h

changeset 301
0f83916c1639
parent 259
2f5dea574a75
equal deleted inserted replaced
299:e7dfcf229625 301:0f83916c1639
89 /** 89 /**
90 * Allocates stack memory. 90 * Allocates stack memory.
91 * 91 *
92 * @param stack a pointer to the stack 92 * @param stack a pointer to the stack
93 * @param n amount of memory to allocate 93 * @param n amount of memory to allocate
94 * @return a pointer to the allocated memory 94 * @return a pointer to the allocated memory or <code>NULL</code> on stack
95 * overflow
95 * @see ucx_allocator_malloc() 96 * @see ucx_allocator_malloc()
96 */ 97 */
97 void *ucx_stack_malloc(UcxStack *stack, size_t n); 98 void *ucx_stack_malloc(UcxStack *stack, size_t n);
98 99
99 /** 100 /**
100 * Alias for #ucx_stack_malloc(). 101 * Allocates memory with #ucx_stack_malloc() and copies the specified data if
102 * the allocation was successful.
103 *
101 * @param stack a pointer to the stack 104 * @param stack a pointer to the stack
102 * @param n amount of memory to allocate 105 * @param n amount of memory to allocate
106 * @param data a pointer to the data to copy
103 * @return a pointer to the allocated memory 107 * @return a pointer to the allocated memory
104 * @see ucx_stack_malloc 108 * @see ucx_stack_malloc
105 */ 109 */
106 #define ucx_stack_push(stack, n) ucx_stack_malloc(stack, n) 110 void *ucx_stack_push(UcxStack *stack, size_t n, const void *data);
107 111
108 /** 112 /**
109 * Allocates an array of stack memory 113 * Allocates an array of stack memory
110 * 114 *
111 * The content of the allocated memory is set to zero. 115 * The content of the allocated memory is set to zero.
117 * @see ucx_allocator_calloc() 121 * @see ucx_allocator_calloc()
118 */ 122 */
119 void *ucx_stack_calloc(UcxStack *stack, size_t nelem, size_t elsize); 123 void *ucx_stack_calloc(UcxStack *stack, size_t nelem, size_t elsize);
120 124
121 /** 125 /**
122 * Alias for #ucx_stack_calloc(). 126 * Allocates memory with #ucx_stack_calloc() and copies the specified data if
123 * 127 * the allocation was successful.
124 * @param stack a pointer to the stack 128 *
125 * @param n amount of elements to allocate 129 * @param stack a pointer to the stack
130 * @param nelem amount of elements to allocate
126 * @param elsize amount of memory per element 131 * @param elsize amount of memory per element
132 * @param data a pointer to the data
127 * @return a pointer to the allocated memory 133 * @return a pointer to the allocated memory
128 * @see ucx_stack_calloc 134 * @see ucx_stack_calloc
129 */ 135 */
130 #define ucx_stack_pusharr(stack,n,elsize) ucx_stack_calloc(stack,n,elssize) 136 void *ucx_stack_pusharr(UcxStack *stack,
137 size_t nelem, size_t elsize, const void *data);
131 138
132 /** 139 /**
133 * Reallocates memory on the stack. 140 * Reallocates memory on the stack.
134 * 141 *
135 * Shrinking memory is always safe. Extending memory can be very expensive. 142 * Shrinking memory is always safe. Extending memory can be very expensive.
182 189
183 /** 190 /**
184 * Removes the top most element from the stack and copies the content to <code> 191 * Removes the top most element from the stack and copies the content to <code>
185 * dest</code>. 192 * dest</code>.
186 * 193 *
187 * In contrast to #ucx_stack_pop() the <code>dest</code> pointer <code>MUST 194 * This function copies at most <code>n</code> bytes to the destination, but
188 * NOT</code> be <code>NULL</code>. 195 * the element is always freed as a whole.
196 * If the element was larger than <code>n</code>, the remaining data is lost.
189 * 197 *
190 * @param stack a pointer to the stack 198 * @param stack a pointer to the stack
191 * @param dest the location where the contents shall be written to 199 * @param dest the location where the contents shall be written to
192 * @param n copies at most n elements to <code>dest</code> 200 * @param n copies at most n bytes to <code>dest</code>
193 * @see ucx_stack_pop 201 * @see ucx_stack_pop
194 */ 202 */
195 void ucx_stack_popn(UcxStack *stack, void *dest, size_t n); 203 void ucx_stack_popn(UcxStack *stack, void *dest, size_t n);
196 204
197 /** 205 /**

mercurial