New upstream version 18.08
[deb_dpdk.git] / test / test / resource.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2016 RehiveTech. All rights reserved.
3  */
4
5 #ifndef _RESOURCE_H_
6 #define _RESOURCE_H_
7
8 /**
9  * @file
10  *
11  * Test Resource API
12  *
13  * Each test can require and use some external resources. Usually, an external
14  * resource is a file or a filesystem sub-hierarchy. A resource is included
15  * inside the test executable.
16  */
17
18 #include <sys/queue.h>
19 #include <stdio.h>
20 #include <stddef.h>
21
22 #include <rte_eal.h>
23 #include <rte_common.h>
24
25 TAILQ_HEAD(resource_list, resource);
26 extern struct resource_list resource_list;
27
28 /**
29  * Representation of a resource. It points to the resource's binary data.
30  * The semantics of the binary data are defined by the target test.
31  */
32 struct resource {
33         const char *name;  /**< Unique name of the resource */
34         const char *begin; /**< Start of resource data */
35         const char *end;   /**< End of resource data */
36         TAILQ_ENTRY(resource) next;
37 };
38
39 /**
40  * @return size of the given resource
41  */
42 size_t resource_size(const struct resource *r);
43
44 /**
45  * Find a resource by name in the global list of resources.
46  */
47 const struct resource *resource_find(const char *name);
48
49 /**
50  * Write the raw data of the resource to the given file.
51  * @return 0 on success
52  */
53 int resource_fwrite(const struct resource *r, FILE *f);
54
55 /**
56  * Write the raw data of the resource to the given file given by name.
57  * The name is relative to the current working directory.
58  * @return 0 on success
59  */
60 int resource_fwrite_file(const struct resource *r, const char *fname);
61
62 /**
63  * Treat the given resource as a tar archive. Extract
64  * the archive to the current directory.
65  */
66 int resource_untar(const struct resource *res);
67
68 /**
69  * Treat the given resource as a tar archive. Remove
70  * all files (related to the current directory) listed
71  * in the tar archive.
72  */
73 int resource_rm_by_tar(const struct resource *res);
74
75 /**
76  * Register a resource in the global list of resources.
77  * Not intended for direct use, please check the REGISTER_RESOURCE
78  * macro.
79  */
80 void resource_register(struct resource *r);
81
82 /**
83  * Definition of a resource linked externally (by means of the used toolchain).
84  * Only the base name of the resource is expected. The name refers to the
85  * linked pointers beg_<name> and end_<name> provided externally.
86  */
87 #define REGISTER_LINKED_RESOURCE(n) \
88 extern const char beg_ ##n;         \
89 extern const char end_ ##n;         \
90 REGISTER_RESOURCE(n, &beg_ ##n, &end_ ##n) \
91
92 /**
93  * Definition of a resource described by its name, and pointers begin, end.
94  */
95 #define REGISTER_RESOURCE(n, b, e) \
96 static struct resource linkres_ ##n = {       \
97         .name = RTE_STR(n),     \
98         .begin = b,             \
99         .end = e,               \
100 };                              \
101 static void __attribute__((constructor, used)) resinitfn_ ##n(void) \
102 {                               \
103         resource_register(&linkres_ ##n);  \
104 }
105
106 #endif