Skip to content
Browse files

Merge pull request #1 from jedbrown/master

C ABI stability and interface updates
  • Loading branch information...
2 parents 7c2f714 + c23cbc6 commit 2fa3ab158ebee20b4858fcbc3be9a64763e2686b @certik committed Jan 22, 2012
Showing with 45 additions and 23 deletions.
  1. +9 −8 c/a.c
  2. +11 −0 c/compute-impl.h
  3. +14 −7 c/compute.c
  4. +11 −8 c/compute.h
View
17 c/a.c
@@ -17,11 +17,11 @@ void derivs(double x, double y, double *dx, double *dy, void *data)
*dy = d->a21 * x + d->a22 * y;
}
-void print_material_parameters(eq *d)
+void print_material_parameters(eq d)
{
// Access the context:
my_data *ctx;
- get_context(d, (void **)(&ctx));
+ get_context(d, &ctx);
printf("Material parameters: %f %f %f %f\n", ctx->a11, ctx->a12,
ctx->a21, ctx->a22);
}
@@ -42,13 +42,14 @@ int main()
eq d;
init(&d);
- register_func(&d, &derivs, &data1);
- run(&d, 0, 1, 0.1, 10);
- print_material_parameters(&d);
+ register_func(d, derivs, &data1);
+ run(d, 0, 1, 0.1, 10);
+ print_material_parameters(d);
printf("\n");
- register_func(&d, &derivs, &data2);
- run(&d, 0, 1, 0.1, 10);
- print_material_parameters(&d);
+ register_func(d, derivs, &data2);
+ run(d, 0, 1, 0.1, 10);
+ print_material_parameters(d);
+ destroy(&d);
return 0;
}
View
11 c/compute-impl.h
@@ -0,0 +1,11 @@
+#ifndef _compute_impl_h
+#define _compute_impl_h
+
+#include "compute.h"
+
+struct _eq_private {
+ void *data;
+ derivs_pt func;
+};
+
+#endif
View
21 c/compute.c
@@ -1,25 +1,26 @@
#include <stdio.h>
#include <stdlib.h>
-#include "compute.h"
+#include "compute-impl.h"
void init(eq *d)
{
- d->func = NULL;
+ *d = malloc(sizeof **d);
+ (*d)->func = NULL;
}
-void register_func(eq *d, derivs_pt func, void *data)
+void register_func(eq d, derivs_pt func, void *data)
{
d->func = func;
d->data = data;
}
-void get_context(eq *d, void **data)
+void get_context(eq d, void *data)
{
- (*data) = d->data;
+ *(void**)data = d->data;
}
-void run(eq *d, double x0, double y0, double dt, double n_steps)
+void run(eq d, double x0, double y0, double dt, double n_steps)
{
if (d->func == NULL) {
printf("d.func is NULL\n");
@@ -31,10 +32,16 @@ void run(eq *d, double x0, double y0, double dt, double n_steps)
y = y0;
t = 0;
for (i=0; i < n_steps; i++) {
- (*(d->func))(x, y, &dx, &dy, d->data);
+ d->func(x, y, &dx, &dy, d->data);
printf("%f %f\n", x, y);
x += dx * dt;
y += dy * dt;
t += dt;
}
}
+
+void destroy(eq *d)
+{
+ if (!*d) return;
+ free(*d);
+}
View
19 c/compute.h
@@ -1,12 +1,15 @@
+#ifndef _compute_h
+#define _compute_h
+
typedef void (*derivs_pt)(double x, double y, double *dx, double *dy,
void *data);
-typedef struct {
- void *data;
- derivs_pt func;
-} eq;
-
+/* Definition of the struct is not visible to user code, changes do not affect ABI */
+typedef struct _eq_private *eq;
void init(eq *d);
-void register_func(eq *d, derivs_pt func, void* data);
-void get_context(eq *d, void **data);
-void run(eq *d, double x0, double y0, double dt, double n_steps);
+void register_func(eq d, derivs_pt func, void* data);
+void get_context(eq d, void *data);
+void run(eq d, double x0, double y0, double dt, double n_steps);
+void destroy(eq *d);
+
+#endif

0 comments on commit 2fa3ab1

Please sign in to comment.
Something went wrong with that request. Please try again.