add libipset

This commit is contained in:
Max Lv
2014-05-07 12:05:03 +08:00
parent 75a98f8ecc
commit 8be020efd5
105 changed files with 17543 additions and 1754 deletions
+18
View File
@@ -0,0 +1,18 @@
/* -*- coding: utf-8 -*-
* ----------------------------------------------------------------------
* Copyright © 2012, RedJack, LLC.
* All rights reserved.
*
* Please see the COPYING file in this distribution for license
* details.
* ----------------------------------------------------------------------
*/
#ifndef LIBCORK_CLI_H
#define LIBCORK_CLI_H
/*** include all of the parts ***/
#include <libcork/cli/commands.h>
#endif /* LIBCORK_CLI_H */
+61
View File
@@ -0,0 +1,61 @@
/* -*- coding: utf-8 -*-
* ----------------------------------------------------------------------
* Copyright © 2012, RedJack, LLC.
* All rights reserved.
*
* Please see the COPYING file in this distribution for license
* details.
* ----------------------------------------------------------------------
*/
#ifndef LIBCORK_COMMANDS_H
#define LIBCORK_COMMANDS_H
#include <libcork/core/api.h>
typedef void
(*cork_leaf_command_run)(int argc, char **argv);
typedef int
(*cork_option_parser)(int argc, char **argv);
enum cork_command_type {
CORK_COMMAND_SET,
CORK_LEAF_COMMAND
};
struct cork_command {
enum cork_command_type type;
const char *name;
const char *short_desc;
const char *usage_suffix;
const char *full_help;
int
(*parse_options)(int argc, char **argv);
struct cork_command **set;
cork_leaf_command_run run;
};
#define cork_command_set(name, sd, parse_options, set) \
{ \
CORK_COMMAND_SET, name, sd, NULL, NULL, \
parse_options, set, NULL \
}
#define cork_leaf_command(name, sd, us, fh, parse_options, run) \
{ \
CORK_LEAF_COMMAND, name, sd, us, fh, \
parse_options, NULL, run \
}
CORK_API void
cork_command_show_help(struct cork_command *command, const char *message);
CORK_API int
cork_command_main(struct cork_command *root, int argc, char **argv);
#endif /* LIBCORK_COMMANDS_H */
+45
View File
@@ -0,0 +1,45 @@
/* -*- coding: utf-8 -*-
* ----------------------------------------------------------------------
* Copyright © 2012-2013, RedJack, LLC.
* All rights reserved.
*
* Please see the COPYING file in this distribution for license
* details.
* ----------------------------------------------------------------------
*/
#ifndef LIBCORK_CONFIG_ARCH_H
#define LIBCORK_CONFIG_ARCH_H
/*-----------------------------------------------------------------------
* Platform
*/
#if defined(__i386__) || defined(_M_IX86)
#define CORK_CONFIG_ARCH_X86 1
#else
#define CORK_CONFIG_ARCH_X86 0
#endif
#if defined(__x86_64__) || defined(_M_X64)
#define CORK_CONFIG_ARCH_X64 1
#else
#define CORK_CONFIG_ARCH_X64 0
#endif
#if defined(__powerpc__) || defined(__ppc__)
/* GCC-ish compiler */
#define CORK_CONFIG_ARCH_PPC 1
#elif defined(_M_PPC)
/* VS-ish compiler */
#define CORK_CONFIG_ARCH_PPC 1
#elif defined(_ARCH_PPC)
/* Something called XL C/C++? */
#define CORK_CONFIG_ARCH_PPC 1
#else
#define CORK_CONFIG_ARCH_PPC 0
#endif
#endif /* LIBCORK_CONFIG_ARCH_H */
+34
View File
@@ -0,0 +1,34 @@
/* -*- coding: utf-8 -*-
* ----------------------------------------------------------------------
* Copyright © 2013, RedJack, LLC.
* All rights reserved.
*
* Please see the COPYING file in this distribution for license
* details.
* ----------------------------------------------------------------------
*/
#ifndef LIBCORK_CONFIG_BSD_H
#define LIBCORK_CONFIG_BSD_H
/*-----------------------------------------------------------------------
* Endianness
*/
#include <sys/endian.h>
#if BYTE_ORDER == BIG_ENDIAN
#define CORK_CONFIG_IS_BIG_ENDIAN 1
#define CORK_CONFIG_IS_LITTLE_ENDIAN 0
#elif BYTE_ORDER == LITTLE_ENDIAN
#define CORK_CONFIG_IS_BIG_ENDIAN 0
#define CORK_CONFIG_IS_LITTLE_ENDIAN 1
#else
#error "Cannot determine system endianness"
#endif
#define CORK_HAVE_REALLOCF 1
#define CORK_HAVE_PTHREADS 1
#endif /* LIBCORK_CONFIG_BSD_H */
+91
View File
@@ -0,0 +1,91 @@
/* -*- coding: utf-8 -*-
* ----------------------------------------------------------------------
* Copyright © 2011-2012, RedJack, LLC.
* All rights reserved.
*
* Please see the COPYING file in this distribution for license
* details.
* ----------------------------------------------------------------------
*/
#ifndef LIBCORK_CONFIG_GCC_H
#define LIBCORK_CONFIG_GCC_H
/* Figure out the GCC version */
#if defined(__GNUC_PATCHLEVEL__)
#define CORK_CONFIG_GCC_VERSION (__GNUC__ * 10000 \
+ __GNUC_MINOR__ * 100 \
+ __GNUC_PATCHLEVEL__)
#else
#define CORK_CONFIG_GCC_VERSION (__GNUC__ * 10000 \
+ __GNUC_MINOR__ * 100)
#endif
/*-----------------------------------------------------------------------
* Compiler attributes
*/
/* The GCC assembly syntax has been available basically forever. */
#if defined(CORK_CONFIG_GCC_VERSION)
#define CORK_CONFIG_HAVE_GCC_ASM 1
#else
#define CORK_CONFIG_HAVE_GCC_ASM 0
#endif
/* The GCC atomic instrinsics are available as of GCC 4.1.0. */
#if CORK_CONFIG_GCC_VERSION >= 40100
#define CORK_CONFIG_HAVE_GCC_ATOMICS 1
#else
#define CORK_CONFIG_HAVE_GCC_ATOMICS 0
#endif
/* The attributes we want to use are available as of GCC 2.96. */
#if CORK_CONFIG_GCC_VERSION >= 29600
#define CORK_CONFIG_HAVE_GCC_ATTRIBUTES 1
#else
#define CORK_CONFIG_HAVE_GCC_ATTRIBUTES 0
#endif
/* __int128 seems to be available on 64-bit platforms as of GCC 4.6. The
* attribute((mode(TI))) syntax seems to be available as of 4.1. */
#if CORK_CONFIG_ARCH_X64 && CORK_CONFIG_GCC_VERSION >= 40600
#define CORK_CONFIG_HAVE_GCC_INT128 1
#else
#define CORK_CONFIG_HAVE_GCC_INT128 0
#endif
#if CORK_CONFIG_ARCH_X64 && CORK_CONFIG_GCC_VERSION >= 40100
#define CORK_CONFIG_HAVE_GCC_MODE_ATTRIBUTE 1
#else
#define CORK_CONFIG_HAVE_GCC_MODE_ATTRIBUTE 0
#endif
/* Statement expressions have been available since GCC 3.1. */
#if CORK_CONFIG_GCC_VERSION >= 30100
#define CORK_CONFIG_HAVE_GCC_STATEMENT_EXPRS 1
#else
#define CORK_CONFIG_HAVE_GCC_STATEMENT_EXPRS 0
#endif
/* Thread-local storage has been available since GCC 3.3, but not on Mac
* OS X. */
#if !(defined(__APPLE__) && defined(__MACH__))
#if CORK_CONFIG_GCC_VERSION >= 30300
#define CORK_CONFIG_HAVE_THREAD_STORAGE_CLASS 1
#else
#define CORK_CONFIG_HAVE_THREAD_STORAGE_CLASS 0
#endif
#else
#define CORK_CONFIG_HAVE_THREAD_STORAGE_CLASS 0
#endif
#endif /* LIBCORK_CONFIG_GCC_H */
+34
View File
@@ -0,0 +1,34 @@
/* -*- coding: utf-8 -*-
* ----------------------------------------------------------------------
* Copyright © 2011, RedJack, LLC.
* All rights reserved.
*
* Please see the COPYING file in this distribution for license
* details.
* ----------------------------------------------------------------------
*/
#ifndef LIBCORK_CONFIG_LINUX_H
#define LIBCORK_CONFIG_LINUX_H
/*-----------------------------------------------------------------------
* Endianness
*/
#include <endian.h>
#if __BYTE_ORDER == __BIG_ENDIAN
#define CORK_CONFIG_IS_BIG_ENDIAN 1
#define CORK_CONFIG_IS_LITTLE_ENDIAN 0
#elif __BYTE_ORDER == __LITTLE_ENDIAN
#define CORK_CONFIG_IS_BIG_ENDIAN 0
#define CORK_CONFIG_IS_LITTLE_ENDIAN 1
#else
#error "Cannot determine system endianness"
#endif
#define CORK_HAVE_REALLOCF 0
#define CORK_HAVE_PTHREADS 1
#endif /* LIBCORK_CONFIG_LINUX_H */
+34
View File
@@ -0,0 +1,34 @@
/* -*- coding: utf-8 -*-
* ----------------------------------------------------------------------
* Copyright © 2011, RedJack, LLC.
* All rights reserved.
*
* Please see the COPYING file in this distribution for license
* details.
* ----------------------------------------------------------------------
*/
#ifndef LIBCORK_CONFIG_MACOSX_H
#define LIBCORK_CONFIG_MACOSX_H
/*-----------------------------------------------------------------------
* Endianness
*/
#include <machine/endian.h>
#if BYTE_ORDER == BIG_ENDIAN
#define CORK_CONFIG_IS_BIG_ENDIAN 1
#define CORK_CONFIG_IS_LITTLE_ENDIAN 0
#elif BYTE_ORDER == LITTLE_ENDIAN
#define CORK_CONFIG_IS_BIG_ENDIAN 0
#define CORK_CONFIG_IS_LITTLE_ENDIAN 1
#else
#error "Cannot determine system endianness"
#endif
#define CORK_HAVE_REALLOCF 1
#define CORK_HAVE_PTHREADS 1
#endif /* LIBCORK_CONFIG_MACOSX_H */
+29
View File
@@ -0,0 +1,29 @@
/* -*- coding: utf-8 -*-
* ----------------------------------------------------------------------
* Copyright © 2011-2013, RedJack, LLC.
* All rights reserved.
*
* Please see the COPYING file in this distribution for license details.
* ----------------------------------------------------------------------
*/
#ifndef LIBCORK_CORE_H
#define LIBCORK_CORE_H
/*** include all of the parts ***/
#include <libcork/core/allocator.h>
#include <libcork/core/attributes.h>
#include <libcork/core/byte-order.h>
#include <libcork/core/callbacks.h>
#include <libcork/core/error.h>
#include <libcork/core/gc.h>
#include <libcork/core/hash.h>
#include <libcork/core/id.h>
#include <libcork/core/mempool.h>
#include <libcork/core/net-addresses.h>
#include <libcork/core/timestamp.h>
#include <libcork/core/types.h>
#include <libcork/core/u128.h>
#endif /* LIBCORK_CORE_H */
+26
View File
@@ -0,0 +1,26 @@
/* -*- coding: utf-8 -*-
* ----------------------------------------------------------------------
* Copyright © 2011, RedJack, LLC.
* All rights reserved.
*
* Please see the COPYING file in this distribution for license
* details.
* ----------------------------------------------------------------------
*/
#ifndef LIBCORK_DS_H
#define LIBCORK_DS_H
/*** include all of the parts ***/
#include <libcork/ds/array.h>
#include <libcork/ds/bitset.h>
#include <libcork/ds/buffer.h>
#include <libcork/ds/dllist.h>
#include <libcork/ds/hash-table.h>
#include <libcork/ds/managed-buffer.h>
#include <libcork/ds/ring-buffer.h>
#include <libcork/ds/slice.h>
#include <libcork/ds/stream.h>
#endif /* LIBCORK_DS_H */
+161
View File
@@ -0,0 +1,161 @@
/* -*- coding: utf-8 -*-
* ----------------------------------------------------------------------
* Copyright © 2011-2013, RedJack, LLC.
* All rights reserved.
*
* Please see the COPYING file in this distribution for license details.
* ----------------------------------------------------------------------
*/
#ifndef LIBCORK_DS_ARRAY_H
#define LIBCORK_DS_ARRAY_H
#include <libcork/core/api.h>
#include <libcork/core/callbacks.h>
#include <libcork/core/types.h>
/*-----------------------------------------------------------------------
* Resizable arrays
*/
struct cork_array_priv;
struct cork_raw_array {
void *items;
size_t size;
struct cork_array_priv *priv;
};
CORK_API void
cork_raw_array_init(struct cork_raw_array *array, size_t element_size);
CORK_API void
cork_raw_array_done(struct cork_raw_array *array);
CORK_API void
cork_raw_array_set_callback_data(struct cork_raw_array *array,
void *user_data, cork_free_f free_user_data);
CORK_API void
cork_raw_array_set_init(struct cork_raw_array *array, cork_init_f init);
CORK_API void
cork_raw_array_set_done(struct cork_raw_array *array, cork_done_f done);
CORK_API void
cork_raw_array_set_reuse(struct cork_raw_array *array, cork_init_f reuse);
CORK_API void
cork_raw_array_set_remove(struct cork_raw_array *array, cork_done_f remove);
CORK_API size_t
cork_raw_array_element_size(const struct cork_raw_array *array);
CORK_API void
cork_raw_array_clear(struct cork_raw_array *array);
CORK_API void *
cork_raw_array_elements(const struct cork_raw_array *array);
CORK_API void *
cork_raw_array_at(const struct cork_raw_array *array, size_t index);
CORK_API size_t
cork_raw_array_size(const struct cork_raw_array *array);
CORK_API bool
cork_raw_array_is_empty(const struct cork_raw_array *array);
CORK_API void
cork_raw_array_ensure_size(struct cork_raw_array *array, size_t count);
CORK_API void *
cork_raw_array_append(struct cork_raw_array *array);
CORK_API int
cork_raw_array_copy(struct cork_raw_array *dest,
const struct cork_raw_array *src,
cork_copy_f copy, void *user_data);
/*-----------------------------------------------------------------------
* Type-checked resizable arrays
*/
#define cork_array(T) \
struct { \
T *items; \
size_t size; \
struct cork_array_priv *priv; \
}
#define cork_array_element_size(arr) (sizeof((arr)->items[0]))
#define cork_array_elements(arr) ((arr)->items)
#define cork_array_at(arr, i) ((arr)->items[(i)])
#define cork_array_size(arr) ((arr)->size)
#define cork_array_is_empty(arr) ((arr)->size == 0)
#define cork_array_to_raw(arr) ((struct cork_raw_array *) (void *) (arr))
#define cork_array_init(arr) \
(cork_raw_array_init(cork_array_to_raw(arr), cork_array_element_size(arr)))
#define cork_array_done(arr) \
(cork_raw_array_done(cork_array_to_raw(arr)))
#define cork_array_set_callback_data(arr, ud, fud) \
(cork_raw_array_set_callback_data(cork_array_to_raw(arr), (ud), (fud)))
#define cork_array_set_init(arr, i) \
(cork_raw_array_set_init(cork_array_to_raw(arr), (i)))
#define cork_array_set_done(arr, d) \
(cork_raw_array_set_done(cork_array_to_raw(arr), (d)))
#define cork_array_set_reuse(arr, r) \
(cork_raw_array_set_reuse(cork_array_to_raw(arr), (r)))
#define cork_array_set_remove(arr, r) \
(cork_raw_array_set_remove(cork_array_to_raw(arr), (r)))
#define cork_array_clear(arr) \
(cork_raw_array_clear(cork_array_to_raw(arr)))
#define cork_array_copy(d, s, c, ud) \
(cork_raw_array_copy(cork_array_to_raw(d), cork_array_to_raw(s), (c), (ud)))
#define cork_array_ensure_size(arr, count) \
(cork_raw_array_ensure_size(cork_array_to_raw(arr), (count)))
#define cork_array_append(arr, element) \
(cork_raw_array_append(cork_array_to_raw(arr)), \
((arr)->items[(arr)->size - 1] = (element), (void) 0))
#define cork_array_append_get(arr) \
(cork_raw_array_append(cork_array_to_raw(arr)), \
&(arr)->items[(arr)->size - 1])
/*-----------------------------------------------------------------------
* Builtin array types
*/
CORK_API void
cork_raw_pointer_array_init(struct cork_raw_array *array, cork_free_f free);
#define cork_pointer_array_init(arr, f) \
(cork_raw_pointer_array_init(cork_array_to_raw(arr), (f)))
struct cork_string_array {
const char **items;
size_t size;
struct cork_array_priv *priv;
};
CORK_API void
cork_string_array_init(struct cork_string_array *array);
CORK_API void
cork_string_array_append(struct cork_string_array *array, const char *str);
CORK_API void
cork_string_array_copy(struct cork_string_array *dest,
const struct cork_string_array *src);
#endif /* LIBCORK_DS_ARRAY_H */
+64
View File
@@ -0,0 +1,64 @@
/* -*- coding: utf-8 -*-
* ----------------------------------------------------------------------
* Copyright © 2013, RedJack, LLC.
* All rights reserved.
*
* Please see the COPYING file in this distribution for license details.
* ----------------------------------------------------------------------
*/
#ifndef LIBCORK_DS_BITS_H
#define LIBCORK_DS_BITS_H
#include <libcork/core/api.h>
#include <libcork/core/types.h>
/*-----------------------------------------------------------------------
* Bit sets
*/
struct cork_bitset {
uint8_t *bits;
size_t bit_count;
size_t byte_count;
};
CORK_API struct cork_bitset *
cork_bitset_new(size_t bit_count);
CORK_API void
cork_bitset_free(struct cork_bitset *set);
CORK_API void
cork_bitset_clear(struct cork_bitset *set);
/* Extract the byte that contains a particular bit in an array. */
#define cork_bitset_byte_for_bit(set, i) \
((set)->bits[(i) / 8])
/* Create a bit mask that extracts a particular bit from the byte that it lives
* in. */
#define cork_bitset_pos_mask_for_bit(i) \
(0x80 >> ((i) % 8))
/* Create a bit mask that extracts everything except for a particular bit from
* the byte that it lives in. */
#define cork_bitset_neg_mask_for_bit(i) \
(~cork_bitset_pos_mask_for_bit(i))
/* Return whether a particular bit is set in a byte array. Bits are numbered
* from 0, in a big-endian order. */
#define cork_bitset_get(set, i) \
((cork_bitset_byte_for_bit(set, i) & cork_bitset_pos_mask_for_bit(i)) != 0)
/* Set (or unset) a particular bit is set in a byte array. Bits are numbered
* from 0, in a big-endian order. */
#define cork_bitset_set(set, i, val) \
(cork_bitset_byte_for_bit(set, i) = \
(cork_bitset_byte_for_bit(set, i) & cork_bitset_neg_mask_for_bit(i)) \
| ((val)? cork_bitset_pos_mask_for_bit(i): 0))
#endif /* LIBCORK_DS_BITS_H */
+163
View File
@@ -0,0 +1,163 @@
/* -*- coding: utf-8 -*-
* ----------------------------------------------------------------------
* Copyright © 2011-2012, RedJack, LLC.
* All rights reserved.
*
* Please see the COPYING file in this distribution for license
* details.
* ----------------------------------------------------------------------
*/
#ifndef LIBCORK_DS_BUFFER_H
#define LIBCORK_DS_BUFFER_H
#include <stdarg.h>
#include <libcork/core/api.h>
#include <libcork/core/attributes.h>
#include <libcork/core/types.h>
struct cork_buffer {
/* The current contents of the buffer. */
void *buf;
/* The current size of the buffer. */
size_t size;
/* The amount of space allocated for buf. */
size_t allocated_size;
};
CORK_API void
cork_buffer_init(struct cork_buffer *buffer);
#define CORK_BUFFER_INIT() { NULL, 0, 0 }
CORK_API struct cork_buffer *
cork_buffer_new(void);
CORK_API void
cork_buffer_done(struct cork_buffer *buffer);
CORK_API void
cork_buffer_free(struct cork_buffer *buffer);
CORK_API bool
cork_buffer_equal(const struct cork_buffer *buffer1,
const struct cork_buffer *buffer2);
CORK_API void
cork_buffer_ensure_size(struct cork_buffer *buffer, size_t desired_size);
CORK_API void
cork_buffer_clear(struct cork_buffer *buffer);
CORK_API void
cork_buffer_truncate(struct cork_buffer *buffer, size_t length);
#define cork_buffer_byte(buffer, i) (((const uint8_t *) (buffer)->buf)[(i)])
#define cork_buffer_char(buffer, i) (((const char *) (buffer)->buf)[(i)])
/*-----------------------------------------------------------------------
* A whole bunch of methods for adding data
*/
#define cork_buffer_copy(dest, src) \
(cork_buffer_set((dest), (src)->buf, (src)->size))
CORK_API void
cork_buffer_set(struct cork_buffer *buffer, const void *src, size_t length);
#define cork_buffer_append_copy(dest, src) \
(cork_buffer_append((dest), (src)->buf, (src)->size))
CORK_API void
cork_buffer_append(struct cork_buffer *buffer, const void *src, size_t length);
CORK_API void
cork_buffer_set_string(struct cork_buffer *buffer, const char *str);
CORK_API void
cork_buffer_append_string(struct cork_buffer *buffer, const char *str);
#define cork_buffer_set_literal(buffer, str) \
(cork_buffer_set((buffer), (str), sizeof((str)) - 1))
#define cork_buffer_append_literal(buffer, str) \
(cork_buffer_append((buffer), (str), sizeof((str)) - 1))
CORK_API void
cork_buffer_printf(struct cork_buffer *buffer, const char *format, ...)
CORK_ATTR_PRINTF(2,3);
CORK_API void
cork_buffer_append_printf(struct cork_buffer *buffer, const char *format, ...)
CORK_ATTR_PRINTF(2,3);
CORK_API void
cork_buffer_vprintf(struct cork_buffer *buffer, const char *format,
va_list args)
CORK_ATTR_PRINTF(2,0);
CORK_API void
cork_buffer_append_vprintf(struct cork_buffer *buffer, const char *format,
va_list args)
CORK_ATTR_PRINTF(2,0);
/*-----------------------------------------------------------------------
* Some helpers for pretty-printing data
*/
CORK_API void
cork_buffer_append_indent(struct cork_buffer *buffer, size_t indent);
CORK_API void
cork_buffer_append_c_string(struct cork_buffer *buffer,
const char *src, size_t length);
CORK_API void
cork_buffer_append_hex_dump(struct cork_buffer *buffer, size_t indent,
const char *src, size_t length);
CORK_API void
cork_buffer_append_multiline(struct cork_buffer *buffer, size_t indent,
const char *src, size_t length);
CORK_API void
cork_buffer_append_binary(struct cork_buffer *buffer, size_t indent,
const char *src, size_t length);
/*-----------------------------------------------------------------------
* Buffer's managed buffer/slice implementation
*/
#include <libcork/ds/managed-buffer.h>
#include <libcork/ds/slice.h>
CORK_API struct cork_managed_buffer *
cork_buffer_to_managed_buffer(struct cork_buffer *buffer);
CORK_API int
cork_buffer_to_slice(struct cork_buffer *buffer, struct cork_slice *slice);
/*-----------------------------------------------------------------------
* Buffer's stream consumer implementation
*/
#include <libcork/ds/stream.h>
CORK_API struct cork_stream_consumer *
cork_buffer_to_stream_consumer(struct cork_buffer *buffer);
#endif /* LIBCORK_DS_BUFFER_H */
+151
View File
@@ -0,0 +1,151 @@
/* -*- coding: utf-8 -*-
* ----------------------------------------------------------------------
* Copyright © 2011-2014, RedJack, LLC.
* All rights reserved.
*
* Please see the COPYING file in this distribution for license details.
* ----------------------------------------------------------------------
*/
#ifndef LIBCORK_DS_DLLIST_H
#define LIBCORK_DS_DLLIST_H
#include <libcork/core/api.h>
#include <libcork/core/types.h>
struct cork_dllist_item {
/* A pointer to the next element in the list. */
struct cork_dllist_item *next;
/* A pointer to the previous element in the list. */
struct cork_dllist_item *prev;
};
struct cork_dllist {
/* The sentinel element for this list. */
struct cork_dllist_item head;
};
#define CORK_DLLIST_INIT(list) { { &(list).head, &(list).head } }
#define cork_dllist_init(list) \
do { \
(list)->head.next = &(list)->head; \
(list)->head.prev = &(list)->head; \
} while (0)
/* DEPRECATED! Use cork_dllist_foreach or cork_dllist_visit instead. */
typedef void
(*cork_dllist_map_func)(struct cork_dllist_item *element, void *user_data);
CORK_API void
cork_dllist_map(struct cork_dllist *list,
cork_dllist_map_func func, void *user_data);
typedef int
cork_dllist_visit_f(void *ud, struct cork_dllist_item *element);
CORK_API int
cork_dllist_visit(struct cork_dllist *list, void *ud,
cork_dllist_visit_f *visit);
#define cork_dllist_foreach_void(list, curr, _next) \
for ((curr) = cork_dllist_start((list)), (_next) = (curr)->next; \
!cork_dllist_is_end((list), (curr)); \
(curr) = (_next), (_next) = (curr)->next)
#define cork_dllist_foreach(list, curr, _next, etype, element, item_field) \
for ((curr) = cork_dllist_start((list)), (_next) = (curr)->next, \
(element) = cork_container_of((curr), etype, item_field); \
!cork_dllist_is_end((list), (curr)); \
(curr) = (_next), (_next) = (curr)->next, \
(element) = cork_container_of((curr), etype, item_field))
CORK_API size_t
cork_dllist_size(const struct cork_dllist *list);
#define cork_dllist_add_after(pred, element) \
do { \
(element)->prev = (pred); \
(element)->next = (pred)->next; \
(pred)->next->prev = (element); \
(pred)->next = (element); \
} while (0)
#define cork_dllist_add_before(succ, element) \
do { \
(element)->next = (succ); \
(element)->prev = (succ)->prev; \
(succ)->prev->next = (element); \
(succ)->prev = (element); \
} while (0)
#define cork_dllist_add_to_head(list, element) \
cork_dllist_add_after(&(list)->head, (element))
#define cork_dllist_add_to_tail(list, element) \
cork_dllist_add_before(&(list)->head, (element))
#define cork_dllist_add cork_dllist_add_to_tail
#define cork_dllist_add_list_to_head(dest, src) \
do { \
struct cork_dllist_item *dest_start = cork_dllist_start(dest); \
struct cork_dllist_item *src_start = cork_dllist_start(src); \
dest_start->prev = &(src)->head; \
src_start->prev = &(dest)->head; \
(src)->head.next = dest_start; \
(dest)->head.next = src_start; \
cork_dllist_remove(&(src)->head); \
cork_dllist_init(src); \
} while (0)
#define cork_dllist_add_list_to_tail(dest, src) \
do { \
struct cork_dllist_item *dest_end = cork_dllist_end(dest); \
struct cork_dllist_item *src_end = cork_dllist_end(src); \
dest_end->next = &(src)->head; \
src_end->next = &(dest)->head; \
(src)->head.prev = dest_end; \
(dest)->head.prev = src_end; \
cork_dllist_remove(&(src)->head); \
cork_dllist_init(src); \
} while (0)
#define cork_dllist_remove(element) \
do { \
(element)->prev->next = (element)->next; \
(element)->next->prev = (element)->prev; \
} while (0)
#define cork_dllist_is_empty(list) \
(cork_dllist_is_end((list), cork_dllist_start((list))))
#define cork_dllist_head(list) \
(((list)->head.next == &(list)->head)? NULL: (list)->head.next)
#define cork_dllist_tail(list) \
(((list)->head.prev == &(list)->head)? NULL: (list)->head.prev)
#define cork_dllist_start(list) \
((list)->head.next)
#define cork_dllist_end(list) \
((list)->head.prev)
#define cork_dllist_is_start(list, element) \
((element) == &(list)->head)
#define cork_dllist_is_end(list, element) \
((element) == &(list)->head)
#endif /* LIBCORK_DS_DLLIST_H */
+159
View File
@@ -0,0 +1,159 @@
/* -*- coding: utf-8 -*-
* ----------------------------------------------------------------------
* Copyright © 2011-2013, RedJack, LLC.
* All rights reserved.
*
* Please see the COPYING file in this distribution for license details.
* ----------------------------------------------------------------------
*/
#ifndef LIBCORK_DS_HASH_TABLE_H
#define LIBCORK_DS_HASH_TABLE_H
#include <libcork/core/api.h>
#include <libcork/core/callbacks.h>
#include <libcork/core/hash.h>
#include <libcork/core/mempool.h>
#include <libcork/core/types.h>
#include <libcork/ds/dllist.h>
/*-----------------------------------------------------------------------
* Hash tables
*/
struct cork_hash_table_entry {
cork_hash hash;
void *key;
void *value;
};
struct cork_hash_table;
CORK_API struct cork_hash_table *
cork_hash_table_new(size_t initial_size, unsigned int flags);
CORK_API void
cork_hash_table_free(struct cork_hash_table *table);
CORK_API void
cork_hash_table_set_user_data(struct cork_hash_table *table,
void *user_data, cork_free_f free_user_data);
CORK_API void
cork_hash_table_set_equals(struct cork_hash_table *table, cork_equals_f equals);
CORK_API void
cork_hash_table_set_free_key(struct cork_hash_table *table, cork_free_f free);
CORK_API void
cork_hash_table_set_free_value(struct cork_hash_table *table, cork_free_f free);
CORK_API void
cork_hash_table_set_hash(struct cork_hash_table *table, cork_hash_f hash);
CORK_API void
cork_hash_table_clear(struct cork_hash_table *table);
CORK_API void
cork_hash_table_ensure_size(struct cork_hash_table *table,
size_t desired_count);
CORK_API size_t
cork_hash_table_size(const struct cork_hash_table *table);
CORK_API void *
cork_hash_table_get(const struct cork_hash_table *table, const void *key);
CORK_API void *
cork_hash_table_get_hash(const struct cork_hash_table *table,
cork_hash hash, const void *key);
CORK_API struct cork_hash_table_entry *
cork_hash_table_get_entry(const struct cork_hash_table *table,
const void *key);
CORK_API struct cork_hash_table_entry *
cork_hash_table_get_entry_hash(const struct cork_hash_table *table,
cork_hash hash, const void *key);
CORK_API struct cork_hash_table_entry *
cork_hash_table_get_or_create(struct cork_hash_table *table,
void *key, bool *is_new);
CORK_API struct cork_hash_table_entry *
cork_hash_table_get_or_create_hash(struct cork_hash_table *table,
cork_hash hash, void *key, bool *is_new);
CORK_API void
cork_hash_table_put(struct cork_hash_table *table,
void *key, void *value,
bool *is_new, void **old_key, void **old_value);
CORK_API void
cork_hash_table_put_hash(struct cork_hash_table *table,
cork_hash hash, void *key, void *value,
bool *is_new, void **old_key, void **old_value);
CORK_API void
cork_hash_table_delete_entry(struct cork_hash_table *table,
struct cork_hash_table_entry *entry);
CORK_API bool
cork_hash_table_delete(struct cork_hash_table *table, const void *key,
void **deleted_key, void **deleted_value);
CORK_API bool
cork_hash_table_delete_hash(struct cork_hash_table *table,
cork_hash hash, const void *key,
void **deleted_key, void **deleted_value);
enum cork_hash_table_map_result {
/* Abort the current @ref cork_hash_table_map operation. */
CORK_HASH_TABLE_MAP_ABORT = 0,
/* Continue on to the next entry in the hash table. */
CORK_HASH_TABLE_MAP_CONTINUE = 1,
/* Delete the entry that was just processed, and then continue on to
* the next entry in the hash table. */
CORK_HASH_TABLE_MAP_DELETE = 2
};
typedef enum cork_hash_table_map_result
(*cork_hash_table_map_f)(void *user_data, struct cork_hash_table_entry *entry);
CORK_API void
cork_hash_table_map(struct cork_hash_table *table, void *user_data,
cork_hash_table_map_f mapper);
struct cork_hash_table_iterator {
struct cork_hash_table *table;
void *priv;
};
CORK_API void
cork_hash_table_iterator_init(struct cork_hash_table *table,
struct cork_hash_table_iterator *iterator);
CORK_API struct cork_hash_table_entry *
cork_hash_table_iterator_next(struct cork_hash_table_iterator *iterator);
/*-----------------------------------------------------------------------
* Built-in key types
*/
CORK_API struct cork_hash_table *
cork_string_hash_table_new(size_t initial_size, unsigned int flags);
CORK_API struct cork_hash_table *
cork_pointer_hash_table_new(size_t initial_size, unsigned int flags);
#endif /* LIBCORK_DS_HASH_TABLE_H */
@@ -0,0 +1,76 @@
/* -*- coding: utf-8 -*-
* ----------------------------------------------------------------------
* Copyright © 2011, RedJack, LLC.
* All rights reserved.
*
* Please see the COPYING file in this distribution for license
* details.
* ----------------------------------------------------------------------
*/
#ifndef LIBCORK_DS_MANAGED_BUFFER_H
#define LIBCORK_DS_MANAGED_BUFFER_H
#include <libcork/core/api.h>
#include <libcork/core/types.h>
#include <libcork/ds/slice.h>
/*-----------------------------------------------------------------------
* Managed buffers
*/
struct cork_managed_buffer;
struct cork_managed_buffer_iface {
/* Free the contents of a managed buffer, and the managed buffer
* object itself. */
void
(*free)(struct cork_managed_buffer *buf);
};
struct cork_managed_buffer {
/* The buffer that this instance manages */
const void *buf;
/* The size of buf */
size_t size;
/* A reference count for the buffer. If this drops to 0, the buffer
* will be finalized. */
volatile int ref_count;
/* The managed buffer implementation for this instance. */
struct cork_managed_buffer_iface *iface;
};
CORK_API struct cork_managed_buffer *
cork_managed_buffer_new_copy(const void *buf, size_t size);
typedef void
(*cork_managed_buffer_freer)(void *buf, size_t size);
CORK_API struct cork_managed_buffer *
cork_managed_buffer_new(const void *buf, size_t size,
cork_managed_buffer_freer free);
CORK_API struct cork_managed_buffer *
cork_managed_buffer_ref(struct cork_managed_buffer *buf);
CORK_API void
cork_managed_buffer_unref(struct cork_managed_buffer *buf);
CORK_API int
cork_managed_buffer_slice(struct cork_slice *dest,
struct cork_managed_buffer *buffer,
size_t offset, size_t length);
CORK_API int
cork_managed_buffer_slice_offset(struct cork_slice *dest,
struct cork_managed_buffer *buffer,
size_t offset);
#endif /* LIBCORK_DS_MANAGED_BUFFER_H */
+60
View File
@@ -0,0 +1,60 @@
/* -*- coding: utf-8 -*-
* ----------------------------------------------------------------------
* Copyright © 2011, RedJack, LLC.
* All rights reserved.
*
* Please see the COPYING file in this distribution for license
* details.
* ----------------------------------------------------------------------
*/
#ifndef LIBCORK_DS_RING_BUFFER_H
#define LIBCORK_DS_RING_BUFFER_H
#include <libcork/core/api.h>
#include <libcork/core/types.h>
struct cork_ring_buffer {
/* The elements of the ring buffer */
void **elements;
/* The number of elements that can be stored in this ring
* buffer. */
size_t allocated_size;
/* The actual number of elements currently in the ring buffer. */
size_t size;
/* The index of the next element to read from the buffer */
size_t read_index;
/* The index of the next element to write into the buffer */
size_t write_index;
};
CORK_API int
cork_ring_buffer_init(struct cork_ring_buffer *buf, size_t size);
CORK_API struct cork_ring_buffer *
cork_ring_buffer_new(size_t size);
CORK_API void
cork_ring_buffer_done(struct cork_ring_buffer *buf);
CORK_API void
cork_ring_buffer_free(struct cork_ring_buffer *buf);
#define cork_ring_buffer_is_empty(buf) ((buf)->size == 0)
#define cork_ring_buffer_is_full(buf) ((buf)->size == (buf)->allocated_size)
CORK_API int
cork_ring_buffer_add(struct cork_ring_buffer *buf, void *element);
CORK_API void *
cork_ring_buffer_pop(struct cork_ring_buffer *buf);
CORK_API void *
cork_ring_buffer_peek(struct cork_ring_buffer *buf);
#endif /* LIBCORK_DS_RING_BUFFER_H */
+151
View File
@@ -0,0 +1,151 @@
/* -*- coding: utf-8 -*-
* ----------------------------------------------------------------------
* Copyright © 2011, RedJack, LLC.
* All rights reserved.
*
* Please see the COPYING file in this distribution for license
* details.
* ----------------------------------------------------------------------
*/
#ifndef LIBCORK_DS_SLICE_H
#define LIBCORK_DS_SLICE_H
#include <libcork/core/api.h>
#include <libcork/core/types.h>
/*-----------------------------------------------------------------------
* Error handling
*/
/* hash of "libcork/ds/slice.h" */
#define CORK_SLICE_ERROR 0x960ca750
enum cork_slice_error {
/* Trying to slice a nonexistent subset of a buffer */
CORK_SLICE_INVALID_SLICE
};
/*-----------------------------------------------------------------------
* Slices
*/
struct cork_slice;
struct cork_slice_iface {
/* Free the slice. Can be NULL if you don't need to free any
* underlying buffer. */
void
(*free)(struct cork_slice *self);
/* Create a copy of a slice. You can assume that offset and length
* refer to a valid subset of the buffer. */
int
(*copy)(struct cork_slice *dest, const struct cork_slice *self,
size_t offset, size_t length);
/* Create a “light” copy of a slice. A light copy is not allowed to exist
* longer than the slice that it was copied from, which can sometimes let
* you perform less work to produce the copy. You can assume that offset
* and length refer to a valid subset of the buffer. */
int
(*light_copy)(struct cork_slice *dest, const struct cork_slice *self,
size_t offset, size_t length);
/* Update the current slice to point at a different subset. You can
* assume that offset and length refer to a valid subset of the
* buffer. Can be NULL if you don't need to do anything special to
* the underlying buffer; in this case, we'll update the slice's buf
* and size fields for you. */
int
(*slice)(struct cork_slice *self, size_t offset, size_t length);
};
struct cork_slice {
/* The beginning of the sliced portion of the buffer. */
const void *buf;
/* The length of the sliced portion of the buffer. */
size_t size;
/* The slice implementation of the underlying buffer. */
struct cork_slice_iface *iface;
/* An opaque pointer used by the slice implementation to refer to
* the underlying buffer. */
void *user_data;
};
CORK_API void
cork_slice_clear(struct cork_slice *slice);
#define cork_slice_is_empty(slice) ((slice)->buf == NULL)
CORK_API int
cork_slice_copy(struct cork_slice *dest, const struct cork_slice *slice,
size_t offset, size_t length);
#define cork_slice_copy_fast(dest, slice, offset, length) \
((slice)->iface->copy((dest), (slice), (offset), (length)))
CORK_API int
cork_slice_copy_offset(struct cork_slice *dest, const struct cork_slice *slice,
size_t offset);
#define cork_slice_copy_offset_fast(dest, slice, offset) \
((slice)->iface->copy \
((dest), (slice), (offset), (slice)->size - (offset)))
CORK_API int
cork_slice_light_copy(struct cork_slice *dest, const struct cork_slice *slice,
size_t offset, size_t length);
#define cork_slice_light_copy_fast(dest, slice, offset, length) \
((slice)->iface->light_copy((dest), (slice), (offset), (length)))
CORK_API int
cork_slice_light_copy_offset(struct cork_slice *dest,
const struct cork_slice *slice, size_t offset);
#define cork_slice_light_copy_offset_fast(dest, slice, offset) \
((slice)->iface->light_copy \
((dest), (slice), (offset), (slice)->size - (offset)))
CORK_API int
cork_slice_slice(struct cork_slice *slice, size_t offset, size_t length);
#define cork_slice_slice_fast(_slice, offset, length) \
((_slice)->iface->slice == NULL? \
((_slice)->buf += (offset), (_slice)->size = (length), 0): \
((_slice)->iface->slice((_slice), (offset), (length))))
CORK_API int
cork_slice_slice_offset(struct cork_slice *slice, size_t offset);
#define cork_slice_slice_offset_fast(_slice, offset) \
((_slice)->iface->slice == NULL? \
((_slice)->buf += (offset), (_slice)->size -= (offset), 0): \
((_slice)->iface->slice \
((_slice), (offset), (_slice)->size - (offset))))
CORK_API void
cork_slice_finish(struct cork_slice *slice);
CORK_API bool
cork_slice_equal(const struct cork_slice *slice1,
const struct cork_slice *slice2);
CORK_API void
cork_slice_init_static(struct cork_slice *dest, const void *buf, size_t size);
CORK_API void
cork_slice_init_copy_once(struct cork_slice *dest, const void *buf,
size_t size);
#endif /* LIBCORK_DS_SLICE_H */
+64
View File
@@ -0,0 +1,64 @@
/* -*- coding: utf-8 -*-
* ----------------------------------------------------------------------
* Copyright © 2011, RedJack, LLC.
* All rights reserved.
*
* Please see the COPYING file in this distribution for license
* details.
* ----------------------------------------------------------------------
*/
#ifndef LIBCORK_DS_STREAM_H
#define LIBCORK_DS_STREAM_H
#include <stdio.h>
#include <libcork/core/api.h>
#include <libcork/core/types.h>
struct cork_stream_consumer {
int
(*data)(struct cork_stream_consumer *consumer,
const void *buf, size_t size, bool is_first_chunk);
int
(*eof)(struct cork_stream_consumer *consumer);
void
(*free)(struct cork_stream_consumer *consumer);
};
#define cork_stream_consumer_data(consumer, buf, size, is_first) \
((consumer)->data((consumer), (buf), (size), (is_first)))
#define cork_stream_consumer_eof(consumer) \
((consumer)->eof((consumer)))
#define cork_stream_consumer_free(consumer) \
((consumer)->free((consumer)))
CORK_API int
cork_consume_fd(struct cork_stream_consumer *consumer, int fd);
CORK_API int
cork_consume_file(struct cork_stream_consumer *consumer, FILE *fp);
CORK_API int
cork_consume_file_from_path(struct cork_stream_consumer *consumer,
const char *path, int flags);
CORK_API struct cork_stream_consumer *
cork_fd_consumer_new(int fd);
CORK_API struct cork_stream_consumer *
cork_file_consumer_new(FILE *fp);
CORK_API struct cork_stream_consumer *
cork_file_from_path_consumer_new(const char *path, int flags);
#endif /* LIBCORK_DS_STREAM_H */
+142
View File
@@ -0,0 +1,142 @@
/* -*- coding: utf-8 -*-
* ----------------------------------------------------------------------
* Copyright © 2011-2012, RedJack, LLC.
* All rights reserved.
*
* Please see the COPYING file in this distribution for license
* details.
* ----------------------------------------------------------------------
*/
#ifndef LIBCORK_HELPERS_ERRORS_H
#define LIBCORK_HELPERS_ERRORS_H
/* This header is *not* automatically included when you include
* libcork/core.h, since we define some macros that don't include a
* cork_ or CORK_ prefix. Don't want to pollute your namespace unless
* you ask for it! */
#include <libcork/core/allocator.h>
#include <libcork/core/attributes.h>
#include <libcork/core/error.h>
#if !defined(CORK_PRINT_ERRORS)
#define CORK_PRINT_ERRORS 0
#endif
#if !defined(CORK_PRINT_ERROR)
#if CORK_PRINT_ERRORS
#include <stdio.h>
#define CORK_PRINT_ERROR_(func, file, line) \
fprintf(stderr, "---\nError in %s (%s:%u)\n %s\n", \
(func), (file), (unsigned int) (line), \
cork_error_message());
#define CORK_PRINT_ERROR() CORK_PRINT_ERROR_(__func__, __FILE__, __LINE__)
#else
#define CORK_PRINT_ERROR() /* do nothing */
#endif
#endif
/* A bunch of macros for calling a function that returns an error. If
* an error occurs, it will automatically be propagated out as the
* result of your own function. With these macros, you won't have a
* check to check or modify the error condition; it's returned as-is.
*
* XZ_check
*
* where:
*
* X = what happens if an error occurs
* "e" = jump to the "error" label
* "rY" = return a default error result (Y defined below)
* "x" = return an error result that you specify
*
* Y = your return type
* "i" = int
* "p" = some pointer type
*
* Z = the return type of the function you're calling
* "e" = use cork_error_occurred() to check
* "i" = int
* "p" = some pointer type
*
* In all cases, we assume that your function has a cork_error parameter
* called "err".
*/
/* jump to "error" label */
#define ee_check(call) \
do { \
(call); \
if (CORK_UNLIKELY(cork_error_occurred())) { \
CORK_PRINT_ERROR(); \
goto error; \
} \
} while (0)
#define ei_check(call) \
do { \
int __rc = (call); \
if (CORK_UNLIKELY(__rc != 0)) { \
CORK_PRINT_ERROR(); \
goto error; \
} \
} while (0)
#define ep_check(call) \
do { \
const void *__result = (call); \
if (CORK_UNLIKELY(__result == NULL)) { \
CORK_PRINT_ERROR(); \
goto error; \
} \
} while (0)
/* return specific error code */
#define xe_check(result, call) \
do { \
(call); \
if (CORK_UNLIKELY(cork_error_occurred())) { \
CORK_PRINT_ERROR(); \
return result; \
} \
} while (0)
#define xi_check(result, call) \
do { \
int __rc = (call); \
if (CORK_UNLIKELY(__rc != 0)) { \
CORK_PRINT_ERROR(); \
return result; \
} \
} while (0)
#define xp_check(result, call) \
do { \
const void *__result = (call); \
if (CORK_UNLIKELY(__result == NULL)) { \
CORK_PRINT_ERROR(); \
return result; \
} \
} while (0)
/* return default error code */
#define rie_check(call) xe_check(-1, call)
#define rii_check(call) xi_check(__rc, call)
#define rip_check(call) xp_check(-1, call)
#define rpe_check(call) xe_check(NULL, call)
#define rpi_check(call) xi_check(NULL, call)
#define rpp_check(call) xp_check(NULL, call)
#endif /* LIBCORK_HELPERS_ERRORS_H */
+51
View File
@@ -0,0 +1,51 @@
/* -*- coding: utf-8 -*-
* ----------------------------------------------------------------------
* Copyright © 2011-2012, RedJack, LLC.
* All rights reserved.
*
* Please see the COPYING file in this distribution for license
* details.
* ----------------------------------------------------------------------
*/
#ifndef LIBCORK_HELPERS_REFCOUNT_H
#define LIBCORK_HELPERS_REFCOUNT_H
#include <libcork/core/gc.h>
#include <libcork/core/types.h>
#define _free_(name) \
static void \
name##__free(void *obj)
#define _recurse_(name) \
static void \
name##__recurse(struct cork_gc *gc, void *obj, \
cork_gc_recurser recurse, void *ud)
#define _gc_(name) \
static struct cork_gc_obj_iface name##__gc = { \
name##__free, name##__recurse \
};
#define _gc_no_free_(name) \
static struct cork_gc_obj_iface name##__gc = { \
NULL, name##__recurse \
};
#define _gc_no_recurse_(name) \
static struct cork_gc_obj_iface name##__gc = { \
name##__free, NULL \
};
#define _gc_leaf_(name) \
static struct cork_gc_obj_iface name##__gc = { \
NULL, NULL \
};
#endif /* LIBCORK_HELPERS_REFCOUNT_H */
+87
View File
@@ -0,0 +1,87 @@
/* -*- coding: utf-8 -*-
* ----------------------------------------------------------------------
* Copyright © 2013, RedJack, LLC.
* All rights reserved.
*
* Please see the COPYING file in this distribution for license details.
* ----------------------------------------------------------------------
*/
#ifndef LIBCORK_HELPERS_POSIX_H
#define LIBCORK_HELPERS_POSIX_H
/* This header is *not* automatically included when you include
* libcork/core.h, since we define some macros that don't include a
* cork_ or CORK_ prefix. Don't want to pollute your namespace unless
* you ask for it! */
#include <errno.h>
#include <libcork/core/allocator.h>
#include <libcork/core/attributes.h>
#include <libcork/core/error.h>
#if !defined(CORK_PRINT_ERRORS)
#define CORK_PRINT_ERRORS 0
#endif
#if !defined(CORK_PRINT_ERROR)
#if CORK_PRINT_ERRORS
#include <stdio.h>
#define CORK_PRINT_ERROR_(func, file, line) \
fprintf(stderr, "---\nError in %s (%s:%u)\n %s\n", \
(func), (file), (unsigned int) (line), \
cork_error_message());
#define CORK_PRINT_ERROR() CORK_PRINT_ERROR_(__func__, __FILE__, __LINE__)
#else
#define CORK_PRINT_ERROR() /* do nothing */
#endif
#endif
#define xi_check_posix(call, on_error) \
do { \
while (true) { \
if ((call) == -1) { \
if (errno == EINTR) { \
continue; \
} else { \
cork_system_error_set(); \
CORK_PRINT_ERROR(); \
on_error; \
} \
} else { \
break; \
} \
} \
} while (0)
#define xp_check_posix(call, on_error) \
do { \
while (true) { \
if ((call) == NULL) { \
if (errno == EINTR) { \
continue; \
} else { \
cork_system_error_set(); \
CORK_PRINT_ERROR(); \
on_error; \
} \
} else { \
break; \
} \
} \
} while (0)
#define ei_check_posix(call) xi_check_posix(call, goto error)
#define rii_check_posix(call) xi_check_posix(call, return -1)
#define rpi_check_posix(call) xi_check_posix(call, return NULL)
#define ep_check_posix(call) xp_check_posix(call, goto error)
#define rip_check_posix(call) xp_check_posix(call, return -1)
#define rpp_check_posix(call) xp_check_posix(call, return NULL)
#endif /* LIBCORK_HELPERS_POSIX_H */
+20
View File
@@ -0,0 +1,20 @@
/* -*- coding: utf-8 -*-
* ----------------------------------------------------------------------
* Copyright © 2012-2013, RedJack, LLC.
* All rights reserved.
*
* Please see the COPYING file in this distribution for license
* details.
* ----------------------------------------------------------------------
*/
#ifndef LIBCORK_OS_H
#define LIBCORK_OS_H
/*** include all of the parts ***/
#include <libcork/os/files.h>
#include <libcork/os/process.h>
#include <libcork/os/subprocess.h>
#endif /* LIBCORK_OS_H */
+271
View File
@@ -0,0 +1,271 @@
/* -*- coding: utf-8 -*-
* ----------------------------------------------------------------------
* Copyright © 2012-2013, RedJack, LLC.
* All rights reserved.
*
* Please see the COPYING file in this distribution for license
* details.
* ----------------------------------------------------------------------
*/
#ifndef LIBCORK_CORE_FILES_H
#define LIBCORK_CORE_FILES_H
#include <libcork/core/api.h>
#include <libcork/core/types.h>
/*-----------------------------------------------------------------------
* Paths
*/
struct cork_path;
/* path can be relative or absolute */
CORK_API struct cork_path *
cork_path_new(const char *path);
CORK_API struct cork_path *
cork_path_clone(const struct cork_path *other);
CORK_API void
cork_path_free(struct cork_path *path);
CORK_API void
cork_path_set(struct cork_path *path, const char *content);
CORK_API const char *
cork_path_get(const struct cork_path *path);
CORK_API int
cork_path_set_cwd(struct cork_path *path);
CORK_API struct cork_path *
cork_path_cwd(void);
CORK_API int
cork_path_set_absolute(struct cork_path *path);
CORK_API struct cork_path *
cork_path_absolute(const struct cork_path *other);
CORK_API void
cork_path_append(struct cork_path *path, const char *more);
CORK_API void
cork_path_append_path(struct cork_path *path, const struct cork_path *more);
CORK_API struct cork_path *
cork_path_join(const struct cork_path *other, const char *more);
CORK_API struct cork_path *
cork_path_join_path(const struct cork_path *other,
const struct cork_path *more);
CORK_API void
cork_path_set_basename(struct cork_path *path);
CORK_API struct cork_path *
cork_path_basename(const struct cork_path *other);
CORK_API void
cork_path_set_dirname(struct cork_path *path);
CORK_API struct cork_path *
cork_path_dirname(const struct cork_path *other);
/*-----------------------------------------------------------------------
* Lists of paths
*/
struct cork_path_list;
CORK_API struct cork_path_list *
cork_path_list_new_empty(void);
/* list must be a colon-separated list of paths */
CORK_API struct cork_path_list *
cork_path_list_new(const char *list);
CORK_API void
cork_path_list_free(struct cork_path_list *list);
CORK_API const char *
cork_path_list_to_string(const struct cork_path_list *list);
/* Takes control of path. path must not already be in the list. */
CORK_API void
cork_path_list_add(struct cork_path_list *list, struct cork_path *path);
CORK_API size_t
cork_path_list_size(const struct cork_path_list *list);
/* The list still owns path; you must not free it or modify it. */
CORK_API const struct cork_path *
cork_path_list_get(const struct cork_path_list *list, size_t index);
/*-----------------------------------------------------------------------
* Files
*/
#define CORK_FILE_RECURSIVE 0x0001
#define CORK_FILE_PERMISSIVE 0x0002
typedef unsigned int cork_file_mode;
enum cork_file_type {
CORK_FILE_MISSING = 0,
CORK_FILE_REGULAR = 1,
CORK_FILE_DIRECTORY = 2,
CORK_FILE_SYMLINK = 3,
CORK_FILE_UNKNOWN = 4
};
struct cork_file;
CORK_API struct cork_file *
cork_file_new(const char *path);
/* Takes control of path */
CORK_API struct cork_file *
cork_file_new_from_path(struct cork_path *path);
CORK_API void
cork_file_free(struct cork_file *file);
/* File owns the result; you should not free it */
CORK_API const struct cork_path *
cork_file_path(struct cork_file *file);
CORK_API int
cork_file_exists(struct cork_file *file, bool *exists);
CORK_API int
cork_file_type(struct cork_file *file, enum cork_file_type *type);
typedef int
(*cork_file_directory_iterator)(struct cork_file *child, const char *rel_name,
void *user_data);
CORK_API int
cork_file_iterate_directory(struct cork_file *file,
cork_file_directory_iterator iterator,
void *user_data);
/* If flags includes CORK_FILE_RECURSIVE, this creates parent directories,
* if needed. If flags doesn't include CORK_FILE_PERMISSIVE, then it's an error
* if the directory already exists. */
CORK_API int
cork_file_mkdir(struct cork_file *file, cork_file_mode mode,
unsigned int flags);
/* Removes a file or directory. If file is a directory, and flags contains
* CORK_FILE_RECURSIVE, then all of the directory's contents are removed, too.
* Otherwise, the directory must already be empty. */
CORK_API int
cork_file_remove(struct cork_file *file, unsigned int flags);
CORK_API struct cork_file *
cork_path_list_find_file(const struct cork_path_list *list,
const char *rel_path);
/*-----------------------------------------------------------------------
* Lists of files
*/
struct cork_file_list;
CORK_API struct cork_file_list *
cork_file_list_new_empty(void);
CORK_API struct cork_file_list *
cork_file_list_new(struct cork_path_list *path_list);
CORK_API void
cork_file_list_free(struct cork_file_list *list);
/* Takes control of file. file must not already be in the list. */
CORK_API void
cork_file_list_add(struct cork_file_list *list, struct cork_file *file);
CORK_API size_t
cork_file_list_size(struct cork_file_list *list);
/* The list still owns file; you must not free it. Editing the file updates the
* entry in the list. */
CORK_API struct cork_file *
cork_file_list_get(struct cork_file_list *list, size_t index);
CORK_API struct cork_file_list *
cork_path_list_find_files(const struct cork_path_list *list,
const char *rel_path);
/*-----------------------------------------------------------------------
* Walking a directory tree
*/
#define CORK_SKIP_DIRECTORY 1
struct cork_dir_walker {
int
(*enter_directory)(struct cork_dir_walker *walker, const char *full_path,
const char *rel_path, const char *base_name);
int
(*file)(struct cork_dir_walker *walker, const char *full_path,
const char *rel_path, const char *base_name);
int
(*leave_directory)(struct cork_dir_walker *walker, const char *full_path,
const char *rel_path, const char *base_name);
};
#define cork_dir_walker_enter_directory(w, fp, rp, bn) \
((w)->enter_directory((w), (fp), (rp), (bn)))
#define cork_dir_walker_file(w, fp, rp, bn) \
((w)->file((w), (fp), (rp), (bn)))
#define cork_dir_walker_leave_directory(w, fp, rp, bn) \
((w)->leave_directory((w), (fp), (rp), (bn)))
CORK_API int
cork_walk_directory(const char *path, struct cork_dir_walker *walker);
/*-----------------------------------------------------------------------
* Standard paths and path lists
*/
CORK_API struct cork_path *
cork_path_home(void);
CORK_API struct cork_path_list *
cork_path_config_paths(void);
CORK_API struct cork_path_list *
cork_path_data_paths(void);
CORK_API struct cork_path *
cork_path_user_cache_path(void);
CORK_API struct cork_path *
cork_path_user_runtime_path(void);
#endif /* LIBCORK_CORE_FILES_H */
+28
View File
@@ -0,0 +1,28 @@
/* -*- coding: utf-8 -*-
* ----------------------------------------------------------------------
* Copyright © 2013, RedJack, LLC.
* All rights reserved.
*
* Please see the COPYING file in this distribution for license
* details.
* ----------------------------------------------------------------------
*/
#ifndef LIBCORK_CORE_PROCESS_H
#define LIBCORK_CORE_PROCESS_H
#include <libcork/core/api.h>
typedef void
(*cork_cleanup_function)(void);
CORK_API void
cork_cleanup_at_exit_named(const char *name, int priority,
cork_cleanup_function function);
#define cork_cleanup_at_exit(priority, function) \
cork_cleanup_at_exit_named(#function, priority, function)
#endif /* LIBCORK_CORE_PROCESS_H */
+196
View File
@@ -0,0 +1,196 @@
/* -*- coding: utf-8 -*-
* ----------------------------------------------------------------------
* Copyright © 2012-2013, RedJack, LLC.
* All rights reserved.
*
* Please see the COPYING file in this distribution for license
* details.
* ----------------------------------------------------------------------
*/
#ifndef LIBCORK_OS_SUBPROCESS_H
#define LIBCORK_OS_SUBPROCESS_H
#include <stdarg.h>
#include <libcork/core/api.h>
#include <libcork/core/types.h>
#include <libcork/ds/stream.h>
#include <libcork/threads/basics.h>
/*-----------------------------------------------------------------------
* Environments
*/
struct cork_env;
CORK_API struct cork_env *
cork_env_new(void);
CORK_API struct cork_env *
cork_env_clone_current(void);
CORK_API void
cork_env_free(struct cork_env *env);
CORK_API void
cork_env_replace_current(struct cork_env *env);
/* For all of the following, if env is NULL, these functions access or update
* the actual environment of the current process. Otherwise, they act on the
* given environment instance. */
CORK_API const char *
cork_env_get(struct cork_env *env, const char *name);
CORK_API void
cork_env_add(struct cork_env *env, const char *name, const char *value);
CORK_API void
cork_env_add_printf(struct cork_env *env, const char *name,
const char *format, ...)
CORK_ATTR_PRINTF(3,4);
CORK_API void
cork_env_add_vprintf(struct cork_env *env, const char *name,
const char *format, va_list args)
CORK_ATTR_PRINTF(3,0);
CORK_API void
cork_env_remove(struct cork_env *env, const char *name);
/*-----------------------------------------------------------------------
* Executing another process
*/
struct cork_exec;
CORK_API struct cork_exec *
cork_exec_new(const char *program);
CORK_ATTR_SENTINEL
CORK_API struct cork_exec *
cork_exec_new_with_params(const char *program, ...);
CORK_API struct cork_exec *
cork_exec_new_with_param_array(const char *program, char * const *params);
CORK_API void
cork_exec_free(struct cork_exec *exec);
CORK_API const char *
cork_exec_description(struct cork_exec *exec);
CORK_API const char *
cork_exec_program(struct cork_exec *exec);
CORK_API size_t
cork_exec_param_count(struct cork_exec *exec);
CORK_API const char *
cork_exec_param(struct cork_exec *exec, size_t index);
CORK_API void
cork_exec_add_param(struct cork_exec *exec, const char *param);
/* Can return NULL */
CORK_API struct cork_env *
cork_exec_env(struct cork_exec *exec);
/* Takes control of env */
CORK_API void
cork_exec_set_env(struct cork_exec *exec, struct cork_env *env);
/* Can return NULL */
CORK_API const char *
cork_exec_cwd(struct cork_exec *exec);
CORK_API void
cork_exec_set_cwd(struct cork_exec *exec, const char *directory);
CORK_API int
cork_exec_run(struct cork_exec *exec);
/*-----------------------------------------------------------------------
* Subprocesses
*/
struct cork_subprocess;
/* If env is NULL, we use the environment variables of the calling process. */
/* Takes control of body */
CORK_API struct cork_subprocess *
cork_subprocess_new(struct cork_thread_body *body,
struct cork_stream_consumer *stdout_consumer,
struct cork_stream_consumer *stderr_consumer,
int *exit_code);
/* Takes control of exec */
CORK_API struct cork_subprocess *
cork_subprocess_new_exec(struct cork_exec *exec,
struct cork_stream_consumer *stdout_consumer,
struct cork_stream_consumer *stderr_consumer,
int *exit_code);
CORK_API void
cork_subprocess_free(struct cork_subprocess *sub);
CORK_API struct cork_stream_consumer *
cork_subprocess_stdin(struct cork_subprocess *sub);
CORK_API int
cork_subprocess_start(struct cork_subprocess *sub);
CORK_API bool
cork_subprocess_is_finished(struct cork_subprocess *sub);
CORK_API int
cork_subprocess_abort(struct cork_subprocess *sub);
CORK_API bool
cork_subprocess_drain(struct cork_subprocess *sub);
CORK_API int
cork_subprocess_wait(struct cork_subprocess *sub);
/*-----------------------------------------------------------------------
* Groups of subprocesses
*/
struct cork_subprocess_group;
CORK_API struct cork_subprocess_group *
cork_subprocess_group_new(void);
CORK_API void
cork_subprocess_group_free(struct cork_subprocess_group *group);
/* Takes control of sub */
CORK_API void
cork_subprocess_group_add(struct cork_subprocess_group *group,
struct cork_subprocess *sub);
CORK_API int
cork_subprocess_group_start(struct cork_subprocess_group *group);
CORK_API bool
cork_subprocess_group_is_finished(struct cork_subprocess_group *group);
CORK_API int
cork_subprocess_group_abort(struct cork_subprocess_group *group);
CORK_API bool
cork_subprocess_group_drain(struct cork_subprocess_group *group);
CORK_API int
cork_subprocess_group_wait(struct cork_subprocess_group *group);
#endif /* LIBCORK_OS_SUBPROCESS_H */
+19
View File
@@ -0,0 +1,19 @@
/* -*- coding: utf-8 -*-
* ----------------------------------------------------------------------
* Copyright © 2012, RedJack, LLC.
* All rights reserved.
*
* Please see the COPYING file in this distribution for license
* details.
* ----------------------------------------------------------------------
*/
#ifndef LIBCORK_THREADS_H
#define LIBCORK_THREADS_H
/*** include all of the parts ***/
#include <libcork/threads/atomics.h>
#include <libcork/threads/basics.h>
#endif /* LIBCORK_THREADS_H */
+45
View File
@@ -0,0 +1,45 @@
/* -*- coding: utf-8 -*-
* ----------------------------------------------------------------------
* Copyright © 2012, RedJack, LLC.
* All rights reserved.
*
* Please see the COPYING file in this distribution for license
* details.
* ----------------------------------------------------------------------
*/
#ifndef LIBCORK_THREADS_ATOMICS_H
#define LIBCORK_THREADS_ATOMICS_H
#include <libcork/config.h>
#include <libcork/core/types.h>
/*-----------------------------------------------------------------------
* GCC intrinsics
*/
/* Ideally we can use GCC's intrinsics to define everything */
#if defined(CORK_CONFIG_HAVE_GCC_ATOMICS)
#define cork_int_atomic_add __sync_add_and_fetch
#define cork_uint_atomic_add __sync_add_and_fetch
#define cork_int_atomic_pre_add __sync_fetch_and_add
#define cork_uint_atomic_pre_add __sync_fetch_and_add
#define cork_int_atomic_sub __sync_sub_and_fetch
#define cork_uint_atomic_sub __sync_sub_and_fetch
#define cork_int_atomic_pre_sub __sync_fetch_and_sub
#define cork_uint_atomic_pre_sub __sync_fetch_and_sub
#define cork_int_cas __sync_val_compare_and_swap
#define cork_uint_cas __sync_val_compare_and_swap
#define cork_ptr_cas __sync_val_compare_and_swap
/*-----------------------------------------------------------------------
* End of atomic implementations
*/
#else
#error "No atomics implementation!"
#endif
#endif /* LIBCORK_THREADS_ATOMICS_H */
+216
View File
@@ -0,0 +1,216 @@
/* -*- coding: utf-8 -*-
* ----------------------------------------------------------------------
* Copyright © 2012, RedJack, LLC.
* All rights reserved.
*
* Please see the COPYING file in this distribution for license
* details.
* ----------------------------------------------------------------------
*/
#ifndef LIBCORK_THREADS_BASICS_H
#define LIBCORK_THREADS_BASICS_H
#include <assert.h>
#include <libcork/core/api.h>
#include <libcork/core/attributes.h>
#include <libcork/threads/atomics.h>
/*-----------------------------------------------------------------------
* Thread IDs
*/
typedef unsigned int cork_thread_id;
#define CORK_THREAD_NONE ((cork_thread_id) 0)
/* Returns a valid ID for any thread — even the main thread and threads that
* aren't created by libcork. */
CORK_API cork_thread_id
cork_current_thread_get_id(void);
/*-----------------------------------------------------------------------
* Main functions
*/
struct cork_thread_body {
int
(*run)(struct cork_thread_body *self);
void
(*free)(struct cork_thread_body *self);
};
#define cork_thread_body_run(tb) ((tb)->run((tb)))
#define cork_thread_body_free(tb) ((tb)->free((tb)))
/*-----------------------------------------------------------------------
* Threads
*/
struct cork_thread;
/* Returns NULL for the main thread, and for any thread not created via
* cork_thread_new/cork_thread_start. */
CORK_API struct cork_thread *
cork_current_thread_get(void);
CORK_API struct cork_thread *
cork_thread_new(const char *name, struct cork_thread_body *body);
/* Thread must not have been started yet. */
CORK_API void
cork_thread_free(struct cork_thread *thread);
CORK_API const char *
cork_thread_get_name(struct cork_thread *thread);
CORK_API cork_thread_id
cork_thread_get_id(struct cork_thread *thread);
/* Can only be called once per thread. Thread will automatically be freed when
* its done. */
CORK_API int
cork_thread_start(struct cork_thread *thread);
/* Can only be called once per thread; must be called after cork_thread_start. */
CORK_API int
cork_thread_join(struct cork_thread *thread);
/*-----------------------------------------------------------------------
* Executing something once
*/
#if CORK_CONFIG_HAVE_GCC_ASM && (CORK_CONFIG_ARCH_X86 || CORK_CONFIG_ARCH_X64)
#define cork_pause() \
do { \
__asm__ __volatile__ ("pause"); \
} while (0)
#else
#define cork_pause() do { /* do nothing */ } while (0)
#endif
#define cork_once_barrier(name) \
static struct { \
volatile int barrier; \
cork_thread_id initializing_thread; \
} name##__once;
#define cork_once(name, call) \
do { \
if (CORK_LIKELY(name##__once.barrier == 2)) { \
/* already initialized */ \
} else { \
/* Try to claim the ability to perform the initialization */ \
int prior_state = cork_int_cas(&name##__once.barrier, 0, 1); \
if (CORK_LIKELY(prior_state == 0)) { \
CORK_ATTR_UNUSED int result; \
/* we get to initialize */ \
call; \
result = cork_int_cas(&name##__once.barrier, 1, 2); \
assert(result == 1); \
} else { \
/* someone else is initializing, spin/wait until done */ \
while (name##__once.barrier != 2) { cork_pause(); } \
} \
} \
} while (0)
#define cork_once_recursive(name, call) \
do { \
if (CORK_LIKELY(name##__once.barrier == 2)) { \
/* already initialized */ \
} else { \
/* Try to claim the ability to perform the initialization */ \
int prior_state = cork_int_cas(&name##__once.barrier, 0, 1); \
if (CORK_LIKELY(prior_state == 0)) { \
CORK_ATTR_UNUSED int result; \
/* we get to initialize */ \
name##__once.initializing_thread = \
cork_current_thread_get_id(); \
call; \
result = cork_int_cas(&name##__once.barrier, 1, 2); \
assert(result == 1); \
} else { \
/* someone else is initializing, is it us? */ \
if (name##__once.initializing_thread == \
cork_current_thread_get_id()) { \
/* yep, fall through to let our recursion continue */ \
} else { \
/* nope; wait for the initialization to finish */ \
while (name##__once.barrier != 2) { cork_pause(); } \
} \
} \
} \
} while (0)
/*-----------------------------------------------------------------------
* Thread-local storage
*/
/* Prefer, in order:
*
* 1) __thread storage class
* 2) pthread_key_t
*/
#if CORK_CONFIG_HAVE_THREAD_STORAGE_CLASS
#define cork_tls(TYPE, NAME) \
static __thread TYPE NAME##__tls; \
\
static TYPE * \
NAME##_get(void) \
{ \
return &NAME##__tls; \
}
#elif CORK_HAVE_PTHREADS
#include <stdlib.h>
#include <pthread.h>
#include <libcork/core/allocator.h>
#define cork_tls(TYPE, NAME) \
static pthread_key_t NAME##__tls_key; \
cork_once_barrier(NAME##__tls_barrier); \
\
static void \
NAME##__tls_destroy(void *vself) \
{ \
free(vself); \
} \
\
static void \
NAME##__create_key(void) \
{ \
CORK_ATTR_UNUSED int rc; \
rc = pthread_key_create(&NAME##__tls_key, &NAME##__tls_destroy); \
assert(rc == 0); \
} \
\
static TYPE * \
NAME##_get(void) \
{ \
TYPE *self; \
cork_once(NAME##__tls_barrier, NAME##__create_key()); \
self = pthread_getspecific(NAME##__tls_key); \
if (CORK_UNLIKELY(self == NULL)) { \
self = cork_calloc(1, sizeof(TYPE)); \
pthread_setspecific(NAME##__tls_key, self); \
} \
return self; \
}
#else
#error "No thread-local storage implementation!"
#endif
#endif /* LIBCORK_THREADS_BASICS_H */