moved id-to-data mapping functions to imap-imp.h out of mod-dir.c

This commit is contained in:
hyung-hwan 2019-05-13 16:35:33 +00:00
parent 546d34d732
commit 86c1819a1d
9 changed files with 262 additions and 161 deletions

View File

@ -37,7 +37,7 @@ endif
noinst_HEADERS = awk-prv.h err.h rio.h val.h fnc.h misc.h parse.h run.h tree.h std.h
lib_LTLIBRARIES = libqseawk.la
libqseawk_la_SOURCES = awk.c err.c tree.c parse.c run.c rec.c val.c val-imp.h fnc.c misc.c misc-imp.h rio.c std.c
libqseawk_la_SOURCES = awk.c err.c tree.c parse.c run.c rec.c val.c val-imp.h fnc.c imap-imp.h misc.c misc-imp.h rio.c std.c
libqseawk_la_CPPFLAGS = $(CPPFLAGS_LIB_COMMON)
libqseawk_la_LDFLAGS = $(LDFLAGS_LIB_COMMON)
libqseawk_la_LIBADD = $(LIBADD_LIB_COMMON)

View File

@ -219,7 +219,7 @@ libqseawk_sys_la_LINK = $(LIBTOOL) $(AM_V_lt) --tag=CC \
am__DEPENDENCIES_4 = $(am__DEPENDENCIES_1) $(am__DEPENDENCIES_2) \
$(am__DEPENDENCIES_3)
am__libqseawk_la_SOURCES_DIST = awk.c err.c tree.c parse.c run.c rec.c \
val.c val-imp.h fnc.c misc.c misc-imp.h rio.c std.c mod-dir.c \
val.c val-imp.h fnc.c imap-imp.h misc.c misc-imp.h rio.c std.c mod-dir.c \
mod-dir.h mod-math.c mod-math.h mod-str.c mod-str.h mod-sys.c \
mod-sys.h
@ENABLE_STATIC_MODULE_TRUE@am__objects_1 = libqseawk_la-mod-dir.lo \
@ -516,7 +516,7 @@ LIBADD_LIB_COMMON = -lqsecmn -lqsesi $(LIBM) $(am__append_1) \
noinst_HEADERS = awk-prv.h err.h rio.h val.h fnc.h misc.h parse.h run.h tree.h std.h
lib_LTLIBRARIES = libqseawk.la $(am__append_5)
libqseawk_la_SOURCES = awk.c err.c tree.c parse.c run.c rec.c val.c \
val-imp.h fnc.c misc.c misc-imp.h rio.c std.c $(am__append_6)
val-imp.h fnc.c imap-imp.h misc.c misc-imp.h rio.c std.c $(am__append_6)
libqseawk_la_CPPFLAGS = $(CPPFLAGS_LIB_COMMON)
libqseawk_la_LDFLAGS = $(LDFLAGS_LIB_COMMON)
libqseawk_la_LIBADD = $(LIBADD_LIB_COMMON) $(am__append_7) \

155
qse/lib/awk/imap-imp.h Normal file
View File

@ -0,0 +1,155 @@
/*
* $Id$
*
Copyright (c) 2006-2014 Chung, Hyung-Hwan. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/* THIS FILE IS SUPPOSED TO BE INCLUDED BY MODULE SOURCE THAT MAINTAINS MAPPING BETWEEN ID AND DATA */
typedef struct __IMAP_NODE_T __IMAP_NODE_T;
struct __IMAP_NODE_T
{
__IMAP_NODE_T* prev;
__IMAP_NODE_T* next;
int id;
__IMAP_NODE_T_DATA
};
struct __IMAP_LIST_T
{
__IMAP_NODE_T* head;
__IMAP_NODE_T* tail;
__IMAP_NODE_T* free;
/* mapping table to map 'id' to 'node' */
struct
{
__IMAP_NODE_T** tab;
int capa;
int high;
} map;
__IMAP_LIST_T_DATA
};
static __IMAP_NODE_T* __MAKE_IMAP_NODE (qse_awk_rtx_t* rtx, __IMAP_LIST_T* list)
{
/* create a new context node and append it to the list tail */
__IMAP_NODE_T* node;
node = QSE_NULL;
if (list->free) node = list->free;
else
{
node = qse_awk_rtx_callocmem(rtx, QSE_SIZEOF(*node));
if (!node) goto oops;
}
if (node == list->free) list->free = node->next;
else
{
if (list->map.high <= list->map.capa)
{
qse_size_t newcapa, inc;
__IMAP_NODE_T** tmp;
inc = QSE_TYPE_MAX(int) - list->map.capa;
if (inc == 0) goto oops; /* too many ndoes */
if (inc > 64) inc = 64;
newcapa = (qse_size_t)list->map.capa + inc;
tmp = (__IMAP_NODE_T**)qse_awk_rtx_reallocmem(rtx, list->map.tab, QSE_SIZEOF(*tmp) * newcapa);
if (!tmp) goto oops;
QSE_MEMSET (&tmp[list->map.capa], 0, QSE_SIZEOF(*tmp) * (newcapa - list->map.capa));
list->map.tab = tmp;
list->map.capa = newcapa;
}
node->id = list->map.high;
QSE_ASSERT (list->map.tab[node->id] == QSE_NULL);
list->map.tab[node->id] = node;
list->map.high++;
}
/* append it to the tail */
node->next = QSE_NULL;
node->prev = list->tail;
if (list->tail) list->tail->next = node;
else list->head = node;
list->tail = node;
return node;
oops:
if (node) qse_awk_rtx_freemem (rtx, node);
return QSE_NULL;
}
static void __FREE_IMAP_NODE (qse_awk_rtx_t* rtx, __IMAP_LIST_T* list, __IMAP_NODE_T* node)
{
if (node->prev) node->prev->next = node->next;
if (node->next) node->next->prev = node->prev;
if (list->head == node) list->head = node->next;
if (list->tail == node) list->tail = node->prev;
list->map.tab[node->id] = QSE_NULL;
if (list->map.high == node->id + 1)
{
/* destroy the actual node if the node to be freed has the
* highest id */
qse_awk_rtx_freemem (rtx, node);
list->map.high--;
}
else
{
/* otherwise, chain the node to the free list */
node->next = list->free;
list->free = node;
}
/* however, i destroy the whole free list when all the nodes are
* chanined to the free list */
if (list->head == QSE_NULL)
{
__IMAP_NODE_T* curnode;
while (list->free)
{
curnode = list->free;
list->free = list->free->next;
QSE_ASSERT (curnode->ctx == QSE_NULL);
qse_awk_rtx_freemem (rtx, curnode);
}
qse_awk_rtx_freemem (rtx, list->map.tab);
list->map.high = 0;
list->map.capa = 0;
list->map.tab = QSE_NULL;
}
}

