فهرست منبع

- The generic part of other db api related structures.
- If included in other data structures then DB drivers can
extended them with their own data
- also implements DBLIST macros that can be used to arrange
data structures into linked lists

Jan Janak 18 سال پیش
والد
کامیت
dae0465787
2فایلهای تغییر یافته به همراه267 افزوده شده و 0 حذف شده
  1. 57 0
      db/db_gen.c
  2. 210 0
      db/db_gen.h

+ 57 - 0
db/db_gen.c

@@ -0,0 +1,57 @@
+/* 
+ * $Id$ 
+ *
+ * Copyright (C) 2001-2003 FhG FOKUS
+ * Copyright (C) 2006-2007 iptelorg GmbH
+ *
+ * This file is part of ser, a free SIP server.
+ *
+ * ser is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version
+ *
+ * For a license to use the ser software under conditions
+ * other than those described here, or to purchase support for this
+ * software, please contact iptel.org by e-mail at the following addresses:
+ *    [email protected]
+ *
+ * ser is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License 
+ * along with this program; if not, write to the Free Software 
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+#include <string.h>
+#include "db_gen.h"
+
+int db_payload_idx = 0;
+
+
+/*
+ * Initialize a db_gen structure and make space for the data
+ * from n database drivers
+ */
+int db_gen_init(db_gen_t* gen)
+{
+	memset(gen, '\0', sizeof(db_gen_t));
+	return 0;
+}
+
+
+/*
+ * Free all memory allocated by a db_gen structure
+ */
+void db_gen_free(db_gen_t* gen)
+{
+	int i;
+
+	/* Dispose all the attached data structures */
+	for(i = 0; i < DB_PAYLOAD_MAX && gen->data[i]; i++) {
+		if (gen->data[i]) gen->data[i]->free(gen->data[i]);
+	}
+}

+ 210 - 0
db/db_gen.h

