Use cool memory hooks (bloat)

FossilOrigin-Name: d46dabde38291803f6832582a832ee636ef6cc42de5191854b94ec38a35bcd32
This commit is contained in:
nekobit 2022-10-17 20:17:07 +00:00
parent 64f72cc20f
commit f2107a803a
23 changed files with 83 additions and 79 deletions

View file

@ -38,7 +38,7 @@ void content_about(PATH_ARGS)
};
render_base_page(&b, req, ssn, api);
Safefree(dup);
tb_free(dup);
}
@ -61,6 +61,6 @@ void content_about_license(PATH_ARGS)
};
render_base_page(&b, req, ssn, api);
Safefree(dup);
tb_free(dup);
}

View file

@ -283,7 +283,7 @@ static void fetch_account_page(FCGX_Request* req,
mstdnt_cleanup_relationships(relationships);
mstdnt_storage_cleanup(&storage);
mstdnt_storage_cleanup(&relations_storage);
Safefree(data);
tb_free(data);
}
void content_account_statuses(PATH_ARGS)
@ -436,7 +436,7 @@ void content_account_blocked(PATH_ARGS)
};
render_base_page(&b, req, ssn, api);
Safefree(result);
tb_free(result);
}
void content_account_muted(PATH_ARGS)
@ -468,7 +468,7 @@ void content_account_muted(PATH_ARGS)
};
render_base_page(&b, req, ssn, api);
Safefree(result);
tb_free(result);
}
void content_account_favourites(PATH_ARGS)

View file

@ -45,10 +45,10 @@ int try_upload_media(struct mstdnt_storage** storage,
return 1;
if (media_ids)
*media_ids = safemalloc(sizeof(char*) * size);
*media_ids = tb_malloc(sizeof(char*) * size);
*attachments = safemalloc(sizeof(struct mstdnt_attachment) * size);
*storage = safecalloc(1, sizeof(struct mstdnt_storage) * size);
*attachments = tb_malloc(sizeof(struct mstdnt_attachment) * size);
*storage = tb_calloc(1, sizeof(struct mstdnt_storage) * size);
for (int i = 0; i < size; ++i)
{
@ -72,26 +72,26 @@ int try_upload_media(struct mstdnt_storage** storage,
{
for (size_t j = 0; j < i; ++j)
{
if (media_ids) Safefree((*media_ids)[j]);
if (media_ids) tb_free((*media_ids)[j]);
mstdnt_storage_cleanup(*storage + j);
}
if (media_ids)
{
Safefree(*media_ids);
tb_free(*media_ids);
*media_ids = NULL;
}
Safefree(*attachments);
tb_free(*attachments);
*attachments = NULL;
Safefree(*storage);
tb_free(*storage);
*storage = NULL;
return 1;
}
if (media_ids)
{
(*media_ids)[i] = safemalloc(strlen((*attachments)[i].id)+1);
(*media_ids)[i] = tb_malloc(strlen((*attachments)[i].id)+1);
strcpy((*media_ids)[i], (*attachments)[i].id);
}
}
@ -104,7 +104,7 @@ void cleanup_media_storages(struct session* ssn, struct mstdnt_storage* storage)
if (!FILES_READY(ssn)) return;
for (size_t i = 0; i < keyfile(ssn->post.files).array_size; ++i)
mstdnt_storage_cleanup(storage + i);
Safefree(storage);
tb_free(storage);
}
void cleanup_media_ids(struct session* ssn, char** media_ids)
@ -112,8 +112,8 @@ void cleanup_media_ids(struct session* ssn, char** media_ids)
if (!FILES_READY(ssn)) return;
if (!media_ids) return;
for (size_t i = 0; i < keyfile(ssn->post.files).array_size; ++i)
Safefree(media_ids[i]);
Safefree(media_ids);
tb_free(media_ids[i]);
tb_free(media_ids);
}
HV* perlify_attachment(const struct mstdnt_attachment* const attachment)
@ -153,7 +153,7 @@ void api_attachment_create(PATH_ARGS)
{
string = cJSON_Print(root);
send_result(req, NULL, "application/json", string, 0);
Safefree(string);
tb_free(string);
}
else
send_result(req, NULL, "application/json", "{\"status\":\"Couldn't\"}", 0);
@ -161,6 +161,6 @@ void api_attachment_create(PATH_ARGS)
// Cleanup media stuff
cleanup_media_storages(ssn, att_storage);
cleanup_media_ids(ssn, media_ids);
Safefree(attachments);
tb_free(attachments);
cJSON_Delete(root);
}

