test1.c 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include <stdio.h>
  6. #include <stdint.h>
  7. #include <string.h>
  8. extern void *
  9. shared_heap_malloc(uint32_t size);
  10. extern void
  11. shared_heap_free(void *ptr);
  12. void *
  13. my_shared_heap_malloc(uint32_t size, uint32_t index)
  14. {
  15. char *buf1 = NULL, *buf2 = NULL, *buf;
  16. buf1 = shared_heap_malloc(128);
  17. if (!buf1)
  18. return NULL;
  19. buf1[0] = 'H';
  20. buf1[1] = 'e';
  21. buf1[2] = 'l';
  22. buf1[3] = 'l';
  23. buf1[4] = 'o';
  24. buf1[5] = ',';
  25. buf1[6] = ' ';
  26. buf2 = shared_heap_malloc(128);
  27. if (!buf2) {
  28. shared_heap_free(buf1);
  29. return NULL;
  30. }
  31. snprintf(buf2, 128, "this is buf %u allocated from shared heap", index);
  32. buf = shared_heap_malloc(size);
  33. if (!buf) {
  34. shared_heap_free(buf1);
  35. shared_heap_free(buf2);
  36. return NULL;
  37. }
  38. memset(buf, 0, size);
  39. memcpy(buf, buf1, strlen(buf1));
  40. memcpy(buf + strlen(buf1), buf2, strlen(buf2));
  41. shared_heap_free(buf1);
  42. shared_heap_free(buf2);
  43. return buf;
  44. }
  45. void
  46. my_shared_heap_free(void *ptr)
  47. {
  48. shared_heap_free(ptr);
  49. }
  50. void *
  51. produce_str(char *addr, uint32_t index)
  52. {
  53. char c;
  54. snprintf(addr, 512, "Data: %u stores to pre-allocated shared heap", index);
  55. /* Actually access it in wasm */
  56. c = addr[0];
  57. printf("In WASM: the first char is %c\n", c);
  58. return addr;
  59. }