Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/python-wasm
Path: blob/main/core/dylink/test/malloc/hellomodule.c
1393 views
1
#include "app.h"
2
#include <stdio.h>
3
#include <stdlib.h>
4
#include <assert.h>
5
6
extern int vecsum(int *v, int n);
7
8
static PyObject *hello(PyObject *self, PyObject *args) {
9
printf("hello...\n");
10
printf("allocate some memory...\n");
11
int n = 101;
12
int *a = malloc(sizeof(int) * n);
13
printf("got memory at a=%p\n", a);
14
printf("\nNow summing up numbers placed there:\n");
15
for (int i = 0; i < n; i++) {
16
a[i] = i;
17
}
18
printf("printf: sum is %d\n", vecsum(a, n));
19
assert(stdout != 0);
20
printf("stdout=%d, &stdout=%p\n", stdout, &stdout);
21
fprintf(stdout, "fprintf to stdout: sum is %d\n", vecsum(a, n));
22
return PyNone;
23
}
24
25
static struct PyMethodDef module_methods[] = {{"hello", &hello}, {NULL, NULL}};
26
27
struct PyModuleDef _hellomodule = {
28
.m_name = "hello",
29
.m_methods = module_methods,
30
};
31
32
EXPORTED_SYMBOL int PyInit_hello(void) {
33
printf("PyInit_hello\n");
34
// initialize the module:
35
printf("the hello function is at %p\n", &hello);
36
return PyModuleDef_Init(&_hellomodule);
37
}
38