Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 51 additions & 42 deletions include/my_bit.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,17 @@
#ifndef MY_BIT_INCLUDED
#define MY_BIT_INCLUDED

#ifdef __cplusplus
extern "C" {
#else
#define constexpr
#endif

/*
Some useful bit functions
*/

C_MODE_START

extern const uchar _my_bits_reverse_table[256];
extern const unsigned char _my_bits_reverse_table[256];


/*
Expand All @@ -43,42 +47,42 @@ extern const uchar _my_bits_reverse_table[256];
Let's return 0 for the input 0, for the code simplicity.
See the 000x branch. It covers both (1<<0) and 0.
*/
static inline CONSTEXPR uint my_bit_log2_hex_digit(uint8 value)
static inline constexpr unsigned int my_bit_log2_hex_digit(unsigned char value)
{
return value & 0x0C ? /*1100*/ (value & 0x08 ? /*1000*/ 3 : /*0100*/ 2) :
/*0010*/ (value & 0x02 ? /*0010*/ 1 : /*000x*/ 0);
}
static inline CONSTEXPR uint my_bit_log2_uint8(uint8 value)
static inline constexpr unsigned int my_bit_log2_uint8(unsigned char value)
{
return value & 0xF0 ? my_bit_log2_hex_digit((uint8) (value >> 4)) + 4:
return value & 0xF0 ? my_bit_log2_hex_digit((unsigned char) (value >> 4)) + 4:
my_bit_log2_hex_digit(value);
}
static inline CONSTEXPR uint my_bit_log2_uint16(uint16 value)
static inline constexpr unsigned int my_bit_log2_uint16(unsigned short value)
{
return value & 0xFF00 ? my_bit_log2_uint8((uint8) (value >> 8)) + 8 :
my_bit_log2_uint8((uint8) value);
return value & 0xFF00 ? my_bit_log2_uint8((unsigned char) (value >> 8)) + 8 :
my_bit_log2_uint8((unsigned char) value);
}
static inline CONSTEXPR uint my_bit_log2_uint32(uint32 value)
static inline constexpr unsigned int my_bit_log2_uint32(unsigned int value)
{
return value & 0xFFFF0000UL ?
my_bit_log2_uint16((uint16) (value >> 16)) + 16 :
my_bit_log2_uint16((uint16) value);
my_bit_log2_uint16((unsigned short) (value >> 16)) + 16 :
my_bit_log2_uint16((unsigned short) value);
}
static inline CONSTEXPR uint my_bit_log2_uint64(ulonglong value)
static inline constexpr unsigned int my_bit_log2_uint64(unsigned long long int value)
{
return value & 0xFFFFFFFF00000000ULL ?
my_bit_log2_uint32((uint32) (value >> 32)) + 32 :
my_bit_log2_uint32((uint32) value);
my_bit_log2_uint32((unsigned int) (value >> 32)) + 32 :
my_bit_log2_uint32((unsigned int) value);
}
static inline CONSTEXPR uint my_bit_log2_size_t(size_t value)
static inline constexpr unsigned int my_bit_log2_size_t(size_t value)
{
#ifdef __cplusplus
static_assert(sizeof(size_t) <= sizeof(ulonglong),
"size_t <= ulonglong is an assumption that needs to be fixed "
static_assert(sizeof(size_t) <= sizeof(unsigned long long int),
"size_t <= unsigned long long int is an assumption that needs to be fixed "
"for this architecture. Please create an issue on "
"https://jira.mariadb.org");
#endif
return my_bit_log2_uint64((ulonglong) value);
return my_bit_log2_uint64((unsigned long long int) value);
}


Expand All @@ -91,17 +95,17 @@ Count bits in 32bit integer

(Original code public domain).
*/
static inline uint my_count_bits_uint32(uint32 v)
static inline unsigned int my_count_bits_uint32(unsigned int v)
Comment thread
FooBarrior marked this conversation as resolved.
{
v = v - ((v >> 1) & 0x55555555);
v = (v & 0x33333333) + ((v >> 2) & 0x33333333);
return (((v + (v >> 4)) & 0xF0F0F0F) * 0x1010101) >> 24;
}


static inline uint my_count_bits(ulonglong x)
static inline unsigned int my_count_bits(unsigned long long int x)
Comment thread
FooBarrior marked this conversation as resolved.
{
return my_count_bits_uint32((uint32)x) + my_count_bits_uint32((uint32)(x >> 32));
return my_count_bits_uint32((unsigned int)x) + my_count_bits_uint32((unsigned int)(x >> 32));
}


Expand All @@ -126,7 +130,7 @@ static inline uint my_count_bits(ulonglong x)
Comments shows how this works with 01100000000000000000000000001011
*/

static inline uint32 my_round_up_to_next_power(uint32 v)
static inline unsigned int my_round_up_to_next_power(unsigned int v)
{
v--; /* 01100000000000000000000000001010 */
v|= v >> 1; /* 01110000000000000000000000001111 */
Expand All @@ -137,9 +141,9 @@ static inline uint32 my_round_up_to_next_power(uint32 v)
return v+1; /* 10000000000000000000000000000000 */
}

static inline uint32 my_clear_highest_bit(uint32 v)
static inline unsigned int my_clear_highest_bit(unsigned int v)
{
uint32 w=v >> 1;
unsigned int w=v >> 1;
w|= w >> 1;
w|= w >> 2;
w|= w >> 4;
Expand All @@ -148,34 +152,34 @@ static inline uint32 my_clear_highest_bit(uint32 v)
return v & w;
}

static inline uint32 my_reverse_bits(uint32 key)
static inline unsigned int my_reverse_bits(unsigned int key)
{
return
((uint32)_my_bits_reverse_table[ key & 255] << 24) |
((uint32)_my_bits_reverse_table[(key>> 8) & 255] << 16) |
((uint32)_my_bits_reverse_table[(key>>16) & 255] << 8) |
(uint32)_my_bits_reverse_table[(key>>24) ];
((unsigned int)_my_bits_reverse_table[ key & 255] << 24) |
((unsigned int)_my_bits_reverse_table[(key>> 8) & 255] << 16) |
((unsigned int)_my_bits_reverse_table[(key>>16) & 255] << 8) |
(unsigned int)_my_bits_reverse_table[(key>>24) ];
}

/*
a number with the n lowest bits set
an overflow-safe version of (1 << n) - 1
*/
static inline uint64 my_set_bits(int n)
static inline unsigned long long int my_set_bits(int n)
{
return (((1ULL << (n - 1)) - 1) << 1) | 1;
}

/* Create a mask of the significant bits for the last byte (1,3,7,..255) */
static inline uchar last_byte_mask(uint bits)
static inline unsigned char last_byte_mask(unsigned int bits)
{
/* Get the number of used bits-1 (0..7) in the last byte */
unsigned int const used = (bits - 1U) & 7U;
/* Return bitmask for the significant bits */
return (uchar) ((2U << used) - 1);
return (unsigned char) ((2U << used) - 1);
}

static inline uint my_bits_in_bytes(uint n)
static inline unsigned int my_bits_in_bytes(unsigned int n)
{
return ((n + 7) / 8);
}
Expand All @@ -188,7 +192,7 @@ static inline uint my_bits_in_bytes(uint n)
Find the position of the first(least significant) bit set in
the argument. Returns 64 if the argument was 0.
*/
static inline uint my_find_first_bit(ulonglong n)
static inline unsigned int my_find_first_bit(unsigned long long int n)
{
if(!n)
return 64;
Expand All @@ -197,9 +201,9 @@ static inline uint my_find_first_bit(ulonglong n)
#elif defined(_MSC_VER)
#if defined(_M_IX86)
unsigned long bit;
if( _BitScanForward(&bit, (uint)n))
if( _BitScanForward(&bit, (unsigned int)n))
return bit;
_BitScanForward(&bit, (uint)(n>>32));
_BitScanForward(&bit, (unsigned int)(n>>32));
return bit + 32;
#else
unsigned long bit;
Expand All @@ -208,18 +212,23 @@ static inline uint my_find_first_bit(ulonglong n)
#endif
#else
/* Generic case */
uint shift= 0;
static const uchar last_bit[16] = { 32, 0, 1, 0,
unsigned int shift= 0;
static const unsigned char last_bit[16] = { 32, 0, 1, 0,
2, 0, 1, 0,
3, 0, 1, 0,
2, 0, 1, 0};
uint bit;
unsigned int bit;
while ((bit = last_bit[(n >> shift) & 0xF]) == 32)
shift+= 4;
return shift+bit;
#endif
}
C_MODE_END

#ifdef __cplusplus
}
#else
#undef constexpr
#endif

/*
The helper function my_nlz(x) calculates the number of leading zeros
Expand Down Expand Up @@ -289,4 +298,4 @@ inline unsigned int my_nlz (unsigned long long x)
}
#endif

#endif /* MY_BIT_INCLUDED */
#endif /* MY_BIT_INCLUDED */
2 changes: 1 addition & 1 deletion include/mysql/plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ typedef struct st_mysql_xid MYSQL_XID;
#define MYSQL_PLUGIN_INTERFACE_VERSION 0x0105

/** MariaDB plugin interface version */
#define MARIA_PLUGIN_INTERFACE_VERSION 0x010f
#define MARIA_PLUGIN_INTERFACE_VERSION 0x0110

/*
The allowable types of plugins
Expand Down
6 changes: 6 additions & 0 deletions include/mysql/plugin_audit.h.pp
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,12 @@
void my_sha512_result(void *context, unsigned char *digest);
}
extern "C" {
extern struct thd_service_st {
THD* (*get_current_thd)(void);
} *thd_service;
THD* get_current_thd();
}
extern "C" {
struct st_mysql_const_lex_string
{
const char *str;
Expand Down
6 changes: 6 additions & 0 deletions include/mysql/plugin_auth.h.pp
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,12 @@
void my_sha512_result(void *context, unsigned char *digest);
}
extern "C" {
extern struct thd_service_st {
THD* (*get_current_thd)(void);
} *thd_service;
THD* get_current_thd();
}
extern "C" {
struct st_mysql_const_lex_string
{
const char *str;
Expand Down
6 changes: 6 additions & 0 deletions include/mysql/plugin_data_type.h.pp
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,12 @@
void my_sha512_result(void *context, unsigned char *digest);
}
extern "C" {
extern struct thd_service_st {
THD* (*get_current_thd)(void);
} *thd_service;
THD* get_current_thd();
}
extern "C" {
struct st_mysql_const_lex_string
{
const char *str;
Expand Down
6 changes: 6 additions & 0 deletions include/mysql/plugin_encryption.h.pp
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,12 @@
void my_sha512_result(void *context, unsigned char *digest);
}
extern "C" {
extern struct thd_service_st {
THD* (*get_current_thd)(void);
} *thd_service;
THD* get_current_thd();
}
extern "C" {
struct st_mysql_const_lex_string
{
const char *str;
Expand Down
6 changes: 6 additions & 0 deletions include/mysql/plugin_ftparser.h.pp
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,12 @@
void my_sha512_result(void *context, unsigned char *digest);
}
extern "C" {
extern struct thd_service_st {
THD* (*get_current_thd)(void);
} *thd_service;
THD* get_current_thd();
}
extern "C" {
struct st_mysql_const_lex_string
{
const char *str;
Expand Down
6 changes: 6 additions & 0 deletions include/mysql/plugin_function.h.pp
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,12 @@
void my_sha512_result(void *context, unsigned char *digest);
}
extern "C" {
extern struct thd_service_st {
THD* (*get_current_thd)(void);
} *thd_service;
THD* get_current_thd();
}
extern "C" {
struct st_mysql_const_lex_string
{
const char *str;
Expand Down
6 changes: 6 additions & 0 deletions include/mysql/plugin_password_validation.h.pp
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,12 @@
void my_sha512_result(void *context, unsigned char *digest);
}
extern "C" {
extern struct thd_service_st {
THD* (*get_current_thd)(void);
} *thd_service;
THD* get_current_thd();
}
extern "C" {
struct st_mysql_const_lex_string
{
const char *str;
Expand Down
46 changes: 46 additions & 0 deletions include/mysql/service_thd.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#ifndef MYSQL_SERVICE_THD_INCLUDED
/* Copyright (c) 2026, MariaDB Corporation.

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-1335 USA */

/**
@file include/mysql/service_thd.h
This service provides functions for plugins and storage engines to access
current thd.
*/

#ifdef __cplusplus
extern "C" {
#endif

extern struct thd_service_st {
MYSQL_THD (*get_current_thd)(void);
} *thd_service;

#ifdef MYSQL_DYNAMIC_PLUGIN
# define get_current_thd() thd_service->get_current_thd()
#else
/**
current thd accessor
@return pointer to current thd
*/
MYSQL_THD get_current_thd();
#endif

#ifdef __cplusplus
}
#endif

#define MYSQL_SERVICE_THD_INCLUDED
#endif
1 change: 1 addition & 0 deletions include/mysql/services.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ extern "C" {
#include <mysql/service_progress_report.h>
#include <mysql/service_sha1.h>
#include <mysql/service_sha2.h>
#include <mysql/service_thd.h>
#include <mysql/service_thd_alloc.h>
#include <mysql/service_thd_autoinc.h>
#include <mysql/service_thd_error_context.h>
Expand Down
1 change: 1 addition & 0 deletions include/service_versions.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#define VERSION_thd_specifics 0x0100
#define VERSION_thd_timezone 0x0100
#define VERSION_thd_wait 0x0100
#define VERSION_thd 0x0100
#define VERSION_wsrep 0x0500
#define VERSION_json 0x0100
#define VERSION_sql_service 0x0102
Expand Down
1 change: 1 addition & 0 deletions libservices/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ SET(MYSQLSERVICES_SOURCES
thd_error_context_service.c
thd_rnd_service.c
thd_specifics_service.c
thd_service.c
thd_timezone_service.c
thd_wait_service.c
wsrep_service.c
Expand Down
Loading