]> git.ipfire.org Git - thirdparty/bind9.git/commitdiff
implement fixed-size array stack data structure
authorEvan Hunt <each@isc.org>
Tue, 5 Nov 2019 21:50:43 +0000 (13:50 -0800)
committerEvan Hunt <each@isc.org>
Thu, 7 Nov 2019 19:55:37 +0000 (11:55 -0800)
lib/isc/Makefile.in
lib/isc/astack.c [new file with mode: 0644]
lib/isc/include/isc/Makefile.in
lib/isc/include/isc/astack.h [new file with mode: 0644]
lib/isc/include/isc/types.h
lib/isc/win32/libisc.def.in
lib/isc/win32/libisc.vcxproj.filters.in
lib/isc/win32/libisc.vcxproj.in
util/copyrights

index d30031faeb2e4900cf0b723586db230a6af835b3..1f0066a60da8cf47854d8b18c3ebe5f373aa945c 100644 (file)
@@ -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 \
diff --git a/lib/isc/astack.c b/lib/isc/astack.c
new file mode 100644 (file)
index 0000000..05d9436
--- /dev/null
@@ -0,0 +1,80 @@
+/*
+ * 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));
+}
index 9b93fbe1f6d38c9c0e2aa869d16d1e4ef6181eec..620c8c308d0ba6b18fc0e380f798feae181209f6 100644 (file)
@@ -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 \
diff --git a/lib/isc/include/isc/astack.h b/lib/isc/include/isc/astack.h
new file mode 100644 (file)
index 0000000..e0ea66c
--- /dev/null
@@ -0,0 +1,44 @@
+/*
+ * 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.
+ */
index 6520d23711127556e88cc5baa71331312cae3478..0c11eadc6293e5507d738dc9d49c4495986ae3d8 100644 (file)
@@ -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 */
index 89a46daa1abe7d96661043ff2a4f10ca97688bd0..6df89c29ff62f4042e019a5e8aa39b0221515822 100644 (file)
@@ -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
index 36bfa472586cf8ee5817bfe3e4f7081aa264841d..188f0dbdb4dd107a3dba8a51d1c631c058bf8794 100644 (file)
@@ -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>
     <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>
index 552443fd506b3451fa45de0885215fb77d28e8a3..eed5338b8573ac91051a016bb495592f6d4da294 100644 (file)
@@ -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" />
index 7f6dd8774c095f59e69ff83d37b85a628d4cb2db..b2be20d76f1cb633f250a6ed4bcfae58afe848e5 100644 (file)
 ./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
 ./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