Removed any usage of GNU C-only macros from libcsptr, using smalloc & sfree directly instead

This commit is contained in:
Snaipe 2015-09-05 20:39:44 +02:00
parent 48e96c06e6
commit b9e3923b7c
7 changed files with 139 additions and 59 deletions

View file

@ -23,7 +23,8 @@
*/
#include <stdio.h>
#include <csptr/smart_ptr.h>
#include <string.h>
#include <csptr/smalloc.h>
#include "criterion/stats.h"
#include "criterion/common.h"
#include "criterion/hooks.h"
@ -50,9 +51,12 @@ struct event *read_event(FILE *f) {
return NULL;
}
return unique_ptr(struct event,
.value = { .kind = kind, .data = buf },
.dtor = destroy_event);
struct event *ev = smalloc(
.size = sizeof (struct event),
.dtor = destroy_event
);
*ev = (struct event) { .kind = kind, .data = buf };
return ev;
}
case THEORY_FAIL: {
size_t *len = malloc(sizeof (size_t));
@ -69,10 +73,12 @@ struct event *read_event(FILE *f) {
}
free(len);
return unique_ptr(struct event,
.value = { .kind = kind, .data = buf },
.dtor = destroy_event);
struct event *ev = smalloc(
.size = sizeof (struct event),
.dtor = destroy_event
);
*ev = (struct event) { .kind = kind, .data = buf };
return ev;
}
case POST_TEST: {
double *elapsed_time = malloc(sizeof (double));
@ -81,12 +87,18 @@ struct event *read_event(FILE *f) {
return NULL;
}
return unique_ptr(struct event,
.value = { .kind = kind, .data = elapsed_time },
.dtor = destroy_event);
struct event *ev = smalloc(
.size = sizeof (struct event),
.dtor = destroy_event
);
*ev = (struct event) { .kind = kind, .data = elapsed_time };
return ev;
}
default: {
struct event *ev = smalloc(sizeof (struct event));
*ev = (struct event) { .kind = kind, .data = NULL };
return ev;
}
default:
return unique_ptr(struct event, { .kind = kind, .data = NULL });
}
}

View file

@ -28,7 +28,7 @@
#include <stdio.h>
#include <locale.h>
#include <getopt.h>
#include <csptr/smart_ptr.h>
#include <csptr/smalloc.h>
#include "runner.h"
#include "config.h"
@ -90,7 +90,7 @@ bool is_disabled(struct criterion_suite *s, struct criterion_test *t) {
}
int list_tests(bool unicode) {
smart struct criterion_test_set *set = criterion_init();
struct criterion_test_set *set = criterion_init();
const char *node = unicode ? UTF8_TREE_NODE : ASCII_TREE_NODE;
const char *join = unicode ? UTF8_TREE_JOIN : ASCII_TREE_JOIN;
@ -114,6 +114,8 @@ int list_tests(bool unicode) {
is_disabled(&s->suite, t) ? " (disabled)" : "");
}
}
sfree(set);
return 0;
}

View file

