init.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #include <libc/unistd.h>
  2. #include <libc/stdio.h>
  3. #include <libc/fcntl.h>
  4. #include <libc/stdlib.h>
  5. int main()
  6. {
  7. char string[] = "333.txt";
  8. uint8_t buf[128] = {0};
  9. char tips_str[] = "The first application 'init.bin' started successfully!\n";
  10. put_string(tips_str, COLOR_GREEN, COLOR_BLACK);
  11. printf("test printf: %s size: %d\n", string, sizeof(string));
  12. /*
  13. int fd = open(string, 0);
  14. printf("fd=%d\n", fd);
  15. read(fd, buf, 128);
  16. put_string(buf, COLOR_ORANGE, COLOR_BLACK);
  17. lseek(fd, 0, SEEK_SET);
  18. write(fd, tips_str, sizeof(tips_str)-1);
  19. lseek(fd, 0, SEEK_SET);
  20. // 由于暂时没有实现用户态的memset,因此先手动清零
  21. for(int i=0;i<128;++i)
  22. buf[i] = 0;
  23. read(fd, buf, 128);
  24. put_string(buf, COLOR_YELLOW, COLOR_BLACK);
  25. close(fd);
  26. */
  27. void *ptr[256] = {0};
  28. for (int k = 0; k < 2; ++k)
  29. {
  30. printf("try to malloc 256*16K=4MB\n");
  31. uint64_t js = 0;
  32. for (int i = 0; i < 256; ++i)
  33. {
  34. ptr[i] = malloc(4096 * 4);
  35. js += *(uint64_t *)((uint64_t)(ptr[i]) - sizeof(uint64_t));
  36. if (*(uint64_t *)((uint64_t)(ptr[i]) - sizeof(uint64_t)) > 0x4008)
  37. printf("[%d] start_addr = %#018lx, len = %#010lx\n", (uint64_t)(ptr[i]) - 8, *(uint64_t *)((uint64_t)(ptr[i]) - sizeof(uint64_t)));
  38. }
  39. // printf("ptr[0]->len=%lld\n", *(uint64_t *)((uint64_t)ptr[0] - sizeof(uint64_t)));
  40. // printf("ptr[1]->len=%lld\n", *(uint64_t *)((uint64_t)ptr[1] - sizeof(uint64_t)));
  41. // printf("ptr[24]->len=%lld\n", *(uint64_t*)((uint64_t)ptr[24] - sizeof(uint64_t)));
  42. printf("alloc done. total used: %lld bytes\n", js);
  43. printf("try to free...\n");
  44. for (int i = 0; i < 256; ++i)
  45. {
  46. free(ptr[i]);
  47. }
  48. printf("free done!\n");
  49. }
  50. // *p = 'a';
  51. /*
  52. pid_t p = fork();
  53. if(p == 0)
  54. put_string("subproc\n", COLOR_PURPLE, COLOR_BLACK);
  55. else put_string("parent proc\n", COLOR_ORANGE, COLOR_BLACK);
  56. */
  57. while (1)
  58. ;
  59. }