Add allocators and free functions

This commit is contained in:
Alex D. 2021-02-20 17:16:41 +00:00
parent 335a078861
commit 44451a6c88
Signed by: caskd
GPG Key ID: F92BA85F61F4C173
5 changed files with 242 additions and 30 deletions

View File

@ -23,7 +23,8 @@ set(UIRC_SOURCE
src/assemblers/user.c
src/tokenizers/message.c
src/tokenizers/user.c
src/memory/memory.c
src/memory/allocators.c
src/memory/free.c
)
set(UIRC_HEADERS

172
src/memory/allocators.c Normal file
View File

@ -0,0 +1,172 @@
/*
* This file is part of uIRC. (https://git.redxen.eu/caskd/uIRC)
* Copyright (c) 2019, 2020 Alex-David Denes
*
* uIRC is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* uIRC 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with uIRC. If not, see <https://www.gnu.org/licenses/>.
*/
#include "../types.h" // IRC_*
#include "memory.h"
#include <assert.h> // assert()
#include <corelibs/llist.h> // llist_t remove_linked_list_elem()
#include <corelibs/stringext.h> // malloc_string()
#include <stdarg.h> // va_*() va_list
#include <stdlib.h> // free()
#include <string.h> // strcpy()
IRC_Tag*
uirc_malloc_tag(const char* key, const char* value)
{
IRC_Tag* ret;
assert(key != NULL);
if ((ret = malloc(sizeof(IRC_Tag))) != NULL) {
memset(ret, 0, sizeof(IRC_Tag));
if ((ret->key = malloc_string(key, strlen(key))) == NULL) {
uirc_free_tag(ret);
return NULL;
}
if (value != NULL) {
if ((ret->value = malloc_string(value, strlen(value))) == NULL) {
uirc_free_tag(ret);
return NULL;
}
}
return ret;
}
return NULL;
}
IRC_Capability*
uirc_malloc_capability(const char* name)
{
IRC_Capability* ret;
assert(name != NULL);
if ((ret = malloc(sizeof(IRC_Capability))) != NULL) {
memset(ret, 0, sizeof(IRC_Capability));
if ((ret->name = malloc_string(name, strlen(name))) == NULL) {
uirc_free_capability(ret);
return NULL;
}
return ret;
}
return NULL;
}
IRC_User*
uirc_malloc_user(const char* nick, const char* user, const char* real)
{
IRC_User* ret;
if ((ret = malloc(sizeof(IRC_User))) != NULL) {
memset(ret, 0, sizeof(IRC_User));
if (nick != NULL) {
if ((ret->nick = malloc_string(nick, strlen(nick))) == NULL) {
uirc_free_user(ret);
return NULL;
}
}
if (user != NULL) {
if ((ret->user = malloc_string(user, strlen(user))) == NULL) {
uirc_free_user(ret);
return NULL;
}
}
if (real != NULL) {
if ((ret->real = malloc_string(real, strlen(real))) == NULL) {
uirc_free_user(ret);
return NULL;
}
}
return ret;
}
return NULL;
}
IRC_Message*
uirc_malloc_message(const char* command, ...)
{
IRC_Message* ret;
assert(command != NULL);
if ((ret = malloc(sizeof(IRC_Capability))) != NULL) {
memset(ret, 0, sizeof(IRC_Capability));
if ((ret->command = malloc_string(command, strlen(command))) == NULL) {
uirc_free_message(ret);
return NULL;
}
return ret;
}
va_list ap;
va_start(ap, command);
for (unsigned short i = 0; i < IRC_MAXARGS; i++) {
const char* carg = NULL;
if ((carg = va_arg(ap, const char*)) != NULL) {
if ((ret->args[i] = malloc_string(carg, strlen(carg))) == NULL) {
uirc_free_message(ret);
return NULL;
}
}
}
va_end(ap);
return NULL;
}
IRC_Buffer*
uirc_malloc_buffer(const char* name, const char* topic, const char* key)
{
IRC_Buffer* ret;
assert(name != NULL);
if ((ret = malloc(sizeof(IRC_Buffer))) != NULL) {
memset(ret, 0, sizeof(IRC_Buffer));
if ((ret->name = malloc_string(name, strlen(name))) == NULL) {
uirc_free_buffer(ret);
return NULL;
}
if (topic != NULL) {
if ((ret->topic = malloc_string(topic, strlen(topic))) == NULL) {
uirc_free_buffer(ret);
return NULL;
}
}
if (key != NULL) {
if ((ret->key = malloc_string(key, strlen(key))) == NULL) {
uirc_free_buffer(ret);
return NULL;
}
}
return ret;
}
return NULL;
}
IRC_Network*
uirc_malloc_network(const char* addr, const char* svc)
{
IRC_Network* ret;
assert(addr != NULL);
assert(svc != NULL);
if ((ret = malloc(sizeof(IRC_Network))) != NULL) {
memset(ret, 0, sizeof(IRC_Network));
if ((ret->addr = malloc_string(addr, strlen(addr))) == NULL) {
uirc_free_network(ret);
return NULL;
}
if ((ret->svc = malloc_string(svc, strlen(svc))) == NULL) {
uirc_free_network(ret);
return NULL;
}
return ret;
}
return NULL;
}

View File

@ -16,9 +16,8 @@
* along with uIRC. If not, see <https://www.gnu.org/licenses/>.
*/
#include "memory.h"
#include "../types.h" // IRC_*
#include "memory.h"
#include <assert.h> // assert()
#include <corelibs/llist.h> // llist_t remove_linked_list_elem()
@ -26,22 +25,6 @@
#include <stdlib.h> // free()
#include <string.h> // strcpy()
// TODO: Indicate failure of this at any step and free previous if that happens
IRC_User*
uirc_malloc_user(const char* nick, const char* user, const char* real)
{
IRC_User* ret;
if ((ret = malloc(sizeof(IRC_User))) != NULL) {
ret->nick = (nick == NULL) ? NULL : malloc_string(nick, strlen(nick));
ret->user = (user == NULL) ? NULL : malloc_string(user, strlen(user));
ret->real = (real == NULL) ? NULL : malloc_string(real, strlen(real));
return ret;
} else
return NULL;
}
// TODO: uirc_malloc_tag_list uirc_malloc_tag
void
uirc_free_tag(IRC_Tag* t)
{
@ -50,6 +33,13 @@ uirc_free_tag(IRC_Tag* t)
free(t->value);
}
void
uirc_free_capability(IRC_Capability* c)
{
assert(c != NULL);
free(c->name);
}
void
uirc_free_user(IRC_User* u)
{
@ -68,8 +58,7 @@ uirc_free_message(IRC_Message* m)
for (unsigned short i = 0; m->args[i] != NULL; i++) free(m->args[i]);
llist_t* t = m->tag_list;
for (; t != NULL; t = t->next) {
for (llist_t* t = m->tag_list; t != NULL; t = t->next) {
uirc_free_tag(t->content);
remove_linked_list_elem(t);
}
@ -80,3 +69,46 @@ uirc_free_message(IRC_Message* m)
}
}
void
uirc_free_buffer(IRC_Buffer* b)
{
assert(b != NULL);
free(b->name);
free(b->key);
free(b->topic);
for (llist_t* t = b->user_list; t != NULL; t = t->next) {
uirc_free_user(t->content);
remove_linked_list_elem(t);
}
for (llist_t* t = b->message_list; t != NULL; t = t->next) {
uirc_free_message(t->content);
remove_linked_list_elem(t);
}
}
void
uirc_free_network(IRC_Network* n)
{
assert(n != NULL);
free(n->addr);
free(n->svc);
uirc_free_user(n->user);
free(n->user);
for (llist_t* t = n->buf_list; t != NULL; t = t->next) {
uirc_free_buffer(t->content);
remove_linked_list_elem(t);
}
#ifdef UIRC_FEATURE_IRCV3
for (llist_t* t = n->cap_list; t != NULL; t = t->next) {
uirc_free_capability(t->content);
remove_linked_list_elem(t);
}
#endif /* UIRC_FEATURE_IRCV3 */
}

View File

@ -20,15 +20,22 @@
#include "../types.h" // IRC_Tag IRC_User IRC_Meesage
#include <corelibs/llist.h> // llist_t
#ifndef UIRC_GUARD_PUBLIC_MEMORY
#define UIRC_GUARD_PUBLIC_MEMORY
IRC_User* uirc_malloc_user(const char* nick, const char* user, const char* real);
void uirc_free_tag(IRC_Tag* t);
void uirc_free_user(IRC_User* u);
void uirc_free_message(IRC_Message* m);
IRC_Tag* uirc_malloc_tag(const char* key, const char* value);
IRC_Capability* uirc_malloc_capability(const char* name);
IRC_User* uirc_malloc_user(const char* nick, const char* user, const char* real);
IRC_Message* uirc_malloc_message(const char* command, ...);
IRC_Buffer* uirc_malloc_buffer(const char* name, const char* topic, const char* key);
IRC_Network* uirc_malloc_network(const char* addr, const char* svc);
void uirc_free_tag(IRC_Tag* t);
void uirc_free_capability(IRC_Capability* c);
void uirc_free_user(IRC_User* u);
void uirc_free_message(IRC_Message* m);
void uirc_free_buffer(IRC_Buffer* b);
void uirc_free_network(IRC_Network* n);
#endif /* UIRC_GUARD_PUBLIC_MEMORY */

View File

@ -69,9 +69,9 @@ typedef struct {
} IRC_Buffer;
typedef struct {
char * addr, *svc;
IRC_User* user;
IRC_Buffer* buf_list;
char * addr, *svc;
IRC_User* user;
llist_t* buf_list;
#ifdef UIRC_FEATURE_IRCV3
llist_t* cap_list;
#endif /* UIRC_FEATURE_IRCV3 */