prevent running qpl code on unknown platforms

This commit is contained in:
hnwyllmm
2023-12-28 10:13:18 +00:00
committed by ob-robot
parent 2882851e54
commit 9ebc672082
2 changed files with 70 additions and 13 deletions

View File

@ -11,6 +11,9 @@
*/ */
#include "zlib_lite_adaptor.h" #include "zlib_lite_adaptor.h"
#include <cpuid.h>
#include "codec_deflate_qpl.h" #include "codec_deflate_qpl.h"
#include "zlib_lite_src/deflate.h" #include "zlib_lite_src/deflate.h"
#include "zlib_lite_src/zlib.h" #include "zlib_lite_src/zlib.h"
@ -21,6 +24,47 @@ namespace common
{ {
namespace ZLIB_LITE namespace ZLIB_LITE
{ {
static bool check_support_qpl(void)
{
bool bret = false;
unsigned int eax, ebx, ecx, edx;
//LEVEL=0: Highest Function Parameter and Manufacturer ID
__cpuid(0, eax, ebx, ecx, edx);
//Determine whether it is an Intel manufacturer.
if (ebx == 0x756e6547 && edx == 0x49656e69 && ecx == 0x6c65746e) {
//LEVEL=1: Processor Info and Feature Bit
__cpuid(1, eax, ebx, ecx, edx);
unsigned int family, model;
/*
If the Family ID field is equal to 15, the family is equal to the sum of
the Extended Family ID and the Family ID fields.
Otherwise, the family is equal to the value of the Family ID field.
*/
family = (eax >> 8) & 0xF;
if (family == 0xF) {
family += (eax >> 20) & 0xF;
}
/*
If the Family ID field is either 6 or 15, the model is equal to the sum of
the Extended Model ID field shifted left by 4 bits and the Model field,
Otherwise, the model is equal to the value of the Model field.
*/
model = (eax >> 4) & 0xF;
if (family >= 0x6) {
model += ((eax >> 16) & 0xF) << 4;
}
// if Model less then 0X8F or family not equal 6, QPL compress and
// decompress is not supported, will use zlib instead.
if (family == 6 && model >= 0X8F) {
bret = true;
}
}
return bret;
}
/*zlib_lite supports two algorithms. On the platform that supports qpl, the qpl compression algorithm will be used, /*zlib_lite supports two algorithms. On the platform that supports qpl, the qpl compression algorithm will be used,
otherwise the zlib algorithm will be used.*/ otherwise the zlib algorithm will be used.*/
@ -37,12 +81,15 @@ int ObZlibLiteAdaptor::init(allocator alloc, deallocator dealloc, int32_t io_thr
int ret = 0; int ret = 0;
#ifdef ENABLE_QPL_COMPRESSION #ifdef ENABLE_QPL_COMPRESSION
QplAllocator allocator; qpl_support_ = check_support_qpl();
allocator.allocate = alloc; if (qpl_support_) {
allocator.deallocate = dealloc; QplAllocator allocator;
int qpl_ret = qpl_init(allocator, io_thread_count); allocator.allocate = alloc;
if (0 != qpl_ret) { allocator.deallocate = dealloc;
ret = -1; int qpl_ret = qpl_init(allocator, io_thread_count);
if (0 != qpl_ret) {
ret = -1;
}
} }
#endif #endif
@ -52,7 +99,9 @@ int ObZlibLiteAdaptor::init(allocator alloc, deallocator dealloc, int32_t io_thr
void ObZlibLiteAdaptor::deinit() void ObZlibLiteAdaptor::deinit()
{ {
#ifdef ENABLE_QPL_COMPRESSION #ifdef ENABLE_QPL_COMPRESSION
qpl_deinit(); if (qpl_support_) {
qpl_deinit();
}
#endif #endif
} }
@ -129,8 +178,10 @@ int64_t ObZlibLiteAdaptor::compress(const char* src_buffer,
{ {
#ifdef ENABLE_QPL_COMPRESSION #ifdef ENABLE_QPL_COMPRESSION
return qpl_compress(src_buffer, dst_buffer, static_cast<int>(src_data_size), if (qpl_support_) {
static_cast<int>(dst_buffer_size)); return qpl_compress(src_buffer, dst_buffer, static_cast<int>(src_data_size),
static_cast<int>(dst_buffer_size));
}
#endif // ENABLE_QPL_COMPRESSION #endif // ENABLE_QPL_COMPRESSION
@ -152,9 +203,11 @@ int64_t ObZlibLiteAdaptor::decompress(const char* src_buffer,
{ {
#ifdef ENABLE_QPL_COMPRESSION #ifdef ENABLE_QPL_COMPRESSION
return qpl_decompress(src_buffer, dst_buffer, if (qpl_support_) {
static_cast<int>(src_data_size), return qpl_decompress(src_buffer, dst_buffer,
static_cast<int>(dst_buffer_size)); static_cast<int>(src_data_size),
static_cast<int>(dst_buffer_size));
}
#endif #endif
int64_t decompress_ret_size = dst_buffer_size; int64_t decompress_ret_size = dst_buffer_size;
@ -170,7 +223,9 @@ int64_t ObZlibLiteAdaptor::decompress(const char* src_buffer,
const char *ObZlibLiteAdaptor::compression_method() const const char *ObZlibLiteAdaptor::compression_method() const
{ {
#ifdef ENABLE_QPL_COMPRESSION #ifdef ENABLE_QPL_COMPRESSION
return qpl_hardware_enabled() ? "qpl_hardware" : "qpl_software"; if (qpl_support_) {
return qpl_hardware_enabled() ? "qpl_hardware" : "qpl_software";
}
#endif #endif
return "zlib_native"; return "zlib_native";

View File

@ -54,6 +54,8 @@ private:
int zlib_decompress(char *dest, int64_t *dest_len, const char *source, int64_t source_len); int zlib_decompress(char *dest, int64_t *dest_len, const char *source, int64_t source_len);
private: private:
bool qpl_support_ = false;
//zlib compress level,default is 1. //zlib compress level,default is 1.
static constexpr int compress_level = 1; static constexpr int compress_level = 1;