@@ -0,0 +1,210 @@
+/* 
+ * $Id$ 
+ *
+ * Copyright (C) 2001-2003 FhG FOKUS
+ * Copyright (C) 2006-2007 iptelorg GmbH
+ *
+ * This file is part of ser, a free SIP server.
+ *
+ * ser is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version
+ *
+ * For a license to use the ser software under conditions
+ * other than those described here, or to purchase support for this
+ * software, please contact iptel.org by e-mail at the following addresses:
+ *    [email protected]
+ *
+ * ser is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License 
+ * along with this program; if not, write to the Free Software 
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+#ifndef _DB_GEN_H
+#define _DB_GEN_H  1
+
+#include "../str.h"
+#include "../list.h"
+#include "db_drv.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif /* __cplusplus */
+
+
+/*
+ * Operations on top of linked-lists of generic DB API structures
+ */
+
+
+/*
+ * Declare a list of DB API structures with given structure
+ * name
+ */
+#define DBLIST_HEAD(name) \
+	STAILQ_HEAD(name, db_gen)
+
+/*
+ * All structures that ought to be members of the DB API
+ * linked lists must have this element as the _first_
+ * element in the structure
+ */
+#define DBLIST_ENTRY \
+	STAILQ_ENTRY(db_gen) next
+
+
+/*
+ * Initialize the static head of linked lists of DB API
+ * structures
+ */
+#define DBLIST_INITIALIZER(head) \
+	STAILQ_HEAD_INITIALIZER(head)
+
+/*
+ * Initialize the head of the list
+ */
+#define DBLIST_INIT(head) \
+	STAILQ_INIT(head)
+
+
+/*
+ * Insert a new DB API structure at the beginning of the
+ * linked list
+ */
+#define DBLIST_INSERT_HEAD(head, elem) \
+	STAILQ_INSERT_HEAD((head), (struct db_gen*)(elem), next)
+
+/*
+ * Add an element at the tail of the list
+ */
+#define DBLIST_INSERT_TAIL(head, elem) \
+	STAILQ_INSERT_TAIL((head), ((struct db_gen*)(elem)), next)
+
+/*
+ * Remove a given structure from a linked list of DB API
+ * structures
+ */
+#define DBLIST_REMOVE(head, elem) \
+	STAILQ_REMOVE(head, (struct db_gen*)(elem), db_gen, next)
+
+/*
+ * Remove a given structure from a linked list of DB API
+ * structures
+ */
+#define DBLIST_REMOVE_HEAD(head) \
+	STAILQ_REMOVE_HEAD(head, next)
+
+/*
+ * Iterate through the elements of the list, store
+ * the pointer to the current element in var variable
+ */
+
+/*
+ * FIXME: We should find some other way of doing this than just copying
+ * and pasting the code from STAILQ_FOREACH
+ */
+#define DBLIST_FOREACH(var, head)				 \
+	for((var) = (void*)STAILQ_FIRST((head));	 \
+		(var);								     \
+		(var) = (void*)STAILQ_NEXT(((struct db_gen*)(var)), next))
+
+/*
+ * Iterate through the elements of the list, the pointer
+ * to the current element is stored in var variable, this
+ * is the safe version of the macro which allows you to
+ * remove the element from the list. tvar is a temporary
+ * variable for internal use by the macro
+ */
+
+/*
+ * FIXME: We should find some other way of doing this than just copying
+ * and pasting the code from STAILQ_FOREACH_SAFE
+ */
+#define DBLIST_FOREACH_SAFE(var, head, tvar)					 \
+	for ((var) = (void*)STAILQ_FIRST((head));					 \
+		 (var) && ((tvar) = (void*)STAILQ_NEXT(((struct db_gen*)(var)), next), 1); \
+		 (var) = (tvar))
+
+/*
+ * Maximum number of payload structures that can be attached to
+ * any DB API structure at a time.
+ */
+#define DB_PAYLOAD_MAX 16
+
+struct db_drv;
+
+/*
+ * Template for generic data structures defined in the
+ * DB API. Drivers can cast structure pointers to this to
+ * obtain the pointer to driver specific data
+ *
+ * All variables and attributes to be shared across all DB API
+ * structures should be put into this structure. This structure
+ * is at the beginnning of each DB API structure to ensure that
+ * all DB API structures share some common variables.
+ */
+typedef struct db_gen {
+    DBLIST_ENTRY; /* All DB API structures can be arranged into lists */
+	/* Array of pointers to driver-specific data. The database API
+	 * supports access to multiple databases at the same time and each
+	 * database driver may want to append some data to generic DB structures,
+	 * hence an array. The current position in the array is stored 
+	 * in db_data_idx
+	 */
+	struct db_drv* data[DB_PAYLOAD_MAX];
+} db_gen_t;
+
+/*
+ * Global variable holding the current index of the payload of the driver that
+ * is being executed. DB API is responsible for setting this vaiable before 
+ * calling functions of DB drivers.
+ */
+extern int db_payload_idx;
+
+
+/*
+ * Macros to set/get variable (DB driver specific)
+ * payload to/from generic DB API structures
+ */
+
+
+/*
+ * Attach a driver specific data structure to a generic 
+ * DB API structure
+ */
+#define DB_SET_PAYLOAD(db_struct, drv_data) do { \
+    ((struct db_gen*)(db_struct))->data[db_payload_idx] = (void*)(drv_data); \
+} while(0)
+
+
+/*
+ * Return a driver specific data structure
+ */
+#define DB_GET_PAYLOAD(db_struct) \
+    ((void*)(((struct db_gen*)(db_struct))->data[db_payload_idx]))
+
+
+/*
+ * Initialize a db_gen structure and make space for the data
+ * from n database drivers
+ */
+int db_gen_init(struct db_gen* gen);
+
+
+/*
+ * Free all memory allocated by a db_gen structure
+ */
+void db_gen_free(struct db_gen* gen);
+
+
+#ifdef __cplusplus
+}
+#endif /* __cplusplus */
+
+#endif /* _DB_GEN_H */