@ -21,9 +21,10 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <string.h>
#include <criterion/common.h>
#include <criterion/ordered-set.h>
#include <csptr/smart_ptr.h>
#include <csptr/smalloc.h>
static void destroy_ordered_set(void *ptr, UNUSED void *meta) {
sfree(((struct criterion_ordered_set *) ptr)->first);
@ -42,9 +43,14 @@ static void destroy_ordered_set_node(void *ptr, void *meta) {
struct criterion_ordered_set *new_ordered_set(f_criterion_cmp cmp,
f_destructor dtor) {
return unique_ptr(struct criterion_ordered_set,
.value = { .cmp = cmp, .dtor = dtor },
.dtor = destroy_ordered_set);
struct criterion_ordered_set *newset = smalloc(
.size = sizeof (struct criterion_ordered_set),
.dtor = destroy_ordered_set
);
struct criterion_ordered_set data = { .cmp = cmp, .dtor = dtor };
memcpy(newset, &data, sizeof (struct criterion_ordered_set));
return newset;
}
void *insert_ordered_set(struct criterion_ordered_set *l,

View file

@ -63,7 +63,7 @@
# include <sys/fcntl.h>
#endif
#include <csptr/smart_ptr.h>
#include <csptr/smalloc.h>
struct proc_handle {
#ifdef VANILLA_WIN32
@ -201,14 +201,19 @@ s_proc_handle *fork_process() {
UnmapViewOfFile(ctx);
CloseHandle(sharedMem);
return unique_ptr(s_proc_handle, { info.hProcess });
s_proc_handle *handle = smalloc(sizeof (s_proc_handle));
*handle = (s_proc_handle) { info.hProcess };
return handle;
#else
pid_t pid = fork();
if (pid == -1)
return (void *) -1;
if (pid == 0)
return NULL;
return unique_ptr(s_proc_handle, { pid });
s_proc_handle *handle = smalloc(sizeof (s_proc_handle));
*handle = (s_proc_handle) { pid };
return handle;
#endif
}
@ -294,6 +299,7 @@ FILE *pipe_out(s_pipe_handle *p) {
}
s_pipe_handle *stdpipe() {
s_pipe_handle *handle = smalloc(sizeof (s_pipe_handle));
#ifdef VANILLA_WIN32
HANDLE fhs[2];
SECURITY_ATTRIBUTES attr = {
@ -302,21 +308,25 @@ s_pipe_handle *stdpipe() {
};
if (!CreatePipe(fhs, fhs + 1, &attr, 0))
return NULL;
return unique_ptr(s_pipe_handle, {{ fhs[0], fhs[1] }});
*handle = (s_pipe_handle) {{ fhs[0], fhs[1] }};
return handle;
#else
int fds[2] = { -1, -1 };
if (pipe(fds) == -1)
return NULL;
return unique_ptr(s_pipe_handle, {{ fds[0], fds[1] }});
*handle = (s_pipe_handle) {{ fds[0], fds[1] }};
return handle;
#endif
}
s_proc_handle *get_current_process() {
s_proc_handle *handle = smalloc(sizeof (s_proc_handle));
#ifdef VANILLA_WIN32
return unique_ptr(s_proc_handle, { GetCurrentProcess() });
*handle = (s_proc_handle) { GetCurrentProcess() };
#else
return unique_ptr(s_proc_handle, { getpid() });
*handle = (s_proc_handle) { getpid() };
#endif
return handle;
}
bool is_current_process(s_proc_handle *proc) {

View file

@ -23,7 +23,7 @@
*/
#include <stdlib.h>
#include <stdbool.h>
#include <csptr/smart_ptr.h>
#include <csptr/smalloc.h>
#include "criterion/types.h"
#include "criterion/options.h"
@ -76,7 +76,7 @@ void run_worker(struct worker_context *ctx) {
struct process *spawn_test_worker(struct criterion_test *test,
struct criterion_suite *suite,
f_worker_func func) {
smart s_pipe_handle *pipe = stdpipe();
s_pipe_handle *pipe = stdpipe();
if (pipe == NULL)
abort();
@ -86,17 +86,26 @@ struct process *spawn_test_worker(struct criterion_test *test,
.func = func,
.pipe = pipe
};
struct process *ptr = NULL;
s_proc_handle *proc = fork_process();
if (proc == (void *) -1) {
return NULL;
goto cleanup;
} else if (proc == NULL) {
run_worker(&g_worker_context);
return NULL;
goto cleanup;
}
return unique_ptr(struct process,
.value = { .proc = proc, .in = pipe_in(pipe) },
.dtor = close_process);
ptr = smalloc(
.size = sizeof (struct process),
.kind = UNIQUE,
.dtor = close_process);
*ptr = (struct process) { .proc = proc, .in = pipe_in(pipe) };
cleanup:
sfree(pipe);
return ptr;
}
struct process_status wait_proc(struct process *proc) {

View file

@ -23,7 +23,7 @@
*/
#include <stdlib.h>
#include <stdio.h>
#include <csptr/smart_ptr.h>
#include <csptr/smalloc.h>
#include "criterion/criterion.h"
#include "criterion/options.h"
#include "criterion/ordered-set.h"
@ -104,10 +104,17 @@ struct criterion_test_set *criterion_init(void) {
++nb_tests;
}
return unique_ptr(struct criterion_test_set, {
struct criterion_test_set *set = smalloc(
.size = sizeof (struct criterion_test_set),
.dtor = dtor_test_set
);
*set = (struct criterion_test_set) {
suites,
nb_tests,
}, dtor_test_set);
};
return set;
}
typedef void (*f_test_run)(struct criterion_global_stats *,
@ -126,7 +133,7 @@ static void map_tests(struct criterion_test_set *set,
report(PRE_SUITE, s);
log(pre_suite, s);
smart struct criterion_suite_stats *suite_stats = suite_stats_init(&s->suite);
struct criterion_suite_stats *suite_stats = suite_stats_init(&s->suite);
struct event ev = { .kind = PRE_SUITE };
stat_push_event(stats, suite_stats, NULL, &ev);
@ -135,13 +142,18 @@ static void map_tests(struct criterion_test_set *set,
fun(stats, suite_stats, t, &s->suite);
if (criterion_options.fail_fast && stats->tests_failed > 0)
break;
if (!is_runner())
if (!is_runner()) {
sfree(suite_stats);
return;
}
}
report(POST_SUITE, suite_stats);
log(post_suite, suite_stats);
sfree(suite_stats);
}
}
static void run_test_child(struct criterion_test *test,
@ -191,19 +203,20 @@ static void run_test(struct criterion_global_stats *stats,
struct criterion_test *test,
struct criterion_suite *suite) {
smart struct criterion_test_stats *test_stats = test_stats_init(test);
struct criterion_test_stats *test_stats = test_stats_init(test);
struct process *proc = NULL;
if (is_disabled(test, suite)) {
stat_push_event(stats,
suite_stats,
test_stats,
&(struct event) { .kind = PRE_INIT });
return;
goto cleanup;
}
smart struct process *proc = spawn_test_worker(test, suite, run_test_child);
proc = spawn_test_worker(test, suite, run_test_child);
if (proc == NULL && !is_runner())
return;
goto cleanup;
bool test_started = false;
bool normal_finish = false;
@ -257,7 +270,7 @@ static void run_test(struct criterion_global_stats *stats,
test_stats,
&(struct event) { .kind = TEST_CRASH });
}
return;
goto cleanup;
}
test_stats->signal = status.status;
if (test->data->signal == 0) {
@ -279,7 +292,7 @@ static void run_test(struct criterion_global_stats *stats,
test_stats,
&(struct event) { .kind = TEST_CRASH });
}
return;
goto cleanup;
}
test_stats->exit_code = status.status;
if (!normal_finish) {
@ -295,6 +308,10 @@ static void run_test(struct criterion_global_stats *stats,
}
}
}
cleanup:
sfree(test_stats);
sfree(proc);
}
#ifdef HAVE_PCRE
@ -321,7 +338,7 @@ static int criterion_run_all_tests_impl(void) {
if (resume_child()) // (windows only) resume from the fork
return -1;
smart struct criterion_test_set *set = criterion_init();
struct criterion_test_set *set = criterion_init();
#ifdef HAVE_PCRE
if (criterion_options.pattern)
disable_unmatching(set);
@ -332,15 +349,21 @@ static int criterion_run_all_tests_impl(void) {
fflush(NULL); // flush everything before forking
smart struct criterion_global_stats *stats = stats_init();
struct criterion_global_stats *stats = stats_init();
map_tests(set, stats, run_test);
int result = is_runner() ? stats->tests_failed == 0 : -1;
if (!is_runner())
return -1;
goto cleanup;
report(POST_ALL, stats);
log(post_all, stats);
return stats->tests_failed == 0;
cleanup:
sfree(set);
sfree(stats);
return result;
}
int criterion_run_all_tests(void) {

View file

@ -21,7 +21,8 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <csptr/smart_ptr.h>
#include <string.h>
#include <csptr/smalloc.h>
#include "criterion/common.h"
#include "stats.h"
@ -48,7 +49,12 @@ static void destroy_stats(void *ptr, UNUSED void *meta) {
}
s_glob_stats *stats_init(void) {
return unique_ptr(s_glob_stats, .dtor = destroy_stats);
s_glob_stats *stats = smalloc(
.size = sizeof (s_glob_stats),
.dtor = destroy_stats
);
*stats = (s_glob_stats) { .suites = NULL };
return stats;
}
static void destroy_suite_stats(void *ptr, UNUSED void *meta) {
@ -60,9 +66,13 @@ static void destroy_suite_stats(void *ptr, UNUSED void *meta) {
}
s_suite_stats *suite_stats_init(struct criterion_suite *s) {
return shared_ptr(s_suite_stats, {
.suite = s,
}, destroy_suite_stats);
s_suite_stats *stats = smalloc(
.size = sizeof (s_suite_stats),
.kind = SHARED,
.dtor = destroy_suite_stats
);
*stats = (s_suite_stats) { .suite = s };
return stats;
}
static void destroy_test_stats(void *ptr, UNUSED void *meta) {
@ -74,11 +84,17 @@ static void destroy_test_stats(void *ptr, UNUSED void *meta) {
}
s_test_stats *test_stats_init(struct criterion_test *t) {
return shared_ptr(s_test_stats, {
.test = t,
.progress = t->data->line_,
.file = t->data->file_
}, destroy_test_stats);
s_test_stats *stats = smalloc(
.size = sizeof (s_test_stats),
.kind = SHARED,
.dtor = destroy_test_stats
);
*stats = (s_test_stats) {
.test = t,
.progress = t->data->line_,
.file = t->data->file_
};
return stats;
}
void stat_push_event(s_glob_stats *stats,
@ -140,7 +156,9 @@ static void push_assert(s_glob_stats *stats,
s_suite_stats *suite,
s_test_stats *test,
s_assert_stats *data) {
s_assert_stats *dup = unique_ptr(s_assert_stats, (*data));
s_assert_stats *dup = smalloc(sizeof (s_assert_stats));
memcpy(dup, data, sizeof (s_assert_stats));
dup->next = test->asserts;
test->asserts = dup;