Line | Count | Source |
1 | | /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * |
2 | | * Copyright by The HDF Group. * |
3 | | * All rights reserved. * |
4 | | * * |
5 | | * This file is part of HDF5. The full HDF5 copyright notice, including * |
6 | | * terms governing use, modification, and redistribution, is contained in * |
7 | | * the LICENSE file, which can be found at the root of the source code * |
8 | | * distribution tree, or in https://www.hdfgroup.org/licenses. * |
9 | | * If you do not have access to either file, you may request a copy from * |
10 | | * help@hdfgroup.org. * |
11 | | * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ |
12 | | |
13 | | #include "H5Fmodule.h" /* This source code file is part of the H5F module */ |
14 | | |
15 | | /* Packages needed by this file... */ |
16 | | #include "H5private.h" /* Generic Functions */ |
17 | | #include "H5Eprivate.h" /* Error handling */ |
18 | | #include "H5Fpkg.h" /* File access */ |
19 | | #include "H5FDprivate.h" /* File drivers */ |
20 | | #include "H5FLprivate.h" /* Free lists */ |
21 | | |
22 | | /* PRIVATE TYPEDEFS */ |
23 | | |
24 | | /* Struct for tracking "shared" file structs */ |
25 | | typedef struct H5F_sfile_node_t { |
26 | | H5F_shared_t *shared; /* Pointer to "shared" file struct */ |
27 | | struct H5F_sfile_node_t *next; /* Pointer to next node */ |
28 | | } H5F_sfile_node_t; |
29 | | |
30 | | /* PRIVATE PROTOTYPES */ |
31 | | |
32 | | /* PRIVATE VARIABLES */ |
33 | | |
34 | | /* Declare a free list to manage the H5F_sfile_node_t struct */ |
35 | | H5FL_DEFINE_STATIC(H5F_sfile_node_t); |
36 | | |
37 | | /* Declare a local variable to track the shared file information */ |
38 | | static H5F_sfile_node_t *H5F_sfile_head_s = NULL; |
39 | | |
40 | | /*------------------------------------------------------------------------- |
41 | | * Function: H5F_sfile_assert_num |
42 | | * |
43 | | * Purpose: Sanity checking that shared file list is empty |
44 | | * |
45 | | * Return: void |
46 | | * |
47 | | *------------------------------------------------------------------------- |
48 | | */ |
49 | | void |
50 | | H5F_sfile_assert_num(unsigned H5_ATTR_NDEBUG_UNUSED n) |
51 | 1 | { |
52 | 1 | FUNC_ENTER_NOAPI_NOINIT_NOERR |
53 | | |
54 | | /* The only useful work this function does is asserting so when NDEBUG |
55 | | * is defined it's a no-op. |
56 | | */ |
57 | | #ifndef NDEBUG |
58 | | if (n == 0) { |
59 | | assert(H5F_sfile_head_s == NULL); |
60 | | } |
61 | | else { |
62 | | unsigned count; /* Number of open shared files */ |
63 | | H5F_sfile_node_t *curr; /* Current shared file node */ |
64 | | |
65 | | /* Iterate through low-level files for matching low-level file info */ |
66 | | curr = H5F_sfile_head_s; |
67 | | count = 0; |
68 | | while (curr) { |
69 | | /* Increment # of open shared file structs */ |
70 | | count++; |
71 | | |
72 | | /* Advance to next shared file node */ |
73 | | curr = curr->next; |
74 | | } |
75 | | |
76 | | assert(count == n); |
77 | | } |
78 | | #endif |
79 | | |
80 | 1 | FUNC_LEAVE_NOAPI_VOID |
81 | 1 | } /* H5F_sfile_assert_num() */ |
82 | | |
83 | | /*------------------------------------------------------------------------- |
84 | | * Function: H5F__sfile_add |
85 | | * |
86 | | * Purpose: Add a "shared" file struct to the list of open files |
87 | | * |
88 | | * Return: SUCCEED/FAIL |
89 | | * |
90 | | *------------------------------------------------------------------------- |
91 | | */ |
92 | | herr_t |
93 | | H5F__sfile_add(H5F_shared_t *shared) |
94 | 163 | { |
95 | 163 | H5F_sfile_node_t *new_shared; /* New shared file node */ |
96 | 163 | herr_t ret_value = SUCCEED; /* Return value */ |
97 | | |
98 | 163 | FUNC_ENTER_PACKAGE |
99 | | |
100 | | /* Sanity check */ |
101 | 163 | assert(shared); |
102 | | |
103 | | /* Allocate new shared file node */ |
104 | 163 | if (NULL == (new_shared = H5FL_CALLOC(H5F_sfile_node_t))) |
105 | 0 | HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, FAIL, "memory allocation failed"); |
106 | | |
107 | | /* Set shared file value */ |
108 | 163 | new_shared->shared = shared; |
109 | | |
110 | | /* Prepend to list of shared files open */ |
111 | 163 | new_shared->next = H5F_sfile_head_s; |
112 | 163 | H5F_sfile_head_s = new_shared; |
113 | | |
114 | 163 | done: |
115 | 163 | FUNC_LEAVE_NOAPI(ret_value) |
116 | 163 | } /* end H5F__sfile_add() */ |
117 | | |
118 | | /*------------------------------------------------------------------------- |
119 | | * Function: H5F__sfile_search |
120 | | * |
121 | | * Purpose: Search for a "shared" file with low-level file info that |
122 | | * matches |
123 | | * |
124 | | * Return: Non-NULL on success / NULL on failure |
125 | | * |
126 | | *------------------------------------------------------------------------- |
127 | | */ |
128 | | H5F_shared_t * |
129 | | H5F__sfile_search(H5FD_t *lf) |
130 | 163 | { |
131 | 163 | H5F_sfile_node_t *curr; /* Current shared file node */ |
132 | 163 | H5F_shared_t *ret_value = NULL; /* Return value */ |
133 | | |
134 | 163 | FUNC_ENTER_PACKAGE_NOERR |
135 | | |
136 | | /* Sanity check */ |
137 | 163 | assert(lf); |
138 | | |
139 | | /* Iterate through low-level files for matching low-level file info */ |
140 | 163 | curr = H5F_sfile_head_s; |
141 | 1.37k | while (curr) { |
142 | | /* Check for match */ |
143 | 1.21k | if (0 == H5FD_cmp(curr->shared->lf, lf)) |
144 | 0 | HGOTO_DONE(curr->shared); |
145 | | |
146 | | /* Advance to next shared file node */ |
147 | 1.21k | curr = curr->next; |
148 | 1.21k | } /* end while */ |
149 | | |
150 | 163 | done: |
151 | 163 | FUNC_LEAVE_NOAPI(ret_value) |
152 | 163 | } /* end H5F__sfile_search() */ |
153 | | |
154 | | /*------------------------------------------------------------------------- |
155 | | * Function: H5F__sfile_remove |
156 | | * |
157 | | * Purpose: Remove a "shared" file struct from the list of open files |
158 | | * |
159 | | * Return: SUCCEED/FAIL |
160 | | * |
161 | | *------------------------------------------------------------------------- |
162 | | */ |
163 | | herr_t |
164 | | H5F__sfile_remove(H5F_shared_t *shared) |
165 | 163 | { |
166 | 163 | H5F_sfile_node_t *curr; /* Current shared file node */ |
167 | 163 | H5F_sfile_node_t *last; /* Last shared file node */ |
168 | 163 | herr_t ret_value = SUCCEED; /* Return value */ |
169 | | |
170 | 163 | FUNC_ENTER_PACKAGE |
171 | | |
172 | | /* Sanity check */ |
173 | 163 | assert(shared); |
174 | | |
175 | | /* Locate shared file node with correct shared file */ |
176 | 163 | last = NULL; |
177 | 163 | curr = H5F_sfile_head_s; |
178 | 199 | while (curr && curr->shared != shared) { |
179 | | /* Advance to next node */ |
180 | 36 | last = curr; |
181 | 36 | curr = curr->next; |
182 | 36 | } /* end while */ |
183 | | |
184 | | /* Indicate error if the node wasn't found */ |
185 | 163 | if (curr == NULL) |
186 | 0 | HGOTO_ERROR(H5E_FILE, H5E_NOTFOUND, FAIL, "can't find shared file info"); |
187 | | |
188 | | /* Remove node found from list */ |
189 | 163 | if (last != NULL) |
190 | | /* Removing middle or tail node in list */ |
191 | 10 | last->next = curr->next; |
192 | 153 | else |
193 | | /* Removing head node in list */ |
194 | 153 | H5F_sfile_head_s = curr->next; |
195 | | |
196 | | /* Release the shared file node struct */ |
197 | | /* (the shared file info itself is freed elsewhere) */ |
198 | 163 | curr = H5FL_FREE(H5F_sfile_node_t, curr); |
199 | | |
200 | 163 | done: |
201 | 163 | FUNC_LEAVE_NOAPI(ret_value) |
202 | 163 | } /* end H5F__sfile_remove() */ |