Commit a8c814cb authored by Evan Hunt's avatar Evan Hunt

implement fixed-size array stack data structure

parent 402969bf
......@@ -46,7 +46,7 @@ WIN32OBJS = win32/condition.@O@ win32/dir.@O@ win32/errno.@O@ \
# Alphabetically
OBJS = pk11.@O@ pk11_result.@O@ \
aes.@O@ app.@O@ assertions.@O@ \
aes.@O@ app.@O@ assertions.@O@ astack.@O@ \
backtrace.@O@ base32.@O@ base64.@O@ \
bind9.@O@ buffer.@O@ bufferlist.@O@ \
commandline.@O@ counter.@O@ crc64.@O@ error.@O@ entropy.@O@ \
......@@ -66,7 +66,7 @@ SYMTBLOBJS = backtrace-emptytbl.@O@
# Alphabetically
SRCS = pk11.c pk11_result.c \
aes.c app.c assertions.c \
aes.c app.c assertions.c astack.c \
backtrace.c base32.c base64.c bind9.c \
buffer.c bufferlist.c commandline.c counter.c crc64.c \
entropy.c error.c event.c hash.c ht.c heap.c \
......
/*
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* See the COPYRIGHT file distributed with this work for additional
* information regarding copyright ownership.
*/
#include <inttypes.h>
#include <string.h>
#include <isc/astack.h>
#include <isc/atomic.h>
#include <isc/mem.h>
#include <isc/mutex.h>
#include <isc/types.h>
#include <isc/util.h>
struct isc_astack {
isc_mem_t *mctx;
size_t size;
size_t pos;
isc_mutex_t lock;
uintptr_t nodes[];
};
isc_astack_t *
isc_astack_new(isc_mem_t *mctx, size_t size) {
isc_astack_t *stack =
isc_mem_get(mctx,
sizeof(isc_astack_t) + size * sizeof(uintptr_t));
stack->mctx = NULL;
isc_mem_attach(mctx, &stack->mctx);
stack->size = size;
stack->pos = 0;
memset(stack->nodes, 0, size * sizeof(uintptr_t));
isc_mutex_init(&stack->lock);
return (stack);
}
bool
isc_astack_trypush(isc_astack_t *stack, void *obj) {
if (isc_mutex_trylock(&stack->lock) == false) {
if (stack->pos >= stack->size) {
isc_mutex_unlock(&stack->lock);
return (false);
}
stack->nodes[stack->pos++] = (uintptr_t) obj;
isc_mutex_unlock(&stack->lock);
return (true);
} else {
return (false);
}
}
void *
isc_astack_pop(isc_astack_t *stack) {
isc_mutex_lock(&stack->lock);
uintptr_t rv;
if (stack->pos == 0) {
rv = 0;
} else {
rv = stack->nodes[--stack->pos];
}
isc_mutex_unlock(&stack->lock);
return ((void*) rv);
}
void
isc_astack_destroy(isc_astack_t *stack) {
REQUIRE(stack->pos == 0);
isc_mem_putanddetach(&stack->mctx, stack,
sizeof(struct isc_astack) +
stack->size * sizeof(uintptr_t));
}
......@@ -18,7 +18,7 @@ VERSION=@BIND9_VERSION@
# machine generated. The latter are handled specially in the
# install target below.
#
HEADERS = aes.h app.h assertions.h atomic.h backtrace.h \
HEADERS = aes.h app.h assertions.h astack.h atomic.h backtrace.h \
base32.h base64.h bind9.h buffer.h bufferlist.h \
commandline.h counter.h crc64.h deprecated.h \
endian.h errno.h error.h event.h eventclass.h \
......
/*
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* See the COPYRIGHT file distributed with this work for additional
* information regarding copyright ownership.
*/
#include <inttypes.h>
#include <isc/mem.h>
#include <isc/types.h>
isc_astack_t *
isc_astack_new(isc_mem_t *mctx, size_t size);
/*%<
* Allocate and initialize a new array stack of size 'size'.
*/
void
isc_astack_destroy(isc_astack_t *stack);
/*%<
* Free an array stack 'stack'.
*
* Requires:
* \li 'stack' is empty.
*/
bool
isc_astack_trypush(isc_astack_t *stack, void *obj);
/*%<
* Try to push 'obj' onto array stack 'astack'. On failure, either
* because the stack size limit has been reached or because another
* thread has already changed the stack pointer, return 'false'.
*/
void *
isc_astack_pop(isc_astack_t *stack);
/*%<
* Pop an object off of array stack 'stack'. If the stack is empty,
* return NULL.
*/
......@@ -31,6 +31,7 @@
*/
#define ISC_LIST(type) struct { type *head, *tail; }
typedef struct isc_astack isc_astack_t; /*%< Array-based fast stack */
typedef struct isc_appctx isc_appctx_t; /*%< Application context */
typedef struct isc_backtrace_symmap isc_backtrace_symmap_t; /*%< Symbol Table Entry */
typedef struct isc_buffer isc_buffer_t; /*%< Buffer */
......
......@@ -24,6 +24,10 @@ isc_app_start
isc_app_unblock
isc_appctx_create
isc_appctx_destroy
isc_astack_destroy
isc_astack_new
isc_astack_pop
isc_astack_trypush
isc__buffer_activeregion
isc__buffer_add
isc__buffer_availableregion
......
......@@ -35,6 +35,9 @@
<ClInclude Include="..\include\isc\assertions.h">
<Filter>Library Header Files</Filter>
</ClInclude>
<ClInclude Include="..\include\isc\astack.h">
<Filter>Library Header Files</Filter>
</ClInclude>
<ClInclude Include="..\include\isc\atomic.h">
<Filter>Library Header Files</Filter>
</ClInclude>
......@@ -452,6 +455,9 @@
<ClCompile Include="..\assertions.c">
<Filter>Library Source Files</Filter>
</ClCompile>
<ClCompile Include="..\astack.c">
<Filter>Library Source Files</Filter>
</ClCompile>
<ClCompile Include="..\backtrace.c">
<Filter>Library Source Files</Filter>
</ClCompile>
......
......@@ -288,6 +288,7 @@ copy InstallFiles ..\Build\Release\
<ClInclude Include="..\include\isc\aes.h" />
<ClInclude Include="..\include\isc\app.h" />
<ClInclude Include="..\include\isc\assertions.h" />
<ClInclude Include="..\include\isc\astack.h" />
<ClInclude Include="..\include\isc\atomic.h" />
<ClInclude Include="..\include\isc\backtrace.h" />
<ClInclude Include="..\include\isc\base32.h" />
......@@ -405,6 +406,7 @@ copy InstallFiles ..\Build\Release\
<ClCompile Include="..\aes.c" />
<ClCompile Include="..\app.c" />
<ClCompile Include="..\assertions.c" />
<ClCompile Include="..\astack.c" />
<ClCompile Include="..\backtrace-emptytbl.c" />
<ClCompile Include="..\backtrace.c" />
<ClCompile Include="..\base32.c" />
......
......@@ -2129,6 +2129,7 @@
./lib/isc/api X 1999,2000,2001,2006,2008,2009,2010,2011,2012,2013,2014,2015,2016,2017,2018,2019
./lib/isc/app.c C 1999,2000,2001,2002,2003,2004,2005,2007,2008,2009,2013,2014,2015,2016,2017,2018,2019
./lib/isc/assertions.c C 1997,1998,1999,2000,2001,2004,2005,2007,2008,2009,2015,2016,2018,2019
./lib/isc/astack.c C 2019
./lib/isc/backtrace-emptytbl.c C 2009,2016,2018,2019
./lib/isc/backtrace.c C 2009,2013,2014,2015,2016,2018,2019
./lib/isc/base32.c C 2008,2009,2013,2014,2015,2016,2018,2019
......@@ -2156,6 +2157,7 @@
./lib/isc/include/isc/aes.h C 2014,2016,2018,2019
./lib/isc/include/isc/app.h C 1999,2000,2001,2004,2005,2006,2007,2009,2013,2014,2015,2016,2018,2019
./lib/isc/include/isc/assertions.h C 1997,1998,1999,2000,2001,2004,2005,2006,2007,2008,2009,2016,2017,2018,2019
./lib/isc/include/isc/astack.h C 2019
./lib/isc/include/isc/atomic.h C 2018,2019
./lib/isc/include/isc/backtrace.h C 2009,2016,2018,2019
./lib/isc/include/isc/base32.h C 2008,2014,2016,2018,2019
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment