34 changed files with 764 additions and 258 deletions
-
1CMakeLists.txt
-
4Makefile.am
-
2configure.in
-
5include/Makefile.am
-
10include/m_string.h
-
2include/my_sys.h
-
129include/mysql/plugin.h
-
55include/mysql/plugin.h.pp
-
98include/mysql/service_my_snprintf.h
-
128include/mysql/service_thd_alloc.h
-
30include/mysql/services.h
-
24include/service_versions.h
-
20libservices/CMakeLists.txt
-
100libservices/HOWTO
-
19libservices/Makefile.am
-
17libservices/my_snprintf_service.c
-
17libservices/thd_alloc_service.c
-
9mysql-test/r/plugin.result
-
6mysql-test/t/plugin.test
-
23mysys/my_error.c
-
3plugin/daemon_example/Makefile.am
-
2sql/CMakeLists.txt
-
2sql/Makefile.am
-
10sql/mysql_priv.h
-
60sql/replication.h
-
20sql/sql_class.cc
-
22sql/sql_class.h
-
122sql/sql_plugin.cc
-
10sql/sql_plugin.h
-
44sql/sql_plugin_services.h
-
2storage/example/Makefile.am
-
22storage/example/ha_example.cc
-
2storage/mysql_storage_engine.cmake
-
2strings/my_vsnprintf.c
@ -0,0 +1,98 @@ |
|||
#ifndef MYSQL_SERVICE_MY_SNPRINTF_INCLUDED |
|||
/* Copyright (C) 2009 Sun Microsystems, Inc. |
|||
|
|||
This program 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; version 2 of the License. |
|||
|
|||
This program 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 */ |
|||
|
|||
/** |
|||
@file |
|||
my_snprintf service |
|||
|
|||
Portable and limited vsnprintf() implementation. |
|||
|
|||
This is a portable, limited vsnprintf() implementation, with some |
|||
extra features. "Portable" means that it'll produce identical result |
|||
on all platforms (for example, on Windows and Linux system printf %e |
|||
formats the exponent differently, on different systems %p either |
|||
prints leading 0x or not, %s may accept null pointer or crash on |
|||
it). "Limited" means that it does not support all the C89 features. |
|||
But it supports few extensions, not in any standard. |
|||
|
|||
my_vsnprintf(to, n, fmt, ap) |
|||
|
|||
@param[out] to A buffer to store the result in |
|||
@param[in] n Store up to n-1 characters, followed by an end 0 |
|||
@param[in] fmt printf-like format string |
|||
@param[in] ap Arguments |
|||
|
|||
@return a number of bytes written to a buffer *excluding* terminating '\0' |
|||
|
|||
@post |
|||
The syntax of a format string is generally the same: |
|||
% <flag> <width> <precision> <length modifier> <format> |
|||
where everithing but the format is optional. |
|||
|
|||
Three one-character flags are regognized: |
|||
'0' has the standard zero-padding semantics; |
|||
'-' is parsed, but silently ignored; |
|||
'`' (backtick) is only supported for strings (%s) and means that the |
|||
string will be quoted according to MySQL identifier quoting rules. |
|||
|
|||
Both <width> and <precision> can be specified as numbers or '*'. |
|||
|
|||
<length modifier> can be 'l', 'll', or 'z'. |
|||
|
|||
Supported formats are 's' (null pointer is accepted, printed as |
|||
"(null)"), 'b' (extension, see below), 'c', 'd', 'u', 'x', |
|||
'X', 'p' (works as 0x%x). |
|||
|
|||
Standard syntax for positional arguments $n is supported. |
|||
|
|||
Extensions: |
|||
|
|||
Flag '`' (backtick): see above. |
|||
|
|||
Format 'b': binary buffer, prints exactly <precision> bytes from the |
|||
argument, without stopping at '\0'. |
|||
*/ |
|||
|
|||
#ifdef __cplusplus |
|||
extern "C" { |
|||
#endif |
|||
|
|||
#include <stdarg.h> |
|||
#include <stdlib.h> |
|||
extern struct my_snprintf_service_st { |
|||
size_t (*my_snprintf_type)(char*, size_t, const char*, ...); |
|||
size_t (*my_vsnprintf_type)(char *, size_t, const char*, va_list); |
|||
} *my_snprintf_service; |
|||
|
|||
#ifdef MYSQL_DYNAMIC_PLUGIN |
|||
|
|||
#define my_vsnprintf my_snprintf_service->my_vsnprintf_type |
|||
#define my_snprintf my_snprintf_service->my_snprintf_type |
|||
|
|||
#else |
|||
|
|||
size_t my_snprintf(char* to, size_t n, const char* fmt, ...); |
|||
size_t my_vsnprintf(char *to, size_t n, const char* fmt, va_list ap); |
|||
|
|||
#endif |
|||
|
|||
#ifdef __cplusplus |
|||
} |
|||
#endif |
|||
|
|||
#define MYSQL_SERVICE_MY_SNPRINTF_INCLUDED |
|||
#endif |
|||
|
|||
@ -0,0 +1,128 @@ |
|||
#ifndef MYSQL_SERVICE_THD_ALLOC_INCLUDED |
|||
/* Copyright (C) 2009 Sun Microsystems, Inc. |
|||
|
|||
This program 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; version 2 of the License. |
|||
|
|||
This program 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 */ |
|||
|
|||
/** |
|||
@file |
|||
This service provdes functions to allocate memory in a connection local |
|||
memory pool. The memory allocated there will be automatically freed at the |
|||
end of the statement, don't use it for allocations that should live longer |
|||
than that. For short living allocations this is more efficient than |
|||
using my_malloc and friends, and automatic "garbage collection" allows not |
|||
to think about memory leaks. |
|||
|
|||
The pool is best for small to medium objects, don't use it for large |
|||
allocations - they are better served with my_malloc. |
|||
*/ |
|||
|
|||
#include <stdlib.h> |
|||
|
|||
#ifdef __cplusplus |
|||
extern "C" { |
|||
#endif |
|||
|
|||
struct st_mysql_lex_string |
|||
{ |
|||
char *str; |
|||
size_t length; |
|||
}; |
|||
typedef struct st_mysql_lex_string MYSQL_LEX_STRING; |
|||
|
|||
extern struct thd_alloc_service_st { |
|||
void *(*thd_alloc_func)(MYSQL_THD, unsigned int); |
|||
void *(*thd_calloc_func)(MYSQL_THD, unsigned int); |
|||
char *(*thd_strdup_func)(MYSQL_THD, const char *); |
|||
char *(*thd_strmake_func)(MYSQL_THD, const char *, unsigned int); |
|||
void *(*thd_memdup_func)(MYSQL_THD, const void*, unsigned int); |
|||
MYSQL_LEX_STRING *(*thd_make_lex_string_func)(MYSQL_THD, MYSQL_LEX_STRING *, |
|||
const char *, unsigned int, int); |
|||
} *thd_alloc_service; |
|||
|
|||
#ifdef MYSQL_DYNAMIC_PLUGIN |
|||
|
|||
#define thd_alloc(thd,size) (thd_alloc_service->thd_alloc_func((thd), (size))) |
|||
|
|||
#define thd_calloc(thd,size) (thd_alloc_service->thd_calloc_func((thd), (size))) |
|||
|
|||
#define thd_strdup(thd,str) (thd_alloc_service->thd_strdup_func((thd), (str))) |
|||
|
|||
#define thd_strmake(thd,str,size) \ |
|||
(thd_alloc_service->thd_strmake_func((thd), (str), (size))) |
|||
|
|||
#define thd_memdup(thd,str,size) \ |
|||
(thd_alloc_service->thd_memdup_func((thd), (str), (size))) |
|||
|
|||
#define thd_make_lex_string(thd, lex_str, str, size, allocate_lex_string) \ |
|||
(thd_alloc_service->thd_make_lex_string_func((thd), (lex_str), (str), \ |
|||
(size), (allocate_lex_string))) |
|||
|
|||
#else |
|||
|
|||
/** |
|||
Allocate memory in the connection's local memory pool |
|||
|
|||
@details |
|||
When properly used in place of @c my_malloc(), this can significantly |
|||
improve concurrency. Don't use this or related functions to allocate |
|||
large chunks of memory. Use for temporary storage only. The memory |
|||
will be freed automatically at the end of the statement; no explicit |
|||
code is required to prevent memory leaks. |
|||
|
|||
@see alloc_root() |
|||
*/ |
|||
void *thd_alloc(MYSQL_THD thd, unsigned int size); |
|||
/** |
|||
@see thd_alloc() |
|||
*/ |
|||
void *thd_calloc(MYSQL_THD thd, unsigned int size); |
|||
/** |
|||
@see thd_alloc() |
|||
*/ |
|||
char *thd_strdup(MYSQL_THD thd, const char *str); |
|||
/** |
|||
@see thd_alloc() |
|||
*/ |
|||
char *thd_strmake(MYSQL_THD thd, const char *str, unsigned int size); |
|||
/** |
|||
@see thd_alloc() |
|||
*/ |
|||
void *thd_memdup(MYSQL_THD thd, const void* str, unsigned int size); |
|||
|
|||
/** |
|||
Create a LEX_STRING in this connection's local memory pool |
|||
|
|||
@param thd user thread connection handle |
|||
@param lex_str pointer to LEX_STRING object to be initialized |
|||
@param str initializer to be copied into lex_str |
|||
@param size length of str, in bytes |
|||
@param allocate_lex_string flag: if TRUE, allocate new LEX_STRING object, |
|||
instead of using lex_str value |
|||
@return NULL on failure, or pointer to the LEX_STRING object |
|||
|
|||
@see thd_alloc() |
|||
*/ |
|||
MYSQL_LEX_STRING *thd_make_lex_string(MYSQL_THD thd, MYSQL_LEX_STRING *lex_str, |
|||
const char *str, unsigned int size, |
|||
int allocate_lex_string); |
|||
|
|||
#endif |
|||
|
|||
#ifdef __cplusplus |
|||
} |
|||
#endif |
|||
|
|||
#define MYSQL_SERVICE_THD_ALLOC_INCLUDED |
|||
#endif |
|||
|
|||
@ -0,0 +1,30 @@ |
|||
#ifndef MYSQL_SERVICES_INCLUDED |
|||
/* Copyright (C) 2009 Sun Microsystems, Inc. |
|||
|
|||
This program 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; version 2 of the License. |
|||
|
|||
This program 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 */ |
|||
|
|||
#ifdef __cplusplus |
|||
extern "C" { |
|||
#endif |
|||
|
|||
#include <mysql/service_my_snprintf.h> |
|||
#include <mysql/service_thd_alloc.h> |
|||
|
|||
#ifdef __cplusplus |
|||
} |
|||
#endif |
|||
|
|||
#define MYSQL_SERVICES_INCLUDED |
|||
#endif |
|||
|
|||
@ -0,0 +1,24 @@ |
|||
/* Copyright (C) 2009 Sun Microsystems, Inc. |
|||
|
|||
This program 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; version 2 of the License. |
|||
|
|||
This program 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 */ |
|||
|
|||
#ifdef _WIN32 |
|||
#define SERVICE_VERSION __declspec(dllexport) void * |
|||
#else |
|||
#define SERVICE_VERSION void * |
|||
#endif |
|||
|
|||
#define VERSION_my_snprintf 0x0100 |
|||
#define VERSION_thd_alloc 0x0100 |
|||
|
|||
@ -0,0 +1,20 @@ |
|||
# Copyright (C) 2006 MySQL AB |
|||
# |
|||
# This program 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; version 2 of the License. |
|||
# |
|||
# This program 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
|||
|
|||
INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/include) |
|||
|
|||
SET(MYSQLSERVICES_SOURCES my_snprintf_service.c thd_alloc_service.c) |
|||
|
|||
ADD_LIBRARY(mysqlservices ${MYSQLSERVICES_SOURCES}) |
|||
@ -0,0 +1,100 @@ |
|||
How to create a new service |
|||
^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
|||
|
|||
A "service" is a set of C functions in a structure that a |
|||
service dynamic linker uses when a dynamic plugin is loaded. |
|||
|
|||
If you want to export C++ class you need to provide an |
|||
extern "C" function that will create a new instance of your class, |
|||
and put it in a service. |
|||
|
|||
Data structures are not part of the service structure, but they are part |
|||
of the API you create and usually need to be declared in the same |
|||
service_*.h file. |
|||
|
|||
To turn a set of functions (foo_func1, foo_func2) |
|||
into a service "foo" you need to |
|||
|
|||
1. create a new file include/mysql/service_foo.h |
|||
|
|||
2. the template is |
|||
================================================================== |
|||
#ifndef MYSQL_SERVICE_FOO_INCLUDED |
|||
/* standard GPL header */ |
|||
|
|||
/** |
|||
@file |
|||
*exhaustive* description of the interface you provide. |
|||
This file is the main user documentation of the new service |
|||
*/ |
|||
#ifdef __cplusplus |
|||
extern "C" { |
|||
#endif |
|||
|
|||
extern struct foo_service_st { |
|||
int (*foo_func1_type)(...); /* fix the prototype as appropriate */ |
|||
void (*foo_func2_type)(...); /* fix the prototype as appropriate */ |
|||
} *foo_service; |
|||
|
|||
#ifdef MYSQL_DYNAMIC_PLUGIN |
|||
|
|||
#define foo_func1(...) foo_service->foo_func1_type(...) |
|||
#define foo_func2(...) foo_service->foo_func2_type(...) |
|||
|
|||
#else |
|||
|
|||
int foo_func1_type(...); /* fix the prototype as appropriate */ |
|||
void foo_func2_type(...); /* fix the prototype as appropriate */ |
|||
|
|||
#endif |
|||
|
|||
#ifdef __cplusplus |
|||
} |
|||
#endif |
|||
|
|||
#define MYSQL_SERVICE_FOO_INCLUDED |
|||
#endif |
|||
================================================================== |
|||
|
|||
the service_foo.h file should be self-contained, if it needs system headers - |
|||
include them in it, e.g. if you use size_t - #include <stdlib.h> |
|||
|
|||
it should also declare all the accompanying data structures, as necessary |
|||
(e.g. thd_alloc_service declares MYSQL_LEX_STRING). |
|||
|
|||
3. add the new file to include/Makefile.am (pkginclude_HEADERS) |
|||
4. add the new file to include/mysql/services.h |
|||
5. increase the minor plugin ABI version in include/mysql/plugin.h |
|||
(MYSQL_PLUGIN_INTERFACE_VERSION = MYSQL_PLUGIN_INTERFACE_VERSION+1) |
|||
6. add the version of your service to include/service_versions.h: |
|||
================================================================== |
|||
#define VERSION_foo 0x0100 |
|||
================================================================== |
|||
|
|||
7. create a new file libservices/foo_service.h using the following template: |
|||
================================================================== |
|||
/* GPL header */ |
|||
#include <service_versions.h> |
|||
SERVICE_VERSION *foo_service= (void*)VERSION_foo; |
|||
================================================================== |
|||
|
|||
8. add the new file to libservices/CMakeLists.txt (MYSQLSERVICES_SOURCES) |
|||
9. add the new file to libservices/Makefile.am (libmysqlservices_a_SOURCES) |
|||
10. and finally, register your service for dynamic linking in |
|||
sql/sql_plugin_services.h |
|||
10.1 fill in the service structure: |
|||
================================================================== |
|||
static struct foo_service_st foo_handler = { |
|||
foo_func1, |
|||
foo_func2 |
|||
} |
|||
================================================================== |
|||
|
|||
10.2 and add it to the list of services |
|||
|
|||
================================================================== |
|||
{ "foo_service", VERSION_foo, &foo_handler } |
|||
================================================================== |
|||
|
|||
that's all. |
|||
|
|||
@ -0,0 +1,19 @@ |
|||
# Copyright 2009 Sun Microsystems, Inc.
|
|||
#
|
|||
# This program 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; version 2 of the License.
|
|||
#
|
|||
# This program 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
|
|||
|
|||
AM_CPPFLAGS = -I$(top_srcdir)/include |
|||
pkglib_LIBRARIES = libmysqlservices.a |
|||
libmysqlservices_a_SOURCES = my_snprintf_service.c thd_alloc_service.c |
|||
EXTRA_DIST = CMakeLists.txt |
|||
@ -0,0 +1,17 @@ |
|||
/* Copyright (C) 2009 Sun Microsystems, Inc. |
|||
|
|||
This program 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; version 2 of the License. |
|||
|
|||
This program 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 <service_versions.h> |
|||
SERVICE_VERSION my_snprintf_service= (void*)VERSION_my_snprintf; |
|||
@ -0,0 +1,17 @@ |
|||
/* Copyright (C) 2009 Sun Microsystems, Inc. |
|||
|
|||
This program 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; version 2 of the License. |
|||
|
|||
This program 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 <service_versions.h> |
|||
SERVICE_VERSION *thd_alloc_service= (void*)VERSION_thd_alloc; |
|||
@ -0,0 +1,44 @@ |
|||
/* Copyright (C) 2009 Sun Microsystems, Inc. |
|||
|
|||
This program 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; version 2 of the License. |
|||
|
|||
This program 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 */ |
|||
|
|||
/* support for Services */ |
|||
#include <service_versions.h> |
|||
|
|||
struct st_service_ref { |
|||
const char *name; |
|||
uint version; |
|||
void *service; |
|||
}; |
|||
|
|||
static struct my_snprintf_service_st my_snprintf_handler = { |
|||
my_snprintf, |
|||
my_vsnprintf |
|||
}; |
|||
|
|||
static struct thd_alloc_service_st thd_alloc_handler= { |
|||
thd_alloc, |
|||
thd_calloc, |
|||
thd_strdup, |
|||
thd_strmake, |
|||
thd_memdup, |
|||
thd_make_lex_string |
|||
}; |
|||
|
|||
static struct st_service_ref list_of_services[]= |
|||
{ |
|||
{ "my_snprintf_service", VERSION_my_snprintf, &my_snprintf_handler }, |
|||
{ "thd_alloc_service", VERSION_thd_alloc, &thd_alloc_handler } |
|||
}; |
|||
|
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue