Skip to content

Commit

Permalink
C overhead fixes (#20402)
Browse files Browse the repository at this point in the history
* [C][Client] Remove redundant casts

Don't explicitly cast void pointers as it's unnecessary in C, but
leave printf arguments untouched to avoid setting off -Wformat.

* [C][Client] Cosmetic: remove unnecessary parens, align some lines

* [C][Client] Reduce number of unnecessary strlen() calls
  • Loading branch information
imaami authored Jan 9, 2025
1 parent 536519c commit 40d4703
Show file tree
Hide file tree
Showing 23 changed files with 187 additions and 355 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,7 @@ end:
apiClient->response_code = 0;

// create the path
long sizeOfPath = strlen("{{{path}}}")+1;
char *localVarPath = malloc(sizeOfPath);
snprintf(localVarPath, sizeOfPath, "{{{path}}}");
char *localVarPath = strdup("{{{path}}}");

{{#pathParams}}
{{#isString}}
Expand All @@ -126,7 +124,7 @@ end:
{{#pathParams}}

// Path Params
long sizeOfPathParams_{{{paramName}}} = {{#pathParams}}{{#isLong}}sizeof({{paramName}})+3{{/isLong}}{{#isString}}strlen({{^isEnum}}{{paramName}}{{/isEnum}}{{#isEnum}}{{{operationId}}}_{{enumName}}_ToString({{paramName}}){{/isEnum}})+3{{/isString}}{{^-last}} + {{/-last}}{{/pathParams}} + strlen("{ {{baseName}} }");
long sizeOfPathParams_{{{paramName}}} = {{#pathParams}}{{#isLong}}sizeof({{paramName}})+3{{/isLong}}{{#isString}}strlen({{^isEnum}}{{paramName}}{{/isEnum}}{{#isEnum}}{{{operationId}}}_{{enumName}}_ToString({{paramName}}){{/isEnum}})+3{{/isString}}{{^-last}} + {{/-last}}{{/pathParams}} + sizeof("{ {{baseName}} }") - 1;
{{#isNumeric}}
if({{paramName}} == 0){
goto end;
Expand Down Expand Up @@ -254,7 +252,7 @@ end:
valueQuery_{{{paramName}}} = {{#isString}}{{^isEnum}}strdup({{/isEnum}}{{/isString}}({{{paramName}}}){{#isString}}{{^isEnum}}){{/isEnum}}{{/isString}};
{{/isBoolean}}
{{/isInteger}}
keyPairQuery_{{paramName}} = keyValuePair_create(keyQuery_{{{paramName}}}, {{#isEnum}}(void *)strdup({{{operationId}}}_{{enumName}}_ToString(
keyPairQuery_{{paramName}} = keyValuePair_create(keyQuery_{{{paramName}}}, {{#isEnum}}strdup({{{operationId}}}_{{enumName}}_ToString(
{{/isEnum}}{{^isString}}{{^isInteger}}{{^isBoolean}}&{{/isBoolean}}{{/isInteger}}{{/isString}}valueQuery_{{{paramName}}}{{#isEnum}})){{/isEnum}});
list_addElement(localVarQueryParameters,keyPairQuery_{{paramName}});
{{/isArray}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,11 @@ void sslConfig_free(sslConfig_t *sslConfig) {
free(sslConfig);
}

static void replaceSpaceWithPlus(char *stringToProcess) {
for(int i = 0; i < strlen(stringToProcess); i++) {
if(stringToProcess[i] == ' ') {
stringToProcess[i] = '+';
static void replaceSpaceWithPlus(char *str) {
if (str) {
for (; *str; str++) {
if (*str == ' ')
*str = '+';
}
}
}
Expand Down Expand Up @@ -286,38 +287,26 @@ void apiClient_invoke(apiClient_t *apiClient,
if(headerType != NULL) {
list_ForEach(listEntry, headerType) {
if(strstr((char *) listEntry->data,
"xml") == NULL)
if(strstr(listEntry->data, "xml") == NULL)
{
buffHeader = malloc(strlen(
"Accept: ") +
strlen((char *)
listEntry->
data) + 1);
sprintf(buffHeader, "%s%s", "Accept: ",
buffHeader = malloc(sizeof("Accept: ") +
strlen(listEntry->data));
sprintf(buffHeader, "Accept: %s",
(char *) listEntry->data);
headers = curl_slist_append(headers,
buffHeader);
headers = curl_slist_append(headers, buffHeader);
free(buffHeader);
}
}
}
if(contentType != NULL) {
list_ForEach(listEntry, contentType) {
if(strstr((char *) listEntry->data,
"xml") == NULL)
if(strstr(listEntry->data, "xml") == NULL)
{
buffContent =
malloc(strlen(
"Content-Type: ") + strlen(
(char *)
listEntry->data) +
1);
sprintf(buffContent, "%s%s",
"Content-Type: ",
buffContent = malloc(sizeof("Content-Type: ") +
strlen(listEntry->data));
sprintf(buffContent, "Content-Type: %s",
(char *) listEntry->data);
headers = curl_slist_append(headers,
buffContent);
headers = curl_slist_append(headers, buffContent);
free(buffContent);
buffContent = NULL;
}
Expand Down Expand Up @@ -594,8 +583,8 @@ void apiClient_invoke(apiClient_t *apiClient,

size_t writeDataCallback(void *buffer, size_t size, size_t nmemb, void *userp) {
size_t size_this_time = nmemb * size;
apiClient_t *apiClient = (apiClient_t *)userp;
apiClient->dataReceived = (char *)realloc( apiClient->dataReceived, apiClient->dataReceivedLen + size_this_time + 1);
apiClient_t *apiClient = userp;
apiClient->dataReceived = realloc( apiClient->dataReceived, apiClient->dataReceivedLen + size_this_time + 1);
memcpy((char *)apiClient->dataReceived + apiClient->dataReceivedLen, buffer, size_this_time);
apiClient->dataReceivedLen += size_this_time;
((char*)apiClient->dataReceived)[apiClient->dataReceivedLen] = '\0'; // the space size of (apiClient->dataReceived) = dataReceivedLen + 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ void listEntry_free(listEntry_t *listEntry, void *additionalData) {
}

void listEntry_printAsInt(listEntry_t *listEntry, void *additionalData) {
printf("%i\n", *((int *) (listEntry->data)));
printf("%i\n", *(int *)listEntry->data);
}

list_t *list_createList() {
Expand Down Expand Up @@ -176,8 +176,8 @@ char* findStrInStrList(list_t *strList, const char *str)

listEntry_t* listEntry = NULL;
list_ForEach(listEntry, strList) {
if (strstr((char*)listEntry->data, str) != NULL) {
return (char*)listEntry->data;
if (strstr(listEntry->data, str) != NULL) {
return listEntry->data;
}
}

Expand All @@ -197,4 +197,4 @@ void clear_and_free_string_list(list_t *list)
list_item = NULL;
}
list_freeList(list);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ void {{classname}}_free({{classname}}_t *{{classname}}) {
{{#isMap}}
if ({{{classname}}}->{{{name}}}) {
list_ForEach(listEntry, {{classname}}->{{name}}) {
keyValuePair_t *localKeyValue = (keyValuePair_t*) listEntry->data;
keyValuePair_t *localKeyValue = listEntry->data;
free (localKeyValue->key);
free (localKeyValue->value);
keyValuePair_free(localKeyValue);
Expand Down Expand Up @@ -570,7 +570,7 @@ cJSON *{{classname}}_convertToJSON({{classname}}_t *{{classname}}) {
list_ForEach({{{name}}}ListEntry, {{{classname}}}->{{{name}}}) {
{{#items}}
{{#isString}}
if(cJSON_AddStringToObject({{{name}}}, "", (char*){{{name}}}ListEntry->data) == NULL)
if(cJSON_AddStringToObject({{{name}}}, "", {{{name}}}ListEntry->data) == NULL)
{
goto fail;
}
Expand Down Expand Up @@ -617,16 +617,16 @@ cJSON *{{classname}}_convertToJSON({{classname}}_t *{{classname}}) {
listEntry_t *{{{name}}}ListEntry;
if ({{{classname}}}->{{{name}}}) {
list_ForEach({{{name}}}ListEntry, {{{classname}}}->{{{name}}}) {
keyValuePair_t *localKeyValue = (keyValuePair_t*){{{name}}}ListEntry->data;
keyValuePair_t *localKeyValue = {{{name}}}ListEntry->data;
{{#items}}
{{#isString}}
if(cJSON_AddStringToObject(localMapObject, localKeyValue->key, (char*)localKeyValue->value) == NULL)
if(cJSON_AddStringToObject(localMapObject, localKeyValue->key, localKeyValue->value) == NULL)
{
goto fail;
}
{{/isString}}
{{#isByteArray}}
if(cJSON_AddStringToObject(localMapObject, localKeyValue->key, (char*)localKeyValue->value) == NULL)
if(cJSON_AddStringToObject(localMapObject, localKeyValue->key, localKeyValue->value) == NULL)
{
goto fail;
}
Expand Down Expand Up @@ -848,7 +848,7 @@ fail:
{
goto end;
}
double *{{{name}}}_local_value = (double *)calloc(1, sizeof(double));
double *{{{name}}}_local_value = calloc(1, sizeof(double));
if(!{{{name}}}_local_value)
{
goto end;
Expand Down Expand Up @@ -1088,7 +1088,7 @@ end:
if ({{{name}}}List) {
listEntry_t *listEntry = NULL;
list_ForEach(listEntry, {{{name}}}List) {
keyValuePair_t *localKeyValue = (keyValuePair_t*) listEntry->data;
keyValuePair_t *localKeyValue = listEntry->data;
free(localKeyValue->key);
localKeyValue->key = NULL;
{{#items}}
Expand Down
12 changes: 3 additions & 9 deletions samples/client/others/c/bearerAuth/api/DefaultAPI.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ DefaultAPI_privateGet(apiClient_t *apiClient)
apiClient->response_code = 0;

// create the path
long sizeOfPath = strlen("/private")+1;
char *localVarPath = malloc(sizeOfPath);
snprintf(localVarPath, sizeOfPath, "/private");
char *localVarPath = strdup("/private");



Expand Down Expand Up @@ -98,9 +96,7 @@ DefaultAPI_publicGet(apiClient_t *apiClient)
apiClient->response_code = 0;

// create the path
long sizeOfPath = strlen("/public")+1;
char *localVarPath = malloc(sizeOfPath);
snprintf(localVarPath, sizeOfPath, "/public");
char *localVarPath = strdup("/public");



Expand Down Expand Up @@ -170,9 +166,7 @@ DefaultAPI_usersGet(apiClient_t *apiClient)
apiClient->response_code = 0;

// create the path
long sizeOfPath = strlen("/users")+1;
char *localVarPath = malloc(sizeOfPath);
snprintf(localVarPath, sizeOfPath, "/users");
char *localVarPath = strdup("/users");



Expand Down
45 changes: 17 additions & 28 deletions samples/client/others/c/bearerAuth/src/apiClient.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,11 @@ void sslConfig_free(sslConfig_t *sslConfig) {
free(sslConfig);
}

static void replaceSpaceWithPlus(char *stringToProcess) {
for(int i = 0; i < strlen(stringToProcess); i++) {
if(stringToProcess[i] == ' ') {
stringToProcess[i] = '+';
static void replaceSpaceWithPlus(char *str) {
if (str) {
for (; *str; str++) {
if (*str == ' ')
*str = '+';
}
}
}
Expand Down Expand Up @@ -202,38 +203,26 @@ void apiClient_invoke(apiClient_t *apiClient,

if(headerType != NULL) {
list_ForEach(listEntry, headerType) {
if(strstr((char *) listEntry->data,
"xml") == NULL)
if(strstr(listEntry->data, "xml") == NULL)
{
buffHeader = malloc(strlen(
"Accept: ") +
strlen((char *)
listEntry->
data) + 1);
sprintf(buffHeader, "%s%s", "Accept: ",
buffHeader = malloc(sizeof("Accept: ") +
strlen(listEntry->data));
sprintf(buffHeader, "Accept: %s",
(char *) listEntry->data);
headers = curl_slist_append(headers,
buffHeader);
headers = curl_slist_append(headers, buffHeader);
free(buffHeader);
}
}
}
if(contentType != NULL) {
list_ForEach(listEntry, contentType) {
if(strstr((char *) listEntry->data,
"xml") == NULL)
if(strstr(listEntry->data, "xml") == NULL)
{
buffContent =
malloc(strlen(
"Content-Type: ") + strlen(
(char *)
listEntry->data) +
1);
sprintf(buffContent, "%s%s",
"Content-Type: ",
buffContent = malloc(sizeof("Content-Type: ") +
strlen(listEntry->data));
sprintf(buffContent, "Content-Type: %s",
(char *) listEntry->data);
headers = curl_slist_append(headers,
buffContent);
headers = curl_slist_append(headers, buffContent);
free(buffContent);
buffContent = NULL;
}
Expand Down Expand Up @@ -438,8 +427,8 @@ void apiClient_invoke(apiClient_t *apiClient,

size_t writeDataCallback(void *buffer, size_t size, size_t nmemb, void *userp) {
size_t size_this_time = nmemb * size;
apiClient_t *apiClient = (apiClient_t *)userp;
apiClient->dataReceived = (char *)realloc( apiClient->dataReceived, apiClient->dataReceivedLen + size_this_time + 1);
apiClient_t *apiClient = userp;
apiClient->dataReceived = realloc( apiClient->dataReceived, apiClient->dataReceivedLen + size_this_time + 1);
memcpy((char *)apiClient->dataReceived + apiClient->dataReceivedLen, buffer, size_this_time);
apiClient->dataReceivedLen += size_this_time;
((char*)apiClient->dataReceived)[apiClient->dataReceivedLen] = '\0'; // the space size of (apiClient->dataReceived) = dataReceivedLen + 1
Expand Down
8 changes: 4 additions & 4 deletions samples/client/others/c/bearerAuth/src/list.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ void listEntry_free(listEntry_t *listEntry, void *additionalData) {
}

void listEntry_printAsInt(listEntry_t *listEntry, void *additionalData) {
printf("%i\n", *((int *) (listEntry->data)));
printf("%i\n", *(int *)listEntry->data);
}

list_t *list_createList() {
Expand Down Expand Up @@ -176,8 +176,8 @@ char* findStrInStrList(list_t *strList, const char *str)

listEntry_t* listEntry = NULL;
list_ForEach(listEntry, strList) {
if (strstr((char*)listEntry->data, str) != NULL) {
return (char*)listEntry->data;
if (strstr(listEntry->data, str) != NULL) {
return listEntry->data;
}
}

Expand All @@ -197,4 +197,4 @@ void clear_and_free_string_list(list_t *list)
list_item = NULL;
}
list_freeList(list);
}
}
Loading

0 comments on commit 40d4703

Please sign in to comment.