Start migrating to new driver framework

pull/27/head
Guo Hui 2018-10-24 16:20:14 +08:00
parent ef74f714f0
commit 4d22fb9aa1
26 changed files with 5675 additions and 654 deletions

View File

@ -13,7 +13,7 @@ cmake_minimum_required(VERSION 3.0)
include(./cmake/common.cmake)
project(${PROJ})
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_LIST_DIR}/lib/arch/include ${CMAKE_CURRENT_LIST_DIR}/lib/utils/include)
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_LIST_DIR}/lib/arch/include ${CMAKE_CURRENT_LIST_DIR}/lib/utils/include ${CMAKE_CURRENT_LIST_DIR}/third_party)
# config self use headers
header_directories(${SDK_ROOT}/lib)
# build library first

7
lib/.clang-format Normal file
View File

@ -0,0 +1,7 @@
---
BasedOnStyle: WebKit
BreakBeforeBraces: Allman
ConstructorInitializerAllOnOneLineOrOnePerLine: 'true'
UseTab: Never
PointerAlignment: Right
...

View File

@ -15,6 +15,11 @@
#ifndef _BSP_PLATFORM_H
#define _BSP_PLATFORM_H
#ifdef __INTELLISENSE__
#define __attribute__(x)
#define _HAS_CXX17 1
#endif
/* clang-format off */
/* Register base address */

View File

