Skip to content

Commit

Permalink
Use qsort and set linkage to static for internal functions used for o…
Browse files Browse the repository at this point in the history
…ffload-arch-fix (#375)
  • Loading branch information
rasmith committed Apr 26, 2024
1 parent b82c0ec commit 58d2ca0
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions llamafile/cuda.c
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,11 @@ struct StringList {
size_t length;
};

void AddStringToStringList(struct StringList *string_list, char *string, size_t string_length) {
static int StringCompare(const void* a, const void* b) {
return strcmp(*(const char**)a, *(const char**)b);
}

static void AddStringToStringList(struct StringList *string_list, char *string, size_t string_length) {
struct StringListEntry *new_entry = malloc(sizeof(struct StringListEntry));
new_entry->string = malloc(sizeof(char) * (string_length + 1));
strncpy(new_entry->string, string, string_length);
Expand All @@ -232,7 +236,7 @@ void AddStringToStringList(struct StringList *string_list, char *string, size_t
++string_list->length;
}

void CopyStringListToStringArray(struct StringList *string_list, char ***string_array) {
static void CopyStringListToStringArray(struct StringList *string_list, char ***string_array) {
*string_array = malloc(sizeof(char *) * string_list->length);
struct StringListEntry *current = string_list->head;
int i = 0;
Expand All @@ -243,7 +247,7 @@ void CopyStringListToStringArray(struct StringList *string_list, char ***string_
}
}

int RemoveDuplicatesFromStringArray(char **strings, int num_strings) {
static int RemoveDuplicatesFromSortedStringArray(char **strings, int num_strings) {
if (num_strings == 1)
return 1;
int tail = 0;
Expand All @@ -258,7 +262,7 @@ int RemoveDuplicatesFromStringArray(char **strings, int num_strings) {
return tail + 1;
}

void FreeStringList(struct StringList *string_list) {
static void FreeStringList(struct StringList *string_list) {
struct StringListEntry *current = string_list->head;
while (current != NULL) {
struct StringListEntry *next = current->next;
Expand Down Expand Up @@ -372,7 +376,10 @@ static bool get_amd_offload_arch_flag(char out[static 64]) {
char **names = NULL;

CopyStringListToStringArray(&name_list, &names);
int num_names = RemoveDuplicatesFromStringArray(names, name_list.length);
qsort(names, name_list.length, sizeof(char*), StringCompare);

int num_names = RemoveDuplicatesFromSortedStringArray(names, name_list.length);

for (int i = 0; i < num_names; ++i) {
if (i > 0)
*p++ = ',';
Expand Down

0 comments on commit 58d2ca0

Please sign in to comment.