View file

@ -92,7 +92,7 @@ void render_base_page(struct base_page* page, FCGX_Request* req, struct session*
mstdnt_cleanup_notifications(notifs, notifs_len);
mstdnt_storage_cleanup(&storage);
Safefree(dup);
tb_free(dup);
}
void send_result(FCGX_Request* req, char* status, char* content_type, char* data, size_t data_len)

View file

@ -71,7 +71,7 @@ void content_chats(PATH_ARGS)
// Cleanup
mstdnt_storage_cleanup(&storage);
mstdnt_cleanup_chats(chats, chats_len);
Safefree(dup);
tb_free(dup);
}
void content_chat_view(PATH_ARGS)
@ -127,7 +127,7 @@ void content_chat_view(PATH_ARGS)
mstdnt_storage_cleanup(&storage_chat);
mstdnt_cleanup_chat(&chat);
mstdnt_cleanup_messages(messages);
Safefree(dup);
tb_free(dup);
}
HV* perlify_chat(const struct mstdnt_chat* chat)

View file

@ -36,7 +36,7 @@ char* read_cookies_env(REQUEST_T req, struct cookie_values* cookies)
if (!cookies_env)
return NULL;
char* cookies_str = safemalloc(strlen(cookies_env) + 1);
char* cookies_str = tb_malloc(strlen(cookies_env) + 1);
if (!cookies_str)
{
perror("malloc");

View file

@ -29,7 +29,7 @@ int evasprintf(char** ret, const char* format, va_list ap)
int sz = vsnprintf(NULL, 0, format, ap);
va_end(ap);
*ret = safemalloc(sz + 1);
*ret = tb_malloc(sz + 1);
if(*ret == NULL)
{
perror("malloc");

View file

@ -72,7 +72,7 @@ void content_emoji_picker(PATH_ARGS)
send_result(req, NULL, NULL, picker, 0);
Safefree(picker);
tb_free(picker);
}
char* construct_emoji_picker(char* status_id, size_t* size)
@ -119,7 +119,7 @@ char* construct_emoji_picker(char* status_id, size_t* size)
// Cleanup
for (size_t i = 0; i < EMO_CAT_LEN; ++i)
Safefree(emojis[i]);
tb_free(emojis[i]);
return dup;
}

View file

@ -20,6 +20,7 @@
#define GLOBAL_PERL_H
#include <EXTERN.h>
#include <perl.h>
#include "memory.h"
#include <pthread.h>
/* SV* tmpsv = newSV(0); \ */

View file

@ -47,13 +47,13 @@ void key_files(char* val, struct file_content* form, struct key* arg)
struct file_array* arr = &(arg->type.f);
char* content_cpy;
arr->content = saferealloc(arr->content,
arr->content = tb_realloc(arr->content,
sizeof(struct file_content) * ++(arr->array_size));
if (!(arr->content))
return;
// Make a copy so we can remember it later
if (!(content_cpy = safemalloc(form->content_size+1)))
if (!(content_cpy = tb_malloc(form->content_size+1)))
return;
memcpy(content_cpy, val, form->content_size+1);

View file

@ -76,7 +76,7 @@ void content_lists(PATH_ARGS)
// Cleanup
mstdnt_storage_cleanup(&storage);
mstdnt_cleanup_lists(lists);
Safefree(dup);
tb_free(dup);
}
void list_edit(PATH_ARGS)

View file

