You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by mt...@apache.org on 2011/05/02 18:44:11 UTC
svn commit: r1098655 - in /commons/sandbox/runtime/trunk/src/main/native:
Makefile.msc.in Makefile.unx.in include/acr/table.h shared/table.c
Author: mturk
Date: Mon May 2 16:44:10 2011
New Revision: 1098655
URL: http://svn.apache.org/viewvc?rev=1098655&view=rev
Log:
Add fifo table for managing socket pollsets
Added:
commons/sandbox/runtime/trunk/src/main/native/include/acr/table.h (with props)
commons/sandbox/runtime/trunk/src/main/native/shared/table.c (with props)
Modified:
commons/sandbox/runtime/trunk/src/main/native/Makefile.msc.in
commons/sandbox/runtime/trunk/src/main/native/Makefile.unx.in
Modified: commons/sandbox/runtime/trunk/src/main/native/Makefile.msc.in
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/Makefile.msc.in?rev=1098655&r1=1098654&r2=1098655&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/Makefile.msc.in (original)
+++ commons/sandbox/runtime/trunk/src/main/native/Makefile.msc.in Mon May 2 16:44:10 2011
@@ -127,6 +127,7 @@ LIBSOURCES=\
$(TOPDIR)\shared\ssock.c \
$(TOPDIR)\shared\string.c \
$(TOPDIR)\shared\system.c \
+ $(TOPDIR)\shared\table.c \
$(TOPDIR)\shared\unsafe.c \
$(TOPDIR)\shared\utime.c \
$(TOPDIR)\shared\uuid.c \
Modified: commons/sandbox/runtime/trunk/src/main/native/Makefile.unx.in
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/Makefile.unx.in?rev=1098655&r1=1098654&r2=1098655&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/Makefile.unx.in (original)
+++ commons/sandbox/runtime/trunk/src/main/native/Makefile.unx.in Mon May 2 16:44:10 2011
@@ -121,6 +121,7 @@ LIBSOURCES=\
$(TOPDIR)/shared/ssock.c \
$(TOPDIR)/shared/string.c \
$(TOPDIR)/shared/system.c \
+ $(TOPDIR)/shared/table.c \
$(TOPDIR)/shared/unsafe.c \
$(TOPDIR)/shared/utime.c \
$(TOPDIR)/shared/uuid.c \
Added: commons/sandbox/runtime/trunk/src/main/native/include/acr/table.h
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/include/acr/table.h?rev=1098655&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/include/acr/table.h (added)
+++ commons/sandbox/runtime/trunk/src/main/native/include/acr/table.h Mon May 2 16:44:10 2011
@@ -0,0 +1,126 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef _ACR_TABLE_H
+#define _ACR_TABLE_H
+
+#include "acr/stdtypes.h"
+#include "acr/memory.h"
+
+typedef struct acr_table_t acr_table_t;
+struct acr_table_t {
+ int nelts;
+ int nalloc;
+ size_t esize;
+ char *elts;
+};
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/**
+ * @file table.h
+ * @brief
+ *
+ * ACR Table functions
+ *
+ */
+/**
+ * Init table
+ * @param nelts Initial number of elements in the table.
+ * @param esize Size of the each element
+ * @note for structures make sure esize is aligned to 8 bytes.
+ */
+int
+AcrTableInit(acr_table_t *arr, int nelts, size_t esize);
+
+/**
+ * Create table
+ * @param nelts Initial number of elements in the table.
+ * @param esize Size of the each element
+ * @note for structures make sure esize is aligned to 8 bytes.
+ */
+acr_table_t *
+AcrTableCreate(int nelts, size_t esize);
+
+/**
+ * Destroy the table and free used memory
+ * @param arr Table to destroy.
+ */
+void
+AcrTableDestroy(acr_table_t *arr);
+
+/**
+ * Add a new element to an table (as a first-in, last-out stack)
+ * @param arr The table to add an element to.
+ */
+void *
+AcrTableAdd(acr_table_t *arr);
+
+/**
+ * Remove an element from an table (as a first-in, last-out stack)
+ * @param arr The table to remove an element from.
+ * @return Location of the element in the table.
+ * @remark If there are no elements in the table, NULL is returned.
+ */
+void *
+AcrTablePop(acr_table_t *arr);
+
+/**
+ * Get an element from an table (as a first-in, last-out stack)
+ * @param arr The table to get an element from.
+ * @return Location of the element in the table.
+ * @remark If there are no elements in the table, NULL is returned.
+ */
+void *
+AcrTableTop(acr_table_t *arr);
+
+/**
+ * Remove all elements from an table.
+ * @param arr The table to remove all elements from.
+ * @remark No memory is freed by this operation,
+ * but is available for reuse.
+ */
+void
+AcrTableClear(acr_table_t *arr);
+
+/** A helper macro for accessing a member of an CPR table.
+ *
+ * @param A the table
+ * @param I the index into the table to return
+ * @param T the type of the objects stored in the table
+ *
+ * @return the item at index i
+ */
+#define ACR_TABLE_GET(T, A, I) ((T *)((A)->elts + ((A)->esize * (I))))
+#define ACR_TABLE_REF(T, A, I) (*((T *)((A)->elts + ((A)->esize * (I)))))
+
+/** A helper macro for pushing elements into an CPR table.
+ *
+ * @param A the table
+ * @param T the type of the objects stored in the table
+ *
+ * @return the location of the new object
+ */
+#define ACR_TABLE_ADD(T, A) ((T *)AcrTableAdd(A))
+
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* _ACR_TABLE_H */
Propchange: commons/sandbox/runtime/trunk/src/main/native/include/acr/table.h
------------------------------------------------------------------------------
svn:eol-style = native
Added: commons/sandbox/runtime/trunk/src/main/native/shared/table.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/shared/table.c?rev=1098655&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/shared/table.c (added)
+++ commons/sandbox/runtime/trunk/src/main/native/shared/table.c Mon May 2 16:44:10 2011
@@ -0,0 +1,140 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "acr/table.h"
+#include "acr/error.h"
+
+int
+AcrTableInit(acr_table_t *arr, int nelts, size_t esize)
+{
+ acr_u32_t celts = nelts;
+
+ if (esize < 1)
+ esize = 1;
+ if (nelts < 8)
+ celts = 8;
+ else
+ celts = ACR_ALIGN_DEFAULT(celts);
+ if (celts > INT_MAX) {
+ ACR_SET_OS_ERROR(ACR_ERANGE);
+ return 0;
+ }
+ arr->elts = (char *)malloc(celts * esize);
+ if (arr->elts == 0)
+ return ACR_GET_OS_ERROR();
+ arr->nalloc = celts;
+ arr->esize = esize;
+ arr->nelts = 0;
+
+ return 0;
+}
+
+acr_table_t *
+AcrTableCreate(int nelts, size_t esize)
+{
+ int rc;
+ acr_table_t *arr = malloc(sizeof(acr_table_t));
+
+ if (arr == 0)
+ return 0;
+ rc = AcrTableInit(arr, nelts, esize);
+ if (rc != 0) {
+ AcrFree(arr);
+ ACR_SET_OS_ERROR(rc);
+ return 0;
+ }
+
+ return arr;
+}
+
+void
+AcrTableDestroy(acr_table_t *arr)
+{
+ if (arr != 0) {
+ AcrFree(arr->elts);
+ AcrFree(arr);
+ }
+}
+
+void *
+AcrTableAdd(acr_table_t *arr)
+{
+ if (arr->elts == 0) {
+ arr->nelts = 0;
+ arr->nalloc = 0;
+ }
+ if (arr->nelts == arr->nalloc) {
+ acr_u32_t ns;
+ char *nd;
+ if (arr->nalloc == 0)
+ ns = 8;
+ else if (arr->nalloc < 65536)
+ ns = arr->nalloc * 2;
+ else
+ ns = arr->nalloc + 65536;
+ if (ns > INT_MAX) {
+ ACR_SET_OS_ERROR(ACR_ERANGE);
+ return 0;
+ }
+ if (arr->elts == 0)
+ nd = (char *)malloc(arr->esize * ns);
+ else
+ nd = (char *)realloc(arr->elts, arr->esize * ns);
+ if (nd == 0)
+ return 0;
+ arr->elts = nd;
+ arr->nalloc = ns;
+ }
+
+ return arr->elts + (arr->esize * arr->nelts++);
+}
+
+void *
+AcrTablePop(acr_table_t *arr)
+{
+ if (arr->elts == 0 || arr->nelts == 0) {
+ return 0;
+ }
+ else {
+ --arr->nelts;
+ return arr->elts + (arr->esize * arr->nelts);
+ }
+}
+
+void *
+AcrTableTop(acr_table_t *arr)
+{
+ if (arr->elts == 0 || arr->nelts == 0) {
+ return 0;
+ }
+ else {
+ /* Return the last element without removing it */
+ return arr->elts + (arr->esize * (arr->nelts - 1));
+ }
+}
+
+void
+AcrTableClear(acr_table_t *arr)
+{
+ if (arr != 0) {
+ arr->nelts = 0;
+ if ((arr->nalloc * arr->esize) > 65536) {
+ AcrFree(arr->elts);
+ arr->nalloc = 0;
+ arr->elts = 0;
+ }
+ }
+}
Propchange: commons/sandbox/runtime/trunk/src/main/native/shared/table.c
------------------------------------------------------------------------------
svn:eol-style = native