View File

@ -62,31 +62,6 @@ enum
DIR_EMAPTOSCALAR
};
struct dir_node_t
{
int id;
qse_dir_t* ctx;
dir_node_t* prev;
dir_node_t* next;
};
struct dir_list_t
{
dir_node_t* head;
dir_node_t* tail;
dir_node_t* free;
/* mapping table to map 'id' to 'node' */
struct
{
dir_node_t** tab;
qse_size_t capa;
qse_size_t high;
} map;
int errnum;
};
static int dir_err_to_errnum (qse_dir_errnum_t num)
{
switch (num)
@ -131,131 +106,46 @@ static int awk_err_to_errnum (qse_awk_errnum_t num)
}
}
#define __IMAP_NODE_T_DATA qse_dir_t* ctx;
#define __IMAP_LIST_T_DATA int errnum;
#define __IMAP_LIST_T dir_list_t
#define __IMAP_NODE_T dir_node_t
#define __MAKE_IMAP_NODE __new_dir_node
#define __FREE_IMAP_NODE __free_dir_node
#include "imap-imp.h"
static dir_node_t* new_dir_node (qse_awk_rtx_t* rtx, dir_list_t* list, const qse_char_t* path, qse_awk_int_t flags)
{
/* create a new context node and append it to the list tail */
dir_node_t* node;
qse_dir_errnum_t oe;
node = QSE_NULL;
if (list->free) node = list->free;
else
{
node = qse_awk_rtx_callocmem (rtx, QSE_SIZEOF(*node));
node = __new_dir_node(rtx, list);
if (!node)
{
list->errnum = DIR_ENOMEM;
goto oops;
}
return QSE_NULL;
}
node->ctx = qse_dir_open(qse_awk_rtx_getmmgr(rtx), 0, path, flags, &oe);
if (!node->ctx)
{
list->errnum = dir_err_to_errnum(oe);
goto oops;
__free_dir_node (rtx, list, node);
return QSE_NULL;
}
if (node == list->free) list->free = node->next;
else
{
if (list->map.high <= list->map.capa)
{
qse_size_t newcapa;
dir_node_t** tmp;
newcapa = list->map.capa + 64;
if (newcapa < list->map.capa) goto oops; /* overflow */
tmp = (dir_node_t**) qse_awk_rtx_reallocmem (
rtx, list->map.tab, QSE_SIZEOF(*tmp) * newcapa);
if (!tmp)
{
list->errnum = DIR_ENOMEM;
goto oops;
}
QSE_MEMSET (&tmp[list->map.capa], 0,
QSE_SIZEOF(*tmp) * (newcapa - list->map.capa));
list->map.tab = tmp;
list->map.capa = newcapa;
}
node->id = list->map.high;
QSE_ASSERT (list->map.tab[node->id] == QSE_NULL);
list->map.tab[node->id] = node;
list->map.high++;
}
/* append it to the tail */
node->next = QSE_NULL;
node->prev = list->tail;
if (list->tail) list->tail->next = node;
else list->head = node;
list->tail = node;
return node;
oops:
if (node) qse_awk_rtx_freemem (rtx, node);
return QSE_NULL;
}
static void free_dir_node (qse_awk_rtx_t* rtx, dir_list_t* list, dir_node_t* node)
{
if (node->prev) node->prev->next = node->next;
if (node->next) node->next->prev = node->prev;
if (list->head == node) list->head = node->next;
if (list->tail == node) list->tail = node->prev;
list->map.tab[node->id] = QSE_NULL;
if (node->ctx)
{
qse_dir_close(node->ctx);
}
if (list->map.high == node->id + 1)
{
/* destroy the actual node if the node to be freed has the
* highest id */
QSE_MMGR_FREE (qse_awk_rtx_getmmgr(rtx), node);
list->map.high--;
}
else
{
/* otherwise, chain the node to the free list */
node->ctx = QSE_NULL;
node->next = list->free;
list->free = node;
}
/* however, i destroy the whole free list when all the nodes are
* chanined to the free list */
if (list->head == QSE_NULL)
{
qse_mmgr_t* mmgr;
dir_node_t* curnode;
mmgr = qse_awk_rtx_getmmgr(rtx);
while (list->free)
{
curnode = list->free;
list->free = list->free->next;
QSE_ASSERT (curnode->ctx == QSE_NULL);
QSE_MMGR_FREE (mmgr, curnode);
__free_dir_node (rtx, list, node);
}
QSE_MMGR_FREE (mmgr, list->map.tab);
list->map.high = 0;
list->map.capa = 0;
list->map.tab = QSE_NULL;
}
}
/* ------------------------------------------------------------------------ */
static int close_byid (qse_awk_rtx_t* rtx, dir_list_t* list, qse_awk_int_t id)