@ -105,7 +105,7 @@ void content_login_oauth(PATH_ARGS)
PRINTF("Set-Cookie: client_secret=%s; Path=/; Max-Age=3153600\r\n", app.client_secret);
redirect(req, REDIRECT_303, url);
Safefree(url);
tb_free(url);
curl_free(encode_id);
}
}
@ -117,7 +117,7 @@ void content_login_oauth(PATH_ARGS)
mstdnt_storage_cleanup(&storage);
mstdnt_storage_cleanup(&oauth_storage);
if (urlify_redirect_url) Safefree(urlify_redirect_url);
if (urlify_redirect_url) tb_free(urlify_redirect_url);
if (decode_url) curl_free(decode_url);
}
@ -192,7 +192,7 @@ void content_login(PATH_ARGS)
PUT("Set-Cookie: instance_url=; Path=/; Max-Age=-1\r\n");
apply_access_token(req, token.access_token);
Safefree(url_link);
tb_free(url_link);
return;
}
}
@ -201,7 +201,7 @@ void content_login(PATH_ARGS)
{
// Restore and cleanup, an error occured
m_args.url = orig_url;
Safefree(url_link);
tb_free(url_link);
}
}
@ -229,5 +229,5 @@ void content_login(PATH_ARGS)
// Cleanup
mstdnt_storage_cleanup(&storage);
mstdnt_storage_cleanup(&oauth_store);
Safefree(page);
tb_free(page);
}

View file

@ -19,6 +19,7 @@
#include "global_perl.h"
#include <pthread.h>
#include <string.h>
#include "memory.h"
#include <mastodont.h>
#include <stdlib.h>
#include "../config.h"
@ -127,6 +128,8 @@ static struct path_info paths[] = {
static void application(mastodont_t* api, REQUEST_T req)
{
propagate_memory();
// Default config
struct session ssn = {
.config = {
@ -174,9 +177,9 @@ static void application(mastodont_t* api, REQUEST_T req)
handle_paths(req, &ssn, api, paths, sizeof(paths)/sizeof(paths[0]));
// Cleanup
if (cookies_str) Safefree(cookies_str);
if (post_str) Safefree(post_str);
if (get_str) Safefree(get_str);
if (cookies_str) tb_free(cookies_str);
if (post_str) tb_free(post_str);
if (get_str) tb_free(get_str);
free_files(&(keyfile(ssn.post.files)));
if (ssn.logged_in) mstdnt_cleanup_account(&(ssn.acct));
mstdnt_storage_cleanup(&(ssn.acct_storage));
@ -251,11 +254,11 @@ int main(int argc, char **argv, char **env)
// Setup mstdnt hooks to use Perl functions
struct mstdnt_hooks hooks = {
.malloc = safemalloc,
// Not sure how this differs from Safefree? That's undefined... (but used elsewhere in the code just fine)
.free = safefree,
.calloc = safecalloc,
.realloc = saferealloc,
.malloc = tb_malloc,
// Not sure how this differs from tb_free? That's undefined... (but used elsewhere in the code just fine)
.free = tb_free,
.calloc = tb_calloc,
.realloc = tb_realloc,
};
mstdnt_set_hooks(&hooks);

View file

@ -34,7 +34,7 @@ char* get_mime_boundary(char* content_type_str, char** bound)
char* content = content_type_str;
// Data gets changed in place
char* content_type = safemalloc(strlen(content)+1);
char* content_type = tb_malloc(strlen(content)+1);
if (!content_type)
{
perror("malloc");
@ -67,7 +67,7 @@ char* get_mime_boundary(char* content_type_str, char** bound)
return content_type;
error:
Safefree(content_type);
tb_free(content_type);
return NULL;
}

View file

@ -80,7 +80,7 @@ void content_notifications(PATH_ARGS)
render_base_page(&b, req, ssn, api);
mstdnt_storage_cleanup(&storage);
mstdnt_cleanup_notifications(notifs, notifs_len);
Safefree(dup);
tb_free(dup);
}
void content_notifications_compact(PATH_ARGS)
@ -125,7 +125,7 @@ void content_notifications_compact(PATH_ARGS)
mstdnt_storage_cleanup(&storage);
mstdnt_cleanup_notifications(notifs, notifs_len);
Safefree(page);
tb_free(page);
}
void content_notifications_clear(PATH_ARGS)

View file

@ -60,7 +60,7 @@ void content_config_general(PATH_ARGS)
render_base_page(&b, req, ssn, api);
// Cleanup
Safefree(dup);
tb_free(dup);
}
@ -84,7 +84,7 @@ void content_config_appearance(PATH_ARGS)
render_base_page(&b, req, ssn, api);
// Cleanup
Safefree(dup);
tb_free(dup);
}
void content_config(PATH_ARGS)

