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