From 5287e5c824b9fad789bf525ea1c2b41cd80e9ec5 Mon Sep 17 00:00:00 2001 From: nekobit Date: Sat, 15 Oct 2022 20:19:25 +0000 Subject: [PATCH] Hooks FossilOrigin-Name: 80ef0c70a70f51d6798855f7c95c4e14f4bca86ae092309195ada7a5be657023 --- include/mastodont_hooks.h | 36 ++++++++++++++++++++++++++++++++++++ src/account.c | 5 +++-- src/application.c | 3 ++- src/attachment.c | 5 +++-- src/chats.c | 3 ++- src/emoji.c | 9 +++++---- src/fetch.c | 7 ++++--- src/history.c | 3 ++- src/hooks.c | 24 ++++++++++++++++++++++++ src/list.c | 3 ++- src/mastodont.c | 5 ----- src/nodeinfo.c | 11 ++++++----- src/notification.c | 11 ++++++----- src/query.c | 9 +++++---- src/relationship.c | 3 ++- src/request.c | 5 +++-- src/status.c | 11 ++++++----- src/tag.c | 5 +++-- 18 files changed, 114 insertions(+), 44 deletions(-) create mode 100644 include/mastodont_hooks.h create mode 100644 src/hooks.c diff --git a/include/mastodont_hooks.h b/include/mastodont_hooks.h new file mode 100644 index 0000000..0e887c6 --- /dev/null +++ b/include/mastodont_hooks.h @@ -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 . + */ + +#ifndef MASTODONT_HOOKS_H +#define MASTODONT_HOOKS_H +#include + +#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 diff --git a/src/account.c b/src/account.c index c5d4e4f..58e7668 100644 --- a/src/account.c +++ b/src/account.c @@ -16,6 +16,7 @@ #include #include #include +#include #include #include #include @@ -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); } diff --git a/src/application.c b/src/application.c index 81d5b03..a5c3cc6 100644 --- a/src/application.c +++ b/src/application.c @@ -15,6 +15,7 @@ #include #include +#include #include #include #include @@ -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); diff --git a/src/attachment.c b/src/attachment.c index d8e72b4..c2ae4d8 100644 --- a/src/attachment.c +++ b/src/attachment.c @@ -15,6 +15,7 @@ #include #include +#include #include #include #include @@ -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); } diff --git a/src/chats.c b/src/chats.c index 95ae873..5aaee34 100644 --- a/src/chats.c +++ b/src/chats.c @@ -15,6 +15,7 @@ #include #include +#include #include #include #include @@ -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) diff --git a/src/emoji.c b/src/emoji.c index 2d2317d..1acc940 100644 --- a/src/emoji.c +++ b/src/emoji.c @@ -15,6 +15,7 @@ #include #include +#include #include #include @@ -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); } diff --git a/src/fetch.c b/src/fetch.c index e92f50e..71269bc 100644 --- a/src/fetch.c +++ b/src/fetch.c @@ -16,7 +16,8 @@ #include #include #include -#include "mastodont_fetch.h" +#include +#include /* 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 diff --git a/src/history.c b/src/history.c index 5fb1a95..4cecf55 100644 --- a/src/history.c +++ b/src/history.c @@ -14,6 +14,7 @@ */ #include +#include #include #include #include @@ -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; diff --git a/src/hooks.c b/src/hooks.c new file mode 100644 index 0000000..9750df4 --- /dev/null +++ b/src/hooks.c @@ -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 . + */ + +#include +#include + +struct _mstdnt_hooks _mstdnt_hooks_def = { + .malloc = malloc, + .free = free, + .calloc = calloc, + .realloc = realloc, +}; diff --git a/src/list.c b/src/list.c index 5f8fd51..0fa9c9f 100644 --- a/src/list.c +++ b/src/list.c @@ -15,6 +15,7 @@ #include #include +#include #include #include #include @@ -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); } diff --git a/src/mastodont.c b/src/mastodont.c index 5d3bc95..80a31a2 100644 --- a/src/mastodont.c +++ b/src/mastodont.c @@ -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) diff --git a/src/nodeinfo.c b/src/nodeinfo.c index 0f1fe93..9e9ebae 100644 --- a/src/nodeinfo.c +++ b/src/nodeinfo.c @@ -13,9 +13,10 @@ * along with this program. If not, see . */ -#include #include #include +#include +#include #include #include @@ -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); } diff --git a/src/notification.c b/src/notification.c index 27fa014..79ac9cd 100644 --- a/src/notification.c +++ b/src/notification.c @@ -15,6 +15,7 @@ #include #include +#include #include #include #include @@ -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) diff --git a/src/query.c b/src/query.c index c224967..fbf1cfc 100644 --- a/src/query.c +++ b/src/query.c @@ -15,6 +15,7 @@ #include #include +#include #include #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 */ diff --git a/src/relationship.c b/src/relationship.c index 5d38310..92be546 100644 --- a/src/relationship.c +++ b/src/relationship.c @@ -14,6 +14,7 @@ */ #include +#include #include #include #include @@ -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); } diff --git a/src/request.c b/src/request.c index 783a3ce..84ca494 100644 --- a/src/request.c +++ b/src/request.c @@ -17,6 +17,7 @@ #include #include #include +#include #include #include #include @@ -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; } diff --git a/src/status.c b/src/status.c index 419bbaf..dff69e5 100644 --- a/src/status.c +++ b/src/status.c @@ -15,6 +15,7 @@ #include #include +#include #include #include #include @@ -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); } diff --git a/src/tag.c b/src/tag.c index 4a00da0..82a7664 100644 --- a/src/tag.c +++ b/src/tag.c @@ -15,6 +15,7 @@ #include #include +#include #include #include #include @@ -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); }