add win32 (msvc) support
This commit is contained in:
10
core/include/blue/core/bitop.h
Normal file
10
core/include/blue/core/bitop.h
Normal file
@@ -0,0 +1,10 @@
|
||||
#ifndef BLUELIB_CORE_BITOP_H_
|
||||
#define BLUELIB_CORE_BITOP_H_
|
||||
|
||||
#include <blue/core/misc.h>
|
||||
|
||||
BLUE_API int b_popcountl(long v);
|
||||
BLUE_API int b_ctzl(long v);
|
||||
BLUE_API int b_clzl(long v);
|
||||
|
||||
#endif
|
||||
@@ -267,34 +267,34 @@ typedef struct b_btree_iterator {
|
||||
@param tree the tree to re-balance.
|
||||
@param node the node that was just inserted into the tree.
|
||||
*/
|
||||
extern void b_btree_insert_fixup(b_btree *tree, b_btree_node *node);
|
||||
BLUE_API void b_btree_insert_fixup(b_btree *tree, b_btree_node *node);
|
||||
|
||||
/* delete a node from a binary tree and re-balance the tree afterwards.
|
||||
|
||||
@param tree the tree to delete from
|
||||
@param node the node to delete.
|
||||
*/
|
||||
extern void b_btree_delete(b_btree *tree, b_btree_node *node);
|
||||
BLUE_API void b_btree_delete(b_btree *tree, b_btree_node *node);
|
||||
|
||||
/* get the first node in a binary tree.
|
||||
|
||||
this will be the node with the smallest key (i.e. the node that is
|
||||
furthest-left from the root)
|
||||
*/
|
||||
extern b_btree_node *b_btree_first(const b_btree *tree);
|
||||
BLUE_API b_btree_node *b_btree_first(const b_btree *tree);
|
||||
|
||||
/* get the last node in a binary tree.
|
||||
|
||||
this will be the node with the largest key (i.e. the node that is
|
||||
furthest-right from the root)
|
||||
*/
|
||||
extern b_btree_node *b_btree_last(const b_btree *tree);
|
||||
BLUE_API b_btree_node *b_btree_last(const b_btree *tree);
|
||||
/* for any binary tree node, this function returns the node with the
|
||||
* next-largest key value */
|
||||
extern b_btree_node *b_btree_next(const b_btree_node *node);
|
||||
BLUE_API b_btree_node *b_btree_next(const b_btree_node *node);
|
||||
/* for any binary tree node, this function returns the node with the
|
||||
* next-smallest key value */
|
||||
extern b_btree_node *b_btree_prev(const b_btree_node *node);
|
||||
BLUE_API b_btree_node *b_btree_prev(const b_btree_node *node);
|
||||
/* return true if the btree is empty, false otherwise */
|
||||
static inline bool b_btree_empty(const b_btree *tree)
|
||||
{
|
||||
@@ -345,10 +345,10 @@ static inline unsigned short b_btree_height(b_btree_node *node)
|
||||
return node->b_height;
|
||||
}
|
||||
|
||||
extern int b_btree_iterator_begin(const b_btree *tree, b_btree_iterator *it);
|
||||
extern bool b_btree_iterator_next(b_btree_iterator *it);
|
||||
extern b_status b_btree_iterator_erase(b_btree_iterator *it);
|
||||
extern bool b_btree_iterator_is_valid(const b_btree_iterator *it);
|
||||
BLUE_API int b_btree_iterator_begin(const b_btree *tree, b_btree_iterator *it);
|
||||
BLUE_API bool b_btree_iterator_next(b_btree_iterator *it);
|
||||
BLUE_API b_status b_btree_iterator_erase(b_btree_iterator *it);
|
||||
BLUE_API bool b_btree_iterator_is_valid(const b_btree_iterator *it);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@
|
||||
}; \
|
||||
static f##_t_ f##_; \
|
||||
static void f(void)
|
||||
#elif defined(_MSB_VER)
|
||||
#elif defined(_MSC_VER)
|
||||
#pragma section(".CRT$XCU", read)
|
||||
#define B_INIT2_(f, p) \
|
||||
static void f(void); \
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
#define BLUELIB_CORE_ITERATOR_H_
|
||||
|
||||
#include <blue/core/status.h>
|
||||
#include <blue/core/misc.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
struct b_iterator;
|
||||
@@ -17,9 +18,9 @@ typedef struct b_iterator {
|
||||
const b_iterator_ops *it_ops;
|
||||
} b_iterator;
|
||||
|
||||
extern b_status b_iterator_close(b_iterator *it);
|
||||
extern bool b_iterator_next(b_iterator *it);
|
||||
extern b_status b_iterator_erase(b_iterator *it);
|
||||
extern bool b_iterator_is_valid(const b_iterator *it);
|
||||
BLUE_API b_status b_iterator_close(b_iterator *it);
|
||||
BLUE_API bool b_iterator_next(b_iterator *it);
|
||||
BLUE_API b_status b_iterator_erase(b_iterator *it);
|
||||
BLUE_API bool b_iterator_is_valid(const b_iterator *it);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -14,4 +14,14 @@
|
||||
#define z__b_numargs(arg_type, ...) \
|
||||
(sizeof((arg_type[]) {__VA_ARGS__}) / sizeof(arg_type))
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#ifdef BLUELIB_EXPORT
|
||||
#define BLUE_API extern __declspec(dllexport)
|
||||
#else
|
||||
#define BLUE_API extern __declspec(dllimport)
|
||||
#endif
|
||||
#else
|
||||
#define BLUE_API extern
|
||||
#endif
|
||||
|
||||
#endif // B_MISB_H_
|
||||
|
||||
@@ -60,26 +60,26 @@ static inline b_queue_entry *b_queue_prev(const b_queue_entry *entry)
|
||||
return entry->qe_prev;
|
||||
}
|
||||
|
||||
extern size_t b_queue_length(b_queue *q);
|
||||
BLUE_API size_t b_queue_length(b_queue *q);
|
||||
|
||||
extern void b_queue_insert_before(
|
||||
BLUE_API void b_queue_insert_before(
|
||||
b_queue *q, b_queue_entry *entry, b_queue_entry *before);
|
||||
extern void b_queue_insert_after(
|
||||
BLUE_API void b_queue_insert_after(
|
||||
b_queue *q, b_queue_entry *entry, b_queue_entry *after);
|
||||
|
||||
extern void b_queue_push_front(b_queue *q, b_queue_entry *entry);
|
||||
extern void b_queue_push_back(b_queue *q, b_queue_entry *entry);
|
||||
BLUE_API void b_queue_push_front(b_queue *q, b_queue_entry *entry);
|
||||
BLUE_API void b_queue_push_back(b_queue *q, b_queue_entry *entry);
|
||||
|
||||
extern b_queue_entry *b_queue_pop_front(b_queue *q);
|
||||
extern b_queue_entry *b_queue_pop_back(b_queue *q);
|
||||
BLUE_API b_queue_entry *b_queue_pop_front(b_queue *q);
|
||||
BLUE_API b_queue_entry *b_queue_pop_back(b_queue *q);
|
||||
|
||||
extern void b_queue_delete(b_queue *q, b_queue_entry *entry);
|
||||
extern void b_queue_delete_all(b_queue *q);
|
||||
BLUE_API void b_queue_delete(b_queue *q, b_queue_entry *entry);
|
||||
BLUE_API void b_queue_delete_all(b_queue *q);
|
||||
|
||||
extern int b_queue_iterator_begin(const b_queue *q, b_queue_iterator *it);
|
||||
extern bool b_queue_iterator_next(b_queue_iterator *it);
|
||||
extern b_status b_queue_iterator_erase(b_queue_iterator *it);
|
||||
extern bool b_queue_iterator_is_valid(const b_queue_iterator *it);
|
||||
BLUE_API int b_queue_iterator_begin(const b_queue *q, b_queue_iterator *it);
|
||||
BLUE_API bool b_queue_iterator_next(b_queue_iterator *it);
|
||||
BLUE_API b_status b_queue_iterator_erase(b_queue_iterator *it);
|
||||
BLUE_API bool b_queue_iterator_is_valid(const b_queue_iterator *it);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
||||
@@ -26,12 +26,12 @@ typedef struct b_random_ctx {
|
||||
};
|
||||
} b_random_ctx;
|
||||
|
||||
extern b_random_ctx *b_random_global_ctx(void);
|
||||
BLUE_API b_random_ctx *b_random_global_ctx(void);
|
||||
|
||||
extern b_status b_random_init(b_random_ctx *ctx, b_random_flags flags);
|
||||
extern unsigned long long b_random_next_int64(b_random_ctx *ctx);
|
||||
extern double b_random_next_double(b_random_ctx *ctx);
|
||||
extern void b_random_next_bytes(
|
||||
BLUE_API b_status b_random_init(b_random_ctx *ctx, b_random_flags flags);
|
||||
BLUE_API unsigned long long b_random_next_int64(b_random_ctx *ctx);
|
||||
BLUE_API double b_random_next_double(b_random_ctx *ctx);
|
||||
BLUE_API void b_random_next_bytes(
|
||||
b_random_ctx *ctx, unsigned char *out, size_t nbytes);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
#ifndef BLUELIB_CORE_STATUS_H_
|
||||
#define BLUELIB_CORE_STATUS_H_
|
||||
|
||||
#include <blue/core/misc.h>
|
||||
|
||||
#define B_OK(status) ((status) == B_SUCCESS)
|
||||
#define B_ERR(status) ((status) != B_SUCCESS)
|
||||
|
||||
@@ -18,6 +20,6 @@ typedef enum {
|
||||
B_ERR_BAD_FORMAT,
|
||||
} b_status;
|
||||
|
||||
extern const char *b_status_to_string(b_status status);
|
||||
BLUE_API const char *b_status_to_string(b_status status);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
#define BLUELIB_CORE_STRINGSTREAM_H_
|
||||
|
||||
#include <blue/core/status.h>
|
||||
#include <blue/core/misc.h>
|
||||
#include <stddef.h>
|
||||
|
||||
typedef struct b_stringstream {
|
||||
@@ -14,18 +15,18 @@ typedef struct b_stringstream {
|
||||
size_t ss_istack_ptr, ss_istack_size;
|
||||
} b_stringstream;
|
||||
|
||||
extern void b_stringstream_begin(b_stringstream *strv, char *buf, size_t max);
|
||||
extern void b_stringstream_begin_dynamic(b_stringstream *strv);
|
||||
BLUE_API void b_stringstream_begin(b_stringstream *strv, char *buf, size_t max);
|
||||
BLUE_API void b_stringstream_begin_dynamic(b_stringstream *strv);
|
||||
|
||||
extern void b_stringstream_push_indent(b_stringstream *strv, int indent);
|
||||
extern void b_stringstream_pop_indent(b_stringstream *strv);
|
||||
BLUE_API void b_stringstream_push_indent(b_stringstream *strv, int indent);
|
||||
BLUE_API void b_stringstream_pop_indent(b_stringstream *strv);
|
||||
|
||||
extern b_status b_stringstream_add(b_stringstream *strv, const char *str);
|
||||
extern b_status b_stringstream_addf(b_stringstream *strv, const char *format, ...);
|
||||
extern b_status b_stringstream_addv(b_stringstream *strv, const char **strs);
|
||||
extern b_status b_stringstream_addvl(
|
||||
BLUE_API b_status b_stringstream_add(b_stringstream *strv, const char *str);
|
||||
BLUE_API b_status b_stringstream_addf(b_stringstream *strv, const char *format, ...);
|
||||
BLUE_API b_status b_stringstream_addv(b_stringstream *strv, const char **strs);
|
||||
BLUE_API b_status b_stringstream_addvl(
|
||||
b_stringstream *strv, const char **strs, size_t count);
|
||||
extern b_status b_stringstream_add_many(b_stringstream *strv, ...);
|
||||
extern char *b_stringstream_end(b_stringstream *strv);
|
||||
BLUE_API b_status b_stringstream_add_many(b_stringstream *strv, ...);
|
||||
BLUE_API char *b_stringstream_end(b_stringstream *strv);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
|
||||
#define GET_ALGORITHM_ID(flags) ((flags) & 0xFF)
|
||||
|
||||
extern struct b_random_algorithm z__b_gen_mt19937;
|
||||
BLUE_API struct b_random_algorithm z__b_gen_mt19937;
|
||||
|
||||
static struct b_random_algorithm *generators[] = {
|
||||
[B_RANDOM_MT19937] = &z__b_gen_mt19937,
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
#define _BLUELIB_RANDOM_H_
|
||||
|
||||
#include <stdint.h>
|
||||
#include <blue/core/misc.h>
|
||||
|
||||
struct b_random_ctx;
|
||||
|
||||
@@ -11,7 +12,7 @@ struct b_random_algorithm {
|
||||
uint64_t(*gen_getrand)(struct b_random_ctx *);
|
||||
};
|
||||
|
||||
extern uint64_t z__b_platform_random_seed();
|
||||
extern uint64_t z__b_platform_random_seed_secure();
|
||||
BLUE_API uint64_t z__b_platform_random_seed(void);
|
||||
BLUE_API uint64_t z__b_platform_random_seed_secure(void);
|
||||
|
||||
#endif
|
||||
|
||||
54
core/sys/windows/bitop.c
Normal file
54
core/sys/windows/bitop.c
Normal file
@@ -0,0 +1,54 @@
|
||||
#include <blue/core/bitop.h>
|
||||
#include <intrin.h>
|
||||
#include <assert.h>
|
||||
|
||||
#define CPU_FEATURE_YES 1
|
||||
#define CPU_FEATURE_NO -1
|
||||
#define CPU_FEATURE_UNKNOWN 0
|
||||
|
||||
static int cpu_supports_popcnt = CPU_FEATURE_UNKNOWN;
|
||||
|
||||
static int check_popcnt_support()
|
||||
{
|
||||
int d[4];
|
||||
__cpuid(d, 0x00000001);
|
||||
|
||||
return (d[2] & 0x800000) ? CPU_FEATURE_YES : CPU_FEATURE_NO;
|
||||
}
|
||||
|
||||
int b_popcountl(long v)
|
||||
{
|
||||
if (cpu_supports_popcnt == CPU_FEATURE_UNKNOWN) {
|
||||
cpu_supports_popcnt = check_popcnt_support();
|
||||
}
|
||||
|
||||
if (cpu_supports_popcnt == CPU_FEATURE_YES) {
|
||||
return __popcnt64(v);
|
||||
}
|
||||
|
||||
assert(0 && "CPU does not support popcount!");
|
||||
return 0;
|
||||
}
|
||||
|
||||
int b_ctzl(long v)
|
||||
{
|
||||
unsigned long trailing_zero = 0;
|
||||
|
||||
if (_BitScanForward64(&trailing_zero, v)) {
|
||||
return trailing_zero;
|
||||
} else {
|
||||
return 64;
|
||||
}
|
||||
}
|
||||
|
||||
int b_clzl(long v)
|
||||
{
|
||||
unsigned long leading_zero = 0;
|
||||
|
||||
if (_BitScanReverse64(&leading_zero, v)) {
|
||||
return 64 - leading_zero;
|
||||
} else {
|
||||
// Same remarks as above
|
||||
return 64;
|
||||
}
|
||||
}
|
||||
30
core/sys/windows/random.c
Normal file
30
core/sys/windows/random.c
Normal file
@@ -0,0 +1,30 @@
|
||||
#include <fcntl.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#include <Windows.h>
|
||||
#include <wincrypt.h>
|
||||
|
||||
uint64_t z__b_platform_random_seed_secure(void)
|
||||
{
|
||||
BOOL status;
|
||||
HCRYPTPROV hCryptProv;
|
||||
status = CryptAcquireContext(
|
||||
&hCryptProv, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT);
|
||||
|
||||
if (status == FALSE) {
|
||||
return (uint64_t)-1;
|
||||
}
|
||||
|
||||
uint64_t v = 0;
|
||||
status = CryptGenRandom(hCryptProv, sizeof v, (BYTE *)&v);
|
||||
|
||||
CryptReleaseContext(hCryptProv, 0);
|
||||
|
||||
return status == TRUE ? v : (uint64_t)-1;
|
||||
}
|
||||
|
||||
uint64_t z__b_platform_random_seed(void)
|
||||
{
|
||||
return z__b_platform_random_seed_secure();
|
||||
}
|
||||
Reference in New Issue
Block a user