1// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
2// for details. All rights reserved. Use of this source code is governed by a
3// BSD-style license that can be found in the LICENSE file.
4
5#include <stdlib.h>
6#include <string.h>
7#include <iostream>
8
9#if defined(_WIN32)
10#define DART_EXPORT extern "C" __declspec(dllexport)
11#else
12#define DART_EXPORT \
13 extern "C" __attribute__((visibility("default"))) __attribute((used))
14#endif
15
16DART_EXPORT intptr_t return42() {
17 return 42;
18}
19
20DART_EXPORT double timesFour(double d) {
21 return d * 4.0;
22}
23
24// Wrap memmove so we can easily find it on all platforms.
25//
26// We use this in our samples to illustrate resource lifetime management.
27DART_EXPORT void MemMove(void* destination, void* source, intptr_t num_bytes) {
28 memmove(destination, source, num_bytes);
29}
30
31// Some opaque struct.
32typedef struct {
33} some_resource;
34
35DART_EXPORT some_resource* AllocateResource() {
36 void* pointer = malloc(sizeof(int64_t));
37
38 // Dummy initialize.
39 static_cast<int64_t*>(pointer)[0] = 10;
40
41 return static_cast<some_resource*>(pointer);
42}
43
44DART_EXPORT void UseResource(some_resource* resource) {
45 // Dummy change.
46 reinterpret_cast<int64_t*>(resource)[0] += 10;
47}
48
49DART_EXPORT void ReleaseResource(some_resource* resource) {
50 free(resource);
51}
52