View File

@ -282,7 +282,7 @@ int qse_awk_mod_mpi (qse_awk_mod_t* mod, qse_awk_t* awk)
* and the module wasn't built. So you can't access mpi::xxx symbols either
*/
QSE_EXPORT int qse_awk_mod_mpi_init (int argc, qse_achar_t* argv[])
int qse_awk_mod_mpi_init (int argc, qse_achar_t* argv[])
{
int rx;
@ -296,7 +296,7 @@ QSE_EXPORT int qse_awk_mod_mpi_init (int argc, qse_achar_t* argv[])
return rx;
}
QSE_EXPORT void qse_awk_mod_mpi_fini (void)
void qse_awk_mod_mpi_fini (void)
{
MPI_Finalize ();
}

View File

@ -35,6 +35,10 @@ extern "C" {
QSE_EXPORT int qse_awk_mod_mpi (qse_awk_mod_t* mod, qse_awk_t* awk);
QSE_EXPORT int qse_awk_mod_mpi_init (int argc, qse_achar_t* argv[]);
QSE_EXPORT void qse_awk_mod_mpi_fini (void);
#if defined(__cplusplus)
}
#endif

View File

@ -25,9 +25,41 @@
*/
#include "mod-mysql.h"
#include <mysql/mysql.h>
#define __IMAP_NODE_T_DATA MYSQL ctx;
#define __IMAP_LIST_T_DATA /* nothing */
#define __IMAP_LIST_T sql_list_t
#define __IMAP_NODE_T sql_node_t
#define __MAKE_IMAP_NODE __new_sql_node
#define __FREE_IMAP_NODE __free_sql_node
#include "../lib/awk/imap-imp.h"
static sql_node_t* new_sql_node (qse_awk_rtx_t* rtx, sql_list_t* list)
{
sql_node_t* node;
node = __new_sql_node(rtx, list);
if (!node) return QSE_NULL;
if (mysql_init(&node->ctx) == QSE_NULL)
{
__free_sql_node (rtx, list, node);
return QSE_NULL;
}
return node;
}
static void free_sql_node (qse_awk_rtx_t* rtx, sql_list_t* list, sql_node_t* node)
{
mysqL_close (&node->ctx);
__free_sql_node (rtx, list, node);
}
static int fnc_open (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi)
{
sql_node_t* node;
return -1;
}
@ -115,12 +147,14 @@ static int query (qse_awk_mod_t* mod, qse_awk_t* awk, const qse_char_t* name, qs
static int init (qse_awk_mod_t* mod, qse_awk_rtx_t* rtx)
{
mysql_library_init (0, QSE_NULL, QSE_NULL);
return 0;
}
static void fini (qse_awk_mod_t* mod, qse_awk_rtx_t* rtx)
{
/* TODO: anything */
mysql_library_end ();
}
static void unload (qse_awk_mod_t* mod, qse_awk_t* awk)
@ -139,5 +173,19 @@ int qse_awk_mod_mysql (qse_awk_mod_t* mod, qse_awk_t* awk)
mod->ctx...
*/
return 0;
}
int qse_awk_mod_mysql_init (int argc, qse_achar_t* argv[])
{
if (mysql_library_init(argc, argv, QSE_NULL) != 0) return -1;
return 0;
}
void qse_awk_mod_mysql_fini (void)
{
mysql_library_end ();
}

View File

@ -35,6 +35,10 @@ extern "C" {
QSE_EXPORT int qse_awk_mod_mysql (qse_awk_mod_t* mod, qse_awk_t* awk);
QSE_EXPORT int qse_awk_mod_mysql_init (int argc, qse_achar_t* argv[]);
QSE_EXPORT void qse_awk_mod_mysql_fini (void);
#if defined(__cplusplus)
}
#endif