View file

@ -63,11 +63,11 @@ int parse_path(REQUEST_T req,
read_len = (size_t)after_str - (size_t)(p2 + i);
// Copy in new data from the string we just read
tmp = safemalloc(read_len+1);
tmp = tb_malloc(read_len+1);
strncpy(tmp, after_str - read_len, read_len);
tmp[read_len] = '\0';
// Add our new string
data = saferealloc(data, ++size * sizeof(tmp));
data = tb_realloc(data, ++size * sizeof(tmp));
data[size-1] = tmp;
// Move ahead (-1 because we move again)
i += read_len - 1;
@ -95,9 +95,9 @@ breakpt:
// Cleanup
for (size_t i = 0; i < size; ++i)
{
Safefree(data[i]);
tb_free(data[i]);
}
if (data) Safefree(data);
if (data) tb_free(data);
return res;
}

View file

@ -43,7 +43,7 @@ char* read_get_data(REQUEST_T req, struct get_values* query)
if (query_string)
{
get_query = safemalloc(strlen(query_string) + 1);
get_query = tb_malloc(strlen(query_string) + 1);
if (!get_query)
{
perror("malloc");
@ -135,7 +135,7 @@ char* read_post_data(REQUEST_T req, struct post_values* post)
char* mime_boundary;
char* mime_mem = get_mime_boundary(GET_ENV("CONTENT_TYPE", req), &mime_boundary);
int content_length = atoi(GET_ENV("CONTENT_LENGTH", req));
post_query = safemalloc(content_length + 1);
post_query = tb_malloc(content_length + 1);
if (!post_query)
{
perror("malloc");
@ -188,7 +188,7 @@ char* read_post_data(REQUEST_T req, struct post_values* post)
}
while (p_query_read);
if (mime_mem) Safefree(mime_mem);
if (mime_mem) tb_free(mime_mem);
}
// Free me afterwards!
@ -226,7 +226,7 @@ char* try_handle_post(REQUEST_T req, void (*call)(struct http_query_info*, void*
if (request_method && (strcmp("POST", request_method) == 0))
{
int content_length = atoi(GET_ENV("CONTENT_LENGTH", req));
post_query = safemalloc(content_length + 1);
post_query = tb_malloc(content_length + 1);
if (!post_query)
{
puts("Malloc error!");
@ -265,9 +265,9 @@ void free_files(struct file_array* files)
struct file_content* content = files->content;
for (size_t i = 0; i < files->array_size; ++i)
{
Safefree(content[i].content);
tb_free(content[i].content);
}
Safefree(content);
tb_free(content);
}
// TODO use hvstores_XXX macros

View file

@ -70,7 +70,7 @@ void content_search_all(PATH_ARGS)
redirect(req, REDIRECT_303, url);
break;
}
Safefree(url);
tb_free(url);
curl_free(query);
return;
}
@ -99,7 +99,7 @@ void content_search_all(PATH_ARGS)
mstdnt_cleanup_search_results(&results);
mstdnt_storage_cleanup(&storage);
Safefree(dup);
tb_free(dup);
}
void content_search_statuses(PATH_ARGS)
@ -146,7 +146,7 @@ void content_search_statuses(PATH_ARGS)
mstdnt_cleanup_search_results(&results);
mstdnt_storage_cleanup(&storage);
Safefree(dup);
tb_free(dup);
}
void content_search_accounts(PATH_ARGS)
@ -192,7 +192,7 @@ void content_search_accounts(PATH_ARGS)
mstdnt_cleanup_search_results(&results);
mstdnt_storage_cleanup(&storage);
Safefree(dup);
tb_free(dup);
}
void content_search_hashtags(PATH_ARGS)
@ -220,7 +220,7 @@ void content_search_hashtags(PATH_ARGS)
mstdnt_cleanup_search_results(&results);
mstdnt_storage_cleanup(&storage);
// Safefree(dup);
// tb_free(dup);
}
HV* perlify_search_results(struct mstdnt_search_results* results)

View file

