_info.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include "config.h"
  4. /**
  5. * alloc - memory allocator routines
  6. *
  7. * The alloc module implements a simple allocator which you can use to
  8. * dynamically allocate space within a region of memory. This can be useful
  9. * for suballocations within a given region, or a memory-mapped file.
  10. *
  11. * All metadata is kept within the memory handed to the allocator: you only
  12. * need hand the pointer and the size of the memory to each call.
  13. *
  14. * The region contents is always in offsets, so it can be mapped in different
  15. * places, but is not endian-safe.
  16. *
  17. * Example:
  18. * #include <sys/mman.h>
  19. * #include <unistd.h>
  20. * #include <sys/types.h>
  21. * #include <err.h>
  22. * #include "alloc/alloc.h"
  23. *
  24. * static void usage(const char *name)
  25. * {
  26. * errx(1, "Usage: %s --create <mapfile>\n"
  27. * " %s --check <mapfile>\n"
  28. * " %s --alloc <mapfile>\n"
  29. * " %s --free=<offset> <mapfile>\n", name, name, name);
  30. * }
  31. *
  32. * // Create a memory mapped file, and allocate from within it
  33. * int main(int argc, char *argv[])
  34. * {
  35. * void *a, *p;
  36. * int fd;
  37. * enum { CREATE, CHECK, ALLOC, FREE } cmd;
  38. *
  39. * if (argc != 3)
  40. * usage(argv[0]);
  41. *
  42. * if (strcmp(argv[1], "--create") == 0)
  43. * cmd = CREATE;
  44. * else if (strcmp(argv[1], "--check") == 0)
  45. * cmd = CHECK;
  46. * else if (strcmp(argv[1], "--alloc") == 0)
  47. * cmd = ALLOC;
  48. * else if (strncmp(argv[1], "--free=", strlen("--free=")) == 0)
  49. * cmd = FREE;
  50. * else
  51. * usage(argv[0]);
  52. *
  53. * if (cmd == CREATE) {
  54. * fd = open(argv[2], O_RDWR|O_CREAT|O_EXCL, 0600);
  55. * if (fd < 0)
  56. * err(1, "Could not create %s", argv[2]);
  57. * if (ftruncate(fd, 1048576) != 0)
  58. * err(1, "Could not set length on %s", argv[2]);
  59. * } else {
  60. * fd = open(argv[2], O_RDWR);
  61. * if (fd < 0)
  62. * err(1, "Could not open %s", argv[2]);
  63. * }
  64. *
  65. * a = mmap(NULL, 1048576, PROT_READ|PROT_WRITE, MAP_SHARED, fd,0);
  66. * if (a == MAP_FAILED)
  67. * err(1, "Could not map %s", argv[2]);
  68. *
  69. * switch (cmd) {
  70. * case CREATE:
  71. * alloc_init(a, 1048576);
  72. * break;
  73. * case CHECK:
  74. * if (!alloc_check(a, 1048576))
  75. * err(1, "Region is corrupt");
  76. * break;
  77. * case ALLOC:
  78. * p = alloc_get(a, 1048576, 1024, 16);
  79. * if (!p)
  80. * errx(1, "Could not allocate");
  81. * printf("%zu\n", (char *)p - (char *)a);
  82. * break;
  83. * case FREE:
  84. * p = (char *)a + atol(argv[1] + strlen("--free="));
  85. * alloc_free(a, 1048576, p);
  86. * break;
  87. * }
  88. * return 0;
  89. * }
  90. */
  91. int main(int argc, char *argv[])
  92. {
  93. if (argc != 2)
  94. return 1;
  95. if (strcmp(argv[1], "depends") == 0) {
  96. printf("ccan/build_assert\n");
  97. return 0;
  98. }
  99. return 1;
  100. }