null+****@clear*****
null+****@clear*****
2012年 2月 9日 (木) 15:57:47 JST
Susumu Yata 2012-02-09 15:57:47 +0900 (Thu, 09 Feb 2012)
New Revision: 5632ce3e39c0d8bf3c6b758d4cbf5e012cfa00b0
Log:
moved functions and macros from the tokenizer module to the plugin module.
Modified files:
include/groonga/plugin.h
lib/plugin.c
lib/plugin_in.h
Modified: include/groonga/plugin.h (+113 -0)
===================================================================
--- include/groonga/plugin.h 2012-02-09 15:40:01 +0900 (cb4bd10)
+++ include/groonga/plugin.h 2012-02-09 15:57:47 +0900 (0aa56f8)
@@ -18,6 +18,8 @@
#ifndef GRN_PLUGIN_H
#define GRN_PLUGIN_H
+#include <stddef.h>
+
#include <groonga.h>
#ifdef __cplusplus
@@ -38,6 +40,117 @@ GRN_PLUGIN_EXPORT grn_rc GRN_PLUGIN_INIT(grn_ctx *ctx);
GRN_PLUGIN_EXPORT grn_rc GRN_PLUGIN_REGISTER(grn_ctx *ctx);
GRN_PLUGIN_EXPORT grn_rc GRN_PLUGIN_FIN(grn_ctx *ctx);
+/*
+ Don't call these functions directly. Use GRN_PLUGIN_MALLOC(),
+ GRN_PLUGIN_REALLOC() and GRN_PLUGIN_FREE() instead.
+ */
+void *grn_plugin_malloc(grn_ctx *ctx, size_t size, const char *file,
+ int line, const char *func);
+void *grn_plugin_realloc(grn_ctx *ctx, void *ptr, size_t size,
+ const char *file, int line, const char *func);
+void grn_plugin_free(grn_ctx *ctx, void *ptr, const char *file,
+ int line, const char *func);
+
+/*
+ GRN_PLUGIN_MALLOC() allocates `size' bytes and returns a pointer to the
+ allocated memory space. Note that the memory space is associated with `ctx'.
+ */
+#define GRN_PLUGIN_MALLOC(ctx, size) \
+ grn_plugin_malloc((ctx), (size), __FILE__, __LINE__, __FUNCTION__)
+/*
+ GRN_PLUGIN_REALLOC() resizes the memory space pointed to by `ptr' or
+ allocates a new memory space of `size' bytes. GRN_PLUGIN_REALLOC() returns
+ a pointer to the memory space. The contents is unchanged or copied from the
+ old memory space to the new memory space.
+ */
+#define GRN_PLUGIN_REALLOC(ctx, ptr, size) \
+ grn_plugin_realloc((ctx), (ptr), (size), __FILE__, __LINE__, __FUNCTION__)
+/*
+ GRN_PLUGIN_FREE() frees a memory space allocated by GRN_PLUGIN_MALLOC() or
+ GRN_PLUGIN_REALLOC(). This means that `ptr' must be a pointer returned by
+ GRN_PLUGIN_MALLOC() or GRN_PLUGIN_REALLOC().
+ */
+#define GRN_PLUGIN_FREE(ctx, ptr) \
+ grn_plugin_free((ctx), (ptr), __FILE__, __LINE__, __FUNCTION__)
+
+/*
+ GRN_PLUGIN_LOG() reports a log of `level'. Its error message is generated
+ from the varying number of arguments, in which the first one is the format
+ string and the rest are its arguments. See grn_log_level in "groonga.h" for
+ more details of `level'.
+ */
+#define GRN_PLUGIN_LOG(ctx, level, ...) \
+ GRN_LOG((ctx), (level), __VA_ARGS__)
+
+/*
+ Don't call grn_plugin_set_error() directly. This function is used in
+ GRN_PLUGIN_SET_ERROR().
+ */
+void grn_plugin_set_error(grn_ctx *ctx, grn_log_level level,
+ grn_rc error_code,
+ const char *file, int line, const char *func,
+ const char *format, ...);
+
+/*
+ Don't call these functions directly. grn_plugin_backtrace() and
+ grn_plugin_logtrace() are used in GRN_PLUGIN_SET_ERROR().
+ */
+void grn_plugin_backtrace(grn_ctx *ctx);
+void grn_plugin_logtrace(grn_ctx *ctx, grn_log_level level);
+
+/*
+ Don't use GRN_PLUGIN_SET_ERROR() directly. This macro is used in
+ GRN_PLUGIN_ERROR().
+ */
+#define GRN_PLUGIN_SET_ERROR(ctx, level, error_code, ...) do { \
+ grn_plugin_set_error(ctx, level, error_code, \
+ __FILE__, __LINE__, __FUNCTION__, __VA_ARGS__); \
+ GRN_LOG(ctx, level, __VA_ARGS__); \
+ grn_plugin_backtrace(ctx); \
+ grn_plugin_logtrace(ctx, level); \
+} while (0)
+
+/*
+ GRN_PLUGIN_ERROR() reports an error of `error_code'. Its error message is
+ generated from the varying number of arguments, in which the first one is the
+ format string and the rest are its arguments. See grn_rc in "groonga.h" for
+ more details of `error_code'.
+ */
+#define GRN_PLUGIN_ERROR(ctx, error_code, ...) \
+ GRN_PLUGIN_SET_ERROR(ctx, GRN_LOG_ERROR, error_code, __VA_ARGS__)
+
+/*
+ grn_plugin_mutex is available to make a critical section. See the
+ following functions.
+ */
+typedef struct _grn_plugin_mutex grn_plugin_mutex;
+
+/*
+ grn_plugin_mutex_create() returns a pointer to a new object of
+ grn_plugin_mutex. Memory for the new object is obtained with
+ GRN_PLUGIN_MALLOC(). grn_plugin_mutex_create() returns NULL if sufficient
+ memory is not available.
+ */
+grn_plugin_mutex *grn_plugin_mutex_create(grn_ctx *ctx);
+
+/*
+ grn_plugin_mutex_destroy() finalizes an object of grn_plugin_mutex and then
+ frees memory allocated for that object.
+ */
+void grn_plugin_mutex_destroy(grn_ctx *ctx, grn_plugin_mutex *mutex);
+
+/*
+ grn_plugin_mutex_lock() locks a mutex object. If the object is already
+ locked, the calling thread waits until the object will be unlocked.
+ */
+void grn_plugin_mutex_lock(grn_ctx *ctx, grn_plugin_mutex *mutex);
+
+/*
+ grn_plugin_mutex_unlock() unlocks a mutex object. grn_plugin_mutex_unlock()
+ should not be called for an unlocked object.
+ */
+void grn_plugin_mutex_unlock(grn_ctx *ctx, grn_plugin_mutex *mutex);
+
#ifdef __cplusplus
}
#endif
Modified: lib/plugin.c (+113 -0)
===================================================================
--- lib/plugin.c 2012-02-09 15:40:01 +0900 (2c03f93)
+++ lib/plugin.c 2012-02-09 15:57:47 +0900 (143fa3d)
@@ -15,11 +15,16 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
+#ifndef GRN_PLUGIN_H
+#include "groonga/plugin.h"
+#endif /* GRN_PLUGIN_H */
+
#include "db.h"
#include "plugin_in.h"
#include "ctx_impl.h"
#include "util.h"
+#include <stdarg.h>
#include <stdio.h>
#include <string.h>
@@ -413,3 +418,111 @@ grn_plugin_register(grn_ctx *ctx, const char *name)
return grn_plugin_register_by_path(ctx, path);
}
+
+void *
+grn_plugin_malloc(grn_ctx *ctx, size_t size, const char *file, int line,
+ const char *func)
+{
+ return grn_malloc(ctx, size, file, line, func);
+}
+
+void *
+grn_plugin_realloc(grn_ctx *ctx, void *ptr, size_t size,
+ const char *file, int line, const char *func)
+{
+ return grn_realloc(ctx, ptr, size, file, line, func);
+}
+
+void
+grn_plugin_free(grn_ctx *ctx, void *ptr, const char *file, int line,
+ const char *func)
+{
+ return grn_free(ctx, ptr, file, line, func);
+}
+
+/*
+ grn_plugin_ctx_log() is a clone of grn_ctx_log() in ctx.c. The only
+ difference is that grn_plugin_ctx_log() uses va_list instead of `...'.
+ */
+static void
+grn_plugin_ctx_log(grn_ctx *ctx, const char *format, va_list ap)
+{
+ va_list aq;
+ va_copy(aq, ap);
+ vsnprintf(ctx->errbuf, GRN_CTX_MSGSIZE, format, aq);
+ va_end(aq);
+}
+
+void
+grn_plugin_set_error(grn_ctx *ctx, grn_log_level level, grn_rc error_code,
+ const char *file, int line, const char *func,
+ const char *format, ...)
+{
+ ctx->errlvl = level;
+ ctx->rc = error_code;
+ ctx->errfile = file;
+ ctx->errline = line;
+ ctx->errfunc = func;
+ grn_ctx_impl_err(ctx);
+
+ {
+ va_list ap;
+ va_start(ap, format);
+ grn_plugin_ctx_log(ctx, format, ap);
+ va_end(ap);
+ }
+}
+
+void
+grn_plugin_backtrace(grn_ctx *ctx)
+{
+ BACKTRACE(ctx);
+}
+
+void
+grn_plugin_logtrace(grn_ctx *ctx, grn_log_level level)
+{
+ if (level <= GRN_LOG_ERROR) {
+ LOGTRACE(ctx, level);
+ }
+}
+
+struct _grn_plugin_mutex {
+ grn_critical_section critical_section;
+};
+
+grn_plugin_mutex *
+grn_plugin_mutex_create(grn_ctx *ctx)
+{
+ grn_plugin_mutex * const mutex =
+ GRN_PLUGIN_MALLOC(ctx, sizeof(grn_plugin_mutex));
+ if (mutex != NULL) {
+ CRITICAL_SECTION_INIT(mutex->critical_section);
+ }
+ return mutex;
+}
+
+void
+grn_plugin_mutex_destroy(grn_ctx *ctx, grn_plugin_mutex *mutex)
+{
+ if (mutex != NULL) {
+ CRITICAL_SECTION_FIN(mutex->critical_section);
+ GRN_PLUGIN_FREE(ctx, mutex);
+ }
+}
+
+void
+grn_plugin_mutex_lock(grn_ctx *ctx, grn_plugin_mutex *mutex)
+{
+ if (mutex != NULL) {
+ CRITICAL_SECTION_ENTER(mutex->critical_section);
+ }
+}
+
+void
+grn_plugin_mutex_unlock(grn_ctx *ctx, grn_plugin_mutex *mutex)
+{
+ if (mutex != NULL) {
+ CRITICAL_SECTION_LEAVE(mutex->critical_section);
+ }
+}
Modified: lib/plugin_in.h (+3 -3)
===================================================================
--- lib/plugin_in.h 2012-02-09 15:40:01 +0900 (6acfe5f)
+++ lib/plugin_in.h 2012-02-09 15:57:47 +0900 (b22c26b)
@@ -14,8 +14,8 @@
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
-#ifndef GRN_PLUGIN_H
-#define GRN_PLUGIN_H
+#ifndef GRN_PLUGIN_IN_H
+#define GRN_PLUGIN_IN_H
#ifndef GROONGA_IN_H
#include "groonga_in.h"
@@ -64,4 +64,4 @@ const char *grn_plugin_path(grn_ctx *ctx, grn_id id);
}
#endif
-#endif /* GRN_PLUGIN_H */
+#endif /* GRN_PLUGIN_IN_H */