Path: blob/main/core/dylink/test/malloc/hellomodule.c
1393 views
#include "app.h"1#include <stdio.h>2#include <stdlib.h>3#include <assert.h>45extern int vecsum(int *v, int n);67static PyObject *hello(PyObject *self, PyObject *args) {8printf("hello...\n");9printf("allocate some memory...\n");10int n = 101;11int *a = malloc(sizeof(int) * n);12printf("got memory at a=%p\n", a);13printf("\nNow summing up numbers placed there:\n");14for (int i = 0; i < n; i++) {15a[i] = i;16}17printf("printf: sum is %d\n", vecsum(a, n));18assert(stdout != 0);19printf("stdout=%d, &stdout=%p\n", stdout, &stdout);20fprintf(stdout, "fprintf to stdout: sum is %d\n", vecsum(a, n));21return PyNone;22}2324static struct PyMethodDef module_methods[] = {{"hello", &hello}, {NULL, NULL}};2526struct PyModuleDef _hellomodule = {27.m_name = "hello",28.m_methods = module_methods,29};3031EXPORTED_SYMBOL int PyInit_hello(void) {32printf("PyInit_hello\n");33// initialize the module:34printf("the hello function is at %p\n", &hello);35return PyModuleDef_Init(&_hellomodule);36}3738