]> git.ozlabs.org Git - ccan/blob - ccan/alloc/_info
ccanlint: print coverage amount when -vv
[ccan] / ccan / alloc / _info
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 <sys/stat.h>
24  *      #include <fcntl.h>
25  *      #include <string.h>
26  *      #include <stdlib.h>
27  *      #include <ccan/alloc/alloc.h>
28  *
29  *      static void usage(const char *name)
30  *      {
31  *              errx(1, "Usage: %s --create <mapfile>\n"
32  *                   " %s --check <mapfile>\n"
33  *                   " %s --alloc <mapfile>\n"
34  *                   " %s --free=<offset> <mapfile>\n", name, name, name, name);
35  *      }
36  *
37  *      // Create a memory mapped file, and allocate from within it
38  *      int main(int argc, char *argv[])
39  *      {
40  *              void *a, *p;
41  *              int fd;
42  *              enum { CREATE, CHECK, ALLOC, FREE } cmd;
43  *
44  *              if (argc != 3)
45  *                      usage(argv[0]);
46  *
47  *              if (strcmp(argv[1], "--create") == 0)
48  *                      cmd = CREATE;
49  *              else if (strcmp(argv[1], "--check") == 0)
50  *                      cmd = CHECK;
51  *              else if (strcmp(argv[1], "--alloc") == 0)
52  *                      cmd = ALLOC;
53  *              else if (strncmp(argv[1], "--free=", strlen("--free=")) == 0)
54  *                      cmd = FREE;
55  *              else
56  *                      usage(argv[0]);
57  *
58  *              if (cmd == CREATE) {
59  *                      fd = open(argv[2], O_RDWR|O_CREAT|O_EXCL, 0600);
60  *                      if (fd < 0)
61  *                              err(1, "Could not create %s", argv[2]);
62  *                      if (ftruncate(fd, 1048576) != 0)
63  *                              err(1, "Could not set length on %s", argv[2]);
64  *              } else {
65  *                      fd = open(argv[2], O_RDWR);
66  *                      if (fd < 0)
67  *                              err(1, "Could not open %s", argv[2]);
68  *              }
69  *
70  *              a = mmap(NULL, 1048576, PROT_READ|PROT_WRITE, MAP_SHARED, fd,0);
71  *              if (a == MAP_FAILED)
72  *                      err(1, "Could not map %s", argv[2]);
73  *
74  *              switch (cmd) {
75  *              case CREATE:
76  *                      alloc_init(a, 1048576);
77  *                      break;
78  *              case CHECK:
79  *                      if (!alloc_check(a, 1048576))
80  *                              err(1, "Region is corrupt");
81  *                      break;
82  *              case ALLOC:
83  *                      p = alloc_get(a, 1048576, 1024, 16);
84  *                      if (!p)
85  *                              errx(1, "Could not allocate");
86  *                      printf("%zu\n", (char *)p - (char *)a);
87  *                      break;
88  *              case FREE:
89  *                      p = (char *)a + atol(argv[1] + strlen("--free="));
90  *                      alloc_free(a, 1048576, p);
91  *                      break;
92  *              }
93  *              return 0;
94  *      }
95  *
96  * License: LGPL (2 or any later version)
97  * Author: Rusty Russell <rusty@rustcorp.com.au>
98  */
99 int main(int argc, char *argv[])
100 {
101         if (argc != 2)
102                 return 1;
103
104         if (strcmp(argv[1], "depends") == 0) {
105                 printf("ccan/alignof\n");
106                 printf("ccan/build_assert\n");
107                 printf("ccan/compiler\n");
108                 printf("ccan/ilog\n");
109                 printf("ccan/likely\n");
110                 printf("ccan/short_types\n");
111                 return 0;
112         }
113
114         return 1;
115 }