FossilOrigin-Name: 80ef0c70a70f51d6798855f7c95c4e14f4bca86ae092309195ada7a5be657023
This commit is contained in:
nekobit 2022-10-15 20:19:25 +00:00
parent 0ff56024de
commit 5287e5c824
18 changed files with 114 additions and 44 deletions

36
include/mastodont_hooks.h Normal file
View file

@ -0,0 +1,36 @@
/*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef MASTODONT_HOOKS_H
#define MASTODONT_HOOKS_H
#include <stddef.h>
#define mstdnt_malloc (*_mstdnt_hooks_def.malloc)
#define mstdnt_free (*_mstdnt_hooks_def.free)
#define mstdnt_calloc (*_mstdnt_hooks_def.calloc)
#define mstdnt_realloc (*_mstdnt_hooks_def.realloc)
struct _mstdnt_hooks
{
void* (*malloc)(size_t size);
void (*free)(void* ptr);
void* (*calloc)(size_t nmemb, size_t size);
void* (*realloc)(void* ptr, size_t size);
};
/** Hooks for Mastodont functions */
extern struct _mstdnt_hooks _mstdnt_hooks_def;
#endif // MASTODONT_HOOKS_H

View file

@ -16,6 +16,7 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <mastodont_hooks.h>
#include <mastodont_account.h>
#include <mastodont_request.h>
#include <mastodont_json_helper.h>
@ -32,7 +33,7 @@ void _mstdnt_val_malloc_account_call(cJSON* v, void* _type)
{
struct mstdnt_account** type = _type;
*type = calloc(1, sizeof(struct mstdnt_account));
*type = mstdnt_calloc(1, sizeof(struct mstdnt_account));
if (*type)
mstdnt_account_json(*type, v->child);
@ -282,6 +283,6 @@ void mstdnt_cleanup_accounts(struct mstdnt_account* accts, size_t len)
if (!accts) return;
for (int i = 0; i < len; ++i)
mstdnt_cleanup_account(accts + i);
free(accts);
mstdnt_free(accts);
}

View file

@ -15,6 +15,7 @@
#include <string.h>
#include <stdlib.h>
#include <mastodont_hooks.h>
#include <mastodont_json_helper.h>
#include <mastodont_application.h>
#include <mastodont_query.h>
@ -80,7 +81,7 @@ void _mstdnt_val_malloc_application_call(cJSON* v, void* _type)
}
*type = calloc(1, sizeof(struct mstdnt_app));
*type = mstdnt_calloc(1, sizeof(struct mstdnt_app));
if (*type)
mstdnt_app_json(v->child, *type);

View file

@ -15,6 +15,7 @@
#include <string.h>
#include <stdlib.h>
#include <mastodont_hooks.h>
#include <mastodont_attachment.h>
#include <mastodont_json_helper.h>
#include <mastodont_request.h>
@ -81,7 +82,7 @@ void _mstdnt_val_attachments_call(cJSON* v, void* _type)
return;
}
*attachments = calloc(1, sizeof(struct mstdnt_attachment) * size);
*attachments = mstdnt_calloc(1, sizeof(struct mstdnt_attachment) * size);
if (*attachments == NULL)
return;
@ -125,5 +126,5 @@ int mstdnt_upload_media(mstdnt_t* api,
void mstdnt_cleanup_attachments(struct mstdnt_attachment* attachment)
{
if (attachment) free(attachment);
if (attachment) mstdnt_free(attachment);
}

View file

@ -15,6 +15,7 @@
#include <string.h>
#include <stdlib.h>
#include <mastodont_hooks.h>
#include <mastodont_chats.h>
#include <mastodont_fetch.h>
#include <mastodont_query.h>
@ -210,7 +211,7 @@ void mstdnt_cleanup_chats(struct mstdnt_chat* chats, size_t len)
if (!chats) return;
for (size_t i = 0; i < len; ++i)
mstdnt_cleanup_chat(chats + i);
free(chats);
mstdnt_free(chats);
}
void mstdnt_cleanup_messages(struct mstdnt_message* messages)

View file

