summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2012-01-31 22:28:18 +0000
committerDavid Robillard <d@drobilla.net>2012-01-31 22:28:18 +0000
commitfe3491419b084c2d41ad9f29274325e6c0043856 (patch)
treee50dcaea6879029f2a80f3b8884a36620ecea55d
parent86826ae6733119d462be9f3642161db895756643 (diff)
downloadzix-fe3491419b084c2d41ad9f29274325e6c0043856.tar.gz
zix-fe3491419b084c2d41ad9f29274325e6c0043856.tar.bz2
zix-fe3491419b084c2d41ad9f29274325e6c0043856.zip
Windows/Visual C++ portability.
git-svn-id: http://svn.drobilla.net/zix/trunk@51 df6676b4-ccc9-40e5-b5d6-7c4628a128e3
-rw-r--r--src/fat_patree.c13
-rw-r--r--src/hash.c2
-rw-r--r--src/patree.c3
-rw-r--r--src/ring.c22
-rw-r--r--src/sorted_array.c9
-rw-r--r--src/strindex.c7
-rw-r--r--src/tree.c5
-rw-r--r--test/sorted_array_test.c1
-rw-r--r--test/tree_test.c1
-rw-r--r--wscript16
-rw-r--r--zix/common.h17
-rw-r--r--zix/ring.h20
-rw-r--r--zix/sorted_array.h2
-rw-r--r--zix/tree.h16
14 files changed, 93 insertions, 41 deletions
diff --git a/src/fat_patree.c b/src/fat_patree.c
index 278da2a..454cfba 100644
--- a/src/fat_patree.c
+++ b/src/fat_patree.c
@@ -17,7 +17,6 @@
#define _XOPEN_SOURCE 500
#include <assert.h>
-#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
@@ -85,15 +84,16 @@ patree_find_edge(ZixFatPatreeNode* n, uint8_t c, n_edges_t* index)
static void
patree_add_edge(ZixFatPatreeNode* n,
- char* str,
- char* first,
- char* last)
+ char* str,
+ char* first,
+ char* last)
{
assert(last >= first);
const int index = (uint8_t)first[0];
assert(!n->children[index]);
- ZixFatPatreeNode* new_node = malloc(sizeof(ZixFatPatreeNode));
+ ZixFatPatreeNode* new_node = (ZixFatPatreeNode*)malloc(
+ sizeof(ZixFatPatreeNode));
n->children[index] = new_node;
n->children[index]->label_first = first;
n->children[index]->label_last = last;
@@ -112,7 +112,8 @@ patree_split_edge(ZixFatPatreeNode* child,
char* const last = child->label_last;
assert(last >= first);
- ZixFatPatreeNode* new_node = malloc(sizeof(ZixFatPatreeNode));
+ ZixFatPatreeNode* new_node = (ZixFatPatreeNode*)malloc(
+ sizeof(ZixFatPatreeNode));
new_node->label_first = first;
new_node->label_last = last;
new_node->str = child->str;
diff --git a/src/hash.c b/src/hash.c
index c267757..f654e91 100644
--- a/src/hash.c
+++ b/src/hash.c
@@ -55,7 +55,7 @@ zix_hash_new(ZixHashFunc hash_func,
hash->key_equal_func = key_equal_func;
hash->count = 0;
hash->n_buckets = &sizes[0];
- hash->buckets = malloc(*hash->n_buckets * sizeof(Entry*));
+ hash->buckets = (Entry**)malloc(*hash->n_buckets * sizeof(Entry*));
memset(hash->buckets, 0, *hash->n_buckets * sizeof(Entry*));
return hash;
diff --git a/src/patree.c b/src/patree.c
index 0cb74d3..f3603a2 100644
--- a/src/patree.c
+++ b/src/patree.c
@@ -17,7 +17,6 @@
#define _XOPEN_SOURCE 500
#include <assert.h>
-#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
@@ -61,7 +60,7 @@ zix_patree_print_rec(ZixPatreeNode* node, FILE* fd)
{
if (node->label_first) {
size_t edge_label_len = node->label_last - node->label_first + 1;
- char* edge_label = malloc(edge_label_len + 1);
+ char* edge_label = (char*)malloc(edge_label_len + 1);
strncpy(edge_label, node->label_first, edge_label_len);
fprintf(fd, "\t\"%p\" [label=<"
"<TABLE BORDER=\"0\" CELLBORDER=\"1\" CELLSPACING=\"0\">"
diff --git a/src/ring.c b/src/ring.c
index fd5133a..7ebbe67 100644
--- a/src/ring.c
+++ b/src/ring.c
@@ -20,15 +20,25 @@
#ifdef HAVE_MLOCK
# include <sys/mman.h>
+# define ZIX_MLOCK(ptr, size) mlock((ptr), (size))
+#elif defined(_WIN32)
+# include <windows.h>
+# define ZIX_MLOCK(ptr, size) VirtualLock((ptr), (size))
+#else
+# pragma message("warning: No memory locking, possible RT violations")
+# define ZIX_MLOCK(ptr, size)
#endif
#if defined(__APPLE__)
# include <libkern/OSAtomic.h>
# define ZIX_FULL_BARRIER() OSMemoryBarrier()
+#elif defined(_WIN32)
+# include <windows.h>
+# define ZIX_FULL_BARRIER() MemoryBarrier()
#elif (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 1)
# define ZIX_FULL_BARRIER() __sync_synchronize()
#else
-# warning Memory barriers unsupported, possible bugs on SMP systems
+# pragma message("warning: No memory barriers, possible SMP bugs")
# define ZIX_FULL_BARRIER()
#endif
@@ -68,7 +78,7 @@ zix_ring_new(uint32_t size)
ring->read_head = 0;
ring->size = next_power_of_two(size);
ring->size_mask = ring->size - 1;
- ring->buf = malloc(ring->size);
+ ring->buf = (char*)malloc(ring->size);
return ring;
}
@@ -82,12 +92,8 @@ zix_ring_free(ZixRing* ring)
void
zix_ring_mlock(ZixRing* ring)
{
-#ifdef HAVE_MLOCK
- mlock(ring, sizeof(ZixRing));
- mlock(ring->buf, ring->size);
-#else
-# warning Memory locking (via mlock) unsupported
-#endif
+ ZIX_MLOCK(ring, sizeof(ZixRing));
+ ZIX_MLOCK(ring->buf, ring->size);
}
void
diff --git a/src/sorted_array.c b/src/sorted_array.c
index ae08946..f8e785d 100644
--- a/src/sorted_array.c
+++ b/src/sorted_array.c
@@ -15,7 +15,6 @@
*/
#include <assert.h>
-#include <inttypes.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
@@ -51,10 +50,12 @@ zix_sorted_array_print(ZixSortedArray* a)
ZIX_API
ZixSortedArray*
-zix_sorted_array_new(bool allow_duplicates, ZixComparator cmp, void* cmp_data,
- size_t elem_size)
+zix_sorted_array_new(bool allow_duplicates,
+ ZixComparator cmp,
+ void* cmp_data,
+ size_t elem_size)
{
- ZixSortedArray* a = malloc(sizeof(ZixSortedArray));
+ ZixSortedArray* a = (ZixSortedArray*)malloc(sizeof(ZixSortedArray));
a->array = NULL;
a->cmp = cmp;
a->cmp_data = cmp_data;
diff --git a/src/strindex.c b/src/strindex.c
index eae98b2..886d261 100644
--- a/src/strindex.c
+++ b/src/strindex.c
@@ -109,8 +109,8 @@ strindex_add_edge(ZixStrindexNode* n,
char* last)
{
assert(last > first);
- n->children = realloc(n->children,
- (n->num_children + 1) * sizeof(ZixStrindexNode));
+ n->children = (ZixStrindexNode*)realloc(
+ n->children, (n->num_children + 1) * sizeof(ZixStrindexNode));
memset(&n->children[n->num_children], '\0', sizeof(ZixStrindexNode));
@@ -133,7 +133,8 @@ strindex_split_edge(ZixStrindexNode* child,
assert(last > first);
assert(child->first);
- child->children = malloc(sizeof(ZixStrindexNode));
+ child->children = (ZixStrindexNode*)malloc(sizeof(ZixStrindexNode));
+
child->children[0].children = children;
child->children[0].num_children = num_children;
child->children[0].first = child->first;
diff --git a/src/tree.c b/src/tree.c
index 13b7745..74f882a 100644
--- a/src/tree.c
+++ b/src/tree.c
@@ -15,7 +15,6 @@
*/
#include <assert.h>
-#include <inttypes.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
@@ -74,7 +73,7 @@ zix_tree_new(bool allow_duplicates,
void* cmp_data,
ZixDestroyFunc destroy)
{
- ZixTree* t = malloc(sizeof(ZixTree));
+ ZixTree* t = (ZixTree*)malloc(sizeof(ZixTree));
t->root = NULL;
t->destroy = destroy;
t->cmp = cmp;
@@ -366,7 +365,7 @@ zix_tree_insert(ZixTree* t, void* e, ZixTreeIter** ti)
}
// Allocate a new node n
- if (!(n = malloc(sizeof(ZixTreeNode)))) {
+ if (!(n = (ZixTreeNode*)malloc(sizeof(ZixTreeNode)))) {
return ZIX_STATUS_NO_MEM;
}
memset(n, '\0', sizeof(ZixTreeNode));
diff --git a/test/sorted_array_test.c b/test/sorted_array_test.c
index e8c2fdd..63c076b 100644
--- a/test/sorted_array_test.c
+++ b/test/sorted_array_test.c
@@ -14,7 +14,6 @@
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
-#include <inttypes.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
diff --git a/test/tree_test.c b/test/tree_test.c
index b89d5a8..b756202 100644
--- a/test/tree_test.c
+++ b/test/tree_test.c
@@ -14,7 +14,6 @@
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
-#include <inttypes.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
diff --git a/wscript b/wscript
index 965426d..3632e13 100644
--- a/wscript
+++ b/wscript
@@ -36,11 +36,14 @@ def options(opt):
help="Build benchmarks")
def configure(conf):
+ conf.load('compiler_c')
autowaf.configure(conf)
autowaf.display_header('Zix Configuration')
- conf.load('compiler_c')
- conf.env.append_value('CFLAGS', '-std=c99')
+ if conf.env['MSVC_COMPILER']:
+ conf.env.append_unique('CFLAGS', ['-TP', '-MD'])
+ else:
+ conf.env.append_unique('CFLAGS', '-std=c99')
conf.env['BUILD_BENCH'] = Options.options.build_bench
conf.env['BUILD_TESTS'] = Options.options.build_tests
@@ -92,6 +95,10 @@ def build(bld):
if Options.platform == 'darwin':
framework = ['CoreServices']
+ libflags = [ '-fvisibility=hidden' ]
+ if bld.env['MSVC_COMPILER']:
+ libflags = []
+
lib_source = '''
src/fat_patree.c
src/hash.c
@@ -112,9 +119,8 @@ def build(bld):
vnum = ZIX_LIB_VERSION,
install_path = '${LIBDIR}',
framework = framework,
- cflags = ['-fvisibility=hidden',
- '-DZIX_SHARED',
- '-DZIX_INTERNAL' ])
+ cflags = libflags + ['-DZIX_SHARED',
+ '-DZIX_INTERNAL'])
if bld.env['BUILD_TESTS']:
test_libs = ['pthread']
diff --git a/zix/common.h b/zix/common.h
index c0dffeb..ab7e431 100644
--- a/zix/common.h
+++ b/zix/common.h
@@ -17,8 +17,6 @@
#ifndef ZIX_COMMON_H
#define ZIX_COMMON_H
-#include <stdbool.h>
-
/**
@addtogroup zix
@{
@@ -26,7 +24,7 @@
/** @cond */
#ifdef ZIX_SHARED
-# ifdef __WIN32__
+# ifdef _WIN32
# define ZIX_LIB_IMPORT __declspec(dllimport)
# define ZIX_LIB_EXPORT __declspec(dllexport)
# else
@@ -43,6 +41,12 @@
#endif
/** @endcond */
+#ifdef __cplusplus
+extern "C" {
+#else
+# include <stdbool.h>
+#endif
+
typedef enum {
ZIX_STATUS_SUCCESS,
ZIX_STATUS_ERROR,
@@ -66,7 +70,12 @@ typedef bool (*ZixEqualFunc)(const void* a, const void* b);
*/
typedef void (*ZixDestroyFunc)(void* ptr);
-/**@}
+/**
+ @}
*/
+#ifdef __cplusplus
+} /* extern "C" */
+#endif
+
#endif /* ZIX_COMMON_H */
diff --git a/zix/ring.h b/zix/ring.h
index 3abb1e8..34ba2fa 100644
--- a/zix/ring.h
+++ b/zix/ring.h
@@ -21,6 +21,17 @@
#include "zix/common.h"
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/**
+ @addtogroup zix
+ @{
+ @name Ring
+ @{
+*/
+
/**
A lock-free ring buffer.
@@ -107,4 +118,13 @@ zix_ring_skip(ZixRing* ring, uint32_t size);
uint32_t
zix_ring_write(ZixRing* ring, const void* src, uint32_t size);
+/**
+ @}
+ @}
+*/
+
+#ifdef __cplusplus
+} /* extern "C" */
+#endif
+
#endif /* ZIX_RING_H */
diff --git a/zix/sorted_array.h b/zix/sorted_array.h
index 1d508f2..06177dd 100644
--- a/zix/sorted_array.h
+++ b/zix/sorted_array.h
@@ -18,8 +18,6 @@
#ifndef ZIX_SORTED_ARRAY_H
#define ZIX_SORTED_ARRAY_H
-#include <stdbool.h>
-
#include "zix/common.h"
#ifdef __cplusplus
diff --git a/zix/tree.h b/zix/tree.h
index 378db28..b54087c 100644
--- a/zix/tree.h
+++ b/zix/tree.h
@@ -17,7 +17,6 @@
#ifndef ZIX_TREE_H
#define ZIX_TREE_H
-#include <stdbool.h>
#include <stddef.h>
#include "zix/common.h"
@@ -46,6 +45,7 @@ typedef struct ZixTreeNodeImpl ZixTreeIter;
/**
Create a new (empty) tree.
*/
+ZIX_API
ZixTree*
zix_tree_new(bool allow_duplicates,
ZixComparator cmp,
@@ -55,24 +55,28 @@ zix_tree_new(bool allow_duplicates,
/**
Free @a t.
*/
+ZIX_API
void
zix_tree_free(ZixTree* t);
/**
Return the number of elements in @a t.
*/
+ZIX_API
size_t
zix_tree_size(ZixTree* t);
/**
Insert the element @a e into @a t and point @a ti at the new element.
*/
+ZIX_API
ZixStatus
zix_tree_insert(ZixTree* t, void* e, ZixTreeIter** ti);
/**
Remove the item pointed at by @a ti from @a t.
*/
+ZIX_API
ZixStatus
zix_tree_remove(ZixTree* t, ZixTreeIter* ti);
@@ -80,60 +84,70 @@ zix_tree_remove(ZixTree* t, ZixTreeIter* ti);
Set @a ti to an element equal to @a e in @a t.
If no such item exists, @a ti is set to NULL.
*/
+ZIX_API
ZixStatus
zix_tree_find(const ZixTree* t, const void* e, ZixTreeIter** ti);
/**
Return the data associated with the given tree item.
*/
+ZIX_API
void*
zix_tree_get(ZixTreeIter* ti);
/**
Return an iterator to the first (smallest) element in @a t.
*/
+ZIX_API
ZixTreeIter*
zix_tree_begin(ZixTree* t);
/**
Return an iterator the the element one past the last element in @a t.
*/
+ZIX_API
ZixTreeIter*
zix_tree_end(ZixTree* t);
/**
Return true iff @a i is an iterator to the end of its tree.
*/
+ZIX_API
bool
zix_tree_iter_is_end(ZixTreeIter* i);
/**
Return an iterator to the last (largest) element in @a t.
*/
+ZIX_API
ZixTreeIter*
zix_tree_rbegin(ZixTree* t);
/**
Return an iterator the the element one before the first element in @a t.
*/
+ZIX_API
ZixTreeIter*
zix_tree_rend(ZixTree* t);
/**
Return true iff @a i is an iterator to the reverse end of its tree.
*/
+ZIX_API
bool
zix_tree_iter_is_rend(ZixTreeIter* i);
/**
Return an iterator that points to the element one past @a i.
*/
+ZIX_API
ZixTreeIter*
zix_tree_iter_next(ZixTreeIter* i);
/**
Return an iterator that points to the element one before @a i.
*/
+ZIX_API
ZixTreeIter*
zix_tree_iter_prev(ZixTreeIter* i);