@ -70,7 +70,7 @@ int try_post_status(struct session* ssn, mastodont_t* api)
json_ids_len = cJSON_GetArraySize(json_ids);
if (json_ids_len)
{
media_ids = safemalloc(json_ids_len * sizeof(char*));
media_ids = tb_malloc(json_ids_len * sizeof(char*));
// TODO error
cJSON* id;
int i = 0;
@ -110,11 +110,11 @@ int try_post_status(struct session* ssn, mastodont_t* api)
cleanup_media_storages(ssn, att_storage);
if (json_ids)
Safefree(media_ids);
tb_free(media_ids);
else
cleanup_media_ids(ssn, media_ids);
Safefree(attachments);
tb_free(attachments);
if (json_ids) cJSON_Delete(json_ids);
return 0;
}
@ -335,7 +335,7 @@ void content_status_interactions(FCGX_Request* req,
render_base_page(&page, req, ssn, api);
// Cleanup
Safefree(dup);
tb_free(dup);
}
void content_status(PATH_ARGS, uint8_t flags)
@ -402,8 +402,8 @@ void content_status(PATH_ARGS, uint8_t flags)
mstdnt_cleanup_status(&status);
mstdnt_storage_cleanup(&storage);
mstdnt_storage_cleanup(&status_storage);
Safefree(dup);
Safefree(picker);
tb_free(dup);
tb_free(picker);
}
void notice_redirect(PATH_ARGS)
@ -411,7 +411,7 @@ void notice_redirect(PATH_ARGS)
char* url;
easprintf(&url, "%s/status/%s", config_url_prefix, data[0]);
redirect(req, REDIRECT_303, url);
Safefree(url);
tb_free(url);
}
HV* perlify_status_pleroma(const struct mstdnt_status_pleroma* pleroma)

View file

@ -111,7 +111,7 @@ char* strrepl(char* source, char* find, char* repl, int flags)
str_size += curr - last;
// Create and copy
result = saferealloc(result, str_size + (!is_last ? repl_len : 0) + 1);
result = tb_realloc(result, str_size + (!is_last ? repl_len : 0) + 1);
strncpy(result + last_str_size, last, curr - last);
if (!is_last)
{
@ -135,9 +135,9 @@ char* sanitize_html(char* html)
char* left = strrepl(amp, "<", "&lt;", STRREPL_ALL);
char* right = strrepl(left, ">", "&gt;", STRREPL_ALL);
char* quot = strrepl(right, "\"", "&quot;", STRREPL_ALL);
if (quot != right && right != html && right != left) Safefree(right);
if (left != html && left != amp) Safefree(left);
if (amp != html) Safefree(amp);
if (quot != right && right != html && right != left) tb_free(right);
if (left != html && left != amp) tb_free(left);
if (amp != html) tb_free(amp);
return quot;
}

View file

@ -43,24 +43,24 @@ char* construct_func_strings(char* (*func)(void*, size_t, size_t*),
if (parse_size == -1) /* Malloc error */
{
if (result) Safefree(result);
if (result) tb_free(result);
return NULL;
}
last_parse_size = curr_parse_size;
curr_parse_size += parse_size;
result = saferealloc(result, curr_parse_size + 1);
result = tb_realloc(result, curr_parse_size + 1);
if (result == NULL)
{
perror("malloc");
Safefree(res_html);
tb_free(res_html);
return NULL;
}
// Copy res_html to result in correct position
strncpy(result + last_parse_size, res_html, parse_size);
// Cleanup
Safefree(res_html);
tb_free(res_html);
}
if (result)

View file

@ -75,7 +75,7 @@ void content_timeline(REQUEST_T req,
// Cleanup
mstdnt_storage_cleanup(storage);
mstdnt_cleanup_statuses(statuses, statuses_len);
Safefree(dup);
tb_free(dup);
}
void tl_home(REQUEST_T req, struct session* ssn, mastodont_t* api, int local)
@ -221,7 +221,7 @@ void tl_tag(REQUEST_T req, struct session* ssn, mastodont_t* api, char* tag_id)
easprintf(&header, "Hashtag - #%s", tag_id);
content_timeline(req, ssn, api, &storage, statuses, statuses_len, BASE_CAT_NONE, header, 0, 0);
Safefree(header);
tb_free(header);
}
void content_tl_home(PATH_ARGS)