Skip to content

Commit 0817c63

Browse files
Create prepending variants of add_if_manifest_file & string funcs
1 parent 235d1d2 commit 0817c63

File tree

2 files changed

+69
-15
lines changed

2 files changed

+69
-15
lines changed

loader/loader.c

Lines changed: 55 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -305,30 +305,52 @@ VkResult create_string_list(const struct loader_instance *inst, uint32_t allocat
305305
return VK_SUCCESS;
306306
}
307307

308-
VkResult append_str_to_string_list(const struct loader_instance *inst, struct loader_string_list *string_list, char *str) {
309-
assert(string_list && str);
308+
VkResult incrase_str_capacity_by_at_least_one(const struct loader_instance *inst, struct loader_string_list *string_list) {
309+
assert(string_list);
310310
if (string_list->allocated_count == 0) {
311311
string_list->allocated_count = 32;
312312
string_list->list =
313313
loader_instance_heap_calloc(inst, sizeof(char *) * string_list->allocated_count, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
314314
if (NULL == string_list->list) {
315-
loader_instance_heap_free(inst, str); // Must clean up in case of failure
316315
return VK_ERROR_OUT_OF_HOST_MEMORY;
317316
}
318317
} else if (string_list->count + 1 > string_list->allocated_count) {
319318
uint32_t new_allocated_count = string_list->allocated_count * 2;
320319
string_list->list = loader_instance_heap_realloc(inst, string_list->list, sizeof(char *) * string_list->allocated_count,
321320
sizeof(char *) * new_allocated_count, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
322321
if (NULL == string_list->list) {
323-
loader_instance_heap_free(inst, str); // Must clean up in case of failure
324322
return VK_ERROR_OUT_OF_HOST_MEMORY;
325323
}
326324
string_list->allocated_count *= 2;
327325
}
326+
return VK_SUCCESS;
327+
}
328+
329+
VkResult append_str_to_string_list(const struct loader_instance *inst, struct loader_string_list *string_list, char *str) {
330+
assert(string_list && str);
331+
VkResult res = incrase_str_capacity_by_at_least_one(inst, string_list);
332+
if (res == VK_ERROR_OUT_OF_HOST_MEMORY) {
333+
loader_instance_heap_free(inst, str); // Must clean up in case of failure
334+
return res;
335+
}
328336
string_list->list[string_list->count++] = str;
329337
return VK_SUCCESS;
330338
}
331339

340+
VkResult prepend_str_to_string_list(const struct loader_instance *inst, struct loader_string_list *string_list, char *str) {
341+
assert(string_list && str);
342+
VkResult res = incrase_str_capacity_by_at_least_one(inst, string_list);
343+
if (res == VK_ERROR_OUT_OF_HOST_MEMORY) {
344+
loader_instance_heap_free(inst, str); // Must clean up in case of failure
345+
return res;
346+
}
347+
// Shift everything down one
348+
void *ptr_to_list = memmove(string_list->list + 1, string_list->list, sizeof(char *) * string_list->count);
349+
if (ptr_to_list) string_list->list[0] = str; // Write new string to start of list
350+
string_list->count++;
351+
return VK_SUCCESS;
352+
}
353+
332354
VkResult copy_str_to_string_list(const struct loader_instance *inst, struct loader_string_list *string_list, const char *str,
333355
size_t str_len) {
334356
assert(string_list && str);
@@ -341,6 +363,18 @@ VkResult copy_str_to_string_list(const struct loader_instance *inst, struct load
341363
return append_str_to_string_list(inst, string_list, new_str);
342364
}
343365

366+
VkResult copy_str_to_start_of_string_list(const struct loader_instance *inst, struct loader_string_list *string_list,
367+
const char *str, size_t str_len) {
368+
assert(string_list && str);
369+
char *new_str = loader_instance_heap_calloc(inst, sizeof(char *) * str_len + 1, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
370+
if (NULL == new_str) {
371+
return VK_ERROR_OUT_OF_HOST_MEMORY;
372+
}
373+
loader_strncpy(new_str, sizeof(char *) * str_len + 1, str, str_len);
374+
new_str[str_len] = '\0';
375+
return prepend_str_to_string_list(inst, string_list, new_str);
376+
}
377+
344378
void free_string_list(const struct loader_instance *inst, struct loader_string_list *string_list) {
345379
assert(string_list);
346380
if (string_list->list) {
@@ -3002,10 +3036,8 @@ void copy_data_file_info(const char *cur_path, const char *relative_path, size_t
30023036
}
30033037
}
30043038

3005-
// If the file found is a manifest file name, add it to the out_files manifest list.
3039+
// If the file found is a manifest file name, add it to the end of out_files manifest list.
30063040
VkResult add_if_manifest_file(const struct loader_instance *inst, const char *file_name, struct loader_string_list *out_files) {
3007-
VkResult vk_result = VK_SUCCESS;
3008-
30093041
assert(NULL != file_name && "add_if_manifest_file: Received NULL pointer for file_name");
30103042
assert(NULL != out_files && "add_if_manifest_file: Received NULL pointer for out_files");
30113043

@@ -3014,15 +3046,26 @@ VkResult add_if_manifest_file(const struct loader_instance *inst, const char *fi
30143046
const char *name_suffix = file_name + name_len - 5;
30153047
if (!is_json(name_suffix, name_len)) {
30163048
// Use incomplete to indicate invalid name, but to keep going.
3017-
vk_result = VK_INCOMPLETE;
3018-
goto out;
3049+
return VK_INCOMPLETE;
30193050
}
30203051

3021-
vk_result = copy_str_to_string_list(inst, out_files, file_name, name_len);
3052+
return copy_str_to_string_list(inst, out_files, file_name, name_len);
3053+
}
30223054

3023-
out:
3055+
// If the file found is a manifest file name, add it to the start of the out_files manifest list.
3056+
VkResult prepend_if_manifest_file(const struct loader_instance *inst, const char *file_name, struct loader_string_list *out_files) {
3057+
assert(NULL != file_name && "prepend_if_manifest_file: Received NULL pointer for file_name");
3058+
assert(NULL != out_files && "prepend_if_manifest_file: Received NULL pointer for out_files");
30243059

3025-
return vk_result;
3060+
// Look for files ending with ".json" suffix
3061+
size_t name_len = strlen(file_name);
3062+
const char *name_suffix = file_name + name_len - 5;
3063+
if (!is_json(name_suffix, name_len)) {
3064+
// Use incomplete to indicate invalid name, but to keep going.
3065+
return VK_INCOMPLETE;
3066+
}
3067+
3068+
return copy_str_to_start_of_string_list(inst, out_files, file_name, name_len);
30263069
}
30273070

30283071
// Add any files found in the search_path. If any path in the search path points to a specific JSON, attempt to

loader/loader.h

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,14 +110,23 @@ VkResult loader_copy_to_new_str(const struct loader_instance *inst, const char *
110110
// Allocate a loader_string_list with enough space for allocated_count strings inside of it
111111
VkResult create_string_list(const struct loader_instance *inst, uint32_t allocated_count, struct loader_string_list *string_list);
112112
// Resize if there isn't enough space, then add the string str to the end of the loader_string_list
113-
// This function takes ownership of the str passed in - but only when it succeeds
113+
// This function takes ownership of the str passed in
114114
VkResult append_str_to_string_list(const struct loader_instance *inst, struct loader_string_list *string_list, char *str);
115-
// Resize if there isn't enough space, then copy the string str to a new string the end of the loader_string_list
115+
// Resize if there isn't enough space, then add the string str to the start of the loader_string_list
116+
// This function takes ownership of the str passed in
117+
VkResult prepend_str_to_string_list(const struct loader_instance *inst, struct loader_string_list *string_list, char *str);
118+
// Copy the string str to a new string and append it to string_list, resizing string_list if there isn't enough space.
116119
// This function does not take ownership of the string, it merely copies it.
117-
// This function appends a null terminator to the string automatically
120+
// This function automatically appends a null terminator to the string being copied
118121
// The str_len parameter does not include the null terminator
119122
VkResult copy_str_to_string_list(const struct loader_instance *inst, struct loader_string_list *string_list, const char *str,
120123
size_t str_len);
124+
// Copy the string str to a new string and prepend it to string_list, resizing string_list if there isn't enough space.
125+
// This function does not take ownership of the string, it merely copies it.
126+
// This function automatically appends a null terminator to the string being copied
127+
// The str_len parameter does not include the null terminator
128+
VkResult copy_str_to_start_of_string_list(const struct loader_instance *inst, struct loader_string_list *string_list,
129+
const char *str, size_t str_len);
121130

122131
// Free any string inside of loader_string_list and then free the list itself
123132
void free_string_list(const struct loader_instance *inst, struct loader_string_list *string_list);
@@ -214,6 +223,8 @@ void unload_drivers_without_physical_devices(struct loader_instance *inst);
214223

215224
VkStringErrorFlags vk_string_validate(const int max_length, const char *char_array);
216225
char *loader_get_next_path(char *path);
226+
VkResult add_if_manifest_file(const struct loader_instance *inst, const char *file_name, struct loader_string_list *out_files);
227+
VkResult prepend_if_manifest_file(const struct loader_instance *inst, const char *file_name, struct loader_string_list *out_files);
217228
VkResult add_data_files(const struct loader_instance *inst, char *search_path, struct loader_string_list *out_files,
218229
bool use_first_found_manifest);
219230

0 commit comments

Comments
 (0)