@ -15,6 +15,7 @@
#include <string.h>
#include <stdlib.h>
#include <mastodont_hooks.h>
#include <mastodont_json_helper.h>
#include <mastodont_emoji.h>
@ -72,7 +73,7 @@ void _mstdnt_val_emoji_reactions_call(cJSON* v, void* _type)
return;
}
*emos = calloc(1, sizeof(struct mstdnt_emoji_reaction) * size);
*emos = mstdnt_calloc(1, sizeof(struct mstdnt_emoji_reaction) * size);
if (*emos == NULL)
return;
@ -100,7 +101,7 @@ void _mstdnt_val_emojis_call(cJSON* v, void* _type)
return;
}
*emos = calloc(1, sizeof(struct mstdnt_emoji) * size);
*emos = mstdnt_calloc(1, sizeof(struct mstdnt_emoji) * size);
if (*emos == NULL)
return;
@ -124,11 +125,11 @@ void mstdnt_cleanup_emoji_reactions(struct mstdnt_emoji_reaction* reactions, siz
if (!reactions) return;
for (i = 0; i < s; ++i)
mstdnt_cleanup_emoji_reaction(reactions + s);
free(reactions);
mstdnt_free(reactions);
}
void mstdnt_cleanup_emojis(struct mstdnt_emoji* emo)
{
if (!emo) return;
free(emo);
mstdnt_free(emo);
}

View file