@ -12,17 +12,406 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <string.h>
#include <stdlib.h>
#include <FreeRTOS.h>
#include <aes.h>
#include <driver.h>
#include <sysctl.h>
#include <hal.h>
#include <kernel/driver_impl.hpp>
#include <stdlib.h>
#include <string.h>
#include <sysctl.h>
#define COMMON_ENTRY \
aes_dev_data *data = (aes_dev_data *)userdata; \
volatile aes_t *aes = (volatile aes_t *)data->base_addr; \
using namespace sys;
#define COMMON_ENTRY \
semaphore_lock locker(free_mutex_);
class k_aes_driver : public aes_driver, public static_object, public free_object_access
{
public:
k_aes_driver(uintptr_t base_addr, sysctl_clock_t clock, sysctl_dma_select_t dma_req)
: aes_(*reinterpret_cast<volatile aes_t *>(base_addr)), clock_(clock), dma_req_(dma_req)
{
}
virtual void install() override
{
free_mutex_ = xSemaphoreCreateMutex();
sysctl_clock_disable(clock_);
}
virtual void on_first_open() override
{
sysctl_clock_enable(clock_);
}
virtual void on_last_close() override
{
sysctl_clock_disable(clock_);
}
virtual void aes_ecb128_hard_decrypt(gsl::span<const uint8_t> input_key, gsl::span<const uint8_t> input_data, gsl::span<uint8_t> output_data) override
{
COMMON_ENTRY;
size_t input_len = input_data.size();
size_t padding_len = ((input_len + 15) / 16) * 16;
os_aes_init(input_key.data(), AES_128, NULL, 0L, NULL, AES_ECB, AES_HARD_DECRYPTION, 0L, input_len);
if (padding_len <= AES_TRANSMISSION_THRESHOLD)
{
os_aes_process(input_data.data(), output_data.data(), padding_len, AES_ECB);
}
else
{
handle_t aes_read = dma_open_free();
dma_set_request_source(aes_read, dma_req_);
SemaphoreHandle_t event_read = xSemaphoreCreateBinary();
aes_.dma_sel = 1;
dma_transmit_async(aes_read, &aes_.aes_out_data, output_data.data(), 0, 1, sizeof(uint32_t), padding_len >> 2, 4, event_read);
aes_input_bytes(input_data.data(), input_len, AES_ECB);
configASSERT(xSemaphoreTake(event_read, portMAX_DELAY) == pdTRUE);
dma_close(aes_read);
vSemaphoreDelete(event_read);
}
}
virtual void aes_ecb128_hard_encrypt(gsl::span<const uint8_t> input_key, gsl::span<const uint8_t> input_data, gsl::span<uint8_t> output_data) override
{
size_t input_len = input_data.size();
size_t padding_len = ((input_len + 15) / 16) * 16;
os_aes_init(input_key.data(), AES_128, NULL, 0L, NULL, AES_ECB, AES_HARD_ENCRYPTION, 0L, input_len);
if (padding_len <= AES_TRANSMISSION_THRESHOLD)
{
os_aes_process(input_data.data(), output_data.data(), input_len, AES_ECB);
}
else
{
handle_t aes_read = dma_open_free();
dma_set_request_source(aes_read, dma_req_);
SemaphoreHandle_t event_read = xSemaphoreCreateBinary();
aes_.dma_sel = 1;
dma_transmit_async(aes_read, &aes_.aes_out_data, output_data.data(), 0, 1, sizeof(uint32_t), padding_len >> 2, 4, event_read);
aes_input_bytes(input_data.data(), input_len, AES_ECB);
configASSERT(xSemaphoreTake(event_read, portMAX_DELAY) == pdTRUE);
dma_close(aes_read);
vSemaphoreDelete(event_read);
}
}
virtual void aes_ecb192_hard_decrypt(gsl::span<const uint8_t> input_key, gsl::span<const uint8_t> input_data, gsl::span<uint8_t> output_data) override
{
size_t input_len = input_data.size();
size_t padding_len = ((input_len + 15) / 16) * 16;
os_aes_init(input_key.data(), AES_192, NULL, 0L, NULL, AES_ECB, AES_HARD_DECRYPTION, 0L, input_len);
if (padding_len <= AES_TRANSMISSION_THRESHOLD)
{
os_aes_process(input_data.data(), output_data.data(), padding_len, AES_ECB);
}
else
{
handle_t aes_read = dma_open_free();
dma_set_request_source(aes_read, dma_req_);
SemaphoreHandle_t event_read = xSemaphoreCreateBinary();
aes_.dma_sel = 1;
dma_transmit_async(aes_read, &aes_.aes_out_data, output_data.data(), 0, 1, sizeof(uint32_t), padding_len >> 2, 4, event_read);
aes_input_bytes(input_data.data(), input_len, AES_ECB);
configASSERT(xSemaphoreTake(event_read, portMAX_DELAY) == pdTRUE);
dma_close(aes_read);
vSemaphoreDelete(event_read);
}
}
virtual void aes_ecb192_hard_encrypt(gsl::span<const uint8_t> input_key, gsl::span<const uint8_t> input_data, gsl::span<uint8_t> output_data) override
{
size_t input_len = input_data.size();
size_t padding_len = ((input_len + 15) / 16) * 16;
os_aes_init(input_key.data(), AES_192, NULL, 0L, NULL, AES_ECB, AES_HARD_ENCRYPTION, 0L, input_len);
if (padding_len <= AES_TRANSMISSION_THRESHOLD)
{
os_aes_process(input_data.data(), output_data.data(), input_len, AES_ECB);
}
else
{
handle_t aes_read = dma_open_free();
dma_set_request_source(aes_read, dma_req_);
SemaphoreHandle_t event_read = xSemaphoreCreateBinary();
aes_.dma_sel = 1;
dma_transmit_async(aes_read, &aes_.aes_out_data, output_data.data(), 0, 1, sizeof(uint32_t), padding_len >> 2, 4, event_read);
aes_input_bytes(input_data.data(), input_len, AES_ECB);
configASSERT(xSemaphoreTake(event_read, portMAX_DELAY) == pdTRUE);
dma_close(aes_read);
vSemaphoreDelete(event_read);
}
}
virtual void aes_ecb256_hard_decrypt(gsl::span<const uint8_t> input_key, gsl::span<const uint8_t> input_data, gsl::span<uint8_t> output_data) override
{
size_t input_len = input_data.size();
size_t padding_len = ((input_len + 15) / 16) * 16;
os_aes_init(input_key.data(), AES_256, NULL, 0L, NULL, AES_ECB, AES_HARD_DECRYPTION, 0L, input_len);
if (padding_len <= AES_TRANSMISSION_THRESHOLD)
{
os_aes_process(input_data.data(), output_data.data(), padding_len, AES_ECB);
}
else
{
handle_t aes_read = dma_open_free();
dma_set_request_source(aes_read, dma_req_);
SemaphoreHandle_t event_read = xSemaphoreCreateBinary();
aes_.dma_sel = 1;
dma_transmit_async(aes_read, &aes_.aes_out_data, output_data.data(), 0, 1, sizeof(uint32_t), padding_len >> 2, 4, event_read);
aes_input_bytes(input_data.data(), input_len, AES_ECB);
configASSERT(xSemaphoreTake(event_read, portMAX_DELAY) == pdTRUE);
dma_close(aes_read);
vSemaphoreDelete(event_read);
}
}
virtual void aes_ecb256_hard_encrypt(gsl::span<const uint8_t> input_key, gsl::span<const uint8_t> input_data, gsl::span<uint8_t> output_data) override
{
}
virtual void aes_cbc128_hard_decrypt(cbc_context_t &context, gsl::span<const uint8_t> input_data, gsl::span<uint8_t> output_data) override
{
}
virtual void aes_cbc128_hard_encrypt(cbc_context_t &context, gsl::span<const uint8_t> input_data, gsl::span<uint8_t> output_data) override
{
}
virtual void aes_cbc192_hard_decrypt(cbc_context_t &context, gsl::span<const uint8_t> input_data, gsl::span<uint8_t> output_data) override
{
}
virtual void aes_cbc192_hard_encrypt(cbc_context_t &context, gsl::span<const uint8_t> input_data, gsl::span<uint8_t> output_data) override
{
}
virtual void aes_cbc256_hard_decrypt(cbc_context_t &context, gsl::span<const uint8_t> input_data, gsl::span<uint8_t> output_data) override
{
}
virtual void aes_cbc256_hard_encrypt(cbc_context_t &context, gsl::span<const uint8_t> input_data, gsl::span<uint8_t> output_data) override
{
}
virtual void aes_gcm128_hard_decrypt(gcm_context_t &context, gsl::span<const uint8_t> input_data, gsl::span<uint8_t> output_data, gsl::span<uint8_t> gcm_tag) override
{
}
private:
void os_aes_process(const uint8_t *input_data,
uint8_t *output_data,
size_t input_data_len,
aes_cipher_mode_t cipher_mode)
{
size_t temp_len = 0;
uint32_t i = 0;
if (input_data_len >= 80)
{
for (i = 0; i < (input_data_len / 80); i++)
aes_process_less_80_bytes(&input_data[i * 80], &output_data[i * 80], 80, cipher_mode);
}
temp_len = input_data_len % 80;
if (temp_len)
aes_process_less_80_bytes(&input_data[i * 80], &output_data[i * 80], temp_len, cipher_mode);
}
void aes_process_less_80_bytes(const uint8_t *input_data,
uint8_t *output_data,
size_t input_data_len,
aes_cipher_mode_t cipher_mode)
{
size_t padding_len, uint32_num, uint8_num, remainder, i;
uint32_t uint32_data;
uint8_t uint8_data[4] = { 0 };
padding_len = ((input_data_len + 15) / 16) * 16;
uint32_num = input_data_len / 4;
for (i = 0; i < uint32_num; i++)
{
uint32_data = *((uint32_t *)(&input_data[i * 4]));
while (!os_aes_get_data_in_flag())
;
os_aes_write_text(uint32_data);
}
uint8_num = 4 * uint32_num;
remainder = input_data_len % 4;
if (remainder)
{
switch (remainder)
{
case 1:
uint8_data[0] = input_data[uint8_num];
break;
case 2:
uint8_data[0] = input_data[uint8_num];
uint8_data[1] = input_data[uint8_num + 1];
break;
case 3:
uint8_data[0] = input_data[uint8_num];
uint8_data[1] = input_data[uint8_num + 1];
uint8_data[2] = input_data[uint8_num + 2];
break;
default:
break;
}
uint32_data = *((uint32_t *)(&uint8_data[0]));
while (!os_aes_get_data_in_flag(userdata))
;
os_aes_write_text(uint32_data, userdata);
}
if ((cipher_mode == AES_ECB) || (cipher_mode == AES_CBC))
{
uint32_num = (padding_len - input_data_len) / 4;
for (i = 0; i < uint32_num; i++)
{
while (!os_aes_get_data_in_flag(userdata))
;
os_aes_write_text(0, userdata);
}
uint32_num = padding_len / 4;
}
for (i = 0; i < uint32_num; i++)
{
while (!os_aes_get_data_out_flag(userdata))
;
*((uint32_t *)(&output_data[i * 4])) = os_aes_read_out_data(userdata);
}
if ((cipher_mode == AES_GCM) && (remainder))
{
while (!os_aes_get_data_out_flag(userdata))
;
*((uint32_t *)(&uint8_data[0])) = os_aes_read_out_data(userdata);
switch (remainder)
{
case 1:
output_data[uint32_num * 4] = uint8_data[0];
break;
case 2:
output_data[uint32_num * 4] = uint8_data[0];
output_data[(i * 4) + 1] = uint8_data[1];
break;
case 3:
output_data[uint32_num * 4] = uint8_data[0];
output_data[(i * 4) + 1] = uint8_data[1];
output_data[(i * 4) + 2] = uint8_data[2];
break;
default:
break;
}
}
}
uint32_t os_aes_get_data_in_flag()
{
return aes_.data_in_flag;
}
void os_aes_write_text(uint32_t text_data)
{
aes_.aes_text_data = text_data;
}
void os_gcm_write_tag(uint32_t *tag)
{
aes_.gcm_in_tag[0] = tag[3];
aes_.gcm_in_tag[1] = tag[2];
aes_.gcm_in_tag[2] = tag[1];
aes_.gcm_in_tag[3] = tag[0];
}
uint32_t os_aes_get_data_in_flag()
{
return aes_.data_in_flag;
}
uint32_t os_aes_get_data_out_flag()
{
return aes_.data_out_flag;
}
uint32_t os_gcm_get_tag_in_flag()
{
return aes_.tag_in_flag;
}
uint32_t os_aes_read_out_data()
{
return aes_.aes_out_data;
}
uint32_t os_gcm_get_tag_chk()
{
return aes_.tag_chk;
}
void os_gcm_clear_chk_tag()
{
aes_.tag_clear = 0;
}
int os_gcm_check_tag(uint32_t *gcm_tag)
{
while (!os_gcm_get_tag_in_flag())
;
os_gcm_write_tag(gcm_tag);
while (!os_gcm_get_tag_chk())
;
if (os_gcm_get_tag_chk() == 0x2)
{
os_gcm_clear_chk_tag();
return 1;
}
else
{
os_gcm_clear_chk_tag();
return 0;
}
}
void os_gcm_get_tag(uint8_t *gcm_tag)
{
COMMON_ENTRY;
uint32_t uint32_tag;
uint8_t i = 0;
uint32_tag = aes_.gcm_out_tag[3];
gcm_tag[i++] = (uint8_t)((uint32_tag >> 24) & 0xff);
gcm_tag[i++] = (uint8_t)((uint32_tag >> 16) & 0xff);
gcm_tag[i++] = (uint8_t)((uint32_tag >> 8) & 0xff);
gcm_tag[i++] = (uint8_t)((uint32_tag)&0xff);
uint32_tag = aes_.gcm_out_tag[2];
gcm_tag[i++] = (uint8_t)((uint32_tag >> 24) & 0xff);
gcm_tag[i++] = (uint8_t)((uint32_tag >> 16) & 0xff);
gcm_tag[i++] = (uint8_t)((uint32_tag >> 8) & 0xff);
gcm_tag[i++] = (uint8_t)((uint32_tag)&0xff);
uint32_tag = aes_.gcm_out_tag[1];
gcm_tag[i++] = (uint8_t)((uint32_tag >> 24) & 0xff);
gcm_tag[i++] = (uint8_t)((uint32_tag >> 16) & 0xff);
gcm_tag[i++] = (uint8_t)((uint32_tag >> 8) & 0xff);
gcm_tag[i++] = (uint8_t)((uint32_tag)&0xff);
uint32_tag = aes_.gcm_out_tag[0];
gcm_tag[i++] = (uint8_t)((uint32_tag >> 24) & 0xff);
gcm_tag[i++] = (uint8_t)((uint32_tag >> 16) & 0xff);
gcm_tag[i++] = (uint8_t)((uint32_tag >> 8) & 0xff);
gcm_tag[i++] = (uint8_t)((uint32_tag)&0xff);
os_gcm_check_tag((uint32_t *)gcm_tag);
}
private:
volatile aes_t &aes_;
sysctl_clock_t clock_;
sysctl_dma_select_t dma_req_;
SemaphoreHandle_t free_mutex_;
};
#define COMMON_ENTRY \
aes_dev_data *data = (aes_dev_data *)userdata; \
volatile aes_t *aes = (volatile aes_t *)data->base_addr; \
(void)aes;
typedef struct
@ -187,7 +576,7 @@ static void os_aes_init(const uint8_t *input_key,
COMMON_ENTRY;
size_t remainder, uint32_num, uint8_num, i;
uint32_t uint32_data;
uint8_t uint8_data[4] = {0};
uint8_t uint8_data[4] = { 0 };
size_t padding_len = input_data_len;
if ((cipher_mode == AES_ECB) || (cipher_mode == AES_CBC))
padding_len = ((input_data_len + 15) / 16) * 16;
@ -256,7 +645,7 @@ static void aes_input_bytes(const uint8_t *input_data, size_t input_data_len, ae
{
size_t padding_len, uint32_num, uint8_num, remainder, i;
uint32_t uint32_data;
uint8_t uint8_data[4] = {0};
uint8_t uint8_data[4] = { 0 };
padding_len = ((input_data_len + 15) / 16) * 16;
uint32_num = input_data_len / 4;
@ -315,7 +704,7 @@ static void aes_process_less_80_bytes(const uint8_t *input_data,
{
size_t padding_len, uint32_num, uint8_num, remainder, i;
uint32_t uint32_data;
uint8_t uint8_data[4] = {0};
uint8_t uint8_data[4] = { 0 };
padding_len = ((input_data_len + 15) / 16) * 16;
uint32_num = input_data_len / 4;
@ -414,143 +803,13 @@ static void os_aes_process(const uint8_t *input_data,
aes_process_less_80_bytes(&input_data[i * 80], &output_data[i * 80], temp_len, cipher_mode, userdata);
}
static void aes_ecb128_hard_decrypt(const uint8_t *input_key, const uint8_t *input_data, size_t input_len, uint8_t *output_data, void *userdata)
{
COMMON_ENTRY;
entry_exclusive(data);
size_t padding_len = ((input_len + 15) / 16) * 16;
os_aes_init(input_key, AES_128, NULL, 0L, NULL, AES_ECB, AES_HARD_DECRYPTION, 0L, input_len, userdata);
if(padding_len <= AES_TRANSMISSION_THRESHOLD)
{
os_aes_process(input_data, output_data, padding_len, AES_ECB, userdata);
}
else
{
handle_t aes_read = dma_open_free();
dma_set_request_source(aes_read, data->dma_req_base);
SemaphoreHandle_t event_read = xSemaphoreCreateBinary();
aes->dma_sel = 1;
dma_transmit_async(aes_read, &aes->aes_out_data, output_data, 0, 1, sizeof(uint32_t), padding_len >> 2, 4, event_read);
aes_input_bytes(input_data, input_len, AES_ECB, userdata);
configASSERT(xSemaphoreTake(event_read, portMAX_DELAY) == pdTRUE);
dma_close(aes_read);
vSemaphoreDelete(event_read);
}
exit_exclusive(data);
}
static void aes_ecb128_hard_encrypt(const uint8_t *input_key, const uint8_t *input_data, size_t input_len, uint8_t *output_data, void *userdata)
{
COMMON_ENTRY;
entry_exclusive(data);
size_t padding_len = ((input_len + 15) / 16) * 16;
os_aes_init(input_key, AES_128, NULL, 0L, NULL, AES_ECB, AES_HARD_ENCRYPTION, 0L, input_len, userdata);
if(padding_len <= AES_TRANSMISSION_THRESHOLD)
{
os_aes_process(input_data, output_data, input_len, AES_ECB, userdata);
}
else
{
handle_t aes_read = dma_open_free();
dma_set_request_source(aes_read, data->dma_req_base);
SemaphoreHandle_t event_read = xSemaphoreCreateBinary();
aes->dma_sel = 1;
dma_transmit_async(aes_read, &aes->aes_out_data, output_data, 0, 1, sizeof(uint32_t), padding_len >> 2, 4, event_read);
aes_input_bytes(input_data, input_len, AES_ECB, userdata);
configASSERT(xSemaphoreTake(event_read, portMAX_DELAY) == pdTRUE);
dma_close(aes_read);
vSemaphoreDelete(event_read);
}
exit_exclusive(data);
}
static void aes_ecb192_hard_decrypt(const uint8_t *input_key, const uint8_t *input_data, size_t input_len, uint8_t *output_data, void *userdata)
{
COMMON_ENTRY;
entry_exclusive(data);
size_t padding_len = ((input_len + 15) / 16) * 16;
os_aes_init(input_key, AES_192, NULL, 0L, NULL, AES_ECB, AES_HARD_DECRYPTION, 0L, input_len, userdata);
if(padding_len <= AES_TRANSMISSION_THRESHOLD)
{
os_aes_process(input_data, output_data, padding_len, AES_ECB, userdata);
}
else
{
handle_t aes_read = dma_open_free();
dma_set_request_source(aes_read, data->dma_req_base);
SemaphoreHandle_t event_read = xSemaphoreCreateBinary();
aes->dma_sel = 1;
dma_transmit_async(aes_read, &aes->aes_out_data, output_data, 0, 1, sizeof(uint32_t), padding_len >> 2, 4, event_read);
aes_input_bytes(input_data, input_len, AES_ECB, userdata);
configASSERT(xSemaphoreTake(event_read, portMAX_DELAY) == pdTRUE);
dma_close(aes_read);
vSemaphoreDelete(event_read);
}
exit_exclusive(data);
}
static void aes_ecb192_hard_encrypt(const uint8_t *input_key, const uint8_t *input_data, size_t input_len, uint8_t *output_data, void *userdata)
{
COMMON_ENTRY;
entry_exclusive(data);
size_t padding_len = ((input_len + 15) / 16) * 16;
os_aes_init(input_key, AES_192, NULL, 0L, NULL, AES_ECB, AES_HARD_ENCRYPTION, 0L, input_len, userdata);
if(padding_len <= AES_TRANSMISSION_THRESHOLD)
{
os_aes_process(input_data, output_data, input_len, AES_ECB, userdata);
}
else
{
handle_t aes_read = dma_open_free();
dma_set_request_source(aes_read, data->dma_req_base);
SemaphoreHandle_t event_read = xSemaphoreCreateBinary();
aes->dma_sel = 1;
dma_transmit_async(aes_read, &aes->aes_out_data, output_data, 0, 1, sizeof(uint32_t), padding_len >> 2, 4, event_read);
aes_input_bytes(input_data, input_len, AES_ECB, userdata);
configASSERT(xSemaphoreTake(event_read, portMAX_DELAY) == pdTRUE);
dma_close(aes_read);
vSemaphoreDelete(event_read);
}
exit_exclusive(data);
}
static void aes_ecb256_hard_decrypt(const uint8_t *input_key, const uint8_t *input_data, size_t input_len, uint8_t *output_data, void *userdata)
{
COMMON_ENTRY;
entry_exclusive(data);
size_t padding_len = ((input_len + 15) / 16) * 16;
os_aes_init(input_key, AES_256, NULL, 0L, NULL, AES_ECB, AES_HARD_DECRYPTION, 0L, input_len, userdata);
if(padding_len <= AES_TRANSMISSION_THRESHOLD)
{
os_aes_process(input_data, output_data, padding_len, AES_ECB, userdata);
}
else
{
handle_t aes_read = dma_open_free();
dma_set_request_source(aes_read, data->dma_req_base);
SemaphoreHandle_t event_read = xSemaphoreCreateBinary();
aes->dma_sel = 1;
dma_transmit_async(aes_read, &aes->aes_out_data, output_data, 0, 1, sizeof(uint32_t), padding_len >> 2, 4, event_read);
aes_input_bytes(input_data, input_len, AES_ECB, userdata);
configASSERT(xSemaphoreTake(event_read, portMAX_DELAY) == pdTRUE);
dma_close(aes_read);
vSemaphoreDelete(event_read);
}
exit_exclusive(data);
}
static void aes_ecb256_hard_encrypt(const uint8_t *input_key, const uint8_t *input_data, size_t input_len, uint8_t *output_data, void *userdata)
{
COMMON_ENTRY;
entry_exclusive(data);
size_t padding_len = ((input_len + 15) / 16) * 16;
os_aes_init(input_key, AES_256, NULL, 0L, NULL, AES_ECB, AES_HARD_ENCRYPTION, 0L, input_len, userdata);
if(padding_len <= AES_TRANSMISSION_THRESHOLD)
if (padding_len <= AES_TRANSMISSION_THRESHOLD)
{
os_aes_process(input_data, output_data, input_len, AES_ECB, userdata);
}
@ -576,7 +835,7 @@ static void aes_cbc128_hard_decrypt(cbc_context_t *context, const uint8_t *input
entry_exclusive(data);
size_t padding_len = ((input_len + 15) / 16) * 16;
os_aes_init(context->input_key, AES_128, context->iv, IV_LEN_128, NULL, AES_CBC, AES_HARD_DECRYPTION, 0L, input_len, userdata);
if(padding_len <= AES_TRANSMISSION_THRESHOLD)
if (padding_len <= AES_TRANSMISSION_THRESHOLD)
{
os_aes_process(input_data, output_data, padding_len, AES_CBC, userdata);
}
@ -602,7 +861,7 @@ static void aes_cbc128_hard_encrypt(cbc_context_t *context, const uint8_t *input
entry_exclusive(data);
size_t padding_len = ((input_len + 15) / 16) * 16;
os_aes_init(context->input_key, AES_128, context->iv, IV_LEN_128, NULL, AES_CBC, AES_HARD_ENCRYPTION, 0L, input_len, userdata);
if(padding_len <= AES_TRANSMISSION_THRESHOLD)
if (padding_len <= AES_TRANSMISSION_THRESHOLD)
{
os_aes_process(input_data, output_data, input_len, AES_CBC, userdata);
}
@ -630,7 +889,7 @@ static void aes_cbc192_hard_decrypt(cbc_context_t *context, const uint8_t *input
entry_exclusive(data);
size_t padding_len = ((input_len + 15) / 16) * 16;
os_aes_init(context->input_key, AES_192, context->iv, IV_LEN_128, NULL, AES_CBC, AES_HARD_DECRYPTION, 0L, input_len, userdata);
if(padding_len <= AES_TRANSMISSION_THRESHOLD)
if (padding_len <= AES_TRANSMISSION_THRESHOLD)
{
os_aes_process(input_data, output_data, padding_len, AES_CBC, userdata);
}
@ -656,7 +915,7 @@ static void aes_cbc192_hard_encrypt(cbc_context_t *context, const uint8_t *input
entry_exclusive(data);
size_t padding_len = ((input_len + 15) / 16) * 16;
os_aes_init(context->input_key, AES_192, context->iv, IV_LEN_128, NULL, AES_CBC, AES_HARD_ENCRYPTION, 0L, input_len, userdata);
if(padding_len <= AES_TRANSMISSION_THRESHOLD)
if (padding_len <= AES_TRANSMISSION_THRESHOLD)
{
os_aes_process(input_data, output_data, input_len, AES_CBC, userdata);
}
@ -682,7 +941,7 @@ static void aes_cbc256_hard_decrypt(cbc_context_t *context, const uint8_t *input
entry_exclusive(data);
size_t padding_len = ((input_len + 15) / 16) * 16;
os_aes_init(context->input_key, AES_256, context->iv, IV_LEN_128, NULL, AES_CBC, AES_HARD_DECRYPTION, 0L, input_len, userdata);
if(padding_len <= AES_TRANSMISSION_THRESHOLD)
if (padding_len <= AES_TRANSMISSION_THRESHOLD)
{
os_aes_process(input_data, output_data, padding_len, AES_CBC, userdata);
}
@ -708,7 +967,7 @@ static void aes_cbc256_hard_encrypt(cbc_context_t *context, const uint8_t *input
entry_exclusive(data);
size_t padding_len = ((input_len + 15) / 16) * 16;
os_aes_init(context->input_key, AES_256, context->iv, IV_LEN_128, NULL, AES_CBC, AES_HARD_ENCRYPTION, 0L, input_len, userdata);
if(padding_len <= AES_TRANSMISSION_THRESHOLD)
if (padding_len <= AES_TRANSMISSION_THRESHOLD)
{
os_aes_process(input_data, output_data, input_len, AES_CBC, userdata);
}
@ -733,8 +992,8 @@ static void aes_gcm128_hard_decrypt(gcm_context_t *context, const uint8_t *input
COMMON_ENTRY;
entry_exclusive(data);
os_aes_init(context->input_key, AES_128, context->iv, IV_LEN_96, context->gcm_aad,
AES_GCM, AES_HARD_DECRYPTION, context->gcm_aad_len, input_len, userdata);
if(input_len <= AES_TRANSMISSION_THRESHOLD)
AES_GCM, AES_HARD_DECRYPTION, context->gcm_aad_len, input_len, userdata);
if (input_len <= AES_TRANSMISSION_THRESHOLD)
{
os_aes_process(input_data, output_data, input_len, AES_GCM, userdata);
}
@ -762,8 +1021,8 @@ static void aes_gcm128_hard_encrypt(gcm_context_t *context, const uint8_t *input
COMMON_ENTRY;
entry_exclusive(data);
os_aes_init(context->input_key, AES_128, context->iv, IV_LEN_96, context->gcm_aad,
AES_GCM, AES_HARD_ENCRYPTION, context->gcm_aad_len, input_len, userdata);
if(input_len <= AES_TRANSMISSION_THRESHOLD)
AES_GCM, AES_HARD_ENCRYPTION, context->gcm_aad_len, input_len, userdata);
if (input_len <= AES_TRANSMISSION_THRESHOLD)
{
os_aes_process(input_data, output_data, input_len, AES_GCM, userdata);
}
@ -791,8 +1050,8 @@ static void aes_gcm192_hard_decrypt(gcm_context_t *context, const uint8_t *input
COMMON_ENTRY;
entry_exclusive(data);
os_aes_init(context->input_key, AES_192, context->iv, IV_LEN_96, context->gcm_aad,
AES_GCM, AES_HARD_DECRYPTION, context->gcm_aad_len, input_len, userdata);
if(input_len <= AES_TRANSMISSION_THRESHOLD)
AES_GCM, AES_HARD_DECRYPTION, context->gcm_aad_len, input_len, userdata);
if (input_len <= AES_TRANSMISSION_THRESHOLD)
{
os_aes_process(input_data, output_data, input_len, AES_GCM, userdata);
}
@ -820,8 +1079,8 @@ static void aes_gcm192_hard_encrypt(gcm_context_t *context, const uint8_t *input
COMMON_ENTRY;
entry_exclusive(data);
os_aes_init(context->input_key, AES_192, context->iv, IV_LEN_96, context->gcm_aad,
AES_GCM, AES_HARD_ENCRYPTION, context->gcm_aad_len, input_len, userdata);
if(input_len <= AES_TRANSMISSION_THRESHOLD)
AES_GCM, AES_HARD_ENCRYPTION, context->gcm_aad_len, input_len, userdata);
if (input_len <= AES_TRANSMISSION_THRESHOLD)
{
os_aes_process(input_data, output_data, input_len, AES_GCM, userdata);
}
@ -849,8 +1108,8 @@ static void aes_gcm256_hard_decrypt(gcm_context_t *context, const uint8_t *input
COMMON_ENTRY;
entry_exclusive(data);
os_aes_init(context->input_key, AES_256, context->iv, IV_LEN_96, context->gcm_aad,
AES_GCM, AES_HARD_DECRYPTION, context->gcm_aad_len, input_len, userdata);
if(input_len <= AES_TRANSMISSION_THRESHOLD)
AES_GCM, AES_HARD_DECRYPTION, context->gcm_aad_len, input_len, userdata);
if (input_len <= AES_TRANSMISSION_THRESHOLD)
{
os_aes_process(input_data, output_data, input_len, AES_GCM, userdata);
}
@ -878,8 +1137,8 @@ static void aes_gcm256_hard_encrypt(gcm_context_t *context, const uint8_t *input
COMMON_ENTRY;
entry_exclusive(data);
os_aes_init(context->input_key, AES_256, context->iv, IV_LEN_96, context->gcm_aad,
AES_GCM, AES_HARD_ENCRYPTION, context->gcm_aad_len, input_len, userdata);
if(input_len <= AES_TRANSMISSION_THRESHOLD)
AES_GCM, AES_HARD_ENCRYPTION, context->gcm_aad_len, input_len, userdata);
if (input_len <= AES_TRANSMISSION_THRESHOLD)
{
os_aes_process(input_data, output_data, input_len, AES_GCM, userdata);
}
@ -902,11 +1161,10 @@ static void aes_gcm256_hard_encrypt(gcm_context_t *context, const uint8_t *input
exit_exclusive(data);
}
static aes_dev_data dev0_data = {SYSCTL_CLOCK_AES, AES_BASE_ADDR, SYSCTL_DMA_SELECT_AES_REQ, 0, {{0}}};
static aes_dev_data dev0_data = { SYSCTL_CLOCK_AES, AES_BASE_ADDR, SYSCTL_DMA_SELECT_AES_REQ, 0, { { 0 } } };
const aes_driver_t g_aes_driver_aes0 =
{
{&dev0_data, aes_install, aes_open, aes_close},
const aes_driver_t g_aes_driver_aes0 = {
{ &dev0_data, aes_install, aes_open, aes_close },
aes_ecb128_hard_decrypt,
aes_ecb128_hard_encrypt,
aes_ecb192_hard_decrypt,

View File

@ -16,7 +16,7 @@
#define _DRIVERS_SDCARD_H
#include <stdint.h>
#include <driver.h>
#include <osdefs.h>
#ifdef __cplusplus
extern "C"

View File

@ -17,6 +17,7 @@
#include <hal.h>
#include <stdlib.h>
#include <string.h>
#include <kernel/driver.hpp>
#define COMMON_ENTRY \
spi_sdcard_dev_data_t *data = (spi_sdcard_dev_data_t *)userdata;

View File

@ -14,9 +14,7 @@
*/
#include "FreeRTOS.h"
#include <atomic.h>
#include "device_priv.h"
#include <devices.h>
#include <driver.h>
#include <hal.h>
#include <plic.h>
#include <semphr.h>
@ -25,6 +23,8 @@
#include <string.h>
#include <sysctl.h>
#include <uarths.h>
#include "device_priv.h"
#include "kernel/driver.hpp"
#define MAX_HANDLES 256
#define HANDLE_OFFSET 256

View File

@ -0,0 +1,16 @@
/* Copyright 2018 Canaan Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "FreeRTOS.h"
#include "filesystem.h"

View File

@ -17,7 +17,7 @@
#include <stddef.h>
#include <stdint.h>
#include "driver.h"
#include "osdefs.h"
#ifdef __cplusplus
extern "C"

View File

@ -1,470 +0,0 @@
/* Copyright 2018 Canaan Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef _FREERTOS_DRIVER_H
#define _FREERTOS_DRIVER_H
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <time.h>
#include "FreeRTOS.h"
#include "semphr.h"
#ifdef __cplusplus
extern "C"
{
#endif
typedef uintptr_t handle_t;
#define NULL_HANDLE 0
typedef struct _driver_base
{
void *userdata;
void (*install)(void *userdata);
int (*open)(void *userdata);
void (*close)(void *userdata);
} driver_base_t;
typedef enum
{
DRIVER_UART,
DRIVER_GPIO,
DRIVER_I2C,
DRIVER_I2C_DEVICE,
DRIVER_I2S,
DRIVER_SPI,
DRIVER_SPI_DEVICE,
DRIVER_DVP,
DRIVER_SCCB,
DRIVER_SCCB_DEVICE,
DRIVER_FFT,
DRIVER_AES,
DRIVER_SHA256,
DRIVER_TIMER,
DRIVER_PWM,
DRIVER_WDT,
DRIVER_RTC,
DRIVER_PIC,
DRIVER_DMAC,
DRIVER_DMA,
DRIVER_BLOCK_STORAGE,
DRIVER_FILE,
DRIVER_CUSTOM
} driver_type_t;
typedef struct tag_driver_registry
{
const char *name;
const void *driver;
driver_type_t type;
} driver_registry_t;
typedef enum _uart_stopbits
{
UART_STOP_1,
UART_STOP_1_5,
UART_STOP_2
} uart_stopbits_t;
typedef enum _uart_parity
{
UART_PARITY_NONE,
UART_PARITY_ODD,
UART_PARITY_EVEN
} uart_parity_t;
typedef struct tag_uart_driver
{
driver_base_t base;
void (*config)(uint32_t baud_rate, uint32_t databits, uart_stopbits_t stopbits, uart_parity_t parity, void *userdata);
int (*read)(uint8_t *buffer, size_t len, void *userdata);
int (*write)(const uint8_t *buffer, size_t len, void *userdata);
} uart_driver_t;
typedef enum _gpio_drive_mode
{
GPIO_DM_INPUT,
GPIO_DM_INPUT_PULL_DOWN,
GPIO_DM_INPUT_PULL_UP,
GPIO_DM_OUTPUT
} gpio_drive_mode_t;
typedef enum _gpio_pin_edge
{
GPIO_PE_NONE,
GPIO_PE_FALLING,
GPIO_PE_RISING,
GPIO_PE_BOTH
} gpio_pin_edge_t;
typedef enum _gpio_pin_value
{
GPIO_PV_LOW,
GPIO_PV_HIGH
} gpio_pin_value_t;
typedef void (*gpio_on_changed_t)(uint32_t pin, void *userdata);
typedef struct tag_gpio_driver
{
driver_base_t base;
uint32_t pin_count;
void (*set_drive_mode)(uint32_t pin, gpio_drive_mode_t mode, void *userdata);
void (*set_pin_edge)(uint32_t pin, gpio_pin_edge_t edge, void *userdata);
void (*set_on_changed)(uint32_t pin, gpio_on_changed_t callback, void *callback_data, void *userdata);
void (*set_pin_value)(uint32_t pin, gpio_pin_value_t value, void *userdata);
gpio_pin_value_t (*get_pin_value)(uint32_t pin, void *userdata);
} gpio_driver_t;
typedef struct tag_i2c_device_driver
{
driver_base_t base;
double (*set_clock_rate)(double clock_rate, void *userdata);
int (*read)(uint8_t *buffer, size_t len, void *userdata);
int (*write)(const uint8_t *buffer, size_t len, void *userdata);
int (*transfer_sequential)(const uint8_t *write_buffer, size_t write_len, uint8_t *read_buffer, size_t read_len, void *userdata);
} i2c_device_driver_t;
typedef enum _i2c_event
{
I2C_EV_START,
I2C_EV_RESTART,
I2C_EV_STOP
} i2c_event_t;
typedef struct _i2c_slave_handler
{
void (*on_receive)(uint32_t data);
uint32_t (*on_transmit)();
void (*on_event)(i2c_event_t event);
} i2c_slave_handler_t;
typedef struct tag_i2c_driver
{
driver_base_t base;
i2c_device_driver_t * (*get_device)(uint32_t slave_address, uint32_t address_width, void *userdata);
void (*config_as_slave)(uint32_t slave_address, uint32_t address_width, i2c_slave_handler_t *handler, void *userdata);
double (*slave_set_clock_rate)(double clock_rate, void *userdata);
} i2c_driver_t;
typedef enum _audio_format_type
{
AUDIO_FMT_PCM
} audio_format_type_t;
typedef struct _audio_format
{
audio_format_type_t type;
uint32_t bits_per_sample;
uint32_t sample_rate;
uint32_t channels;
} audio_format_t;
typedef enum _i2s_align_mode
{
I2S_AM_STANDARD,
I2S_AM_RIGHT,
I2S_AM_LEFT
} i2s_align_mode_t;
typedef struct tag_i2s_driver
{
driver_base_t base;
void (*config_as_render)(const audio_format_t *format, size_t delay_ms, i2s_align_mode_t align_mode, size_t channels_mask, void *userdata);
void (*config_as_capture)(const audio_format_t *format, size_t delay_ms, i2s_align_mode_t align_mode, size_t channels_mask, void *userdata);
void (*get_buffer)(uint8_t **buffer, size_t *frames, void *userdata);
void (*release_buffer)(size_t frames, void *userdata);
void (*start)(void *userdata);
void (*stop)(void *userdata);
} i2s_driver_t;
typedef enum _spi_mode
{
SPI_MODE_0,
SPI_MODE_1,
SPI_MODE_2,
SPI_MODE_3,
} spi_mode_t;
typedef enum _spi_frame_format
{
SPI_FF_STANDARD,
SPI_FF_DUAL,
SPI_FF_QUAD,
SPI_FF_OCTAL
} spi_frame_format_t;
typedef enum _spi_inst_addr_trans_mode
{
SPI_AITM_STANDARD,
SPI_AITM_ADDR_STANDARD,
SPI_AITM_AS_FRAME_FORMAT
} spi_inst_addr_trans_mode_t;
typedef struct tag_spi_device_driver
{
driver_base_t base;
void (*config)(uint32_t instruction_length, uint32_t address_length, uint32_t wait_cycles, spi_inst_addr_trans_mode_t trans_mode, void *userdata);
double (*set_clock_rate)(double clock_rate, void *userdata);
int (*read)(uint8_t *buffer, size_t len, void *userdata);
int (*write)(const uint8_t *buffer, size_t len, void *userdata);
int (*transfer_full_duplex)(const uint8_t *write_buffer, size_t write_len, uint8_t *read_buffer, size_t read_len, void *userdata);
int (*transfer_sequential)(const uint8_t *write_buffer, size_t write_len, uint8_t *read_buffer, size_t read_len, void *userdata);
void (*fill)(uint32_t instruction, uint32_t address, uint32_t value, size_t count, void *userdata);
} spi_device_driver_t;
typedef struct tag_spi_driver
{
driver_base_t base;
spi_device_driver_t * (*get_device)(spi_mode_t mode, spi_frame_format_t frame_format, uint32_t chip_select_mask, uint32_t data_bit_length, void *userdata);
} spi_driver_t;
typedef enum _video_format
{
VIDEO_FMT_RGB565,
VIDEO_FMT_RGB24_PLANAR
} video_format_t;
typedef enum _video_frame_event
{
VIDEO_FE_BEGIN,
VIDEO_FE_END
} dvp_frame_event_t;
typedef enum _dvp_signal_type
{
DVP_SIG_POWER_DOWN,
DVP_SIG_RESET
} dvp_signal_type_t;
typedef void (*dvp_on_frame_event_t)(dvp_frame_event_t event, void *userdata);
typedef struct tag_dvp_driver
{
driver_base_t base;
uint32_t output_num;
void (*config)(uint32_t width, uint32_t height, bool auto_enable, void *userdata);
void (*enable_frame)(void* userdata);
void (*set_signal)(dvp_signal_type_t type, bool value, void *userdata);
void (*set_output_enable)(uint32_t index, bool enable, void *userdata);
void (*set_output_attributes)(uint32_t index, video_format_t format, void *output_buffer, void *userdata);
void (*set_frame_event_enable)(dvp_frame_event_t event, bool enable, void *userdata);
void (*set_on_frame_event)(dvp_on_frame_event_t callback, void *callback_data, void *userdata);
double (*xclk_set_clock_rate)(double clock_rate, void *userdata);
} dvp_driver_t;
typedef struct tag_sccb_device_driver
{
driver_base_t base;
uint8_t(*read_byte)(uint16_t reg_address, void *userdata);
void(*write_byte)(uint16_t reg_address, uint8_t value, void *userdata);
} sccb_device_driver_t;
typedef struct tag_sccb_driver
{
driver_base_t base;
sccb_device_driver_t* (*get_device)(uint32_t slave_address, uint32_t reg_address_width, void *userdata);
} sccb_driver_t;
typedef struct tag_fft_data
{
int16_t I1;
int16_t R1;
int16_t I2;
int16_t R2;
} fft_data_t;
typedef enum tag_fft_direction
{
FFT_DIR_BACKWARD,
FFT_DIR_FORWARD,
FFT_DIR_MAX,
} fft_direction_t;
typedef struct tag_fft_driver
{
driver_base_t base;
void (*complex_uint16)(uint16_t shift, fft_direction_t direction, const uint64_t *input, size_t point_num, uint64_t *output, void *userdata);
} fft_driver_t;
typedef enum
{
AES_ECB = 0,
AES_CBC = 1,
AES_GCM = 2,
AES_CIPHER_MAX
} aes_cipher_mode_t;
typedef enum
{
AES_128 = 16,
AES_192 = 24,
AES_256 = 32,
} aes_kmode;
typedef enum
{
AES_HARD_ENCRYPTION = 0,
AES_HARD_DECRYPTION = 1,
} aes_encrypt_sel_t;
typedef struct _gcm_context
{
/* The buffer holding the encryption or decryption key. */
uint8_t *input_key;
/* The initialization vector. must be 96 bit */
uint8_t *iv;
/* The buffer holding the Additional authenticated data. or NULL */
uint8_t *gcm_aad;
/* The length of the Additional authenticated data. or 0L */
size_t gcm_aad_len;
} gcm_context_t;
typedef struct _cbc_context
{
/* The buffer holding the encryption or decryption key. */
uint8_t *input_key;
/* The initialization vector. must be 128 bit */
uint8_t *iv;
} cbc_context_t;
typedef struct tag_aes_driver
{
driver_base_t base;
void (*aes_ecb128_hard_decrypt)(const uint8_t *input_key, const uint8_t *input_data, size_t input_len, uint8_t *output_data, void *userdata);
void (*aes_ecb128_hard_encrypt)(const uint8_t *input_key, const uint8_t *input_data, size_t input_len, uint8_t *output_data, void *userdata);
void (*aes_ecb192_hard_decrypt)(const uint8_t *input_key, const uint8_t *input_data, size_t input_len, uint8_t *output_data, void *userdata);
void (*aes_ecb192_hard_encrypt)(const uint8_t *input_key, const uint8_t *input_data, size_t input_len, uint8_t *output_data, void *userdata);
void (*aes_ecb256_hard_decrypt)(const uint8_t *input_key, const uint8_t *input_data, size_t input_len, uint8_t *output_data, void *userdata);
void (*aes_ecb256_hard_encrypt)(const uint8_t *input_key, const uint8_t *input_data, size_t input_len, uint8_t *output_data, void *userdata);
void (*aes_cbc128_hard_decrypt)(cbc_context_t *context, const uint8_t *input_data, size_t input_len, uint8_t *output_data, void *userdata);
void (*aes_cbc128_hard_encrypt)(cbc_context_t *context, const uint8_t *input_data, size_t input_len, uint8_t *output_data, void *userdata);
void (*aes_cbc192_hard_decrypt)(cbc_context_t *context, const uint8_t *input_data, size_t input_len, uint8_t *output_data, void *userdata);
void (*aes_cbc192_hard_encrypt)(cbc_context_t *context, const uint8_t *input_data, size_t input_len, uint8_t *output_data, void *userdata);
void (*aes_cbc256_hard_decrypt)(cbc_context_t *context, const uint8_t *input_data, size_t input_len, uint8_t *output_data, void *userdata);
void (*aes_cbc256_hard_encrypt)(cbc_context_t *context, const uint8_t *input_data, size_t input_len, uint8_t *output_data, void *userdata);
void (*aes_gcm128_hard_decrypt)(gcm_context_t *context, const uint8_t *input_data, size_t input_len, uint8_t *output_data, uint8_t *gcm_tag, void *userdata);
void (*aes_gcm128_hard_encrypt)(gcm_context_t *context, const uint8_t *input_data, size_t input_len, uint8_t *output_data, uint8_t *gcm_tag, void *userdata);
void (*aes_gcm192_hard_decrypt)(gcm_context_t *context, const uint8_t *input_data, size_t input_len, uint8_t *output_data, uint8_t *gcm_tag, void *userdata);
void (*aes_gcm192_hard_encrypt)(gcm_context_t *context, const uint8_t *input_data, size_t input_len, uint8_t *output_data, uint8_t *gcm_tag, void *userdata);
void (*aes_gcm256_hard_decrypt)(gcm_context_t *context, const uint8_t *input_data, size_t input_len, uint8_t *output_data, uint8_t *gcm_tag, void *userdata);
void (*aes_gcm256_hard_encrypt)(gcm_context_t *context, const uint8_t *input_data, size_t input_len, uint8_t *output_data, uint8_t *gcm_tag, void *userdata);
} aes_driver_t;
typedef struct tag_sha256_driver
{
driver_base_t base;
void (*sha256_hard_calculate)(const uint8_t *input, size_t input_len, uint8_t *output, void *userdata);
} sha256_driver_t;
typedef void (*timer_on_tick_t)(void* userdata);
typedef struct tag_timer_driver
{
driver_base_t base;
size_t (*set_interval)(size_t nanoseconds, void *userdata);
void (*set_on_tick)(timer_on_tick_t on_tick, void *ontick_data, void *userdata);
void (*set_enable)(bool enable, void *userdata);
} timer_driver_t;
typedef struct tag_pwm_driver
{
driver_base_t base;
uint32_t pin_count;
double (*set_frequency)(double frequency, void *userdata);
double (*set_active_duty_cycle_percentage)(uint32_t pin, double duty_cycle_percentage, void *userdata);
void (*set_enable)(uint32_t pin, bool enable, void *userdata);
} pwm_driver_t;
typedef enum _wdt_response_mode
{
WDT_RESP_RESET,
WDT_RESP_INTERRUPT
} wdt_response_mode_t;
typedef int (*wdt_on_timeout_t)(void *userdata);
typedef struct tag_wdt_driver
{
driver_base_t base;
void (*set_response_mode)(wdt_response_mode_t mode, void *userdata);
size_t (*set_timeout)(size_t nanoseconds, void *userdata);
void (*set_on_timeout)(wdt_on_timeout_t handler, void *handler_userdata, void *userdata);
void (*restart_counter)(void *userdata);
void (*set_enable)(bool enable, void *userdata);
} wdt_driver_t;
typedef struct tag_rtc_driver
{
driver_base_t base;
void (*get_datetime)(struct tm *datetime, void *userdata);
void (*set_datetime)(const struct tm *datetime, void *userdata);
} rtc_driver_t;
typedef struct _custom_driver
{
driver_base_t base;
int (*io_control)(uint32_t control_code, const uint8_t *write_buffer, size_t write_len, uint8_t *read_buffer, size_t read_len, void *userdata);
} custom_driver_t;
/* ===== internal drivers ======*/
typedef void (*pic_irq_handler_t)(void *userdata);
void kernel_iface_pic_on_irq(uint32_t irq);
typedef struct tag_pic_driver
{
driver_base_t base;
void (*set_irq_enable)(uint32_t irq, bool enable, void *userdata);
void (*set_irq_priority)(uint32_t irq, uint32_t priority, void *userdata);
} pic_driver_t;
typedef struct tag_dmac_driver
{
driver_base_t base;
} dmac_driver_t;
typedef void (*dma_stage_completion_handler_t)(void *userdata);
typedef struct tag_dma_driver
{
driver_base_t base;
void (*set_select_request)(uint32_t request, void *userdata);
void (*config)(uint32_t priority, void *userdata);
void (*transmit_async)(const volatile void *src, volatile void *dest, bool src_inc, bool dest_inc, size_t element_size, size_t count, size_t burst_size, SemaphoreHandle_t completion_event, void *userdata);
void (*loop_async)(const volatile void **srcs, size_t src_num, volatile void **dests, size_t dest_num, bool src_inc, bool dest_inc, size_t element_size, size_t count, size_t burst_size, dma_stage_completion_handler_t stage_completion_handler, void *stage_completion_handler_data, SemaphoreHandle_t completion_event, int *stop_signal, void *userdata);
} dma_driver_t;
typedef struct tag_block_storage_driver
{
driver_base_t base;
uint32_t (*get_rw_block_size)(void *userdata);
uint32_t (*get_blocks_count)(void *userdata);
int (*read_blocks)(uint32_t start_block, uint32_t blocks_count, uint8_t *buffer, void *userdata);
int (*write_blocks)(uint32_t start_block, uint32_t blocks_count, const uint8_t *buffer, void *userdata);
} block_storage_driver_t;
extern driver_registry_t g_hal_drivers[];
extern driver_registry_t g_dma_drivers[];
extern driver_registry_t g_system_drivers[];
#ifdef __cplusplus
}
#endif
#endif /* _FREERTOS_DRIVER_H */

View File

@ -0,0 +1,45 @@
/* Copyright 2018 Canaan Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef _FREERTOS_FILESYSTEM_H
#define _FREERTOS_FILESYSTEM_H
#include <driver.h>
#include <stddef.h>
#include <stdint.h>
#ifdef __cplusplus
extern "C"
{
#endif
/**
* @brief Mount a filesystem
*
* @param[in] name The filesystem path
* @param[in] storage_device_name The storage device path
*
* @return result
* - 0 Fail
* - other The filesystem handle
*/
handle_t filesystem_mount(const char *name, const char *storage_device_name);
handle_t filesystem_file_open(handle_t filesystem, const char* name);
#ifdef __cplusplus
}
#endif
#endif /* _FREERTOS_FILESYSTEM_H */

View File

@ -12,12 +12,12 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef _FREERTOS_DEVICES_INTERN_H
#define _FREERTOS_DEVICES_INTERN_H
#ifndef _FREERTOS_HAL_H
#define _FREERTOS_HAL_H
#include <driver.h>
#include <stddef.h>
#include <stdint.h>
#include "osdefs.h"
#ifdef __cplusplus
extern "C"
@ -32,18 +32,6 @@ extern "C"
*/
uint32_t system_set_cpu_frequency(uint32_t frequency);
/**
* @brief Install a driver
* @param[in] name Specify the path to access it later
* @param[in] type The type of driver
* @param[in] driver The driver info
*
* @return result
* - NULL Fail
* - other The driver registry
*/
driver_registry_t * system_install_driver(const char *name, driver_type_t type, const void *driver);
/**
* @brief Enable or disable IRQ
* @param[in] irq IRQ number
@ -136,4 +124,4 @@ void dma_loop_async(handle_t file, const volatile void **srcs, size_t src_num, v
}
#endif
#endif /* _FREERTOS_DEVICES_INTERN_H */
#endif /* _FREERTOS_HAL_H */

View File

@ -0,0 +1,284 @@
/* Copyright 2018 Canaan Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef _FREERTOS_DRIVER_H
#define _FREERTOS_DRIVER_H
#include "object.hpp"
#include <gsl/span>
namespace sys
{
typedef enum _driver_type
{
DRIVER_UART,
DRIVER_GPIO,
DRIVER_I2C,
DRIVER_I2C_DEVICE,
DRIVER_I2S,
DRIVER_SPI,
DRIVER_SPI_DEVICE,
DRIVER_DVP,
DRIVER_SCCB,
DRIVER_SCCB_DEVICE,
DRIVER_FFT,
DRIVER_AES,
DRIVER_SHA256,
DRIVER_TIMER,
DRIVER_PWM,
DRIVER_WDT,
DRIVER_RTC,
DRIVER_PIC,
DRIVER_DMAC,
DRIVER_DMA,
DRIVER_BLOCK_STORAGE,
DRIVER_FILE,
DRIVER_CUSTOM
} driver_type_t;
typedef struct tag_driver_registry
{
const char *name;
const void *driver;
driver_type_t type;
} driver_registry_t;
class object_access : public virtual object
{
public:
virtual void open() = 0;
virtual void close() = 0;
};
class driver : public virtual object, public virtual object_access
{
public:
virtual void install() = 0;
};
class uart_driver : public driver
{
public:
virtual void config(uint32_t baud_rate, uint32_t databits, uart_stopbits_t stopbits, uart_parity_t parity) = 0;
virtual size_t read(gsl::span<uint8_t> buffer) = 0;
virtual void write(gsl::span<const uint8_t> buffer) = 0;
};
class gpio_driver : public driver
{
public:
virtual uint32_t get_pin_count() = 0;
virtual void set_drive_mode(uint32_t pin, gpio_drive_mode_t mode) = 0;
virtual void set_pin_edge(uint32_t pin, gpio_pin_edge_t edge) = 0;
virtual void set_on_changed(uint32_t pin, gpio_on_changed_t callback, void *userdata) = 0;
virtual gpio_pin_value_t get_pin_value(uint32_t pin) = 0;
virtual void set_pin_value(uint32_t pin, gpio_pin_value_t value) = 0;
};
class i2c_device_driver : public driver
{
public:
virtual double set_clock_rate(double clock_rate) = 0;
virtual size_t read(gsl::span<uint8_t> buffer) = 0;
virtual void write(gsl::span<const uint8_t> buffer) = 0;
virtual size_t transfer_sequential(gsl::span<const uint8_t> write_buffer, gsl::span<uint8_t> read_buffer) = 0;
};
class i2c_driver : public driver
{
public:
virtual object_ptr<i2c_device_driver> get_device(uint32_t slave_address, uint32_t address_width) = 0;
virtual void config_as_slave(uint32_t slave_address, uint32_t address_width, const i2c_slave_handler_t &handler) = 0;
virtual double slave_set_clock_rate(double clock_rate) = 0;
};
class i2s_driver : public driver
{
public:
virtual void config_as_render(const audio_format_t &format, size_t delay_ms, i2s_align_mode_t align_mode, uint32_t channels_mask) = 0;
virtual void config_as_capture(const audio_format_t &format, size_t delay_ms, i2s_align_mode_t align_mode, uint32_t channels_mask) = 0;
virtual void get_buffer(gsl::span<uint8_t> &buffer, uint32_t &frames) = 0;
virtual void release_buffer(uint32_t frames) = 0;
virtual void start() = 0;
virtual void stop() = 0;
};
class spi_device_driver : public driver
{
public:
virtual void config_non_standard(uint32_t instruction_length, uint32_t address_length, uint32_t wait_cycles, spi_inst_addr_trans_mode_t trans_mode) = 0;
virtual double set_clock_rate(double clock_rate) = 0;
virtual size_t read(gsl::span<uint8_t> buffer) = 0;
virtual void write(gsl::span<const uint8_t> buffer) = 0;
virtual size_t transfer_full_duplex(gsl::span<const uint8_t> write_buffer, gsl::span<uint8_t> read_buffer) = 0;
virtual size_t transfer_sequential(gsl::span<const uint8_t> write_buffer, gsl::span<uint8_t> read_buffer) = 0;
virtual void fill(uint32_t instruction, uint32_t address, uint32_t value, size_t count) = 0;
};
class spi_driver : public driver
{
public:
virtual object_ptr<spi_device_driver> get_device(spi_mode_t mode, spi_frame_format_t frame_format, uint32_t chip_select_mask, uint32_t data_bit_length) = 0;
};
class dvp_driver : public driver
{
public:
virtual uint32_t get_output_num() = 0;
virtual void config(uint32_t width, uint32_t height, bool auto_enable) = 0;
virtual void enable_frame() = 0;
virtual void set_signal(dvp_signal_type_t type, bool value) = 0;
virtual void set_output_enable(uint32_t index, bool enable) = 0;
virtual void set_output_attributes(uint32_t index, video_format_t format, gsl::span<uint8_t> output_buffer) = 0;
virtual void set_frame_event_enable(dvp_frame_event_t event, bool enable) = 0;
virtual void set_on_frame_event(dvp_on_frame_event_t callback, void *userdata) = 0;
virtual double xclk_set_clock_rate(double clock_rate) = 0;
};
class sccb_device_driver : public driver
{
public:
virtual uint8_t read_byte(uint16_t reg_address) = 0;
virtual void write_byte(uint16_t reg_address, uint8_t value) = 0;
};
class sccb_driver : public driver
{
public:
virtual object_ptr<sccb_device_driver> get_device(uint32_t slave_address, uint32_t reg_address_width) = 0;
};
class fft_driver : public driver
{
public:
virtual void complex_uint16(uint16_t shift, fft_direction_t direction, gsl::span<const uint64_t> input, size_t point_num, gsl::span<uint64_t> output) = 0;
};
class aes_driver : public driver
{
public:
virtual void aes_ecb128_hard_decrypt(gsl::span<const uint8_t> input_key, gsl::span<const uint8_t> input_data, gsl::span<uint8_t> output_data) = 0;
virtual void aes_ecb128_hard_encrypt(gsl::span<const uint8_t> input_key, gsl::span<const uint8_t> input_data, gsl::span<uint8_t> output_data) = 0;
virtual void aes_ecb192_hard_decrypt(gsl::span<const uint8_t> input_key, gsl::span<const uint8_t> input_data, gsl::span<uint8_t> output_data) = 0;
virtual void aes_ecb192_hard_encrypt(gsl::span<const uint8_t> input_key, gsl::span<const uint8_t> input_data, gsl::span<uint8_t> output_data) = 0;
virtual void aes_ecb256_hard_decrypt(gsl::span<const uint8_t> input_key, gsl::span<const uint8_t> input_data, gsl::span<uint8_t> output_data) = 0;
virtual void aes_ecb256_hard_encrypt(gsl::span<const uint8_t> input_key, gsl::span<const uint8_t> input_data, gsl::span<uint8_t> output_data) = 0;
virtual void aes_cbc128_hard_decrypt(cbc_context_t &context, gsl::span<const uint8_t> input_data, gsl::span<uint8_t> output_data) = 0;
virtual void aes_cbc128_hard_encrypt(cbc_context_t &context, gsl::span<const uint8_t> input_data, gsl::span<uint8_t> output_data) = 0;
virtual void aes_cbc192_hard_decrypt(cbc_context_t &context, gsl::span<const uint8_t> input_data, gsl::span<uint8_t> output_data) = 0;
virtual void aes_cbc192_hard_encrypt(cbc_context_t &context, gsl::span<const uint8_t> input_data, gsl::span<uint8_t> output_data) = 0;
virtual void aes_cbc256_hard_decrypt(cbc_context_t &context, gsl::span<const uint8_t> input_data, gsl::span<uint8_t> output_data) = 0;
virtual void aes_cbc256_hard_encrypt(cbc_context_t &context, gsl::span<const uint8_t> input_data, gsl::span<uint8_t> output_data) = 0;
virtual void aes_gcm128_hard_decrypt(gcm_context_t &context, gsl::span<const uint8_t> input_data, gsl::span<uint8_t> output_data, gsl::span<uint8_t> gcm_tag) = 0;
};
class sha256_driver : public driver
{
public:
virtual void sha256_hard_calculate(gsl::span<const uint8_t> input_data, gsl::span<uint8_t> output_data) = 0;
};
class timer_driver : public driver
{
public:
virtual void set_interval(size_t nanoseconds) = 0;
virtual void set_on_tick(timer_on_tick_t on_tick, void *userdata) = 0;
virtual void set_enable(bool enable) = 0;
};
class pwm_driver : public driver
{
public:
virtual uint32_t get_pin_count() = 0;
virtual double set_frequency(double frequency) = 0;
virtual double set_active_duty_cycle_percentage(uint32_t pin, double duty_cycle_percentage) = 0;
virtual void set_enable(uint32_t pin, bool enable) = 0;
};
class wdt_driver : public driver
{
public:
virtual void set_response_mode(wdt_response_mode_t mode) = 0;
virtual size_t set_timeout(size_t nanoseconds) = 0;
virtual void set_on_timeout(wdt_on_timeout_t handler, void *userdata) = 0;
virtual void restart_counter() = 0;
virtual void set_enable(bool enable) = 0;
};
class rtc_driver : public driver
{
public:
virtual void get_datetime(struct tm &datetime) = 0;
virtual void set_datatime(const struct tm &datetime) = 0;
};
class custom_driver : public driver
{
public:
virtual void control(uint32_t control_code, gsl::span<const uint8_t> write_buffer, gsl::span<uint8_t> read_buffer) = 0;
};
/* ===== internal drivers ======*/
void kernel_iface_pic_on_irq(uint32_t irq);
class pic_driver : public driver
{
public:
virtual void set_irq_enable(uint32_t irq, bool enable) = 0;
virtual void set_irq_priority(uint32_t irq, uint32_t priority) = 0;
};
class dma_driver : public driver
{
public:
virtual void set_select_request(uint32_t request) = 0;
virtual void config(uint32_t priority) = 0;
virtual void transmit_async(const volatile void *src, volatile void *dest, bool src_inc, bool dest_inc, size_t element_size, size_t count, size_t burst_size, SemaphoreHandle_t completion_event) = 0;
virtual void loop_async(const volatile void **srcs, size_t src_num, volatile void **dests, size_t dest_num, bool src_inc, bool dest_inc, size_t element_size, size_t count, size_t burst_size, dma_stage_completion_handler_t stage_completion_handler, void *stage_completion_handler_data, SemaphoreHandle_t completion_event, int *stop_signal) = 0;
};
class dmac_driver : public driver
{
public:
};
class block_storage_driver : public driver
{
public:
virtual uint32_t get_rw_block_size() = 0;
virtual uint32_t get_blocks_count() = 0;
virtual void read_blocks(uint32_t start_block, uint32_t blocks_count, gsl::span<uint8_t> buffer) = 0;
virtual void write_blocks(uint32_t start_block, uint32_t blocks_count, gsl::span<const uint8_t> buffer) = 0;
};
extern driver_registry_t g_hal_drivers[];
extern driver_registry_t g_dma_drivers[];
extern driver_registry_t g_system_drivers[];
/**
* @brief Install a driver
* @param[in] name Specify the path to access it later
* @param[in] type The type of driver
* @param[in] driver The driver info
*
* @return result
* - NULL Fail
* - other The driver registry
*/
driver_registry_t *system_install_driver(const char *name, driver_type_t type, const void *driver);
}
#endif /* _FREERTOS_DRIVER_H */

View File

@ -0,0 +1,57 @@
/* Copyright 2018 Canaan Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef _FREERTOS_DRIVER_IMPL_H
#define _FREERTOS_DRIVER_IMPL_H
#include "driver.hpp"
#include <atomic>
namespace sys
{
class static_object : public virtual object
{
public:
virtual void add_ref() override;
virtual bool release() override;
};
class free_object_access : public virtual object_access
{
public:
free_object_access() noexcept;
virtual void open() override;
virtual void close() override;
protected:
virtual void on_first_open() = 0;
virtual void on_last_close() = 0;
private:
std::atomic<size_t> used_count_;
};
class semaphore_lock
{
public:
semaphore_lock(SemaphoreHandle_t semaphore) noexcept;
~semaphore_lock();
private:
SemaphoreHandle_t semaphore_;
};
}
#endif /* _FREERTOS_DRIVER_H */

View File

@ -0,0 +1,46 @@
/* Copyright 2018 Canaan Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef _FREERTOS_OBJECT_H
#define _FREERTOS_OBJECT_H
#include "osdefs.h"
#include <memory>
namespace sys
{
class object
{
public:
virtual void add_ref() = 0;
virtual bool release() = 0;
};
template<class T>
class object_ptr
{
public:
constexpr object_ptr(nullptr_t = nullptr) noexcept
: obj_(nullptr)
{
}
object_ptr(std::in_place_t, T* obj) noexcept
: obj_(obj)
{
}
};
}
#endif /* _FREERTOS_OBJECT_H */

View File

@ -0,0 +1,223 @@
/* Copyright 2018 Canaan Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef _FREERTOS_OSDEFS_H
#define _FREERTOS_OSDEFS_H
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <time.h>
#include "FreeRTOS.h"
#include "semphr.h"
#ifdef __cplusplus
extern "C"
{
#endif
#define NULL_HANDLE 0
typedef uintptr_t handle_t;
typedef enum _uart_stopbits
{
UART_STOP_1,
UART_STOP_1_5,
UART_STOP_2
} uart_stopbits_t;
typedef enum _uart_parity
{
UART_PARITY_NONE,
UART_PARITY_ODD,
UART_PARITY_EVEN
} uart_parity_t;
typedef enum _gpio_drive_mode
{
GPIO_DM_INPUT,
GPIO_DM_INPUT_PULL_DOWN,
GPIO_DM_INPUT_PULL_UP,
GPIO_DM_OUTPUT
} gpio_drive_mode_t;
typedef enum _gpio_pin_edge
{
GPIO_PE_NONE,
GPIO_PE_FALLING,
GPIO_PE_RISING,
GPIO_PE_BOTH
} gpio_pin_edge_t;
typedef enum _gpio_pin_value
{
GPIO_PV_LOW,
GPIO_PV_HIGH
} gpio_pin_value_t;
typedef void(*gpio_on_changed_t)(uint32_t pin, void *userdata);
typedef enum _i2c_event
{
I2C_EV_START,
I2C_EV_RESTART,
I2C_EV_STOP
} i2c_event_t;
typedef struct _i2c_slave_handler
{
void(*on_receive)(uint32_t data);
uint32_t(*on_transmit)();
void(*on_event)(i2c_event_t event);
} i2c_slave_handler_t;
typedef enum _audio_format_type
{
AUDIO_FMT_PCM
} audio_format_type_t;
typedef struct _audio_format
{
audio_format_type_t type;
uint32_t bits_per_sample;
uint32_t sample_rate;
uint32_t channels;
} audio_format_t;
typedef enum _i2s_align_mode
{
I2S_AM_STANDARD,
I2S_AM_RIGHT,
I2S_AM_LEFT
} i2s_align_mode_t;
typedef enum _spi_mode
{
SPI_MODE_0,
SPI_MODE_1,
SPI_MODE_2,
SPI_MODE_3,
} spi_mode_t;
typedef enum _spi_frame_format
{
SPI_FF_STANDARD,
SPI_FF_DUAL,
SPI_FF_QUAD,
SPI_FF_OCTAL
} spi_frame_format_t;
typedef enum _spi_inst_addr_trans_mode
{
SPI_AITM_STANDARD,
SPI_AITM_ADDR_STANDARD,
SPI_AITM_AS_FRAME_FORMAT
} spi_inst_addr_trans_mode_t;
typedef enum _video_format
{
VIDEO_FMT_RGB565,
VIDEO_FMT_RGB24_PLANAR
} video_format_t;
typedef enum _video_frame_event
{
VIDEO_FE_BEGIN,
VIDEO_FE_END
} dvp_frame_event_t;
typedef enum _dvp_signal_type
{
DVP_SIG_POWER_DOWN,
DVP_SIG_RESET
} dvp_signal_type_t;
typedef void(*dvp_on_frame_event_t)(dvp_frame_event_t event, void *userdata);
typedef struct tag_fft_data
{
int16_t I1;
int16_t R1;
int16_t I2;
int16_t R2;
} fft_data_t;
typedef enum tag_fft_direction
{
FFT_DIR_BACKWARD,
FFT_DIR_FORWARD,
FFT_DIR_MAX,
} fft_direction_t;
typedef enum
{
AES_ECB = 0,
AES_CBC = 1,
AES_GCM = 2,
AES_CIPHER_MAX
} aes_cipher_mode_t;
typedef enum
{
AES_128 = 16,
AES_192 = 24,
AES_256 = 32,
} aes_kmode;
typedef enum
{
AES_HARD_ENCRYPTION = 0,
AES_HARD_DECRYPTION = 1,
} aes_encrypt_sel_t;
typedef struct _gcm_context
{
/* The buffer holding the encryption or decryption key. */
uint8_t *input_key;
/* The initialization vector. must be 96 bit */
uint8_t *iv;
/* The buffer holding the Additional authenticated data. or NULL */
uint8_t *gcm_aad;
/* The length of the Additional authenticated data. or 0L */
size_t gcm_aad_len;
} gcm_context_t;
typedef struct _cbc_context
{
/* The buffer holding the encryption or decryption key. */
uint8_t *input_key;
/* The initialization vector. must be 128 bit */
uint8_t *iv;
} cbc_context_t;
typedef void(*timer_on_tick_t)(void* userdata);
typedef enum _wdt_response_mode
{
WDT_RESP_RESET,
WDT_RESP_INTERRUPT
} wdt_response_mode_t;
typedef int(*wdt_on_timeout_t)(void *userdata);
typedef void(*pic_irq_handler_t)(void *userdata);
typedef void(*dma_stage_completion_handler_t)(void *userdata);
#ifdef __cplusplus
}
#endif
#endif /* _FREERTOS_OSDEFS_H */

View File

@ -0,0 +1,54 @@
/* Copyright 2018 Canaan Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "kernel/driver_impl.hpp"
using namespace sys;
void static_object::add_ref()
{
}
bool static_object::release()
{
return false;
}
free_object_access::free_object_access() noexcept
: used_count_(0)
{
}
void free_object_access::open()
{
if (used_count_.fetch_add(1, std::memory_order_relaxed) == 0)
on_first_open();
}
void free_object_access::close()
{
if (used_count_.fetch_sub(1, std::memory_order_relaxed) == 1)
on_last_close();
}
semaphore_lock::semaphore_lock(SemaphoreHandle_t semaphore) noexcept
: semaphore_(semaphore)
{
configASSERT(xSemaphoreTake(semaphore, portMAX_DELAY) == pdTRUE);
}
semaphore_lock::~semaphore_lock()
{
xSemaphoreGive(semaphore_);
}

29
third_party/gsl/gsl vendored Normal file
View File

@ -0,0 +1,29 @@
///////////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2015 Microsoft Corporation. All rights reserved.
//
// This code is licensed under the MIT License (MIT).
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
///////////////////////////////////////////////////////////////////////////////
#ifndef GSL_GSL_H
#define GSL_GSL_H
#include <gsl/gsl_algorithm> // copy
#include <gsl/gsl_assert> // Ensures/Expects
#include <gsl/gsl_byte> // byte
#include <gsl/gsl_util> // finally()/narrow()/narrow_cast()...
#include <gsl/multi_span> // multi_span, strided_span...
#include <gsl/pointers> // owner, not_null
#include <gsl/span> // span
#include <gsl/string_span> // zstring, string_span, zstring_builder...
#endif // GSL_GSL_H

63
third_party/gsl/gsl_algorithm vendored Normal file
View File

@ -0,0 +1,63 @@
///////////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2015 Microsoft Corporation. All rights reserved.
//
// This code is licensed under the MIT License (MIT).
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
///////////////////////////////////////////////////////////////////////////////
#ifndef GSL_ALGORITHM_H
#define GSL_ALGORITHM_H
#include <gsl/gsl_assert> // for Expects
#include <gsl/span> // for dynamic_extent, span
#include <algorithm> // for copy_n
#include <cstddef> // for ptrdiff_t
#include <type_traits> // for is_assignable
#ifdef _MSC_VER
#pragma warning(push)
// turn off some warnings that are noisy about our Expects statements
#pragma warning(disable : 4127) // conditional expression is constant
#pragma warning(disable : 4996) // unsafe use of std::copy_n
// blanket turn off warnings from CppCoreCheck for now
// so people aren't annoyed by them when running the tool.
// more targeted suppressions will be added in a future update to the GSL
#pragma warning(disable : 26481 26482 26483 26485 26490 26491 26492 26493 26495)
#endif // _MSC_VER
namespace gsl
{
template <class SrcElementType, std::ptrdiff_t SrcExtent, class DestElementType,
std::ptrdiff_t DestExtent>
void copy(span<SrcElementType, SrcExtent> src, span<DestElementType, DestExtent> dest)
{
static_assert(std::is_assignable<decltype(*dest.data()), decltype(*src.data())>::value,
"Elements of source span can not be assigned to elements of destination span");
static_assert(SrcExtent == dynamic_extent || DestExtent == dynamic_extent ||
(SrcExtent <= DestExtent),
"Source range is longer than target range");
Expects(dest.size() >= src.size());
std::copy_n(src.data(), src.size(), dest.data());
}
} // namespace gsl
#ifdef _MSC_VER
#pragma warning(pop)
#endif // _MSC_VER
#endif // GSL_ALGORITHM_H

145
third_party/gsl/gsl_assert vendored Normal file
View File

@ -0,0 +1,145 @@
///////////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2015 Microsoft Corporation. All rights reserved.
//
// This code is licensed under the MIT License (MIT).
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
///////////////////////////////////////////////////////////////////////////////
#ifndef GSL_CONTRACTS_H
#define GSL_CONTRACTS_H
#include <exception>
#include <stdexcept> // for logic_error
//
// Temporary until MSVC STL supports no-exceptions mode.
// Currently terminate is a no-op in this mode, so we add termination behavior back
//
#if defined(_MSC_VER) && defined(_HAS_EXCEPTIONS) && !_HAS_EXCEPTIONS
#define GSL_MSVC_USE_STL_NOEXCEPTION_WORKAROUND
#endif
//
// There are three configuration options for this GSL implementation's behavior
// when pre/post conditions on the GSL types are violated:
//
// 1. GSL_TERMINATE_ON_CONTRACT_VIOLATION: std::terminate will be called (default)
// 2. GSL_THROW_ON_CONTRACT_VIOLATION: a gsl::fail_fast exception will be thrown
// 3. GSL_UNENFORCED_ON_CONTRACT_VIOLATION: nothing happens
//
#if !(defined(GSL_THROW_ON_CONTRACT_VIOLATION) || defined(GSL_TERMINATE_ON_CONTRACT_VIOLATION) || \
defined(GSL_UNENFORCED_ON_CONTRACT_VIOLATION))
#define GSL_TERMINATE_ON_CONTRACT_VIOLATION
#endif
#define GSL_STRINGIFY_DETAIL(x) #x
#define GSL_STRINGIFY(x) GSL_STRINGIFY_DETAIL(x)
#if defined(__clang__) || defined(__GNUC__)
#define GSL_LIKELY(x) __builtin_expect(!!(x), 1)
#define GSL_UNLIKELY(x) __builtin_expect(!!(x), 0)
#else
#define GSL_LIKELY(x) (!!(x))
#define GSL_UNLIKELY(x) (!!(x))
#endif
//
// GSL_ASSUME(cond)
//
// Tell the optimizer that the predicate cond must hold. It is unspecified
// whether or not cond is actually evaluated.
//
#ifdef _MSC_VER
#define GSL_ASSUME(cond) __assume(cond)
#elif defined(__GNUC__)
#define GSL_ASSUME(cond) ((cond) ? static_cast<void>(0) : __builtin_unreachable())
#else
#define GSL_ASSUME(cond) static_cast<void>((cond) ? 0 : 0)
#endif
//
// GSL.assert: assertions
//
namespace gsl
{
struct fail_fast : public std::logic_error
{
explicit fail_fast(char const* const message) : std::logic_error(message) {}
};
namespace details
{
#if defined(GSL_MSVC_USE_STL_NOEXCEPTION_WORKAROUND)
typedef void (__cdecl *terminate_handler)();
inline gsl::details::terminate_handler& get_terminate_handler() noexcept
{
static terminate_handler handler = &abort;
return handler;
}
#endif
[[noreturn]] inline void terminate() noexcept
{
#if defined(GSL_MSVC_USE_STL_NOEXCEPTION_WORKAROUND)
(*gsl::details::get_terminate_handler())();
#else
std::terminate();
#endif
}
#if defined(GSL_TERMINATE_ON_CONTRACT_VIOLATION)
template <typename Exception>
[[noreturn]] void throw_exception(Exception&&)
{
gsl::details::terminate();
}
#else
template <typename Exception>
[[noreturn]] void throw_exception(Exception&& exception)
{
throw std::forward<Exception>(exception);
}
#endif
} // namespace details
} // namespace gsl
#if defined(GSL_THROW_ON_CONTRACT_VIOLATION)
#define GSL_CONTRACT_CHECK(type, cond) \
(GSL_LIKELY(cond) ? static_cast<void>(0) \
: gsl::details::throw_exception(gsl::fail_fast( \
"GSL: " type " failure at " __FILE__ ": " GSL_STRINGIFY(__LINE__))))
#elif defined(GSL_TERMINATE_ON_CONTRACT_VIOLATION)
#define GSL_CONTRACT_CHECK(type, cond) \
(GSL_LIKELY(cond) ? static_cast<void>(0) : gsl::details::terminate())
#elif defined(GSL_UNENFORCED_ON_CONTRACT_VIOLATION)
#define GSL_CONTRACT_CHECK(type, cond) GSL_ASSUME(cond)
#endif
#define Expects(cond) GSL_CONTRACT_CHECK("Precondition", cond)
#define Ensures(cond) GSL_CONTRACT_CHECK("Postcondition", cond)
#endif // GSL_CONTRACTS_H

181
third_party/gsl/gsl_byte vendored Normal file
View File

@ -0,0 +1,181 @@
///////////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2015 Microsoft Corporation. All rights reserved.
//
// This code is licensed under the MIT License (MIT).
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
///////////////////////////////////////////////////////////////////////////////
#ifndef GSL_BYTE_H
#define GSL_BYTE_H
#include <type_traits>
#ifdef _MSC_VER
#pragma warning(push)
// don't warn about function style casts in byte related operators
#pragma warning(disable : 26493)
#ifndef GSL_USE_STD_BYTE
// this tests if we are under MSVC and the standard lib has std::byte and it is enabled
#if defined(_HAS_STD_BYTE) && _HAS_STD_BYTE
#define GSL_USE_STD_BYTE 1
#else // defined(_HAS_STD_BYTE) && _HAS_STD_BYTE
#define GSL_USE_STD_BYTE 0
#endif // defined(_HAS_STD_BYTE) && _HAS_STD_BYTE
#endif // GSL_USE_STD_BYTE
#else // _MSC_VER
#ifndef GSL_USE_STD_BYTE
// this tests if we are under GCC or Clang with enough -std:c++1z power to get us std::byte
#if defined(__cplusplus) && (__cplusplus >= 201703L)
#define GSL_USE_STD_BYTE 1
#include <cstddef>
#else // defined(__cplusplus) && (__cplusplus >= 201703L)
#define GSL_USE_STD_BYTE 0
#endif //defined(__cplusplus) && (__cplusplus >= 201703L)
#endif // GSL_USE_STD_BYTE
#endif // _MSC_VER
// Use __may_alias__ attribute on gcc and clang
#if defined __clang__ || (__GNUC__ > 5)
#define byte_may_alias __attribute__((__may_alias__))
#else // defined __clang__ || defined __GNUC__
#define byte_may_alias
#endif // defined __clang__ || defined __GNUC__
namespace gsl
{
#if GSL_USE_STD_BYTE
using std::byte;
using std::to_integer;
#else // GSL_USE_STD_BYTE
// This is a simple definition for now that allows
// use of byte within span<> to be standards-compliant
enum class byte_may_alias byte : unsigned char
{
};
template <class IntegerType, class = std::enable_if_t<std::is_integral<IntegerType>::value>>
constexpr byte& operator<<=(byte& b, IntegerType shift) noexcept
{
return b = byte(static_cast<unsigned char>(b) << shift);
}
template <class IntegerType, class = std::enable_if_t<std::is_integral<IntegerType>::value>>
constexpr byte operator<<(byte b, IntegerType shift) noexcept
{
return byte(static_cast<unsigned char>(b) << shift);
}
template <class IntegerType, class = std::enable_if_t<std::is_integral<IntegerType>::value>>
constexpr byte& operator>>=(byte& b, IntegerType shift) noexcept
{
return b = byte(static_cast<unsigned char>(b) >> shift);
}
template <class IntegerType, class = std::enable_if_t<std::is_integral<IntegerType>::value>>
constexpr byte operator>>(byte b, IntegerType shift) noexcept
{
return byte(static_cast<unsigned char>(b) >> shift);
}
constexpr byte& operator|=(byte& l, byte r) noexcept
{
return l = byte(static_cast<unsigned char>(l) | static_cast<unsigned char>(r));
}
constexpr byte operator|(byte l, byte r) noexcept
{
return byte(static_cast<unsigned char>(l) | static_cast<unsigned char>(r));
}
constexpr byte& operator&=(byte& l, byte r) noexcept
{
return l = byte(static_cast<unsigned char>(l) & static_cast<unsigned char>(r));
}
constexpr byte operator&(byte l, byte r) noexcept
{
return byte(static_cast<unsigned char>(l) & static_cast<unsigned char>(r));
}
constexpr byte& operator^=(byte& l, byte r) noexcept
{
return l = byte(static_cast<unsigned char>(l) ^ static_cast<unsigned char>(r));
}
constexpr byte operator^(byte l, byte r) noexcept
{
return byte(static_cast<unsigned char>(l) ^ static_cast<unsigned char>(r));
}
constexpr byte operator~(byte b) noexcept { return byte(~static_cast<unsigned char>(b)); }
template <class IntegerType, class = std::enable_if_t<std::is_integral<IntegerType>::value>>
constexpr IntegerType to_integer(byte b) noexcept
{
return static_cast<IntegerType>(b);
}
#endif // GSL_USE_STD_BYTE
template <bool E, typename T>
constexpr byte to_byte_impl(T t) noexcept
{
static_assert(
E, "gsl::to_byte(t) must be provided an unsigned char, otherwise data loss may occur. "
"If you are calling to_byte with an integer contant use: gsl::to_byte<t>() version.");
return static_cast<byte>(t);
}
template <>
constexpr byte to_byte_impl<true, unsigned char>(unsigned char t) noexcept
{
return byte(t);
}
template <typename T>
constexpr byte to_byte(T t) noexcept
{
return to_byte_impl<std::is_same<T, unsigned char>::value, T>(t);
}
template <int I>
constexpr byte to_byte() noexcept
{
static_assert(I >= 0 && I <= 255,
"gsl::byte only has 8 bits of storage, values must be in range 0-255");
return static_cast<byte>(I);
}
} // namespace gsl
#ifdef _MSC_VER
#pragma warning(pop)
#endif // _MSC_VER
#endif // GSL_BYTE_H

158
third_party/gsl/gsl_util vendored Normal file
View File

@ -0,0 +1,158 @@
///////////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2015 Microsoft Corporation. All rights reserved.
//
// This code is licensed under the MIT License (MIT).
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
///////////////////////////////////////////////////////////////////////////////
#ifndef GSL_UTIL_H
#define GSL_UTIL_H
#include <gsl/gsl_assert> // for Expects
#include <array>
#include <cstddef> // for ptrdiff_t, size_t
#include <exception> // for exception
#include <initializer_list> // for initializer_list
#include <type_traits> // for is_signed, integral_constant
#include <utility> // for forward
#if defined(_MSC_VER)
#pragma warning(push)
#pragma warning(disable : 4127) // conditional expression is constant
#if _MSC_VER < 1910
#pragma push_macro("constexpr")
#define constexpr /*constexpr*/
#endif // _MSC_VER < 1910
#endif // _MSC_VER
namespace gsl
{
//
// GSL.util: utilities
//
// index type for all container indexes/subscripts/sizes
using index = std::ptrdiff_t;
// final_action allows you to ensure something gets run at the end of a scope
template <class F>
class final_action
{
public:
explicit final_action(F f) noexcept : f_(std::move(f)) {}
final_action(final_action&& other) noexcept : f_(std::move(other.f_)), invoke_(other.invoke_)
{
other.invoke_ = false;
}
final_action(const final_action&) = delete;
final_action& operator=(const final_action&) = delete;
final_action& operator=(final_action&&) = delete;
~final_action() noexcept
{
if (invoke_) f_();
}
private:
F f_;
bool invoke_ {true};
};
// finally() - convenience function to generate a final_action
template <class F>
final_action<F> finally(const F& f) noexcept
{
return final_action<F>(f);
}
template <class F>
final_action<F> finally(F&& f) noexcept
{
return final_action<F>(std::forward<F>(f));
}
// narrow_cast(): a searchable way to do narrowing casts of values
template <class T, class U>
constexpr T narrow_cast(U&& u) noexcept
{
return static_cast<T>(std::forward<U>(u));
}
struct narrowing_error : public std::exception
{
};
namespace details
{
template <class T, class U>
struct is_same_signedness
: public std::integral_constant<bool, std::is_signed<T>::value == std::is_signed<U>::value>
{
};
}
// narrow() : a checked version of narrow_cast() that throws if the cast changed the value
template <class T, class U>
T narrow(U u)
{
T t = narrow_cast<T>(u);
if (static_cast<U>(t) != u) gsl::details::throw_exception(narrowing_error());
if (!details::is_same_signedness<T, U>::value && ((t < T{}) != (u < U{})))
gsl::details::throw_exception(narrowing_error());
return t;
}
//
// at() - Bounds-checked way of accessing builtin arrays, std::array, std::vector
//
template <class T, std::size_t N>
constexpr T& at(T (&arr)[N], const index i)
{
Expects(i >= 0 && i < narrow_cast<index>(N));
return arr[static_cast<std::size_t>(i)];
}
template <class Cont>
constexpr auto at(Cont& cont, const index i) -> decltype(cont[cont.size()])
{
Expects(i >= 0 && i < narrow_cast<index>(cont.size()));
using size_type = decltype(cont.size());
return cont[static_cast<size_type>(i)];
}
template <class T>
constexpr T at(const std::initializer_list<T> cont, const index i)
{
Expects(i >= 0 && i < narrow_cast<index>(cont.size()));
return *(cont.begin() + i);
}
} // namespace gsl
#if defined(_MSC_VER)
#if _MSC_VER < 1910
#undef constexpr
#pragma pop_macro("constexpr")
#endif // _MSC_VER < 1910
#pragma warning(pop)
#endif // _MSC_VER
#endif // GSL_UTIL_H

2242
third_party/gsl/multi_span vendored Normal file

File diff suppressed because it is too large Load Diff

193
third_party/gsl/pointers vendored Normal file
View File

@ -0,0 +1,193 @@
///////////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2015 Microsoft Corporation. All rights reserved.
//
// This code is licensed under the MIT License (MIT).
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
///////////////////////////////////////////////////////////////////////////////
#ifndef GSL_POINTERS_H
#define GSL_POINTERS_H
#include <gsl/gsl_assert> // for Ensures, Expects
#include <algorithm> // for forward
#include <iosfwd> // for ptrdiff_t, nullptr_t, ostream, size_t
#include <memory> // for shared_ptr, unique_ptr
#include <system_error> // for hash
#include <type_traits> // for enable_if_t, is_convertible, is_assignable
#if defined(_MSC_VER) && _MSC_VER < 1910
#pragma push_macro("constexpr")
#define constexpr /*constexpr*/
#endif // defined(_MSC_VER) && _MSC_VER < 1910
namespace gsl
{
//
// GSL.owner: ownership pointers
//
using std::unique_ptr;
using std::shared_ptr;
//
// owner
//
// owner<T> is designed as a bridge for code that must deal directly with owning pointers for some reason
//
// T must be a pointer type
// - disallow construction from any type other than pointer type
//
template <class T, class = std::enable_if_t<std::is_pointer<T>::value>>
using owner = T;
//
// not_null
//
// Restricts a pointer or smart pointer to only hold non-null values.
//
// Has zero size overhead over T.
//
// If T is a pointer (i.e. T == U*) then
// - allow construction from U*
// - disallow construction from nullptr_t
// - disallow default construction
// - ensure construction from null U* fails
// - allow implicit conversion to U*
//
template <class T>
class not_null
{
public:
static_assert(std::is_assignable<T&, std::nullptr_t>::value, "T cannot be assigned nullptr.");
template <typename U, typename = std::enable_if_t<std::is_convertible<U, T>::value>>
constexpr explicit not_null(U&& u) : ptr_(std::forward<U>(u))
{
Expects(ptr_ != nullptr);
}
template <typename = std::enable_if_t<!std::is_same<std::nullptr_t, T>::value>>
constexpr explicit not_null(T u) : ptr_(u)
{
Expects(ptr_ != nullptr);
}
template <typename U, typename = std::enable_if_t<std::is_convertible<U, T>::value>>
constexpr not_null(const not_null<U>& other) : not_null(other.get())
{
}
not_null(not_null&& other) = default;
not_null(const not_null& other) = default;
not_null& operator=(const not_null& other) = default;
constexpr T get() const
{
Ensures(ptr_ != nullptr);
return ptr_;
}
constexpr operator T() const { return get(); }
constexpr T operator->() const { return get(); }
constexpr decltype(auto) operator*() const { return *get(); }
// prevents compilation when someone attempts to assign a null pointer constant
not_null(std::nullptr_t) = delete;
not_null& operator=(std::nullptr_t) = delete;
// unwanted operators...pointers only point to single objects!
not_null& operator++() = delete;
not_null& operator--() = delete;
not_null operator++(int) = delete;
not_null operator--(int) = delete;
not_null& operator+=(std::ptrdiff_t) = delete;
not_null& operator-=(std::ptrdiff_t) = delete;
void operator[](std::ptrdiff_t) const = delete;
private:
T ptr_;
};
template <class T>
std::ostream& operator<<(std::ostream& os, const not_null<T>& val)
{
os << val.get();
return os;
}
template <class T, class U>
auto operator==(const not_null<T>& lhs, const not_null<U>& rhs) -> decltype(lhs.get() == rhs.get())
{
return lhs.get() == rhs.get();
}
template <class T, class U>
auto operator!=(const not_null<T>& lhs, const not_null<U>& rhs) -> decltype(lhs.get() != rhs.get())
{
return lhs.get() != rhs.get();
}
template <class T, class U>
auto operator<(const not_null<T>& lhs, const not_null<U>& rhs) -> decltype(lhs.get() < rhs.get())
{
return lhs.get() < rhs.get();
}
template <class T, class U>
auto operator<=(const not_null<T>& lhs, const not_null<U>& rhs) -> decltype(lhs.get() <= rhs.get())
{
return lhs.get() <= rhs.get();
}
template <class T, class U>
auto operator>(const not_null<T>& lhs, const not_null<U>& rhs) -> decltype(lhs.get() > rhs.get())
{
return lhs.get() > rhs.get();
}
template <class T, class U>
auto operator>=(const not_null<T>& lhs, const not_null<U>& rhs) -> decltype(lhs.get() >= rhs.get())
{
return lhs.get() >= rhs.get();
}
// more unwanted operators
template <class T, class U>
std::ptrdiff_t operator-(const not_null<T>&, const not_null<U>&) = delete;
template <class T>
not_null<T> operator-(const not_null<T>&, std::ptrdiff_t) = delete;
template <class T>
not_null<T> operator+(const not_null<T>&, std::ptrdiff_t) = delete;
template <class T>
not_null<T> operator+(std::ptrdiff_t, const not_null<T>&) = delete;
} // namespace gsl
namespace std
{
template <class T>
struct hash<gsl::not_null<T>>
{
std::size_t operator()(const gsl::not_null<T>& value) const { return hash<T>{}(value); }
};
} // namespace std
#if defined(_MSC_VER) && _MSC_VER < 1910
#undef constexpr
#pragma pop_macro("constexpr")
#endif // defined(_MSC_VER) && _MSC_VER < 1910
#endif // GSL_POINTERS_H

766
third_party/gsl/span vendored Normal file
View File

@ -0,0 +1,766 @@
///////////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2015 Microsoft Corporation. All rights reserved.
//
// This code is licensed under the MIT License (MIT).
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
///////////////////////////////////////////////////////////////////////////////
#ifndef GSL_SPAN_H
#define GSL_SPAN_H
#include <gsl/gsl_assert> // for Expects
#include <gsl/gsl_byte> // for byte
#include <gsl/gsl_util> // for narrow_cast, narrow
#include <algorithm> // for lexicographical_compare
#include <array> // for array
#include <cstddef> // for ptrdiff_t, size_t, nullptr_t
#include <iterator> // for reverse_iterator, distance, random_access_...
#include <limits>
#include <stdexcept>
#include <type_traits> // for enable_if_t, declval, is_convertible, inte...
#include <utility>
#ifdef _MSC_VER
#pragma warning(push)
// turn off some warnings that are noisy about our Expects statements
#pragma warning(disable : 4127) // conditional expression is constant
#pragma warning(disable : 4702) // unreachable code
// blanket turn off warnings from CppCoreCheck for now
// so people aren't annoyed by them when running the tool.
// more targeted suppressions will be added in a future update to the GSL
#pragma warning(disable : 26481 26482 26483 26485 26490 26491 26492 26493 26495)
#if _MSC_VER < 1910
#pragma push_macro("constexpr")
#define constexpr /*constexpr*/
#define GSL_USE_STATIC_CONSTEXPR_WORKAROUND
#endif // _MSC_VER < 1910
#else // _MSC_VER
// See if we have enough C++17 power to use a static constexpr data member
// without needing an out-of-line definition
#if !(defined(__cplusplus) && (__cplusplus >= 201703L))
#define GSL_USE_STATIC_CONSTEXPR_WORKAROUND
#endif // !(defined(__cplusplus) && (__cplusplus >= 201703L))
#endif // _MSC_VER
// GCC 7 does not like the signed unsigned missmatch (size_t ptrdiff_t)
// While there is a conversion from signed to unsigned, it happens at
// compiletime, so the compiler wouldn't have to warn indiscriminently, but
// could check if the source value actually doesn't fit into the target type
// and only warn in those cases.
#if __GNUC__ > 6
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wsign-conversion"
#endif
namespace gsl
{
// [views.constants], constants
constexpr const std::ptrdiff_t dynamic_extent = -1;
template <class ElementType, std::ptrdiff_t Extent = dynamic_extent>
class span;
// implementation details
namespace details
{
template <class T>
struct is_span_oracle : std::false_type
{
};
template <class ElementType, std::ptrdiff_t Extent>
struct is_span_oracle<gsl::span<ElementType, Extent>> : std::true_type
{
};
template <class T>
struct is_span : public is_span_oracle<std::remove_cv_t<T>>
{
};
template <class T>
struct is_std_array_oracle : std::false_type
{
};
template <class ElementType, std::size_t Extent>
struct is_std_array_oracle<std::array<ElementType, Extent>> : std::true_type
{
};
template <class T>
struct is_std_array : public is_std_array_oracle<std::remove_cv_t<T>>
{
};
template <std::ptrdiff_t From, std::ptrdiff_t To>
struct is_allowed_extent_conversion
: public std::integral_constant<bool, From == To || From == gsl::dynamic_extent ||
To == gsl::dynamic_extent>
{
};
template <class From, class To>
struct is_allowed_element_type_conversion
: public std::integral_constant<bool, std::is_convertible<From (*)[], To (*)[]>::value>
{
};
template <class Span, bool IsConst>
class span_iterator
{
using element_type_ = typename Span::element_type;
public:
#ifdef _MSC_VER
// Tell Microsoft standard library that span_iterators are checked.
using _Unchecked_type = typename Span::pointer;
#endif
using iterator_category = std::random_access_iterator_tag;
using value_type = std::remove_cv_t<element_type_>;
using difference_type = typename Span::index_type;
using reference = std::conditional_t<IsConst, const element_type_, element_type_>&;
using pointer = std::add_pointer_t<reference>;
span_iterator() = default;
constexpr span_iterator(const Span* span, typename Span::index_type idx) noexcept
: span_(span), index_(idx)
{}
friend span_iterator<Span, true>;
template<bool B, std::enable_if_t<!B && IsConst>* = nullptr>
constexpr span_iterator(const span_iterator<Span, B>& other) noexcept
: span_iterator(other.span_, other.index_)
{
}
constexpr reference operator*() const
{
Expects(index_ != span_->size());
return *(span_->data() + index_);
}
constexpr pointer operator->() const
{
Expects(index_ != span_->size());
return span_->data() + index_;
}
constexpr span_iterator& operator++()
{
Expects(0 <= index_ && index_ != span_->size());
++index_;
return *this;
}
constexpr span_iterator operator++(int)
{
auto ret = *this;
++(*this);
return ret;
}
constexpr span_iterator& operator--()
{
Expects(index_ != 0 && index_ <= span_->size());
--index_;
return *this;
}
constexpr span_iterator operator--(int)
{
auto ret = *this;
--(*this);
return ret;
}
constexpr span_iterator operator+(difference_type n) const
{
auto ret = *this;
return ret += n;
}
friend constexpr span_iterator operator+(difference_type n, span_iterator const& rhs)
{
return rhs + n;
}
constexpr span_iterator& operator+=(difference_type n)
{
Expects((index_ + n) >= 0 && (index_ + n) <= span_->size());
index_ += n;
return *this;
}
constexpr span_iterator operator-(difference_type n) const
{
auto ret = *this;
return ret -= n;
}
constexpr span_iterator& operator-=(difference_type n) { return *this += -n; }
constexpr difference_type operator-(span_iterator rhs) const
{
Expects(span_ == rhs.span_);
return index_ - rhs.index_;
}
constexpr reference operator[](difference_type n) const
{
return *(*this + n);
}
constexpr friend bool operator==(span_iterator lhs,
span_iterator rhs) noexcept
{
return lhs.span_ == rhs.span_ && lhs.index_ == rhs.index_;
}
constexpr friend bool operator!=(span_iterator lhs,
span_iterator rhs) noexcept
{
return !(lhs == rhs);
}
constexpr friend bool operator<(span_iterator lhs,
span_iterator rhs) noexcept
{
return lhs.index_ < rhs.index_;
}
constexpr friend bool operator<=(span_iterator lhs,
span_iterator rhs) noexcept
{
return !(rhs < lhs);
}
constexpr friend bool operator>(span_iterator lhs,
span_iterator rhs) noexcept
{
return rhs < lhs;
}
constexpr friend bool operator>=(span_iterator lhs,
span_iterator rhs) noexcept
{
return !(rhs > lhs);
}
#ifdef _MSC_VER
// MSVC++ iterator debugging support; allows STL algorithms in 15.8+
// to unwrap span_iterator to a pointer type after a range check in STL
// algorithm calls
friend constexpr void _Verify_range(span_iterator lhs,
span_iterator rhs) noexcept
{ // test that [lhs, rhs) forms a valid range inside an STL algorithm
Expects(lhs.span_ == rhs.span_ // range spans have to match
&& lhs.index_ <= rhs.index_); // range must not be transposed
}
constexpr void _Verify_offset(const difference_type n) const noexcept
{ // test that the iterator *this + n is a valid range in an STL
// algorithm call
Expects((index_ + n) >= 0 && (index_ + n) <= span_->size());
}
constexpr pointer _Unwrapped() const noexcept
{ // after seeking *this to a high water mark, or using one of the
// _Verify_xxx functions above, unwrap this span_iterator to a raw
// pointer
return span_->data() + index_;
}
// Tell the STL that span_iterator should not be unwrapped if it can't
// validate in advance, even in release / optimized builds:
#if defined(GSL_USE_STATIC_CONSTEXPR_WORKAROUND)
static constexpr const bool _Unwrap_when_unverified = false;
#else
static constexpr bool _Unwrap_when_unverified = false;
#endif
constexpr void _Seek_to(const pointer p) noexcept
{ // adjust the position of *this to previously verified location p
// after _Unwrapped
index_ = p - span_->data();
}
#endif
protected:
const Span* span_ = nullptr;
std::ptrdiff_t index_ = 0;
};
template <std::ptrdiff_t Ext>
class extent_type
{
public:
using index_type = std::ptrdiff_t;
static_assert(Ext >= 0, "A fixed-size span must be >= 0 in size.");
constexpr extent_type() noexcept {}
template <index_type Other>
constexpr extent_type(extent_type<Other> ext)
{
static_assert(Other == Ext || Other == dynamic_extent,
"Mismatch between fixed-size extent and size of initializing data.");
Expects(ext.size() == Ext);
}
constexpr extent_type(index_type size) { Expects(size == Ext); }
constexpr index_type size() const noexcept { return Ext; }
};
template <>
class extent_type<dynamic_extent>
{
public:
using index_type = std::ptrdiff_t;
template <index_type Other>
explicit constexpr extent_type(extent_type<Other> ext) : size_(ext.size())
{
}
explicit constexpr extent_type(index_type size) : size_(size) { Expects(size >= 0); }
constexpr index_type size() const noexcept { return size_; }
private:
index_type size_;
};
template <class ElementType, std::ptrdiff_t Extent, std::ptrdiff_t Offset, std::ptrdiff_t Count>
struct calculate_subspan_type
{
using type = span<ElementType, Count != dynamic_extent ? Count : (Extent != dynamic_extent ? Extent - Offset : Extent)>;
};
} // namespace details
// [span], class template span
template <class ElementType, std::ptrdiff_t Extent>
class span
{
public:
// constants and types
using element_type = ElementType;
using value_type = std::remove_cv_t<ElementType>;
using index_type = std::ptrdiff_t;
using pointer = element_type*;
using reference = element_type&;
using iterator = details::span_iterator<span<ElementType, Extent>, false>;
using const_iterator = details::span_iterator<span<ElementType, Extent>, true>;
using reverse_iterator = std::reverse_iterator<iterator>;
using const_reverse_iterator = std::reverse_iterator<const_iterator>;
using size_type = index_type;
#if defined(GSL_USE_STATIC_CONSTEXPR_WORKAROUND)
static constexpr const index_type extent { Extent };
#else
static constexpr index_type extent { Extent };
#endif
// [span.cons], span constructors, copy, assignment, and destructor
template <bool Dependent = false,
// "Dependent" is needed to make "std::enable_if_t<Dependent || Extent <= 0>" SFINAE,
// since "std::enable_if_t<Extent <= 0>" is ill-formed when Extent is greater than 0.
class = std::enable_if_t<(Dependent || Extent <= 0)>>
constexpr span() noexcept : storage_(nullptr, details::extent_type<0>())
{
}
constexpr span(pointer ptr, index_type count) : storage_(ptr, count) {}
constexpr span(pointer firstElem, pointer lastElem)
: storage_(firstElem, std::distance(firstElem, lastElem))
{
}
template <std::size_t N>
constexpr span(element_type (&arr)[N]) noexcept
: storage_(KnownNotNull{&arr[0]}, details::extent_type<N>())
{
}
template <std::size_t N, class ArrayElementType = std::remove_const_t<element_type>>
constexpr span(std::array<ArrayElementType, N>& arr) noexcept
: storage_(&arr[0], details::extent_type<N>())
{
}
template <std::size_t N>
constexpr span(const std::array<std::remove_const_t<element_type>, N>& arr) noexcept
: storage_(&arr[0], details::extent_type<N>())
{
}
// NB: the SFINAE here uses .data() as a incomplete/imperfect proxy for the requirement
// on Container to be a contiguous sequence container.
template <class Container,
class = std::enable_if_t<
!details::is_span<Container>::value && !details::is_std_array<Container>::value &&
std::is_convertible<typename Container::pointer, pointer>::value &&
std::is_convertible<typename Container::pointer,
decltype(std::declval<Container>().data())>::value>>
constexpr span(Container& cont) : span(cont.data(), narrow<index_type>(cont.size()))
{
}
template <class Container,
class = std::enable_if_t<
std::is_const<element_type>::value && !details::is_span<Container>::value &&
std::is_convertible<typename Container::pointer, pointer>::value &&
std::is_convertible<typename Container::pointer,
decltype(std::declval<Container>().data())>::value>>
constexpr span(const Container& cont) : span(cont.data(), narrow<index_type>(cont.size()))
{
}
constexpr span(const span& other) noexcept = default;
template <
class OtherElementType, std::ptrdiff_t OtherExtent,
class = std::enable_if_t<
details::is_allowed_extent_conversion<OtherExtent, Extent>::value &&
details::is_allowed_element_type_conversion<OtherElementType, element_type>::value>>
constexpr span(const span<OtherElementType, OtherExtent>& other)
: storage_(other.data(), details::extent_type<OtherExtent>(other.size()))
{
}
~span() noexcept = default;
constexpr span& operator=(const span& other) noexcept = default;
// [span.sub], span subviews
template <std::ptrdiff_t Count>
constexpr span<element_type, Count> first() const
{
Expects(Count >= 0 && Count <= size());
return {data(), Count};
}
template <std::ptrdiff_t Count>
constexpr span<element_type, Count> last() const
{
Expects(Count >= 0 && size() - Count >= 0);
return {data() + (size() - Count), Count};
}
template <std::ptrdiff_t Offset, std::ptrdiff_t Count = dynamic_extent>
constexpr auto subspan() const -> typename details::calculate_subspan_type<ElementType, Extent, Offset, Count>::type
{
Expects((Offset >= 0 && size() - Offset >= 0) &&
(Count == dynamic_extent || (Count >= 0 && Offset + Count <= size())));
return {data() + Offset, Count == dynamic_extent ? size() - Offset : Count};
}
constexpr span<element_type, dynamic_extent> first(index_type count) const
{
Expects(count >= 0 && count <= size());
return {data(), count};
}
constexpr span<element_type, dynamic_extent> last(index_type count) const
{
return make_subspan(size() - count, dynamic_extent, subspan_selector<Extent>{});
}
constexpr span<element_type, dynamic_extent> subspan(index_type offset,
index_type count = dynamic_extent) const
{
return make_subspan(offset, count, subspan_selector<Extent>{});
}
// [span.obs], span observers
constexpr index_type size() const noexcept { return storage_.size(); }
constexpr index_type size_bytes() const noexcept
{
return size() * narrow_cast<index_type>(sizeof(element_type));
}
constexpr bool empty() const noexcept { return size() == 0; }
// [span.elem], span element access
constexpr reference operator[](index_type idx) const
{
Expects(idx >= 0 && idx < storage_.size());
return data()[idx];
}
constexpr reference at(index_type idx) const { return this->operator[](idx); }
constexpr reference operator()(index_type idx) const { return this->operator[](idx); }
constexpr pointer data() const noexcept { return storage_.data(); }
// [span.iter], span iterator support
constexpr iterator begin() const noexcept { return {this, 0}; }
constexpr iterator end() const noexcept { return {this, size()}; }
constexpr const_iterator cbegin() const noexcept { return {this, 0}; }
constexpr const_iterator cend() const noexcept { return {this, size()}; }
constexpr reverse_iterator rbegin() const noexcept { return reverse_iterator{end()}; }
constexpr reverse_iterator rend() const noexcept { return reverse_iterator{begin()}; }
constexpr const_reverse_iterator crbegin() const noexcept { return const_reverse_iterator{cend()}; }
constexpr const_reverse_iterator crend() const noexcept { return const_reverse_iterator{cbegin()}; }
#ifdef _MSC_VER
// Tell MSVC how to unwrap spans in range-based-for
constexpr pointer _Unchecked_begin() const noexcept { return data(); }
constexpr pointer _Unchecked_end() const noexcept { return data() + size(); }
#endif // _MSC_VER
private:
// Needed to remove unnecessary null check in subspans
struct KnownNotNull
{
pointer p;
};
// this implementation detail class lets us take advantage of the
// empty base class optimization to pay for only storage of a single
// pointer in the case of fixed-size spans
template <class ExtentType>
class storage_type : public ExtentType
{
public:
// KnownNotNull parameter is needed to remove unnecessary null check
// in subspans and constructors from arrays
template <class OtherExtentType>
constexpr storage_type(KnownNotNull data, OtherExtentType ext) : ExtentType(ext), data_(data.p)
{
Expects(ExtentType::size() >= 0);
}
template <class OtherExtentType>
constexpr storage_type(pointer data, OtherExtentType ext) : ExtentType(ext), data_(data)
{
Expects(ExtentType::size() >= 0);
Expects(data || ExtentType::size() == 0);
}
constexpr pointer data() const noexcept { return data_; }
private:
pointer data_;
};
storage_type<details::extent_type<Extent>> storage_;
// The rest is needed to remove unnecessary null check
// in subspans and constructors from arrays
constexpr span(KnownNotNull ptr, index_type count) : storage_(ptr, count) {}
template <std::ptrdiff_t CallerExtent>
class subspan_selector {};
template <std::ptrdiff_t CallerExtent>
span<element_type, dynamic_extent> make_subspan(index_type offset,
index_type count,
subspan_selector<CallerExtent>) const
{
span<element_type, dynamic_extent> tmp(*this);
return tmp.subspan(offset, count);
}
span<element_type, dynamic_extent> make_subspan(index_type offset,
index_type count,
subspan_selector<dynamic_extent>) const
{
Expects(offset >= 0 && size() - offset >= 0);
if (count == dynamic_extent)
{
return { KnownNotNull{ data() + offset }, size() - offset };
}
Expects(count >= 0 && size() - offset >= count);
return { KnownNotNull{ data() + offset }, count };
}
};
#if defined(GSL_USE_STATIC_CONSTEXPR_WORKAROUND)
template <class ElementType, std::ptrdiff_t Extent>
constexpr const typename span<ElementType, Extent>::index_type span<ElementType, Extent>::extent;
#endif
// [span.comparison], span comparison operators
template <class ElementType, std::ptrdiff_t FirstExtent, std::ptrdiff_t SecondExtent>
constexpr bool operator==(span<ElementType, FirstExtent> l,
span<ElementType, SecondExtent> r)
{
return std::equal(l.begin(), l.end(), r.begin(), r.end());
}
template <class ElementType, std::ptrdiff_t Extent>
constexpr bool operator!=(span<ElementType, Extent> l,
span<ElementType, Extent> r)
{
return !(l == r);
}
template <class ElementType, std::ptrdiff_t Extent>
constexpr bool operator<(span<ElementType, Extent> l,
span<ElementType, Extent> r)
{
return std::lexicographical_compare(l.begin(), l.end(), r.begin(), r.end());
}
template <class ElementType, std::ptrdiff_t Extent>
constexpr bool operator<=(span<ElementType, Extent> l,
span<ElementType, Extent> r)
{
return !(l > r);
}
template <class ElementType, std::ptrdiff_t Extent>
constexpr bool operator>(span<ElementType, Extent> l,
span<ElementType, Extent> r)
{
return r < l;
}
template <class ElementType, std::ptrdiff_t Extent>
constexpr bool operator>=(span<ElementType, Extent> l,
span<ElementType, Extent> r)
{
return !(l < r);
}
namespace details
{
// if we only supported compilers with good constexpr support then
// this pair of classes could collapse down to a constexpr function
// we should use a narrow_cast<> to go to std::size_t, but older compilers may not see it as
// constexpr
// and so will fail compilation of the template
template <class ElementType, std::ptrdiff_t Extent>
struct calculate_byte_size
: std::integral_constant<std::ptrdiff_t,
static_cast<std::ptrdiff_t>(sizeof(ElementType) *
static_cast<std::size_t>(Extent))>
{
};
template <class ElementType>
struct calculate_byte_size<ElementType, dynamic_extent>
: std::integral_constant<std::ptrdiff_t, dynamic_extent>
{
};
}
// [span.objectrep], views of object representation
template <class ElementType, std::ptrdiff_t Extent>
span<const byte, details::calculate_byte_size<ElementType, Extent>::value>
as_bytes(span<ElementType, Extent> s) noexcept
{
return {reinterpret_cast<const byte*>(s.data()), s.size_bytes()};
}
template <class ElementType, std::ptrdiff_t Extent,
class = std::enable_if_t<!std::is_const<ElementType>::value>>
span<byte, details::calculate_byte_size<ElementType, Extent>::value>
as_writeable_bytes(span<ElementType, Extent> s) noexcept
{
return {reinterpret_cast<byte*>(s.data()), s.size_bytes()};
}
//
// make_span() - Utility functions for creating spans
//
template <class ElementType>
constexpr span<ElementType> make_span(ElementType* ptr, typename span<ElementType>::index_type count)
{
return span<ElementType>(ptr, count);
}
template <class ElementType>
constexpr span<ElementType> make_span(ElementType* firstElem, ElementType* lastElem)
{
return span<ElementType>(firstElem, lastElem);
}
template <class ElementType, std::size_t N>
constexpr span<ElementType, N> make_span(ElementType (&arr)[N]) noexcept
{
return span<ElementType, N>(arr);
}
template <class Container>
constexpr span<typename Container::value_type> make_span(Container& cont)
{
return span<typename Container::value_type>(cont);
}
template <class Container>
constexpr span<const typename Container::value_type> make_span(const Container& cont)
{
return span<const typename Container::value_type>(cont);
}
template <class Ptr>
constexpr span<typename Ptr::element_type> make_span(Ptr& cont, std::ptrdiff_t count)
{
return span<typename Ptr::element_type>(cont, count);
}
template <class Ptr>
constexpr span<typename Ptr::element_type> make_span(Ptr& cont)
{
return span<typename Ptr::element_type>(cont);
}
// Specialization of gsl::at for span
template <class ElementType, std::ptrdiff_t Extent>
constexpr ElementType& at(span<ElementType, Extent> s, index i)
{
// No bounds checking here because it is done in span::operator[] called below
return s[i];
}
} // namespace gsl
#ifdef _MSC_VER
#if _MSC_VER < 1910
#undef constexpr
#pragma pop_macro("constexpr")
#endif // _MSC_VER < 1910
#pragma warning(pop)
#endif // _MSC_VER
#if __GNUC__ > 6
#pragma GCC diagnostic pop
#endif // __GNUC__ > 6
#endif // GSL_SPAN_H

730
third_party/gsl/string_span vendored Normal file
View File

@ -0,0 +1,730 @@
///////////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2015 Microsoft Corporation. All rights reserved.
//
// This code is licensed under the MIT License (MIT).
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
///////////////////////////////////////////////////////////////////////////////
#ifndef GSL_STRING_SPAN_H
#define GSL_STRING_SPAN_H
#include <gsl/gsl_assert> // for Ensures, Expects
#include <gsl/gsl_util> // for narrow_cast
#include <gsl/span> // for operator!=, operator==, dynamic_extent
#include <algorithm> // for equal, lexicographical_compare
#include <array> // for array
#include <cstddef> // for ptrdiff_t, size_t, nullptr_t
#include <cstdint> // for PTRDIFF_MAX
#include <cstring>
#include <string> // for basic_string, allocator, char_traits
#include <type_traits> // for declval, is_convertible, enable_if_t, add_...
#ifdef _MSC_VER
#pragma warning(push)
// blanket turn off warnings from CppCoreCheck for now
// so people aren't annoyed by them when running the tool.
// more targeted suppressions will be added in a future update to the GSL
#pragma warning(disable : 26481 26482 26483 26485 26490 26491 26492 26493 26495)
#if _MSC_VER < 1910
#pragma push_macro("constexpr")
#define constexpr /*constexpr*/
#endif // _MSC_VER < 1910
#endif // _MSC_VER
// In order to test the library, we need it to throw exceptions that we can catch
#ifdef GSL_THROW_ON_CONTRACT_VIOLATION
#define GSL_NOEXCEPT /*noexcept*/
#else
#define GSL_NOEXCEPT noexcept
#endif // GSL_THROW_ON_CONTRACT_VIOLATION
namespace gsl
{
//
// czstring and wzstring
//
// These are "tag" typedefs for C-style strings (i.e. null-terminated character arrays)
// that allow static analysis to help find bugs.
//
// There are no additional features/semantics that we can find a way to add inside the
// type system for these types that will not either incur significant runtime costs or
// (sometimes needlessly) break existing programs when introduced.
//
template <typename CharT, std::ptrdiff_t Extent = dynamic_extent>
using basic_zstring = CharT*;
template <std::ptrdiff_t Extent = dynamic_extent>
using czstring = basic_zstring<const char, Extent>;
template <std::ptrdiff_t Extent = dynamic_extent>
using cwzstring = basic_zstring<const wchar_t, Extent>;
template <std::ptrdiff_t Extent = dynamic_extent>
using cu16zstring = basic_zstring<const char16_t, Extent>;
template <std::ptrdiff_t Extent = dynamic_extent>
using cu32zstring = basic_zstring<const char32_t, Extent>;
template <std::ptrdiff_t Extent = dynamic_extent>
using zstring = basic_zstring<char, Extent>;
template <std::ptrdiff_t Extent = dynamic_extent>
using wzstring = basic_zstring<wchar_t, Extent>;
template <std::ptrdiff_t Extent = dynamic_extent>
using u16zstring = basic_zstring<char16_t, Extent>;
template <std::ptrdiff_t Extent = dynamic_extent>
using u32zstring = basic_zstring<char32_t, Extent>;
namespace details
{
template <class CharT>
std::ptrdiff_t string_length(const CharT* str, std::ptrdiff_t n)
{
if (str == nullptr || n <= 0) return 0;
const span<const CharT> str_span{str, n};
std::ptrdiff_t len = 0;
while (len < n && str_span[len]) len++;
return len;
}
}
//
// ensure_sentinel()
//
// Provides a way to obtain an span from a contiguous sequence
// that ends with a (non-inclusive) sentinel value.
//
// Will fail-fast if sentinel cannot be found before max elements are examined.
//
template <typename T, const T Sentinel>
span<T, dynamic_extent> ensure_sentinel(T* seq, std::ptrdiff_t max = PTRDIFF_MAX)
{
auto cur = seq;
while ((cur - seq) < max && *cur != Sentinel) ++cur;
Ensures(*cur == Sentinel);
return {seq, cur - seq};
}
//
// ensure_z - creates a span for a zero terminated strings.
// Will fail fast if a null-terminator cannot be found before
// the limit of size_type.
//
template <typename CharT>
span<CharT, dynamic_extent> ensure_z(CharT* const& sz, std::ptrdiff_t max = PTRDIFF_MAX)
{
return ensure_sentinel<CharT, CharT(0)>(sz, max);
}
template <typename CharT, std::size_t N>
span<CharT, dynamic_extent> ensure_z(CharT (&sz)[N])
{
return ensure_z(&sz[0], static_cast<std::ptrdiff_t>(N));
}
template <class Cont>
span<typename std::remove_pointer<typename Cont::pointer>::type, dynamic_extent>
ensure_z(Cont& cont)
{
return ensure_z(cont.data(), static_cast<std::ptrdiff_t>(cont.size()));
}
template <typename CharT, std::ptrdiff_t>
class basic_string_span;
namespace details
{
template <typename T>
struct is_basic_string_span_oracle : std::false_type
{
};
template <typename CharT, std::ptrdiff_t Extent>
struct is_basic_string_span_oracle<basic_string_span<CharT, Extent>> : std::true_type
{
};
template <typename T>
struct is_basic_string_span : is_basic_string_span_oracle<std::remove_cv_t<T>>
{
};
}
//
// string_span and relatives
//
template <typename CharT, std::ptrdiff_t Extent = dynamic_extent>
class basic_string_span
{
public:
using element_type = CharT;
using pointer = std::add_pointer_t<element_type>;
using reference = std::add_lvalue_reference_t<element_type>;
using const_reference = std::add_lvalue_reference_t<std::add_const_t<element_type>>;
using impl_type = span<element_type, Extent>;
using index_type = typename impl_type::index_type;
using iterator = typename impl_type::iterator;
using const_iterator = typename impl_type::const_iterator;
using reverse_iterator = typename impl_type::reverse_iterator;
using const_reverse_iterator = typename impl_type::const_reverse_iterator;
// default (empty)
constexpr basic_string_span() GSL_NOEXCEPT = default;
// copy
constexpr basic_string_span(const basic_string_span& other) GSL_NOEXCEPT = default;
// assign
constexpr basic_string_span& operator=(const basic_string_span& other) GSL_NOEXCEPT = default;
constexpr basic_string_span(pointer ptr, index_type length) : span_(ptr, length) {}
constexpr basic_string_span(pointer firstElem, pointer lastElem) : span_(firstElem, lastElem) {}
// From static arrays - if 0-terminated, remove 0 from the view
// All other containers allow 0s within the length, so we do not remove them
template <std::size_t N>
constexpr basic_string_span(element_type (&arr)[N]) : span_(remove_z(arr))
{
}
template <std::size_t N, class ArrayElementType = std::remove_const_t<element_type>>
constexpr basic_string_span(std::array<ArrayElementType, N>& arr) GSL_NOEXCEPT : span_(arr)
{
}
template <std::size_t N, class ArrayElementType = std::remove_const_t<element_type>>
constexpr basic_string_span(const std::array<ArrayElementType, N>& arr) GSL_NOEXCEPT
: span_(arr)
{
}
// Container signature should work for basic_string after C++17 version exists
template <class Traits, class Allocator>
constexpr basic_string_span(std::basic_string<element_type, Traits, Allocator>& str)
: span_(&str[0], narrow_cast<std::ptrdiff_t>(str.length()))
{
}
template <class Traits, class Allocator>
constexpr basic_string_span(const std::basic_string<element_type, Traits, Allocator>& str)
: span_(&str[0], str.length())
{
}
// from containers. Containers must have a pointer type and data() function signatures
template <class Container,
class = std::enable_if_t<
!details::is_basic_string_span<Container>::value &&
std::is_convertible<typename Container::pointer, pointer>::value &&
std::is_convertible<typename Container::pointer,
decltype(std::declval<Container>().data())>::value>>
constexpr basic_string_span(Container& cont) : span_(cont)
{
}
template <class Container,
class = std::enable_if_t<
!details::is_basic_string_span<Container>::value &&
std::is_convertible<typename Container::pointer, pointer>::value &&
std::is_convertible<typename Container::pointer,
decltype(std::declval<Container>().data())>::value>>
constexpr basic_string_span(const Container& cont) : span_(cont)
{
}
// from string_span
template <
class OtherValueType, std::ptrdiff_t OtherExtent,
class = std::enable_if_t<std::is_convertible<
typename basic_string_span<OtherValueType, OtherExtent>::impl_type, impl_type>::value>>
constexpr basic_string_span(basic_string_span<OtherValueType, OtherExtent> other)
: span_(other.data(), other.length())
{
}
template <index_type Count>
constexpr basic_string_span<element_type, Count> first() const
{
return {span_.template first<Count>()};
}
constexpr basic_string_span<element_type, dynamic_extent> first(index_type count) const
{
return {span_.first(count)};
}
template <index_type Count>
constexpr basic_string_span<element_type, Count> last() const
{
return {span_.template last<Count>()};
}
constexpr basic_string_span<element_type, dynamic_extent> last(index_type count) const
{
return {span_.last(count)};
}
template <index_type Offset, index_type Count>
constexpr basic_string_span<element_type, Count> subspan() const
{
return {span_.template subspan<Offset, Count>()};
}
constexpr basic_string_span<element_type, dynamic_extent>
subspan(index_type offset, index_type count = dynamic_extent) const
{
return {span_.subspan(offset, count)};
}
constexpr reference operator[](index_type idx) const { return span_[idx]; }
constexpr reference operator()(index_type idx) const { return span_[idx]; }
constexpr pointer data() const { return span_.data(); }
constexpr index_type length() const GSL_NOEXCEPT { return span_.size(); }
constexpr index_type size() const GSL_NOEXCEPT { return span_.size(); }
constexpr index_type size_bytes() const GSL_NOEXCEPT { return span_.size_bytes(); }
constexpr index_type length_bytes() const GSL_NOEXCEPT { return span_.length_bytes(); }
constexpr bool empty() const GSL_NOEXCEPT { return size() == 0; }
constexpr iterator begin() const GSL_NOEXCEPT { return span_.begin(); }
constexpr iterator end() const GSL_NOEXCEPT { return span_.end(); }
constexpr const_iterator cbegin() const GSL_NOEXCEPT { return span_.cbegin(); }
constexpr const_iterator cend() const GSL_NOEXCEPT { return span_.cend(); }
constexpr reverse_iterator rbegin() const GSL_NOEXCEPT { return span_.rbegin(); }
constexpr reverse_iterator rend() const GSL_NOEXCEPT { return span_.rend(); }
constexpr const_reverse_iterator crbegin() const GSL_NOEXCEPT { return span_.crbegin(); }
constexpr const_reverse_iterator crend() const GSL_NOEXCEPT { return span_.crend(); }
private:
static impl_type remove_z(pointer const& sz, std::ptrdiff_t max)
{
return {sz, details::string_length(sz, max)};
}
template <std::size_t N>
static impl_type remove_z(element_type (&sz)[N])
{
return remove_z(&sz[0], narrow_cast<std::ptrdiff_t>(N));
}
impl_type span_;
};
template <std::ptrdiff_t Extent = dynamic_extent>
using string_span = basic_string_span<char, Extent>;
template <std::ptrdiff_t Extent = dynamic_extent>
using cstring_span = basic_string_span<const char, Extent>;
template <std::ptrdiff_t Extent = dynamic_extent>
using wstring_span = basic_string_span<wchar_t, Extent>;
template <std::ptrdiff_t Extent = dynamic_extent>
using cwstring_span = basic_string_span<const wchar_t, Extent>;
template <std::ptrdiff_t Extent = dynamic_extent>
using u16string_span = basic_string_span<char16_t, Extent>;
template <std::ptrdiff_t Extent = dynamic_extent>
using cu16string_span = basic_string_span<const char16_t, Extent>;
template <std::ptrdiff_t Extent = dynamic_extent>
using u32string_span = basic_string_span<char32_t, Extent>;
template <std::ptrdiff_t Extent = dynamic_extent>
using cu32string_span = basic_string_span<const char32_t, Extent>;
//
// to_string() allow (explicit) conversions from string_span to string
//
template <typename CharT, std::ptrdiff_t Extent>
std::basic_string<typename std::remove_const<CharT>::type>
to_string(basic_string_span<CharT, Extent> view)
{
return {view.data(), static_cast<std::size_t>(view.length())};
}
template <typename CharT, typename Traits = typename std::char_traits<CharT>,
typename Allocator = std::allocator<CharT>, typename gCharT, std::ptrdiff_t Extent>
std::basic_string<CharT, Traits, Allocator> to_basic_string(basic_string_span<gCharT, Extent> view)
{
return {view.data(), static_cast<std::size_t>(view.length())};
}
template <class ElementType, std::ptrdiff_t Extent>
basic_string_span<const byte, details::calculate_byte_size<ElementType, Extent>::value>
as_bytes(basic_string_span<ElementType, Extent> s) noexcept
{
return { reinterpret_cast<const byte*>(s.data()), s.size_bytes() };
}
template <class ElementType, std::ptrdiff_t Extent,
class = std::enable_if_t<!std::is_const<ElementType>::value>>
basic_string_span<byte, details::calculate_byte_size<ElementType, Extent>::value>
as_writeable_bytes(basic_string_span<ElementType, Extent> s) noexcept
{
return {reinterpret_cast<byte*>(s.data()), s.size_bytes()};
}
// zero-terminated string span, used to convert
// zero-terminated spans to legacy strings
template <typename CharT, std::ptrdiff_t Extent = dynamic_extent>
class basic_zstring_span
{
public:
using value_type = CharT;
using const_value_type = std::add_const_t<CharT>;
using pointer = std::add_pointer_t<value_type>;
using const_pointer = std::add_pointer_t<const_value_type>;
using zstring_type = basic_zstring<value_type, Extent>;
using const_zstring_type = basic_zstring<const_value_type, Extent>;
using impl_type = span<value_type, Extent>;
using string_span_type = basic_string_span<value_type, Extent>;
constexpr basic_zstring_span(impl_type s) GSL_NOEXCEPT : span_(s)
{
// expects a zero-terminated span
Expects(s[s.size() - 1] == '\0');
}
// copy
constexpr basic_zstring_span(const basic_zstring_span& other) = default;
// move
constexpr basic_zstring_span(basic_zstring_span&& other) = default;
// assign
constexpr basic_zstring_span& operator=(const basic_zstring_span& other) = default;
// move assign
constexpr basic_zstring_span& operator=(basic_zstring_span&& other) = default;
constexpr bool empty() const GSL_NOEXCEPT { return span_.size() == 0; }
constexpr string_span_type as_string_span() const GSL_NOEXCEPT
{
auto sz = span_.size();
return { span_.data(), sz > 1 ? sz - 1 : 0 };
}
constexpr string_span_type ensure_z() const GSL_NOEXCEPT { return gsl::ensure_z(span_); }
constexpr const_zstring_type assume_z() const GSL_NOEXCEPT { return span_.data(); }
private:
impl_type span_;
};
template <std::ptrdiff_t Max = dynamic_extent>
using zstring_span = basic_zstring_span<char, Max>;
template <std::ptrdiff_t Max = dynamic_extent>
using wzstring_span = basic_zstring_span<wchar_t, Max>;
template <std::ptrdiff_t Max = dynamic_extent>
using u16zstring_span = basic_zstring_span<char16_t, Max>;
template <std::ptrdiff_t Max = dynamic_extent>
using u32zstring_span = basic_zstring_span<char32_t, Max>;
template <std::ptrdiff_t Max = dynamic_extent>
using czstring_span = basic_zstring_span<const char, Max>;
template <std::ptrdiff_t Max = dynamic_extent>
using cwzstring_span = basic_zstring_span<const wchar_t, Max>;
template <std::ptrdiff_t Max = dynamic_extent>
using cu16zstring_span = basic_zstring_span<const char16_t, Max>;
template <std::ptrdiff_t Max = dynamic_extent>
using cu32zstring_span = basic_zstring_span<const char32_t, Max>;
// operator ==
template <class CharT, std::ptrdiff_t Extent, class T,
class = std::enable_if_t<
details::is_basic_string_span<T>::value ||
std::is_convertible<T, gsl::basic_string_span<std::add_const_t<CharT>>>::value>>
bool operator==(const gsl::basic_string_span<CharT, Extent>& one, const T& other) GSL_NOEXCEPT
{
const gsl::basic_string_span<std::add_const_t<CharT>> tmp(other);
return std::equal(one.begin(), one.end(), tmp.begin(), tmp.end());
}
template <class CharT, std::ptrdiff_t Extent, class T,
class = std::enable_if_t<
!details::is_basic_string_span<T>::value &&
std::is_convertible<T, gsl::basic_string_span<std::add_const_t<CharT>>>::value>>
bool operator==(const T& one, const gsl::basic_string_span<CharT, Extent>& other) GSL_NOEXCEPT
{
gsl::basic_string_span<std::add_const_t<CharT>> tmp(one);
return std::equal(tmp.begin(), tmp.end(), other.begin(), other.end());
}
// operator !=
template <typename CharT, std::ptrdiff_t Extent = gsl::dynamic_extent, typename T,
typename = std::enable_if_t<std::is_convertible<
T, gsl::basic_string_span<std::add_const_t<CharT>, Extent>>::value>>
bool operator!=(gsl::basic_string_span<CharT, Extent> one, const T& other) GSL_NOEXCEPT
{
return !(one == other);
}
template <
typename CharT, std::ptrdiff_t Extent = gsl::dynamic_extent, typename T,
typename = std::enable_if_t<
std::is_convertible<T, gsl::basic_string_span<std::add_const_t<CharT>, Extent>>::value &&
!gsl::details::is_basic_string_span<T>::value>>
bool operator!=(const T& one, gsl::basic_string_span<CharT, Extent> other) GSL_NOEXCEPT
{
return !(one == other);
}
// operator<
template <typename CharT, std::ptrdiff_t Extent = gsl::dynamic_extent, typename T,
typename = std::enable_if_t<std::is_convertible<
T, gsl::basic_string_span<std::add_const_t<CharT>, Extent>>::value>>
bool operator<(gsl::basic_string_span<CharT, Extent> one, const T& other) GSL_NOEXCEPT
{
const gsl::basic_string_span<std::add_const_t<CharT>, Extent> tmp(other);
return std::lexicographical_compare(one.begin(), one.end(), tmp.begin(), tmp.end());
}
template <
typename CharT, std::ptrdiff_t Extent = gsl::dynamic_extent, typename T,
typename = std::enable_if_t<
std::is_convertible<T, gsl::basic_string_span<std::add_const_t<CharT>, Extent>>::value &&
!gsl::details::is_basic_string_span<T>::value>>
bool operator<(const T& one, gsl::basic_string_span<CharT, Extent> other) GSL_NOEXCEPT
{
gsl::basic_string_span<std::add_const_t<CharT>, Extent> tmp(one);
return std::lexicographical_compare(tmp.begin(), tmp.end(), other.begin(), other.end());
}
#ifndef _MSC_VER
// VS treats temp and const containers as convertible to basic_string_span,
// so the cases below are already covered by the previous operators
template <
typename CharT, std::ptrdiff_t Extent = gsl::dynamic_extent, typename T,
typename DataType = typename T::value_type,
typename = std::enable_if_t<
!gsl::details::is_span<T>::value && !gsl::details::is_basic_string_span<T>::value &&
std::is_convertible<DataType*, CharT*>::value &&
std::is_same<std::decay_t<decltype(std::declval<T>().size(), *std::declval<T>().data())>,
DataType>::value>>
bool operator<(gsl::basic_string_span<CharT, Extent> one, const T& other) GSL_NOEXCEPT
{
gsl::basic_string_span<std::add_const_t<CharT>, Extent> tmp(other);
return std::lexicographical_compare(one.begin(), one.end(), tmp.begin(), tmp.end());
}
template <
typename CharT, std::ptrdiff_t Extent = gsl::dynamic_extent, typename T,
typename DataType = typename T::value_type,
typename = std::enable_if_t<
!gsl::details::is_span<T>::value && !gsl::details::is_basic_string_span<T>::value &&
std::is_convertible<DataType*, CharT*>::value &&
std::is_same<std::decay_t<decltype(std::declval<T>().size(), *std::declval<T>().data())>,
DataType>::value>>
bool operator<(const T& one, gsl::basic_string_span<CharT, Extent> other) GSL_NOEXCEPT
{
gsl::basic_string_span<std::add_const_t<CharT>, Extent> tmp(one);
return std::lexicographical_compare(tmp.begin(), tmp.end(), other.begin(), other.end());
}
#endif
// operator <=
template <typename CharT, std::ptrdiff_t Extent = gsl::dynamic_extent, typename T,
typename = std::enable_if_t<std::is_convertible<
T, gsl::basic_string_span<std::add_const_t<CharT>, Extent>>::value>>
bool operator<=(gsl::basic_string_span<CharT, Extent> one, const T& other) GSL_NOEXCEPT
{
return !(other < one);
}
template <
typename CharT, std::ptrdiff_t Extent = gsl::dynamic_extent, typename T,
typename = std::enable_if_t<
std::is_convertible<T, gsl::basic_string_span<std::add_const_t<CharT>, Extent>>::value &&
!gsl::details::is_basic_string_span<T>::value>>
bool operator<=(const T& one, gsl::basic_string_span<CharT, Extent> other) GSL_NOEXCEPT
{
return !(other < one);
}
#ifndef _MSC_VER
// VS treats temp and const containers as convertible to basic_string_span,
// so the cases below are already covered by the previous operators
template <
typename CharT, std::ptrdiff_t Extent = gsl::dynamic_extent, typename T,
typename DataType = typename T::value_type,
typename = std::enable_if_t<
!gsl::details::is_span<T>::value && !gsl::details::is_basic_string_span<T>::value &&
std::is_convertible<DataType*, CharT*>::value &&
std::is_same<std::decay_t<decltype(std::declval<T>().size(), *std::declval<T>().data())>,
DataType>::value>>
bool operator<=(gsl::basic_string_span<CharT, Extent> one, const T& other) GSL_NOEXCEPT
{
return !(other < one);
}
template <
typename CharT, std::ptrdiff_t Extent = gsl::dynamic_extent, typename T,
typename DataType = typename T::value_type,
typename = std::enable_if_t<
!gsl::details::is_span<T>::value && !gsl::details::is_basic_string_span<T>::value &&
std::is_convertible<DataType*, CharT*>::value &&
std::is_same<std::decay_t<decltype(std::declval<T>().size(), *std::declval<T>().data())>,
DataType>::value>>
bool operator<=(const T& one, gsl::basic_string_span<CharT, Extent> other) GSL_NOEXCEPT
{
return !(other < one);
}
#endif
// operator>
template <typename CharT, std::ptrdiff_t Extent = gsl::dynamic_extent, typename T,
typename = std::enable_if_t<std::is_convertible<
T, gsl::basic_string_span<std::add_const_t<CharT>, Extent>>::value>>
bool operator>(gsl::basic_string_span<CharT, Extent> one, const T& other) GSL_NOEXCEPT
{
return other < one;
}
template <
typename CharT, std::ptrdiff_t Extent = gsl::dynamic_extent, typename T,
typename = std::enable_if_t<
std::is_convertible<T, gsl::basic_string_span<std::add_const_t<CharT>, Extent>>::value &&
!gsl::details::is_basic_string_span<T>::value>>
bool operator>(const T& one, gsl::basic_string_span<CharT, Extent> other) GSL_NOEXCEPT
{
return other < one;
}
#ifndef _MSC_VER
// VS treats temp and const containers as convertible to basic_string_span,
// so the cases below are already covered by the previous operators
template <
typename CharT, std::ptrdiff_t Extent = gsl::dynamic_extent, typename T,
typename DataType = typename T::value_type,
typename = std::enable_if_t<
!gsl::details::is_span<T>::value && !gsl::details::is_basic_string_span<T>::value &&
std::is_convertible<DataType*, CharT*>::value &&
std::is_same<std::decay_t<decltype(std::declval<T>().size(), *std::declval<T>().data())>,
DataType>::value>>
bool operator>(gsl::basic_string_span<CharT, Extent> one, const T& other) GSL_NOEXCEPT
{
return other < one;
}
template <
typename CharT, std::ptrdiff_t Extent = gsl::dynamic_extent, typename T,
typename DataType = typename T::value_type,
typename = std::enable_if_t<
!gsl::details::is_span<T>::value && !gsl::details::is_basic_string_span<T>::value &&
std::is_convertible<DataType*, CharT*>::value &&
std::is_same<std::decay_t<decltype(std::declval<T>().size(), *std::declval<T>().data())>,
DataType>::value>>
bool operator>(const T& one, gsl::basic_string_span<CharT, Extent> other) GSL_NOEXCEPT
{
return other < one;
}
#endif
// operator >=
template <typename CharT, std::ptrdiff_t Extent = gsl::dynamic_extent, typename T,
typename = std::enable_if_t<std::is_convertible<
T, gsl::basic_string_span<std::add_const_t<CharT>, Extent>>::value>>
bool operator>=(gsl::basic_string_span<CharT, Extent> one, const T& other) GSL_NOEXCEPT
{
return !(one < other);
}
template <
typename CharT, std::ptrdiff_t Extent = gsl::dynamic_extent, typename T,
typename = std::enable_if_t<
std::is_convertible<T, gsl::basic_string_span<std::add_const_t<CharT>, Extent>>::value &&
!gsl::details::is_basic_string_span<T>::value>>
bool operator>=(const T& one, gsl::basic_string_span<CharT, Extent> other) GSL_NOEXCEPT
{
return !(one < other);
}
#ifndef _MSC_VER
// VS treats temp and const containers as convertible to basic_string_span,
// so the cases below are already covered by the previous operators
template <
typename CharT, std::ptrdiff_t Extent = gsl::dynamic_extent, typename T,
typename DataType = typename T::value_type,
typename = std::enable_if_t<
!gsl::details::is_span<T>::value && !gsl::details::is_basic_string_span<T>::value &&
std::is_convertible<DataType*, CharT*>::value &&
std::is_same<std::decay_t<decltype(std::declval<T>().size(), *std::declval<T>().data())>,
DataType>::value>>
bool operator>=(gsl::basic_string_span<CharT, Extent> one, const T& other) GSL_NOEXCEPT
{
return !(one < other);
}
template <
typename CharT, std::ptrdiff_t Extent = gsl::dynamic_extent, typename T,
typename DataType = typename T::value_type,
typename = std::enable_if_t<
!gsl::details::is_span<T>::value && !gsl::details::is_basic_string_span<T>::value &&
std::is_convertible<DataType*, CharT*>::value &&
std::is_same<std::decay_t<decltype(std::declval<T>().size(), *std::declval<T>().data())>,
DataType>::value>>
bool operator>=(const T& one, gsl::basic_string_span<CharT, Extent> other) GSL_NOEXCEPT
{
return !(one < other);
}
#endif
} // namespace gsl
#undef GSL_NOEXCEPT
#ifdef _MSC_VER
#pragma warning(pop)
#if _MSC_VER < 1910
#undef constexpr
#pragma pop_macro("constexpr")
#endif // _MSC_VER < 1910
#endif // _MSC_VER
#endif // GSL_STRING_SPAN_H