@ -16,7 +16,8 @@
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include "mastodont_fetch.h"
#include <mastodont_hooks.h>
#include <mastodont_fetch.h>
/* For use with libcurl */
size_t mstdnt_curl_write_callback(char* ptr, size_t _size, size_t nmemb, void* _content)
@ -25,7 +26,7 @@ size_t mstdnt_curl_write_callback(char* ptr, size_t _size, size_t nmemb, void* _
struct mstdnt_fetch_results* res = _content; /* Cast */
char* data;
if ((data = realloc(res->response, res->size + size + 1)) == NULL)
if ((data = mstdnt_realloc(res->response, res->size + size + 1)) == NULL)
{
perror("realloc");
return 0;
@ -41,7 +42,7 @@ size_t mstdnt_curl_write_callback(char* ptr, size_t _size, size_t nmemb, void* _
void mstdnt_fetch_results_cleanup(struct mstdnt_fetch_results* res)
{
free(res->response);
mstdnt_free(res->response);
}
#define TOKEN_STR_SIZE 512

View file

@ -14,6 +14,7 @@
*/
#include <string.h>
#include <mastodont_hooks.h>
#include <mastodont_history.h>
#include <mastodont_json_helper.h>
#include <mastodont_generate.h>
@ -53,7 +54,7 @@ void _mstdnt_val_histories_call(cJSON* v, void* _type)
return;
}
*hist = calloc(1, sizeof(struct mstdnt_history) * size);
*hist = mstdnt_calloc(1, sizeof(struct mstdnt_history) * size);
if (*hist == NULL)
return;

24
src/hooks.c Normal file
View file

@ -0,0 +1,24 @@
/*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <mastodont_hooks.h>
struct _mstdnt_hooks _mstdnt_hooks_def = {
.malloc = malloc,
.free = free,
.calloc = calloc,
.realloc = realloc,
};

View file

@ -15,6 +15,7 @@
#include <string.h>
#include <stdlib.h>
#include <mastodont_hooks.h>
#include <mastodont_list.h>
#include <mastodont_json_helper.h>
#include <mastodont_fetch.h>
@ -315,5 +316,5 @@ int mstdnt_list_get_accounts(mstdnt_t* data,
void mstdnt_cleanup_lists(struct mstdnt_list* lists)
{
free(lists);
mstdnt_free(lists);
}

View file

@ -25,11 +25,6 @@ void mstdnt_cleanup(mstdnt_t* data)
curl_multi_cleanup(data->curl);
}
void mstdnt_free(void* ptr)
{
free(ptr);
}
void mstdnt_storage_cleanup(struct mstdnt_storage* storage)
{
if (storage->needs_cleanup)

View file

@ -13,9 +13,10 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <mastodont_nodeinfo.h>
#include <stdlib.h>
#include <string.h>
#include <mastodont_nodeinfo.h>
#include <mastodont_hooks.h>
#include <mastodont_request.h>
#include <mastodont_json_helper.h>
@ -23,7 +24,7 @@ static void _mstdnt_val_software_malloc_call(cJSON* v, void* _type)
{
struct mstdnt_nodeinfo_software** type = _type;
*type = calloc(1, sizeof(struct mstdnt_nodeinfo_software));
*type = mstdnt_calloc(1, sizeof(struct mstdnt_nodeinfo_software));
// Do json stuff in here, it's only done once
if (*type)
@ -49,7 +50,7 @@ static void _mstdnt_val_metadata_malloc_call(cJSON* v, void* _type)
{
struct mstdnt_nodeinfo_metadata** type = _type;
*type = calloc(1, sizeof(struct mstdnt_nodeinfo_metadata));
*type = mstdnt_calloc(1, sizeof(struct mstdnt_nodeinfo_metadata));
// Do json stuff in here, it's only done once
if (*type)
@ -120,6 +121,6 @@ int mstdnt_get_nodeinfo(mstdnt_t* api,
void mstdnt_cleanup_nodeinfo(struct mstdnt_nodeinfo* nodeinfo)
{
if (!nodeinfo) return;
free(nodeinfo->software);
free(nodeinfo->metadata);
mstdnt_free(nodeinfo->software);
mstdnt_free(nodeinfo->metadata);
}

View file

@ -15,6 +15,7 @@
#include <stdlib.h>
#include <string.h>
#include <mastodont_hooks.h>
#include <mastodont_notification.h>
#include <mastodont_fetch.h>
#include <mastodont_json_helper.h>
@ -49,7 +50,7 @@ static void _mstdnt_val_malloc_notification_pleroma_call(cJSON* v, void* _type)
{
struct mstdnt_notification_pleroma** type = _type;
*type = calloc(1, sizeof(struct mstdnt_notification_pleroma));
*type = mstdnt_calloc(1, sizeof(struct mstdnt_notification_pleroma));
if (*type)
mstdnt_notification_pleroma_json(*type, v->child);
@ -231,7 +232,7 @@ void mstdnt_cleanup_notifications(struct mstdnt_notification* notifs, size_t not
for (i = 0; i < notifs_len; ++i)
mstdnt_cleanup_notification(notifs + i);
free(notifs);
mstdnt_free(notifs);
}
void mstdnt_cleanup_notification(struct mstdnt_notification* notif)
@ -239,14 +240,14 @@ void mstdnt_cleanup_notification(struct mstdnt_notification* notif)
if (notif->account)
{
mstdnt_cleanup_account(notif->account);
free(notif->account);
mstdnt_free(notif->account);
}
if (notif->status)
{
mstdnt_cleanup_status(notif->status);
free(notif->status);
mstdnt_free(notif->status);
}
free(notif->pleroma);
mstdnt_free(notif->pleroma);
}
const char* mstdnt_notification_t_to_str(mstdnt_notification_t type)

View file

@ -15,6 +15,7 @@
#include <string.h>
#include <stdlib.h>
#include <mastodont_hooks.h>
#include <mastodont_query.h>
#define CONV_SIZE 64
@ -48,7 +49,7 @@ char* _mstdnt_query_string(mstdnt_t* data,
res_len = src_l+1;
else
res_len = 0;
char* result = malloc(res_len);
char* result = mstdnt_malloc(res_len);
if (src_l)
strcpy(result, src);
@ -73,7 +74,7 @@ char* _mstdnt_query_string(mstdnt_t* data,
if (params[i].type == _MSTDNT_QUERY_ARRAY && arr_ind == 0)
{
size_t str_s = strlen(params[i].key);
key_ptr = malloc(str_s+3); /* 2 "[]" + 1 \0 */
key_ptr = mstdnt_malloc(str_s+3); /* 2 "[]" + 1 \0 */
strcpy(key_ptr, params[i].key);
strcpy(key_ptr+str_s, "[]");
}
@ -134,7 +135,7 @@ char* _mstdnt_query_string(mstdnt_t* data,
res_len += 1 + key_len + 1 + val_len;
result = realloc(result, res_len + 1); /* NULL terminator space */
result = mstdnt_realloc(result, res_len + 1); /* NULL terminator space */
if (res_count - 1 != 0)
result[res_prev-1] = '&';
@ -160,7 +161,7 @@ char* _mstdnt_query_string(mstdnt_t* data,
if (arr_ind >= params[i].value.a.arr_len)
{
arr_ind = 0;
free(key_ptr);
mstdnt_free(key_ptr);
}
else {
--i; /* Flip flop i */

View file

@ -14,6 +14,7 @@
*/
#include <stdlib.h>
#include <mastodont_hooks.h>
#include <mastodont_relationship.h>
#include <mastodont_json_helper.h>
#include <mastodont_query.h>
@ -130,5 +131,5 @@ int mstdnt_get_relationships(mstdnt_t* data, struct mstdnt_args* m_args,
void mstdnt_cleanup_relationships(struct mstdnt_relationship* rels)
{
if (!rels) return;
free(rels);
mstdnt_free(rels);
}

View file

@ -17,6 +17,7 @@
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <mastodont_hooks.h>
#include <mastodont_request.h>
#include <mastodont_query.h>
#include <mastodont_error.h>
@ -152,8 +153,8 @@ cleanup:
// Note: the fetch removed the handle from our multi handle
curl_easy_cleanup(curl);
if (args->params_post && args->request_type == CURLOPT_POST) free(post);
if (args->params_post && args->request_type == CURLOPT_POST) mstdnt_free(post);
/* Only free if params_query set */
if (args->params_query) free(url_query);
if (args->params_query) mstdnt_free(url_query);
return res;
}

View file

@ -15,6 +15,7 @@
#include <string.h>
#include <stdlib.h>
#include <mastodont_hooks.h>
#include <mastodont_json_helper.h>
#include <mastodont_status.h>
#include <mastodont_account.h>
@ -39,7 +40,7 @@ void _mstdnt_val_malloc_status_call(cJSON* v, void* _type)
if (!(v->child))
return;
*type = calloc(1, sizeof(struct mstdnt_status));
*type = mstdnt_calloc(1, sizeof(struct mstdnt_status));
if (*type)
mstdnt_status_json(*type, v->child);
@ -364,7 +365,7 @@ int mstdnt_status_context_json(struct mstdnt_status* statuses_before[],
if (cJSON_GetArraySize(v) <= 0)
continue;
*stat_ptr = calloc(1, cJSON_GetArraySize(v) * sizeof(struct mstdnt_status));
*stat_ptr = mstdnt_calloc(1, cJSON_GetArraySize(v) * sizeof(struct mstdnt_status));
if (*stat_ptr == NULL)
return 1;
@ -569,9 +570,9 @@ void mstdnt_cleanup_status(struct mstdnt_status* status)
if (status->reblog)
{
mstdnt_cleanup_status(status->reblog);
free(status->reblog);
mstdnt_free(status->reblog);
}
free(status->application);
mstdnt_free(status->application);
}
void mstdnt_cleanup_statuses(struct mstdnt_status* statuses, size_t s)
@ -582,5 +583,5 @@ void mstdnt_cleanup_statuses(struct mstdnt_status* statuses, size_t s)
{
mstdnt_cleanup_status(statuses + i);
}
free(statuses);
mstdnt_free(statuses);
}

View file

@ -15,6 +15,7 @@
#include <string.h>
#include <stdlib.h>
#include <mastodont_hooks.h>
#include <mastodont_tag.h>
#include <mastodont_history.h>
#include <mastodont_generate.h>
@ -47,7 +48,7 @@ GENERATE_JSON_ARRAY_FUNC(mstdnt_tags_json, struct mstdnt_tag, mstdnt_tag_json)
void mstdnt_cleanup_tag(struct mstdnt_tag* tag)
{
if (tag->history)
free(tag->history);
mstdnt_free(tag->history);
}
void mstdnt_cleanup_tags(struct mstdnt_tag* tags, size_t s)
@ -57,5 +58,5 @@ void mstdnt_cleanup_tags(struct mstdnt_tag* tags, size_t s)
{
mstdnt_cleanup_tag(tags + i);
}
free(tags);
mstdnt_free(tags);
}